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