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-tny-account.h>
36 #include <modest-text-utils.h>
37 #include <modest-runtime.h>
38 #include <modest-signal-mgr.h>
40 #include <gtk/gtkcellrenderertoggle.h>
41 #include <gtk/gtkcellrenderertext.h>
42 #include <gtk/gtktreeselection.h>
43 #include <gtk/gtkliststore.h>
44 #include <string.h> /* For strcmp(). */
45 #include <modest-account-mgr-helpers.h>
47 /* 'private'/'protected' functions */
48 static void modest_account_view_class_init (ModestAccountViewClass *klass);
49 static void modest_account_view_init (ModestAccountView *obj);
50 static void modest_account_view_finalize (GObject *obj);
52 static void modest_account_view_select_account (ModestAccountView *account_view,
53 const gchar* account_name);
55 static void on_default_account_changed (ModestAccountMgr *mgr,
58 static void on_display_name_changed (ModestAccountMgr *self,
62 static void modest_account_view_select_first_account (ModestAccountView *account_view);
64 static void on_account_updated (ModestAccountMgr* mgr, gchar* account_name,
68 MODEST_ACCOUNT_VIEW_NAME_COLUMN,
69 MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN,
70 MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN,
71 MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,
72 MODEST_ACCOUNT_VIEW_PROTO_COLUMN,
73 MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN,
75 MODEST_ACCOUNT_VIEW_COLUMN_NUM
78 typedef struct _ModestAccountViewPrivate ModestAccountViewPrivate;
79 struct _ModestAccountViewPrivate {
80 ModestAccountMgr *account_mgr;
85 #define MODEST_ACCOUNT_VIEW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
86 MODEST_TYPE_ACCOUNT_VIEW, \
87 ModestAccountViewPrivate))
89 static GtkTreeViewClass *parent_class = NULL;
92 modest_account_view_get_type (void)
94 static GType my_type = 0;
96 static const GTypeInfo my_info = {
97 sizeof(ModestAccountViewClass),
99 NULL, /* base finalize */
100 (GClassInitFunc) modest_account_view_class_init,
101 NULL, /* class finalize */
102 NULL, /* class data */
103 sizeof(ModestAccountView),
105 (GInstanceInitFunc) modest_account_view_init,
108 my_type = g_type_register_static (GTK_TYPE_TREE_VIEW,
116 modest_account_view_class_init (ModestAccountViewClass *klass)
118 GObjectClass *gobject_class;
119 gobject_class = (GObjectClass*) klass;
121 parent_class = g_type_class_peek_parent (klass);
122 gobject_class->finalize = modest_account_view_finalize;
124 g_type_class_add_private (gobject_class, sizeof(ModestAccountViewPrivate));
128 modest_account_view_init (ModestAccountView *obj)
130 ModestAccountViewPrivate *priv;
132 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
134 priv->sig_handlers = NULL;
138 modest_account_view_finalize (GObject *obj)
140 ModestAccountViewPrivate *priv;
142 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
144 /* Disconnect signals */
145 modest_signal_mgr_disconnect_all_and_destroy (priv->sig_handlers);
147 if (priv->account_mgr) {
148 g_object_unref (G_OBJECT(priv->account_mgr));
149 priv->account_mgr = NULL;
152 G_OBJECT_CLASS(parent_class)->finalize (obj);
155 /* Get the string for the last updated time. Result must NOT be g_freed */
157 get_last_updated_string(ModestAccountMgr* account_mgr, ModestAccountSettings *settings)
159 /* FIXME: let's assume that 'last update' applies to the store account... */
160 const gchar *last_updated_string;
161 const gchar *store_account_name;
162 const gchar *account_name;
164 ModestServerAccountSettings *server_settings;
166 server_settings = modest_account_settings_get_store_settings (settings);
167 store_account_name = modest_server_account_settings_get_account_name (server_settings);
168 last_updated = modest_account_mgr_get_last_updated (account_mgr, store_account_name);
169 g_object_unref (server_settings);
170 account_name = modest_account_settings_get_account_name (settings);
171 if (!modest_account_mgr_account_is_busy(account_mgr, account_name)) {
172 if (last_updated > 0)
173 last_updated_string = modest_text_utils_get_display_date(last_updated);
175 last_updated_string = _("mcen_va_never");
177 /* FIXME: There should be a logical name in the UI specs */
178 last_updated_string = _("mcen_va_refreshing");
181 return last_updated_string;
185 update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
187 GSList *account_names, *cursor;
190 model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(view)));
192 /* Get the ID of the currently-selected account,
193 * so we can select it again after rebuilding the list.
194 * Note that the name doesn't change even when the display name changes.
196 gchar *selected_name = modest_account_view_get_selected_account (view);
198 gtk_list_store_clear (model);
200 /* Note: We do not show disabled accounts.
201 * Of course, this means that there is no UI to enable or disable
202 * accounts. That is OK for maemo where no such feature or UI is
203 * specified, so the "enabled" property is used internally to avoid
204 * showing unfinished accounts. If a user-visible "enabled" is
205 * needed in the future, we must use a second property for the
206 * current use instead */
207 cursor = account_names = modest_account_mgr_account_names (account_mgr,
208 TRUE /* only enabled accounts. */);
212 ModestAccountSettings *settings;
213 ModestServerAccountSettings *store_settings;
215 account_name = (gchar*)cursor->data;
217 settings = modest_account_mgr_load_account_settings (account_mgr, account_name);
219 g_printerr ("modest: failed to get account data for %s\n", account_name);
222 store_settings = modest_account_settings_get_store_settings (settings);
224 /* don't display accounts without stores */
225 if (modest_server_account_settings_get_account_name (store_settings) != NULL) {
230 const gchar *last_updated_string = get_last_updated_string(account_mgr, settings);
232 if (modest_account_settings_get_enabled (settings)) {
233 const gchar *proto_name;
235 proto_name = modest_protocol_info_get_transport_store_protocol_name
236 (modest_server_account_settings_get_protocol (store_settings));
237 gtk_list_store_insert_with_values (
239 MODEST_ACCOUNT_VIEW_NAME_COLUMN, account_name,
240 MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN,
241 modest_account_settings_get_display_name (settings),
242 MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN,
243 modest_account_settings_get_enabled (settings),
244 MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,
245 modest_account_settings_get_is_default (settings),
246 MODEST_ACCOUNT_VIEW_PROTO_COLUMN, proto_name,
247 MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, last_updated_string,
252 g_object_unref (store_settings);
253 g_object_unref (settings);
254 cursor = cursor->next;
257 modest_account_mgr_free_account_names (account_names);
258 account_names = NULL;
260 /* Try to re-select the same account: */
262 modest_account_view_select_account (view, selected_name);
263 g_free (selected_name);
268 on_account_busy_changed(ModestAccountMgr *account_mgr,
269 const gchar *account_name,
271 ModestAccountView *self)
273 GtkListStore *model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(self)));
275 gboolean found = FALSE;
277 if (!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(model), &iter))
282 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter,
283 MODEST_ACCOUNT_VIEW_NAME_COLUMN,
286 if (g_str_equal(cur_name, account_name)) {
287 ModestAccountSettings* settings =
288 modest_account_mgr_load_account_settings (account_mgr, account_name);
293 const gchar* last_updated_string = get_last_updated_string(account_mgr, settings);
294 gtk_list_store_set(model, &iter,
295 MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, last_updated_string,
297 g_object_unref (settings);
302 } while (!found && gtk_tree_model_iter_next(GTK_TREE_MODEL(model), &iter));
306 on_account_inserted (TnyAccountStore *account_store,
310 ModestAccountView *self;
311 ModestAccountViewPrivate *priv;
313 g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (user_data));
315 self = MODEST_ACCOUNT_VIEW (user_data);
316 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE (self);
318 /* Do not refresh the view with transport accounts */
319 if (TNY_IS_STORE_ACCOUNT (account))
320 update_account_view (priv->account_mgr, self);
324 on_account_removed (TnyAccountStore *account_store,
328 ModestAccountView *self;
329 ModestAccountViewPrivate *priv;
331 g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (user_data));
333 self = MODEST_ACCOUNT_VIEW (user_data);
334 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE (self);
336 gchar *selected_name = modest_account_view_get_selected_account (self);
337 if (selected_name == NULL) {
338 /* we select the first account if none is selected */
339 modest_account_view_select_first_account (self);
341 g_free (selected_name);
344 update_account_view (priv->account_mgr, self);
349 on_account_changed (TnyAccountStore *account_store,
353 ModestAccountView *self = NULL;
354 ModestAccountViewPrivate *priv = NULL;
356 g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (user_data));
357 g_return_if_fail (account);
358 g_return_if_fail (TNY_IS_ACCOUNT (account));
360 self = MODEST_ACCOUNT_VIEW (user_data);
361 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE (self);
363 /* Update account view */
364 /* update_account_view (priv->account_mgr, self); */
368 on_account_default_toggled (GtkCellRendererToggle *cell_renderer,
370 ModestAccountView *self)
372 ModestAccountViewPrivate *priv;
375 gchar *account_name = NULL;
377 g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (self));
379 /* If it's active then do nothing, no need to reenable it as
381 if (gtk_cell_renderer_toggle_get_active (cell_renderer))
384 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
385 model = gtk_tree_view_get_model (GTK_TREE_VIEW(self));
386 gtk_tree_model_get_iter_from_string (model, &iter, path);
388 gtk_tree_model_get (model, &iter,
389 MODEST_ACCOUNT_VIEW_NAME_COLUMN,
392 /* Set this previously-non-default account as the
393 default. We're not updating here the value of the
394 DEFAULT_COLUMN because we'll do it in the
395 "default_account_changed" signal handler. We do it like
396 this because that way the signal handler is useful also
397 when we're inserting a new account and there is no other
398 one defined, in that case the change of account is provoked
399 by the account mgr and not by a signal toggle.*/
400 modest_account_mgr_set_default_account (priv->account_mgr, account_name);
402 g_free (account_name);
406 on_account_updated (ModestAccountMgr* mgr,
410 update_account_view (mgr, MODEST_ACCOUNT_VIEW (user_data));
414 bold_if_default_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
415 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer user_data)
418 gtk_tree_model_get (tree_model, iter, MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,
420 g_object_set (G_OBJECT(renderer),
421 "weight", is_default ? 800: 400,
426 init_view (ModestAccountView *self)
428 ModestAccountViewPrivate *priv;
429 GtkCellRenderer *toggle_renderer, *text_renderer;
431 GtkTreeViewColumn *column;
433 g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (self));
434 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
436 model = gtk_list_store_new (6,
437 G_TYPE_STRING, /* account name */
438 G_TYPE_STRING, /* account display name */
439 G_TYPE_BOOLEAN, /* is-enabled */
440 G_TYPE_BOOLEAN, /* is-default */
441 G_TYPE_STRING, /* account proto (pop, imap,...) */
442 G_TYPE_STRING /* last updated (time_t) */
445 gtk_tree_sortable_set_sort_column_id (
446 GTK_TREE_SORTABLE (model), MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN,
449 gtk_tree_view_set_model (GTK_TREE_VIEW(self), GTK_TREE_MODEL(model));
450 g_object_unref (G_OBJECT (model));
452 toggle_renderer = gtk_cell_renderer_toggle_new ();
453 text_renderer = gtk_cell_renderer_text_new ();
454 g_object_set (G_OBJECT (text_renderer), "ellipsize", PANGO_ELLIPSIZE_END,
455 "ellipsize-set", TRUE, NULL);
457 /* the is_default column */
458 g_object_set (G_OBJECT(toggle_renderer), "activatable", TRUE, "radio", TRUE, NULL);
459 gtk_tree_view_append_column (GTK_TREE_VIEW(self),
460 gtk_tree_view_column_new_with_attributes (
461 _("mcen_ti_default"), toggle_renderer,
462 "active", MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, NULL));
464 /* Disable the Maemo GtkTreeView::allow-checkbox-mode Maemo modification,
465 * which causes the model column to be updated automatically when the row is clicked.
466 * Making this the default in Maemo's GTK+ is obviously a bug:
467 * https://maemo.org/bugzilla/show_bug.cgi?id=146
469 * djcb: indeed, they have been removed for post-bora, i added the ifdefs...
471 #ifdef MODEST_HAVE_HILDON0_WIDGETS
472 g_object_set(G_OBJECT(self), "allow-checkbox-mode", FALSE, NULL);
473 g_object_set(G_OBJECT(toggle_renderer), "checkbox-mode", FALSE, NULL);
474 #endif /* MODEST_HAVE_HILDON0_WIDGETS */
477 modest_signal_mgr_connect (priv->sig_handlers,
478 G_OBJECT(toggle_renderer),
480 G_CALLBACK(on_account_default_toggled),
484 column = gtk_tree_view_column_new_with_attributes (_("mcen_ti_account"), text_renderer, "text",
485 MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN, NULL);
486 gtk_tree_view_append_column (GTK_TREE_VIEW(self), column);
487 gtk_tree_view_column_set_expand (column, TRUE);
488 gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_cell_data,
491 /* last update for this account */
492 column = gtk_tree_view_column_new_with_attributes (_("mcen_ti_lastupdated"), text_renderer,"text",
493 MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, NULL);
494 gtk_tree_view_append_column (GTK_TREE_VIEW(self),column);
495 gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_cell_data,
498 /* Show the column headers,
499 * which does not seem to be the default on Maemo.
501 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(self), TRUE);
504 modest_signal_mgr_connect (priv->sig_handlers,
505 G_OBJECT (modest_runtime_get_account_store ()),
507 G_CALLBACK(on_account_removed),
510 modest_signal_mgr_connect (priv->sig_handlers,
511 G_OBJECT (modest_runtime_get_account_store ()),
513 G_CALLBACK(on_account_inserted),
516 modest_signal_mgr_connect (priv->sig_handlers,
517 G_OBJECT (modest_runtime_get_account_store ()),
519 G_CALLBACK(on_account_changed),
522 modest_signal_mgr_connect (priv->sig_handlers,
523 G_OBJECT(priv->account_mgr),
524 "account_busy_changed",
525 G_CALLBACK(on_account_busy_changed),
528 modest_signal_mgr_connect (priv->sig_handlers,
529 G_OBJECT(priv->account_mgr),
530 "default_account_changed",
531 G_CALLBACK(on_default_account_changed),
534 modest_signal_mgr_connect (priv->sig_handlers,
535 G_OBJECT(priv->account_mgr),
536 "display_name_changed",
537 G_CALLBACK(on_display_name_changed),
540 modest_signal_mgr_connect (priv->sig_handlers,
541 G_OBJECT (priv->account_mgr),
543 G_CALLBACK (on_account_updated),
549 modest_account_view_new (ModestAccountMgr *account_mgr)
552 ModestAccountViewPrivate *priv;
554 g_return_val_if_fail (account_mgr, NULL);
556 obj = g_object_new(MODEST_TYPE_ACCOUNT_VIEW, NULL);
557 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
559 g_object_ref (G_OBJECT (account_mgr));
560 priv->account_mgr = account_mgr;
562 init_view (MODEST_ACCOUNT_VIEW (obj));
563 update_account_view (account_mgr, MODEST_ACCOUNT_VIEW (obj));
565 return MODEST_ACCOUNT_VIEW (obj);
569 modest_account_view_get_selected_account (ModestAccountView *self)
571 gchar *account_name = NULL;
572 GtkTreeSelection *sel;
576 g_return_val_if_fail (MODEST_IS_ACCOUNT_VIEW (self), NULL);
578 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
579 if (gtk_tree_selection_get_selected (sel, &model, &iter)) {
580 gtk_tree_model_get (model, &iter,
581 MODEST_ACCOUNT_VIEW_NAME_COLUMN,
588 /* This allows us to pass more than one piece of data to the signal handler,
589 * and get a result: */
592 ModestAccountView* self;
593 const gchar *account_name;
597 on_model_foreach_select_account(GtkTreeModel *model,
598 GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
600 ForEachData *state = (ForEachData*)(user_data);
602 /* Select the item if it has the matching account name: */
603 gchar *this_account_name = NULL;
604 gtk_tree_model_get (model, iter,
605 MODEST_ACCOUNT_VIEW_NAME_COLUMN, &this_account_name,
607 if(this_account_name && state->account_name
608 && (strcmp (this_account_name, state->account_name) == 0)) {
610 GtkTreeSelection *selection =
611 gtk_tree_view_get_selection (GTK_TREE_VIEW (state->self));
612 gtk_tree_selection_select_iter (selection, iter);
614 return TRUE; /* Stop walking the tree. */
617 return FALSE; /* Keep walking the tree. */
621 modest_account_view_select_account (ModestAccountView *account_view,
622 const gchar* account_name)
624 /* Create a state instance so we can send two items of data to the signal handler: */
625 ForEachData *state = g_new0 (ForEachData, 1);
626 state->self = account_view;
627 state->account_name = account_name;
629 GtkTreeModel *model = gtk_tree_view_get_model (
630 GTK_TREE_VIEW (account_view));
631 gtk_tree_model_foreach (model,
632 on_model_foreach_select_account, state);
638 modest_account_view_select_first_account (ModestAccountView *account_view)
640 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (account_view));
641 g_return_if_fail (model != NULL);
644 gtk_tree_model_get_iter_first (model, &iter);
646 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (account_view));
647 g_return_if_fail (selection != NULL);
649 gtk_tree_selection_select_iter (selection, &iter);
653 on_default_account_changed (ModestAccountMgr *mgr,
657 gchar *default_account_name;
658 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (user_data));
660 if (!gtk_tree_model_get_iter_first(model, &iter))
663 default_account_name = modest_account_mgr_get_default_account (mgr);
669 gtk_tree_model_get (model, &iter,
670 MODEST_ACCOUNT_VIEW_NAME_COLUMN, &name,
671 MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, &is_default,
674 /* Update the default account column */
675 if ((default_account_name != NULL) && (!strcmp (name, default_account_name)))
676 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
677 MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, TRUE, -1);
679 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
680 MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, FALSE, -1);
684 } while (gtk_tree_model_iter_next(model, &iter));
686 /* Free and force a redraw */
687 g_free (default_account_name);
688 gtk_widget_queue_draw (GTK_WIDGET (user_data));
692 on_display_name_changed (ModestAccountMgr *mgr,
693 const gchar *account,
696 /* Update the view */
697 update_account_view (mgr, MODEST_ACCOUNT_VIEW (user_data));