1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
30 #include <glib/gi18n.h>
31 #include "modest-account-view.h"
32 /* include other impl specific header files */
34 /* 'private'/'protected' functions */
35 static void modest_account_view_class_init (ModestAccountViewClass *klass);
36 static void modest_account_view_init (ModestAccountView *obj);
37 static void modest_account_view_finalize (GObject *obj);
40 enum _AccountViewColumns {
46 typedef enum _AccountViewColumns AccountViewColumns;
56 typedef struct _ModestAccountViewPrivate ModestAccountViewPrivate;
57 struct _ModestAccountViewPrivate {
58 ModestAccountMgr *account_mgr;
62 #define MODEST_ACCOUNT_VIEW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
63 MODEST_TYPE_ACCOUNT_VIEW, \
64 ModestAccountViewPrivate))
66 static GtkTreeViewClass *parent_class = NULL;
68 /* uncomment the following if you have defined any signals */
69 /* static guint signals[LAST_SIGNAL] = {0}; */
72 modest_account_view_get_type (void)
74 static GType my_type = 0;
76 static const GTypeInfo my_info = {
77 sizeof(ModestAccountViewClass),
79 NULL, /* base finalize */
80 (GClassInitFunc) modest_account_view_class_init,
81 NULL, /* class finalize */
82 NULL, /* class data */
83 sizeof(ModestAccountView),
85 (GInstanceInitFunc) modest_account_view_init,
88 my_type = g_type_register_static (GTK_TYPE_TREE_VIEW,
96 modest_account_view_class_init (ModestAccountViewClass *klass)
98 GObjectClass *gobject_class;
99 gobject_class = (GObjectClass*) klass;
101 parent_class = g_type_class_peek_parent (klass);
102 gobject_class->finalize = modest_account_view_finalize;
104 g_type_class_add_private (gobject_class, sizeof(ModestAccountViewPrivate));
106 /* signal definitions go here, e.g.: */
107 /* signals[MY_SIGNAL_1] = */
108 /* g_signal_new ("my_signal_1",....); */
109 /* signals[MY_SIGNAL_2] = */
110 /* g_signal_new ("my_signal_2",....); */
115 modest_account_view_init (ModestAccountView *obj)
117 ModestAccountViewPrivate *priv;
119 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
121 priv->account_mgr = NULL;
125 modest_account_view_finalize (GObject *obj)
127 ModestAccountViewPrivate *priv;
129 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
131 g_signal_handler_disconnect (G_OBJECT(priv->account_mgr),
133 g_signal_handler_disconnect (G_OBJECT(priv->account_mgr),
136 if (priv->account_mgr) {
137 g_object_unref (G_OBJECT(priv->account_mgr));
138 priv->account_mgr = NULL;
141 G_OBJECT_CLASS(parent_class)->finalize (obj);
147 update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
149 GSList *account_names, *cursor;
152 model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(view)));
153 gtk_list_store_clear (model);
155 cursor = account_names =
156 modest_account_mgr_account_names (account_mgr, NULL);
160 gchar *store, *account_name;
163 account_name = (gchar*)cursor->data;
165 store = modest_account_mgr_get_string (account_mgr,
167 MODEST_ACCOUNT_STORE_ACCOUNT,
171 proto = modest_account_mgr_get_string (account_mgr,
173 MODEST_ACCOUNT_PROTO,
178 enabled = modest_account_mgr_account_get_enabled (account_mgr, account_name);
179 gtk_list_store_insert_with_values (
181 ENABLED_COLUMN, enabled,
182 NAME_COLUMN, account_name,
186 g_free (account_name);
189 cursor = cursor->next;
191 g_slist_free (account_names);
196 on_account_changed (ModestAccountMgr *account_mgr,
197 const gchar* account, const gchar* key,
198 gboolean server_account, ModestAccountView *self)
200 update_account_view (account_mgr, self);
205 on_account_removed (ModestAccountMgr *account_mgr,
206 const gchar* account, gboolean server_account,
207 ModestAccountView *self)
209 on_account_changed (account_mgr, account, NULL, server_account, self);
216 on_account_enable_toggled (GtkCellRendererToggle *cell_renderer, gchar *path,
217 ModestAccountView *self)
220 ModestAccountViewPrivate *priv;
225 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
226 model = gtk_tree_view_get_model (GTK_TREE_VIEW(self));
228 if (!gtk_tree_model_get_iter_from_string (model, &iter, path)) {
229 g_printerr ("modest: cannot find iterator\n");
233 gtk_tree_model_get (model, &iter, ENABLED_COLUMN, &enabled,
234 NAME_COLUMN, &account_name,
237 /* toggle enabled / disabled */
238 modest_account_mgr_account_set_enabled (priv->account_mgr, account_name, !enabled);
239 g_free (account_name);
244 init_view (ModestAccountView *self)
246 ModestAccountViewPrivate *priv;
247 GtkTreeSelection *sel;
248 GtkCellRenderer *renderer;
251 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
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,...) */
258 gtk_tree_view_set_model (GTK_TREE_VIEW(self), GTK_TREE_MODEL(model));
260 renderer = gtk_cell_renderer_toggle_new ();
261 g_object_set (G_OBJECT(renderer), "activatable", TRUE,"radio", FALSE, NULL);
263 g_signal_connect (G_OBJECT(renderer), "toggled",
264 G_CALLBACK(on_account_enable_toggled),
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 (
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 (
279 gtk_cell_renderer_text_new (),
280 "text", PROTO_COLUMN, NULL));
282 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
284 priv->sig1 = g_signal_connect (G_OBJECT(priv->account_mgr),
286 G_CALLBACK(on_account_removed), self);
287 priv->sig2 = g_signal_connect (G_OBJECT(priv->account_mgr),
289 G_CALLBACK(on_account_changed), self);
295 modest_account_view_new (ModestAccountMgr *account_mgr)
298 ModestAccountViewPrivate *priv;
300 g_return_val_if_fail (account_mgr, NULL);
302 obj = g_object_new(MODEST_TYPE_ACCOUNT_VIEW, NULL);
303 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
305 g_object_ref (G_OBJECT(account_mgr));
306 priv->account_mgr = account_mgr;
308 init_view (MODEST_ACCOUNT_VIEW (obj));
309 update_account_view (account_mgr, MODEST_ACCOUNT_VIEW(obj));
311 return MODEST_ACCOUNT_VIEW(obj);