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