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