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 last_updated_string = _("mcen_va_refreshing");
180 return last_updated_string;
184 update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
186 GSList *account_names, *cursor;
189 model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(view)));
191 /* Get the ID of the currently-selected account,
192 * so we can select it again after rebuilding the list.
193 * Note that the name doesn't change even when the display name changes.
195 gchar *selected_name = modest_account_view_get_selected_account (view);
197 gtk_list_store_clear (model);
199 /* Note: We do not show disabled accounts.
200 * Of course, this means that there is no UI to enable or disable
201 * accounts. That is OK for maemo where no such feature or UI is
202 * specified, so the "enabled" property is used internally to avoid
203 * showing unfinished accounts. If a user-visible "enabled" is
204 * needed in the future, we must use a second property for the
205 * current use instead */
206 cursor = account_names = modest_account_mgr_account_names (account_mgr,
207 TRUE /* only enabled accounts. */);
211 ModestAccountSettings *settings;
212 ModestServerAccountSettings *store_settings;
214 account_name = (gchar*)cursor->data;
216 settings = modest_account_mgr_load_account_settings (account_mgr, account_name);
218 g_printerr ("modest: failed to get account data for %s\n", account_name);
221 store_settings = modest_account_settings_get_store_settings (settings);
223 /* don't display accounts without stores */
224 if (modest_server_account_settings_get_account_name (store_settings) != NULL) {
229 const gchar *last_updated_string = get_last_updated_string(account_mgr, settings);
231 if (modest_account_settings_get_enabled (settings)) {
232 const gchar *proto_name;
234 proto_name = modest_protocol_info_get_transport_store_protocol_name
235 (modest_server_account_settings_get_protocol (store_settings));
236 gtk_list_store_insert_with_values (
238 MODEST_ACCOUNT_VIEW_NAME_COLUMN, account_name,
239 MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN,
240 modest_account_settings_get_display_name (settings),
241 MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN,
242 modest_account_settings_get_enabled (settings),
243 MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,
244 modest_account_settings_get_is_default (settings),
245 MODEST_ACCOUNT_VIEW_PROTO_COLUMN, proto_name,
246 MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, last_updated_string,
251 g_object_unref (store_settings);
252 g_object_unref (settings);
253 cursor = cursor->next;
256 modest_account_mgr_free_account_names (account_names);
257 account_names = NULL;
259 /* Try to re-select the same account: */
261 modest_account_view_select_account (view, selected_name);
262 g_free (selected_name);
264 modest_account_view_select_first_account (view);
269 on_account_busy_changed(ModestAccountMgr *account_mgr,
270 const gchar *account_name,
272 ModestAccountView *self)
274 GtkListStore *model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(self)));
276 gboolean found = FALSE;
278 if (!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(model), &iter))
283 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter,
284 MODEST_ACCOUNT_VIEW_NAME_COLUMN,
287 if (g_str_equal(cur_name, account_name)) {
288 ModestAccountSettings* settings =
289 modest_account_mgr_load_account_settings (account_mgr, account_name);
294 const gchar* last_updated_string = get_last_updated_string(account_mgr, settings);
295 gtk_list_store_set(model, &iter,
296 MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, last_updated_string,
298 g_object_unref (settings);
303 } while (!found && gtk_tree_model_iter_next(GTK_TREE_MODEL(model), &iter));
307 on_account_inserted (TnyAccountStore *account_store,
311 ModestAccountView *self;
312 ModestAccountViewPrivate *priv;
314 g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (user_data));
316 self = MODEST_ACCOUNT_VIEW (user_data);
317 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE (self);
319 /* Do not refresh the view with transport accounts */
320 if (TNY_IS_STORE_ACCOUNT (account))
321 update_account_view (priv->account_mgr, self);
325 on_account_removed (TnyAccountStore *account_store,
329 ModestAccountView *self;
330 ModestAccountViewPrivate *priv;
331 gchar *selected_name;
333 g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (user_data));
335 self = MODEST_ACCOUNT_VIEW (user_data);
336 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE (self);
338 /* Do not refresh the view with transport accounts */
339 if (!TNY_IS_STORE_ACCOUNT (account))
342 selected_name = modest_account_view_get_selected_account (self);
343 if (selected_name == NULL) {
344 /* we select the first account if none is selected */
345 modest_account_view_select_first_account (self);
347 g_free (selected_name);
350 update_account_view (priv->account_mgr, self);
355 on_account_default_toggled (GtkCellRendererToggle *cell_renderer,
357 ModestAccountView *self)
359 ModestAccountViewPrivate *priv;
362 gchar *account_name = NULL;
364 g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (self));
366 /* If it's active then do nothing, no need to reenable it as
368 if (gtk_cell_renderer_toggle_get_active (cell_renderer))
371 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
372 model = gtk_tree_view_get_model (GTK_TREE_VIEW(self));
373 gtk_tree_model_get_iter_from_string (model, &iter, path);
375 gtk_tree_model_get (model, &iter,
376 MODEST_ACCOUNT_VIEW_NAME_COLUMN,
379 /* Set this previously-non-default account as the
380 default. We're not updating here the value of the
381 DEFAULT_COLUMN because we'll do it in the
382 "default_account_changed" signal handler. We do it like
383 this because that way the signal handler is useful also
384 when we're inserting a new account and there is no other
385 one defined, in that case the change of account is provoked
386 by the account mgr and not by a signal toggle.*/
387 modest_account_mgr_set_default_account (priv->account_mgr, account_name);
389 g_free (account_name);
393 on_account_updated (ModestAccountMgr* mgr,
397 update_account_view (mgr, MODEST_ACCOUNT_VIEW (user_data));
401 bold_if_default_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
402 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer user_data)
405 gtk_tree_model_get (tree_model, iter, MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,
407 g_object_set (G_OBJECT(renderer),
408 "weight", is_default ? 800: 400,
413 init_view (ModestAccountView *self)
415 ModestAccountViewPrivate *priv;
416 GtkCellRenderer *toggle_renderer, *text_renderer;
418 GtkTreeViewColumn *column;
420 g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (self));
421 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
423 model = gtk_list_store_new (6,
424 G_TYPE_STRING, /* account name */
425 G_TYPE_STRING, /* account display name */
426 G_TYPE_BOOLEAN, /* is-enabled */
427 G_TYPE_BOOLEAN, /* is-default */
428 G_TYPE_STRING, /* account proto (pop, imap,...) */
429 G_TYPE_STRING /* last updated (time_t) */
432 gtk_tree_sortable_set_sort_column_id (
433 GTK_TREE_SORTABLE (model), MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN,
436 gtk_tree_view_set_model (GTK_TREE_VIEW(self), GTK_TREE_MODEL(model));
437 g_object_unref (G_OBJECT (model));
439 toggle_renderer = gtk_cell_renderer_toggle_new ();
440 text_renderer = gtk_cell_renderer_text_new ();
441 g_object_set (G_OBJECT (text_renderer), "ellipsize", PANGO_ELLIPSIZE_END,
442 "ellipsize-set", TRUE, NULL);
444 /* the is_default column */
445 g_object_set (G_OBJECT(toggle_renderer), "activatable", TRUE, "radio", TRUE, NULL);
446 gtk_tree_view_append_column (GTK_TREE_VIEW(self),
447 gtk_tree_view_column_new_with_attributes (
448 _("mcen_ti_default"), toggle_renderer,
449 "active", MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, NULL));
451 /* Disable the Maemo GtkTreeView::allow-checkbox-mode Maemo modification,
452 * which causes the model column to be updated automatically when the row is clicked.
453 * Making this the default in Maemo's GTK+ is obviously a bug:
454 * https://maemo.org/bugzilla/show_bug.cgi?id=146
456 * djcb: indeed, they have been removed for post-bora, i added the ifdefs...
458 #ifdef MODEST_HAVE_HILDON0_WIDGETS
459 g_object_set(G_OBJECT(self), "allow-checkbox-mode", FALSE, NULL);
460 g_object_set(G_OBJECT(toggle_renderer), "checkbox-mode", FALSE, NULL);
461 #endif /* MODEST_HAVE_HILDON0_WIDGETS */
464 modest_signal_mgr_connect (priv->sig_handlers,
465 G_OBJECT(toggle_renderer),
467 G_CALLBACK(on_account_default_toggled),
471 column = gtk_tree_view_column_new_with_attributes (_("mcen_ti_account"), text_renderer, "text",
472 MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN, NULL);
473 gtk_tree_view_append_column (GTK_TREE_VIEW(self), column);
474 gtk_tree_view_column_set_expand (column, TRUE);
475 gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_cell_data,
478 /* last update for this account */
479 column = gtk_tree_view_column_new_with_attributes (_("mcen_ti_lastupdated"), text_renderer,"text",
480 MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, NULL);
481 gtk_tree_view_append_column (GTK_TREE_VIEW(self),column);
482 gtk_tree_view_column_set_expand (column, TRUE);
483 gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_cell_data,
486 /* Show the column headers,
487 * which does not seem to be the default on Maemo.
489 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(self), TRUE);
492 modest_signal_mgr_connect (priv->sig_handlers,
493 G_OBJECT (modest_runtime_get_account_store ()),
495 G_CALLBACK(on_account_removed),
498 modest_signal_mgr_connect (priv->sig_handlers,
499 G_OBJECT (modest_runtime_get_account_store ()),
501 G_CALLBACK(on_account_inserted),
504 modest_signal_mgr_connect (priv->sig_handlers,
505 G_OBJECT(priv->account_mgr),
506 "account_busy_changed",
507 G_CALLBACK(on_account_busy_changed),
510 modest_signal_mgr_connect (priv->sig_handlers,
511 G_OBJECT(priv->account_mgr),
512 "default_account_changed",
513 G_CALLBACK(on_default_account_changed),
516 modest_signal_mgr_connect (priv->sig_handlers,
517 G_OBJECT(priv->account_mgr),
518 "display_name_changed",
519 G_CALLBACK(on_display_name_changed),
522 modest_signal_mgr_connect (priv->sig_handlers,
523 G_OBJECT (priv->account_mgr),
525 G_CALLBACK (on_account_updated),
531 modest_account_view_new (ModestAccountMgr *account_mgr)
534 ModestAccountViewPrivate *priv;
536 g_return_val_if_fail (account_mgr, NULL);
538 obj = g_object_new(MODEST_TYPE_ACCOUNT_VIEW, NULL);
539 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
541 g_object_ref (G_OBJECT (account_mgr));
542 priv->account_mgr = account_mgr;
544 init_view (MODEST_ACCOUNT_VIEW (obj));
545 update_account_view (account_mgr, MODEST_ACCOUNT_VIEW (obj));
547 return MODEST_ACCOUNT_VIEW (obj);
551 modest_account_view_get_selected_account (ModestAccountView *self)
553 gchar *account_name = NULL;
554 GtkTreeSelection *sel;
558 g_return_val_if_fail (MODEST_IS_ACCOUNT_VIEW (self), NULL);
560 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
561 if (gtk_tree_selection_get_selected (sel, &model, &iter)) {
562 gtk_tree_model_get (model, &iter,
563 MODEST_ACCOUNT_VIEW_NAME_COLUMN,
570 /* This allows us to pass more than one piece of data to the signal handler,
571 * and get a result: */
574 ModestAccountView* self;
575 const gchar *account_name;
579 on_model_foreach_select_account(GtkTreeModel *model,
580 GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
582 ForEachData *state = (ForEachData*)(user_data);
584 /* Select the item if it has the matching account name: */
585 gchar *this_account_name = NULL;
586 gtk_tree_model_get (model, iter,
587 MODEST_ACCOUNT_VIEW_NAME_COLUMN, &this_account_name,
589 if(this_account_name && state->account_name
590 && (strcmp (this_account_name, state->account_name) == 0)) {
592 GtkTreeSelection *selection =
593 gtk_tree_view_get_selection (GTK_TREE_VIEW (state->self));
594 gtk_tree_selection_select_iter (selection, iter);
596 return TRUE; /* Stop walking the tree. */
599 return FALSE; /* Keep walking the tree. */
603 modest_account_view_select_account (ModestAccountView *account_view,
604 const gchar* account_name)
606 /* Create a state instance so we can send two items of data to the signal handler: */
607 ForEachData *state = g_new0 (ForEachData, 1);
608 state->self = account_view;
609 state->account_name = account_name;
611 GtkTreeModel *model = gtk_tree_view_get_model (
612 GTK_TREE_VIEW (account_view));
613 gtk_tree_model_foreach (model,
614 on_model_foreach_select_account, state);
620 modest_account_view_select_first_account (ModestAccountView *account_view)
623 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (account_view));
625 if (gtk_tree_model_get_iter_first (model, &iter)) {
626 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (account_view));
628 gtk_tree_selection_select_iter (selection, &iter);
633 on_default_account_changed (ModestAccountMgr *mgr,
637 gchar *default_account_name;
638 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (user_data));
640 if (!gtk_tree_model_get_iter_first(model, &iter))
643 default_account_name = modest_account_mgr_get_default_account (mgr);
649 gtk_tree_model_get (model, &iter,
650 MODEST_ACCOUNT_VIEW_NAME_COLUMN, &name,
651 MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, &is_default,
654 /* Update the default account column */
655 if ((default_account_name != NULL) && (!strcmp (name, default_account_name)))
656 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
657 MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, TRUE, -1);
659 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
660 MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, FALSE, -1);
664 } while (gtk_tree_model_iter_next(model, &iter));
666 /* Free and force a redraw */
667 g_free (default_account_name);
668 gtk_widget_queue_draw (GTK_WIDGET (user_data));
672 on_display_name_changed (ModestAccountMgr *mgr,
673 const gchar *account,
676 /* Update the view */
677 update_account_view (mgr, MODEST_ACCOUNT_VIEW (user_data));