~mardy/indicator-session/webcredentials

« back to all changes in this revision

Viewing changes to src/webcredentials-mgr.c

  • Committer: Alberto Mardegan
  • Date: 2012-02-08 13:24:25 UTC
  • Revision ID: alberto.mardegan@canonical.com-20120208132425-qqibt14slx35s5ud
Initial implementation of webcredentials session service

This commit defines the DBus API for the webcredentials service and implements
the bookkeeping of the account failures.

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 <glib/gi18n.h>
 
21
 
 
22
#include "webcredentials-mgr.h"
 
23
 
 
24
struct _WebcredentialsMgr
 
25
{
 
26
  WebcredentialsSkeleton parent_instance;
 
27
  GDBusConnection *connection;
 
28
  DbusmenuMenuitem *menu_item;
 
29
  GHashTable *failed_accounts;
 
30
};
 
31
 
 
32
#define WEBCREDENTIALS_OBJECT_PATH "/com/canonical/indicators/webcredentials"
 
33
 
 
34
G_DEFINE_TYPE (WebcredentialsMgr, webcredentials_mgr,
 
35
               TYPE_WEBCREDENTIALS_SKELETON);
 
36
 
 
37
static void
 
38
update_failed_accounts_property (WebcredentialsMgr *self)
 
39
{
 
40
  GHashTableIter iter;
 
41
  GArray *account_ids;
 
42
  GVariant *value;
 
43
  gpointer key;
 
44
 
 
45
  account_ids = g_array_sized_new (FALSE, FALSE,
 
46
                                   sizeof (guint32),
 
47
                                   g_hash_table_size (self->failed_accounts));
 
48
 
 
49
  g_hash_table_iter_init (&iter, self->failed_accounts);
 
50
  while (g_hash_table_iter_next (&iter, &key, NULL)) {
 
51
    g_array_append_val (account_ids, key);
 
52
  }
 
53
 
 
54
  value = g_variant_new_fixed_array (G_VARIANT_TYPE_UINT32,
 
55
                                     account_ids->data,
 
56
                                     account_ids->len,
 
57
                                     sizeof (guint32));
 
58
  g_object_set ((GObject *)self,
 
59
                "failures", value,
 
60
                NULL);
 
61
  g_array_free (account_ids, TRUE);
 
62
}
 
63
 
 
64
static gboolean
 
65
webcredentials_mgr_clear_error_status (WebcredentialsMgr *self,
 
66
                                       GDBusMethodInvocation *invocation)
 
67
{
 
68
  g_return_val_if_fail (WEBCREDENTIALS_IS_MGR (self), FALSE);
 
69
 
 
70
  /* TODO */
 
71
 
 
72
  webcredentials_complete_clear_error_status ((Webcredentials *)self,
 
73
                                              invocation);
 
74
  return TRUE;
 
75
}
 
76
 
 
77
static gboolean
 
78
webcredentials_mgr_remove_failures (WebcredentialsMgr *self,
 
79
                                    GDBusMethodInvocation *invocation,
 
80
                                    GVariant *arg_account_ids)
 
81
{
 
82
  gsize n_accounts = 0;
 
83
  const guint32 *account_ids;
 
84
  gsize i;
 
85
  gboolean changed = FALSE;
 
86
 
 
87
  g_return_val_if_fail (WEBCREDENTIALS_IS_MGR (self), FALSE);
 
88
 
 
89
  account_ids = g_variant_get_fixed_array (arg_account_ids,
 
90
                                           &n_accounts,
 
91
                                           sizeof (guint32));
 
92
  for (i = 0; i < n_accounts; i++) {
 
93
    if (g_hash_table_remove (self->failed_accounts,
 
94
                             GINT_TO_POINTER (account_ids[i])))
 
95
      changed = TRUE;
 
96
  }
 
97
 
 
98
  if (changed)
 
99
    update_failed_accounts_property (self);
 
100
 
 
101
  webcredentials_complete_remove_failures ((Webcredentials *)self,
 
102
                                           invocation);
 
103
  return TRUE;
 
104
}
 
105
 
 
106
static gboolean
 
107
webcredentials_mgr_report_failure (WebcredentialsMgr *self,
 
108
                                   GDBusMethodInvocation *invocation,
 
109
                                   guint account_id,
 
110
                                   GVariant *arg_notification)
 
111
{
 
112
  g_return_val_if_fail (WEBCREDENTIALS_IS_MGR (self), FALSE);
 
113
 
 
114
  g_hash_table_insert (self->failed_accounts,
 
115
                       GINT_TO_POINTER(account_id),
 
116
                       GINT_TO_POINTER(TRUE));
 
117
  update_failed_accounts_property (self);
 
118
 
 
119
  /* TODO: emit the notification and set the error status */
 
120
 
 
121
  webcredentials_complete_report_failure ((Webcredentials *)self,
 
122
                                          invocation);
 
123
  return TRUE;
 
124
}
 
125
 
 
126
static void
 
127
webcredentials_mgr_init (WebcredentialsMgr *self)
 
128
{
 
129
  GError *error = NULL;
 
130
 
 
131
  self->menu_item = dbusmenu_menuitem_new ();
 
132
  /* TODO: setup the menu item */
 
133
 
 
134
  self->failed_accounts = g_hash_table_new (g_direct_hash,
 
135
                                            g_direct_equal);
 
136
  self->connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
 
137
  if (G_UNLIKELY (error != NULL)) {
 
138
    g_warning ("Couldn't connect to session bus: %s", error->message);
 
139
    g_clear_error (&error);
 
140
    return;
 
141
  }
 
142
 
 
143
  g_signal_connect (self, "handle-clear-error-status",
 
144
                    G_CALLBACK (webcredentials_mgr_clear_error_status), NULL);
 
145
  g_signal_connect (self, "handle-remove-failures",
 
146
                    G_CALLBACK (webcredentials_mgr_remove_failures), NULL);
 
147
  g_signal_connect (self, "handle-report-failure",
 
148
                    G_CALLBACK (webcredentials_mgr_report_failure), NULL);
 
149
 
 
150
  if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (self),
 
151
                                         self->connection,
 
152
                                         WEBCREDENTIALS_OBJECT_PATH,
 
153
                                         &error)) {
 
154
    g_return_if_fail (error != NULL);
 
155
    g_warning ("Couldn't register webcredentials service: %s", error->message);
 
156
    g_clear_error (&error);
 
157
  }
 
158
}
 
159
 
 
160
static void
 
161
webcredentials_mgr_dispose (GObject *object)
 
162
{
 
163
  WebcredentialsMgr *self = WEBCREDENTIALS_MGR (object);
 
164
 
 
165
  if (self->menu_item != NULL) {
 
166
    g_object_unref (self->menu_item);
 
167
    self->menu_item = NULL;
 
168
  }
 
169
 
 
170
  if (self->connection != NULL) {
 
171
    g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (self));
 
172
    g_object_unref (self->connection);
 
173
    self->connection = NULL;
 
174
  }
 
175
 
 
176
  G_OBJECT_CLASS (webcredentials_mgr_parent_class)->dispose (object);
 
177
}
 
178
 
 
179
static void
 
180
webcredentials_mgr_finalize (GObject *object)
 
181
{
 
182
  WebcredentialsMgr *self = WEBCREDENTIALS_MGR (object);
 
183
 
 
184
  g_hash_table_destroy (self->failed_accounts);
 
185
 
 
186
  G_OBJECT_CLASS (webcredentials_mgr_parent_class)->finalize (object);
 
187
}
 
188
 
 
189
static void
 
190
webcredentials_mgr_class_init (WebcredentialsMgrClass *klass)
 
191
{
 
192
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
193
  object_class->dispose = webcredentials_mgr_dispose;
 
194
  object_class->finalize = webcredentials_mgr_finalize;
 
195
}
 
196
 
 
197
WebcredentialsMgr *webcredentials_mgr_new ()
 
198
{
 
199
  return g_object_new (WEBCREDENTIALS_TYPE_MGR, NULL);
 
200
}
 
201
 
 
202
DbusmenuMenuitem *webcredentials_mgr_get_menu_item (WebcredentialsMgr *self)
 
203
{
 
204
  g_return_val_if_fail (WEBCREDENTIALS_IS_MGR (self), NULL);
 
205
  return self->menu_item;
 
206
}