24e735bd6048a3e9f3ac13ba786799383e91bb52
[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 }
127
128 static void
129 modest_account_view_finalize (GObject *obj)
130 {
131         ModestAccountViewPrivate *priv;
132
133         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
134
135         if (priv->account_mgr) {
136                 g_object_unref (G_OBJECT(priv->account_mgr));
137                 priv->account_mgr = NULL; 
138         }
139         
140         G_OBJECT_CLASS(parent_class)->finalize (obj);
141 }
142
143
144
145 static void
146 update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
147 {
148         GSList *account_names, *cursor;
149         GtkListStore *model;
150                 
151         model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(view)));  
152         gtk_list_store_clear (model);
153
154         cursor = account_names =
155                 modest_account_mgr_account_names (account_mgr, NULL);
156
157         while (cursor) {
158                 gchar *account_name;
159                 ModestAccountData *account_data;
160                 
161                 account_name = (gchar*)cursor->data;
162                 
163                 account_data = modest_account_mgr_get_account_data (account_mgr, account_name);
164                 if (!account_data) {
165                         g_printerr ("modest: failed to get account data for %s\n", account_name);
166                         continue;
167                 }
168
169                 /* don't display accounts without stores */
170                 if (account_data->store_account) {
171                         
172                         time_t last_updated; 
173                         gchar *last_updated_string;
174                         
175                         /* FIXME: let's assume that 'last update' applies to the store account... */
176                         last_updated = account_data->store_account->last_updated;
177                         if (last_updated > 0) 
178                                 last_updated_string = modest_text_utils_get_display_date(last_updated);
179                         else
180                                 last_updated_string = g_strdup (_("Never"));
181                         
182                         gtk_list_store_insert_with_values (
183                                 model, NULL, 0,
184                                 MODEST_ACCOUNT_VIEW_NAME_COLUMN,          account_name,
185                                 MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN,  account_data->display_name,
186                                 MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN,    account_data->is_enabled,
187                                 MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,    account_data->is_default,
188
189                                 MODEST_ACCOUNT_VIEW_PROTO_COLUMN,
190                                 modest_protocol_info_get_protocol_name  (account_data->store_account->proto),
191
192                                 MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN,  last_updated_string,
193                                 -1);
194                         g_free (last_updated_string);
195                 }
196
197                 modest_account_mgr_free_account_data (account_mgr, account_data);
198                 cursor = cursor->next;
199         }
200         g_slist_free (account_names);
201 }
202
203
204 static void
205 on_account_changed (ModestAccountMgr *account_mgr,
206                     const gchar* account, const gchar* key,
207                     gboolean server_account, ModestAccountView *self)
208 {       
209         update_account_view (account_mgr, self);
210 }
211
212
213 static void
214 on_account_removed (ModestAccountMgr *account_mgr,
215                     const gchar* account, gboolean server_account,
216                     ModestAccountView *self)
217 {
218         on_account_changed (account_mgr, account, NULL, server_account, self);
219 }
220
221
222
223
224 static void
225 on_account_enable_toggled (GtkCellRendererToggle *cell_renderer, gchar *path,
226                            ModestAccountView *self)
227 {
228         GtkTreeIter iter;
229         ModestAccountViewPrivate *priv;
230         GtkTreeModel *model;
231         gchar *account_name;
232         gboolean enabled;
233         
234         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
235         model = gtk_tree_view_get_model (GTK_TREE_VIEW(self));
236         
237         if (!gtk_tree_model_get_iter_from_string (model, &iter, path)) {
238                 g_printerr ("modest: cannot find iterator\n");
239                 return;
240         }
241         gtk_tree_model_get (model, &iter, MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN, &enabled,
242                             MODEST_ACCOUNT_VIEW_NAME_COLUMN, &account_name,
243                             -1);
244         
245         /* toggle enabled / disabled */
246         modest_account_mgr_set_enabled (priv->account_mgr, account_name, !enabled);
247         g_free (account_name);
248 }
249
250
251 void
252 bold_if_default_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
253                             GtkTreeModel *tree_model,  GtkTreeIter *iter,  gpointer user_data)
254 {
255         gboolean is_default;
256         gtk_tree_model_get (tree_model, iter, MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,
257                             &is_default, -1);
258         g_object_set (G_OBJECT(renderer),
259                       "weight", is_default ? 800: 400,
260                       NULL);
261 }
262
263
264 static void
265 init_view (ModestAccountView *self)
266 {
267         ModestAccountViewPrivate *priv;
268         GtkCellRenderer *toggle_renderer, *text_renderer;
269         GtkListStore *model;
270         GtkTreeViewColumn *column;
271         
272         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
273                 
274         model = gtk_list_store_new (6,
275                                     G_TYPE_STRING,  /* account name */
276                                     G_TYPE_STRING,  /* account display name */
277                                     G_TYPE_BOOLEAN, /* is-enabled */
278                                     G_TYPE_BOOLEAN, /* is-default */
279                                     G_TYPE_STRING,  /* account proto (pop, imap,...) */
280                                     G_TYPE_STRING   /* last updated (time_t) */
281                 ); 
282
283         gtk_tree_view_set_model (GTK_TREE_VIEW(self), GTK_TREE_MODEL(model));
284
285         toggle_renderer = gtk_cell_renderer_toggle_new ();
286         text_renderer = gtk_cell_renderer_text_new ();
287
288         /* the is_enabled column */
289         g_object_set (G_OBJECT(toggle_renderer), "activatable", TRUE,"radio", FALSE, NULL);
290         g_signal_connect (G_OBJECT(toggle_renderer), "toggled", G_CALLBACK(on_account_enable_toggled),
291                           self);
292         gtk_tree_view_append_column (GTK_TREE_VIEW(self),
293                                      gtk_tree_view_column_new_with_attributes (
294                                              _("Enabled"), toggle_renderer,
295                                              "active", MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN, NULL));
296         
297         /* account name */
298         column =  gtk_tree_view_column_new_with_attributes (_("Account"), text_renderer,"text",
299                                                             MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN, NULL);
300         gtk_tree_view_append_column (GTK_TREE_VIEW(self),column);
301         gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_cell_data,
302                                                 NULL, NULL);
303
304         /* account type */
305         column =  gtk_tree_view_column_new_with_attributes (_("Type"), text_renderer,"text",
306                                                             MODEST_ACCOUNT_VIEW_PROTO_COLUMN, NULL);
307         gtk_tree_view_append_column (GTK_TREE_VIEW(self),column);
308         gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_cell_data,
309                                                 NULL, NULL);
310
311         /* last update for this account */
312         column =  gtk_tree_view_column_new_with_attributes (_("Last update"), text_renderer,"text",
313                                                             MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, NULL);
314         gtk_tree_view_append_column (GTK_TREE_VIEW(self),column);
315         gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_cell_data,
316                                                 NULL, NULL);
317
318         g_signal_connect (G_OBJECT(priv->account_mgr),"account_removed",
319                                        G_CALLBACK(on_account_removed), self);
320         g_signal_connect (G_OBJECT(priv->account_mgr), "account_changed",
321                                        G_CALLBACK(on_account_changed), self);
322 }
323
324
325
326 ModestAccountView*
327 modest_account_view_new (ModestAccountMgr *account_mgr)
328 {
329         GObject *obj;
330         ModestAccountViewPrivate *priv;
331         
332         g_return_val_if_fail (account_mgr, NULL);
333         
334         obj  = g_object_new(MODEST_TYPE_ACCOUNT_VIEW, NULL);
335         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
336         
337         g_object_ref (G_OBJECT(account_mgr));
338         priv->account_mgr = account_mgr;
339
340         init_view (MODEST_ACCOUNT_VIEW (obj));
341         update_account_view (account_mgr, MODEST_ACCOUNT_VIEW(obj));
342         
343         return MODEST_ACCOUNT_VIEW(obj);
344 }
345
346 const gchar *
347 modest_account_view_get_selected_account (ModestAccountView *self)
348 {
349         const gchar *account_name = NULL;
350         GtkTreeSelection *sel;
351         GtkTreeModel *model;
352         GtkTreeIter iter;
353
354         g_return_val_if_fail (MODEST_IS_ACCOUNT_VIEW (self), NULL);
355         
356         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
357         if (gtk_tree_selection_get_selected (sel, &model, &iter)) {
358                 gtk_tree_model_get (model, &iter, MODEST_ACCOUNT_VIEW_NAME_COLUMN, &account_name,
359                                     -1);
360         }
361
362         return account_name;
363 }