Use GTK+ single includes
[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 <gtk/gtk.h>
32 #include "modest-account-view.h"
33
34 #include <modest-account-mgr.h>
35 #include <modest-account-mgr-helpers.h>
36 #include <modest-tny-account.h>
37 #include <modest-text-utils.h>
38 #include <modest-runtime.h>
39 #include <modest-signal-mgr.h>
40
41 #include <string.h> /* For strcmp(). */
42 #include <modest-account-mgr-helpers.h>
43 #include <modest-datetime-formatter.h>
44 #ifdef MODEST_TOOLKIT_HILDON2
45 #include <hildon/hildon-defines.h>
46 #endif
47 #ifdef MODEST_USE_LIBTIME
48 #include <clockd/libtime.h>
49 #endif
50
51 /* 'private'/'protected' functions */
52 static void modest_account_view_class_init    (ModestAccountViewClass *klass);
53 static void modest_account_view_init          (ModestAccountView *obj);
54 static void modest_account_view_finalize      (GObject *obj);
55
56 static void modest_account_view_select_account (ModestAccountView *account_view, 
57                                                 const gchar* account_name);
58
59 static void on_default_account_changed         (ModestAccountMgr *mgr,
60                                                 gpointer user_data);
61
62 static void on_display_name_changed            (ModestAccountMgr *self, 
63                                                 const gchar *account,
64                                                 gpointer user_data);
65
66 #ifndef MODEST_TOOLKIT_HILDON2
67 static void modest_account_view_select_first_account (ModestAccountView *account_view);
68 #endif
69
70 static void on_account_updated (ModestAccountMgr* mgr, gchar* account_name,
71                     gpointer user_data);
72 static void update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view);
73 static void on_notify_style (GObject *obj, GParamSpec *spec, gpointer userdata);
74 static void update_picker_mode (ModestAccountView *self);
75
76 typedef enum {
77         MODEST_ACCOUNT_VIEW_NAME_COLUMN,
78         MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN,
79         MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN,
80         MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,
81         MODEST_ACCOUNT_VIEW_PROTO_COLUMN,
82         MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN,
83
84         MODEST_ACCOUNT_VIEW_COLUMN_NUM
85 } AccountViewColumns;
86
87 typedef struct _ModestAccountViewPrivate ModestAccountViewPrivate;
88 struct _ModestAccountViewPrivate {
89         ModestAccountMgr *account_mgr;
90
91         ModestDatetimeFormatter *datetime_formatter;
92         gboolean picker_mode;
93
94         /* Signal handlers */
95         GSList *sig_handlers;
96 };
97 #define MODEST_ACCOUNT_VIEW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
98                                                  MODEST_TYPE_ACCOUNT_VIEW, \
99                                                  ModestAccountViewPrivate))
100 /* globals */
101 static GtkTreeViewClass *parent_class = NULL;
102
103 GType
104 modest_account_view_get_type (void)
105 {
106         static GType my_type = 0;
107         if (!my_type) {
108                 static const GTypeInfo my_info = {
109                         sizeof(ModestAccountViewClass),
110                         NULL,           /* base init */
111                         NULL,           /* base finalize */
112                         (GClassInitFunc) modest_account_view_class_init,
113                         NULL,           /* class finalize */
114                         NULL,           /* class data */
115                         sizeof(ModestAccountView),
116                         1,              /* n_preallocs */
117                         (GInstanceInitFunc) modest_account_view_init,
118                         NULL
119                 };
120                 my_type = g_type_register_static (GTK_TYPE_TREE_VIEW,
121                                                   "ModestAccountView",
122                                                   &my_info, 0);
123         }
124         return my_type;
125 }
126
127 static void
128 modest_account_view_class_init (ModestAccountViewClass *klass)
129 {
130         GObjectClass *gobject_class;
131         gobject_class = (GObjectClass*) klass;
132
133         parent_class            = g_type_class_peek_parent (klass);
134         gobject_class->finalize = modest_account_view_finalize;
135
136         g_type_class_add_private (gobject_class, sizeof(ModestAccountViewPrivate));
137 }
138
139 static void
140 datetime_format_changed (ModestDatetimeFormatter *formatter,
141                          ModestAccountView *self)
142 {
143         ModestAccountViewPrivate *priv;
144         
145         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
146         update_account_view (priv->account_mgr, self);
147 }
148
149 static void
150 modest_account_view_init (ModestAccountView *obj)
151 {
152         ModestAccountViewPrivate *priv;
153         
154         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
155         
156         priv->sig_handlers = NULL;
157
158         priv->datetime_formatter = modest_datetime_formatter_new ();
159         priv->picker_mode = FALSE;
160         g_signal_connect (G_OBJECT (priv->datetime_formatter), "format-changed", 
161                           G_CALLBACK (datetime_format_changed), (gpointer) obj);
162 #ifdef MODEST_TOOLKIT_HILDON2
163         gtk_rc_parse_string ("style \"fremantle-modest-account-view\" {\n"
164                              "  GtkWidget::hildon-mode = 1\n"
165                              "} widget \"*.fremantle-modest-account-view\" style \"fremantle-modest-account-view\""
166                              "widget_class \"*<HildonPannableArea>.ModestAccountView\" style :highest \"fremantle-modest-account-view\"");
167         
168 #endif
169         g_signal_connect (G_OBJECT (obj), "notify::style", G_CALLBACK (on_notify_style), (gpointer) obj);
170 }
171
172 static void
173 modest_account_view_finalize (GObject *obj)
174 {
175         ModestAccountViewPrivate *priv;
176
177         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
178
179         if (priv->datetime_formatter) {
180                 g_object_unref (priv->datetime_formatter);
181                 priv->datetime_formatter = NULL;
182         }
183
184         /* Disconnect signals */
185         modest_signal_mgr_disconnect_all_and_destroy (priv->sig_handlers);
186
187         if (priv->account_mgr) {        
188                 g_object_unref (G_OBJECT(priv->account_mgr));
189                 priv->account_mgr = NULL; 
190         }
191         
192         G_OBJECT_CLASS(parent_class)->finalize (obj);
193 }
194
195 /* Get the string for the last updated time. Result must NOT be g_freed */
196 static const gchar*
197 get_last_updated_string(ModestAccountView *self, ModestAccountMgr* account_mgr, ModestAccountSettings *settings)
198 {
199         /* FIXME: let's assume that 'last update' applies to the store account... */
200         const gchar *last_updated_string;
201         const gchar *store_account_name;
202         const gchar *account_name;
203         time_t last_updated;
204         ModestServerAccountSettings *server_settings;
205         ModestAccountViewPrivate *priv;
206
207         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE (self);
208         server_settings = modest_account_settings_get_store_settings (settings);
209         store_account_name = modest_server_account_settings_get_account_name (server_settings);
210         last_updated = modest_account_mgr_get_last_updated (account_mgr, store_account_name);
211
212         g_object_unref (server_settings);
213         account_name = modest_account_settings_get_account_name (settings);
214         if (!modest_account_mgr_account_is_busy(account_mgr, account_name)) {
215                 if (last_updated > 0) {
216                         last_updated_string = 
217                                 modest_datetime_formatter_display_datetime (priv->datetime_formatter,
218                                                                            last_updated);
219                 } else {
220                         last_updated_string = _("mcen_va_never");
221                 }
222         } else  {
223                 last_updated_string = _("mcen_va_refreshing");
224         }
225
226         return last_updated_string;
227 }
228
229 static void
230 update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
231 {
232         GSList *account_names, *cursor;
233         GtkListStore *model;
234
235         model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(view)));
236
237         /* Get the ID of the currently-selected account,
238          * so we can select it again after rebuilding the list.
239          * Note that the name doesn't change even when the display name changes.
240          */
241         gchar *selected_name = modest_account_view_get_selected_account (view);
242
243         gtk_list_store_clear (model);
244
245         /* Note: We do not show disabled accounts.
246          * Of course, this means that there is no UI to enable or disable
247          * accounts. That is OK for maemo where no such feature or UI is
248          * specified, so the "enabled" property is used internally to avoid
249          * showing unfinished accounts. If a user-visible "enabled" is
250          * needed in the future, we must use a second property for the
251          * current use instead */
252         cursor = account_names = modest_account_mgr_account_names (account_mgr,
253                 TRUE /* only enabled accounts. */);
254
255         while (cursor) {
256                 gchar *account_name;
257                 ModestAccountSettings *settings;
258                 ModestServerAccountSettings *store_settings;
259                 
260                 account_name = (gchar*)cursor->data;
261                 
262                 settings = modest_account_mgr_load_account_settings (account_mgr, account_name);
263                 if (!settings) {
264                         g_printerr ("modest: failed to get account data for %s\n", account_name);
265                         cursor = cursor->next;
266                         continue;
267                 }
268                 store_settings = modest_account_settings_get_store_settings (settings);
269
270                 /* don't display accounts without stores */
271                 if (modest_server_account_settings_get_account_name (store_settings) != NULL) {
272
273                         GtkTreeIter iter;
274
275                         /* don't free */
276                         const gchar *last_updated_string = get_last_updated_string(view, account_mgr, settings);
277                         
278                         if (modest_account_settings_get_enabled (settings)) {
279                                 ModestProtocolType protocol_type;
280                                 ModestProtocolRegistry *protocol_registry;
281                                 ModestProtocol *protocol;
282                                 const gchar *proto_name;
283 #ifdef MODEST_TOOLKIT_HILDON2
284                                 gchar *last_updated_hildon2;
285
286                                 last_updated_hildon2 = g_strconcat (_("mcen_ti_lastupdated"), "\n", 
287                                                                    last_updated_string,
288                                                                    NULL);
289 #endif
290                                 protocol_registry = modest_runtime_get_protocol_registry ();
291                                 protocol_type = modest_server_account_settings_get_protocol (store_settings);
292                                 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, protocol_type);
293                                 proto_name = modest_protocol_get_name (protocol);
294                                 gtk_list_store_insert_with_values (
295                                         model, &iter, 0,
296                                         MODEST_ACCOUNT_VIEW_NAME_COLUMN, account_name,
297                                         MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN, 
298                                         modest_account_settings_get_display_name (settings),
299                                         MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN, 
300                                         modest_account_settings_get_enabled (settings),
301                                         MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, 
302                                         modest_account_settings_get_is_default (settings),
303                                         MODEST_ACCOUNT_VIEW_PROTO_COLUMN, proto_name,
304 #ifdef MODEST_TOOLKIT_HILDON2
305                                         MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN,  last_updated_hildon2,
306 #else
307                                         MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN,  last_updated_string,
308 #endif
309                                         -1);
310 #ifdef MODEST_TOOLKIT_HILDON2
311                                 g_free (last_updated_hildon2);
312 #endif
313                         }
314                 }
315                 
316                 g_object_unref (store_settings);
317                 g_object_unref (settings);
318                 cursor = cursor->next;
319         }
320
321         modest_account_mgr_free_account_names (account_names);
322         account_names = NULL;
323         
324         /* Try to re-select the same account: */
325         if (selected_name) {
326                 modest_account_view_select_account (view, selected_name);
327                 g_free (selected_name);
328         } else {
329 #ifndef MODEST_TOOLKIT_HILDON2
330                 modest_account_view_select_first_account (view);
331 #endif
332         }
333 }
334
335 static void
336 on_account_busy_changed(ModestAccountMgr *account_mgr, 
337                         const gchar *account_name,
338                         gboolean busy, 
339                         ModestAccountView *self)
340 {
341         GtkListStore *model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(self)));
342         GtkTreeIter iter;
343         gboolean found = FALSE;
344
345         if (!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(model), &iter))
346                 return;
347
348         do {
349                 gchar* cur_name;
350                 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, 
351                                    MODEST_ACCOUNT_VIEW_NAME_COLUMN, 
352                                    &cur_name, -1);
353
354                 if (g_str_equal(cur_name, account_name)) {
355                         ModestAccountSettings* settings = 
356                                 modest_account_mgr_load_account_settings (account_mgr, account_name);
357                         if (!settings) {
358                                 g_free (cur_name);
359                                 return;
360                         }
361                         const gchar* last_updated_string = get_last_updated_string(self, account_mgr, settings);
362 #ifdef MODEST_TOOLKIT_HILDON2
363                         gchar *last_updated_hildon2;
364
365                         last_updated_hildon2 = g_strconcat (_("mcen_ti_lastupdated"), "\n", 
366                                                             last_updated_string,
367                                                             NULL);
368 #endif
369                         gtk_list_store_set(model, &iter, 
370 #ifdef MODEST_TOOLKIT_HILDON2
371                                            MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, last_updated_hildon2,
372 #else
373                                            MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, last_updated_string,
374 #endif
375                                            -1);
376
377 #ifdef MODEST_TOOLKIT_HILDON2
378                         g_free (last_updated_hildon2);
379 #endif
380                         g_object_unref (settings);
381                         found = TRUE;
382                 }
383                 g_free (cur_name);
384
385         } while (!found && gtk_tree_model_iter_next(GTK_TREE_MODEL(model), &iter));
386 }
387
388 static void
389 on_account_inserted (TnyAccountStore *account_store, 
390                      TnyAccount *account,
391                      gpointer user_data)
392 {
393         ModestAccountView *self;
394         ModestAccountViewPrivate *priv;
395
396         g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (user_data));
397
398         self = MODEST_ACCOUNT_VIEW (user_data);
399         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE (self);
400
401         /* Do not refresh the view with transport accounts */
402         if (TNY_IS_STORE_ACCOUNT (account))
403                 update_account_view (priv->account_mgr, self);
404 }
405
406 static void
407 on_account_removed (TnyAccountStore *account_store, 
408                     TnyAccount *account,
409                     gpointer user_data)
410 {
411         ModestAccountView *self;
412         ModestAccountViewPrivate *priv;
413         gchar *selected_name;
414
415         g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (user_data));
416
417         self = MODEST_ACCOUNT_VIEW (user_data);
418         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE (self);
419
420         /* Do not refresh the view with transport accounts */
421         if (!TNY_IS_STORE_ACCOUNT (account))
422                 return;
423         
424         selected_name = modest_account_view_get_selected_account (self);
425         if (selected_name == NULL) {
426 #ifndef MODEST_TOOLKIT_HILDON2
427                 /* we select the first account if none is selected */
428                 modest_account_view_select_first_account (self);
429 #endif
430         } else {
431                 g_free (selected_name);
432         }
433         
434         update_account_view (priv->account_mgr, self);
435 }
436
437
438 static void
439 on_account_default_toggled (GtkCellRendererToggle *cell_renderer, 
440                             gchar *path,
441                             ModestAccountView *self)
442 {
443         ModestAccountViewPrivate *priv;
444         GtkTreeModel *model;
445         GtkTreeIter iter;
446         gchar *account_name = NULL;
447
448         g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (self));
449
450         /* If it's active then do nothing, no need to reenable it as
451            default account */
452         if (gtk_cell_renderer_toggle_get_active (cell_renderer))
453                 return;
454
455         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
456         model = gtk_tree_view_get_model (GTK_TREE_VIEW(self));
457
458         if (!gtk_tree_model_get_iter_from_string (model, &iter, path)) {
459                 g_warning ("Got path of a not existing iter");
460                 return;
461         }
462         
463         gtk_tree_model_get (model, &iter, 
464                             MODEST_ACCOUNT_VIEW_NAME_COLUMN, 
465                             &account_name, -1);
466
467         /* Set this previously-non-default account as the
468            default. We're not updating here the value of the
469            DEFAULT_COLUMN because we'll do it in the
470            "default_account_changed" signal handler. We do it like
471            this because that way the signal handler is useful also
472            when we're inserting a new account and there is no other
473            one defined, in that case the change of account is provoked
474            by the account mgr and not by a signal toggle.*/
475         modest_account_mgr_set_default_account (priv->account_mgr, account_name);
476
477         g_free (account_name);
478 }
479
480 static void
481 on_account_updated (ModestAccountMgr* mgr,
482                     gchar* account_name,
483                     gpointer user_data)
484 {
485         update_account_view (mgr, MODEST_ACCOUNT_VIEW (user_data));
486 }
487
488 static void
489 bold_if_default_account_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
490                                     GtkTreeModel *tree_model,  GtkTreeIter *iter,  gpointer user_data)
491 {
492         gboolean is_default;
493         GtkStyle *style;
494         const gchar *font_style;
495         PangoAttribute *attr;
496         PangoAttrList *attr_list = NULL;
497         GtkWidget *widget;
498
499         gtk_tree_model_get (tree_model, iter, MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,
500                             &is_default, -1);
501
502 /*      widget = gtk_tree_view_column_get_tree_view (column); */
503         widget = GTK_WIDGET (user_data);
504         font_style = is_default?"EmpSystemFont":"SystemFont";
505         style = gtk_rc_get_style_by_paths (gtk_widget_get_settings (GTK_WIDGET(widget)),
506                                            font_style, NULL,
507                                            G_TYPE_NONE);
508         if (style) {
509                 attr = pango_attr_font_desc_new (pango_font_description_copy (style->font_desc));
510
511                 attr_list = pango_attr_list_new ();
512                 pango_attr_list_insert (attr_list, attr);
513
514                 g_object_set (G_OBJECT(renderer),
515                               "attributes", attr_list, 
516                               NULL);
517
518                 pango_attr_list_unref (attr_list);
519         } else {
520                 g_object_set (G_OBJECT(renderer),
521                               "weight", is_default ? 800: 400,
522                               NULL);
523         }
524 }
525
526 static void
527 bold_if_default_last_updated_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
528                                          GtkTreeModel *tree_model,  GtkTreeIter *iter,  gpointer user_data)
529 {
530         gboolean is_default;
531         GtkStyle *style;
532         const gchar *font_style;
533         PangoAttribute *attr;
534         PangoAttrList *attr_list = NULL;
535         GtkWidget *widget;
536
537         gtk_tree_model_get (tree_model, iter, MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,
538                             &is_default, -1);
539
540 /*      widget = gtk_tree_view_column_get_tree_view (column); */
541         widget = GTK_WIDGET (user_data);
542         font_style = is_default?"EmpSmallSystemFont":"SmallSystemFont";
543         style = gtk_rc_get_style_by_paths (gtk_widget_get_settings (GTK_WIDGET(widget)),
544                                            font_style, NULL,
545                                            G_TYPE_NONE);
546         if (style) {
547                 attr = pango_attr_font_desc_new (pango_font_description_copy (style->font_desc));
548
549                 attr_list = pango_attr_list_new ();
550                 pango_attr_list_insert (attr_list, attr);
551
552                 g_object_set (G_OBJECT(renderer),
553                               "attributes", attr_list, 
554                               NULL);
555
556                 pango_attr_list_unref (attr_list);
557         } else {
558                 g_object_set (G_OBJECT(renderer),
559                               "weight", is_default ? 800: 400,
560                               NULL);
561         }
562 }
563
564 static void
565 init_view (ModestAccountView *self)
566 {
567         ModestAccountViewPrivate *priv;
568         GtkCellRenderer *toggle_renderer, *text_renderer;
569         GtkListStore *model;
570         GtkTreeViewColumn *column;
571         
572         g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (self));
573         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
574                 
575         model = gtk_list_store_new (6,
576                                     G_TYPE_STRING,  /* account name */
577                                     G_TYPE_STRING,  /* account display name */
578                                     G_TYPE_BOOLEAN, /* is-enabled */
579                                     G_TYPE_BOOLEAN, /* is-default */
580                                     G_TYPE_STRING,  /* account proto (pop, imap,...) */
581                                     G_TYPE_STRING   /* last updated (time_t) */
582                 ); 
583                 
584         gtk_tree_sortable_set_sort_column_id (
585                 GTK_TREE_SORTABLE (model), MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN, 
586                 GTK_SORT_ASCENDING);
587
588         gtk_tree_view_set_model (GTK_TREE_VIEW(self), GTK_TREE_MODEL(model));
589         g_object_unref (G_OBJECT (model));
590
591         toggle_renderer = gtk_cell_renderer_toggle_new ();
592         /* the is_default column */
593         g_object_set (G_OBJECT(toggle_renderer), "activatable", TRUE, "radio", TRUE, NULL);
594         column = gtk_tree_view_column_new_with_attributes 
595                 (_("mcen_ti_default"), toggle_renderer,
596                  "active", MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, NULL);
597         gtk_tree_view_append_column (GTK_TREE_VIEW(self),
598                                      column);
599 #ifdef MODEST_TOOLKIT_HILDON2
600         gtk_tree_view_column_set_visible (column, FALSE);
601 #endif
602                                         
603         /* Disable the Maemo GtkTreeView::allow-checkbox-mode Maemo modification, 
604          * which causes the model column to be updated automatically when the row is clicked.
605          * Making this the default in Maemo's GTK+ is obviously a bug:
606          * https://maemo.org/bugzilla/show_bug.cgi?id=146
607          *
608          * djcb: indeed, they have been removed for post-bora, i added the ifdefs...
609          */
610 #ifdef MODEST_HAVE_HILDON0_WIDGETS
611         g_object_set(G_OBJECT(self), "allow-checkbox-mode", FALSE, NULL);
612         g_object_set(G_OBJECT(toggle_renderer), "checkbox-mode", FALSE, NULL);
613 #endif /* MODEST_HAVE_HILDON0_WIDGETS */
614
615         priv->sig_handlers = 
616                 modest_signal_mgr_connect (priv->sig_handlers,
617                                            G_OBJECT(toggle_renderer), 
618                                            "toggled", 
619                                            G_CALLBACK(on_account_default_toggled),
620                                            self);
621         
622         /* account name */
623         text_renderer = gtk_cell_renderer_text_new ();
624         g_object_set (G_OBJECT (text_renderer), 
625                       "ellipsize", PANGO_ELLIPSIZE_END, "ellipsize-set", TRUE, 
626 #ifdef MODEST_TOOLKIT_HILDON2
627                       "xpad", HILDON_MARGIN_DOUBLE,
628 #endif
629                       NULL);
630
631         column =  gtk_tree_view_column_new_with_attributes (_("mcen_ti_account"), text_renderer, "text",
632                                                             MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN, NULL);
633         gtk_tree_view_append_column (GTK_TREE_VIEW(self), column);
634         gtk_tree_view_column_set_expand (column, TRUE);
635         gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_account_cell_data,
636                                                 self, NULL);
637
638         /* last update for this account */
639         text_renderer = gtk_cell_renderer_text_new ();
640         g_object_set (G_OBJECT (text_renderer), 
641                       "alignment", PANGO_ALIGN_RIGHT, 
642                       "xalign", 1.0,
643 #ifdef MODEST_TOOLKIT_HILDON2
644                       "xpad", HILDON_MARGIN_DOUBLE,
645 #endif
646                       NULL);
647
648         column =  gtk_tree_view_column_new_with_attributes (_("mcen_ti_lastupdated"), text_renderer,"markup",
649                                                             MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, NULL);
650         gtk_tree_view_append_column (GTK_TREE_VIEW(self),column);
651         gtk_tree_view_column_set_expand (column, FALSE);
652         gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_last_updated_cell_data,
653                                                 self, NULL);
654
655         /* Show the column headers,
656          * which does not seem to be the default on Maemo.
657          */
658 #ifndef MODEST_TOOLKIT_HILDON2
659         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(self), TRUE);
660 #endif
661
662         update_picker_mode (self);
663
664         priv->sig_handlers = 
665                 modest_signal_mgr_connect (priv->sig_handlers, 
666                                            G_OBJECT (modest_runtime_get_account_store ()),
667                                            "account_removed",
668                                            G_CALLBACK(on_account_removed), 
669                                            self);
670         priv->sig_handlers = 
671                 modest_signal_mgr_connect (priv->sig_handlers, 
672                                            G_OBJECT (modest_runtime_get_account_store ()),
673                                            "account_inserted",
674                                            G_CALLBACK(on_account_inserted), 
675                                            self);
676         priv->sig_handlers = 
677                 modest_signal_mgr_connect (priv->sig_handlers, 
678                                            G_OBJECT(priv->account_mgr),
679                                            "account_busy_changed",
680                                            G_CALLBACK(on_account_busy_changed), 
681                                            self);
682         priv->sig_handlers = 
683                 modest_signal_mgr_connect (priv->sig_handlers, 
684                                            G_OBJECT(priv->account_mgr),
685                                            "default_account_changed",
686                                            G_CALLBACK(on_default_account_changed), 
687                                            self);
688         priv->sig_handlers = 
689                 modest_signal_mgr_connect (priv->sig_handlers, 
690                                            G_OBJECT(priv->account_mgr),
691                                            "display_name_changed",
692                                            G_CALLBACK(on_display_name_changed), 
693                                            self);
694         priv->sig_handlers = 
695                         modest_signal_mgr_connect (priv->sig_handlers,
696                                                    G_OBJECT (priv->account_mgr),
697                                                    "account_updated", 
698                                                    G_CALLBACK (on_account_updated),
699                                                    self);
700 }
701
702
703 ModestAccountView*
704 modest_account_view_new (ModestAccountMgr *account_mgr)
705 {
706         GObject *obj;
707         ModestAccountViewPrivate *priv;
708         
709         g_return_val_if_fail (account_mgr, NULL);
710         
711         obj  = g_object_new(MODEST_TYPE_ACCOUNT_VIEW, 
712 #ifdef MODEST_TOOLKIT_HILDON2
713                             "hildon-ui-mode", HILDON_UI_MODE_NORMAL,
714 #endif
715                             NULL);
716         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
717         
718         g_object_ref (G_OBJECT (account_mgr));
719         priv->account_mgr = account_mgr;
720
721         init_view (MODEST_ACCOUNT_VIEW (obj));
722         update_account_view (account_mgr, MODEST_ACCOUNT_VIEW (obj));
723         
724         return MODEST_ACCOUNT_VIEW (obj);
725 }
726
727 gchar *
728 modest_account_view_get_selected_account (ModestAccountView *self)
729 {
730         gchar *account_name = NULL;
731         GtkTreeSelection *sel;
732         GtkTreeModel *model;
733         GtkTreeIter iter;
734
735         g_return_val_if_fail (MODEST_IS_ACCOUNT_VIEW (self), NULL);
736         
737         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
738         if (gtk_tree_selection_get_selected (sel, &model, &iter)) {
739                 gtk_tree_model_get (model, &iter, 
740                                     MODEST_ACCOUNT_VIEW_NAME_COLUMN, 
741                                     &account_name, -1);
742         }
743
744         return account_name;
745 }
746
747 gchar *
748 modest_account_view_get_path_account (ModestAccountView *self, GtkTreePath *path)
749 {
750         gchar *account_name = NULL;
751         GtkTreeModel *model;
752         GtkTreeIter iter;
753
754         g_return_val_if_fail (MODEST_IS_ACCOUNT_VIEW (self), NULL);
755
756         model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
757         if (gtk_tree_model_get_iter (model, &iter, path)) {
758                 gtk_tree_model_get (model, &iter, 
759                                     MODEST_ACCOUNT_VIEW_NAME_COLUMN, 
760                                     &account_name, -1);
761         }
762
763         return account_name;
764 }
765
766 /* This allows us to pass more than one piece of data to the signal handler,
767  * and get a result: */
768 typedef struct 
769 {
770                 ModestAccountView* self;
771                 const gchar *account_name;
772 } ForEachData;
773
774 static gboolean
775 on_model_foreach_select_account(GtkTreeModel *model, 
776         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
777 {
778         ForEachData *state = (ForEachData*)(user_data);
779         
780         /* Select the item if it has the matching account name: */
781         gchar *this_account_name = NULL;
782         gtk_tree_model_get (model, iter, 
783                 MODEST_ACCOUNT_VIEW_NAME_COLUMN, &this_account_name, 
784                 -1); 
785         if(this_account_name && state->account_name 
786                 && (strcmp (this_account_name, state->account_name) == 0)) {
787                 
788                 GtkTreeSelection *selection = 
789                         gtk_tree_view_get_selection (GTK_TREE_VIEW (state->self));
790                 gtk_tree_selection_select_iter (selection, iter);
791                 
792                 return TRUE; /* Stop walking the tree. */
793         }
794         
795         return FALSE; /* Keep walking the tree. */
796 }
797
798 static void 
799 modest_account_view_select_account (ModestAccountView *account_view, 
800                                     const gchar* account_name)
801 {       
802 #ifdef MODEST_TOOLKIT_HILDON2
803         return;
804 #endif
805         /* Create a state instance so we can send two items of data to the signal handler: */
806         ForEachData *state = g_new0 (ForEachData, 1);
807         state->self = account_view;
808         state->account_name = account_name;
809         
810         GtkTreeModel *model = gtk_tree_view_get_model (
811                 GTK_TREE_VIEW (account_view));
812         gtk_tree_model_foreach (model, 
813                 on_model_foreach_select_account, state);
814                 
815         g_free (state);
816 }
817
818 #ifndef MODEST_TOOLKIT_HILDON2
819 static void
820 modest_account_view_select_first_account (ModestAccountView *account_view)
821 {
822         GtkTreeIter iter;
823         GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (account_view));
824
825         if (gtk_tree_model_get_iter_first (model, &iter)) {
826                 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (account_view));
827
828                 gtk_tree_selection_select_iter (selection, &iter);
829         }
830 }
831 #endif
832
833 static void
834 on_default_account_changed (ModestAccountMgr *mgr,
835                             gpointer user_data)
836 {
837         GtkTreeIter iter;
838         gchar *default_account_name;
839         GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (user_data));
840
841         if (!gtk_tree_model_get_iter_first(model, &iter))
842                 return;
843
844         default_account_name = modest_account_mgr_get_default_account (mgr);
845
846         do {
847                 gboolean is_default;
848                 gchar *name;
849
850                 gtk_tree_model_get (model, &iter, 
851                                     MODEST_ACCOUNT_VIEW_NAME_COLUMN, &name,
852                                     MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, &is_default, 
853                                     -1);
854
855                 /* Update the default account column */
856                 if ((default_account_name != NULL) && (!strcmp (name, default_account_name)))
857                         gtk_list_store_set (GTK_LIST_STORE (model), &iter,
858                                             MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, TRUE, -1);
859                 else
860                         gtk_list_store_set (GTK_LIST_STORE (model), &iter,
861                                             MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, FALSE, -1);
862
863                 g_free (name);
864
865         } while (gtk_tree_model_iter_next(model, &iter));
866
867         /* Free and force a redraw */
868         g_free (default_account_name);
869         gtk_widget_queue_draw (GTK_WIDGET (user_data));
870 }
871
872 static void 
873 on_display_name_changed (ModestAccountMgr *mgr, 
874                          const gchar *account,
875                          gpointer user_data)
876 {
877         /* Update the view */
878         update_account_view (mgr, MODEST_ACCOUNT_VIEW (user_data));
879 }
880
881 static void 
882 on_notify_style (GObject *obj, GParamSpec *spec, gpointer userdata)
883 {
884         if (strcmp ("style", spec->name) == 0) {
885                 gtk_widget_queue_draw (GTK_WIDGET (obj));
886         } 
887 }
888
889 static void
890 update_picker_mode (ModestAccountView *self)
891 {
892         ModestAccountViewPrivate *priv;
893         GtkTreeViewColumn *column;
894         GList *renderers;
895         GtkCellRenderer *renderer;
896         
897         g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (self));
898         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
899
900         /* Last updated column */
901         column = gtk_tree_view_get_column (GTK_TREE_VIEW (self), 2);
902         gtk_tree_view_column_set_visible (column, !priv->picker_mode);
903
904         /* Name column */
905         column = gtk_tree_view_get_column (GTK_TREE_VIEW (self), 1);
906         renderers = gtk_tree_view_column_get_cell_renderers (column);
907         renderer = (GtkCellRenderer *) renderers->data;
908         g_object_set (renderer, 
909                       "align-set", TRUE,
910                       "alignment", priv->picker_mode?PANGO_ALIGN_CENTER:PANGO_ALIGN_LEFT,
911                       NULL);
912         g_list_free (renderers);
913 }
914
915 void 
916 modest_account_view_set_picker_mode (ModestAccountView *self, gboolean enable)
917 {
918         ModestAccountViewPrivate *priv;
919         
920         g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (self));
921         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
922
923         priv->picker_mode = enable;
924         update_picker_mode (self);
925 }
926
927 gboolean 
928 modest_account_view_get_picker_mode (ModestAccountView *self)
929 {
930         ModestAccountViewPrivate *priv;
931         
932         g_return_val_if_fail (MODEST_IS_ACCOUNT_VIEW (self), FALSE);
933         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
934
935         return priv->picker_mode;
936 }