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

« back to all changes in this revision

Viewing changes to src/service.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:
105
105
  int rebuild_flags;
106
106
  GDBusConnection * conn;
107
107
  GCancellable * cancellable;
108
 
 
109
 
  /* serialized icon cache */
110
 
  GVariant * alert_icon_serialized;
111
 
  GVariant * default_icon_serialized;
112
108
};
113
109
 
114
110
typedef IndicatorSessionServicePrivate priv_t;
147
143
****
148
144
***/
149
145
 
150
 
static GVariant *
151
 
action_state_for_header (IndicatorSessionService * self)
 
146
static void
 
147
update_header_action (IndicatorSessionService * self)
152
148
{
153
 
  const priv_t * const p = self->priv;
 
149
  gchar * a11y;
154
150
  gboolean need_attn;
155
 
  GVariant * serialized_icon;
156
151
  gboolean show_name;
 
152
  GVariant * variant;
157
153
  const gchar * real_name;
158
154
  const gchar * label;
159
 
  gchar * a11y;
160
 
  GVariantBuilder b;
161
 
  GVariant * state;
 
155
  const gchar * iconstr;
 
156
  const priv_t * const p = self->priv;
 
157
 
 
158
  g_return_if_fail (p->header_action != NULL);
162
159
 
163
160
  if (indicator_session_actions_has_online_account_error (p->backend_actions))
164
161
    {
165
162
      need_attn = TRUE;
166
 
      serialized_icon = p->alert_icon_serialized;
 
163
      iconstr = ICON_ALERT;
167
164
    }
168
165
  else
169
166
    {
170
167
      need_attn = FALSE;
171
 
      serialized_icon = p->default_icon_serialized;
 
168
      iconstr = ICON_DEFAULT;
172
169
    }
173
170
 
174
171
  show_name = g_settings_get_boolean (p->indicator_settings,
197
194
      a11y = g_strdup (_("System"));
198
195
    }
199
196
 
200
 
  /* build the state */
201
 
  g_variant_builder_init (&b, G_VARIANT_TYPE("a{sv}"));
202
 
  g_variant_builder_add (&b, "{sv}", "accessible-desc", g_variant_new_string (a11y));
203
 
  if (serialized_icon != NULL)
204
 
    g_variant_builder_add (&b, "{sv}", "icon", serialized_icon);
205
 
  if (label && *label)
206
 
    g_variant_builder_add (&b, "{sv}", "label", g_variant_new_string (label));
207
 
  g_variant_builder_add (&b, "{sv}", "visible", g_variant_new_boolean (TRUE));
208
 
  state = g_variant_builder_end (&b);
209
 
 
210
 
  /* cleanup */
 
197
  variant = g_variant_new ("(sssb)", label, iconstr, a11y, TRUE);
 
198
  g_simple_action_set_state (p->header_action, variant);
211
199
  g_free (a11y);
212
 
 
213
 
  return state;
214
 
}
215
 
 
216
 
static void
217
 
update_header_action (IndicatorSessionService * self)
218
 
{
219
 
  g_simple_action_set_state (self->priv->header_action, action_state_for_header (self));
220
200
}
221
201
 
222
202
/***
274
254
}
275
255
 
276
256
static const char *
277
 
get_user_label (const IndicatorSessionUser * user)
278
 
{
279
 
  const char * c;
280
 
 
281
 
  /* If blank or whitespace, use username instead */
282
 
  for (c = user->real_name; *c != '\0' && g_ascii_isspace (*c); c++);
283
 
  if (*c == '\0')
284
 
    return user->user_name;
285
 
 
286
 
  return user->real_name;
287
 
}
288
 
 
289
 
static const char *
290
257
get_current_real_name (IndicatorSessionService * self)
291
258
{
292
259
  GHashTableIter iter;
302
269
    {
303
270
      IndicatorSessionUser * user = value;
304
271
      if (user->is_current_user)
305
 
        return get_user_label (user);
 
272
        return user->real_name;
306
273
    }
307
274
 
308
275
  return "";
448
415
  const IndicatorSessionUser * a = *(const IndicatorSessionUser**)ga;
449
416
  const IndicatorSessionUser * b = *(const IndicatorSessionUser**)gb;
450
417
 
451
 
  if ((i = g_strcmp0 (get_user_label (a), get_user_label (b))))
 
418
  if ((i = g_strcmp0 (a->real_name, b->real_name)))
452
419
    return i;
453
420
 
454
421
  return g_strcmp0 (a->user_name, b->user_name);
455
422
}
456
423
 
457
 
static GVariant *
458
 
serialize_icon_file (const gchar * filename)
459
 
{
460
 
  GVariant * serialized_icon = NULL;
461
 
 
462
 
  if (filename != NULL)
463
 
    {
464
 
      GFile * file = g_file_new_for_path (filename);
465
 
      GIcon * icon = g_file_icon_new (file);
466
 
 
467
 
      serialized_icon = g_icon_serialize (icon);
468
 
 
469
 
      g_object_unref (icon);
470
 
      g_object_unref (file);
471
 
    }
472
 
 
473
 
  return serialized_icon;
474
 
}
475
 
 
476
424
static GMenuModel *
477
425
create_switch_section (IndicatorSessionService * self)
478
426
{
500
448
      item = g_menu_item_new (ellipsis ? _("Switch Account…")
501
449
                                       : _("Switch Account"), action);
502
450
    }
 
451
  else if (g_hash_table_size (p->users) == 1)
 
452
    {
 
453
      const char * action = "indicator.switch-to-greeter";
 
454
      item = g_menu_item_new (_("Lock"), action);
 
455
    }
503
456
  else
504
457
    {
505
 
      const char * action = "indicator.switch-to-screensaver";
506
 
 
507
 
      if (g_hash_table_size (p->users) == 1)
508
 
        item = g_menu_item_new (_("Lock"), action);
509
 
      else
510
 
        item = g_menu_item_new (ellipsis ? _("Lock/Switch Account…")
511
 
                                         : _("Lock/Switch Account"), action);
 
458
      const char * action = "indicator.switch-to-greeter";
 
459
      item = g_menu_item_new (ellipsis ? _("Lock/Switch Account…")
 
460
                                       : _("Lock/Switch Account"), action);
512
461
    }
513
462
  str = g_settings_get_string (p->keybinding_settings, "screensaver");
514
463
  g_menu_item_set_attribute (item, "accel", "s", str);
547
496
  for (i=0; i<users->len; ++i)
548
497
    {
549
498
      const IndicatorSessionUser * u = g_ptr_array_index (users, i);
550
 
      GVariant * serialized_icon;
551
 
 
552
 
      item = g_menu_item_new (get_user_label (u), NULL);
 
499
      item = g_menu_item_new (u->real_name, NULL);
553
500
      g_menu_item_set_action_and_target (item, "indicator.switch-to-user", "s", u->user_name);
554
501
      g_menu_item_set_attribute (item, "x-canonical-type", "s", "indicator.user-menu-item");
555
502
 
556
 
      if ((serialized_icon = serialize_icon_file (u->icon_file)))
 
503
      if (u->icon_file != NULL)
557
504
        {
558
 
          g_menu_item_set_attribute_value (item, G_MENU_ATTRIBUTE_ICON, serialized_icon);
559
 
          g_variant_unref (serialized_icon);
 
505
          GFile * file = g_file_new_for_path (u->icon_file);
 
506
          GIcon * icon = g_file_icon_new (file);
 
507
          g_menu_item_set_attribute_value (item, G_MENU_ATTRIBUTE_ICON, g_icon_serialize (icon));
 
508
          g_clear_object (&icon);
 
509
          g_clear_object (&file);
560
510
        }
561
511
 
562
512
      g_menu_append_item (menu, item);
569
519
}
570
520
 
571
521
static GMenuModel *
572
 
create_session_section (IndicatorSessionService * self, int profile)
 
522
create_session_section (IndicatorSessionService * self)
573
523
{
574
524
  GMenu * menu;
575
525
  const priv_t * const p = self->priv;
578
528
 
579
529
  menu = g_menu_new ();
580
530
 
581
 
  if ((profile == PROFILE_DESKTOP) &&
582
 
      (indicator_session_actions_can_logout (p->backend_actions)))
 
531
  if (indicator_session_actions_can_logout (p->backend_actions) && !g_settings_get_boolean (s, "suppress-logout-menuitem"))
583
532
    {
584
533
      const char * label = ellipsis ? _("Log Out…") : _("Log Out");
585
534
      g_menu_append (menu, label, "indicator.logout");
591
540
  if (indicator_session_actions_can_hibernate (p->backend_actions))
592
541
    g_menu_append (menu, _("Hibernate"), "indicator.hibernate");
593
542
 
594
 
  if (indicator_session_actions_can_reboot (p->backend_actions))
 
543
  /* NB: check 'ellipsis' here to skip this item if prompting is enabled
 
544
     because this shows the same prompt as 'Shut Down' in Unity */
 
545
  if (!ellipsis && !g_settings_get_boolean (s, "suppress-restart-menuitem"))
595
546
    {
596
547
      const char * label = ellipsis ? _("Restart…") : _("Restart");
597
548
      g_menu_append (menu, label, "indicator.reboot");
624
575
      sections[n++] = create_admin_section ();
625
576
      sections[n++] = create_settings_section (self);
626
577
      sections[n++] = create_switch_section (self);
627
 
      sections[n++] = create_session_section (self, profile);
 
578
      sections[n++] = create_session_section (self);
628
579
    }
629
580
  else if (profile == PROFILE_GREETER)
630
581
    {
631
 
      sections[n++] = create_session_section (self, profile);
 
582
      sections[n++] = create_session_section (self);
632
583
    }
633
584
 
634
585
  /* add sections to the submenu */
673
624
}
674
625
 
675
626
static void
676
 
on_online_accounts_activated (GSimpleAction * a      G_GNUC_UNUSED,
677
 
                              GVariant      * param  G_GNUC_UNUSED,
678
 
                              gpointer        gself)
679
 
{
680
 
  indicator_session_actions_online_accounts (get_backend_actions(gself));
681
 
}
682
 
 
683
 
static void
684
627
on_help_activated (GSimpleAction  * a      G_GNUC_UNUSED,
685
628
                   GVariant       * param  G_GNUC_UNUSED,
686
629
                   gpointer         gself)
778
721
  priv_t * p = self->priv;
779
722
 
780
723
  GActionEntry entries[] = {
781
 
    { "about",                  on_about_activated           },
782
 
    { "help",                   on_help_activated            },
783
 
    { "hibernate",              on_hibernate_activated       },
784
 
    { "logout",                 on_logout_activated          },
785
 
    { "online-accounts",        on_online_accounts_activated },
786
 
    { "reboot",                 on_reboot_activated          },
787
 
    { "settings",               on_settings_activated        },
788
 
    { "switch-to-screensaver",  on_screensaver_activated     },
789
 
    { "switch-to-greeter",      on_greeter_activated         },
790
 
    { "suspend",                on_suspend_activated         },
791
 
    { "power-off",              on_power_off_activated       }
 
724
    { "about",                  on_about_activated        },
 
725
    { "help",                   on_help_activated         },
 
726
    { "settings",               on_settings_activated     },
 
727
    { "logout",                 on_logout_activated       },
 
728
    { "suspend",                on_suspend_activated      },
 
729
    { "hibernate",              on_hibernate_activated    },
 
730
    { "reboot",                 on_reboot_activated       },
 
731
    { "power-off",              on_power_off_activated    },
 
732
    { "switch-to-screensaver",  on_screensaver_activated  },
 
733
    { "switch-to-greeter",      on_greeter_activated      }
792
734
  };
793
735
 
794
736
  p->actions = g_simple_action_group_new ();
802
744
  v = create_guest_switcher_state (self);
803
745
  a = g_simple_action_new_stateful ("switch-to-guest", NULL, v);
804
746
  g_signal_connect (a, "activate", G_CALLBACK(on_guest_activated), self);
805
 
  g_action_map_add_action (G_ACTION_MAP (p->actions), G_ACTION(a));
 
747
  g_simple_action_group_insert (p->actions, G_ACTION(a));
806
748
  p->guest_switcher_action = a;
807
749
 
808
750
  /* add switch-to-user action... parameter is the uesrname */
809
751
  v = create_user_switcher_state (self);
810
752
  a = g_simple_action_new_stateful ("switch-to-user", G_VARIANT_TYPE_STRING, v);
811
753
  g_signal_connect (a, "activate", G_CALLBACK(on_user_activated), self);
812
 
  g_action_map_add_action (G_ACTION_MAP (p->actions), G_ACTION(a));
 
754
  g_simple_action_group_insert (p->actions, G_ACTION(a));
813
755
  p->user_switcher_action = a;
814
756
 
815
757
  /* add the header action */
816
 
  a = g_simple_action_new_stateful ("_header", NULL,
817
 
                                    action_state_for_header (self));
818
 
  g_action_map_add_action (G_ACTION_MAP (p->actions), G_ACTION(a));
 
758
  v = g_variant_new ("(sssb)", "label", ICON_DEFAULT, "a11y", TRUE);
 
759
  a = g_simple_action_new_stateful ("_header", NULL, v);
 
760
  g_simple_action_group_insert (p->actions, G_ACTION(a));
819
761
  p->header_action = a;
820
762
 
821
763
  rebuild_now (self, SECTION_HEADER);
868
810
 
869
811
  if (sections & SECTION_SESSION)
870
812
    {
871
 
      rebuild_section (desktop->submenu, 3, create_session_section(self, PROFILE_DESKTOP));
872
 
      rebuild_section (greeter->submenu, 0, create_session_section(self, PROFILE_GREETER));
 
813
      rebuild_section (desktop->submenu, 3, create_session_section(self));
 
814
      rebuild_section (greeter->submenu, 0, create_session_section(self));
873
815
    }
874
816
}
875
817
 
1015
957
  GList * uids;
1016
958
  priv_t * p;
1017
959
  gpointer gp;
1018
 
  GIcon * icon;
1019
960
 
1020
961
  /* init our priv pointer */
1021
962
  p = G_TYPE_INSTANCE_GET_PRIVATE (self,
1031
972
                               &p->backend_users,
1032
973
                               &p->backend_guest);
1033
974
 
1034
 
  /* build the serialized icon cache */
1035
 
 
1036
 
  icon = g_themed_icon_new_with_default_fallbacks (ICON_ALERT);
1037
 
  p->alert_icon_serialized = g_icon_serialize (icon);
1038
 
  g_object_unref (icon);
1039
 
 
1040
 
  icon = g_themed_icon_new_with_default_fallbacks (ICON_DEFAULT);
1041
 
  p->default_icon_serialized = g_icon_serialize (icon);
1042
 
  g_object_unref (icon);
1043
 
 
1044
975
  /* init our key-to-User table */
1045
976
  p->users = g_hash_table_new_full (g_direct_hash,
1046
977
                                    g_direct_equal,
1088
1019
                            G_CALLBACK(rebuild_switch_section_soon), self);
1089
1020
  g_signal_connect_swapped (gp, "changed::suppress-logout-restart-shutdown",
1090
1021
                            G_CALLBACK(rebuild_session_section_soon), self);
 
1022
  g_signal_connect_swapped (gp, "changed::suppress-logout-menuitem",
 
1023
                            G_CALLBACK(rebuild_session_section_soon), self);
 
1024
  g_signal_connect_swapped (gp, "changed::suppress-restart-menuitem",
 
1025
                            G_CALLBACK(rebuild_session_section_soon), self);
1091
1026
  g_signal_connect_swapped (gp, "changed::suppress-shutdown-menuitem",
1092
1027
                            G_CALLBACK(rebuild_session_section_soon), self);
1093
1028
  g_signal_connect_swapped (gp, "changed::show-real-name-on-panel",
1198
1133
  g_clear_object (&p->guest_switcher_action);
1199
1134
  g_clear_object (&p->conn);
1200
1135
 
1201
 
  /* clear the serialized icon cache */
1202
 
  g_clear_pointer (&p->alert_icon_serialized, g_variant_unref);
1203
 
  g_clear_pointer (&p->default_icon_serialized, g_variant_unref);
1204
 
 
1205
1136
  G_OBJECT_CLASS (indicator_session_service_parent_class)->dispose (o);
1206
1137
}
1207
1138