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