f190fcec4ab3ed2d5eee0cd19b5d290bc6bc0988
[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 "modest-account-view.h"
31 /* include other impl specific header files */
32
33 /* 'private'/'protected' functions */
34 static void modest_account_view_class_init    (ModestAccountViewClass *klass);
35 static void modest_account_view_init          (ModestAccountView *obj);
36 static void modest_account_view_finalize      (GObject *obj);
37
38
39 enum _AccountViewColumns {
40         ENABLED_COLUMN,
41         NAME_COLUMN,
42         PROTO_COLUMN,
43         N_COLUMNS
44 };
45 typedef enum _AccountViewColumns AccountViewColumns;
46
47
48 /* list my signals */
49 enum {
50         /* MY_SIGNAL_1, */
51         /* MY_SIGNAL_2, */
52         LAST_SIGNAL
53 };
54
55 typedef struct _ModestAccountViewPrivate ModestAccountViewPrivate;
56 struct _ModestAccountViewPrivate {
57         ModestAccountMgr *account_mgr;
58         gulong sig1, sig2;
59         
60 };
61 #define MODEST_ACCOUNT_VIEW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
62                                                  MODEST_TYPE_ACCOUNT_VIEW, \
63                                                  ModestAccountViewPrivate))
64 /* globals */
65 static GtkTreeViewClass *parent_class = NULL;
66
67 /* uncomment the following if you have defined any signals */
68 /* static guint signals[LAST_SIGNAL] = {0}; */
69
70 GType
71 modest_account_view_get_type (void)
72 {
73         static GType my_type = 0;
74         if (!my_type) {
75                 static const GTypeInfo my_info = {
76                         sizeof(ModestAccountViewClass),
77                         NULL,           /* base init */
78                         NULL,           /* base finalize */
79                         (GClassInitFunc) modest_account_view_class_init,
80                         NULL,           /* class finalize */
81                         NULL,           /* class data */
82                         sizeof(ModestAccountView),
83                         1,              /* n_preallocs */
84                         (GInstanceInitFunc) modest_account_view_init,
85                 };
86                 my_type = g_type_register_static (GTK_TYPE_TREE_VIEW,
87                                                   "ModestAccountView",
88                                                   &my_info, 0);
89         }
90         return my_type;
91 }
92
93 static void
94 modest_account_view_class_init (ModestAccountViewClass *klass)
95 {
96         GObjectClass *gobject_class;
97         gobject_class = (GObjectClass*) klass;
98
99         parent_class            = g_type_class_peek_parent (klass);
100         gobject_class->finalize = modest_account_view_finalize;
101
102         g_type_class_add_private (gobject_class, sizeof(ModestAccountViewPrivate));
103
104         /* signal definitions go here, e.g.: */
105 /*      signals[MY_SIGNAL_1] = */
106 /*              g_signal_new ("my_signal_1",....); */
107 /*      signals[MY_SIGNAL_2] = */
108 /*              g_signal_new ("my_signal_2",....); */
109 /*      etc. */
110 }
111
112 static void
113 modest_account_view_init (ModestAccountView *obj)
114 {
115         ModestAccountViewPrivate *priv;
116
117         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
118         
119         priv->account_mgr = NULL; 
120 }
121
122 static void
123 modest_account_view_finalize (GObject *obj)
124 {
125         ModestAccountViewPrivate *priv;
126
127         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
128
129         g_signal_handler_disconnect (G_OBJECT(priv->account_mgr),
130                                      priv->sig1);
131         g_signal_handler_disconnect (G_OBJECT(priv->account_mgr),
132                                      priv->sig2);
133
134         if (priv->account_mgr) {
135                 g_object_unref (G_OBJECT(priv->account_mgr));
136                 priv->account_mgr = NULL; 
137         }
138         
139         G_OBJECT_CLASS(parent_class)->finalize (obj);
140 }
141
142
143
144 static void
145 update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
146 {
147         GSList *account_names, *cursor;
148         GtkListStore *model;
149                 
150         model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(view)));  
151         gtk_list_store_clear (model);
152
153         cursor = account_names =
154                 modest_account_mgr_account_names (account_mgr, NULL);
155
156         while (cursor) {
157                 gchar    *proto = NULL;
158                 gchar    *store, *account_name;
159                 gboolean enabled;
160
161                 account_name = (gchar*)cursor->data;
162                 
163                 store        = modest_account_mgr_get_string (account_mgr,
164                                                               account_name,
165                                                               MODEST_ACCOUNT_STORE_ACCOUNT,
166                                                               FALSE, NULL);
167                 if (store) {
168
169                         proto = modest_account_mgr_get_string (account_mgr,
170                                                                store,
171                                                                MODEST_ACCOUNT_PROTO,
172                                                                TRUE, NULL);
173                         g_free(store);
174                 }
175                 
176                 enabled = modest_account_mgr_account_get_enabled (account_mgr, account_name);
177                 gtk_list_store_insert_with_values (
178                         model, NULL, 0,
179                         ENABLED_COLUMN, enabled,
180                         NAME_COLUMN,  account_name,
181                         PROTO_COLUMN, proto,
182                         -1);
183                 
184                 g_free (account_name);
185                 g_free (proto);
186                 
187                 cursor = cursor->next;
188         }
189         g_slist_free (account_names);
190 }
191
192
193 static void
194 on_account_changed (ModestAccountMgr *account_mgr,
195                     const gchar* account, const gchar* key,
196                     gboolean server_account, ModestAccountView *self)
197 {       
198         update_account_view (account_mgr, self);
199 }
200
201
202 static void
203 on_account_removed (ModestAccountMgr *account_mgr,
204                     const gchar* account, gboolean server_account,
205                     ModestAccountView *self)
206 {
207         on_account_changed (account_mgr, account, NULL, server_account, self);
208 }
209
210
211
212
213 static void
214 on_account_enable_toggled (GtkCellRendererToggle *cell_renderer, gchar *path,
215                            ModestAccountView *self)
216 {
217         GtkTreeIter iter;
218         ModestAccountViewPrivate *priv;
219         GtkTreeModel *model;
220         gchar *account_name;
221         gboolean enabled;
222         
223         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
224         model = gtk_tree_view_get_model (GTK_TREE_VIEW(self));
225         
226         if (!gtk_tree_model_get_iter_from_string (model, &iter, path)) {
227                 g_printerr ("modest: cannot find iterator\n");
228                 return;
229         }
230         
231         gtk_tree_model_get (model, &iter, ENABLED_COLUMN, &enabled,
232                             NAME_COLUMN, &account_name,
233                             -1);
234         
235         /* toggle enabled / disabled */
236         modest_account_mgr_account_set_enabled (priv->account_mgr, account_name, !enabled);
237         g_free (account_name);
238 }
239
240
241 static void
242 init_view (ModestAccountView *self)
243 {
244         ModestAccountViewPrivate *priv;
245         GtkTreeSelection *sel;
246         GtkCellRenderer *renderer;
247         GtkListStore *model;
248         
249         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
250                 
251         model = gtk_list_store_new (3,
252                                     G_TYPE_BOOLEAN, /* checkbox */
253                                     G_TYPE_STRING,  /* account name */
254                                     G_TYPE_STRING); /* account type (pop, imap,...) */
255
256         gtk_tree_view_set_model (GTK_TREE_VIEW(self), GTK_TREE_MODEL(model));
257
258         renderer = gtk_cell_renderer_toggle_new ();
259         g_object_set (G_OBJECT(renderer), "activatable", TRUE,"radio", FALSE, NULL);
260
261         g_signal_connect (G_OBJECT(renderer), "toggled",
262                           G_CALLBACK(on_account_enable_toggled),
263                           self);
264         
265         gtk_tree_view_append_column (GTK_TREE_VIEW(self),
266                                      gtk_tree_view_column_new_with_attributes (
267                                              _("Enabled"), renderer,
268                                              "active", ENABLED_COLUMN, NULL));
269         gtk_tree_view_append_column (GTK_TREE_VIEW(self),
270                                      gtk_tree_view_column_new_with_attributes (
271                                              _("Account"),
272                                              gtk_cell_renderer_text_new (),
273                                              "text", NAME_COLUMN, NULL));
274         gtk_tree_view_append_column (GTK_TREE_VIEW(self),
275                                      gtk_tree_view_column_new_with_attributes (
276                                              _("Type"),
277                                              gtk_cell_renderer_text_new (),
278                                              "text", PROTO_COLUMN, NULL));
279
280         sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
281         
282         priv->sig1 = g_signal_connect (G_OBJECT(priv->account_mgr),
283                                        "account_removed",
284                                        G_CALLBACK(on_account_removed), self);
285         priv->sig2 = g_signal_connect (G_OBJECT(priv->account_mgr),
286                                        "account_changed",
287                                        G_CALLBACK(on_account_changed), self);
288 }
289
290
291
292 ModestAccountView*
293 modest_account_view_new (ModestAccountMgr *account_mgr)
294 {
295         GObject *obj;
296         ModestAccountViewPrivate *priv;
297         
298         g_return_val_if_fail (account_mgr, NULL);
299         
300         obj  = g_object_new(MODEST_TYPE_ACCOUNT_VIEW, NULL);
301         priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
302         
303         g_object_ref (G_OBJECT(account_mgr));
304         priv->account_mgr = account_mgr;
305
306         init_view (MODEST_ACCOUNT_VIEW (obj));
307         update_account_view (account_mgr, MODEST_ACCOUNT_VIEW(obj));
308         
309         return MODEST_ACCOUNT_VIEW(obj);
310 }
311