~vikoadi/elementaryos/os-patch-indicator-session-trusty-fix-lock

« back to all changes in this revision

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

  • Committer: Cody Garver
  • Date: 2014-04-03 17:08:08 UTC
  • Revision ID: cody@elementaryos.org-20140403170808-z56s93rorb1dzvmk
Initial import, version 12.10.5+14.04.20140324-0ubuntu1

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 <glib.h>
 
21
#include <glib/gi18n.h>
 
22
 
 
23
#include "dbus-end-session-dialog.h"
 
24
#include "dbus-login1-manager.h"
 
25
#include "dbus-webcredentials.h"
 
26
#include "gnome-screen-saver.h"
 
27
#include "gnome-session-manager.h"
 
28
 
 
29
#include "actions.h"
 
30
 
 
31
enum
 
32
{
 
33
  END_SESSION_TYPE_LOGOUT = 0,
 
34
  END_SESSION_TYPE_SHUTDOWN,
 
35
  END_SESSION_TYPE_REBOOT
 
36
};
 
37
 
 
38
struct _IndicatorSessionActionsDbusPriv
 
39
{
 
40
  GCancellable * cancellable;
 
41
 
 
42
  GSettings * lockdown_settings;
 
43
  GSettings * indicator_settings;
 
44
  GnomeScreenSaver * screen_saver;
 
45
  GnomeSessionManager * session_manager;
 
46
  Login1Manager * login1_manager;
 
47
  GCancellable * login1_manager_cancellable;
 
48
  Login1Seat * login1_seat;
 
49
  DisplayManagerSeat * dm_seat;
 
50
  GCancellable * dm_seat_cancellable;
 
51
  Webcredentials * webcredentials;
 
52
  EndSessionDialog * end_session_dialog;
 
53
  char * zenity;
 
54
 
 
55
  gboolean can_suspend;
 
56
  gboolean can_hibernate;
 
57
  gboolean seat_allows_activation;
 
58
};
 
59
 
 
60
typedef IndicatorSessionActionsDbusPriv priv_t;
 
61
 
 
62
G_DEFINE_TYPE (IndicatorSessionActionsDbus,
 
63
               indicator_session_actions_dbus,
 
64
               INDICATOR_TYPE_SESSION_ACTIONS)
 
65
 
 
66
/***
 
67
****
 
68
***/
 
69
 
 
70
static void
 
71
log_and_clear_error (GError ** err, const char * loc, const char * func)
 
72
{
 
73
  if (*err)
 
74
    {
 
75
      if (!g_error_matches (*err, G_IO_ERROR, G_IO_ERROR_CANCELLED))
 
76
        g_warning ("%s %s: %s", loc, func, (*err)->message);
 
77
 
 
78
      g_clear_error (err);
 
79
    }
 
80
}
 
81
 
 
82
/***
 
83
****
 
84
***/
 
85
 
 
86
typedef enum
 
87
{
 
88
  PROMPT_NONE,
 
89
  PROMPT_WITH_ZENITY,
 
90
  PROMPT_WITH_UNITY
 
91
}
 
92
prompt_status_t;
 
93
 
 
94
static prompt_status_t
 
95
get_prompt_status (IndicatorSessionActionsDbus * self)
 
96
{
 
97
  prompt_status_t prompt = PROMPT_NONE;
 
98
  const priv_t * p = self->priv;
 
99
 
 
100
  if (!g_settings_get_boolean (p->indicator_settings, "suppress-logout-restart-shutdown"))
 
101
    {
 
102
      /* can we use the Unity prompt? */
 
103
      if ((prompt == PROMPT_NONE) && p && p->end_session_dialog)
 
104
        {
 
105
          GDBusProxy * proxy = G_DBUS_PROXY (p->end_session_dialog);
 
106
          char * name = g_dbus_proxy_get_name_owner (proxy);
 
107
          if (name != NULL)
 
108
            prompt = PROMPT_WITH_UNITY;
 
109
          g_free (name);
 
110
        }
 
111
 
 
112
      /* can we use zenity? */
 
113
      if ((prompt == PROMPT_NONE) && p && p->zenity)
 
114
        prompt = PROMPT_WITH_ZENITY;
 
115
    }
 
116
 
 
117
  return prompt;
 
118
}
 
119
 
 
120
/***
 
121
****
 
122
***/
 
123
 
 
124
static void
 
125
on_seat_notify_multi_session (IndicatorSessionActionsDbus * self)
 
126
{
 
127
  priv_t * p = self->priv;
 
128
  gboolean b;
 
129
 
 
130
  b = login1_seat_get_can_multi_session (p->login1_seat);
 
131
 
 
132
  if (p->seat_allows_activation != b)
 
133
    {
 
134
      p->seat_allows_activation = b;
 
135
 
 
136
      indicator_session_actions_notify_can_switch (INDICATOR_SESSION_ACTIONS(self));
 
137
    }
 
138
}
 
139
 
 
140
static void
 
141
set_login1_seat (IndicatorSessionActionsDbus * self, Login1Seat * seat)
 
142
{
 
143
  priv_t * p = self->priv;
 
144
 
 
145
  if (p->login1_seat != NULL)
 
146
    {
 
147
      g_signal_handlers_disconnect_by_data (p->login1_seat, self);
 
148
      g_clear_object (&p->login1_seat);
 
149
    }
 
150
 
 
151
  if (seat != NULL)
 
152
    {
 
153
      p->login1_seat = g_object_ref (seat);
 
154
 
 
155
      g_signal_connect_swapped (seat, "notify::can-multi-session",
 
156
                                G_CALLBACK(on_seat_notify_multi_session), self);
 
157
    }
 
158
}
 
159
 
 
160
/***
 
161
****
 
162
***/
 
163
 
 
164
static void
 
165
set_dm_seat (IndicatorSessionActionsDbus * self, DisplayManagerSeat * seat)
 
166
{
 
167
  priv_t * p = self->priv;
 
168
 
 
169
  if (p->dm_seat != NULL)
 
170
    {
 
171
      g_cancellable_cancel (p->dm_seat_cancellable);
 
172
      g_clear_object (&p->dm_seat_cancellable);
 
173
      g_clear_object (&p->dm_seat);
 
174
    }
 
175
 
 
176
  if (seat != NULL)
 
177
    {
 
178
      p->dm_seat = g_object_ref (seat);
 
179
      p->dm_seat_cancellable = g_cancellable_new ();
 
180
    }
 
181
}
 
182
 
 
183
static void
 
184
on_screensaver_proxy_ready (GObject * o G_GNUC_UNUSED, GAsyncResult * res, gpointer gself)
 
185
{
 
186
  GError * err;
 
187
  GnomeScreenSaver * ss;
 
188
 
 
189
  err = NULL;
 
190
  ss = gnome_screen_saver_proxy_new_for_bus_finish (res, &err);
 
191
  if (err == NULL)
 
192
    {
 
193
      INDICATOR_SESSION_ACTIONS_DBUS(gself)->priv->screen_saver = ss;
 
194
    }
 
195
 
 
196
  log_and_clear_error (&err, G_STRLOC, G_STRFUNC);
 
197
}
 
198
 
 
199
static void
 
200
on_can_suspend_ready (GObject * o, GAsyncResult * res, gpointer gself)
 
201
{
 
202
  char * str;
 
203
  GError * err;
 
204
 
 
205
  str = NULL;
 
206
  err = NULL;
 
207
  login1_manager_call_can_suspend_finish (LOGIN1_MANAGER(o), &str, res, &err);
 
208
  if (err == NULL)
 
209
    {
 
210
      priv_t * p = INDICATOR_SESSION_ACTIONS_DBUS(gself)->priv;
 
211
 
 
212
      const gboolean b = !g_strcmp0 (str, "yes");
 
213
 
 
214
      if (p->can_suspend != b)
 
215
        {
 
216
          p->can_suspend = b;
 
217
          indicator_session_actions_notify_can_suspend (gself);
 
218
        }
 
219
 
 
220
      g_free (str);
 
221
    }
 
222
 
 
223
  log_and_clear_error (&err, G_STRLOC, G_STRFUNC);
 
224
}
 
225
 
 
226
static void
 
227
on_can_hibernate_ready (GObject * o, GAsyncResult * res, gpointer gself)
 
228
{
 
229
  gchar * str;
 
230
  GError * err;
 
231
 
 
232
  str = NULL;
 
233
  err = NULL;
 
234
  login1_manager_call_can_hibernate_finish (LOGIN1_MANAGER(o), &str, res, &err);
 
235
  if (err == NULL)
 
236
    {
 
237
      priv_t * p = INDICATOR_SESSION_ACTIONS_DBUS(gself)->priv;
 
238
 
 
239
      const gboolean b = !g_strcmp0 (str, "yes");
 
240
 
 
241
      if (p->can_hibernate != b)
 
242
        {
 
243
          p->can_hibernate = b;
 
244
          indicator_session_actions_notify_can_hibernate (gself);
 
245
        }
 
246
 
 
247
      g_free (str);
 
248
    }
 
249
 
 
250
  log_and_clear_error (&err, G_STRLOC, G_STRFUNC);
 
251
}
 
252
 
 
253
static void
 
254
set_login1_manager (IndicatorSessionActionsDbus * self,
 
255
                    Login1Manager               * login1_manager)
 
256
{
 
257
  priv_t * p = self->priv;
 
258
 
 
259
  if (p->login1_manager != NULL)
 
260
    {
 
261
      g_cancellable_cancel (p->login1_manager_cancellable);
 
262
      g_clear_object (&p->login1_manager_cancellable);
 
263
      g_clear_object (&p->login1_manager);
 
264
    }
 
265
 
 
266
  if (login1_manager != NULL)
 
267
    {
 
268
      p->login1_manager_cancellable = g_cancellable_new ();
 
269
 
 
270
      p->login1_manager = g_object_ref (login1_manager);
 
271
 
 
272
      login1_manager_call_can_suspend (p->login1_manager,
 
273
                                       p->login1_manager_cancellable,
 
274
                                       on_can_suspend_ready,
 
275
                                       self);
 
276
 
 
277
      login1_manager_call_can_hibernate (p->login1_manager,
 
278
                                         p->login1_manager_cancellable,
 
279
                                         on_can_hibernate_ready,
 
280
                                         self);
 
281
    }
 
282
}
 
283
 
 
284
static void
 
285
on_session_manager_proxy_ready (GObject * o G_GNUC_UNUSED, GAsyncResult * res, gpointer gself)
 
286
{
 
287
  GError * err;
 
288
  GnomeSessionManager * sm;
 
289
 
 
290
  err = NULL;
 
291
  sm = gnome_session_manager_proxy_new_for_bus_finish (res, &err);
 
292
  if (err == NULL)
 
293
    {
 
294
      INDICATOR_SESSION_ACTIONS_DBUS(gself)->priv->session_manager = sm;
 
295
    }
 
296
 
 
297
  log_and_clear_error (&err, G_STRLOC, G_STRFUNC);
 
298
}
 
299
 
 
300
static void
 
301
on_webcredentials_proxy_ready (GObject * o G_GNUC_UNUSED, GAsyncResult * res, gpointer gself)
 
302
{
 
303
  GError * err;
 
304
  Webcredentials * webcredentials;
 
305
 
 
306
  err = NULL;
 
307
  webcredentials = webcredentials_proxy_new_for_bus_finish (res, &err);
 
308
  if (err == NULL)
 
309
    {
 
310
      INDICATOR_SESSION_ACTIONS_DBUS(gself)->priv->webcredentials = webcredentials;
 
311
 
 
312
      g_signal_connect_swapped (webcredentials, "notify::error-status",
 
313
                                G_CALLBACK(indicator_session_actions_notify_has_online_account_error), gself);
 
314
 
 
315
      if (webcredentials_get_error_status (webcredentials))
 
316
        indicator_session_actions_notify_has_online_account_error (gself);
 
317
    }
 
318
 
 
319
  log_and_clear_error (&err, G_STRLOC, G_STRFUNC);
 
320
}
 
321
 
 
322
static void
 
323
on_end_session_dialog_proxy_ready (GObject * o G_GNUC_UNUSED, GAsyncResult * res, gpointer gself)
 
324
{
 
325
  GError * err;
 
326
  EndSessionDialog * end_session_dialog;
 
327
 
 
328
  err = NULL;
 
329
  end_session_dialog = end_session_dialog_proxy_new_for_bus_finish (res, &err);
 
330
  if (err == NULL)
 
331
    {
 
332
      INDICATOR_SESSION_ACTIONS_DBUS(gself)->priv->end_session_dialog = end_session_dialog;
 
333
 
 
334
      indicator_session_actions_notify_can_prompt (INDICATOR_SESSION_ACTIONS(gself));
 
335
      indicator_session_actions_notify_can_reboot (INDICATOR_SESSION_ACTIONS(gself));
 
336
    }
 
337
 
 
338
  log_and_clear_error (&err, G_STRLOC, G_STRFUNC);
 
339
}
 
340
 
 
341
/***
 
342
****  Virtual Functions
 
343
***/
 
344
 
 
345
static gboolean
 
346
my_can_lock (IndicatorSessionActions * self)
 
347
{
 
348
  priv_t * p = INDICATOR_SESSION_ACTIONS_DBUS(self)->priv;
 
349
 
 
350
  return !g_settings_get_boolean (p->lockdown_settings, "disable-lock-screen");
 
351
}
 
352
 
 
353
static gboolean
 
354
my_can_logout (IndicatorSessionActions * self)
 
355
{
 
356
  priv_t * p = INDICATOR_SESSION_ACTIONS_DBUS(self)->priv;
 
357
 
 
358
  if (g_settings_get_boolean (p->indicator_settings, "suppress-logout-menuitem"))
 
359
    return FALSE;
 
360
 
 
361
  if (g_settings_get_boolean (p->lockdown_settings, "disable-log-out"))
 
362
    return FALSE;
 
363
 
 
364
  return TRUE;
 
365
}
 
366
 
 
367
static gboolean
 
368
my_can_reboot (IndicatorSessionActions * actions)
 
369
{
 
370
  IndicatorSessionActionsDbus * self = INDICATOR_SESSION_ACTIONS_DBUS(actions);
 
371
  priv_t * p = self->priv;
 
372
  
 
373
  if (g_settings_get_boolean (p->indicator_settings, "suppress-restart-menuitem"))
 
374
    return FALSE;
 
375
 
 
376
  /* Shutdown and Restart are the same dialog prompt in Unity,
 
377
     so disable the redundant 'Restart' menuitem in that mode */
 
378
  if (!g_settings_get_boolean (p->indicator_settings, "suppress-shutdown-menuitem"))
 
379
    if (get_prompt_status(self) == PROMPT_WITH_UNITY)
 
380
      return FALSE;
 
381
 
 
382
  return TRUE;
 
383
}
 
384
 
 
385
static gboolean
 
386
my_can_switch (IndicatorSessionActions * self)
 
387
{
 
388
  const priv_t * p = INDICATOR_SESSION_ACTIONS_DBUS(self)->priv;
 
389
 
 
390
  return p->seat_allows_activation
 
391
     && !g_settings_get_boolean (p->lockdown_settings, "disable-user-switching");
 
392
}
 
393
 
 
394
static gboolean
 
395
my_can_suspend (IndicatorSessionActions * self)
 
396
{
 
397
  const priv_t * p = INDICATOR_SESSION_ACTIONS_DBUS(self)->priv;
 
398
 
 
399
  return p && p->can_suspend;
 
400
}
 
401
 
 
402
static gboolean
 
403
my_can_hibernate (IndicatorSessionActions * self)
 
404
{
 
405
  const priv_t * p = INDICATOR_SESSION_ACTIONS_DBUS(self)->priv;
 
406
 
 
407
  return p && p->can_hibernate;
 
408
}
 
409
 
 
410
static gboolean
 
411
my_can_prompt (IndicatorSessionActions * self)
 
412
{
 
413
  return get_prompt_status(INDICATOR_SESSION_ACTIONS_DBUS(self)) != PROMPT_NONE;
 
414
}
 
415
 
 
416
static gboolean
 
417
my_has_online_account_error (IndicatorSessionActions * self)
 
418
{
 
419
  const priv_t * p = INDICATOR_SESSION_ACTIONS_DBUS(self)->priv;
 
420
 
 
421
  return p && (p->webcredentials) && (webcredentials_get_error_status (p->webcredentials));
 
422
}
 
423
 
 
424
static void
 
425
my_suspend (IndicatorSessionActions * self)
 
426
{
 
427
  priv_t * p = INDICATOR_SESSION_ACTIONS_DBUS(self)->priv;
 
428
 
 
429
  g_return_if_fail (p->login1_manager != NULL);
 
430
 
 
431
  login1_manager_call_suspend (p->login1_manager,
 
432
                               FALSE,
 
433
                               p->login1_manager_cancellable,
 
434
                               NULL,
 
435
                               NULL);
 
436
}
 
437
 
 
438
static void
 
439
my_hibernate (IndicatorSessionActions * self)
 
440
{
 
441
  priv_t * p = INDICATOR_SESSION_ACTIONS_DBUS(self)->priv;
 
442
 
 
443
  g_return_if_fail (p->login1_manager != NULL);
 
444
 
 
445
  login1_manager_call_hibernate (p->login1_manager,
 
446
                                 FALSE,
 
447
                                 p->login1_manager_cancellable,
 
448
                                 NULL,
 
449
                                 NULL);
 
450
}
 
451
 
 
452
/***
 
453
****  End Session Dialog
 
454
***/
 
455
 
 
456
static void
 
457
logout_now (IndicatorSessionActionsDbus * self)
 
458
{
 
459
  priv_t * p = self->priv;
 
460
 
 
461
  g_return_if_fail (p->session_manager != NULL);
 
462
 
 
463
  gnome_session_manager_call_logout (p->session_manager,
 
464
                                     1, /* don't prompt */
 
465
                                     p->cancellable,
 
466
                                     NULL,
 
467
                                     NULL);
 
468
}
 
469
 
 
470
static void
 
471
on_reboot_response (GObject      * o,
 
472
                    GAsyncResult * res,
 
473
                    gpointer       unused G_GNUC_UNUSED)
 
474
{
 
475
  GError * err = NULL;
 
476
  login1_manager_call_reboot_finish (LOGIN1_MANAGER(o), res, &err);
 
477
  if (err != NULL)
 
478
    {
 
479
      g_warning ("Unable to reboot: %s", err->message);
 
480
      g_error_free (err);
 
481
    }
 
482
}
 
483
 
 
484
static void
 
485
reboot_now (IndicatorSessionActionsDbus * self)
 
486
{
 
487
  priv_t * p = self->priv;
 
488
 
 
489
  g_return_if_fail (p->login1_manager != NULL);
 
490
 
 
491
  login1_manager_call_reboot (p->login1_manager,
 
492
                              FALSE,
 
493
                              p->login1_manager_cancellable,
 
494
                              on_reboot_response,
 
495
                              NULL);
 
496
}
 
497
 
 
498
static void
 
499
power_off_now (IndicatorSessionActionsDbus * self)
 
500
{
 
501
  priv_t * p = self->priv;
 
502
 
 
503
  g_return_if_fail (p->login1_manager != NULL);
 
504
 
 
505
  login1_manager_call_power_off (p->login1_manager,
 
506
                                 FALSE,
 
507
                                 p->login1_manager_cancellable,
 
508
                                 NULL,
 
509
                                 NULL);
 
510
}
 
511
 
 
512
static void
 
513
stop_listening_to_dialog (IndicatorSessionActionsDbus * self)
 
514
{
 
515
  g_signal_handlers_disconnect_by_data (self->priv->end_session_dialog, self);
 
516
}
 
517
static void
 
518
on_end_session_dialog_canceled (IndicatorSessionActionsDbus * self)
 
519
{
 
520
  stop_listening_to_dialog (self);
 
521
}
 
522
static void
 
523
on_end_session_dialog_closed (IndicatorSessionActionsDbus * self)
 
524
{
 
525
  stop_listening_to_dialog (self);
 
526
}
 
527
 
 
528
static void
 
529
on_open_end_session_dialog_ready (GObject      * o,
 
530
                                  GAsyncResult * res,
 
531
                                  gpointer       gself G_GNUC_UNUSED)
 
532
{
 
533
  GError * err = NULL;
 
534
  end_session_dialog_call_open_finish (END_SESSION_DIALOG(o), res, &err);
 
535
  log_and_clear_error (&err, G_STRLOC, G_STRFUNC);
 
536
}
 
537
 
 
538
static void
 
539
show_unity_end_session_dialog (IndicatorSessionActionsDbus * self, int type)
 
540
{
 
541
  priv_t * p = INDICATOR_SESSION_ACTIONS_DBUS(self)->priv;
 
542
  gpointer o = p->end_session_dialog;
 
543
  const char * inhibitor_paths[]  = { NULL };
 
544
 
 
545
  g_assert (o != NULL);
 
546
 
 
547
  g_signal_connect_swapped (o, "confirmed-logout", G_CALLBACK(logout_now), self);
 
548
  g_signal_connect_swapped (o, "confirmed-reboot", G_CALLBACK(reboot_now), self);
 
549
  g_signal_connect_swapped (o, "confirmed-shutdown", G_CALLBACK(power_off_now), self);
 
550
  g_signal_connect_swapped (o, "canceled", G_CALLBACK(on_end_session_dialog_canceled), self);
 
551
  g_signal_connect_swapped (o, "closed", G_CALLBACK(on_end_session_dialog_closed), self);
 
552
 
 
553
  end_session_dialog_call_open (p->end_session_dialog, type, 0, 0, inhibitor_paths,
 
554
                                p->cancellable,
 
555
                                on_open_end_session_dialog_ready,
 
556
                                self);
 
557
}
 
558
 
 
559
static gboolean
 
560
zenity_question (IndicatorSessionActionsDbus * self,
 
561
                 const char * icon_name,
 
562
                 const char * title,
 
563
                 const char * text,
 
564
                 const char * ok_label,
 
565
                 const char * cancel_label)
 
566
{
 
567
  char * command_line;
 
568
  int exit_status;
 
569
  gboolean confirmed;
 
570
 
 
571
  command_line = g_strdup_printf ("%s"
 
572
                                  " --question"
 
573
                                  " --icon-name=\"%s\""
 
574
                                  " --title=\"%s\""
 
575
                                  " --text=\"%s\""
 
576
                                  " --ok-label=\"%s\""
 
577
                                  " --cancel-label=\"%s\""
 
578
                                  " --no-wrap",
 
579
                                  self->priv->zenity,
 
580
                                  icon_name,
 
581
                                  title,
 
582
                                  text,
 
583
                                  ok_label,
 
584
                                  cancel_label);
 
585
 
 
586
  exit_status = -1;
 
587
  if (!g_spawn_command_line_sync (command_line, NULL, NULL, &exit_status, NULL))
 
588
    {
 
589
      /* Treat failure-to-prompt as user confirmation.
 
590
         Otherwise how will the user ever log out? */
 
591
      confirmed = TRUE;
 
592
    }
 
593
  else
 
594
    {
 
595
      confirmed = exit_status == 0;
 
596
    }
 
597
 
 
598
  g_free (command_line);
 
599
  return confirmed;
 
600
}
 
601
 
 
602
static void
 
603
my_logout (IndicatorSessionActions * actions)
 
604
{
 
605
  IndicatorSessionActionsDbus * self = INDICATOR_SESSION_ACTIONS_DBUS (actions);
 
606
 
 
607
  switch (get_prompt_status (self))
 
608
    {
 
609
      case PROMPT_WITH_UNITY:
 
610
        show_unity_end_session_dialog (self, END_SESSION_TYPE_LOGOUT);
 
611
        break;
 
612
 
 
613
      case PROMPT_NONE:
 
614
        logout_now (self);
 
615
        break;
 
616
 
 
617
      case PROMPT_WITH_ZENITY:
 
618
        {
 
619
          const char * primary = _("Are you sure you want to close all programs and log out?");
 
620
          const char * secondary = _("Some software updates won't be applied until the computer next restarts.");
 
621
          char * text = g_strdup_printf ("<big><b>%s</b></big>\n \n%s", primary, secondary);
 
622
 
 
623
          gboolean confirmed = zenity_question (self,
 
624
                                                "system-log-out",
 
625
                                                _("Log Out"),
 
626
                                                text,
 
627
                                                _("Log Out"),
 
628
                                                _("Cancel"));
 
629
 
 
630
          g_free (text);
 
631
 
 
632
          if (confirmed)
 
633
            logout_now (self);
 
634
          break;
 
635
        }
 
636
    }
 
637
}
 
638
 
 
639
static void
 
640
my_reboot (IndicatorSessionActions * actions)
 
641
{
 
642
  IndicatorSessionActionsDbus * self = INDICATOR_SESSION_ACTIONS_DBUS (actions);
 
643
 
 
644
  switch (get_prompt_status (self))
 
645
    {
 
646
      case PROMPT_WITH_UNITY:
 
647
        show_unity_end_session_dialog (self, END_SESSION_TYPE_REBOOT);
 
648
        break;
 
649
 
 
650
      case PROMPT_NONE:
 
651
        reboot_now (self);
 
652
        break;
 
653
 
 
654
      case PROMPT_WITH_ZENITY:
 
655
        if (zenity_question (self,
 
656
              "system-restart",
 
657
              _("Restart"),
 
658
              _("Are you sure you want to close all programs and restart the computer?"),
 
659
              _("Restart"),
 
660
              _("Cancel")))
 
661
          reboot_now (self);
 
662
        break;
 
663
    }
 
664
}
 
665
 
 
666
static void
 
667
my_power_off (IndicatorSessionActions * actions)
 
668
{
 
669
  IndicatorSessionActionsDbus * self = INDICATOR_SESSION_ACTIONS_DBUS (actions);
 
670
 
 
671
  switch (get_prompt_status (self))
 
672
    {
 
673
      case PROMPT_WITH_UNITY:
 
674
        /* NB: TYPE_REBOOT instead of TYPE_SHUTDOWN because
 
675
           the latter adds lock & logout options in Unity... */
 
676
        show_unity_end_session_dialog (self, END_SESSION_TYPE_REBOOT);
 
677
        break;
 
678
 
 
679
      case PROMPT_WITH_ZENITY:
 
680
        if (zenity_question (self,
 
681
              "system-shutdown",
 
682
              _("Shut Down"),
 
683
              _("Are you sure you want to close all programs and shut down the computer?"),
 
684
              _("Shut Down"),
 
685
              _("Cancel")))
 
686
          power_off_now (self);
 
687
        break;
 
688
 
 
689
      case PROMPT_NONE:
 
690
        power_off_now (self);
 
691
        break;
 
692
    }
 
693
}
 
694
 
 
695
/***
 
696
****
 
697
***/
 
698
 
 
699
static void
 
700
run_outside_app (const char * cmd)
 
701
{
 
702
  GError * err = NULL;
 
703
  g_debug ("%s calling \"%s\"", G_STRFUNC, cmd);
 
704
  g_spawn_command_line_async (cmd, &err);
 
705
  log_and_clear_error (&err, G_STRLOC, G_STRFUNC);
 
706
}
 
707
 
 
708
static void
 
709
my_help (IndicatorSessionActions * self G_GNUC_UNUSED)
 
710
{
 
711
  run_outside_app ("yelp");
 
712
}
 
713
 
 
714
static gboolean
 
715
have_unity_control_center (void)
 
716
{
 
717
  gchar *path;
 
718
  gboolean have_ucc;
 
719
 
 
720
  if (g_strcmp0 (g_getenv ("XDG_CURRENT_DESKTOP"), "Unity") != 0)
 
721
    return FALSE;
 
722
 
 
723
  path = g_find_program_in_path ("unity-control-center");
 
724
  have_ucc = path != NULL;
 
725
  g_free (path);
 
726
 
 
727
  return have_ucc;
 
728
}
 
729
 
 
730
static void
 
731
my_settings (IndicatorSessionActions * self G_GNUC_UNUSED)
 
732
{
 
733
  if (have_unity_control_center ())
 
734
    run_outside_app ("unity-control-center");
 
735
  else
 
736
    run_outside_app ("gnome-control-center");
 
737
}
 
738
 
 
739
static void
 
740
my_online_accounts (IndicatorSessionActions * self G_GNUC_UNUSED)
 
741
{
 
742
  if (have_unity_control_center ())
 
743
    run_outside_app ("unity-control-center credentials");
 
744
  else
 
745
    run_outside_app ("gnome-control-center credentials");
 
746
}
 
747
 
 
748
static void
 
749
my_about (IndicatorSessionActions * self G_GNUC_UNUSED)
 
750
{
 
751
  if (have_unity_control_center ())
 
752
    run_outside_app ("unity-control-center info");
 
753
  else
 
754
    run_outside_app ("gnome-control-center info");
 
755
}
 
756
 
 
757
/***
 
758
****
 
759
***/
 
760
 
 
761
static void
 
762
lock_current_session (IndicatorSessionActions * self)
 
763
{
 
764
  priv_t * p = INDICATOR_SESSION_ACTIONS_DBUS(self)->priv;
 
765
 
 
766
  g_return_if_fail (p->screen_saver != NULL);
 
767
 
 
768
  gnome_screen_saver_call_lock (p->screen_saver, p->cancellable, NULL, NULL);
 
769
}
 
770
 
 
771
static void
 
772
my_switch_to_screensaver (IndicatorSessionActions * self)
 
773
{
 
774
  lock_current_session (self);
 
775
}
 
776
 
 
777
static void
 
778
my_switch_to_greeter (IndicatorSessionActions * self)
 
779
{
 
780
  priv_t * p = INDICATOR_SESSION_ACTIONS_DBUS(self)->priv;
 
781
 
 
782
  g_return_if_fail (p->dm_seat != NULL);
 
783
 
 
784
  display_manager_seat_call_switch_to_greeter (p->dm_seat,
 
785
                                               p->dm_seat_cancellable,
 
786
                                               NULL, NULL);
 
787
}
 
788
 
 
789
static void
 
790
my_switch_to_guest (IndicatorSessionActions * self)
 
791
{
 
792
  priv_t * p = INDICATOR_SESSION_ACTIONS_DBUS(self)->priv;
 
793
 
 
794
  g_return_if_fail (p->dm_seat != NULL);
 
795
 
 
796
  lock_current_session (self);
 
797
 
 
798
  display_manager_seat_call_switch_to_guest (p->dm_seat, "",
 
799
                                             p->dm_seat_cancellable,
 
800
                                             NULL, NULL);
 
801
}
 
802
 
 
803
static void
 
804
my_switch_to_username (IndicatorSessionActions * self, const char * username)
 
805
{
 
806
  priv_t * p = INDICATOR_SESSION_ACTIONS_DBUS(self)->priv;
 
807
 
 
808
  g_return_if_fail (p->dm_seat != NULL);
 
809
 
 
810
  display_manager_seat_call_switch_to_user (p->dm_seat, username, "",
 
811
                                            p->dm_seat_cancellable,
 
812
                                            NULL, NULL);
 
813
}
 
814
 
 
815
static void
 
816
my_dispose (GObject * o)
 
817
{
 
818
  IndicatorSessionActionsDbus * self = INDICATOR_SESSION_ACTIONS_DBUS (o);
 
819
  priv_t * p = self->priv;
 
820
 
 
821
  if (p->cancellable != NULL)
 
822
    {
 
823
      g_cancellable_cancel (p->cancellable);
 
824
      g_clear_object (&p->cancellable);
 
825
    }
 
826
 
 
827
  if (p->indicator_settings != NULL)
 
828
    {
 
829
      g_signal_handlers_disconnect_by_data (p->indicator_settings, self);
 
830
      g_clear_object (&p->indicator_settings);
 
831
    }
 
832
 
 
833
  if (p->lockdown_settings != NULL)
 
834
    {
 
835
      g_signal_handlers_disconnect_by_data (p->lockdown_settings, self);
 
836
      g_clear_object (&p->lockdown_settings);
 
837
    }
 
838
 
 
839
  if (p->webcredentials != NULL)
 
840
    {
 
841
      g_signal_handlers_disconnect_by_data (p->webcredentials, self);
 
842
      g_clear_object (&p->webcredentials);
 
843
    }
 
844
 
 
845
  if (p->end_session_dialog != NULL)
 
846
    {
 
847
      stop_listening_to_dialog (self);
 
848
      g_clear_object (&p->end_session_dialog);
 
849
    }
 
850
 
 
851
  g_clear_object (&p->screen_saver);
 
852
  g_clear_object (&p->session_manager);
 
853
  set_dm_seat (self, NULL);
 
854
  set_login1_manager (self, NULL);
 
855
  set_login1_seat (self, NULL);
 
856
 
 
857
  G_OBJECT_CLASS (indicator_session_actions_dbus_parent_class)->dispose (o);
 
858
}
 
859
 
 
860
static void
 
861
my_finalize (GObject * o)
 
862
{
 
863
  IndicatorSessionActionsDbus * self = INDICATOR_SESSION_ACTIONS_DBUS (o);
 
864
  priv_t * p = self->priv;
 
865
 
 
866
  g_free (p->zenity);
 
867
}
 
868
 
 
869
 
 
870
/***
 
871
****  GObject Boilerplate
 
872
***/
 
873
 
 
874
static void
 
875
/* cppcheck-suppress unusedFunction */
 
876
indicator_session_actions_dbus_class_init (IndicatorSessionActionsDbusClass * klass)
 
877
{
 
878
  GObjectClass * object_class;
 
879
  IndicatorSessionActionsClass * actions_class;
 
880
 
 
881
  object_class = G_OBJECT_CLASS (klass);
 
882
  object_class->dispose = my_dispose;
 
883
  object_class->finalize = my_finalize;
 
884
 
 
885
  actions_class = INDICATOR_SESSION_ACTIONS_CLASS (klass);
 
886
  actions_class->can_lock = my_can_lock;
 
887
  actions_class->can_logout = my_can_logout;
 
888
  actions_class->can_reboot = my_can_reboot;
 
889
  actions_class->can_switch = my_can_switch;
 
890
  actions_class->can_suspend = my_can_suspend;
 
891
  actions_class->can_hibernate = my_can_hibernate;
 
892
  actions_class->can_prompt = my_can_prompt;
 
893
  actions_class->has_online_account_error = my_has_online_account_error;
 
894
  actions_class->logout = my_logout;
 
895
  actions_class->suspend = my_suspend;
 
896
  actions_class->hibernate = my_hibernate;
 
897
  actions_class->reboot = my_reboot;
 
898
  actions_class->power_off = my_power_off;
 
899
  actions_class->settings = my_settings;
 
900
  actions_class->online_accounts = my_online_accounts;
 
901
  actions_class->help = my_help;
 
902
  actions_class->about = my_about;
 
903
  actions_class->switch_to_screensaver = my_switch_to_screensaver;
 
904
  actions_class->switch_to_greeter = my_switch_to_greeter;
 
905
  actions_class->switch_to_guest = my_switch_to_guest;
 
906
  actions_class->switch_to_username = my_switch_to_username;
 
907
 
 
908
  g_type_class_add_private (klass, sizeof (IndicatorSessionActionsDbusPriv));
 
909
}
 
910
 
 
911
static void
 
912
/* cppcheck-suppress unusedFunction */
 
913
indicator_session_actions_dbus_init (IndicatorSessionActionsDbus * self)
 
914
{
 
915
  priv_t * p;
 
916
  GSettings * s;
 
917
 
 
918
  p = G_TYPE_INSTANCE_GET_PRIVATE (self,
 
919
                                   INDICATOR_TYPE_SESSION_ACTIONS_DBUS,
 
920
                                   IndicatorSessionActionsDbusPriv);
 
921
  p->cancellable = g_cancellable_new ();
 
922
  p->seat_allows_activation = TRUE;
 
923
  self->priv = p;
 
924
 
 
925
  p->zenity = g_find_program_in_path ("zenity");
 
926
 
 
927
  s = g_settings_new ("org.gnome.desktop.lockdown");
 
928
  g_signal_connect_swapped (s, "changed::disable-lock-screen",
 
929
                            G_CALLBACK(indicator_session_actions_notify_can_lock), self);
 
930
  g_signal_connect_swapped (s, "changed::disable-log-out",
 
931
                            G_CALLBACK(indicator_session_actions_notify_can_logout), self);
 
932
  g_signal_connect_swapped (s, "changed::disable-user-switching",
 
933
                            G_CALLBACK(indicator_session_actions_notify_can_switch), self);
 
934
  p->lockdown_settings = s;
 
935
 
 
936
  s = g_settings_new ("com.canonical.indicator.session");
 
937
  g_signal_connect_swapped (s, "changed::suppress-logout-restart-shutdown",
 
938
                            G_CALLBACK(indicator_session_actions_notify_can_prompt), self);
 
939
  g_signal_connect_swapped (s, "changed::suppress-logout-restart-shutdown",
 
940
                            G_CALLBACK(indicator_session_actions_notify_can_reboot), self);
 
941
  g_signal_connect_swapped (s, "changed::suppress-restart-menuitem",
 
942
                            G_CALLBACK(indicator_session_actions_notify_can_reboot), self);
 
943
  g_signal_connect_swapped (s, "changed::suppress-shutdown-menuitem",
 
944
                            G_CALLBACK(indicator_session_actions_notify_can_reboot), self);
 
945
  p->indicator_settings = s;
 
946
 
 
947
  gnome_screen_saver_proxy_new_for_bus (G_BUS_TYPE_SESSION,
 
948
                                        G_DBUS_PROXY_FLAGS_NONE,
 
949
                                        "org.gnome.ScreenSaver",
 
950
                                        "/org/gnome/ScreenSaver",
 
951
                                        p->cancellable,
 
952
                                        on_screensaver_proxy_ready,
 
953
                                        self);
 
954
 
 
955
  gnome_session_manager_proxy_new_for_bus (G_BUS_TYPE_SESSION,
 
956
                                           G_DBUS_PROXY_FLAGS_NONE,
 
957
                                           "org.gnome.SessionManager",
 
958
                                           "/org/gnome/SessionManager",
 
959
                                           p->cancellable,
 
960
                                           on_session_manager_proxy_ready,
 
961
                                           self);
 
962
 
 
963
  webcredentials_proxy_new_for_bus (G_BUS_TYPE_SESSION,
 
964
                                    G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
 
965
                                    "com.canonical.indicators.webcredentials",
 
966
                                    "/com/canonical/indicators/webcredentials",
 
967
                                    p->cancellable,
 
968
                                    on_webcredentials_proxy_ready,
 
969
                                    self);
 
970
 
 
971
  end_session_dialog_proxy_new_for_bus (G_BUS_TYPE_SESSION,
 
972
                                        G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
 
973
                                        "com.canonical.Unity",
 
974
                                        "/org/gnome/SessionManager/EndSessionDialog",
 
975
                                        p->cancellable,
 
976
                                        on_end_session_dialog_proxy_ready,
 
977
                                        self);
 
978
}
 
979
 
 
980
/***
 
981
****  Public
 
982
***/
 
983
 
 
984
IndicatorSessionActions *
 
985
indicator_session_actions_dbus_new (void)
 
986
{
 
987
  gpointer o = g_object_new (INDICATOR_TYPE_SESSION_ACTIONS_DBUS, NULL);
 
988
 
 
989
  return INDICATOR_SESSION_ACTIONS (o);
 
990
}
 
991
 
 
992
void
 
993
indicator_session_actions_dbus_set_proxies (IndicatorSessionActionsDbus * self,
 
994
                                            Login1Manager               * login1_manager,
 
995
                                            Login1Seat                  * login1_seat,
 
996
                                            DisplayManagerSeat          * dm_seat)
 
997
{
 
998
  g_return_if_fail (INDICATOR_IS_SESSION_ACTIONS_DBUS(self));
 
999
 
 
1000
  set_login1_manager (self, login1_manager);
 
1001
  set_login1_seat (self, login1_seat);
 
1002
  set_dm_seat (self, dm_seat);
 
1003
}