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