Skip to content

Commit

Permalink
Use GDBus instead of dbus-glib
Browse files Browse the repository at this point in the history
dbus-glib is deprecated since Debian stretch (Debian 9) and
Ubuntu 18.04 (bionic)
  • Loading branch information
kenhys committed Apr 17, 2020
1 parent 3266bb2 commit 248cbd1
Showing 1 changed file with 94 additions and 54 deletions.
148 changes: 94 additions & 54 deletions subscribe/rhythmbox/rhythmbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
#include <stdio.h>

#include <gmodule.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <gio/gio.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
Expand All @@ -40,8 +39,8 @@
#define REQUEST_TIMEOUT (5)

static SUBSCRIPTOR_CONTEXT* sc;
static DBusGConnection *conn;
static DBusGProxy* proxy;
static GDBusConnection *conn;
static GDBusProxy* proxy;
static gboolean enable = FALSE;

static gchar* last_title;
Expand Down Expand Up @@ -152,100 +151,141 @@ static gboolean
get_rhythmbox_info(gpointer GOL_UNUSED_ARG(data)) {
if (!enable) return FALSE;

DBusGProxy *player = NULL;
DBusGProxy *shell = NULL;
GDBusProxy *player = NULL;
GDBusProxy *shell = NULL;
GError *error = NULL;

if (!conn) {
conn = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
if (error) g_error_free(error);
if (!conn) return FALSE;
}

if (!proxy) {
proxy = dbus_g_proxy_new_for_name(
proxy = g_dbus_proxy_new_sync(
conn,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
"org.freedesktop.DBus",
"/org/freedesktop/DBus",
"org.freedesktop.DBus");
"org.freedesktop.DBus",
NULL,
&error);
if (error) g_error_free(error);
if (!proxy) return FALSE;
}

gboolean exists = FALSE;
if (dbus_g_proxy_call_with_timeout(
proxy,
"NameHasOwner",
5000,
&error,
G_TYPE_STRING,
"org.gnome.Rhythmbox",
G_TYPE_INVALID,
G_TYPE_BOOLEAN,
&exists,
G_TYPE_INVALID)) {
GVariant *parameters = g_variant_new("(sb)", "org.gnome.Rhythmbox", exists);
GVariant *variants = g_dbus_proxy_call_sync(
proxy,
"NameHasOwner",
parameters,
G_DBUS_CALL_FLAGS_NONE,
5000,
NULL,
&error);
if (!variants) {
if (error) g_error_free(error);
if (!exists) return TRUE;
return TRUE;
} else {
g_variant_get(parameters, "b", &exists);
if (!exists) {
g_variant_unref(parameters);
g_variant_unref(variants);
return TRUE;
}
g_variant_unref(parameters);
g_variant_unref(variants);
}

if (!shell) {
shell = dbus_g_proxy_new_for_name(
shell = g_dbus_proxy_new_sync(
conn,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
"org.gnome.Rhythmbox",
"/org/gnome/Rhythmbox/Shell",
"org.gnome.Rhythmbox.Shell");
"org.gnome.Rhythmbox.Shell",
NULL,
NULL);
}

if (!player) {
player = dbus_g_proxy_new_for_name(
player = g_dbus_proxy_new_sync(
conn,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
"org.gnome.Rhythmbox",
"/org/gnome/Rhythmbox/Player",
"org.gnome.Rhythmbox.Player");
"org.gnome.Rhythmbox.Player",
NULL,
NULL);
}

gboolean playing;
if (!dbus_g_proxy_call_with_timeout(
player,
"getPlaying",
5000,
&error,
G_TYPE_INVALID,
G_TYPE_BOOLEAN,
&playing,
G_TYPE_INVALID)) {
gboolean playing = FALSE;
parameters = g_variant_new("(b)", playing);
variants = g_dbus_proxy_call_sync(
player,
"getPlaying",
parameters,
G_DBUS_CALL_FLAGS_NONE,
5000,
NULL,
&error);
if (!variants) {
if (error) g_error_free(error);
if (!playing) return TRUE;
g_variant_unref(parameters);
return TRUE;
} else {
g_variant_get(parameters, "b", &playing);
if (!playing) {
g_variant_unref(variants);
return TRUE;
}
g_variant_unref(parameters);
g_variant_unref(variants);
}


char *uri;
if (!dbus_g_proxy_call_with_timeout(
player,
"getPlayingUri",
5000,
&error,
G_TYPE_INVALID,
G_TYPE_STRING,
&uri,
G_TYPE_INVALID)) {
parameters = g_variant_new("(s)");
variants = g_dbus_proxy_call_sync(
player,
"getPlayingUri",
parameters,
G_DBUS_CALL_FLAGS_NONE,
5000,
NULL,
&error);
if (!variants) {
if (error) g_error_free(error);
g_variant_unref(parameters);
return TRUE;
} else {
g_variant_get(parameters, "s", &uri);
g_variant_unref(parameters);
g_variant_unref(variants);
}

GHashTable *table;
if (!dbus_g_proxy_call_with_timeout(
parameters = g_variant_new("(sv)", uri, &table);
variants = g_dbus_proxy_call_sync(
shell,
"getSongProperties",
parameters,
G_DBUS_CALL_FLAGS_NONE,
5000,
&error,
G_TYPE_STRING,
uri,
G_TYPE_INVALID,
dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE),
&table,
G_TYPE_INVALID)) {
NULL,
&error);
if (!variants) {
if (error) g_error_free(error);
g_variant_unref(parameters);
return TRUE;
} else {
g_variant_get(parameters, "v", &table);
g_variant_unref(parameters);
g_variant_unref(variants);
}
g_free(uri);

Expand Down

0 comments on commit 248cbd1

Please sign in to comment.