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