dbus-client: Rewrite with GDBus

This is just a direct port and should change no logic
This commit is contained in:
Patrick Griffis 2016-07-13 23:39:38 -04:00
parent 439ff094ce
commit ec4d3de9d2
1 changed files with 86 additions and 45 deletions

View File

@ -21,20 +21,22 @@
#include "config.h" #include "config.h"
#define GLIB_DISABLE_DEPRECATION_WARNINGS
#include <stdlib.h>
#include <dbus/dbus-glib.h>
#include "dbus-client.h" #include "dbus-client.h"
#include <stdlib.h>
#include <gio/gio.h>
#include "hexchat.h" #include "hexchat.h"
#include "hexchatc.h" #include "hexchatc.h"
#define DBUS_SERVICE "org.hexchat.service" #define DBUS_SERVICE "org.hexchat.service"
#define DBUS_REMOTE "/org/hexchat/Remote" #define DBUS_REMOTE_PATH "/org/hexchat/Remote"
#define DBUS_REMOTE_INTERFACE "org.hexchat.plugin" #define DBUS_REMOTE_INTERFACE "org.hexchat.plugin"
#define DBUS_SERVICE_DBUS "org.freedesktop.DBus"
#define DBUS_PATH_DBUS "/org/freedesktop/DBus"
#define DBUS_INTERFACE_DBUS "org.freedesktop.DBus"
static void static void
write_error (char *message, write_error (char *message, GError **error)
GError **error)
{ {
if (error == NULL || *error == NULL) { if (error == NULL || *error == NULL) {
return; return;
@ -43,6 +45,15 @@ write_error (char *message,
g_clear_error (error); g_clear_error (error);
} }
static inline GVariant *
new_param_variant (const char *arg)
{
GVariant * const args[1] = {
g_variant_new_string (arg)
};
return g_variant_new_tuple (args, 1);
}
void void
hexchat_remote (void) hexchat_remote (void)
/* TODO: dbus_g_connection_unref (connection) are commented because it makes /* TODO: dbus_g_connection_unref (connection) are commented because it makes
@ -50,21 +61,15 @@ hexchat_remote (void)
* https://launchpad.net/distros/ubuntu/+source/dbus/+bug/54375 * https://launchpad.net/distros/ubuntu/+source/dbus/+bug/54375
*/ */
{ {
DBusGConnection *connection; GDBusConnection *connection;
DBusGProxy *dbus = NULL; GDBusProxy *dbus = NULL;
DBusGProxy *remote_object = NULL; GVariant *ret;
GDBusProxy *remote_object = NULL;
gboolean hexchat_running; gboolean hexchat_running;
GError *error = NULL; GError *error = NULL;
char *command = NULL; char *command = NULL;
int i; int i;
/* GnomeVFS >=2.15 uses D-Bus and threads, so threads should be
* initialised before opening for the first time a D-Bus connection */
if (!g_thread_supported ()) {
g_thread_init (NULL);
}
dbus_g_thread_init ();
/* if there is nothing to do, return now. */ /* if there is nothing to do, return now. */
if (!arg_existing || !(arg_url || arg_urls || arg_command)) { if (!arg_existing || !(arg_url || arg_urls || arg_command)) {
return; return;
@ -72,36 +77,63 @@ hexchat_remote (void)
arg_dont_autoconnect = TRUE; arg_dont_autoconnect = TRUE;
connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error); connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
if (!connection) { if (!connection)
{
write_error (_("Couldn't connect to session bus"), &error); write_error (_("Couldn't connect to session bus"), &error);
return; return;
} }
/* Checks if HexChat is already running */ /* Checks if HexChat is already running */
dbus = dbus_g_proxy_new_for_name (connection, dbus = g_dbus_proxy_new_sync (connection,
DBUS_SERVICE_DBUS, G_DBUS_PROXY_FLAGS_NONE,
DBUS_PATH_DBUS, NULL,
DBUS_INTERFACE_DBUS); DBUS_SERVICE_DBUS,
if (!dbus_g_proxy_call (dbus, "NameHasOwner", &error, DBUS_PATH_DBUS,
G_TYPE_STRING, DBUS_SERVICE, DBUS_INTERFACE_DBUS,
G_TYPE_INVALID, NULL,
G_TYPE_BOOLEAN, &hexchat_running, &error);
G_TYPE_INVALID)) {
ret = g_dbus_proxy_call_sync (dbus, "NameHasOwner",
new_param_variant (DBUS_SERVICE),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);
if (!ret)
{
write_error (_("Failed to complete NameHasOwner"), &error); write_error (_("Failed to complete NameHasOwner"), &error);
hexchat_running = FALSE; hexchat_running = FALSE;
} }
else
{
GVariant *child = g_variant_get_child_value (ret, 0);
hexchat_running = g_variant_get_boolean (child);
g_variant_unref (ret);
g_variant_unref (child);
}
g_object_unref (dbus); g_object_unref (dbus);
if (!hexchat_running) { if (!hexchat_running) {
/* dbus_g_connection_unref (connection); */ g_object_unref (connection);
return; return;
} }
remote_object = dbus_g_proxy_new_for_name (connection, remote_object = g_dbus_proxy_new_sync (connection,
DBUS_SERVICE, G_DBUS_PROXY_FLAGS_NONE,
DBUS_REMOTE, NULL,
DBUS_REMOTE_INTERFACE); DBUS_SERVICE,
DBUS_REMOTE_PATH,
DBUS_REMOTE_INTERFACE,
NULL,
&error);
if (!remote_object)
{
write_error("Failed to connect to HexChat", &error);
g_object_unref (connection);
exit (0);
}
if (arg_url) { if (arg_url) {
command = g_strdup_printf ("url %s", arg_url); command = g_strdup_printf ("url %s", arg_url);
@ -109,31 +141,40 @@ hexchat_remote (void)
command = g_strdup (arg_command); command = g_strdup (arg_command);
} }
if (command) { if (command)
if (!dbus_g_proxy_call (remote_object, "Command", {
&error, g_dbus_proxy_call_sync (remote_object, "Command",
G_TYPE_STRING, command, new_param_variant (command),
G_TYPE_INVALID,G_TYPE_INVALID)) { G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);
if (error)
write_error (_("Failed to complete Command"), &error); write_error (_("Failed to complete Command"), &error);
}
g_free (command); g_free (command);
} }
if (arg_urls) if (arg_urls)
{ {
for (i = 0; i < g_strv_length(arg_urls); i++) for (i = 0; i < g_strv_length(arg_urls); i++)
{ {
command = g_strdup_printf ("url %s", arg_urls[i]); command = g_strdup_printf ("url %s", arg_urls[i]);
if (!dbus_g_proxy_call (remote_object, "Command",
&error, g_dbus_proxy_call_sync (remote_object, "Command",
G_TYPE_STRING, command, new_param_variant (command),
G_TYPE_INVALID, G_TYPE_INVALID)) { G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);
if (error)
write_error (_("Failed to complete Command"), &error); write_error (_("Failed to complete Command"), &error);
}
g_free (command); g_free (command);
} }
g_strfreev (arg_urls); g_strfreev (arg_urls);
} }
g_object_unref (remote_object);
g_object_unref (connection);
exit (0); exit (0);
} }