20a9866dcc53452bd62b1762f352c55ef7ca0cc2
[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-tny-account.h>
36 #include <modest-text-utils.h>
37 #include <modest-runtime.h>
38 #include <modest-signal-mgr.h>
39
40 #include <gtk/gtkcellrenderertoggle.h>
41 #include <gtk/gtkcellrenderertext.h>
42 #include <gtk/gtktreeselection.h>
43 #include <gtk/gtkliststore.h>
44 #include <string.h> /* For strcmp(). */
45
46 /* 'private'/'protected' functions */
47 static void modest_account_view_class_init    (ModestAccountViewClass *klass);
48 static void modest_account_view_init          (ModestAccountView *obj);
49 static void modest_account_view_finalize      (GObject *obj);
50
51 static void modest_account_view_select_account (ModestAccountView *account_view, 
52                                                 const gchar* account_name);
53
54 static void on_default_account_changed         (ModestAccountMgr *mgr,
55                                                 gpointer user_data);
56
57 static void on_display_name_changed            (ModestAccountMgr *self, 
58                                                 const gchar *account,
59                                                 gpointer user_data);
60
61 typedef enum {
62         MODEST_ACCOUNT_VIEW_NAME_COLUMN,
63         MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN,
64         MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN,
65         MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,
66         MODEST_ACCOUNT_VIEW_PROTO_COLUMN,
67         MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN,
68
69         MODEST_ACCOUNT_VIEW_COLUMN_NUM
70 } AccountViewColumns;
71
72 typedef struct _ModestAccountViewPrivate ModestAccountViewPrivate;
73 struct _ModestAccountViewPrivate {
74         ModestAccountMgr *account_mgr;
75
76         /* Signal handlers */
77         GSList *sig_handlers;
78 };
79 #define MODEST_ACCOUNT_VIEW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
80                                                  MODEST_TYPE_ACCOUNT_VIEW, \
81                                                  ModestAccountViewPrivate))
82 /* globals */
83 static GtkTreeViewClass *parent_class = NULL;
84
85 GType
86 modest_account_view_get_type (void)
87 {
88         static GType my_type = 0;
89         if (!my_type) {
90                 static const GTypeInfo my_info = {
91                         sizeof(ModestAccountViewClass),
92                         NULL,           /* base init */
93                         NULL,           /* base finalize */
94                         (GClassInitFunc) modest_account_view_class_init,
95                         NULL,           /* class finalize */
96                         NULL,           /* class data */
97                         sizeof(ModestAccountView),
98                         1,              /* n_preallocs */
99                         (GInstanceInitFunc) modest_account_view_init,
100                         NULL
101                 };
102                 my_type = g_type_register_static (GTK_TYPE_TREE_VIEW,
103                                                   "ModestAccountView",
104                                                   &my_info, 0);
105         }
106         return my_type;
107 }
108
109 static void
110 modest_account_view_class_init (ModestAccountViewClass *klass)
111 {
112         GObjectClass *gobject_class;
113         gobject_class = (GObjectClass*) klass;
114
115         parent_class            = g_type_class_peek_parent (klass);
116         gobject_class->finalize = modest_account_view_finalize;
117
118         g_type_class_add_private (gobject_class, sizeof(ModestAccountViewPrivate));
119 }
120
121 static void
122 modest_account_view_init (ModestAccountView *obj)
123 {
124         ModestAccountViewPrivate *priv;
125         
126         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
127         
128         priv->sig_handlers = NULL;
129 }
130
131 static void
132 modest_account_view_finalize (GObject *obj)
133 {
134         ModestAccountViewPrivate *priv;
135
136         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
137
138         /* Disconnect signals */
139         modest_signal_mgr_disconnect_all_and_destroy (priv->sig_handlers);
140
141         if (priv->account_mgr) {        
142                 g_object_unref (G_OBJECT(priv->account_mgr));
143                 priv->account_mgr = NULL; 
144         }
145         
146         G_OBJECT_CLASS(parent_class)->finalize (obj);
147 }
148
149 /* Get the string for the last updated time. Result must be g_freed */
150 static gchar*
151 get_last_updated_string(ModestAccountMgr* account_mgr, ModestAccountData *account_data)
152 {
153         /* FIXME: let's assume that 'last update' applies to the store account... */
154         gchar* last_updated_string;
155         time_t last_updated = account_data->store_account->last_updated;
156         if (!modest_account_mgr_account_is_busy(account_mgr, account_data->account_name)) {
157                 if (last_updated > 0) 
158                         last_updated_string = modest_text_utils_get_display_date(last_updated);
159                 else
160                         last_updated_string = g_strdup (_("mcen_va_never"));
161         } else  {
162                 /* FIXME: There should be a logical name in the UI specs */
163                 last_updated_string = g_strdup(_("mcen_va_refreshing"));
164         }
165         return last_updated_string;
166 }
167
168 static void
169 update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
170 {
171         GSList *account_names, *cursor;
172         GtkListStore *model;
173                 
174         model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(view)));
175         
176         /* Get the ID of the currently-selected account,
177          * so we can select it again after rebuilding the list.
178          * Note that the name doesn't change even when the display name changes.
179          */
180         gchar *selected_name = modest_account_view_get_selected_account (view);
181
182         gtk_list_store_clear (model);
183
184         /* Note: We do not show disabled accounts.
185          * Of course, this means that there is no UI to enable or disable
186          * accounts. That is OK for maemo where no such feature or UI is
187          * specified, so the "enabled" property is used internally to avoid
188          * showing unfinished accounts. If a user-visible "enabled" is
189          * needed in the future, we must use a second property for the
190          * current use instead */
191         cursor = account_names = modest_account_mgr_account_names (account_mgr,
192                 TRUE /* only enabled accounts. */);
193
194         while (cursor) {
195                 gchar *account_name;
196                 ModestAccountData *account_data;
197                 
198                 account_name = (gchar*)cursor->data;
199                 
200                 account_data = modest_account_mgr_get_account_data (account_mgr, account_name);
201                 if (!account_data) {
202                         g_printerr ("modest: failed to get account data for %s\n", account_name);
203                         continue;
204                 }
205
206                 /* don't display accounts without stores */
207                 if (account_data->store_account) {
208
209                         GtkTreeIter iter;
210                         
211                         gchar *last_updated_string = get_last_updated_string(account_mgr, account_data);
212                         
213                         if (account_data->is_enabled) {
214                                 const gchar *proto_name;
215
216                                 proto_name = modest_protocol_info_get_transport_store_protocol_name (account_data->store_account->proto);
217                                 gtk_list_store_insert_with_values (
218                                         model, &iter, 0,
219                                         MODEST_ACCOUNT_VIEW_NAME_COLUMN, account_name,
220                                         MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN, account_data->display_name,
221                                         MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN, account_data->is_enabled,
222                                         MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, account_data->is_default,
223                                         MODEST_ACCOUNT_VIEW_PROTO_COLUMN, proto_name,
224                                         MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN,  last_updated_string,
225                                         -1);
226                         }
227                         g_free (last_updated_string);
228                 }
229
230                 modest_account_mgr_free_account_data (account_mgr, account_data);
231                 cursor = cursor->next;
232         }
233
234         modest_account_mgr_free_account_names (account_names);
235         account_names = NULL;
236         
237         /* Try to re-select the same account: */
238         if (selected_name) {
239                 modest_account_view_select_account (view, selected_name);
240                 g_free (selected_name);
241         }
242 }
243
244 static void
245 on_account_busy_changed(ModestAccountMgr *account_mgr, 
246                         const gchar *account_name,
247                         gboolean busy, 
248                         ModestAccountView *self)
249 {
250         GtkListStore *model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(self)));
251         GtkTreeIter iter;
252         gboolean found = FALSE;
253
254         if (!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(model), &iter))
255                 return;
256
257         do {
258                 gchar* cur_name;
259                 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, 
260                                    MODEST_ACCOUNT_VIEW_NAME_COLUMN, 
261                                    &cur_name, -1);
262
263                 if (g_str_equal(cur_name, account_name)) {
264                         ModestAccountData* account_data = 
265                                 modest_account_mgr_get_account_data (account_mgr, account_name);
266                         if (!account_data) {
267                                 g_free (cur_name);
268                                 return;
269                         }
270                         gchar* last_updated_string = get_last_updated_string(account_mgr, account_data);
271                         gtk_list_store_set(model, &iter, 
272                                            MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, last_updated_string,
273                                            -1);
274                         g_free (last_updated_string);
275                         modest_account_mgr_free_account_data (account_mgr, account_data);
276                         found = TRUE;
277                 }
278                 g_free (cur_name);
279
280         } while (!found && gtk_tree_model_iter_next(GTK_TREE_MODEL(model), &iter));
281 }
282
283 static void
284 on_account_inserted (TnyAccountStore *account_store, 
285                      TnyAccount *account,
286                      gpointer user_data)
287 {
288         ModestAccountView *self;
289         ModestAccountViewPrivate *priv;
290
291         g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (user_data));
292
293         self = MODEST_ACCOUNT_VIEW (user_data);
294         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE (self);
295
296         /* Do not refresh the view with transport accounts */
297         if (TNY_IS_STORE_ACCOUNT (account))
298                 update_account_view (priv->account_mgr, self);
299 }
300
301 static void
302 on_account_removed (TnyAccountStore *account_store, 
303                     TnyAccount *account,
304                     gpointer user_data)
305 {
306         ModestAccountView *self;
307         ModestAccountViewPrivate *priv;
308
309         g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (user_data));
310
311         self = MODEST_ACCOUNT_VIEW (user_data);
312         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE (self);
313
314         update_account_view (priv->account_mgr, self);
315 }
316
317
318 static void
319 on_account_changed (TnyAccountStore *account_store, 
320                     TnyAccount *account,
321                     gpointer user_data)
322 {
323         ModestAccountView *self = NULL;
324         ModestAccountViewPrivate *priv = NULL;
325
326         g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (user_data));
327         g_return_if_fail (account);
328         g_return_if_fail (TNY_IS_ACCOUNT (account));
329
330         self = MODEST_ACCOUNT_VIEW (user_data);
331         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE (self);
332         
333         /* Update account view */
334 /*      update_account_view (priv->account_mgr, self); */
335 }
336
337 static void
338 on_account_default_toggled (GtkCellRendererToggle *cell_renderer, 
339                             gchar *path,
340                             ModestAccountView *self)
341 {
342         ModestAccountViewPrivate *priv;
343         GtkTreeModel *model;
344         GtkTreeIter iter;
345         gchar *account_name = NULL;
346
347         g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (self));
348
349         /* If it's active then do nothing, no need to reenable it as
350            default account */
351         if (gtk_cell_renderer_toggle_get_active (cell_renderer))
352                 return;
353
354         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
355         model = gtk_tree_view_get_model (GTK_TREE_VIEW(self));  
356         gtk_tree_model_get_iter_from_string (model, &iter, path);
357         
358         gtk_tree_model_get (model, &iter, 
359                             MODEST_ACCOUNT_VIEW_NAME_COLUMN, 
360                             &account_name, -1);
361
362         /* Set this previously-non-default account as the
363            default. We're not updating here the value of the
364            DEFAULT_COLUMN because we'll do it in the
365            "default_account_changed" signal handler. We do it like
366            this because that way the signal handler is useful also
367            when we're inserting a new account and there is no other
368            one defined, in that case the change of account is provoked
369            by the account mgr and not by a signal toggle.*/
370         modest_account_mgr_set_default_account (priv->account_mgr, account_name);
371
372         g_free (account_name);
373 }
374
375 void
376 bold_if_default_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
377                             GtkTreeModel *tree_model,  GtkTreeIter *iter,  gpointer user_data)
378 {
379         gboolean is_default;
380         gtk_tree_model_get (tree_model, iter, MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,
381                             &is_default, -1);
382         g_object_set (G_OBJECT(renderer),
383                       "weight", is_default ? 800: 400,
384                       NULL);
385 }
386
387 static void
388 init_view (ModestAccountView *self)
389 {
390         ModestAccountViewPrivate *priv;
391         GtkCellRenderer *toggle_renderer, *text_renderer;
392         GtkListStore *model;
393         GtkTreeViewColumn *column;
394         
395         g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (self));
396         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
397                 
398         model = gtk_list_store_new (6,
399                                     G_TYPE_STRING,  /* account name */
400                                     G_TYPE_STRING,  /* account display name */
401                                     G_TYPE_BOOLEAN, /* is-enabled */
402                                     G_TYPE_BOOLEAN, /* is-default */
403                                     G_TYPE_STRING,  /* account proto (pop, imap,...) */
404                                     G_TYPE_STRING   /* last updated (time_t) */
405                 ); 
406                 
407         gtk_tree_sortable_set_sort_column_id (
408                 GTK_TREE_SORTABLE (model), MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN, 
409                 GTK_SORT_ASCENDING);
410
411         gtk_tree_view_set_model (GTK_TREE_VIEW(self), GTK_TREE_MODEL(model));
412         g_object_unref (G_OBJECT (model));
413
414         toggle_renderer = gtk_cell_renderer_toggle_new ();
415         text_renderer = gtk_cell_renderer_text_new ();
416
417         /* the is_default column */
418         g_object_set (G_OBJECT(toggle_renderer), "activatable", TRUE, "radio", TRUE, NULL);
419         gtk_tree_view_append_column (GTK_TREE_VIEW(self),
420                                      gtk_tree_view_column_new_with_attributes (
421                                              _("mcen_ti_default"), toggle_renderer,
422                                              "active", MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, NULL));
423                                         
424         /* Disable the Maemo GtkTreeView::allow-checkbox-mode Maemo modification, 
425          * which causes the model column to be updated automatically when the row is clicked.
426          * Making this the default in Maemo's GTK+ is obviously a bug:
427          * https://maemo.org/bugzilla/show_bug.cgi?id=146
428          *
429          * djcb: indeed, they have been removed for post-bora, i added the ifdefs...
430          */
431 #ifdef MODEST_HAVE_HILDON0_WIDGETS
432         g_object_set(G_OBJECT(self), "allow-checkbox-mode", FALSE, NULL);
433         g_object_set(G_OBJECT(toggle_renderer), "checkbox-mode", FALSE, NULL);
434 #endif /* MODEST_HAVE_HILDON0_WIDGETS */
435
436         priv->sig_handlers = 
437                 modest_signal_mgr_connect (priv->sig_handlers,
438                                            G_OBJECT(toggle_renderer), 
439                                            "toggled", 
440                                            G_CALLBACK(on_account_default_toggled),
441                                            self);
442         
443         /* account name */
444         column =  gtk_tree_view_column_new_with_attributes (_("mcen_ti_account"), text_renderer, "text",
445                                                             MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN, NULL);
446         gtk_tree_view_append_column (GTK_TREE_VIEW(self), column);
447         gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_cell_data,
448                                                 NULL, NULL);
449
450         /* last update for this account */
451         column =  gtk_tree_view_column_new_with_attributes (_("mcen_ti_lastupdated"), text_renderer,"text",
452                                                             MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, NULL);
453         gtk_tree_view_append_column (GTK_TREE_VIEW(self),column);
454         gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_cell_data,
455                                                 NULL, NULL);
456                         
457         /* Show the column headers,
458          * which does not seem to be the default on Maemo.
459          */                     
460         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(self), TRUE);
461
462         priv->sig_handlers = 
463                 modest_signal_mgr_connect (priv->sig_handlers, 
464                                            G_OBJECT (modest_runtime_get_account_store ()),
465                                            "account_removed",
466                                            G_CALLBACK(on_account_removed), 
467                                            self);
468         priv->sig_handlers = 
469                 modest_signal_mgr_connect (priv->sig_handlers, 
470                                            G_OBJECT (modest_runtime_get_account_store ()),
471                                            "account_inserted",
472                                            G_CALLBACK(on_account_inserted), 
473                                            self);
474         priv->sig_handlers = 
475                 modest_signal_mgr_connect (priv->sig_handlers, 
476                                            G_OBJECT (modest_runtime_get_account_store ()),
477                                            "account_changed",
478                                            G_CALLBACK(on_account_changed), 
479                                            self);
480         priv->sig_handlers = 
481                 modest_signal_mgr_connect (priv->sig_handlers, 
482                                            G_OBJECT(priv->account_mgr),
483                                            "account_busy_changed",
484                                            G_CALLBACK(on_account_busy_changed), 
485                                            self);
486         priv->sig_handlers = 
487                 modest_signal_mgr_connect (priv->sig_handlers, 
488                                            G_OBJECT(priv->account_mgr),
489                                            "default_account_changed",
490                                            G_CALLBACK(on_default_account_changed), 
491                                            self);
492         priv->sig_handlers = 
493                 modest_signal_mgr_connect (priv->sig_handlers, 
494                                            G_OBJECT(priv->account_mgr),
495                                            "display_name_changed",
496                                            G_CALLBACK(on_display_name_changed), 
497                                            self);
498 }
499
500
501 ModestAccountView*
502 modest_account_view_new (ModestAccountMgr *account_mgr)
503 {
504         GObject *obj;
505         ModestAccountViewPrivate *priv;
506         
507         g_return_val_if_fail (account_mgr, NULL);
508         
509         obj  = g_object_new(MODEST_TYPE_ACCOUNT_VIEW, NULL);
510         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
511         
512         g_object_ref (G_OBJECT (account_mgr));
513         priv->account_mgr = account_mgr;
514
515         init_view (MODEST_ACCOUNT_VIEW (obj));
516         update_account_view (account_mgr, MODEST_ACCOUNT_VIEW (obj));
517         
518         return MODEST_ACCOUNT_VIEW (obj);
519 }
520
521 gchar *
522 modest_account_view_get_selected_account (ModestAccountView *self)
523 {
524         gchar *account_name = NULL;
525         GtkTreeSelection *sel;
526         GtkTreeModel *model;
527         GtkTreeIter iter;
528
529         g_return_val_if_fail (MODEST_IS_ACCOUNT_VIEW (self), NULL);
530         
531         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
532         if (gtk_tree_selection_get_selected (sel, &model, &iter)) {
533                 gtk_tree_model_get (model, &iter, 
534                                     MODEST_ACCOUNT_VIEW_NAME_COLUMN, 
535                                     &account_name, -1);
536         }
537
538         return account_name;
539 }
540
541 /* This allows us to pass more than one piece of data to the signal handler,
542  * and get a result: */
543 typedef struct 
544 {
545                 ModestAccountView* self;
546                 const gchar *account_name;
547 } ForEachData;
548
549 static gboolean
550 on_model_foreach_select_account(GtkTreeModel *model, 
551         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
552 {
553         ForEachData *state = (ForEachData*)(user_data);
554         
555         /* Select the item if it has the matching account name: */
556         gchar *this_account_name = NULL;
557         gtk_tree_model_get (model, iter, 
558                 MODEST_ACCOUNT_VIEW_NAME_COLUMN, &this_account_name, 
559                 -1); 
560         if(this_account_name && state->account_name 
561                 && (strcmp (this_account_name, state->account_name) == 0)) {
562                 
563                 GtkTreeSelection *selection = 
564                         gtk_tree_view_get_selection (GTK_TREE_VIEW (state->self));
565                 gtk_tree_selection_select_iter (selection, iter);
566                 
567                 return TRUE; /* Stop walking the tree. */
568         }
569         
570         return FALSE; /* Keep walking the tree. */
571 }
572
573 static void 
574 modest_account_view_select_account (ModestAccountView *account_view, 
575                                     const gchar* account_name)
576 {       
577         /* Create a state instance so we can send two items of data to the signal handler: */
578         ForEachData *state = g_new0 (ForEachData, 1);
579         state->self = account_view;
580         state->account_name = account_name;
581         
582         GtkTreeModel *model = gtk_tree_view_get_model (
583                 GTK_TREE_VIEW (account_view));
584         gtk_tree_model_foreach (model, 
585                 on_model_foreach_select_account, state);
586                 
587         g_free (state);
588 }
589
590 static void
591 on_default_account_changed (ModestAccountMgr *mgr,
592                             gpointer user_data)
593 {
594         GtkTreeIter iter;
595         gchar *default_account_name;
596         GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (user_data));
597
598         if (!gtk_tree_model_get_iter_first(model, &iter))
599                 return;
600
601         default_account_name = modest_account_mgr_get_default_account (mgr);
602
603         do {
604                 gboolean is_default;
605                 gchar *name;
606
607                 gtk_tree_model_get (model, &iter, 
608                                     MODEST_ACCOUNT_VIEW_NAME_COLUMN, &name,
609                                     MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, &is_default, 
610                                     -1);
611
612                 /* Update the default account column */
613                 if ((default_account_name != NULL) && (!strcmp (name, default_account_name)))
614                         gtk_list_store_set (GTK_LIST_STORE (model), &iter,
615                                             MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, TRUE, -1);
616                 else
617                         gtk_list_store_set (GTK_LIST_STORE (model), &iter,
618                                             MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, FALSE, -1);
619
620                 g_free (name);
621
622         } while (gtk_tree_model_iter_next(model, &iter));
623
624         /* Free and force a redraw */
625         g_free (default_account_name);
626         gtk_widget_queue_draw (GTK_WIDGET (user_data));
627 }
628
629 static void 
630 on_display_name_changed (ModestAccountMgr *mgr, 
631                          const gchar *account,
632                          gpointer user_data)
633 {
634         /* Update the view */
635         update_account_view (mgr, MODEST_ACCOUNT_VIEW (user_data));
636 }