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