Skip to content
Permalink
Browse files

Migrate mate-panel from dbus-glib to gdbus

- Code optimization
- Put GDBusProxy in PanelSessionManager struct
  • Loading branch information...
yetist authored and raveit65 committed Sep 4, 2018
1 parent a73abb8 commit a34f531da4f03eecfc38414295ebe70742dfad71
@@ -28,74 +28,80 @@
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>

#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <gio/gio.h>

#include "set-timezone.h"

#define DATETIME_DBUS_NAME "org.mate.SettingsDaemon.DateTimeMechanism"
#define DATETIME_DBUS_PATH "/"

static DBusGConnection *
get_system_bus (void)
static GDBusProxy *
get_bus_proxy (void)
{
GError *error;
static DBusGConnection *bus = NULL;

if (bus == NULL) {
error = NULL;
bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
if (bus == NULL) {
g_warning ("Couldn't connect to system bus: %s",
error->message);
g_error_free (error);
GError *error = NULL;
static GDBusProxy *proxy = NULL;
if (proxy == NULL) {
proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
DATETIME_DBUS_NAME,
DATETIME_DBUS_PATH,
DATETIME_DBUS_NAME,
NULL,
&error);
if (proxy == NULL) {
g_warning ("Unable to contact datetime settings daemon: %s\n", error->message);
g_error_free (error);
}
}

return bus;
}
return proxy;
}

#define CACHE_VALIDITY_SEC 2

typedef void (*CanDoFunc) (gint value);

static void
notify_can_do (DBusGProxy *proxy,
DBusGProxyCall *call,
void *user_data)
notify_can_do (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
GDBusProxy *proxy;
GVariant *variant;
GError *error = NULL;
gint32 value;

CanDoFunc callback = user_data;
GError *error = NULL;
gint value;

if (dbus_g_proxy_end_call (proxy, call,
&error,
G_TYPE_INT, &value,
G_TYPE_INVALID)) {
proxy = get_bus_proxy ();
variant = g_dbus_proxy_call_finish (proxy, res, &error);
if (variant == NULL) {
g_warning ("Call can set time zone dbus method: %s", error->message);
g_error_free (error);
} else {
g_variant_get (variant, "(i)", &value);
g_variant_unref (variant);
callback (value);
}
}

static void
refresh_can_do (const gchar *action, CanDoFunc callback)
{
DBusGConnection *bus;
DBusGProxy *proxy;

bus = get_system_bus ();
if (bus == NULL)
return;

proxy = dbus_g_proxy_new_for_name (bus,
"org.mate.SettingsDaemon.DateTimeMechanism",
"/",
"org.mate.SettingsDaemon.DateTimeMechanism");

dbus_g_proxy_begin_call_with_timeout (proxy,
action,
notify_can_do,
callback, NULL,
INT_MAX,
G_TYPE_INVALID);
GDBusProxy *proxy;

proxy = get_bus_proxy ();
if (proxy == NULL)
return;

g_dbus_proxy_call (proxy,
action,
g_variant_new ("()"),
G_DBUS_CALL_FLAGS_NONE,
G_MAXINT,
NULL,
notify_can_do,
callback);
}

static gint settimezone_cache = 0;
@@ -171,75 +177,61 @@ free_data (gpointer d)
}

static void
set_time_notify (DBusGProxy *proxy,
DBusGProxyCall *call,
void *user_data)
set_time_notify (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
SetTimeCallbackData *data = user_data;
GError *error = NULL;

if (dbus_g_proxy_end_call (proxy, call, &error, G_TYPE_INVALID)) {
if (data->callback)
data->callback (data->data, NULL);
}
else {
if (error->domain == DBUS_GERROR &&
error->code == DBUS_GERROR_NO_REPLY) {
/* these errors happen because dbus doesn't
* use monotonic clocks
*/
g_warning ("ignoring no-reply error when setting time");
SetTimeCallbackData *data = user_data;
GError *error = NULL;
GDBusProxy *proxy;
GVariant *variant;

proxy = get_bus_proxy ();
variant = g_dbus_proxy_call_finish (proxy, res, &error);
if (variant == NULL) {
if (error != NULL) {
if (data->callback)
data->callback (data->data, error);
g_error_free (error);
} else {
if (data->callback)
data->callback (data->data, NULL);
}
else {
if (data->callback)
data->callback (data->data, error);
else
g_error_free (error);
}
} else {
g_variant_unref (variant);
if (data->callback)
data->callback (data->data, NULL);
}
}

static void
set_time_async (SetTimeCallbackData *data)
{
DBusGConnection *bus;
DBusGProxy *proxy;
GDBusProxy *proxy;

bus = get_system_bus ();
if (bus == NULL)
return;

proxy = dbus_g_proxy_new_for_name (bus,
"org.mate.SettingsDaemon.DateTimeMechanism",
"/",
"org.mate.SettingsDaemon.DateTimeMechanism");
proxy = get_bus_proxy ();
if (proxy == NULL)
return;

data->ref_count++;
if (strcmp (data->call, "SetTime") == 0)
dbus_g_proxy_begin_call_with_timeout (proxy,
"SetTime",
set_time_notify,
data, free_data,
INT_MAX,
/* parameters: */
G_TYPE_INT64, data->time,
G_TYPE_INVALID,
/* return values: */
G_TYPE_INVALID);
g_dbus_proxy_call (proxy,
"SetTime",
g_variant_new ("(x)", data->time),
G_DBUS_CALL_FLAGS_NONE,
G_MAXINT,
NULL,
set_time_notify,
data);
else
dbus_g_proxy_begin_call_with_timeout (proxy,
"SetTimezone",
set_time_notify,
data, free_data,
INT_MAX,
/* parameters: */
G_TYPE_STRING, data->filename,
G_TYPE_INVALID,
/* return values: */
G_TYPE_INVALID);
g_dbus_proxy_call (proxy,
"SetTimezone",
g_variant_new ("(s)", data->filename),
G_DBUS_CALL_FLAGS_NONE,
G_MAXINT,
NULL,
set_time_notify,
data);
}

void
@@ -60,7 +60,6 @@ PANGO_REQUIRED=1.15.4
GLIB_REQUIRED=2.50.0
LIBMATE_MENU_REQUIRED=1.21.0
CAIRO_REQUIRED=1.0.0
DBUS_GLIB_REQUIRED=0.80
DCONF_REQUIRED=0.13.4
LIBRSVG_REQUIRED=2.36.2
GTK_REQUIRED=3.22.0
@@ -72,7 +71,7 @@ dnl pkg-config dependency checks
PKG_CHECK_MODULES(EGG_SMCLIENT, ice sm gtk+-3.0)

PKG_CHECK_MODULES(GMODULE, gmodule-2.0,[GMODULE_ADD="gmodule-2.0"],[GMODULE_ADD=""])
PKG_CHECK_MODULES(PANEL, $GMODULE_ADD gdk-pixbuf-2.0 >= $GDK_PIXBUF_REQUIRED pango >= $PANGO_REQUIRED gtk+-3.0 >= $GTK_REQUIRED glib-2.0 >= $GLIB_REQUIRED gio-unix-2.0 >= $GLIB_REQUIRED mate-desktop-2.0 >= $LIBMATE_DESKTOP_REQUIRED gio-2.0 >= $GLIB_REQUIRED libmate-menu >= $LIBMATE_MENU_REQUIRED dbus-glib-1 >= $DBUS_GLIB_REQUIRED)
PKG_CHECK_MODULES(PANEL, $GMODULE_ADD gdk-pixbuf-2.0 >= $GDK_PIXBUF_REQUIRED pango >= $PANGO_REQUIRED gtk+-3.0 >= $GTK_REQUIRED glib-2.0 >= $GLIB_REQUIRED gio-unix-2.0 >= $GLIB_REQUIRED mate-desktop-2.0 >= $LIBMATE_DESKTOP_REQUIRED gio-2.0 >= $GLIB_REQUIRED libmate-menu >= $LIBMATE_MENU_REQUIRED)
AC_SUBST(PANEL_CFLAGS)
AC_SUBST(PANEL_LIBS)

@@ -101,12 +100,10 @@ PKG_CHECK_MODULES(TZ, gio-2.0 >= $GLIB_REQUIRED)
AC_SUBST(TZ_CFLAGS)
AC_SUBST(TZ_LIBS)

PKG_CHECK_MODULES(CLOCK, pango >= $PANGO_REQUIRED gtk+-3.0 >= $GTK_REQUIRED glib-2.0 >= $GLIB_REQUIRED gio-2.0 >= $GLIB_REQUIRED librsvg-2.0 >= $LIBRSVG_REQUIRED dbus-glib-1 mateweather >= $WEATHER_REQUIRED mate-desktop-2.0 >= $LIBMATE_DESKTOP_REQUIRED)
PKG_CHECK_MODULES(CLOCK, pango >= $PANGO_REQUIRED gtk+-3.0 >= $GTK_REQUIRED glib-2.0 >= $GLIB_REQUIRED gio-2.0 >= $GLIB_REQUIRED librsvg-2.0 >= $LIBRSVG_REQUIRED mateweather >= $WEATHER_REQUIRED mate-desktop-2.0 >= $LIBMATE_DESKTOP_REQUIRED)
AC_SUBST(CLOCK_CFLAGS)
AC_SUBST(CLOCK_LIBS)

DBUS_REQUIRED=1.1.2

# Make it possible to compile the applets in-process
PANEL_INPROCESS_NONE=
PANEL_INPROCESS_ALL=
@@ -14,8 +14,6 @@ AM_CFLAGS = $(WARN_CFLAGS)
libpanel_util_la_SOURCES = \
panel-cleanup.c \
panel-cleanup.h \
panel-dbus-service.c \
panel-dbus-service.h \
panel-color.c \
panel-color.h \
panel-error.c \
Oops, something went wrong.

0 comments on commit a34f531

Please sign in to comment.
You can’t perform that action at this time.