Back to index

indicator-datetime  12.10.0
datetime-interface.c
Go to the documentation of this file.
00001 /*
00002 An indicator to time and date related information in the menubar.
00003 
00004 Copyright 2010 Canonical Ltd.
00005 
00006 Authors:
00007     Ted Gould <ted@canonical.com>
00008 
00009 This program is free software: you can redistribute it and/or modify it 
00010 under the terms of the GNU General Public License version 3, as published 
00011 by the Free Software Foundation.
00012 
00013 This program is distributed in the hope that it will be useful, but 
00014 WITHOUT ANY WARRANTY; without even the implied warranties of 
00015 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
00016 PURPOSE.  See the GNU General Public License for more details.
00017 
00018 You should have received a copy of the GNU General Public License along 
00019 with this program.  If not, see <http://www.gnu.org/licenses/>.
00020 */
00021 
00022 #ifdef HAVE_CONFIG_H
00023 #include "config.h"
00024 #endif
00025 
00026 #include <gio/gio.h>
00027 
00028 #include "datetime-interface.h"
00029 #include "gen-datetime-service.xml.h"
00030 #include "dbus-shared.h"
00031 
00040 struct _DatetimeInterfacePrivate {
00041        GDBusConnection * bus;
00042        GCancellable * bus_cancel;
00043        guint dbus_registration;
00044 };
00045 
00046 #define DATETIME_INTERFACE_GET_PRIVATE(o) (DATETIME_INTERFACE(o)->priv)
00047 
00048 /* GDBus Stuff */
00049 static GDBusNodeInfo *      node_info = NULL;
00050 static GDBusInterfaceInfo * interface_info = NULL;
00051 
00052 static void datetime_interface_class_init (DatetimeInterfaceClass *klass);
00053 static void datetime_interface_init       (DatetimeInterface *self);
00054 static void datetime_interface_dispose    (GObject *object);
00055 static void datetime_interface_finalize   (GObject *object);
00056 static void bus_get_cb (GObject * object, GAsyncResult * res, gpointer user_data);
00057 
00058 G_DEFINE_TYPE (DatetimeInterface, datetime_interface, G_TYPE_OBJECT);
00059 
00060 static void
00061 datetime_interface_class_init (DatetimeInterfaceClass *klass)
00062 {
00063        GObjectClass *object_class = G_OBJECT_CLASS (klass);
00064 
00065        g_type_class_add_private (klass, sizeof (DatetimeInterfacePrivate));
00066 
00067        object_class->dispose = datetime_interface_dispose;
00068        object_class->finalize = datetime_interface_finalize;
00069 
00070        /* Setting up the DBus interfaces */
00071        if (node_info == NULL) {
00072               GError * error = NULL;
00073 
00074               node_info = g_dbus_node_info_new_for_xml(_datetime_service, &error);
00075               if (error != NULL) {
00076                      g_error("Unable to parse Datetime Service Interface description: %s", error->message);
00077                      g_error_free(error);
00078               }
00079        }
00080 
00081        if (interface_info == NULL) {
00082               interface_info = g_dbus_node_info_lookup_interface(node_info, SERVICE_IFACE);
00083 
00084               if (interface_info == NULL) {
00085                      g_error("Unable to find interface '" SERVICE_IFACE "'");
00086               }
00087        }
00088 
00089        return;
00090 }
00091 
00092 static void
00093 datetime_interface_init (DatetimeInterface *self)
00094 {
00095        self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, DATETIME_INTERFACE_TYPE, DatetimeInterfacePrivate);
00096 
00097        self->priv->bus = NULL;
00098        self->priv->bus_cancel = NULL;
00099        self->priv->dbus_registration = 0;
00100 
00101        self->priv->bus_cancel = g_cancellable_new();
00102        g_bus_get(G_BUS_TYPE_SESSION,
00103                  self->priv->bus_cancel,
00104                  bus_get_cb,
00105                  self);
00106 
00107        return;
00108 }
00109 
00110 static void
00111 bus_get_cb (GObject * object, GAsyncResult * res, gpointer user_data)
00112 {
00113        GError * error = NULL;
00114        GDBusConnection * connection = g_bus_get_finish(res, &error);
00115 
00116        if (error != NULL) {
00117               g_error("OMG! Unable to get a connection to DBus: %s", error->message);
00118               g_error_free(error);
00119               return;
00120        }
00121 
00122        DatetimeInterfacePrivate * priv = DATETIME_INTERFACE_GET_PRIVATE(user_data);
00123 
00124        g_warn_if_fail(priv->bus == NULL);
00125        priv->bus = connection;
00126 
00127        g_clear_object (&priv->bus_cancel);
00128 
00129        /* Now register our object on our new connection */
00130        priv->dbus_registration = g_dbus_connection_register_object(priv->bus,
00131                                                                    SERVICE_OBJ,
00132                                                                    interface_info,
00133                                                                    NULL,
00134                                                                    user_data,
00135                                                                    NULL,
00136                                                                    &error);
00137 
00138        if (error != NULL) {
00139               g_error("Unable to register the object to DBus: %s", error->message);
00140               g_error_free(error);
00141               return;
00142        }
00143 
00144        return;       
00145 }
00146 
00147 static void
00148 datetime_interface_dispose (GObject *object)
00149 {
00150        DatetimeInterfacePrivate * priv = DATETIME_INTERFACE_GET_PRIVATE(object);
00151 
00152        if (priv->dbus_registration != 0) {
00153               g_dbus_connection_unregister_object(priv->bus, priv->dbus_registration);
00154               /* Don't care if it fails, there's nothing we can do */
00155               priv->dbus_registration = 0;
00156        }
00157 
00158        g_clear_object (&priv->bus);
00159 
00160        if (priv->bus_cancel != NULL) {
00161               g_cancellable_cancel(priv->bus_cancel);
00162               g_clear_object (&priv->bus_cancel);
00163        }
00164 
00165        G_OBJECT_CLASS (datetime_interface_parent_class)->dispose (object);
00166        return;
00167 }
00168 
00169 static void
00170 datetime_interface_finalize (GObject *object)
00171 {
00172 
00173        G_OBJECT_CLASS (datetime_interface_parent_class)->finalize (object);
00174        return;
00175 }
00176 
00177 void
00178 datetime_interface_update (DatetimeInterface *self)
00179 {
00180        g_return_if_fail(IS_DATETIME_INTERFACE(self));
00181 
00182        DatetimeInterfacePrivate * priv = DATETIME_INTERFACE_GET_PRIVATE(self);
00183        GError * error = NULL;
00184 
00185        g_dbus_connection_emit_signal (priv->bus,
00186                                       NULL,
00187                                       SERVICE_OBJ,
00188                                       SERVICE_IFACE,
00189                                       "UpdateTime",
00190                                       NULL,
00191                                       &error);
00192 
00193        if (error != NULL) {
00194               g_error("Unable to send UpdateTime signal: %s", error->message);
00195               g_error_free(error);
00196               return;
00197        }
00198 
00199        return;
00200 }