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