[Xfce4-commits] [apps/xfce4-dict] 38/43: Using gdbus as IPC mechanism

noreply at xfce.org noreply at xfce.org
Tue Nov 1 00:31:51 CET 2016


This is an automated email from the git hooks/post-receive script.

andre pushed a commit to branch master
in repository apps/xfce4-dict.

commit aa47ee82a3d946af9f5daa91f9b7a77f3138a167
Author: Andre Miranda <andre42m at gmail.com>
Date:   Sat Oct 22 16:56:39 2016 -0300

    Using gdbus as IPC mechanism
---
 .gitignore                       |  2 ++
 configure.ac.in                  |  5 +++
 lib/Makefile.am                  | 14 +++++---
 lib/common.c                     | 36 +++++++++++++++++++-
 lib/common.h                     |  7 ++--
 lib/dbus.xml                     |  8 +++++
 lib/libdict.h                    |  1 +
 panel-plugin/xfce4-dict-plugin.c | 35 ++-----------------
 src/popup_plugin.c               | 72 ++++++++++++----------------------------
 src/xfce4-dict.c                 |  2 ++
 10 files changed, 91 insertions(+), 91 deletions(-)

diff --git a/.gitignore b/.gitignore
index 426ccb0..2cda42f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -33,3 +33,5 @@ INSTALL
 xfce4-dict-*.tar.bz2
 lib/resources.c
 lib/resources.h
+lib/dbus.c
+lib/dbus.h
diff --git a/configure.ac.in b/configure.ac.in
index e4b7a59..61038f6 100644
--- a/configure.ac.in
+++ b/configure.ac.in
@@ -61,6 +61,11 @@ XDT_CHECK_PACKAGE([LIBXFCE4PANEL], [libxfce4panel-2.0], [4.10.0])
 XDT_CHECK_PACKAGE([X11], [x11])
 
 dnl ***********************************
+dnl *** Check for gdbus-codegen     ***
+dnl ***********************************
+AC_CHECK_PROG([GDBUS_CODEGEN],[gdbus-codegen],[gdbus-codegen])
+
+dnl ***********************************
 dnl *** Check for debugging support ***
 dnl ***********************************
 XDT_FEATURE_DEBUG()
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 9fdaa51..a9e1dea 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -4,6 +4,8 @@ noinst_LTLIBRARIES = 							\
 libdict_la_SOURCES =							\
 	common.c									\
 	common.h									\
+	dbus.c										\
+	dbus.h										\
 	dictd.c										\
 	dictd.h										\
 	gui.c										\
@@ -33,14 +35,18 @@ libdict_la_LIBADD =								\
 	@GTHREAD_LIBS@
 
 DISTCLEANFILES =								\
-	resources.c									\
-	resources.h
+	resources.c										\
+	resources.h										\
+	dbus.c												\
+	dbus.h
 
-gui.c: resources.c resources.h
+gui.c: resources.c dbus.c
 
-resources.c:
+resources.c: resources.h
 	glib-compile-resources resources.xml --sourcedir=../pixmaps --generate-source --c-name dict
 
 resources.h:
 	glib-compile-resources resources.xml --sourcedir=../pixmaps --generate-header --c-name dict
 
+dbus.c:
+	gdbus-codegen --interface-prefix org.xfce --generate-c-code dbus dbus.xml
diff --git a/lib/common.c b/lib/common.c
index 31bc45d..271482c 100644
--- a/lib/common.c
+++ b/lib/common.c
@@ -38,6 +38,7 @@
 #include "spell.h"
 #include "dictd.h"
 #include "gui.h"
+#include "dbus.h"
 
 
 
@@ -217,7 +218,8 @@ void dict_search_word(DictData *dd, const gchar *word)
 	/* sanity checks */
 	if (! NZV(word))
 	{
-		dict_gui_status_add(dd, _("Invalid input"));
+		/* Just show the main window */
+		dict_gui_show_main_window(dd);
 		return;
 	}
 
@@ -591,3 +593,35 @@ gchar *dict_get_clipboard_contents(void)
 
 	return text;
 }
+
+
+static gboolean on_handle_search(Dict *skeleton, GDBusMethodInvocation *invocation,
+	const gchar *arg_phrase, gpointer user_data)
+{
+	dict_search_word((DictData *) user_data, arg_phrase);
+	dict_complete_search(skeleton, invocation);
+	return TRUE;
+}
+
+
+static void on_name_acquired(GDBusConnection *connection, const gchar *name,
+	gpointer user_data)
+{
+	Dict *skeleton = dict_skeleton_new();
+	g_signal_connect (skeleton, "handle-search", G_CALLBACK(on_handle_search), user_data);
+	g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (skeleton),
+		connection, "/org/xfce/Dict", NULL);
+}
+
+
+void dict_acquire_dbus_name(DictData *dd)
+{
+	g_bus_own_name(G_BUS_TYPE_SESSION,
+      "org.xfce.Dict",
+      G_BUS_NAME_OWNER_FLAGS_NONE,
+      NULL,
+      on_name_acquired,
+      NULL,
+      dd,
+      NULL);
+}
diff --git a/lib/common.h b/lib/common.h
index 089eae4..ab57d51 100644
--- a/lib/common.h
+++ b/lib/common.h
@@ -132,9 +132,10 @@ void dict_drag_data_received(GtkWidget *widget, GdkDragContext *drag_context, gi
 							 GtkSelectionData *data, guint info, guint ltime, DictData *dd);
 
 DictData *dict_create_dictdata(void);
-gboolean dict_start_web_query(DictData *dd, const gchar *word);
-gchar *dict_get_web_query_uri(DictData *dd, const gchar *word);
-gchar *dict_get_clipboard_contents(void);
+gboolean  dict_start_web_query(DictData *dd, const gchar *word);
+gchar    *dict_get_web_query_uri(DictData *dd, const gchar *word);
+gchar    *dict_get_clipboard_contents(void);
+void      dict_acquire_dbus_name(DictData *dd);
 
 void dict_show_msgbox(DictData *dd, gint type, const gchar *text, ...) G_GNUC_PRINTF (3, 4);
 
diff --git a/lib/dbus.xml b/lib/dbus.xml
new file mode 100644
index 0000000..1c72d0d
--- /dev/null
+++ b/lib/dbus.xml
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<node name="/" xmlns:doc="http://www.freedesktop.org/dbus/1.0/doc.dtd">
+  <interface name="org.xfce.Dict">
+    <method name="Search">
+      <arg name="phrase" type="s" direction="in"/>
+    </method>
+  </interface>
+</node>
diff --git a/lib/libdict.h b/lib/libdict.h
index 98ead5b..4435da0 100644
--- a/lib/libdict.h
+++ b/lib/libdict.h
@@ -24,6 +24,7 @@
 #include "dictd.h"
 #include "prefs.h"
 #include "gui.h"
+#include "dbus.h"
 
 
 #endif
diff --git a/panel-plugin/xfce4-dict-plugin.c b/panel-plugin/xfce4-dict-plugin.c
index e12ef6e..88f3b50 100644
--- a/panel-plugin/xfce4-dict-plugin.c
+++ b/panel-plugin/xfce4-dict-plugin.c
@@ -124,37 +124,6 @@ static void dict_plugin_panel_button_clicked(GtkWidget *button, DictPanelData *d
 }
 
 
-/* Handle user messages (xfce4-dict) */
-static gboolean dict_plugin_message_received(GtkWidget *w, GdkEventClient *ev, DictPanelData *dpd)
-{
-	if (ev->data_format == 8 && strncmp(ev->data.b, "xfdict", 6) == 0)
-	{
-		gchar flags = ev->data.b[6];
-		gchar *tts = ev->data.b + 7;
-
-		dpd->dd->mode_in_use = dict_set_search_mode_from_flags(dpd->dd->mode_in_use, flags);
-
-		if (NZV(tts))
-		{
-			gtk_entry_set_text(GTK_ENTRY(dpd->dd->main_entry), tts);
-			dict_search_word(dpd->dd, tts);
-		}
-		else if (flags & DICT_FLAGS_FOCUS_PANEL_ENTRY && dpd->dd->show_panel_entry)
-		{
-			xfce_panel_plugin_focus_widget(dpd->plugin, dpd->dd->panel_entry);
-		}
-		else
-		{
-			dict_plugin_panel_button_clicked(NULL, dpd);
-		}
-
-		return TRUE;
-	}
-
-	return FALSE;
-}
-
-
 static gboolean dict_plugin_set_selection(DictPanelData *dpd)
 {
 	GdkScreen *gscreen;
@@ -181,8 +150,6 @@ static gboolean dict_plugin_set_selection(DictPanelData *dpd)
 	XSelectInput(gdk_x11_display_get_xdisplay(gdk_display_get_default()), xwin, PropertyChangeMask);
 	XSetSelectionOwner(gdk_x11_display_get_xdisplay(gdk_display_get_default()), selection_atom, xwin, GDK_CURRENT_TIME);
 
-	g_signal_connect(win, "client-event", G_CALLBACK(dict_plugin_message_received), dpd);
-
 	return TRUE;
 }
 
@@ -419,6 +386,8 @@ static void dict_plugin_construct(XfcePanelPlugin *plugin)
 	g_signal_connect(dpd->panel_button, "drag-data-received", G_CALLBACK(dict_plugin_drag_data_received), dpd);
 	g_signal_connect(dpd->dd->panel_entry, "drag-data-received", G_CALLBACK(dict_plugin_drag_data_received), dpd);
 
+	dict_acquire_dbus_name(dpd->dd);
+
 	dict_gui_status_add(dpd->dd, _("Ready"));
 }
 XFCE_PANEL_PLUGIN_REGISTER(dict_plugin_construct);
diff --git a/src/popup_plugin.c b/src/popup_plugin.c
index 7115157..260cb54 100644
--- a/src/popup_plugin.c
+++ b/src/popup_plugin.c
@@ -26,62 +26,34 @@
 #include "libdict.h"
 
 
-static gboolean check_is_running(GtkWidget *widget, Window *xid)
-{
-	GdkScreen *gscreen;
-	gchar selection_name[32];
-	Atom selection_atom;
-
-	gscreen = gtk_widget_get_screen(widget);
-	g_snprintf(selection_name, sizeof(selection_name), XFCE_DICT_SELECTION"%d",
-		gdk_screen_get_number(gscreen));
-	selection_atom = XInternAtom(gdk_x11_display_get_xdisplay(gdk_display_get_default()), selection_name, False);
-
-	if ((*xid = XGetSelectionOwner(gdk_x11_display_get_xdisplay(gdk_display_get_default()), selection_atom)))
-		return TRUE;
-
-	return FALSE;
-}
-
-
 gboolean dict_find_panel_plugin(gchar flags, const gchar *text)
 {
-	gboolean ret = FALSE;
-	GdkEventClient gev;
-	GtkWidget *win;
-	Window id;
-
-	win = gtk_invisible_new();
-	gtk_widget_realize(win);
-
-	gev.type = GDK_CLIENT_EVENT;
-	gev.window = win->window;
-	gev.send_event = TRUE;
-	gev.message_type = gdk_atom_intern("STRING", FALSE);
-	gev.data_format = 8;
-
-	if (text == NULL)
-		text = "";
-	else if (strlen(text) > 12)
-		g_warning("The passed search text is longer than 12 characters and was truncated. Currently you can pass only a maximum of 12 characters to the panel plugin (or even less when using non-ASCII characters).");
+	gboolean  ret = FALSE;
+	GError   *error = NULL;
+	Dict     *proxy;
+
+	proxy = dict_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
+																			G_DBUS_PROXY_FLAGS_NONE,
+																			"org.xfce.Dict",
+																			"/org/xfce/Dict",
+																			NULL,
+																			&error);
+
+	if (!proxy)
+	{
+		g_warning ("error connecting to org.xfce.Dict, reason was: %s", error->message);
+		g_clear_error(&error);
+		return FALSE;
+	}
 
-	/* format of the send string: "xfdict?text":
-	 * "xfdict" is for identification of ourselves
-	 * ? is a bitmask to control the behaviour, it can contain one or more of DICT_FLAGS_*,
-	 * we send it as %c to ensure it takes only one char in the string,
-	 * everything after this is the text to search, given on command line */
-	/** FIXME: remove the limit of 12 characters, maybe by sending more than message or by
-	  *        using another IPC mechanism, maybe DBus? */
-	g_snprintf(gev.data.b, sizeof gev.data.b, "xfdict%c%s", flags, text);
+	ret = dict_call_search_sync (proxy, text, NULL, &error);
 
-	if (check_is_running(win, &id))
+	if (error)
 	{
-		gdk_event_send_client_message((GdkEvent*) &gev, (GdkNativeWindow) id);
-		ret = TRUE;
+		g_warning ("failed to connecting to org.xfce.Dict, reason was: %s", error->message);
+		g_clear_error(&error);
+		return FALSE;
 	}
 
-	gdk_flush();
-	gtk_widget_destroy(win);
-
 	return ret;
 }
diff --git a/src/xfce4-dict.c b/src/xfce4-dict.c
index 4185aad..dd84279 100644
--- a/src/xfce4-dict.c
+++ b/src/xfce4-dict.c
@@ -197,6 +197,8 @@ gint main(gint argc, gchar *argv[])
 
 	g_free(search_text);
 
+	dict_acquire_dbus_name(dd);
+
 	gtk_widget_show_all(dd->window);
 
 	gtk_main();

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Xfce4-commits mailing list