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