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