aboutsummaryrefslogtreecommitdiff
path: root/src/dbus-listener.c
blob: 34df92525bd534f6ce4d678c32eb5a63a1ef216c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*

Listens on DBus for Power changes and passes them to an IndicatorPower

Copyright 2012 Canonical Ltd.

Authors:
    Charles Kerr <charles.kerr@canonical.com>

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 3.0 as published by the Free Software Foundation.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License version 3.0 for more details.

You should have received a copy of the GNU General Public
License along with this library. If not, see
<http://www.gnu.org/licenses/>.
*/

#include "dbus-listener.h"
#include "device.h"

struct _IndicatorPowerDbusListenerPrivate
{
  GCancellable * cancellable;
  GDBusProxy * proxy;
  guint watcher_id;
};

#define INDICATOR_POWER_DBUS_LISTENER_GET_PRIVATE(o) (INDICATOR_POWER_DBUS_LISTENER(o)->priv)

/* Signals */
enum {
  SIGNAL_DEVICES,
  SIGNAL_LAST
};
static guint signals[SIGNAL_LAST] = { 0 };


/* GObject stuff */
static void indicator_power_dbus_listener_class_init (IndicatorPowerDbusListenerClass *klass);
static void indicator_power_dbus_listener_init       (IndicatorPowerDbusListener *self);
static void indicator_power_dbus_listener_dispose    (GObject *object);
static void indicator_power_dbus_listener_finalize   (GObject *object);

static void gsd_appeared_callback (GDBusConnection *connection, const gchar *name, const gchar *name_owner, gpointer user_data);

/* LCOV_EXCL_START */
G_DEFINE_TYPE (IndicatorPowerDbusListener, indicator_power_dbus_listener, G_TYPE_OBJECT);
/* LCOV_EXCL_STOP */

static void
indicator_power_dbus_listener_class_init (IndicatorPowerDbusListenerClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  g_type_class_add_private (klass, sizeof (IndicatorPowerDbusListenerPrivate));

  /* methods */
  object_class->dispose = indicator_power_dbus_listener_dispose;
  object_class->finalize = indicator_power_dbus_listener_finalize;

  /**
   * IndicatorPowerDbusListener::indicator-power-dbus-listener-devices-enumerated:
   *
   * @listener: the IndicatorPowerDbusListener
   * @devices: a GSList of #IndicatorPowerDevice objects. (transfer none)
   *
   * This is emitted each time a new set of devices is enumerated over the bus.
   */
  signals[SIGNAL_DEVICES] = g_signal_new (INDICATOR_POWER_DBUS_LISTENER_DEVICES_ENUMERATED,
                                          G_TYPE_FROM_CLASS(klass), 0,
                                          G_STRUCT_OFFSET (IndicatorPowerDbusListenerClass, devices_enumerated),
                                          NULL, NULL,
                                          g_cclosure_marshal_VOID__POINTER,
                                          G_TYPE_NONE, 1, G_TYPE_POINTER);
}

/* Initialize an instance */
static void
indicator_power_dbus_listener_init (IndicatorPowerDbusListener *self)
{
  IndicatorPowerDbusListenerPrivate * priv;

  priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
                                      INDICATOR_POWER_DBUS_LISTENER_TYPE,
                                      IndicatorPowerDbusListenerPrivate);

  priv->cancellable = g_cancellable_new ();

  priv->watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION,
                                       GSD_SERVICE,
                                       G_BUS_NAME_WATCHER_FLAGS_NONE,
                                       gsd_appeared_callback,
                                       NULL,
                                       self,
                                       NULL);

  self->priv = priv;
}

static void
indicator_power_dbus_listener_dispose (GObject *object)
{
  IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(object);
  IndicatorPowerDbusListenerPrivate * priv = self->priv;

  g_clear_object (&priv->proxy);

  if (priv->cancellable != NULL)
    {
      g_cancellable_cancel (priv->cancellable);
      g_clear_object (&priv->cancellable);
    }

  if (priv->watcher_id)
    {
      g_bus_unwatch_name (priv->watcher_id);
      priv->watcher_id = 0;
    }

  G_OBJECT_CLASS (indicator_power_dbus_listener_parent_class)->dispose (object);
}

static void
indicator_power_dbus_listener_finalize (GObject *object)
{
  G_OBJECT_CLASS (indicator_power_dbus_listener_parent_class)->finalize (object);
}

/***
****
***/

static void
get_devices_cb (GObject      * source_object,
                GAsyncResult * res,
                gpointer       user_data)
{
  GError *error;
  GSList * devices = NULL;
  GVariant * devices_container;
  IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER (user_data);

  /* build an array of IndicatorPowerDevices from the DBus response */
  error = NULL;
  devices_container = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object), res, &error);
  if (devices_container == NULL)
    {
      g_message ("Couldn't get devices: %s\n", error->message);
      g_error_free (error);
    }
  else
    {
      gsize i;
      GVariant * devices_variant = g_variant_get_child_value (devices_container, 0);
      const int device_count = devices_variant ? g_variant_n_children (devices_variant) : 0;

      for (i=0; i<device_count; i++)
        {
          GVariant * v = g_variant_get_child_value (devices_variant, i);
          devices = g_slist_prepend (devices, indicator_power_device_new_from_variant (v));
          g_variant_unref (v);
        }

      devices = g_slist_reverse (devices);

      g_variant_unref (devices_variant);
      g_variant_unref (devices_container);
    }

  g_signal_emit (self, signals[SIGNAL_DEVICES], (GQuark)0, devices);
  g_slist_free_full (devices, g_object_unref);
}

static void
request_device_list (IndicatorPowerDbusListener * self)
{
  g_dbus_proxy_call (self->priv->proxy,
                     "GetDevices",
                     NULL,
                     G_DBUS_CALL_FLAGS_NONE,
                     -1,
                     self->priv->cancellable,
                     get_devices_cb,
                     self);
}

static void
receive_properties_changed (GDBusProxy *proxy                  G_GNUC_UNUSED,
                            GVariant   *changed_properties     G_GNUC_UNUSED,
                            GStrv       invalidated_properties G_GNUC_UNUSED,
                            gpointer    user_data)
{
  request_device_list (INDICATOR_POWER_DBUS_LISTENER(user_data));
}


static void
service_proxy_cb (GObject      *object,
                  GAsyncResult *res,
                  gpointer      user_data)
{
  GError * error = NULL;
  IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(user_data);
  IndicatorPowerDbusListenerPrivate * priv = self->priv;

  priv->proxy = g_dbus_proxy_new_for_bus_finish (res, &error);

  if (error != NULL)
    {
      g_error ("Error creating proxy: %s", error->message);
      g_error_free (error);
      return;
    }

  /* we want to change the primary device changes */
  g_signal_connect (priv->proxy, "g-properties-changed",
                    G_CALLBACK (receive_properties_changed), user_data);

  /* get the initial state */
  request_device_list (self);
}

static void
gsd_appeared_callback (GDBusConnection *connection,
                       const gchar     *name,
                       const gchar     *name_owner,
                       gpointer         user_data)
{
  IndicatorPowerDbusListener * self = INDICATOR_POWER_DBUS_LISTENER(user_data);
  IndicatorPowerDbusListenerPrivate * priv = self->priv;

  g_dbus_proxy_new (connection,
                    G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
                    NULL,
                    name,
                    GSD_POWER_DBUS_PATH,
                    GSD_POWER_DBUS_INTERFACE,
                    priv->cancellable,
                    service_proxy_cb,
                    self);
}