[tracker/gdbus-porting: 5/33] libtracker-common: Add support for GDBus



commit 28e22c25ec5dd38ecefab84fc185a8b559c03fcf
Author: Philip Van Hoof <philip codeminded be>
Date:   Wed Dec 29 16:14:03 2010 +0100

    libtracker-common: Add support for GDBus

 src/libtracker-common/tracker-dbus.c |  122 ++++++++++++++++++++++++++++++++++
 src/libtracker-common/tracker-dbus.h |   35 ++++++++++
 2 files changed, 157 insertions(+), 0 deletions(-)
---
diff --git a/src/libtracker-common/tracker-dbus.c b/src/libtracker-common/tracker-dbus.c
index bedb350..edabf00 100644
--- a/src/libtracker-common/tracker-dbus.c
+++ b/src/libtracker-common/tracker-dbus.c
@@ -252,6 +252,52 @@ client_get_for_context (DBusGMethodInvocation *context)
 	return cd;
 }
 
+
+static ClientData *
+client_get_for_invocation (GDBusMethodInvocation *invocation)
+{
+	ClientData *cd;
+	gchar *sender;
+
+	if (!client_lookup_enabled) {
+		return NULL;
+	}
+
+	/* Only really done with tracker-extract where we use
+	 * functions from the command line with dbus code in them.
+	 */
+	if (!invocation) {
+		return NULL;
+	}
+
+	// TODO  -> GDBus port: below is not true anymore (but strdup for
+	// compatibility for now)
+
+	/* Shame we have to allocate memory in any condition here,
+	 * sucky glib D-Bus API is to blame here :/
+	 */
+	sender = g_strdup (g_dbus_method_invocation_get_sender (invocation));
+
+	if (G_UNLIKELY (!clients)) {
+		clients_init ();
+	}
+
+	cd = g_hash_table_lookup (clients, sender);
+	if (!cd) {
+		cd = client_data_new (sender);
+		g_hash_table_insert (clients, sender, cd);
+	} else {
+		g_free (sender);
+		g_source_remove (cd->clean_up_id);
+	}
+
+	cd->clean_up_id = g_timeout_add_seconds (CLIENT_CLEAN_UP_TIME, client_clean_up_cb, cd);
+
+	g_get_current_time (&cd->last_time);
+
+	return cd;
+}
+
 GQuark
 tracker_dbus_error_quark (void)
 {
@@ -317,6 +363,32 @@ tracker_dbus_request_new (gint                   request_id,
 }
 
 void
+tracker_gdbus_request_new (gint                  request_id,
+                           GDBusMethodInvocation *invocation,
+                           const gchar           *format,
+                           ...)
+{
+	ClientData *cd;
+	gchar *str;
+	va_list args;
+
+	va_start (args, format);
+	str = g_strdup_vprintf (format, args);
+	va_end (args);
+
+	cd = client_get_for_invocation (invocation);
+
+	g_debug ("<--- [%d%s%s|%lu] %s",
+	         request_id,
+	         cd ? "|" : "",
+	         cd ? cd->binary : "",
+	         cd ? cd->pid : 0,
+	         str);
+
+	g_free (str);
+}
+
+void
 tracker_dbus_request_success (gint                   request_id,
                               DBusGMethodInvocation *context)
 {
@@ -332,6 +404,21 @@ tracker_dbus_request_success (gint                   request_id,
 }
 
 void
+tracker_gdbus_request_success (gint                   request_id,
+                               GDBusMethodInvocation *invocation)
+{
+	ClientData *cd;
+
+	cd = client_get_for_invocation (invocation);
+
+	g_debug ("---> [%d%s%s|%lu] Success, no error given",
+	         request_id,
+	         cd ? "|" : "",
+	         cd ? cd->binary : "",
+	         cd ? cd->pid : 0);
+}
+
+void
 tracker_dbus_request_failed (gint                    request_id,
                              DBusGMethodInvocation  *context,
                              GError                **error,
@@ -367,6 +454,41 @@ tracker_dbus_request_failed (gint                    request_id,
 }
 
 void
+tracker_gdbus_request_failed (gint                    request_id,
+                              GDBusMethodInvocation  *invocation,
+                              GError                **error,
+                              const gchar            *format,
+                              ...)
+{
+	ClientData *cd;
+	gchar *str;
+	va_list args;
+
+	if (format) {
+		va_start (args, format);
+		str = g_strdup_vprintf (format, args);
+		va_end (args);
+
+		g_set_error (error, TRACKER_DBUS_ERROR, 0, "%s", str);
+	} else if (*error != NULL) {
+		str = g_strdup ((*error)->message);
+	} else {
+		str = g_strdup (_("No error given"));
+		g_warning ("Unset error and no error message.");
+	}
+
+	cd = client_get_for_invocation (invocation);
+
+	g_message ("---> [%d%s%s|%lu] Failed, %s",
+	           request_id,
+	           cd ? "|" : "",
+	           cd ? cd->binary : "",
+	           cd ? cd->pid : 0,
+	           str);
+	g_free (str);
+}
+
+void
 tracker_dbus_request_info (gint                   request_id,
                            DBusGMethodInvocation *context,
                            const gchar           *format,
diff --git a/src/libtracker-common/tracker-dbus.h b/src/libtracker-common/tracker-dbus.h
index 50b70f5..afa9517 100644
--- a/src/libtracker-common/tracker-dbus.h
+++ b/src/libtracker-common/tracker-dbus.h
@@ -78,6 +78,25 @@ G_BEGIN_DECLS
 		}; \
 	} G_STMT_END
 
+
+#define tracker_gdbus_async_return_if_fail(expr,invocation)	\
+	G_STMT_START { \
+		if G_LIKELY(expr) { } else { \
+			GError *assert_error = NULL; \
+	  \
+			g_set_error (&assert_error, \
+			             TRACKER_DBUS_ERROR, \
+			             TRACKER_DBUS_ERROR_ASSERTION_FAILED, \
+			             _("Assertion `%s' failed"), \
+			             #expr); \
+	  \
+			g_dbus_method_invocation_return_gerror (invocation, assert_error); \
+			g_clear_error (&assert_error); \
+	  \
+			return; \
+		}; \
+	} G_STMT_END
+
 #define tracker_dbus_return_val_if_fail(expr,val,error)	\
 	G_STMT_START { \
 		if G_LIKELY(expr) { } else { \
@@ -124,17 +143,33 @@ gchar **         tracker_dbus_slist_to_strv          (GSList
 /* Requests */
 guint            tracker_dbus_get_next_request_id    (void);
 
+guint            tracker_gdbus_get_next_request_id   (void);
+
 void             tracker_dbus_request_new            (gint                        request_id,
                                                       DBusGMethodInvocation      *context,
                                                       const gchar                *format,
                                                       ...);
+void             tracker_gdbus_request_new           (gint                        request_id,
+                                                      GDBusMethodInvocation      *invocation,
+                                                      const gchar                *format,
+                                                      ...);
+
 void             tracker_dbus_request_success        (gint                        request_id,
                                                       DBusGMethodInvocation      *context);
+void             tracker_gdbus_request_success       (gint                        request_id,
+                                                      GDBusMethodInvocation      *invocation);
+
 void             tracker_dbus_request_failed         (gint                        request_id,
                                                       DBusGMethodInvocation      *context,
                                                       GError                    **error,
                                                       const gchar                *format,
                                                       ...);
+void             tracker_gdbus_request_failed        (gint                        request_id,
+                                                      GDBusMethodInvocation      *invocation,
+                                                      GError                    **error,
+                                                      const gchar                *format,
+                                                      ...);
+
 void             tracker_dbus_request_comment        (gint                        request_id,
                                                       DBusGMethodInvocation      *context,
                                                       const gchar                *format,



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