7146e88
From 8a382d4ad190cf07cbd6b1fd6b903975134b0cf1 Mon Sep 17 00:00:00 2001
7146e88
From: =?UTF-8?q?Caol=C3=A1n=20McNamara?= <caolanm@redhat.com>
7146e88
Date: Thu, 1 Sep 2016 15:50:20 +0100
7146e88
Subject: [PATCH] Related: rhbz#1362451 avoid recursive ownerchanged handling
7146e88
 during ownerchange
7146e88
7146e88
Change-Id: Id9c12b7ce6458348890d7c7ff7fdb2cd37c4601c
7146e88
---
7146e88
 vcl/unx/gtk3/gtk3gtkinst.cxx | 55 ++++++++++++++++++++++++++------------------
7146e88
 1 file changed, 32 insertions(+), 23 deletions(-)
7146e88
7146e88
diff --git a/vcl/unx/gtk3/gtk3gtkinst.cxx b/vcl/unx/gtk3/gtk3gtkinst.cxx
7146e88
index 7c04b1d..7148882 100644
7146e88
--- a/vcl/unx/gtk3/gtk3gtkinst.cxx
7146e88
+++ b/vcl/unx/gtk3/gtk3gtkinst.cxx
7146e88
@@ -364,6 +364,29 @@ namespace
7146e88
     }
7146e88
 }
7146e88
 
7146e88
+namespace
7146e88
+{
7146e88
+    void ClipboardGetFunc(GtkClipboard *clipboard, GtkSelectionData *selection_data,
7146e88
+                          guint info,
7146e88
+                          gpointer user_data_or_owner)
7146e88
+    {
7146e88
+        VclGtkClipboard* pThis = static_cast<VclGtkClipboard*>(user_data_or_owner);
7146e88
+        pThis->ClipboardGet(clipboard, selection_data, info);
7146e88
+    }
7146e88
+
7146e88
+    void ClipboardClearFunc(GtkClipboard *clipboard, gpointer user_data_or_owner)
7146e88
+    {
7146e88
+        VclGtkClipboard* pThis = static_cast<VclGtkClipboard*>(user_data_or_owner);
7146e88
+        pThis->ClipboardClear(clipboard);
7146e88
+    }
7146e88
+
7146e88
+    void handle_owner_change(GtkClipboard *clipboard, GdkEvent *event, gpointer user_data)
7146e88
+    {
7146e88
+        VclGtkClipboard* pThis = static_cast<VclGtkClipboard*>(user_data);
7146e88
+        pThis->OwnerPossiblyChanged(clipboard, event);
7146e88
+    }
7146e88
+}
7146e88
+
7146e88
 void VclGtkClipboard::OwnerPossiblyChanged(GtkClipboard* clipboard, GdkEvent* /*event*/)
7146e88
 {
7146e88
     if (!m_aContents.is())
7146e88
@@ -376,6 +399,10 @@ void VclGtkClipboard::OwnerPossiblyChanged(GtkClipboard* clipboard, GdkEvent* /*
7146e88
     //our pid, in which case it's us.
7146e88
     bool bSelf = false;
7146e88
 
7146e88
+    //disconnect and reconnect after gtk_clipboard_wait_for_targets to
7146e88
+    //avoid possible recursion
7146e88
+    g_signal_handler_disconnect(clipboard, m_nOwnerChangedSignalId);
7146e88
+
7146e88
     OString sTunnel = "application/x-libreoffice-internal-id-" + getPID();
7146e88
     GdkAtom *targets;
7146e88
     gint n_targets;
7146e88
@@ -394,6 +421,9 @@ void VclGtkClipboard::OwnerPossiblyChanged(GtkClipboard* clipboard, GdkEvent* /*
7146e88
         g_free(targets);
7146e88
     }
7146e88
 
7146e88
+    m_nOwnerChangedSignalId = g_signal_connect(clipboard, "owner-change",
7146e88
+                                               G_CALLBACK(handle_owner_change), this);
7146e88
+
7146e88
     if (!bSelf)
7146e88
     {
7146e88
         //null out m_aContents to return control to the system-one which
7146e88
@@ -486,29 +516,6 @@ void VclToGtkHelper::setSelectionData(const Reference
7146e88
                            aData.getLength());
7146e88
 }
7146e88
 
7146e88
-namespace
7146e88
-{
7146e88
-    void ClipboardGetFunc(GtkClipboard *clipboard, GtkSelectionData *selection_data,
7146e88
-                          guint info,
7146e88
-                          gpointer user_data_or_owner)
7146e88
-    {
7146e88
-        VclGtkClipboard* pThis = static_cast<VclGtkClipboard*>(user_data_or_owner);
7146e88
-        pThis->ClipboardGet(clipboard, selection_data, info);
7146e88
-    }
7146e88
-
7146e88
-    void ClipboardClearFunc(GtkClipboard *clipboard, gpointer user_data_or_owner)
7146e88
-    {
7146e88
-        VclGtkClipboard* pThis = static_cast<VclGtkClipboard*>(user_data_or_owner);
7146e88
-        pThis->ClipboardClear(clipboard);
7146e88
-    }
7146e88
-
7146e88
-    void handle_owner_change(GtkClipboard *clipboard, GdkEvent *event, gpointer user_data)
7146e88
-    {
7146e88
-        VclGtkClipboard* pThis = static_cast<VclGtkClipboard*>(user_data);
7146e88
-        pThis->OwnerPossiblyChanged(clipboard, event);
7146e88
-    }
7146e88
-}
7146e88
-
7146e88
 VclGtkClipboard::VclGtkClipboard(GdkAtom nSelection)
7146e88
     : cppu::WeakComponentImplHelper
7146e88
                                     datatransfer::clipboard::XFlushableClipboard, XServiceInfo>
7146e88
@@ -539,6 +546,7 @@ VclGtkClipboard::~VclGtkClipboard()
7146e88
     if (!m_aGtkTargets.empty())
7146e88
     {
7146e88
         gtk_clipboard_clear(clipboard);
7146e88
+        ClipboardClear(clipboard);
7146e88
     }
7146e88
     assert(m_aGtkTargets.empty());
7146e88
 }
7146e88
@@ -602,6 +610,7 @@ void VclGtkClipboard::setContents(
7146e88
     if (!m_aGtkTargets.empty())
7146e88
     {
7146e88
         gtk_clipboard_clear(clipboard);
7146e88
+        ClipboardClear(clipboard);
7146e88
     }
7146e88
     assert(m_aGtkTargets.empty());
7146e88
     if (m_aContents.is())
7146e88
-- 
7146e88
2.7.4
7146e88