2007-06-05 Johannes Schmid <johannes.schmid@openismus.com>
[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-text-utils.h>
36 #include <modest-runtime.h>
37
38 #include <gtk/gtkcellrenderertoggle.h>
39 #include <gtk/gtkcellrenderertext.h>
40 #include <gtk/gtktreeselection.h>
41 #include <gtk/gtkliststore.h>
42 #include <string.h> /* For strcmp(). */
43
44 /* 'private'/'protected' functions */
45 static void modest_account_view_class_init    (ModestAccountViewClass *klass);
46 static void modest_account_view_init          (ModestAccountView *obj);
47 static void modest_account_view_finalize      (GObject *obj);
48
49
50 typedef enum {
51         MODEST_ACCOUNT_VIEW_NAME_COLUMN,
52         MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN,
53         MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN,
54         MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,
55         MODEST_ACCOUNT_VIEW_PROTO_COLUMN,
56         MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN,
57
58         MODEST_ACCOUNT_VIEW_COLUMN_NUM
59 } AccountViewColumns;
60
61
62 /* list my signals */
63 enum {
64         /* MY_SIGNAL_1, */
65         /* MY_SIGNAL_2, */
66         LAST_SIGNAL
67 };
68
69 typedef struct _ModestAccountViewPrivate ModestAccountViewPrivate;
70 struct _ModestAccountViewPrivate {
71         ModestAccountMgr *account_mgr;
72         gulong sig1, sig2;
73         
74         /* When this is TRUE, we ignore configuration key changes.
75          * This is useful when making many changes. */
76         gboolean block_conf_updates;
77         
78 };
79 #define MODEST_ACCOUNT_VIEW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
80                                                  MODEST_TYPE_ACCOUNT_VIEW, \
81                                                  ModestAccountViewPrivate))
82 /* globals */
83 static GtkTreeViewClass *parent_class = NULL;
84
85 /* uncomment the following if you have defined any signals */
86 /* static guint signals[LAST_SIGNAL] = {0}; */
87
88 GType
89 modest_account_view_get_type (void)
90 {
91         static GType my_type = 0;
92         if (!my_type) {
93                 static const GTypeInfo my_info = {
94                         sizeof(ModestAccountViewClass),
95                         NULL,           /* base init */
96                         NULL,           /* base finalize */
97                         (GClassInitFunc) modest_account_view_class_init,
98                         NULL,           /* class finalize */
99                         NULL,           /* class data */
100                         sizeof(ModestAccountView),
101                         1,              /* n_preallocs */
102                         (GInstanceInitFunc) modest_account_view_init,
103                         NULL
104                 };
105                 my_type = g_type_register_static (GTK_TYPE_TREE_VIEW,
106                                                   "ModestAccountView",
107                                                   &my_info, 0);
108         }
109         return my_type;
110 }
111
112 static void
113 modest_account_view_class_init (ModestAccountViewClass *klass)
114 {
115         GObjectClass *gobject_class;
116         gobject_class = (GObjectClass*) klass;
117
118         parent_class            = g_type_class_peek_parent (klass);
119         gobject_class->finalize = modest_account_view_finalize;
120
121         g_type_class_add_private (gobject_class, sizeof(ModestAccountViewPrivate));
122 }
123
124 static void
125 modest_account_view_init (ModestAccountView *obj)
126 {
127         ModestAccountViewPrivate *priv;
128
129         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
130         
131         priv->account_mgr = NULL; 
132         priv->sig1 = 0;
133         priv->sig2 = 0;
134 }
135
136 static void
137 modest_account_view_finalize (GObject *obj)
138 {
139         ModestAccountViewPrivate *priv;
140
141         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
142
143         if (priv->account_mgr) {
144                 if (priv->sig1)
145                         g_signal_handler_disconnect (priv->account_mgr, priv->sig1);
146
147                 if (priv->sig2)
148                         g_signal_handler_disconnect (priv->account_mgr, priv->sig2);
149
150                 g_object_unref (G_OBJECT(priv->account_mgr));
151                 priv->account_mgr = NULL; 
152         }
153         
154         G_OBJECT_CLASS(parent_class)->finalize (obj);
155 }
156
157
158
159 static void
160 update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
161 {
162         GSList *account_names, *cursor;
163         GtkListStore *model;
164                 
165         model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(view)));  
166         gtk_list_store_clear (model);
167
168         /* Note: We do not show disabled accounts.
169          * Of course, this means that there is no UI to enable or disable 
170          * accounts. That is OK for maemo where no such feature or UI is 
171          * specified, so the "enabled" property is used internally to avoid 
172          * showing unfinished accounts. If a user-visible "enabled" is 
173          * needed in the future, we must use a second property for the 
174          * current use instead */
175         cursor = account_names = modest_account_mgr_account_names (account_mgr,
176                 TRUE /* only enabled accounts. */);
177         
178         if(account_names == NULL)
179         {
180           printf ("debug: modest_account_mgr_account_names() returned  NULL\n");
181         }
182
183         while (cursor) {
184                 gchar *account_name;
185                 ModestAccountData *account_data;
186                 
187                 account_name = (gchar*)cursor->data;
188                 
189                 account_data = modest_account_mgr_get_account_data (account_mgr, account_name);
190                 if (!account_data) {
191                         g_printerr ("modest: failed to get account data for %s\n", account_name);
192                         continue;
193                 }
194
195                 /* don't display accounts without stores */
196                 if (account_data->store_account) {
197
198                         GtkTreeIter iter;
199                         time_t last_updated; 
200                         gchar *last_updated_string;
201                         
202                         /* FIXME: let's assume that 'last update' applies to the store account... */
203                         last_updated = account_data->store_account->last_updated;
204                         if (last_updated > 0) 
205                                 last_updated_string = modest_text_utils_get_display_date(last_updated);
206                         else
207                                 last_updated_string = g_strdup (_("mcen_va_never"));
208                         
209                         if (account_data->is_enabled) {
210                                 gtk_list_store_insert_with_values (
211                                         model, &iter, 0,
212                                         MODEST_ACCOUNT_VIEW_NAME_COLUMN,          account_name,
213                                         MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN,  account_data->display_name,
214                                         MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN,    account_data->is_enabled,
215                                         MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,    account_data->is_default,
216         
217                                         MODEST_ACCOUNT_VIEW_PROTO_COLUMN,
218                                         modest_protocol_info_get_transport_store_protocol_name (account_data->store_account->proto),
219         
220                                         MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN,  last_updated_string,
221                                         -1);
222                                 g_free (last_updated_string);
223                         }
224                 }
225
226                 modest_account_mgr_free_account_data (account_mgr, account_data);
227                 cursor = cursor->next;
228         }
229         g_slist_free (account_names);
230 }
231
232
233 static void
234 on_account_changed (ModestAccountMgr *account_mgr,
235                     const gchar* account, const gchar* key,
236                     gboolean server_account, ModestAccountView *self)
237 {       
238         /* Never update the view in response to gconf changes.
239          * Always do it explicitly instead.
240          * This is because we have no way to avoid 10 updates when changing 
241          * 10 items, and this blocks the UI.
242          *
243          * But this block/unblock API might be useful on platforms where the 
244          * notification does not happen so long after the key was set.
245          * (We have no way to know when the last key was set, to do a final update)..
246          */
247          return;
248          
249         ModestAccountViewPrivate* priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
250         
251         if (!priv->block_conf_updates)
252                 update_account_view (account_mgr, self);
253 }
254
255
256 static void
257 on_account_removed (ModestAccountMgr *account_mgr,
258                     const gchar* account, gboolean server_account,
259                     ModestAccountView *self)
260 {
261         ModestAccountViewPrivate* priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
262         if (!priv->block_conf_updates)
263                 on_account_changed (account_mgr, account, NULL, server_account, self);
264 }
265
266
267 /* currently unused */
268 #if 0 
269 static void
270 on_account_enable_toggled (GtkCellRendererToggle *cell_renderer, gchar *path,
271                            ModestAccountView *self)
272 {
273         GtkTreeIter iter;
274         ModestAccountViewPrivate *priv;
275         GtkTreeModel *model;
276         gchar *account_name;
277         gboolean enabled;
278         
279         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
280         model = gtk_tree_view_get_model (GTK_TREE_VIEW(self));
281         
282         if (!gtk_tree_model_get_iter_from_string (model, &iter, path)) {
283                 g_printerr ("modest: cannot find iterator\n");
284                 return;
285         }
286         gtk_tree_model_get (model, &iter, MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN, &enabled,
287                             MODEST_ACCOUNT_VIEW_NAME_COLUMN, &account_name,
288                             -1);
289         
290         /* toggle enabled / disabled */
291         modest_account_mgr_set_enabled (priv->account_mgr, account_name, !enabled);
292         g_free (account_name);
293 }
294 #endif
295
296 static gboolean
297 find_default_account(ModestAccountView *self, GtkTreeIter *iter)
298 {
299         GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
300         gboolean result;
301         for (result = gtk_tree_model_get_iter_first(model, iter);
302              result == TRUE; result = gtk_tree_model_iter_next(model, iter))
303         {
304                 gboolean is_default;
305                 gtk_tree_model_get (model, iter, MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, &is_default, -1);
306                 if(is_default)
307                         return TRUE;
308         }
309
310         return FALSE;
311 }
312
313 static void
314 on_account_default_toggled (GtkCellRendererToggle *cell_renderer, gchar *path,
315                            ModestAccountView *self)
316 {
317         gboolean is_default = gtk_cell_renderer_toggle_get_active (cell_renderer);
318         if (is_default) {
319                 /* Do not allow an account to be marked non-default.
320                  * Only allow this to be changed by setting another account to default: */
321                 gtk_cell_renderer_toggle_set_active (cell_renderer, TRUE);
322                 return;
323         }
324
325         ModestAccountViewPrivate *priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
326         GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW(self));
327         
328         GtkTreeIter iter;
329         if (!gtk_tree_model_get_iter_from_string (model, &iter, path)) {
330                 g_printerr ("modest: cannot find iterator\n");
331                 return;
332         }
333         
334         gchar *account_name = NULL;
335         gtk_tree_model_get (model, &iter, MODEST_ACCOUNT_VIEW_NAME_COLUMN, &account_name,
336                             -1);
337         
338         /* Set this previously-non-default account as the default: */
339         if (modest_account_mgr_set_default_account (priv->account_mgr, account_name))
340         {
341                 /* Explicitely set default column because we are ignoring gconf changes */
342                 GtkTreeIter old_default_iter;
343                 if (find_default_account (self, &old_default_iter)) {
344                         gtk_list_store_set (GTK_LIST_STORE (model), &old_default_iter,
345                                             MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, FALSE, -1);
346                 } else {
347                         g_warning ("%s: Did not find old default account in view", __FUNCTION__);
348                 }
349
350                 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
351                                     MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, TRUE, -1);
352         }
353
354         g_free (account_name);
355 }
356
357 void
358 bold_if_default_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
359                             GtkTreeModel *tree_model,  GtkTreeIter *iter,  gpointer user_data)
360 {
361         gboolean is_default;
362         gtk_tree_model_get (tree_model, iter, MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,
363                             &is_default, -1);
364         g_object_set (G_OBJECT(renderer),
365                       "weight", is_default ? 800: 400,
366                       NULL);
367 }
368
369 static void
370 init_view (ModestAccountView *self)
371 {
372         ModestAccountViewPrivate *priv;
373         GtkCellRenderer *toggle_renderer, *text_renderer;
374         GtkListStore *model;
375         GtkTreeViewColumn *column;
376         
377         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
378                 
379         model = gtk_list_store_new (6,
380                                     G_TYPE_STRING,  /* account name */
381                                     G_TYPE_STRING,  /* account display name */
382                                     G_TYPE_BOOLEAN, /* is-enabled */
383                                     G_TYPE_BOOLEAN, /* is-default */
384                                     G_TYPE_STRING,  /* account proto (pop, imap,...) */
385                                     G_TYPE_STRING   /* last updated (time_t) */
386                 ); 
387                 
388         gtk_tree_sortable_set_sort_column_id (
389                 GTK_TREE_SORTABLE (model), MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN, 
390                 GTK_SORT_ASCENDING);
391
392         gtk_tree_view_set_model (GTK_TREE_VIEW(self), GTK_TREE_MODEL(model));
393         g_object_unref (G_OBJECT (model));
394
395         toggle_renderer = gtk_cell_renderer_toggle_new ();
396         text_renderer = gtk_cell_renderer_text_new ();
397
398         /* the is_default column */
399         g_object_set (G_OBJECT(toggle_renderer), "activatable", TRUE, "radio", TRUE, NULL);
400         gtk_tree_view_append_column (GTK_TREE_VIEW(self),
401                                      gtk_tree_view_column_new_with_attributes (
402                                              _("mcen_ti_default"), toggle_renderer,
403                                              "active", MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, NULL));
404                                         
405         /* Disable the Maemo GtkTreeView::allow-checkbox-mode Maemo modification, 
406          * which causes the model column to be updated automatically when the row is clicked.
407          * Making this the default in Maemo's GTK+ is obviously a bug:
408          * https://maemo.org/bugzilla/show_bug.cgi?id=146
409          *
410          * djcb: indeed, they have been removed for post-bora, i added the ifdefs...
411          */
412 #ifdef MODEST_HILDON_VERSION_0  
413         g_object_set(G_OBJECT(self), "allow-checkbox-mode", FALSE, NULL);
414         g_object_set(G_OBJECT(toggle_renderer), "checkbox-mode", FALSE, NULL);
415 #endif /*MODEST_HILDON_VERSION_0 */
416         g_signal_connect (G_OBJECT(toggle_renderer), "toggled", G_CALLBACK(on_account_default_toggled),
417                           self);
418         
419         /* account name */
420         column =  gtk_tree_view_column_new_with_attributes (_("mcen_ti_account"), text_renderer, "text",
421                                                             MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN, NULL);
422         gtk_tree_view_append_column (GTK_TREE_VIEW(self), column);
423         gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_cell_data,
424                                                 NULL, NULL);
425
426         /* last update for this account */
427         column =  gtk_tree_view_column_new_with_attributes (_("mcen_ti_lastupdated"), text_renderer,"text",
428                                                             MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, NULL);
429         gtk_tree_view_append_column (GTK_TREE_VIEW(self),column);
430         gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_cell_data,
431                                                 NULL, NULL);
432                         
433         /* Show the column headers,
434          * which does not seem to be the default on Maemo.
435          */                     
436         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(self), TRUE);
437
438         priv->sig1 = g_signal_connect (G_OBJECT(priv->account_mgr),"account_removed",
439                                        G_CALLBACK(on_account_removed), self);
440         priv->sig2 = g_signal_connect (G_OBJECT(priv->account_mgr), "account_changed",
441                                        G_CALLBACK(on_account_changed), self);
442 }
443
444
445
446 ModestAccountView*
447 modest_account_view_new (ModestAccountMgr *account_mgr)
448 {
449         GObject *obj;
450         ModestAccountViewPrivate *priv;
451         
452         g_return_val_if_fail (account_mgr, NULL);
453         
454         obj  = g_object_new(MODEST_TYPE_ACCOUNT_VIEW, NULL);
455         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
456         
457         g_object_ref (G_OBJECT (account_mgr));
458         priv->account_mgr = account_mgr;
459
460         init_view (MODEST_ACCOUNT_VIEW (obj));
461         update_account_view (account_mgr, MODEST_ACCOUNT_VIEW (obj));
462         
463         return MODEST_ACCOUNT_VIEW (obj);
464 }
465
466 gchar *
467 modest_account_view_get_selected_account (ModestAccountView *self)
468 {
469         gchar *account_name = NULL;
470         GtkTreeSelection *sel;
471         GtkTreeModel *model;
472         GtkTreeIter iter;
473
474         g_return_val_if_fail (MODEST_IS_ACCOUNT_VIEW (self), NULL);
475         
476         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
477         if (gtk_tree_selection_get_selected (sel, &model, &iter)) {
478                 gtk_tree_model_get (model, &iter, 
479                                     MODEST_ACCOUNT_VIEW_NAME_COLUMN, 
480                                     &account_name, -1);
481         }
482
483         return account_name;
484 }
485
486 /* This allows us to pass more than one piece of data to the signal handler,
487  * and get a result: */
488 typedef struct 
489 {
490                 ModestAccountView* self;
491                 const gchar *account_name;
492 } ForEachData;
493
494 static gboolean
495 on_model_foreach_select_account(GtkTreeModel *model, 
496         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
497 {
498         ForEachData *state = (ForEachData*)(user_data);
499         
500         /* Select the item if it has the matching account name: */
501         gchar *this_account_name = NULL;
502         gtk_tree_model_get (model, iter, 
503                 MODEST_ACCOUNT_VIEW_NAME_COLUMN, &this_account_name, 
504                 -1); 
505         if(this_account_name && state->account_name 
506                 && (strcmp (this_account_name, state->account_name) == 0)) {
507                 
508                 GtkTreeSelection *selection = 
509                         gtk_tree_view_get_selection (GTK_TREE_VIEW (state->self));
510                 gtk_tree_selection_select_iter (selection, iter);
511                 
512                 return TRUE; /* Stop walking the tree. */
513         }
514         
515         return FALSE; /* Keep walking the tree. */
516 }
517
518 void modest_account_view_select_account (ModestAccountView *account_view, 
519         const gchar* account_name)
520 {       
521         /* Create a state instance so we can send two items of data to the signal handler: */
522         ForEachData *state = g_new0 (ForEachData, 1);
523         state->self = account_view;
524         state->account_name = account_name;
525         
526         GtkTreeModel *model = gtk_tree_view_get_model (
527                 GTK_TREE_VIEW (account_view));
528         gtk_tree_model_foreach (model, 
529                 on_model_foreach_select_account, state);
530                 
531         g_free (state);
532 }
533
534
535
536 void modest_account_view_block_conf_updates (ModestAccountView *account_view)
537 {
538         ModestAccountViewPrivate* priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(account_view);
539         priv->block_conf_updates = TRUE;
540 }
541
542 void modest_account_view_unblock_conf_updates (ModestAccountView *account_view)
543 {
544         ModestAccountViewPrivate* priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(account_view);
545         priv->block_conf_updates = FALSE;
546         
547         update_account_view (modest_runtime_get_account_mgr(), account_view);
548 }