~larsu/indicator-session/glib-deadlock-workaround

« back to all changes in this revision

Viewing changes to src/online-accounts-mgr.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 2012 Canonical Ltd.
3
 
 
4
 
Authors:
5
 
    Alberto Mardegan <alberto.mardegan@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 <gio/gio.h>
21
 
#include <glib/gi18n.h>
22
 
 
23
 
#include "online-accounts-mgr.h"
24
 
 
25
 
#include <libdbusmenu-glib/client.h>
26
 
 
27
 
struct _OnlineAccountsMgr
28
 
{
29
 
  GObject parent_instance;
30
 
  GDBusProxy *proxy;
31
 
  DbusmenuMenuitem *menu_item;
32
 
};
33
 
 
34
 
#define ONLINE_ACCOUNTS_OBJECT_PATH "/com/canonical/indicators/webcredentials"
35
 
#define ONLINE_ACCOUNTS_BUS_NAME "com.canonical.indicators.webcredentials"
36
 
#define ONLINE_ACCOUNTS_INTERFACE ONLINE_ACCOUNTS_BUS_NAME
37
 
 
38
 
G_DEFINE_TYPE (OnlineAccountsMgr, online_accounts_mgr, G_TYPE_OBJECT);
39
 
 
40
 
static void
41
 
update_disposition (OnlineAccountsMgr *self, GVariant *error_status_prop)
42
 
{
43
 
  gboolean error_status;
44
 
 
45
 
  error_status = g_variant_get_boolean (error_status_prop);
46
 
  dbusmenu_menuitem_property_set (self->menu_item,
47
 
                                  DBUSMENU_MENUITEM_PROP_DISPOSITION,
48
 
                                  error_status ?
49
 
                                  DBUSMENU_MENUITEM_DISPOSITION_ALERT :
50
 
                                  DBUSMENU_MENUITEM_DISPOSITION_NORMAL);
51
 
}
52
 
 
53
 
static void
54
 
on_properties_changed (GDBusProxy *proxy,
55
 
                       GVariant *changed_properties,
56
 
                       GStrv invalidated_properties,
57
 
                       OnlineAccountsMgr *self)
58
 
{
59
 
  if (g_variant_n_children (changed_properties) > 0) {
60
 
    GVariantIter *iter;
61
 
    const gchar *key;
62
 
    GVariant *value;
63
 
 
64
 
    g_variant_get (changed_properties, "a{sv}", &iter);
65
 
    while (g_variant_iter_loop (iter, "{&sv}", &key, &value)) {
66
 
      if (g_strcmp0 (key, "ErrorStatus") == 0) {
67
 
        update_disposition (self, value);
68
 
      }
69
 
    }
70
 
    g_variant_iter_free (iter);
71
 
  }
72
 
}
73
 
 
74
 
static void
75
 
on_menu_item_activated (DbusmenuMenuitem *menu_item,
76
 
                        guint timestamp,
77
 
                        OnlineAccountsMgr *self)
78
 
{
79
 
  GError *error = NULL;
80
 
 
81
 
  if (!g_spawn_command_line_async("gnome-control-center credentials", &error))
82
 
  {
83
 
    g_warning("Unable to show control center: %s", error->message);
84
 
    g_error_free(error);
85
 
  }
86
 
}
87
 
 
88
 
static void
89
 
online_accounts_mgr_init (OnlineAccountsMgr *self)
90
 
{
91
 
  GError *error = NULL;
92
 
  GVariant *error_status_prop;
93
 
 
94
 
  self->menu_item = dbusmenu_menuitem_new ();
95
 
  dbusmenu_menuitem_property_set (self->menu_item,
96
 
                                  DBUSMENU_MENUITEM_PROP_TYPE,
97
 
                                  DBUSMENU_CLIENT_TYPES_DEFAULT);
98
 
  dbusmenu_menuitem_property_set (self->menu_item,
99
 
                                  DBUSMENU_MENUITEM_PROP_LABEL,
100
 
                                  _("Online Accounts\342\200\246"));
101
 
  g_signal_connect (self->menu_item,
102
 
                    DBUSMENU_MENUITEM_SIGNAL_ITEM_ACTIVATED,
103
 
                    G_CALLBACK (on_menu_item_activated),
104
 
                    self);
105
 
 
106
 
  self->proxy =
107
 
    g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
108
 
                                   G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
109
 
                                   NULL,
110
 
                                   ONLINE_ACCOUNTS_BUS_NAME,
111
 
                                   ONLINE_ACCOUNTS_OBJECT_PATH,
112
 
                                   ONLINE_ACCOUNTS_INTERFACE,
113
 
                                   NULL,
114
 
                                   &error);
115
 
  if (G_UNLIKELY (error != NULL)) {
116
 
      g_warning ("Couldn't create online_accounts proxy: %s", error->message);
117
 
      g_clear_error (&error);
118
 
      return;
119
 
  }
120
 
 
121
 
  g_signal_connect (self->proxy, "g-properties-changed",
122
 
                    G_CALLBACK (on_properties_changed), self);
123
 
 
124
 
  error_status_prop =
125
 
    g_dbus_proxy_get_cached_property (self->proxy, "ErrorStatus");
126
 
  if (error_status_prop != NULL) {
127
 
    update_disposition (self, error_status_prop);
128
 
    g_variant_unref (error_status_prop);
129
 
  }
130
 
}
131
 
 
132
 
static void
133
 
online_accounts_mgr_dispose (GObject *object)
134
 
{
135
 
  OnlineAccountsMgr *self = ONLINE_ACCOUNTS_MGR (object);
136
 
 
137
 
  if (self->proxy != NULL) {
138
 
    g_object_unref (self->proxy);
139
 
    self->proxy = NULL;
140
 
  }
141
 
 
142
 
  if (self->menu_item != NULL) {
143
 
    g_object_unref (self->menu_item);
144
 
    self->menu_item = NULL;
145
 
  }
146
 
 
147
 
  G_OBJECT_CLASS (online_accounts_mgr_parent_class)->dispose (object);
148
 
}
149
 
 
150
 
static void
151
 
online_accounts_mgr_class_init (OnlineAccountsMgrClass *klass)
152
 
{
153
 
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
154
 
  object_class->dispose = online_accounts_mgr_dispose;
155
 
}
156
 
 
157
 
OnlineAccountsMgr *online_accounts_mgr_new ()
158
 
{
159
 
  return g_object_new (ONLINE_ACCOUNTS_TYPE_MGR, NULL);
160
 
}
161
 
 
162
 
DbusmenuMenuitem *online_accounts_mgr_get_menu_item (OnlineAccountsMgr *self)
163
 
{
164
 
  g_return_val_if_fail (ONLINE_ACCOUNTS_IS_MGR (self), NULL);
165
 
  return self->menu_item;
166
 
}