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"
33 #include <modest-account-mgr.h>
34 #include <modest-account-mgr-helpers.h>
35 #include <modest-text-utils.h>
37 #include <gtk/gtkcellrenderertoggle.h>
38 #include <gtk/gtkcellrenderertext.h>
39 #include <gtk/gtktreeselection.h>
40 #include <gtk/gtkliststore.h>
42 /* 'private'/'protected' functions */
43 static void modest_account_view_class_init (ModestAccountViewClass *klass);
44 static void modest_account_view_init (ModestAccountView *obj);
45 static void modest_account_view_finalize (GObject *obj);
49 MODEST_ACCOUNT_VIEW_NAME_COLUMN,
50 MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN,
51 MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN,
52 MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,
53 MODEST_ACCOUNT_VIEW_PROTO_COLUMN,
54 MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN,
56 MODEST_ACCOUNT_VIEW_COLUMN_NUM
67 typedef struct _ModestAccountViewPrivate ModestAccountViewPrivate;
68 struct _ModestAccountViewPrivate {
69 ModestAccountMgr *account_mgr;
73 #define MODEST_ACCOUNT_VIEW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
74 MODEST_TYPE_ACCOUNT_VIEW, \
75 ModestAccountViewPrivate))
77 static GtkTreeViewClass *parent_class = NULL;
79 /* uncomment the following if you have defined any signals */
80 /* static guint signals[LAST_SIGNAL] = {0}; */
83 modest_account_view_get_type (void)
85 static GType my_type = 0;
87 static const GTypeInfo my_info = {
88 sizeof(ModestAccountViewClass),
90 NULL, /* base finalize */
91 (GClassInitFunc) modest_account_view_class_init,
92 NULL, /* class finalize */
93 NULL, /* class data */
94 sizeof(ModestAccountView),
96 (GInstanceInitFunc) modest_account_view_init,
99 my_type = g_type_register_static (GTK_TYPE_TREE_VIEW,
107 modest_account_view_class_init (ModestAccountViewClass *klass)
109 GObjectClass *gobject_class;
110 gobject_class = (GObjectClass*) klass;
112 parent_class = g_type_class_peek_parent (klass);
113 gobject_class->finalize = modest_account_view_finalize;
115 g_type_class_add_private (gobject_class, sizeof(ModestAccountViewPrivate));
119 modest_account_view_init (ModestAccountView *obj)
121 ModestAccountViewPrivate *priv;
123 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
125 priv->account_mgr = NULL;
129 modest_account_view_finalize (GObject *obj)
131 ModestAccountViewPrivate *priv;
133 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
135 if (priv->account_mgr) {
136 g_object_unref (G_OBJECT(priv->account_mgr));
137 priv->account_mgr = NULL;
140 G_OBJECT_CLASS(parent_class)->finalize (obj);
146 update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
148 GSList *account_names, *cursor;
151 model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(view)));
152 gtk_list_store_clear (model);
154 cursor = account_names = modest_account_mgr_account_names (account_mgr);
158 ModestAccountData *account_data;
160 account_name = (gchar*)cursor->data;
162 account_data = modest_account_mgr_get_account_data (account_mgr, account_name);
164 g_printerr ("modest: failed to get account data for %s\n", account_name);
168 /* don't display accounts without stores */
169 if (account_data->store_account) {
172 gchar *last_updated_string;
174 /* FIXME: let's assume that 'last update' applies to the store account... */
175 last_updated = account_data->store_account->last_updated;
176 if (last_updated > 0)
177 last_updated_string = modest_text_utils_get_display_date(last_updated);
179 last_updated_string = g_strdup (_("Never"));
181 gtk_list_store_insert_with_values (
183 MODEST_ACCOUNT_VIEW_NAME_COLUMN, account_name,
184 MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN, account_data->display_name,
185 MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN, account_data->is_enabled,
186 MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, account_data->is_default,
188 MODEST_ACCOUNT_VIEW_PROTO_COLUMN,
189 modest_protocol_info_get_protocol_name (account_data->store_account->proto),
191 MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, last_updated_string,
193 g_free (last_updated_string);
196 modest_account_mgr_free_account_data (account_mgr, account_data);
197 cursor = cursor->next;
199 g_slist_free (account_names);
204 on_account_changed (ModestAccountMgr *account_mgr,
205 const gchar* account, const gchar* key,
206 gboolean server_account, ModestAccountView *self)
208 update_account_view (account_mgr, self);
213 on_account_removed (ModestAccountMgr *account_mgr,
214 const gchar* account, gboolean server_account,
215 ModestAccountView *self)
217 on_account_changed (account_mgr, account, NULL, server_account, self);
224 on_account_enable_toggled (GtkCellRendererToggle *cell_renderer, gchar *path,
225 ModestAccountView *self)
228 ModestAccountViewPrivate *priv;
233 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
234 model = gtk_tree_view_get_model (GTK_TREE_VIEW(self));
236 if (!gtk_tree_model_get_iter_from_string (model, &iter, path)) {
237 g_printerr ("modest: cannot find iterator\n");
240 gtk_tree_model_get (model, &iter, MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN, &enabled,
241 MODEST_ACCOUNT_VIEW_NAME_COLUMN, &account_name,
244 /* toggle enabled / disabled */
245 modest_account_mgr_set_enabled (priv->account_mgr, account_name, !enabled);
246 g_free (account_name);
251 bold_if_default_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
252 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer user_data)
255 gtk_tree_model_get (tree_model, iter, MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,
257 g_object_set (G_OBJECT(renderer),
258 "weight", is_default ? 800: 400,
264 init_view (ModestAccountView *self)
266 ModestAccountViewPrivate *priv;
267 GtkCellRenderer *toggle_renderer, *text_renderer;
269 GtkTreeViewColumn *column;
271 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
273 model = gtk_list_store_new (6,
274 G_TYPE_STRING, /* account name */
275 G_TYPE_STRING, /* account display name */
276 G_TYPE_BOOLEAN, /* is-enabled */
277 G_TYPE_BOOLEAN, /* is-default */
278 G_TYPE_STRING, /* account proto (pop, imap,...) */
279 G_TYPE_STRING /* last updated (time_t) */
282 gtk_tree_view_set_model (GTK_TREE_VIEW(self), GTK_TREE_MODEL(model));
284 toggle_renderer = gtk_cell_renderer_toggle_new ();
285 text_renderer = gtk_cell_renderer_text_new ();
287 /* the is_enabled column */
288 g_object_set (G_OBJECT(toggle_renderer), "activatable", TRUE,"radio", FALSE, NULL);
289 g_signal_connect (G_OBJECT(toggle_renderer), "toggled", G_CALLBACK(on_account_enable_toggled),
291 gtk_tree_view_append_column (GTK_TREE_VIEW(self),
292 gtk_tree_view_column_new_with_attributes (
293 _("Enabled"), toggle_renderer,
294 "active", MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN, NULL));
297 column = gtk_tree_view_column_new_with_attributes (_("Account"), text_renderer,"text",
298 MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN, NULL);
299 gtk_tree_view_append_column (GTK_TREE_VIEW(self),column);
300 gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_cell_data,
304 column = gtk_tree_view_column_new_with_attributes (_("Type"), text_renderer,"text",
305 MODEST_ACCOUNT_VIEW_PROTO_COLUMN, NULL);
306 gtk_tree_view_append_column (GTK_TREE_VIEW(self),column);
307 gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_cell_data,
310 /* last update for this account */
311 column = gtk_tree_view_column_new_with_attributes (_("Last update"), text_renderer,"text",
312 MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, NULL);
313 gtk_tree_view_append_column (GTK_TREE_VIEW(self),column);
314 gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_cell_data,
317 g_signal_connect (G_OBJECT(priv->account_mgr),"account_removed",
318 G_CALLBACK(on_account_removed), self);
319 g_signal_connect (G_OBJECT(priv->account_mgr), "account_changed",
320 G_CALLBACK(on_account_changed), self);
326 modest_account_view_new (ModestAccountMgr *account_mgr)
329 ModestAccountViewPrivate *priv;
331 g_return_val_if_fail (account_mgr, NULL);
333 obj = g_object_new(MODEST_TYPE_ACCOUNT_VIEW, NULL);
334 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
336 g_object_ref (G_OBJECT(account_mgr));
337 priv->account_mgr = account_mgr;
339 init_view (MODEST_ACCOUNT_VIEW (obj));
340 update_account_view (account_mgr, MODEST_ACCOUNT_VIEW(obj));
342 return MODEST_ACCOUNT_VIEW(obj);
346 modest_account_view_get_selected_account (ModestAccountView *self)
348 const gchar *account_name = NULL;
349 GtkTreeSelection *sel;
353 g_return_val_if_fail (MODEST_IS_ACCOUNT_VIEW (self), NULL);
355 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
356 if (gtk_tree_selection_get_selected (sel, &model, &iter)) {
357 gtk_tree_model_get (model, &iter, MODEST_ACCOUNT_VIEW_NAME_COLUMN, &account_name,