summaryrefslogtreecommitdiff
path: root/src/panel/goapanelaccountsmodel.c
blob: 7f2dfc70fc36a1ef9fbce3bacb6b2d6c8d19f59a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
 *
 * Copyright (C) 2008-2011 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: David Zeuthen <davidz@redhat.com>
 */

#include "config.h"

#include <glib/gi18n-lib.h>

#include <goabackend/goabackend.h>

#include "goapanelaccountsmodel.h"

struct _GoaPanelAccountsModel
{
  GtkListStore parent_instance;

  GoaClient *client;
};

typedef struct
{
  GtkListStoreClass parent_class;
} GoaPanelAccountsModelClass;

enum
{
  PROP_0,
  PROP_CLIENT,
};

static void init_model (GoaPanelAccountsModel *model);

static gboolean
find_iter_for_object (GoaPanelAccountsModel *model,
                      GoaObject             *object,
                      GtkTreeIter           *out_iter);

static void on_account_added (GoaClient   *client,
                              GoaObject   *object,
                              gpointer     user_data);

static void on_account_removed (GoaClient   *client,
                                GoaObject   *object,
                                gpointer     user_data);

static void on_account_changed (GoaClient   *client,
                                GoaObject   *object,
                                gpointer     user_data);

G_DEFINE_TYPE (GoaPanelAccountsModel, goa_panel_accounts_model, GTK_TYPE_LIST_STORE);

static void
goa_panel_accounts_model_finalize (GObject *object)
{
  GoaPanelAccountsModel *model = GOA_PANEL_ACCOUNTS_MODEL (object);

  g_signal_handlers_disconnect_by_func (model->client, G_CALLBACK (on_account_added), model);
  g_signal_handlers_disconnect_by_func (model->client, G_CALLBACK (on_account_removed), model);
  g_signal_handlers_disconnect_by_func (model->client, G_CALLBACK (on_account_changed), model);
  g_object_unref (model->client);

  G_OBJECT_CLASS (goa_panel_accounts_model_parent_class)->finalize (object);
}

static void
goa_panel_accounts_model_init (GoaPanelAccountsModel *model)
{
}

static void
goa_panel_accounts_model_get_property (GObject    *object,
                                       guint       prop_id,
                                       GValue     *value,
                                       GParamSpec *pspec)
{
  GoaPanelAccountsModel *model = GOA_PANEL_ACCOUNTS_MODEL (object);

  switch (prop_id)
    {
    case PROP_CLIENT:
      g_value_set_object (value, goa_panel_accounts_model_get_client (model));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
goa_panel_accounts_model_set_property (GObject      *object,
                                       guint         prop_id,
                                       const GValue *value,
                                       GParamSpec   *pspec)
{
  GoaPanelAccountsModel *model = GOA_PANEL_ACCOUNTS_MODEL (object);

  switch (prop_id)
    {
    case PROP_CLIENT:
      model->client = g_value_dup_object (value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

/* ---------------------------------------------------------------------------------------------------- */

static void
goa_panel_accounts_model_constructed (GObject *object)
{
  GoaPanelAccountsModel *model = GOA_PANEL_ACCOUNTS_MODEL (object);
  GType types[GOA_PANEL_ACCOUNTS_MODEL_N_COLUMNS];

  types[0] = G_TYPE_STRING;
  types[1] = GOA_TYPE_OBJECT;
  types[2] = G_TYPE_BOOLEAN;
  types[3] = G_TYPE_STRING;
  G_STATIC_ASSERT (4 == GOA_PANEL_ACCOUNTS_MODEL_N_COLUMNS);
  gtk_list_store_set_column_types (GTK_LIST_STORE (model),
                                   GOA_PANEL_ACCOUNTS_MODEL_N_COLUMNS,
                                   types);

  gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model),
                                        GOA_PANEL_ACCOUNTS_MODEL_COLUMN_SORT_KEY,
                                        GTK_SORT_ASCENDING);

  g_signal_connect (model->client,
                    "account-added",
                    G_CALLBACK (on_account_added),
                    model);
  g_signal_connect (model->client,
                    "account-removed",
                    G_CALLBACK (on_account_removed),
                    model);
  g_signal_connect (model->client,
                    "account-changed",
                    G_CALLBACK (on_account_changed),
                    model);

  init_model (model);

  if (G_OBJECT_CLASS (goa_panel_accounts_model_parent_class)->constructed != NULL)
    G_OBJECT_CLASS (goa_panel_accounts_model_parent_class)->constructed (object);
}

static void
goa_panel_accounts_model_class_init (GoaPanelAccountsModelClass *klass)
{
  GObjectClass *gobject_class;

  gobject_class = G_OBJECT_CLASS (klass);
  gobject_class->finalize     = goa_panel_accounts_model_finalize;
  gobject_class->constructed  = goa_panel_accounts_model_constructed;
  gobject_class->get_property = goa_panel_accounts_model_get_property;
  gobject_class->set_property = goa_panel_accounts_model_set_property;

  /**
   * GoaPanelAccountsModel:client:
   *
   * The #GoaClient used by the #GoaPanelAccountsModel instance.
   */
  g_object_class_install_property (gobject_class,
                                   PROP_CLIENT,
                                   g_param_spec_object ("client",
                                                        "Client",
                                                        "The client used by the tree model",
                                                        GOA_TYPE_CLIENT,
                                                        G_PARAM_READABLE |
                                                        G_PARAM_WRITABLE |
                                                        G_PARAM_CONSTRUCT_ONLY |
                                                        G_PARAM_STATIC_STRINGS));
}

/**
 * goa_panel_accounts_model_new:
 * @client: A #GoaClient.
 *
 * Creates a new #GoaPanelAccountsModel for viewing the accounts known
 * by @client.
 *
 * Returns: A #GoaPanelAccountsModel. Free with g_object_unref().
 */
GoaPanelAccountsModel *
goa_panel_accounts_model_new (GoaClient  *client)
{
  return GOA_PANEL_ACCOUNTS_MODEL (g_object_new (GOA_TYPE_PANEL_ACCOUNTS_MODEL,
                                                 "client", client,
                                                 NULL));
}

/**
 * goa_panel_accounts_model_get_client:
 * @model: A #GoaPanelAccountsModel.
 *
 * Gets the #GoaClient used by @model.
 *
 * Returns: (transfer none): A #GoaClient. Do not free, the object
 *   belongs to @model.
 */
GoaClient *
goa_panel_accounts_model_get_client (GoaPanelAccountsModel *model)
{
  g_return_val_if_fail (GOA_IS_PANEL_ACCOUNTS_MODEL (model), NULL);
  return model->client;
}

/**
 * goa_panel_accounts_model_get_iter_for_object:
 * @model: A #GoaPanelAccountsModel.
 * @object: A #GoaObject.
 * @iter: (out): Return location for #GtkTreeIter.
 *
 * Finds @model<!-- -->'s row for @object.
 *
 * Returns: %TRUE if @iter was set, %FALSE if @object wasn't found.
 */
gboolean
goa_panel_accounts_model_get_iter_for_object (GoaPanelAccountsModel  *model,
                                              GoaObject              *object,
                                              GtkTreeIter            *iter)
{
  g_return_val_if_fail (GOA_IS_PANEL_ACCOUNTS_MODEL (model), FALSE);
  g_return_val_if_fail (GOA_IS_OBJECT (object), FALSE);
  g_return_val_if_fail (iter != NULL, FALSE);
  return find_iter_for_object (model, object, iter);
}

/* ---------------------------------------------------------------------------------------------------- */

typedef struct
{
  GoaObject *object;
  GtkTreeIter iter;
  gboolean found;
} FindIterData;

static gboolean
find_iter_for_object_cb (GtkTreeModel  *model,
                         GtkTreePath   *path,
                         GtkTreeIter   *iter,
                         gpointer       user_data)
{
  FindIterData *data = user_data;
  GoaObject *iter_object;

  iter_object = NULL;

  gtk_tree_model_get (model,
                      iter,
                      GOA_PANEL_ACCOUNTS_MODEL_COLUMN_OBJECT, &iter_object,
                      -1);
  if (iter_object == NULL)
    goto out;

  if (iter_object == data->object)
    {
      data->iter = *iter;
      data->found = TRUE;
      goto out;
    }

 out:
  if (iter_object != NULL)
    g_object_unref (iter_object);
  return data->found;
}

static gboolean
find_iter_for_object (GoaPanelAccountsModel *model,
                      GoaObject             *object,
                      GtkTreeIter           *out_iter)
{
  FindIterData data;
  memset (&data, 0, sizeof (data));
  data.object = object;
  data.found = FALSE;
  gtk_tree_model_foreach (GTK_TREE_MODEL (model),
                          find_iter_for_object_cb,
                          &data);
  if (data.found)
    {
      if (out_iter != NULL)
        *out_iter = data.iter;
    }
  return data.found;
}

/* ---------------------------------------------------------------------------------------------------- */

static void
set_values (GoaPanelAccountsModel  *model,
            GoaObject              *object,
            GtkTreeIter            *iter)
{
  GoaAccount *account;
  GoaProvider *provider;
  const gchar *type;
  gchar *provider_name;
  gchar *markup;

  account = goa_object_peek_account (object);

  type = goa_account_get_provider_type (account);
  provider = goa_provider_get_for_provider_type (type);
  if (provider != NULL)
    provider_name = g_strdup (goa_provider_get_name (provider));
  else
    provider_name = g_strdup_printf (_("Unknown Provider (%s)"), type);

  markup = g_strdup_printf ("<b>%s</b>\n"
                            "<small><span foreground=\"#555555\">%s</span></small>",
                            provider_name,
                            goa_account_get_presentation_identity (account));

  gtk_list_store_set (GTK_LIST_STORE (model),
                      iter,
                      GOA_PANEL_ACCOUNTS_MODEL_COLUMN_SORT_KEY, goa_account_get_id (account),
                      GOA_PANEL_ACCOUNTS_MODEL_COLUMN_OBJECT, object,
                      GOA_PANEL_ACCOUNTS_MODEL_COLUMN_ATTENTION_NEEDED, goa_account_get_attention_needed (account),
                      GOA_PANEL_ACCOUNTS_MODEL_COLUMN_MARKUP, markup,
                      -1);

  g_free (markup);
  g_free (provider_name);
  g_clear_object (&provider);
}

static void
add_account (GoaPanelAccountsModel  *model,
             GoaObject              *object)
{
  GtkTreeIter iter;
  gtk_list_store_insert (GTK_LIST_STORE (model),
                         &iter,
                         G_MAXINT); /* position */
  set_values (model, object, &iter);
}

static void
remove_account (GoaPanelAccountsModel  *model,
                GoaObject              *object)
{
  GtkTreeIter iter;
  if (!find_iter_for_object (model, object, &iter))
    {
      g_warning ("Error removing object %s - not in tree", g_dbus_object_get_object_path (G_DBUS_OBJECT (object)));
    }
  else
    {
      gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
    }
}

static void
update_account (GoaPanelAccountsModel  *model,
                GoaObject              *object)
{
  GtkTreeIter iter;
  if (!find_iter_for_object (model, object, &iter))
    {
      g_warning ("Error updating object %s - not in tree", g_dbus_object_get_object_path (G_DBUS_OBJECT (object)));
    }
  else
    {
      set_values (model, object, &iter);
    }
}

static void
init_model (GoaPanelAccountsModel *model)
{
  GList *accounts;
  GList *l;

  accounts = goa_client_get_accounts (model->client);
  for (l = accounts; l != NULL; l = l->next)
    {
      GoaObject *object = GOA_OBJECT (l->data);
      add_account (model, object);
    }
  g_list_foreach (accounts, (GFunc) g_object_unref, NULL);
  g_list_free (accounts);
}

static void
on_account_added (GoaClient   *client,
                  GoaObject   *object,
                  gpointer     user_data)
{
  GoaPanelAccountsModel *model = GOA_PANEL_ACCOUNTS_MODEL (user_data);
  add_account (model, object);
}

static void
on_account_removed (GoaClient   *client,
                    GoaObject   *object,
                    gpointer     user_data)
{
  GoaPanelAccountsModel *model = GOA_PANEL_ACCOUNTS_MODEL (user_data);
  remove_account (model, object);
}

static void
on_account_changed (GoaClient   *client,
                    GoaObject   *object,
                    gpointer     user_data)
{
  GoaPanelAccountsModel *model = GOA_PANEL_ACCOUNTS_MODEL (user_data);
  update_account (model, object);
}