da7af1552736480a7ad27e1091d0f7a8249586c6
[modest] / src / widgets / modest-account-view.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <glib/gi18n.h>
31 #include "modest-account-view.h"
32
33 #include <modest-account-mgr.h>
34 #include <modest-account-mgr-helpers.h>
35 #include <modest-text-utils.h>
36 #include <modest-runtime.h>
37
38 #include <gtk/gtkcellrenderertoggle.h>
39 #include <gtk/gtkcellrenderertext.h>
40 #include <gtk/gtktreeselection.h>
41 #include <gtk/gtkliststore.h>
42 #include <string.h> /* For strcmp(). */
43
44 /* 'private'/'protected' functions */
45 static void modest_account_view_class_init    (ModestAccountViewClass *klass);
46 static void modest_account_view_init          (ModestAccountView *obj);
47 static void modest_account_view_finalize      (GObject *obj);
48
49 static void modest_account_view_select_account (ModestAccountView *account_view, 
50         const gchar* account_name);
51
52 typedef enum {
53         MODEST_ACCOUNT_VIEW_NAME_COLUMN,
54         MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN,
55         MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN,
56         MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,
57         MODEST_ACCOUNT_VIEW_PROTO_COLUMN,
58         MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN,
59
60         MODEST_ACCOUNT_VIEW_COLUMN_NUM
61 } AccountViewColumns;
62
63
64 /* list my signals */
65 enum {
66         /* MY_SIGNAL_1, */
67         /* MY_SIGNAL_2, */
68         LAST_SIGNAL
69 };
70
71 typedef struct _ModestAccountViewPrivate ModestAccountViewPrivate;
72 struct _ModestAccountViewPrivate {
73         ModestAccountMgr *account_mgr;
74         gulong sig1, sig2, sig3;
75         
76         /* When this is TRUE, we ignore configuration key changes.
77          * This is useful when making many changes. */
78         gboolean block_conf_updates;
79         
80 };
81 #define MODEST_ACCOUNT_VIEW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
82                                                  MODEST_TYPE_ACCOUNT_VIEW, \
83                                                  ModestAccountViewPrivate))
84 /* globals */
85 static GtkTreeViewClass *parent_class = NULL;
86
87 /* uncomment the following if you have defined any signals */
88 /* static guint signals[LAST_SIGNAL] = {0}; */
89
90 GType
91 modest_account_view_get_type (void)
92 {
93         static GType my_type = 0;
94         if (!my_type) {
95                 static const GTypeInfo my_info = {
96                         sizeof(ModestAccountViewClass),
97                         NULL,           /* base init */
98                         NULL,           /* base finalize */
99                         (GClassInitFunc) modest_account_view_class_init,
100                         NULL,           /* class finalize */
101                         NULL,           /* class data */
102                         sizeof(ModestAccountView),
103                         1,              /* n_preallocs */
104                         (GInstanceInitFunc) modest_account_view_init,
105                         NULL
106                 };
107                 my_type = g_type_register_static (GTK_TYPE_TREE_VIEW,
108                                                   "ModestAccountView",
109                                                   &my_info, 0);
110         }
111         return my_type;
112 }
113
114 static void
115 modest_account_view_class_init (ModestAccountViewClass *klass)
116 {
117         GObjectClass *gobject_class;
118         gobject_class = (GObjectClass*) klass;
119
120         parent_class            = g_type_class_peek_parent (klass);
121         gobject_class->finalize = modest_account_view_finalize;
122
123         g_type_class_add_private (gobject_class, sizeof(ModestAccountViewPrivate));
124 }
125
126 static void
127 modest_account_view_init (ModestAccountView *obj)
128 {
129         ModestAccountViewPrivate *priv;
130
131         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
132         
133         priv->account_mgr = NULL; 
134         priv->sig1 = 0;
135         priv->sig2 = 0;
136 }
137
138 static void
139 modest_account_view_finalize (GObject *obj)
140 {
141         ModestAccountViewPrivate *priv;
142
143         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
144
145         if (priv->account_mgr) {
146                 if (priv->sig1)
147                         g_signal_handler_disconnect (priv->account_mgr, priv->sig1);
148
149                 if (priv->sig2)
150                         g_signal_handler_disconnect (priv->account_mgr, priv->sig2);
151
152                 if (priv->sig3)
153                         g_signal_handler_disconnect (priv->account_mgr, priv->sig3);
154                 
155                 g_object_unref (G_OBJECT(priv->account_mgr));
156                 priv->account_mgr = NULL; 
157         }
158         
159         G_OBJECT_CLASS(parent_class)->finalize (obj);
160 }
161
162 /* Get the string for the last updated time. Result must be g_freed */
163 static gchar*
164 get_last_updated_string(ModestAccountMgr* account_mgr, ModestAccountData *account_data)
165 {
166         /* FIXME: let's assume that 'last update' applies to the store account... */
167         gchar* last_updated_string;
168         time_t last_updated = account_data->store_account->last_updated;
169         if (!modest_account_mgr_account_is_busy(account_mgr, account_data->account_name))
170         {
171                 if (last_updated > 0) 
172                                 last_updated_string = modest_text_utils_get_display_date(last_updated);
173                 else
174                                 last_updated_string = g_strdup (_("mcen_va_never"));
175         }
176         else
177         {
178                 /* FIXME: There should be a logical name in the UI specs */
179                 last_updated_string = g_strdup(_("Refreshing..."));
180         }
181         return last_updated_string;
182 }
183
184 static void
185 update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
186 {
187         GSList *account_names, *cursor;
188         GtkListStore *model;
189                 
190         model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(view)));
191         
192         /* Get the ID of the currently-selected account, 
193          * so we can select it again after rebuilding the list.
194          * Note that the name doesn't change even when the display name changes.
195          */
196         gchar *selected_name = modest_account_view_get_selected_account (view);
197
198         gtk_list_store_clear (model);
199
200         /* Note: We do not show disabled accounts.
201          * Of course, this means that there is no UI to enable or disable 
202          * accounts. That is OK for maemo where no such feature or UI is 
203          * specified, so the "enabled" property is used internally to avoid 
204          * showing unfinished accounts. If a user-visible "enabled" is 
205          * needed in the future, we must use a second property for the 
206          * current use instead */
207         cursor = account_names = modest_account_mgr_account_names (account_mgr,
208                 TRUE /* only enabled accounts. */);
209         
210         if (!account_names)
211                 g_warning ("debug: modest_account_mgr_account_names() returned  NULL\n");
212
213         while (cursor) {
214                 gchar *account_name;
215                 ModestAccountData *account_data;
216                 
217                 account_name = (gchar*)cursor->data;
218                 
219                 account_data = modest_account_mgr_get_account_data (account_mgr, account_name);
220                 if (!account_data) {
221                         g_printerr ("modest: failed to get account data for %s\n", account_name);
222                         continue;
223                 }
224
225                 /* don't display accounts without stores */
226                 if (account_data->store_account) {
227
228                         GtkTreeIter iter;
229                         
230                         gchar *last_updated_string = get_last_updated_string(account_mgr, account_data);
231                         
232                         if (account_data->is_enabled) {
233                                 gtk_list_store_insert_with_values (
234                                         model, &iter, 0,
235                                         MODEST_ACCOUNT_VIEW_NAME_COLUMN,          account_name,
236                                         MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN,  account_data->display_name,
237                                         MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN,    account_data->is_enabled,
238                                         MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,    account_data->is_default,
239
240                                         MODEST_ACCOUNT_VIEW_PROTO_COLUMN,
241                                         modest_protocol_info_get_transport_store_protocol_name (account_data->store_account->proto),
242         
243                                         MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN,  last_updated_string,
244                                         -1);
245                         }
246                         g_free (last_updated_string);
247                 }
248
249                 modest_account_mgr_free_account_data (account_mgr, account_data);
250                 cursor = cursor->next;
251         }
252
253         modest_account_mgr_free_account_names (account_names);
254         account_names = NULL;
255         
256         /* Try to re-select the same account: */
257         if (selected_name) {
258                 modest_account_view_select_account (view, selected_name);
259                 g_free (selected_name);
260         }
261 }
262
263
264 static void
265 on_account_changed (ModestAccountMgr *account_mgr,
266                     const gchar* account, GSList *keys,
267                     gboolean server_account, ModestAccountView *self)
268 {       
269         /* Never update the view in response to gconf changes.
270          * Always do it explicitly instead.
271          * This is because we have no way to avoid 10 updates when changing 
272          * 10 items, and this blocks the UI.
273          *
274          * But this block/unblock API might be useful on platforms where the 
275          * notification does not happen so long after the key was set.
276          * (We have no way to know when the last key was set, to do a final update)..
277          */
278          
279         update_account_view (account_mgr, self);
280 }
281
282 static void
283 on_account_busy_changed(ModestAccountMgr *account_mgr, const gchar *account_name,
284                                                                                                 gboolean busy, ModestAccountView *self)
285 {
286         GtkListStore *model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(self)));
287         GtkTreeIter iter;
288         g_message(__FUNCTION__);
289         if (!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(model), &iter))
290                 return;
291         do
292         {
293                 gchar* cur_name;
294                 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, MODEST_ACCOUNT_VIEW_NAME_COLUMN, 
295                                                                                          &cur_name, -1);
296                 if (g_str_equal(cur_name, account_name))
297                 {
298                         ModestAccountData* account_data = 
299                                 modest_account_mgr_get_account_data (account_mgr, account_name);
300                         if (!account_data)
301                                 return;
302                         gchar* last_updated_string = get_last_updated_string(account_mgr, account_data);
303                         gtk_list_store_set(model, &iter, 
304                                            MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, last_updated_string,
305                                            -1);
306                         g_free (last_updated_string);
307                         modest_account_mgr_free_account_data (account_mgr, account_data);
308                         return;
309                 }
310         }
311         while (gtk_tree_model_iter_next(GTK_TREE_MODEL(model), &iter));
312 }
313
314 static void
315 on_account_removed (ModestAccountMgr *account_mgr,
316                     const gchar* account, gboolean server_account,
317                     ModestAccountView *self)
318 {                
319         update_account_view (account_mgr, self);
320 }
321
322
323 /* currently unused */
324 #if 0 
325 static void
326 on_account_enable_toggled (GtkCellRendererToggle *cell_renderer, gchar *path,
327                            ModestAccountView *self)
328 {
329         GtkTreeIter iter;
330         ModestAccountViewPrivate *priv;
331         GtkTreeModel *model;
332         gchar *account_name;
333         gboolean enabled;
334         
335         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
336         model = gtk_tree_view_get_model (GTK_TREE_VIEW(self));
337         
338         if (!gtk_tree_model_get_iter_from_string (model, &iter, path)) {
339                 g_printerr ("modest: cannot find iterator\n");
340                 return;
341         }
342         gtk_tree_model_get (model, &iter, MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN, &enabled,
343                             MODEST_ACCOUNT_VIEW_NAME_COLUMN, &account_name,
344                             -1);
345         
346         /* toggle enabled / disabled */
347         modest_account_mgr_set_enabled (priv->account_mgr, account_name, !enabled);
348         g_free (account_name);
349 }
350 #endif
351
352 static gboolean
353 find_default_account(ModestAccountView *self, GtkTreeIter *iter)
354 {
355         GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
356         gboolean result;
357         for (result = gtk_tree_model_get_iter_first(model, iter);
358              result == TRUE; result = gtk_tree_model_iter_next(model, iter))
359         {
360                 gboolean is_default;
361                 gtk_tree_model_get (model, iter, MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, &is_default, -1);
362                 if(is_default)
363                         return TRUE;
364         }
365
366         return FALSE;
367 }
368
369 static void
370 on_account_default_toggled (GtkCellRendererToggle *cell_renderer, gchar *path,
371                            ModestAccountView *self)
372 {
373         gboolean is_default = gtk_cell_renderer_toggle_get_active (cell_renderer);
374         if (is_default) {
375                 /* Do not allow an account to be marked non-default.
376                  * Only allow this to be changed by setting another account to default: */
377                 gtk_cell_renderer_toggle_set_active (cell_renderer, TRUE);
378                 return;
379         }
380
381         ModestAccountViewPrivate *priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
382         GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW(self));
383         
384         GtkTreeIter iter;
385         if (!gtk_tree_model_get_iter_from_string (model, &iter, path)) {
386                 g_printerr ("modest: cannot find iterator\n");
387                 return;
388         }
389         
390         gchar *account_name = NULL;
391         gtk_tree_model_get (model, &iter, MODEST_ACCOUNT_VIEW_NAME_COLUMN, &account_name,
392                             -1);
393         
394         /* Set this previously-non-default account as the default: */
395         if (modest_account_mgr_set_default_account (priv->account_mgr, account_name))
396         {
397                 /* Explicitely set default column because we are ignoring gconf changes */
398                 GtkTreeIter old_default_iter;
399                 if (find_default_account (self, &old_default_iter)) {
400                         gtk_list_store_set (GTK_LIST_STORE (model), &old_default_iter,
401                                             MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, FALSE, -1);
402                 } else {
403                         g_warning ("%s: Did not find old default account in view", __FUNCTION__);
404                 }
405
406                 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
407                                     MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, TRUE, -1);
408         }
409
410         g_free (account_name);
411 }
412
413 void
414 bold_if_default_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
415                             GtkTreeModel *tree_model,  GtkTreeIter *iter,  gpointer user_data)
416 {
417         gboolean is_default;
418         gtk_tree_model_get (tree_model, iter, MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,
419                             &is_default, -1);
420         g_object_set (G_OBJECT(renderer),
421                       "weight", is_default ? 800: 400,
422                       NULL);
423 }
424
425 static void
426 init_view (ModestAccountView *self)
427 {
428         ModestAccountViewPrivate *priv;
429         GtkCellRenderer *toggle_renderer, *text_renderer;
430         GtkListStore *model;
431         GtkTreeViewColumn *column;
432         
433         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
434                 
435         model = gtk_list_store_new (6,
436                                     G_TYPE_STRING,  /* account name */
437                                     G_TYPE_STRING,  /* account display name */
438                                     G_TYPE_BOOLEAN, /* is-enabled */
439                                     G_TYPE_BOOLEAN, /* is-default */
440                                     G_TYPE_STRING,  /* account proto (pop, imap,...) */
441                                     G_TYPE_STRING   /* last updated (time_t) */
442                 ); 
443                 
444         gtk_tree_sortable_set_sort_column_id (
445                 GTK_TREE_SORTABLE (model), MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN, 
446                 GTK_SORT_ASCENDING);
447
448         gtk_tree_view_set_model (GTK_TREE_VIEW(self), GTK_TREE_MODEL(model));
449         g_object_unref (G_OBJECT (model));
450
451         toggle_renderer = gtk_cell_renderer_toggle_new ();
452         text_renderer = gtk_cell_renderer_text_new ();
453
454         /* the is_default column */
455         g_object_set (G_OBJECT(toggle_renderer), "activatable", TRUE, "radio", TRUE, NULL);
456         gtk_tree_view_append_column (GTK_TREE_VIEW(self),
457                                      gtk_tree_view_column_new_with_attributes (
458                                              _("mcen_ti_default"), toggle_renderer,
459                                              "active", MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, NULL));
460                                         
461         /* Disable the Maemo GtkTreeView::allow-checkbox-mode Maemo modification, 
462          * which causes the model column to be updated automatically when the row is clicked.
463          * Making this the default in Maemo's GTK+ is obviously a bug:
464          * https://maemo.org/bugzilla/show_bug.cgi?id=146
465          *
466          * djcb: indeed, they have been removed for post-bora, i added the ifdefs...
467          */
468 #ifdef MODEST_HILDON_VERSION_0  
469         g_object_set(G_OBJECT(self), "allow-checkbox-mode", FALSE, NULL);
470         g_object_set(G_OBJECT(toggle_renderer), "checkbox-mode", FALSE, NULL);
471 #endif /*MODEST_HILDON_VERSION_0 */
472         g_signal_connect (G_OBJECT(toggle_renderer), "toggled", G_CALLBACK(on_account_default_toggled),
473                           self);
474         
475         /* account name */
476         column =  gtk_tree_view_column_new_with_attributes (_("mcen_ti_account"), text_renderer, "text",
477                                                             MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN, NULL);
478         gtk_tree_view_append_column (GTK_TREE_VIEW(self), column);
479         gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_cell_data,
480                                                 NULL, NULL);
481
482         /* last update for this account */
483         column =  gtk_tree_view_column_new_with_attributes (_("mcen_ti_lastupdated"), text_renderer,"text",
484                                                             MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, NULL);
485         gtk_tree_view_append_column (GTK_TREE_VIEW(self),column);
486         gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_cell_data,
487                                                 NULL, NULL);
488                         
489         /* Show the column headers,
490          * which does not seem to be the default on Maemo.
491          */                     
492         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(self), TRUE);
493
494         priv->sig1 = g_signal_connect (G_OBJECT(priv->account_mgr),"account_removed",
495                                        G_CALLBACK(on_account_removed), self);
496         priv->sig2 = g_signal_connect (G_OBJECT(priv->account_mgr), "account_changed",
497                                        G_CALLBACK(on_account_changed), self);
498         priv->sig3 = g_signal_connect (G_OBJECT(priv->account_mgr), "account_busy_changed",
499                                                          G_CALLBACK(on_account_busy_changed), self);
500 }
501
502
503
504 ModestAccountView*
505 modest_account_view_new (ModestAccountMgr *account_mgr)
506 {
507         GObject *obj;
508         ModestAccountViewPrivate *priv;
509         
510         g_return_val_if_fail (account_mgr, NULL);
511         
512         obj  = g_object_new(MODEST_TYPE_ACCOUNT_VIEW, NULL);
513         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
514         
515         g_object_ref (G_OBJECT (account_mgr));
516         priv->account_mgr = account_mgr;
517
518         init_view (MODEST_ACCOUNT_VIEW (obj));
519         update_account_view (account_mgr, MODEST_ACCOUNT_VIEW (obj));
520         
521         return MODEST_ACCOUNT_VIEW (obj);
522 }
523
524 gchar *
525 modest_account_view_get_selected_account (ModestAccountView *self)
526 {
527         gchar *account_name = NULL;
528         GtkTreeSelection *sel;
529         GtkTreeModel *model;
530         GtkTreeIter iter;
531
532         g_return_val_if_fail (MODEST_IS_ACCOUNT_VIEW (self), NULL);
533         
534         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
535         if (gtk_tree_selection_get_selected (sel, &model, &iter)) {
536                 gtk_tree_model_get (model, &iter, 
537                                     MODEST_ACCOUNT_VIEW_NAME_COLUMN, 
538                                     &account_name, -1);
539         }
540
541         return account_name;
542 }
543
544 /* This allows us to pass more than one piece of data to the signal handler,
545  * and get a result: */
546 typedef struct 
547 {
548                 ModestAccountView* self;
549                 const gchar *account_name;
550 } ForEachData;
551
552 static gboolean
553 on_model_foreach_select_account(GtkTreeModel *model, 
554         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
555 {
556         ForEachData *state = (ForEachData*)(user_data);
557         
558         /* Select the item if it has the matching account name: */
559         gchar *this_account_name = NULL;
560         gtk_tree_model_get (model, iter, 
561                 MODEST_ACCOUNT_VIEW_NAME_COLUMN, &this_account_name, 
562                 -1); 
563         if(this_account_name && state->account_name 
564                 && (strcmp (this_account_name, state->account_name) == 0)) {
565                 
566                 GtkTreeSelection *selection = 
567                         gtk_tree_view_get_selection (GTK_TREE_VIEW (state->self));
568                 gtk_tree_selection_select_iter (selection, iter);
569                 
570                 return TRUE; /* Stop walking the tree. */
571         }
572         
573         return FALSE; /* Keep walking the tree. */
574 }
575
576 static void modest_account_view_select_account (ModestAccountView *account_view, 
577         const gchar* account_name)
578 {       
579         /* Create a state instance so we can send two items of data to the signal handler: */
580         ForEachData *state = g_new0 (ForEachData, 1);
581         state->self = account_view;
582         state->account_name = account_name;
583         
584         GtkTreeModel *model = gtk_tree_view_get_model (
585                 GTK_TREE_VIEW (account_view));
586         gtk_tree_model_foreach (model, 
587                 on_model_foreach_select_account, state);
588                 
589         g_free (state);
590 }
591