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