~ubuntu-branches/ubuntu/saucy/gnome-control-center/saucy-proposed

« back to all changes in this revision

Viewing changes to .pc/git_new_goa_build.patch/panels/online-accounts/cc-online-accounts-model.c

  • Committer: Package Import Robot
  • Author(s): Sebastien Bacher
  • Date: 2013-06-14 11:31:15 UTC
  • Revision ID: package-import@ubuntu.com-20130614113115-yqvpx8zuhk4oets0
Tags: 1:3.6.3-0ubuntu28
* debian/patches/git_new_goa_build.patch:
  - update for the api change in the new gnome-online-account, fix the build

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
 
2
 *
 
3
 * Copyright (C) 2008-2011 Red Hat, Inc.
 
4
 *
 
5
 * This library is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU Lesser General Public
 
7
 * License as published by the Free Software Foundation; either
 
8
 * version 2 of the License, or (at your option) any later version.
 
9
 *
 
10
 * This library is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 * Lesser General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Lesser General
 
16
 * Public License along with this library; if not, write to the
 
17
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 
18
 * Boston, MA 02111-1307, USA.
 
19
 *
 
20
 * Author: David Zeuthen <davidz@redhat.com>
 
21
 */
 
22
 
 
23
#include "config.h"
 
24
 
 
25
#include <glib/gi18n-lib.h>
 
26
 
 
27
#define GOA_API_IS_SUBJECT_TO_CHANGE
 
28
#define GOA_BACKEND_API_IS_SUBJECT_TO_CHANGE
 
29
#include <goabackend/goabackend.h>
 
30
 
 
31
#include "cc-online-accounts-model.h"
 
32
 
 
33
struct _GoaPanelAccountsModel
 
34
{
 
35
  GtkListStore parent_instance;
 
36
 
 
37
  GoaClient *client;
 
38
};
 
39
 
 
40
typedef struct
 
41
{
 
42
  GtkListStoreClass parent_class;
 
43
} GoaPanelAccountsModelClass;
 
44
 
 
45
enum
 
46
{
 
47
  PROP_0,
 
48
  PROP_CLIENT,
 
49
};
 
50
 
 
51
static void init_model (GoaPanelAccountsModel *model);
 
52
 
 
53
static gboolean
 
54
find_iter_for_object (GoaPanelAccountsModel *model,
 
55
                      GoaObject             *object,
 
56
                      GtkTreeIter           *out_iter);
 
57
 
 
58
static void on_account_added (GoaClient   *client,
 
59
                              GoaObject   *object,
 
60
                              gpointer     user_data);
 
61
 
 
62
static void on_account_removed (GoaClient   *client,
 
63
                                GoaObject   *object,
 
64
                                gpointer     user_data);
 
65
 
 
66
static void on_account_changed (GoaClient   *client,
 
67
                                GoaObject   *object,
 
68
                                gpointer     user_data);
 
69
 
 
70
G_DEFINE_TYPE (GoaPanelAccountsModel, goa_panel_accounts_model, GTK_TYPE_LIST_STORE);
 
71
 
 
72
static void
 
73
goa_panel_accounts_model_finalize (GObject *object)
 
74
{
 
75
  GoaPanelAccountsModel *model = GOA_PANEL_ACCOUNTS_MODEL (object);
 
76
 
 
77
  g_signal_handlers_disconnect_by_func (model->client, G_CALLBACK (on_account_added), model);
 
78
  g_signal_handlers_disconnect_by_func (model->client, G_CALLBACK (on_account_removed), model);
 
79
  g_signal_handlers_disconnect_by_func (model->client, G_CALLBACK (on_account_changed), model);
 
80
  g_object_unref (model->client);
 
81
 
 
82
  G_OBJECT_CLASS (goa_panel_accounts_model_parent_class)->finalize (object);
 
83
}
 
84
 
 
85
static void
 
86
goa_panel_accounts_model_init (GoaPanelAccountsModel *model)
 
87
{
 
88
}
 
89
 
 
90
static void
 
91
goa_panel_accounts_model_get_property (GObject    *object,
 
92
                                       guint       prop_id,
 
93
                                       GValue     *value,
 
94
                                       GParamSpec *pspec)
 
95
{
 
96
  GoaPanelAccountsModel *model = GOA_PANEL_ACCOUNTS_MODEL (object);
 
97
 
 
98
  switch (prop_id)
 
99
    {
 
100
    case PROP_CLIENT:
 
101
      g_value_set_object (value, goa_panel_accounts_model_get_client (model));
 
102
      break;
 
103
 
 
104
    default:
 
105
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
106
      break;
 
107
    }
 
108
}
 
109
 
 
110
static void
 
111
goa_panel_accounts_model_set_property (GObject      *object,
 
112
                                       guint         prop_id,
 
113
                                       const GValue *value,
 
114
                                       GParamSpec   *pspec)
 
115
{
 
116
  GoaPanelAccountsModel *model = GOA_PANEL_ACCOUNTS_MODEL (object);
 
117
 
 
118
  switch (prop_id)
 
119
    {
 
120
    case PROP_CLIENT:
 
121
      model->client = g_value_dup_object (value);
 
122
      break;
 
123
 
 
124
    default:
 
125
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
126
      break;
 
127
    }
 
128
}
 
129
 
 
130
/* ---------------------------------------------------------------------------------------------------- */
 
131
 
 
132
static void
 
133
goa_panel_accounts_model_constructed (GObject *object)
 
134
{
 
135
  GoaPanelAccountsModel *model = GOA_PANEL_ACCOUNTS_MODEL (object);
 
136
  GType types[GOA_PANEL_ACCOUNTS_MODEL_N_COLUMNS];
 
137
 
 
138
  G_STATIC_ASSERT (5 == GOA_PANEL_ACCOUNTS_MODEL_N_COLUMNS);
 
139
 
 
140
  types[0] = G_TYPE_STRING;
 
141
  types[1] = GOA_TYPE_OBJECT;
 
142
  types[2] = G_TYPE_BOOLEAN;
 
143
  types[3] = G_TYPE_STRING;
 
144
  types[4] = G_TYPE_ICON;
 
145
 
 
146
  gtk_list_store_set_column_types (GTK_LIST_STORE (model),
 
147
                                   GOA_PANEL_ACCOUNTS_MODEL_N_COLUMNS,
 
148
                                   types);
 
149
 
 
150
  gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model),
 
151
                                        GOA_PANEL_ACCOUNTS_MODEL_COLUMN_SORT_KEY,
 
152
                                        GTK_SORT_ASCENDING);
 
153
 
 
154
  g_signal_connect (model->client,
 
155
                    "account-added",
 
156
                    G_CALLBACK (on_account_added),
 
157
                    model);
 
158
  g_signal_connect (model->client,
 
159
                    "account-removed",
 
160
                    G_CALLBACK (on_account_removed),
 
161
                    model);
 
162
  g_signal_connect (model->client,
 
163
                    "account-changed",
 
164
                    G_CALLBACK (on_account_changed),
 
165
                    model);
 
166
 
 
167
  init_model (model);
 
168
 
 
169
  if (G_OBJECT_CLASS (goa_panel_accounts_model_parent_class)->constructed != NULL)
 
170
    G_OBJECT_CLASS (goa_panel_accounts_model_parent_class)->constructed (object);
 
171
}
 
172
 
 
173
static void
 
174
goa_panel_accounts_model_class_init (GoaPanelAccountsModelClass *klass)
 
175
{
 
176
  GObjectClass *gobject_class;
 
177
 
 
178
  gobject_class = G_OBJECT_CLASS (klass);
 
179
  gobject_class->finalize     = goa_panel_accounts_model_finalize;
 
180
  gobject_class->constructed  = goa_panel_accounts_model_constructed;
 
181
  gobject_class->get_property = goa_panel_accounts_model_get_property;
 
182
  gobject_class->set_property = goa_panel_accounts_model_set_property;
 
183
 
 
184
  /**
 
185
   * GoaPanelAccountsModel:client:
 
186
   *
 
187
   * The #GoaClient used by the #GoaPanelAccountsModel instance.
 
188
   */
 
189
  g_object_class_install_property (gobject_class,
 
190
                                   PROP_CLIENT,
 
191
                                   g_param_spec_object ("client",
 
192
                                                        "Client",
 
193
                                                        "The client used by the tree model",
 
194
                                                        GOA_TYPE_CLIENT,
 
195
                                                        G_PARAM_READABLE |
 
196
                                                        G_PARAM_WRITABLE |
 
197
                                                        G_PARAM_CONSTRUCT_ONLY |
 
198
                                                        G_PARAM_STATIC_STRINGS));
 
199
}
 
200
 
 
201
/**
 
202
 * goa_panel_accounts_model_new:
 
203
 * @client: A #GoaClient.
 
204
 *
 
205
 * Creates a new #GoaPanelAccountsModel for viewing the accounts known
 
206
 * by @client.
 
207
 *
 
208
 * Returns: A #GoaPanelAccountsModel. Free with g_object_unref().
 
209
 */
 
210
GoaPanelAccountsModel *
 
211
goa_panel_accounts_model_new (GoaClient  *client)
 
212
{
 
213
  return GOA_PANEL_ACCOUNTS_MODEL (g_object_new (GOA_TYPE_PANEL_ACCOUNTS_MODEL,
 
214
                                                 "client", client,
 
215
                                                 NULL));
 
216
}
 
217
 
 
218
/**
 
219
 * goa_panel_accounts_model_get_client:
 
220
 * @model: A #GoaPanelAccountsModel.
 
221
 *
 
222
 * Gets the #GoaClient used by @model.
 
223
 *
 
224
 * Returns: (transfer none): A #GoaClient. Do not free, the object
 
225
 *   belongs to @model.
 
226
 */
 
227
GoaClient *
 
228
goa_panel_accounts_model_get_client (GoaPanelAccountsModel *model)
 
229
{
 
230
  g_return_val_if_fail (GOA_IS_PANEL_ACCOUNTS_MODEL (model), NULL);
 
231
  return model->client;
 
232
}
 
233
 
 
234
/**
 
235
 * goa_panel_accounts_model_get_iter_for_object:
 
236
 * @model: A #GoaPanelAccountsModel.
 
237
 * @object: A #GoaObject.
 
238
 * @iter: (out): Return location for #GtkTreeIter.
 
239
 *
 
240
 * Finds @model<!-- -->'s row for @object.
 
241
 *
 
242
 * Returns: %TRUE if @iter was set, %FALSE if @object wasn't found.
 
243
 */
 
244
gboolean
 
245
goa_panel_accounts_model_get_iter_for_object (GoaPanelAccountsModel  *model,
 
246
                                              GoaObject              *object,
 
247
                                              GtkTreeIter            *iter)
 
248
{
 
249
  g_return_val_if_fail (GOA_IS_PANEL_ACCOUNTS_MODEL (model), FALSE);
 
250
  g_return_val_if_fail (GOA_IS_OBJECT (object), FALSE);
 
251
  g_return_val_if_fail (iter != NULL, FALSE);
 
252
  return find_iter_for_object (model, object, iter);
 
253
}
 
254
 
 
255
/* ---------------------------------------------------------------------------------------------------- */
 
256
 
 
257
typedef struct
 
258
{
 
259
  GoaObject *object;
 
260
  GtkTreeIter iter;
 
261
  gboolean found;
 
262
} FindIterData;
 
263
 
 
264
static gboolean
 
265
find_iter_for_object_cb (GtkTreeModel  *model,
 
266
                         GtkTreePath   *path,
 
267
                         GtkTreeIter   *iter,
 
268
                         gpointer       user_data)
 
269
{
 
270
  FindIterData *data = user_data;
 
271
  GoaObject *iter_object;
 
272
 
 
273
  iter_object = NULL;
 
274
 
 
275
  gtk_tree_model_get (model,
 
276
                      iter,
 
277
                      GOA_PANEL_ACCOUNTS_MODEL_COLUMN_OBJECT, &iter_object,
 
278
                      -1);
 
279
  if (iter_object == NULL)
 
280
    goto out;
 
281
 
 
282
  if (iter_object == data->object)
 
283
    {
 
284
      data->iter = *iter;
 
285
      data->found = TRUE;
 
286
      goto out;
 
287
    }
 
288
 
 
289
 out:
 
290
  if (iter_object != NULL)
 
291
    g_object_unref (iter_object);
 
292
  return data->found;
 
293
}
 
294
 
 
295
static gboolean
 
296
find_iter_for_object (GoaPanelAccountsModel *model,
 
297
                      GoaObject             *object,
 
298
                      GtkTreeIter           *out_iter)
 
299
{
 
300
  FindIterData data;
 
301
  memset (&data, 0, sizeof (data));
 
302
  data.object = object;
 
303
  data.found = FALSE;
 
304
  gtk_tree_model_foreach (GTK_TREE_MODEL (model),
 
305
                          find_iter_for_object_cb,
 
306
                          &data);
 
307
  if (data.found)
 
308
    {
 
309
      if (out_iter != NULL)
 
310
        *out_iter = data.iter;
 
311
    }
 
312
  return data.found;
 
313
}
 
314
 
 
315
/* ---------------------------------------------------------------------------------------------------- */
 
316
 
 
317
static void
 
318
set_values (GoaPanelAccountsModel  *model,
 
319
            GoaObject              *object,
 
320
            GtkTreeIter            *iter)
 
321
{
 
322
  GoaAccount *account;
 
323
  GIcon *icon;
 
324
  gchar *markup;
 
325
  GError *error;
 
326
 
 
327
  account = goa_object_peek_account (object);
 
328
 
 
329
  error = NULL;
 
330
  icon = g_icon_new_for_string (goa_account_get_provider_icon (account), &error);
 
331
  if (icon == NULL)
 
332
    {
 
333
      goa_warning ("Error creating GIcon for account: %s (%s, %d)",
 
334
                   error->message, g_quark_to_string (error->domain), error->code);
 
335
      g_error_free (error);
 
336
    }
 
337
 
 
338
  markup = g_strdup_printf ("<b>%s</b>\n<small>%s</small>",
 
339
                            goa_account_get_provider_name (account),
 
340
                            goa_account_get_presentation_identity (account));
 
341
 
 
342
  gtk_list_store_set (GTK_LIST_STORE (model),
 
343
                      iter,
 
344
                      GOA_PANEL_ACCOUNTS_MODEL_COLUMN_SORT_KEY, goa_account_get_id (account),
 
345
                      GOA_PANEL_ACCOUNTS_MODEL_COLUMN_OBJECT, object,
 
346
                      GOA_PANEL_ACCOUNTS_MODEL_COLUMN_ATTENTION_NEEDED, goa_account_get_attention_needed (account),
 
347
                      GOA_PANEL_ACCOUNTS_MODEL_COLUMN_MARKUP, markup,
 
348
                      GOA_PANEL_ACCOUNTS_MODEL_COLUMN_ICON, icon,
 
349
                      -1);
 
350
 
 
351
  g_free (markup);
 
352
  g_clear_object (&icon);
 
353
}
 
354
 
 
355
static void
 
356
add_account (GoaPanelAccountsModel  *model,
 
357
             GoaObject              *object)
 
358
{
 
359
  GtkTreeIter iter;
 
360
  gtk_list_store_insert (GTK_LIST_STORE (model),
 
361
                         &iter,
 
362
                         G_MAXINT); /* position */
 
363
  set_values (model, object, &iter);
 
364
}
 
365
 
 
366
static void
 
367
remove_account (GoaPanelAccountsModel  *model,
 
368
                GoaObject              *object)
 
369
{
 
370
  GtkTreeIter iter;
 
371
  if (!find_iter_for_object (model, object, &iter))
 
372
    {
 
373
      goa_warning ("Error removing object %s - not in tree", g_dbus_object_get_object_path (G_DBUS_OBJECT (object)));
 
374
    }
 
375
  else
 
376
    {
 
377
      gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
 
378
    }
 
379
}
 
380
 
 
381
static void
 
382
update_account (GoaPanelAccountsModel  *model,
 
383
                GoaObject              *object)
 
384
{
 
385
  GtkTreeIter iter;
 
386
  if (!find_iter_for_object (model, object, &iter))
 
387
    {
 
388
      goa_warning ("Error updating object %s - not in tree", g_dbus_object_get_object_path (G_DBUS_OBJECT (object)));
 
389
    }
 
390
  else
 
391
    {
 
392
      set_values (model, object, &iter);
 
393
    }
 
394
}
 
395
 
 
396
static void
 
397
init_model (GoaPanelAccountsModel *model)
 
398
{
 
399
  GList *accounts;
 
400
  GList *l;
 
401
 
 
402
  accounts = goa_client_get_accounts (model->client);
 
403
  for (l = accounts; l != NULL; l = l->next)
 
404
    {
 
405
      GoaObject *object = GOA_OBJECT (l->data);
 
406
      add_account (model, object);
 
407
    }
 
408
  g_list_foreach (accounts, (GFunc) g_object_unref, NULL);
 
409
  g_list_free (accounts);
 
410
}
 
411
 
 
412
static void
 
413
on_account_added (GoaClient   *client,
 
414
                  GoaObject   *object,
 
415
                  gpointer     user_data)
 
416
{
 
417
  GoaPanelAccountsModel *model = GOA_PANEL_ACCOUNTS_MODEL (user_data);
 
418
  add_account (model, object);
 
419
}
 
420
 
 
421
static void
 
422
on_account_removed (GoaClient   *client,
 
423
                    GoaObject   *object,
 
424
                    gpointer     user_data)
 
425
{
 
426
  GoaPanelAccountsModel *model = GOA_PANEL_ACCOUNTS_MODEL (user_data);
 
427
  remove_account (model, object);
 
428
}
 
429
 
 
430
static void
 
431
on_account_changed (GoaClient   *client,
 
432
                    GoaObject   *object,
 
433
                    gpointer     user_data)
 
434
{
 
435
  GoaPanelAccountsModel *model = GOA_PANEL_ACCOUNTS_MODEL (user_data);
 
436
  update_account (model, object);
 
437
}