~unity8-desktop-session-team/indicator-session/indicator-session-using-upstart

« back to all changes in this revision

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

  • Committer: Charles Kerr
  • Date: 2013-07-02 00:26:11 UTC
  • mto: This revision was merged to the branch mainline in revision 399.
  • Revision ID: charles.kerr@canonical.com-20130702002611-lhtxz8ouz9uc2ldx
in cmake/Translations.cmake, use the GNUInstallDirs variables

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 */
19
19
 
20
20
#include <glib.h>
21
 
#include <glib/gi18n.h>
22
21
 
23
22
#include "dbus-end-session-dialog.h"
24
23
#include "dbus-login1-manager.h"
40
39
  GCancellable * cancellable;
41
40
 
42
41
  GSettings * lockdown_settings;
43
 
  GSettings * indicator_settings;
44
42
  GnomeScreenSaver * screen_saver;
45
43
  GnomeSessionManager * session_manager;
46
44
  Login1Manager * login1_manager;
50
48
  GCancellable * dm_seat_cancellable;
51
49
  Webcredentials * webcredentials;
52
50
  EndSessionDialog * end_session_dialog;
53
 
  char * zenity;
54
51
 
55
52
  gboolean can_suspend;
56
53
  gboolean can_hibernate;
83
80
****
84
81
***/
85
82
 
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
83
static void
125
84
on_seat_notify_multi_session (IndicatorSessionActionsDbus * self)
126
85
{
169
128
  if (p->dm_seat != NULL)
170
129
    {
171
130
      g_cancellable_cancel (p->dm_seat_cancellable);
172
 
      g_clear_object (&p->dm_seat_cancellable);
 
131
      g_clear_object (&p->dm_seat);
173
132
      g_clear_object (&p->dm_seat);
174
133
    }
175
134
 
311
270
 
312
271
      g_signal_connect_swapped (webcredentials, "notify::error-status",
313
272
                                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
273
    }
318
274
 
319
275
  log_and_clear_error (&err, G_STRLOC, G_STRFUNC);
332
288
      INDICATOR_SESSION_ACTIONS_DBUS(gself)->priv->end_session_dialog = end_session_dialog;
333
289
 
334
290
      indicator_session_actions_notify_can_prompt (INDICATOR_SESSION_ACTIONS(gself));
335
 
      indicator_session_actions_notify_can_reboot (INDICATOR_SESSION_ACTIONS(gself));
336
291
    }
337
292
 
338
293
  log_and_clear_error (&err, G_STRLOC, G_STRFUNC);
355
310
{
356
311
  priv_t * p = INDICATOR_SESSION_ACTIONS_DBUS(self)->priv;
357
312
 
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;
 
313
  return !g_settings_get_boolean (p->lockdown_settings, "disable-log-out");
383
314
}
384
315
 
385
316
static gboolean
410
341
static gboolean
411
342
my_can_prompt (IndicatorSessionActions * self)
412
343
{
413
 
  return get_prompt_status(INDICATOR_SESSION_ACTIONS_DBUS(self)) != PROMPT_NONE;
 
344
  gboolean can_prompt = FALSE;
 
345
  const priv_t * p = INDICATOR_SESSION_ACTIONS_DBUS(self)->priv;
 
346
 
 
347
  if (p && p->end_session_dialog)
 
348
    {
 
349
      GDBusProxy * proxy = G_DBUS_PROXY (p->end_session_dialog);
 
350
      char * name = g_dbus_proxy_get_name_owner (proxy);
 
351
      can_prompt = name != NULL;
 
352
      g_free (name);
 
353
    }
 
354
 
 
355
  return can_prompt;
414
356
}
415
357
 
416
358
static gboolean
454
396
***/
455
397
 
456
398
static void
457
 
logout_now (IndicatorSessionActionsDbus * self)
 
399
logout_now (IndicatorSessionActions * self, gboolean try_to_prompt)
458
400
{
459
 
  priv_t * p = self->priv;
 
401
  priv_t * p = INDICATOR_SESSION_ACTIONS_DBUS(self)->priv;
 
402
  const int type = try_to_prompt ? 0 : 1;
460
403
 
461
404
  g_return_if_fail (p->session_manager != NULL);
462
405
 
463
406
  gnome_session_manager_call_logout (p->session_manager,
464
 
                                     1, /* don't prompt */
 
407
                                     type,
465
408
                                     p->cancellable,
466
409
                                     NULL,
467
410
                                     NULL);
468
411
}
469
412
 
470
413
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;
 
414
logout_now_with_prompt (IndicatorSessionActions * self)
 
415
{
 
416
  logout_now (self, TRUE);
 
417
}
 
418
 
 
419
static void
 
420
logout_now_quietly (IndicatorSessionActions * self)
 
421
{
 
422
  logout_now (self, FALSE);
 
423
}
 
424
 
 
425
static void
 
426
reboot_now (IndicatorSessionActions * self)
 
427
{
 
428
  priv_t * p = INDICATOR_SESSION_ACTIONS_DBUS(self)->priv;
488
429
 
489
430
  g_return_if_fail (p->login1_manager != NULL);
490
431
 
491
432
  login1_manager_call_reboot (p->login1_manager,
492
433
                              FALSE,
493
434
                              p->login1_manager_cancellable,
494
 
                              on_reboot_response,
 
435
                              NULL,
495
436
                              NULL);
496
437
}
497
438
 
498
439
static void
499
 
power_off_now (IndicatorSessionActionsDbus * self)
 
440
power_off_now (IndicatorSessionActions * self)
500
441
{
501
 
  priv_t * p = self->priv;
 
442
  priv_t * p = INDICATOR_SESSION_ACTIONS_DBUS(self)->priv;
502
443
 
503
444
  g_return_if_fail (p->login1_manager != NULL);
504
445
 
536
477
}
537
478
 
538
479
static void
539
 
show_unity_end_session_dialog (IndicatorSessionActionsDbus * self, int type)
 
480
show_end_session_dialog (IndicatorSessionActionsDbus * self, int type)
540
481
{
541
482
  priv_t * p = INDICATOR_SESSION_ACTIONS_DBUS(self)->priv;
542
483
  gpointer o = p->end_session_dialog;
544
485
 
545
486
  g_assert (o != NULL);
546
487
 
547
 
  g_signal_connect_swapped (o, "confirmed-logout", G_CALLBACK(logout_now), self);
 
488
  g_signal_connect_swapped (o, "confirmed-logout", G_CALLBACK(logout_now_quietly), self);
548
489
  g_signal_connect_swapped (o, "confirmed-reboot", G_CALLBACK(reboot_now), self);
549
490
  g_signal_connect_swapped (o, "confirmed-shutdown", G_CALLBACK(power_off_now), self);
550
491
  g_signal_connect_swapped (o, "canceled", G_CALLBACK(on_end_session_dialog_canceled), self);
556
497
                                self);
557
498
}
558
499
 
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
 
    }
 
500
static void
 
501
my_logout (IndicatorSessionActions * self)
 
502
{
 
503
  if (my_can_prompt (self))
 
504
    show_end_session_dialog (INDICATOR_SESSION_ACTIONS_DBUS(self), END_SESSION_TYPE_LOGOUT);
 
505
  else
 
506
    logout_now_with_prompt (self);
 
507
}
 
508
 
 
509
 
 
510
static void
 
511
my_reboot (IndicatorSessionActions * self)
 
512
{
 
513
  if (my_can_prompt (self))
 
514
    show_end_session_dialog (INDICATOR_SESSION_ACTIONS_DBUS(self), END_SESSION_TYPE_REBOOT);
 
515
  else
 
516
    reboot_now (self);
 
517
}
 
518
 
 
519
static void
 
520
my_power_off (IndicatorSessionActions * self)
 
521
{
 
522
  /* NB: TYPE_REBOOT instead of TYPE_SHUTDOWN because
 
523
     the latter adds lock & logout options in Unity... */
 
524
  if (my_can_prompt (self))
 
525
    show_end_session_dialog (INDICATOR_SESSION_ACTIONS_DBUS(self), END_SESSION_TYPE_REBOOT);
 
526
  else
 
527
    power_off_now (self);
693
528
}
694
529
 
695
530
/***
711
546
  run_outside_app ("yelp");
712
547
}
713
548
 
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
549
static void
731
550
my_settings (IndicatorSessionActions * self G_GNUC_UNUSED)
732
551
{
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");
 
552
  run_outside_app ("gnome-control-center");
746
553
}
747
554
 
748
555
static void
749
556
my_about (IndicatorSessionActions * self G_GNUC_UNUSED)
750
557
{
751
 
  if (have_unity_control_center ())
752
 
    run_outside_app ("unity-control-center info");
753
 
  else
754
 
    run_outside_app ("gnome-control-center info");
 
558
  run_outside_app ("gnome-control-center info");
755
559
}
756
560
 
757
561
/***
759
563
***/
760
564
 
761
565
static void
762
 
lock_current_session (IndicatorSessionActions * self)
 
566
my_switch_to_screensaver (IndicatorSessionActions * self)
763
567
{
764
568
  priv_t * p = INDICATOR_SESSION_ACTIONS_DBUS(self)->priv;
765
569
 
769
573
}
770
574
 
771
575
static void
772
 
my_switch_to_screensaver (IndicatorSessionActions * self)
773
 
{
774
 
  lock_current_session (self);
775
 
}
776
 
 
777
 
static void
778
576
my_switch_to_greeter (IndicatorSessionActions * self)
779
577
{
780
578
  priv_t * p = INDICATOR_SESSION_ACTIONS_DBUS(self)->priv;
793
591
 
794
592
  g_return_if_fail (p->dm_seat != NULL);
795
593
 
796
 
  lock_current_session (self);
797
 
 
798
594
  display_manager_seat_call_switch_to_guest (p->dm_seat, "",
799
595
                                             p->dm_seat_cancellable,
800
596
                                             NULL, NULL);
824
620
      g_clear_object (&p->cancellable);
825
621
    }
826
622
 
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
 
 
 
623
  g_clear_object (&p->lockdown_settings);
851
624
  g_clear_object (&p->screen_saver);
852
625
  g_clear_object (&p->session_manager);
 
626
  g_clear_object (&p->webcredentials);
 
627
  g_clear_object (&p->end_session_dialog);
853
628
  set_dm_seat (self, NULL);
854
629
  set_login1_manager (self, NULL);
855
630
  set_login1_seat (self, NULL);
857
632
  G_OBJECT_CLASS (indicator_session_actions_dbus_parent_class)->dispose (o);
858
633
}
859
634
 
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
635
/***
871
636
****  GObject Boilerplate
872
637
***/
880
645
 
881
646
  object_class = G_OBJECT_CLASS (klass);
882
647
  object_class->dispose = my_dispose;
883
 
  object_class->finalize = my_finalize;
884
648
 
885
649
  actions_class = INDICATOR_SESSION_ACTIONS_CLASS (klass);
886
650
  actions_class->can_lock = my_can_lock;
887
651
  actions_class->can_logout = my_can_logout;
888
 
  actions_class->can_reboot = my_can_reboot;
889
652
  actions_class->can_switch = my_can_switch;
890
653
  actions_class->can_suspend = my_can_suspend;
891
654
  actions_class->can_hibernate = my_can_hibernate;
897
660
  actions_class->reboot = my_reboot;
898
661
  actions_class->power_off = my_power_off;
899
662
  actions_class->settings = my_settings;
900
 
  actions_class->online_accounts = my_online_accounts;
901
663
  actions_class->help = my_help;
902
664
  actions_class->about = my_about;
903
665
  actions_class->switch_to_screensaver = my_switch_to_screensaver;
922
684
  p->seat_allows_activation = TRUE;
923
685
  self->priv = p;
924
686
 
925
 
  p->zenity = g_find_program_in_path ("zenity");
926
 
 
927
687
  s = g_settings_new ("org.gnome.desktop.lockdown");
928
688
  g_signal_connect_swapped (s, "changed::disable-lock-screen",
929
689
                            G_CALLBACK(indicator_session_actions_notify_can_lock), self);
933
693
                            G_CALLBACK(indicator_session_actions_notify_can_switch), self);
934
694
  p->lockdown_settings = s;
935
695
 
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
696
  gnome_screen_saver_proxy_new_for_bus (G_BUS_TYPE_SESSION,
948
697
                                        G_DBUS_PROXY_FLAGS_NONE,
949
698
                                        "org.gnome.ScreenSaver",