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