[telepathy-mission-control/master] mcd-misc: copy _tp_dbus_daemon_request_name from telepathy-glib
Simon McVittie
simon.mcvittie at collabora.co.uk
Wed May 13 08:19:09 PDT 2009
MC's current implementation of requesting bus names is wrong; it doesn't
handle rejection from dbus-daemon, only outright errors.
This should perhaps be made public API in a future version of
telepathy-glib.
---
src/mcd-misc.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
src/mcd-misc.h | 6 ++++
2 files changed, 80 insertions(+), 2 deletions(-)
diff --git a/src/mcd-misc.c b/src/mcd-misc.c
index 13dfe70..6acb9fc 100644
--- a/src/mcd-misc.c
+++ b/src/mcd-misc.c
@@ -3,8 +3,8 @@
/*
* This file is part of mission-control
*
- * Copyright (C) 2007-2009 Nokia Corporation.
- * Copyright (C) 2009 Collabora Ltd.
+ * Copyright (C) 2005-2009 Nokia Corporation.
+ * Copyright (C) 2005-2009 Collabora Ltd.
*
* Contact: Alberto Mardegan <alberto.mardegan at nokia.com>
*
@@ -39,6 +39,9 @@
#include "_gen/signals-marshal.h"
#include "_gen/register-dbus-glib-marshallers-body.h"
+#include <dbus/dbus.h>
+#include <dbus/dbus-glib-lowlevel.h>
+
/*
* Miscellaneus functions
*/
@@ -495,3 +498,72 @@ out:
return retval;
}
+/* Copied from telepathy-glib, licensed under LGPL >= 2.1 */
+/*
+ * _mcd_dbus_daemon_request_name:
+ * @self: a TpDBusDaemon
+ * @well_known_name: a well-known name to acquire
+ * @idempotent: whether to consider it to be a success if this process
+ * already owns the name
+ * @error: used to raise an error if %FALSE is returned
+ *
+ * Claim the given well-known name without queueing, allowing replacement
+ * or replacing an existing name-owner.
+ */
+gboolean
+_mcd_dbus_daemon_request_name (TpDBusDaemon *self,
+ const gchar *well_known_name,
+ gboolean idempotent,
+ GError **error)
+{
+ TpProxy *as_proxy = (TpProxy *) self;
+ DBusGConnection *gconn = as_proxy->dbus_connection;
+ DBusConnection *dbc = dbus_g_connection_get_connection (gconn);
+ DBusError dbus_error;
+ int result;
+
+ g_return_val_if_fail (TP_IS_DBUS_DAEMON (self), FALSE);
+ g_return_val_if_fail (tp_dbus_check_valid_bus_name (well_known_name,
+ TP_DBUS_NAME_TYPE_WELL_KNOWN, error), FALSE);
+
+ dbus_error_init (&dbus_error);
+ result = dbus_bus_request_name (dbc, well_known_name,
+ DBUS_NAME_FLAG_DO_NOT_QUEUE, &dbus_error);
+
+ switch (result)
+ {
+ case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
+ return TRUE;
+
+ case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
+ if (idempotent)
+ {
+ return TRUE;
+ }
+ else
+ {
+ g_set_error (error, TP_ERRORS, TP_ERROR_NOT_AVAILABLE,
+ "Name '%s' already in use by this process", well_known_name);
+ return FALSE;
+ }
+
+ case DBUS_REQUEST_NAME_REPLY_EXISTS:
+ case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
+ /* the latter shouldn't actually happen since we said DO_NOT_QUEUE */
+ g_set_error (error, TP_ERRORS, TP_ERROR_NOT_AVAILABLE,
+ "Name '%s' already in use by another process", well_known_name);
+ return FALSE;
+
+ case -1:
+ g_set_error (error, TP_ERRORS, TP_ERROR_NOT_AVAILABLE,
+ "%s: %s", dbus_error.name, dbus_error.message);
+ dbus_error_free (&dbus_error);
+ return FALSE;
+
+ default:
+ g_set_error (error, TP_ERRORS, TP_ERROR_NOT_AVAILABLE,
+ "RequestName('%s') returned %d and I don't know what that means",
+ well_known_name, result);
+ return FALSE;
+ }
+}
diff --git a/src/mcd-misc.h b/src/mcd-misc.h
index cd93ae5..a7b7653 100644
--- a/src/mcd-misc.h
+++ b/src/mcd-misc.h
@@ -29,6 +29,7 @@
#include <glib.h>
#include <glib-object.h>
+#include <telepathy-glib/dbus.h>
#include <telepathy-glib/gtypes.h>
G_BEGIN_DECLS
@@ -64,5 +65,10 @@ gboolean _mcd_file_set_contents (const gchar *filename, const gchar *contents,
G_GNUC_INTERNAL
void _mc_ext_register_dbus_glib_marshallers (void);
+gboolean _mcd_dbus_daemon_request_name (TpDBusDaemon *self,
+ const gchar *well_known_name,
+ gboolean idempotent,
+ GError **error);
+
G_END_DECLS
#endif /* MCD_MISC_H */
--
1.5.6.5
More information about the telepathy-commits
mailing list