[evolution-rss] initial gdbus migration



commit bd46fd70603ab085edf07a5862be8bccba245f06
Author: Lucian Langa <lucilanga gnome org>
Date:   Wed Nov 3 23:48:43 2010 +0200

    initial gdbus migration

 configure.ac               |   38 ++-----
 src/Makefile.am            |   19 +---
 src/dbus.c                 |  234 +++++++++++++++++++--------------------
 src/dbus.h                 |    3 +-
 src/evolution-import-rss.c |  261 +++++++++++++++++++++++++++++++-------------
 src/rss.c                  |   14 +--
 src/rss.h                  |    7 -
 7 files changed, 319 insertions(+), 257 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 1afd715..34381ca 100644
--- a/configure.ac
+++ b/configure.ac
@@ -105,12 +105,15 @@ fi
 
 PKG_CHECK_MODULES([EVOLUTION_DATASERVER], evolution-data-server-1.2, [have_ds=yes], [have_ds=no])
 if test "x$have_ds" == "xYES"; then
-       DATASERVER_VERSION=`$PKG_CONFIG --modversion evolution-data-server-1.2`
+	DATASERVER_VERSION=`$PKG_CONFIG --modversion evolution-data-server-1.2`
+	DATASERVER_LIBS=`$PKG_CONFIG --libs evolution-data-server-1.2`
 else
-       DATASERVER_VERSION=`$PKG_CONFIG --modversion libedataserver-1.2`
+	DATASERVER_VERSION=`$PKG_CONFIG --modversion libedataserver-1.2`
+	DATASERVER_LIBS=`$PKG_CONFIG --libs libedataserver-1.2`
 fi
 DATASERVER_VERSION=`echo $DATASERVER_VERSION|$AWK -F . '{print $1 * 1000000 + $2 * 1000 + $3}'`
 AC_SUBST(DATASERVER_VERSION)
+AC_SUBST(DATASERVER_LIBS)
 
 dnl default renderer
 DEFAULT_RENDER=webkit
@@ -285,8 +288,7 @@ AM_GCONF_SOURCE_2
 dnl ********************
 dnl extract soup version
 dnl ********************
-SOUP_VERSION=`$PKG_CONFIG --modversion libsoup$SOUP`
-SOUP_VERSION=`echo $SOUP_VERSION|$AWK -F . '{print $1 * 1000000 + $2 * 1000 + $3}'`
+SOUP_VERSION="$($PKG_CONFIG --modversion libsoup$SOUP|$AWK -F . '{print $1 * 1000000 + $2 * 1000 + $3}')"
 AC_SUBST(SOUP_VERSION)
 PKG_CHECK_MODULES([LIBSOUP_GNOME], libsoup-gnome$SOUP, [have_libsoup_gnome=yes], [have_libsoup_gnome=no])
 if test "x$have_libsoup_gnome" == "xyes"; then
@@ -298,21 +300,6 @@ else
 fi
 AC_SUBST(LIBSOUP_LIBS)
 
-dnl ***********
-dnl D-BUS stuff
-dnl ***********
-have_dbus=no
-AC_ARG_ENABLE([DBUS],
-        AS_HELP_STRING([--disable-dbus],[Disable D-BUS support (default: enabled)]),
-        [])
-
-if test "x$enable_dbus" != "xno"; then
-	PKG_CHECK_MODULES(DBUS, dbus-glib-1, [have_dbus=yes], [have_dbus=no])
-	AC_SUBST(DBUS_CFLAGS)
-	AC_SUBST(DBUS_LIBS)
-fi
-AM_CONDITIONAL([HAVE_DBUS], [test x$have_dbus = xyes])
-
 evolution_int_version="$(echo "$EVOLUTION_EXEC_VERSION" | $AWK -F . '{print [$]1 * 10000 + [$]2 * 100 + [$]3}')"
 if test $evolution_int_version -lt 22900; then
 	have_bonobo=yes
@@ -401,7 +388,7 @@ pixmaps/Makefile
 
 kits="gtkHTML"
 if test "x$have_webkit" != "xno" -a "$enable_webkit" = "yes"; then
-        kits=$kits", WebKit ($WEBKIT_S_VERSION)"
+	kits=$kits", WebKit ($WEBKIT_S_VERSION)"
 fi
 # rename libxul to xulrunner to prevent confusion on debian
 if test "x$gecko_cv_gecko" == "xlibxul"; then
@@ -411,13 +398,10 @@ if test "x$have_gecko" != "xno" -a "$enable_gecko" = "yes"; then
 kits="$kits, $gecko_cv_gecko (gecko engine version $gecko_cv_gecko_version)"
 fi
 
-if test "x$have_dbus" == "xyes"; then
-	have_dbus_build="yes"
-	import_feeds="evolution-import-rss"
-else
-	have_dbus_build="evolution-import-rss module will not be built"
-	import_feeds="no"
-fi
+dnl on by default now
+have_dbus_build="yes"
+import_feeds="evolution-import-rss"
+
 
 AC_OUTPUT
 echo "
diff --git a/src/Makefile.am b/src/Makefile.am
index 9781ea2..c2a2bba 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -4,7 +4,6 @@ DISTCHECK_CONFIGURE_FLAGS = --disable-schemas-install
 INCLUDES =							\
 	-I$(top_srcdir)						\
 	$(RENDER_CFLAGS)					\
-	$(DBUS_CFLAGS)						\
 	$(LIBSOUP_GNOME_CFLAGS)					\
 	$(GTKHTML_EDITOR_CFLAGS)				\
 	-DGETTEXT_PACKAGE="\"$(GETTEXT_PACKAGE)\""		\
@@ -18,10 +17,7 @@ INCLUDES =							\
 	-DWEBKIT_VERSION="$(WEBKIT_VERSION)"			\
 	-DRENDER=\""$(RENDER)"\"				\
 	-DRENDER_N=$(RENDER_N)					\
-	-DHAVE_DBUS=$(DBD)					\
-	-DDBUS_API_SUBJECT_TO_CHANGE				\
 	$(EVOLUTION_RSS_EPLUGIN_CFLAGS)
-#	$(WARNING_FLAGS)
 
 if ! HAVE_XPCOM_GLUE
 INCLUDES += -DGECKO_HOME=\""$(GECKO_HOME)"\"
@@ -73,7 +69,8 @@ plugin_DATA = org-gnome-evolution-rss.eplug \
 plugin_LTLIBRARIES = liborg-gnome-evolution-rss.la
 
 
-SOURCES =	fetch.c			\
+SOURCES =	dbus.c			\
+		fetch.c			\
 		file-gio.c		\
 		misc.c			\
 		network-soup.c		\
@@ -89,17 +86,14 @@ SOURCES =	fetch.c			\
 if HAVE_GECKO
 SOURCES+=	gecko-utils.cpp
 endif
-if HAVE_DBUS
-SOURCES+=	dbus.c
-endif
 
 liborg_gnome_evolution_rss_la_SOURCES = $(SOURCES)
-liborg_gnome_evolution_rss_la_LIBADD = $(WEBKIT_LIBS) $(GECKO_LIBS) $(LIBSOUP_LIBS) $(GTKHTML_EDITOR_LIBS) $(DBUS_LIBS) $(NO_UNDEFINED_LIBS)
+liborg_gnome_evolution_rss_la_LIBADD = $(WEBKIT_LIBS) $(DATASERVER_LIBS) $(GECKO_LIBS) $(LIBSOUP_LIBS) $(GTKHTML_EDITOR_LIBS) $(NO_UNDEFINED_LIBS)
 liborg_gnome_evolution_rss_la_LDFLAGS = -module -avoid-version $(NO_UNDEFINED)
 #$(GECKO_RPATH)
 
 evolution_import_rss_SOURCES = evolution-import-rss.c
-evolution_import_rss_LDFLAGS = $(DBUS_LIBS)
+evolution_import_rss_LDFLAGS = $(EVOLUTION_RSS_EPLUGIN_LIBS)
 
 
 schemadir	= $(GCONF_SCHEMA_FILE_DIR)
@@ -205,13 +199,8 @@ EXTRA_DIST =					\
 	org-gnome-evolution-rss.xml
 
 
-if HAVE_DBUS
 bin_PROGRAMS = \
 	evolution-import-rss
-DBD=1
-else
-DBD=0
-endif
 
 
 -include $(top_srcdir)/git.mk
diff --git a/src/dbus.c b/src/dbus.c
index e3def2b..7a7ce93 100644
--- a/src/dbus.c
+++ b/src/dbus.c
@@ -25,9 +25,8 @@
 #include <string.h>
 #include <glib.h>
 #include <glib/gi18n-lib.h>
-#include <dbus/dbus-glib.h>
-#include <dbus/dbus-glib-lowlevel.h>
 #include <gtk/gtk.h>
+#include <gio/gio.h>
 
 extern int rss_verbose_debug;
 
@@ -38,45 +37,20 @@ extern int rss_verbose_debug;
 #include "dbus.h"
 
 #define DBUS_PATH "/org/gnome/evolution/mail/rss"
-#define DBUS_INTERFACE "org.gnome.evolution.mail.rss.in"
+#define RSS_DBUS_SERVICE "org.gnome.evolution.mail.rss"
+//#define DBUS_INTERFACE "org.gnome.evolution.mail.rss.in"
+#define DBUS_INTERFACE "org.gnome.evolution.mail.rss"
 #define DBUS_REPLY_INTERFACE "org.gnome.evolution.mail.rss.out"
 
-static DBusConnection *bus = NULL;
+static GDBusConnection *connection = NULL;
 static gboolean enabled = FALSE;
 
 extern rssfeed *rf;
 
-#if 0
-static void
-send_dbus_message (const char *name, const char *data, guint new)
-{
-	DBusMessage *message;
-
-	/* Create a new message on the DBUS_INTERFACE */
-	if (!(message = dbus_message_new_signal (DBUS_PATH, DBUS_INTERFACE, name)))
-		return;
-
-	/* Appends the data as an argument to the message */
-	dbus_message_append_args (message,
-#if DBUS_VERSION >= 310
-				  DBUS_TYPE_STRING, &data,
-#else
-				  DBUS_TYPE_STRING, data,
-#endif
-				  DBUS_TYPE_INVALID);
-
-	/* Sends the message */
-	dbus_connection_send (bus, message, NULL);
-
-	/* Frees the message */
-	dbus_message_unref (message);
-}
-#endif
-
 static gboolean
 reinit_dbus (gpointer user_data)
 {
-	if (!enabled || init_dbus ())
+	if (!enabled || init_gdbus ())
 		return FALSE;
 
 	/* keep trying to re-establish dbus connection */
@@ -84,110 +58,130 @@ reinit_dbus (gpointer user_data)
 	return TRUE;
 }
 
-static DBusHandlerResult
-filter_function (DBusConnection *connection, DBusMessage *message, void *user_data)
+static void
+connection_closed_cb (GDBusConnection *pconnection,
+	gboolean remote_peer_vanished, GError *error,
+	gpointer user_data)
 {
-	if (dbus_message_is_signal (message, DBUS_INTERFACE_LOCAL, "Disconnected") &&
-	    strcmp (dbus_message_get_path (message), DBUS_PATH_LOCAL) == 0) {
-		dbus_connection_unref (bus);
-		bus = NULL;
+	g_return_if_fail (connection != pconnection);
+	g_object_unref (connection);
+	connection = NULL;
 
-		g_timeout_add (3000, reinit_dbus, NULL);
+	g_timeout_add (3000, reinit_dbus, NULL);
+}
 
-		return DBUS_HANDLER_RESULT_HANDLED;
-	}
-	else if (dbus_message_is_signal (message, DBUS_INTERFACE, "evolution_rss_feed")) {
-		DBusError error;
-		char *s;
+static void
+signal_cb (GDBusConnection *connection,
+		const gchar *sender_name,
+		const gchar *object_path,
+		const gchar *interface_name,
+		const gchar *signal_name,
+		GVariant *parameters,
+		gpointer user_data)
+{
+	if (g_strcmp0 (interface_name, DBUS_INTERFACE) != 0
+	|| g_strcmp0 (object_path, DBUS_PATH) != 0)
+		return;
+
+	d("interface:%s!\n", interface_name);
+	d("path:%s!\n", object_path);
+	d("signal_name:%s!\n", signal_name);
+
+
+	if (!g_strcmp0 (signal_name, "import")) {
+		gchar *url = NULL;
 		add_feed *feed = g_new0(add_feed, 1);
-		dbus_error_init (&error);
-		if (dbus_message_get_args
-			(message, &error, DBUS_TYPE_STRING, &s, DBUS_TYPE_INVALID)) {
-			g_print("New Feed received: %s\n", s);
-			feed->feed_url = g_strdup(s);
-			feed->add=1;
-			feed->enabled=feed->validate=1;
-			feed->fetch_html = 0;
-			if (feed->feed_url && strlen(feed->feed_url)) {
-				gchar *text = feed->feed_url;
-				feed->feed_url = sanitize_url(feed->feed_url);
-				g_free(text);
-				d("sanitized feed URL: %s\n", feed->feed_url);
-				if (g_hash_table_find(rf->hr,
-                                        check_if_match,
-					feed->feed_url)) {
-					rss_error(NULL, NULL, _("Error adding feed."),
-						_("Feed already exists!"));
-					return DBUS_HANDLER_RESULT_HANDLED;
-				}
-				if (setup_feed(feed)) {
-					gchar *msg = g_strdup_printf(_("New feed imported: %s"),
-							lookup_chn_name_by_url(feed->feed_url));
-					taskbar_push_message(msg);
-					g_free(msg);
-				}
-				if (rf->treeview)
-					store_redraw(GTK_TREE_VIEW(rf->treeview));
-				save_gconf_feed();
+		g_variant_get (parameters, "((s))", &url);
+		g_print("New Feed received: %s\n", url);
+		feed->feed_url = g_strdup(url);
+		feed->add=1;
+		feed->enabled=feed->validate=1;
+		feed->fetch_html = 0;
+		if (feed->feed_url && strlen(feed->feed_url)) {
+			gchar *text = feed->feed_url;
+			feed->feed_url = sanitize_url(feed->feed_url);
+			g_free(text);
+			d("sanitized feed URL: %s\n", feed->feed_url);
+			if (g_hash_table_find(rf->hr, check_if_match, feed->feed_url)) {
+				rss_error(NULL, NULL, _("Error adding feed."),
+					_("Feed already exists!"));
+					return;
+			}
+			if (setup_feed(feed)) {
+				gchar *msg = g_strdup_printf(_("Importing URL: %s"),
+						feed->feed_url);
+				taskbar_push_message(msg);
+				g_free(msg);
+			}
+			if (rf->treeview)
+				store_redraw(GTK_TREE_VIEW(rf->treeview));
+			save_gconf_feed();
 #if (DATASERVER_VERSION >= 2033001)
-				camel_operation_pop_message (NULL);
+			camel_operation_pop_message (NULL);
 #else
-				camel_operation_end(NULL);
+		camel_operation_end(NULL);
 #endif
-			}
-
-
-//  dbus_message_unref(reply);
-//     return DBUS_HANDLER_RESULT_REMOVE_MESSAGE;
-
-			//dbus_free (s);
-		} else {
-			g_print("Feed received, but error getting message: %s\n", error.message);
-			dbus_error_free (&error);
 		}
-		g_free(feed);
-		return DBUS_HANDLER_RESULT_HANDLED;
-	}
-	else if (dbus_message_is_signal (message, DBUS_INTERFACE, "ping")) {
-		DBusMessage *reply;
-		fprintf(stderr, "!!Ping!! received from %s\n",
-				dbus_message_get_sender(message));
-		fprintf(stderr, "Sending !!Pong!! back\n");
-		reply = dbus_message_new_signal (DBUS_PATH, DBUS_REPLY_INTERFACE, "pong");
-//		dbus_message_set_reply_serial(reply,
-//			dbus_message_get_serial(message));
-		dbus_connection_send (connection, reply, NULL);
-		dbus_connection_flush (connection);
-		return DBUS_HANDLER_RESULT_HANDLED;
+		g_free(url);
 	}
 
-	return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+	if (!g_strcmp0 (signal_name, "ping")) {
+		GDBusMessage *message;
+		GVariantBuilder *builder;
+		GError *error = NULL;
+		d("Ping! received from %s\n", interface_name);
+
+		if (!(message = g_dbus_message_new_signal (DBUS_PATH, DBUS_INTERFACE, "pong")))
+			return;
+		d("Sending Pong! back\n");
+		builder = g_variant_builder_new (G_VARIANT_TYPE_TUPLE);
+		g_variant_builder_add(builder, "s", "ponging");
+		g_dbus_message_set_body (message, g_variant_builder_end (builder));
+		g_dbus_connection_send_message (connection, message,
+			G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &error);
+		g_object_unref (message);
+		if (error) {
+			g_debug ("Error while sending ping-request: %s", error->message);
+			g_error_free (error);
+		}
+	}
 }
 
-DBusConnection *
-init_dbus (void)
+gboolean
+init_gdbus (void)
 {
-	static DBusConnection *bus = NULL;
-	DBusError error;
-	GMainLoop *loop;
-	loop = g_main_loop_new (NULL, FALSE);
-
-	if (rf->bus != NULL)
-		return rf->bus;
-
-	dbus_error_init (&error);
-	if (!(bus = dbus_bus_get (DBUS_BUS_SESSION, &error))) {
-		g_warning ("could not get system bus: %s\n", error.message);
-		dbus_error_free (&error);
-		return NULL;
+	GError *error = NULL;
+
+	connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
+	if (error) {
+		g_warning ("could not get system bus: %s\n", error->message);
+		g_error_free (error);
+		return FALSE;
 	}
 
-	dbus_connection_setup_with_g_main (bus, NULL);
-	dbus_bus_add_match (bus, "type='signal',interface='org.gnome.evolution.mail.rss.in'", NULL);
-	dbus_connection_set_exit_on_disconnect (bus, FALSE);
+	g_dbus_connection_set_exit_on_close (connection, FALSE);
+	g_signal_connect (connection, "closed",
+		G_CALLBACK (connection_closed_cb), NULL);
+
+	if (!g_dbus_connection_signal_subscribe (
+		connection,
+		NULL,
+		RSS_DBUS_SERVICE,
+/*		DBUS_INTERFACE,*/
+		NULL,
+		DBUS_PATH,
+		NULL,
+		G_DBUS_SIGNAL_FLAGS_NONE,
+		signal_cb,
+		NULL,
+		NULL)) {
+			g_warning ("%s: Failed to subscribe for a signal", G_STRFUNC);
+			goto fail;
+		}
+	return FALSE;
 
-	dbus_connection_add_filter (bus, filter_function, loop, NULL);
 
-	return bus;
+fail:	g_object_unref(connection);
+	return TRUE;
 }
 
diff --git a/src/dbus.h b/src/dbus.h
index ec11d8e..70e9bce 100644
--- a/src/dbus.h
+++ b/src/dbus.h
@@ -17,5 +17,4 @@
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-DBusConnection *init_dbus (void);
-
+gboolean init_gdbus (void);
diff --git a/src/evolution-import-rss.c b/src/evolution-import-rss.c
index 63b4b0e..f7bb1cc 100644
--- a/src/evolution-import-rss.c
+++ b/src/evolution-import-rss.c
@@ -1,7 +1,7 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 /*
  *  Evoution RSS Reader Plugin
- *  Copyright (C) 2007-2008 Lucian Langa <cooly gnome eu org> 
+ *  Copyright (C) 2007-2010 Lucian Langa <cooly gnome eu org>
  *
  *  This program is free software; you can redistribute it and/or
  *  modify it under the terms of version 2 of the GNU General Public
@@ -28,129 +28,179 @@
 #include <stdarg.h>
 #include <string.h>
 #include <glib.h>
-#include <dbus/dbus.h>
-#include <dbus/dbus-glib.h>
-#include <dbus/dbus-glib-lowlevel.h>
+
+#include <gio/gio.h>
+
+#define d(x)
 
 #define DBUS_PATH "/org/gnome/evolution/mail/rss"
-#define DBUS_INTERFACE "org.gnome.evolution.mail.rss.in"
+#define RSS_DBUS_SERVICE "org.gnome.evolution.mail.rss"
+//#define DBUS_INTERFACE "org.gnome.evolution.mail.rss.in"
+#define DBUS_INTERFACE "org.gnome.evolution.mail.rss"
 #define DBUS_REPLY_INTERFACE "org.gnome.evolution.mail.rss.out"
 
 //evolution ping roud-trip time in ms, somebody suggest a real value here
 #define EVOLUTION_PING_TIMEOUT 5000
 
-static gboolean init_dbus (void);
+static GDBusConnection *connection = NULL;
+
+static gboolean init_gdbus (void);
 
-static DBusConnection *bus = NULL;
 static gboolean enabled = FALSE;
 GMainLoop *loop;
 gboolean evo_running = FALSE;
+static gchar *feed = NULL;
+
 
 static void
 send_dbus_ping (void)
 {
-	DBusMessage *message;
-	int ret;
+	GDBusMessage *message;
+	GVariantBuilder *builder;
+	GError *error = NULL;
 
-	if (!(message = dbus_message_new_signal (DBUS_PATH, DBUS_INTERFACE, "ping")))
+	if (!(message = g_dbus_message_new_signal (DBUS_PATH, DBUS_INTERFACE, "ping")))
 		return;
 	printf("ping evolution...\n");
-	ret = dbus_connection_send (bus, message, NULL);
-	if (ret == FALSE)
-    	{
-     		printf("Could not send method call\n");
+	builder = g_variant_builder_new (G_VARIANT_TYPE_TUPLE);
+	g_variant_builder_add(builder, "s", "evolution-rss");
+	g_dbus_message_set_body (message, g_variant_builder_end (builder));
+	g_dbus_connection_send_message (connection, message,
+		G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &error);
+	g_object_unref (message);
+	if (error) {
+		g_debug ("Error while sending ping-request: %s", error->message);
+		g_error_free (error);
 	}
-	dbus_message_unref (message);
 }
 
 static void
 send_dbus_message (const char *name, const char *data)
 {
-	DBusMessage *message;
-	
+	GDBusMessage *message;
+	GVariantBuilder *builder;
+	GError *error = NULL;
+
 	/* Create a new message on the DBUS_INTERFACE */
-	if (!(message = dbus_message_new_signal (DBUS_PATH, DBUS_INTERFACE, name)))
+	if (!(message = g_dbus_message_new_signal (DBUS_PATH, DBUS_INTERFACE, name)))
 		return;
-	
+
+	builder = g_variant_builder_new (G_VARIANT_TYPE_TUPLE);
+
 	/* Appends the data as an argument to the message */
-	dbus_message_append_args (message,
-//#if DBUS_VERSION >= 310
-			  DBUS_TYPE_STRING, &data,
-//#else
-//			  DBUS_TYPE_STRING, data,
-//#endif
-			  DBUS_TYPE_INVALID);
+	g_variant_builder_add (builder, "(s)", data);
+	g_dbus_message_set_body (message, g_variant_builder_end (builder));
 
 	/* Sends the message */
-	dbus_connection_send (bus, message, NULL);
-	
+	g_dbus_connection_send_message (connection, message, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &error);
+
 	/* Frees the message */
-	dbus_message_unref (message);
+	g_object_unref (message);
+
+	if (error) {
+		g_debug ("%s: Error while sending DBus message: %s", G_STRFUNC, error->message);
+		g_error_free (error);
+	}
 }
 
 static gboolean
-reinit_dbus (gpointer user_data)
+reinit_gdbus (gpointer user_data)
 {
-	if (!enabled || init_dbus ())
+	if (!enabled || init_gdbus ())
 		return FALSE;
-	
+
 	/* keep trying to re-establish dbus connection */
-	
+
 	return TRUE;
 }
 
-static DBusHandlerResult
-filter_function (DBusConnection *connection, DBusMessage *message, void *user_data)
+static void
+signal_cb (GDBusConnection *connection,
+		const gchar *sender_name,
+		const gchar *object_path,
+		const gchar *interface_name,
+		const gchar *signal_name,
+		GVariant *parameters,
+		gpointer user_data)
 {
-	if (dbus_message_is_signal (message, DBUS_INTERFACE_LOCAL, "Disconnected") &&
-	    strcmp (dbus_message_get_path (message), DBUS_PATH_LOCAL) == 0) {
-		dbus_connection_unref (bus);
-		bus = NULL;
-		
-		g_timeout_add (3000, reinit_dbus, NULL);
-		
-		return DBUS_HANDLER_RESULT_HANDLED;
+	if (g_strcmp0 (interface_name, DBUS_INTERFACE) != 0
+	|| g_strcmp0 (object_path, DBUS_PATH) != 0)
+		return;
+
+	d(g_print("interface:%s!\n", interface_name));
+	d(g_print("path:%s!\n", object_path));
+	d(g_print("signal_name:%s!\n", signal_name));
+
+	if (!g_strcmp0 (signal_name, "pong")) {
+		evo_running = TRUE;
+		d(g_print("pong\n"));
+		send_dbus_message ("import", feed);
+		g_usleep(300);
+		g_main_loop_quit(loop);
 	}
+}
+
+static void
+connection_closed_cb  (GDBusConnection *pconnection,
+	gboolean remote_peer_vanished, GError *error, gpointer user_data)
+{
+	g_return_if_fail (connection != pconnection);
+	g_object_unref (connection);
+	connection = NULL;
+
+	g_timeout_add (3000, reinit_gdbus, NULL);
+#if 0
 	else if (dbus_message_is_signal (message, DBUS_REPLY_INTERFACE, "pong")) {
 		g_print("pong!\n");
 		evo_running = TRUE;
 		g_main_loop_quit(loop);
 		return DBUS_HANDLER_RESULT_HANDLED;
 	}
-	
 	return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+#endif
 }
 
 static gboolean
-init_dbus (void)
+init_gdbus (void)
 {
-	DBusError error;
-	
-	if (bus != NULL)
+	GError *error = NULL;
+
+	if (connection != NULL)
 		return TRUE;
-	
-	dbus_error_init (&error);
-	if (!(bus = dbus_bus_get (DBUS_BUS_SESSION, &error))) {
-		g_warning ("could not get system bus: %s\n", error.message);
-		dbus_error_free (&error);
+	connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
+	if (error) {
+		g_warning ("could not get system bus: %s\n", error->message);
+		g_error_free (error);
 		return FALSE;
 	}
-	
-	dbus_connection_setup_with_g_main (bus, NULL);
-	dbus_bus_add_match (bus, "type='signal'", NULL);
-	dbus_connection_set_exit_on_disconnect (bus, FALSE);
-	dbus_connection_add_filter (bus, filter_function, loop, NULL);
-	
+	g_dbus_connection_set_exit_on_close (connection, FALSE);
+	g_signal_connect (connection, "closed", G_CALLBACK (connection_closed_cb), NULL);
+
 	return TRUE;
 }
 
 static gboolean
 no_evo_cb (gpointer user_data)
 {
-	g_print("no evolution running!\n");
-	g_print("trying to start...\n");
-	g_main_loop_quit(loop);
-	return TRUE;
+	if (!evo_running) {
+		g_print("no evolution running!\n");
+		g_print("trying to start...\n");
+		system("evolution&");
+		g_usleep(30);
+		send_dbus_ping ();
+		return TRUE;
+	} else
+		return FALSE;
+}
+
+
+static gboolean
+main_prog(gpointer user_data)
+{
+	if (connection != NULL)
+		send_dbus_ping ();
+	g_timeout_add (EVOLUTION_PING_TIMEOUT, no_evo_cb, NULL);
+	return FALSE;
 }
 
 static gboolean
@@ -161,48 +211,109 @@ err_evo_cb (gpointer user_data)
 	return TRUE;
 }
 
+static void
+on_bus_acquired (GDBusConnection *connection,
+		const gchar     *name,
+		gpointer         user_data)
+{
+	g_print("Auquired bus connection.\n");
+}
+
+static void
+on_name_acquired (GDBusConnection *connection,
+		const gchar     *name,
+		gpointer         user_data)
+{
+	g_print("Name aquired.\n");
+}
+
+static void
+on_name_lost (GDBusConnection *connection,
+		const gchar     *name,
+		gpointer         user_data)
+{
+	g_print("Name lost.\n");
+}
+
 int
 main (int argc, char *argv[])
 {
 	guint i=0;
-	char *s;
+	guint owner_id;
+
+	feed = argv[1];
+	if (!feed) {
+		g_print("Syntax: %s URL\n", argv[0]);
+		return 0;
+	}
+
+	g_type_init ();
+
+
+	owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
+		RSS_DBUS_SERVICE,
+		G_BUS_NAME_OWNER_FLAGS_NONE,
+		on_bus_acquired,
+		on_name_acquired,
+		on_name_lost,
+		NULL,
+		NULL);
 
 	loop = g_main_loop_new (NULL, FALSE);
 
-	if (!init_dbus ())
+	if (!init_gdbus ())
 		return -1;
 
-	s = argv[1];
+	g_signal_connect (connection, "closed",
+		G_CALLBACK (connection_closed_cb), NULL);
+
+	g_timeout_add (100, main_prog, NULL);
+
+	if (!g_dbus_connection_signal_subscribe (
+		connection,
+		NULL,
+		RSS_DBUS_SERVICE,
+		NULL,
+		NULL,
+		NULL,
+		G_DBUS_SIGNAL_FLAGS_NONE,
+		signal_cb,
+		NULL,
+		NULL)) {
+			g_warning ("%s: Failed to subscribe for a signal", G_STRFUNC);
+			goto fail;
+		}
 
-	if (bus != NULL)
-                send_dbus_ping ();
-	g_timeout_add (EVOLUTION_PING_TIMEOUT, no_evo_cb, NULL);
 	g_main_loop_run(loop);
-	while (!evo_running && i < 2)
-	{
+
+	while (!evo_running && i < 2) {
 		system("evolution&");
 		g_print("fireing evolution...\n");
 		g_usleep(30);
-        	send_dbus_ping ();
+		send_dbus_ping ();
 		g_timeout_add (EVOLUTION_PING_TIMEOUT, err_evo_cb, GINT_TO_POINTER(i++));
 		g_main_loop_run(loop);
 	}
-	
 
+#if 0
 	if (evo_running) {
 		if (s)
-        		send_dbus_message ("evolution_rss_feed", s);
+			send_dbus_message ("evolution_rss_feed", s);
 		else
 			g_print("Syntax: evolution-import-rss URL\n");
 	} else {
 		g_print("evolution repetably failed to start!\n");
 		g_print("Cannot add feed!");
 	}
-	
+
 	if (bus != NULL) {
 		dbus_connection_unref (bus);
 		bus = NULL;
 	}
 	return 0;
+#endif
+
+fail:	return FALSE;
+
 }
 
diff --git a/src/rss.c b/src/rss.c
index f0162d5..18df22d 100644
--- a/src/rss.c
+++ b/src/rss.c
@@ -151,9 +151,7 @@ int rss_verbose_debug = 0;
 #include "file-gio.h"
 #include "fetch.h"
 #include "misc.h"
-#if HAVE_DBUS
 #include "dbus.h"
-#endif
 #include "rss-config-factory.h"
 #include "rss-icon-factory.h"
 #include "rss-status-icon.h"
@@ -4859,11 +4857,9 @@ e_plugin_lib_enable(EPlugin *ep, int enable)
 			proxy = proxy_init();
 #endif
 			rss_soup_init();
-#if HAVE_DBUS
-			d("init_dbus()\n");
-			/*D-BUS init*/
-			rf->bus = init_dbus ();
-#endif
+			d("init_gdbus()\n");
+			/*G D-BUS init*/
+			init_gdbus ();
 			prepare_hashes();
 			if (gconf_client_get_bool (rss_gconf, GCONF_KEY_STATUS_ICON, NULL))
 				create_status_icon();
@@ -4891,10 +4887,6 @@ e_plugin_lib_enable(EPlugin *ep, int enable)
 		}
 		upgrade = 2; //init done
 	} else {
-#if HAVE_DBUS
-		if (rf->bus != NULL)
-			dbus_connection_unref (rf->bus);
-#endif
 		abort_all_soup();
 		printf("Plugin disabled\n");
 	}
diff --git a/src/rss.h b/src/rss.h
index e6ac623..aa5416a 100644
--- a/src/rss.h
+++ b/src/rss.h
@@ -21,10 +21,6 @@
 #endif
 #include "debug.h"
 
-#if HAVE_DBUS
-#include <dbus/dbus-glib.h>
-#include <dbus/dbus-glib-lowlevel.h>
-#endif
 #include "network.h"
 
 #ifndef __RSS_H_
@@ -180,9 +176,6 @@ typedef struct _rssfeed {
 	char		*current_uid;		// currently read article
 	GQueue		*stqueue;		// network downloads tracking
 	GList		*enclist;		// network downloads tracking
-#if HAVE_DBUS
-	DBusConnection	*bus;			// DBUS
-#endif
 } rssfeed;
 
 #define GCONF_KEY_DISPLAY_SUMMARY "/apps/evolution/evolution-rss/display_summary"



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]