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