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) {
173 gchar *last_updated_string;
175 /* FIXME: let's assume that 'last update' applies to the store account... */
176 last_updated = account_data->store_account->last_updated;
177 if (last_updated > 0)
178 last_updated_string = modest_text_utils_get_display_date(last_updated);
180 last_updated_string = g_strdup (_("Never"));
182 gtk_list_store_insert_with_values (
184 MODEST_ACCOUNT_VIEW_NAME_COLUMN, account_name,
185 MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN, account_data->display_name,
186 MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN, account_data->is_enabled,
187 MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, account_data->is_default,
189 MODEST_ACCOUNT_VIEW_PROTO_COLUMN,
190 modest_protocol_info_get_protocol_name (account_data->store_account->proto),
192 MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, last_updated_string,
194 g_free (last_updated_string);
197 modest_account_mgr_free_account_data (account_mgr, account_data);
198 cursor = cursor->next;
200 g_slist_free (account_names);
205 on_account_changed (ModestAccountMgr *account_mgr,
206 const gchar* account, const gchar* key,
207 gboolean server_account, ModestAccountView *self)
209 update_account_view (account_mgr, self);
214 on_account_removed (ModestAccountMgr *account_mgr,
215 const gchar* account, gboolean server_account,
216 ModestAccountView *self)
218 on_account_changed (account_mgr, account, NULL, server_account, self);
225 on_account_enable_toggled (GtkCellRendererToggle *cell_renderer, gchar *path,
226 ModestAccountView *self)
229 ModestAccountViewPrivate *priv;
234 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
235 model = gtk_tree_view_get_model (GTK_TREE_VIEW(self));
237 if (!gtk_tree_model_get_iter_from_string (model, &iter, path)) {
238 g_printerr ("modest: cannot find iterator\n");
241 gtk_tree_model_get (model, &iter, MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN, &enabled,
242 MODEST_ACCOUNT_VIEW_NAME_COLUMN, &account_name,
245 /* toggle enabled / disabled */
246 modest_account_mgr_set_enabled (priv->account_mgr, account_name, !enabled);
247 g_free (account_name);
252 bold_if_default_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
253 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer user_data)
256 gtk_tree_model_get (tree_model, iter, MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,
258 g_object_set (G_OBJECT(renderer),
259 "weight", is_default ? 800: 400,
265 init_view (ModestAccountView *self)
267 ModestAccountViewPrivate *priv;
268 GtkCellRenderer *toggle_renderer, *text_renderer;
270 GtkTreeViewColumn *column;
272 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
274 model = gtk_list_store_new (6,
275 G_TYPE_STRING, /* account name */
276 G_TYPE_STRING, /* account display name */
277 G_TYPE_BOOLEAN, /* is-enabled */
278 G_TYPE_BOOLEAN, /* is-default */
279 G_TYPE_STRING, /* account proto (pop, imap,...) */
280 G_TYPE_STRING /* last updated (time_t) */
283 gtk_tree_view_set_model (GTK_TREE_VIEW(self), GTK_TREE_MODEL(model));
285 toggle_renderer = gtk_cell_renderer_toggle_new ();
286 text_renderer = gtk_cell_renderer_text_new ();
288 /* the is_enabled column */
289 g_object_set (G_OBJECT(toggle_renderer), "activatable", TRUE,"radio", FALSE, NULL);
290 g_signal_connect (G_OBJECT(toggle_renderer), "toggled", G_CALLBACK(on_account_enable_toggled),
292 gtk_tree_view_append_column (GTK_TREE_VIEW(self),
293 gtk_tree_view_column_new_with_attributes (
294 _("Enabled"), toggle_renderer,
295 "active", MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN, NULL));
298 column = gtk_tree_view_column_new_with_attributes (_("Account"), text_renderer,"text",
299 MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN, NULL);
300 gtk_tree_view_append_column (GTK_TREE_VIEW(self),column);
301 gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_cell_data,
305 column = gtk_tree_view_column_new_with_attributes (_("Type"), text_renderer,"text",
306 MODEST_ACCOUNT_VIEW_PROTO_COLUMN, NULL);
307 gtk_tree_view_append_column (GTK_TREE_VIEW(self),column);
308 gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_cell_data,
311 /* last update for this account */
312 column = gtk_tree_view_column_new_with_attributes (_("Last update"), text_renderer,"text",
313 MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, NULL);
314 gtk_tree_view_append_column (GTK_TREE_VIEW(self),column);
315 gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_cell_data,
318 g_signal_connect (G_OBJECT(priv->account_mgr),"account_removed",
319 G_CALLBACK(on_account_removed), self);
320 g_signal_connect (G_OBJECT(priv->account_mgr), "account_changed",
321 G_CALLBACK(on_account_changed), self);
327 modest_account_view_new (ModestAccountMgr *account_mgr)
330 ModestAccountViewPrivate *priv;
332 g_return_val_if_fail (account_mgr, NULL);
334 obj = g_object_new(MODEST_TYPE_ACCOUNT_VIEW, NULL);
335 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
337 g_object_ref (G_OBJECT(account_mgr));
338 priv->account_mgr = account_mgr;
340 init_view (MODEST_ACCOUNT_VIEW (obj));
341 update_account_view (account_mgr, MODEST_ACCOUNT_VIEW(obj));
343 return MODEST_ACCOUNT_VIEW(obj);
347 modest_account_view_get_selected_account (ModestAccountView *self)
349 const gchar *account_name = NULL;
350 GtkTreeSelection *sel;
354 g_return_val_if_fail (MODEST_IS_ACCOUNT_VIEW (self), NULL);
356 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
357 if (gtk_tree_selection_get_selected (sel, &model, &iter)) {
358 gtk_tree_model_get (model, &iter, MODEST_ACCOUNT_VIEW_NAME_COLUMN, &account_name,