source: pkg/main/gnome-power-manager/branches/upstream/current/src/gpm-dbus-system-monitor.c @ 193

Revision 193, 4.4 KB checked in by alanbach-guest, 7 years ago (diff)

[svn-inject] Installing original source of gnome-power-manager

Line 
1/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2005-2006 Richard Hughes <richard@hughsie.com>
4 *
5 * Licensed under the GNU General Public License Version 2
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21
22#include "config.h"
23
24#include <string.h>
25#include <sys/time.h>
26#include <sys/types.h>
27#ifdef HAVE_UNISTD_H
28#include <unistd.h>
29#endif /* HAVE_UNISTD_H */
30
31#include <glib/gi18n.h>
32#include <dbus/dbus-glib.h>
33
34#include "gpm-marshal.h"
35#include "gpm-debug.h"
36
37#include "gpm-dbus-system-monitor.h"
38
39static void     gpm_dbus_system_monitor_class_init (GpmDbusSystemMonitorClass *klass);
40static void     gpm_dbus_system_monitor_init       (GpmDbusSystemMonitor      *dbus_system_monitor);
41static void     gpm_dbus_system_monitor_finalize   (GObject                   *object);
42
43#define GPM_DBUS_SYSTEM_MONITOR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GPM_TYPE_DBUS_SYSTEM_MONITOR, GpmDbusSystemMonitorPrivate))
44
45struct GpmDbusSystemMonitorPrivate
46{
47        DBusGConnection *connection;
48        DBusGProxy      *proxy;
49};
50
51enum {
52        NAME_OWNER_CHANGED,
53        LAST_SIGNAL
54};
55
56static guint         signals [LAST_SIGNAL] = { 0, };
57static gpointer      gpm_system_monitor_object = NULL;
58
59G_DEFINE_TYPE (GpmDbusSystemMonitor, gpm_dbus_system_monitor, G_TYPE_OBJECT)
60
61static void
62gpm_dbus_system_monitor_class_init (GpmDbusSystemMonitorClass *klass)
63{
64        GObjectClass   *object_class = G_OBJECT_CLASS (klass);
65        object_class->finalize     = gpm_dbus_system_monitor_finalize;
66        g_type_class_add_private (klass, sizeof (GpmDbusSystemMonitorPrivate));
67
68        signals [NAME_OWNER_CHANGED] =
69                g_signal_new ("name-owner-changed",
70                              G_TYPE_FROM_CLASS (object_class),
71                              G_SIGNAL_RUN_LAST,
72                              G_STRUCT_OFFSET (GpmDbusSystemMonitorClass, dbus_name_owner_changed),
73                              NULL,
74                              NULL,
75                              gpm_marshal_VOID__STRING_STRING_STRING,
76                              G_TYPE_NONE, 3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
77}
78
79static void
80dbus_name_owner_changed (DBusGProxy             *proxy,
81                         const char             *name,
82                         const char             *prev,
83                         const char             *new,
84                         GpmDbusSystemMonitor   *monitor)
85{
86        gpm_debug ("emitting name-owner-changed : %s (%s->%s)", name, prev, new);
87        g_signal_emit (monitor, signals [NAME_OWNER_CHANGED], 0, name, prev, new);
88}
89
90static void
91gpm_dbus_system_monitor_init (GpmDbusSystemMonitor *monitor)
92{
93        GError *error = NULL;
94        monitor->priv = GPM_DBUS_SYSTEM_MONITOR_GET_PRIVATE (monitor);
95
96        monitor->priv->connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
97        /* FIXME: check errors */
98        monitor->priv->proxy = dbus_g_proxy_new_for_name_owner (monitor->priv->connection,
99                                                                DBUS_SERVICE_DBUS,
100                                                                DBUS_PATH_DBUS,
101                                                                DBUS_INTERFACE_DBUS,
102                                                                &error);
103        dbus_g_proxy_add_signal (monitor->priv->proxy, "NameOwnerChanged",
104                                 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID);
105        dbus_g_proxy_connect_signal (monitor->priv->proxy, "NameOwnerChanged",
106                                     G_CALLBACK (dbus_name_owner_changed),
107                                     monitor, NULL);
108}
109
110static void
111gpm_dbus_system_monitor_finalize (GObject *object)
112{
113        GpmDbusSystemMonitor *monitor;
114
115        g_return_if_fail (object != NULL);
116        g_return_if_fail (GPM_IS_DBUS_SYSTEM_MONITOR (object));
117
118        monitor = GPM_DBUS_SYSTEM_MONITOR (object);
119
120        if (monitor->priv->proxy) {
121                g_object_unref (monitor->priv->proxy);
122                monitor->priv->proxy = NULL;
123        }
124        G_OBJECT_CLASS (gpm_dbus_system_monitor_parent_class)->finalize (object);
125}
126
127GpmDbusSystemMonitor *
128gpm_dbus_system_monitor_new (void)
129{
130        if (gpm_system_monitor_object) {
131                g_object_ref (gpm_system_monitor_object);
132        } else {
133                gpm_system_monitor_object = g_object_new (GPM_TYPE_DBUS_SYSTEM_MONITOR, NULL);
134                g_object_add_weak_pointer (gpm_system_monitor_object,
135                                           (gpointer *) &gpm_system_monitor_object);
136        }
137        return GPM_DBUS_SYSTEM_MONITOR (gpm_system_monitor_object);
138}
Note: See TracBrowser for help on using the repository browser.