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