~ci-train-bot/indicator-session/indicator-session-ubuntu-zesty-2188

« back to all changes in this revision

Viewing changes to src/backend-dbus/utils.c

  • Committer: Charles Kerr
  • Date: 2013-03-22 21:34:34 UTC
  • mto: (384.2.29 ng)
  • mto: This revision was merged to the branch mainline in revision 399.
  • Revision ID: charles.kerr@canonical.com-20130322213434-a85qbob8bi4fvfx2
port indicator-session to GMenu/cmake. Code coverage increased from 0% to 95.4%.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2013 Canonical Ltd.
 
3
 *
 
4
 * Authors:
 
5
 *   Charles Kerr <charles.kerr@canonical.com>
 
6
 *
 
7
 * This program is free software: you can redistribute it and/or modify it
 
8
 * under the terms of the GNU General Public License version 3, as published
 
9
 * by the Free Software Foundation.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful, but
 
12
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 
13
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
14
 * PURPOSE.  See the GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License along
 
17
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
#include "utils.h"
 
21
 
 
22
/***
 
23
**** indicator_session_util_get_session_proxies()
 
24
***/
 
25
 
 
26
struct session_proxy_data
 
27
{
 
28
  ConsoleKitManager * ck_manager;
 
29
  Accounts * account_manager;
 
30
  DisplayManagerSeat * dm_seat;
 
31
 
 
32
  ConsoleKitSeat * current_seat;
 
33
  ConsoleKitSession * current_session;
 
34
  AccountsUser * active_user;
 
35
 
 
36
  GCancellable * cancellable;
 
37
  GError * error;
 
38
  int pending;
 
39
 
 
40
  indicator_session_util_session_proxies_func callback;
 
41
  gpointer user_data;
 
42
};
 
43
 
 
44
static void
 
45
session_proxy_data_free (struct session_proxy_data * data)
 
46
{
 
47
  g_clear_object (&data->ck_manager);
 
48
  g_clear_object (&data->account_manager);
 
49
  g_clear_object (&data->dm_seat);
 
50
 
 
51
  g_clear_object (&data->current_seat);
 
52
  g_clear_object (&data->current_session);
 
53
  g_clear_object (&data->active_user);
 
54
 
 
55
  g_clear_object (&data->cancellable);
 
56
  g_clear_error (&data->error);
 
57
 
 
58
  g_free (data);
 
59
}
 
60
 
 
61
static void
 
62
finish_callback (struct session_proxy_data * data)
 
63
{
 
64
  g_assert (data != NULL);
 
65
  g_debug ("%s %s: pending is %d", G_STRLOC, G_STRFUNC, (data->pending-1));
 
66
 
 
67
  if (!--data->pending)
 
68
    {
 
69
      data->callback (data->ck_manager,
 
70
                      data->account_manager,
 
71
                      data->dm_seat,
 
72
                      data->current_seat,
 
73
                      data->current_session,
 
74
                      data->active_user,
 
75
                      data->error,
 
76
                      data->user_data);
 
77
 
 
78
      session_proxy_data_free (data);
 
79
    }
 
80
}
 
81
 
 
82
static void
 
83
on_user_proxy_ready (GObject       * o       G_GNUC_UNUSED,
 
84
                     GAsyncResult  * res,
 
85
                     gpointer        gdata)
 
86
{
 
87
  struct session_proxy_data * data = gdata;
 
88
  g_debug ("%s %s", G_STRLOC, G_STRFUNC);
 
89
 
 
90
  data->active_user = accounts_user_proxy_new_for_bus_finish (res,  &data->error);
 
91
 
 
92
  if (data->error != NULL)
 
93
    {
 
94
      g_warning ("%s %s: %s", G_STRLOC, G_STRFUNC, data->error->message);
 
95
    }
 
96
  else
 
97
    {
 
98
      g_debug ("%s %s user proxy is %p", G_STRLOC, G_STRFUNC, (void*)data->active_user);
 
99
    }
 
100
 
 
101
  finish_callback (data);
 
102
}
 
103
 
 
104
static void
 
105
on_user_path_ready (GObject * o G_GNUC_UNUSED, GAsyncResult * res, gpointer gdata)
 
106
{
 
107
  char * path = NULL;
 
108
  struct session_proxy_data * data = gdata;
 
109
  g_debug ("%s %s", G_STRLOC, G_STRFUNC);
 
110
 
 
111
  accounts_call_find_user_by_id_finish (data->account_manager, &path, res, &data->error);
 
112
 
 
113
  if (data->error != NULL)
 
114
    {
 
115
      g_warning ("%s %s: %s", G_STRLOC, G_STRFUNC, data->error->message);
 
116
    }
 
117
  else if (path != NULL)
 
118
    {
 
119
      g_debug ("%s %s user path is %s", G_STRLOC, G_STRFUNC, path);
 
120
      ++data->pending;
 
121
      accounts_user_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
 
122
                                       G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES,
 
123
                                       "org.freedesktop.Accounts",
 
124
                                       path,
 
125
                                       data->cancellable,
 
126
                                       on_user_proxy_ready,
 
127
                                       data);
 
128
    }
 
129
 
 
130
  finish_callback (data);
 
131
  g_free (path);
 
132
}
 
133
 
 
134
static void
 
135
on_uid_ready (GObject * o G_GNUC_UNUSED, GAsyncResult * res, gpointer gdata)
 
136
{
 
137
  guint uid = 0;
 
138
  struct session_proxy_data * data = gdata;
 
139
  g_debug ("%s %s", G_STRLOC, G_STRFUNC);
 
140
 
 
141
  console_kit_session_call_get_unix_user_finish (data->current_session, &uid, res, &data->error);
 
142
  if (data->error != NULL)
 
143
    {
 
144
      g_warning ("%s %s: %s", G_STRLOC, G_STRFUNC, data->error->message);
 
145
    }
 
146
  else if (uid)
 
147
    {
 
148
      g_debug ("%s %s uid is %u", G_STRLOC, G_STRFUNC, uid);
 
149
      ++data->pending;
 
150
      accounts_call_find_user_by_id (data->account_manager,
 
151
                                     uid,
 
152
                                     data->cancellable,
 
153
                                     on_user_path_ready,
 
154
                                     data);
 
155
    }
 
156
 
 
157
  finish_callback (data);
 
158
}
 
159
 
 
160
static void
 
161
on_seat_proxy_ready (GObject * o G_GNUC_UNUSED, GAsyncResult * res, gpointer gdata)
 
162
{
 
163
  struct session_proxy_data * data = gdata;
 
164
  g_debug ("%s %s", G_STRLOC, G_STRFUNC);
 
165
 
 
166
  data->current_seat = console_kit_seat_proxy_new_for_bus_finish (res, &data->error);
 
167
 
 
168
  if (data->error != NULL)
 
169
    g_warning ("%s %s: %s", G_STRLOC, G_STRFUNC, data->error->message);
 
170
 
 
171
  finish_callback (data);
 
172
}
 
173
 
 
174
static void
 
175
on_sid_ready (GObject * o G_GNUC_UNUSED, GAsyncResult * res, gpointer gdata)
 
176
{
 
177
  char * sid = NULL;
 
178
  struct session_proxy_data * data = gdata;
 
179
  g_debug ("%s %s", G_STRLOC, G_STRFUNC);
 
180
 
 
181
  console_kit_session_call_get_seat_id_finish (data->current_session, &sid, res, &data->error);
 
182
 
 
183
  if (data->error != NULL)
 
184
    {
 
185
      g_warning ("%s %s: %s", G_STRLOC, G_STRFUNC, data->error->message);
 
186
    }
 
187
  else if (sid != NULL)
 
188
    {
 
189
      g_debug ("%s %s sid is %s", G_STRLOC, G_STRFUNC, sid);
 
190
      ++data->pending;
 
191
      console_kit_seat_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
 
192
                                          G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES,
 
193
                                          "org.freedesktop.ConsoleKit",
 
194
                                          sid,
 
195
                                          data->cancellable,
 
196
                                          on_seat_proxy_ready,
 
197
                                          data);
 
198
    }
 
199
 
 
200
  finish_callback (data);
 
201
  g_free (sid);
 
202
}
 
203
 
 
204
static void
 
205
on_session_proxy_ready (GObject * o G_GNUC_UNUSED, GAsyncResult * res, gpointer gdata)
 
206
{
 
207
  struct session_proxy_data * data = gdata;
 
208
  g_debug ("%s %s", G_STRLOC, G_STRFUNC);
 
209
 
 
210
  data->current_session = console_kit_session_proxy_new_finish (res, &data->error);
 
211
  if (data->error != NULL)
 
212
    {
 
213
      g_warning ("%s %s: %s", G_STRLOC, G_STRFUNC, data->error->message);
 
214
    }
 
215
  else
 
216
    {
 
217
      ++data->pending;
 
218
      console_kit_session_call_get_seat_id (data->current_session,
 
219
                                            data->cancellable,
 
220
                                            on_sid_ready,
 
221
                                            data);
 
222
 
 
223
      ++data->pending;
 
224
      console_kit_session_call_get_unix_user (data->current_session,
 
225
                                              data->cancellable,
 
226
                                              on_uid_ready,
 
227
                                              data);
 
228
    }
 
229
 
 
230
  finish_callback (data);
 
231
}
 
232
 
 
233
static void
 
234
on_current_session_ready (GObject * o G_GNUC_UNUSED, GAsyncResult * res, gpointer gdata)
 
235
{
 
236
  char * ssid = NULL;
 
237
  struct session_proxy_data * data = gdata;
 
238
  g_debug ("%s %s", G_STRLOC, G_STRFUNC);
 
239
 
 
240
  ssid = NULL;
 
241
  console_kit_manager_call_get_current_session_finish (data->ck_manager,
 
242
                                                       &ssid, res,
 
243
                                                       &data->error);
 
244
  if (data->error != NULL)
 
245
    {
 
246
      g_warning ("%s %s: %s", G_STRLOC, G_STRFUNC, data->error->message);
 
247
    }
 
248
  else if (ssid)
 
249
    {
 
250
      g_debug ("%s %s ssid is %s", G_STRLOC, G_STRFUNC, ssid);
 
251
      data->pending++;
 
252
      console_kit_session_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
 
253
                                             G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES,
 
254
                                             "org.freedesktop.ConsoleKit",
 
255
                                             ssid,
 
256
                                             data->cancellable,
 
257
                                             on_session_proxy_ready,
 
258
                                             data);
 
259
 
 
260
    }
 
261
 
 
262
  finish_callback (data);
 
263
  g_free (ssid);
 
264
}
 
265
 
 
266
static void
 
267
on_display_manager_seat_proxy_ready (GObject      * o         G_GNUC_UNUSED,
 
268
                                     GAsyncResult * res,
 
269
                                     gpointer       gdata)
 
270
{
 
271
  DisplayManagerSeat * seat;
 
272
  struct session_proxy_data * data = gdata;
 
273
 
 
274
  seat = display_manager_seat_proxy_new_for_bus_finish (res, &data->error);
 
275
 
 
276
  if (data->error != NULL)
 
277
    {
 
278
      g_warning ("%s %s: %s", G_STRLOC, G_STRFUNC, data->error->message);
 
279
    }
 
280
  else if (seat != NULL)
 
281
    {
 
282
      data->dm_seat = g_object_ref (seat);
 
283
    }
 
284
 
 
285
  finish_callback (data);
 
286
  g_clear_object (&seat);
 
287
}
 
288
 
 
289
static void
 
290
on_console_kit_manager_proxy_ready (GObject      * o       G_GNUC_UNUSED,
 
291
                                    GAsyncResult * res,
 
292
                                    gpointer       gdata)
 
293
{
 
294
  ConsoleKitManager * mgr;
 
295
  struct session_proxy_data * data = gdata;
 
296
  g_debug ("%s %s", G_STRLOC, G_STRFUNC);
 
297
 
 
298
  if (data->error == NULL)
 
299
    { 
 
300
      mgr = console_kit_manager_proxy_new_for_bus_finish (res, &data->error);
 
301
      g_debug ("%s %s mgr is %p, err is %p", G_STRLOC, G_STRFUNC, (void*)mgr, (void*)data->error);
 
302
 
 
303
      if (data->error != NULL)
 
304
        {
 
305
          g_warning ("%s %s: %s", G_STRLOC, G_STRFUNC, data->error->message);
 
306
        }
 
307
      else
 
308
        {
 
309
          data->ck_manager = mgr;
 
310
 
 
311
          data->pending++;
 
312
          console_kit_manager_call_get_current_session (mgr,
 
313
                                                        data->cancellable,
 
314
                                                        on_current_session_ready,
 
315
                                                        data);
 
316
 
 
317
        }
 
318
    }
 
319
 
 
320
  finish_callback (data);
 
321
}
 
322
 
 
323
static void
 
324
on_accounts_proxy_ready (GObject * o G_GNUC_UNUSED, GAsyncResult * res, gpointer gdata)
 
325
{
 
326
  struct session_proxy_data * data = gdata;
 
327
  g_debug ("%s %s", G_STRLOC, G_STRFUNC);
 
328
 
 
329
  if (data->error == NULL)
 
330
    {
 
331
      data->account_manager = accounts_proxy_new_for_bus_finish (res, &data->error);
 
332
 
 
333
      if (data->error != NULL)
 
334
        g_warning ("%s %s: %s", G_STRLOC, G_STRFUNC, data->error->message);
 
335
    }
 
336
 
 
337
  finish_callback (data);
 
338
}
 
339
 
 
340
/**
 
341
 * Getting all the proxies we want is kind of a pain -- 
 
342
 * especially without blocking (ie, using _sync() funcs) -- 
 
343
 * so it's farmed out to this wrapper utility.
 
344
 *
 
345
 * 1. in this func, start getting the ConsoleKit and Accounts proxies
 
346
 * 2. when the accounts proxy is ready, stash it in data.account_manager
 
347
 * 3. when the ck manager proxy is ready, stash it in data.ck_manager and
 
348
 *    ask it for the current session's ssid
 
349
 * 4. when the ssid is ready, start getting a proxy for it
 
350
 * 5. when the session's proxy is ready, stash it in data.current_session
 
351
 *    and ask it for both the current seat's sid and the active user's uid
 
352
 * 6. When the current seat's sid is ready, start getting a proxy for it
 
353
 * 7. When the current seat's proxy is ready, stash it in data.current_seat
 
354
 * 8. when the active user's uid is ready, ask data.account_manager for the path
 
355
 * 9. when the user path is ready, start getting an Accounts.User proxy for it
 
356
 * 10. when the Accounts.User proxy is read, stash it in data.active_user
 
357
 *
 
358
 * When everything is done, or if there's an error, invoke the data.callback
 
359
 */
 
360
void
 
361
indicator_session_util_get_session_proxies (
 
362
                     indicator_session_util_session_proxies_func   func,
 
363
                     GCancellable                                * cancellable,
 
364
                     gpointer                                      user_data)
 
365
{
 
366
  struct session_proxy_data * data;
 
367
 
 
368
  data = g_new0 (struct session_proxy_data, 1);
 
369
  data->callback = func;
 
370
  data->user_data = user_data;
 
371
  data->cancellable = g_object_ref (cancellable);
 
372
 
 
373
  data->pending++;
 
374
  accounts_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
 
375
                              G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES,
 
376
                              "org.freedesktop.Accounts",
 
377
                              "/org/freedesktop/Accounts",
 
378
                              data->cancellable,
 
379
                              on_accounts_proxy_ready, data);
 
380
 
 
381
  data->pending++;
 
382
  console_kit_manager_proxy_new_for_bus (
 
383
                               G_BUS_TYPE_SYSTEM,
 
384
                               G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES,
 
385
                               "org.freedesktop.ConsoleKit",
 
386
                               "/org/freedesktop/ConsoleKit/Manager",
 
387
                               data->cancellable,
 
388
                               on_console_kit_manager_proxy_ready, data);
 
389
 
 
390
  data->pending++;
 
391
  display_manager_seat_proxy_new_for_bus (
 
392
                               G_BUS_TYPE_SYSTEM,
 
393
                               G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES,
 
394
                               "org.freedesktop.DisplayManager",
 
395
                               g_getenv ("XDG_SEAT_PATH"),
 
396
                               data->cancellable,
 
397
                               on_display_manager_seat_proxy_ready, data);
 
398
 
 
399
}