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);
267 on_account_busy_changed(ModestAccountMgr *account_mgr,
268 const gchar *account_name,
270 ModestAccountView *self)
272 GtkListStore *model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(self)));
274 gboolean found = FALSE;
276 if (!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(model), &iter))
281 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter,
282 MODEST_ACCOUNT_VIEW_NAME_COLUMN,
285 if (g_str_equal(cur_name, account_name)) {
286 ModestAccountSettings* settings =
287 modest_account_mgr_load_account_settings (account_mgr, account_name);
292 const gchar* last_updated_string = get_last_updated_string(account_mgr, settings);
293 gtk_list_store_set(model, &iter,
294 MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, last_updated_string,
296 g_object_unref (settings);
301 } while (!found && gtk_tree_model_iter_next(GTK_TREE_MODEL(model), &iter));
305 on_account_inserted (TnyAccountStore *account_store,
309 ModestAccountView *self;
310 ModestAccountViewPrivate *priv;
312 g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (user_data));
314 self = MODEST_ACCOUNT_VIEW (user_data);
315 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE (self);
317 /* Do not refresh the view with transport accounts */
318 if (TNY_IS_STORE_ACCOUNT (account))
319 update_account_view (priv->account_mgr, self);
323 on_account_removed (TnyAccountStore *account_store,
327 ModestAccountView *self;
328 ModestAccountViewPrivate *priv;
330 g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (user_data));
332 self = MODEST_ACCOUNT_VIEW (user_data);
333 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE (self);
335 gchar *selected_name = modest_account_view_get_selected_account (self);
336 if (selected_name == NULL) {
337 /* we select the first account if none is selected */
338 modest_account_view_select_first_account (self);
340 g_free (selected_name);
343 update_account_view (priv->account_mgr, self);
348 on_account_default_toggled (GtkCellRendererToggle *cell_renderer,
350 ModestAccountView *self)
352 ModestAccountViewPrivate *priv;
355 gchar *account_name = NULL;
357 g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (self));
359 /* If it's active then do nothing, no need to reenable it as
361 if (gtk_cell_renderer_toggle_get_active (cell_renderer))
364 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
365 model = gtk_tree_view_get_model (GTK_TREE_VIEW(self));
366 gtk_tree_model_get_iter_from_string (model, &iter, path);
368 gtk_tree_model_get (model, &iter,
369 MODEST_ACCOUNT_VIEW_NAME_COLUMN,
372 /* Set this previously-non-default account as the
373 default. We're not updating here the value of the
374 DEFAULT_COLUMN because we'll do it in the
375 "default_account_changed" signal handler. We do it like
376 this because that way the signal handler is useful also
377 when we're inserting a new account and there is no other
378 one defined, in that case the change of account is provoked
379 by the account mgr and not by a signal toggle.*/
380 modest_account_mgr_set_default_account (priv->account_mgr, account_name);
382 g_free (account_name);
386 on_account_updated (ModestAccountMgr* mgr,
390 update_account_view (mgr, MODEST_ACCOUNT_VIEW (user_data));
394 bold_if_default_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
395 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer user_data)
398 gtk_tree_model_get (tree_model, iter, MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,
400 g_object_set (G_OBJECT(renderer),
401 "weight", is_default ? 800: 400,
406 init_view (ModestAccountView *self)
408 ModestAccountViewPrivate *priv;
409 GtkCellRenderer *toggle_renderer, *text_renderer;
411 GtkTreeViewColumn *column;
413 g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (self));
414 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
416 model = gtk_list_store_new (6,
417 G_TYPE_STRING, /* account name */
418 G_TYPE_STRING, /* account display name */
419 G_TYPE_BOOLEAN, /* is-enabled */
420 G_TYPE_BOOLEAN, /* is-default */
421 G_TYPE_STRING, /* account proto (pop, imap,...) */
422 G_TYPE_STRING /* last updated (time_t) */
425 gtk_tree_sortable_set_sort_column_id (
426 GTK_TREE_SORTABLE (model), MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN,
429 gtk_tree_view_set_model (GTK_TREE_VIEW(self), GTK_TREE_MODEL(model));
430 g_object_unref (G_OBJECT (model));
432 toggle_renderer = gtk_cell_renderer_toggle_new ();
433 text_renderer = gtk_cell_renderer_text_new ();
434 g_object_set (G_OBJECT (text_renderer), "ellipsize", PANGO_ELLIPSIZE_END,
435 "ellipsize-set", TRUE, NULL);
437 /* the is_default column */
438 g_object_set (G_OBJECT(toggle_renderer), "activatable", TRUE, "radio", TRUE, NULL);
439 gtk_tree_view_append_column (GTK_TREE_VIEW(self),
440 gtk_tree_view_column_new_with_attributes (
441 _("mcen_ti_default"), toggle_renderer,
442 "active", MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, NULL));
444 /* Disable the Maemo GtkTreeView::allow-checkbox-mode Maemo modification,
445 * which causes the model column to be updated automatically when the row is clicked.
446 * Making this the default in Maemo's GTK+ is obviously a bug:
447 * https://maemo.org/bugzilla/show_bug.cgi?id=146
449 * djcb: indeed, they have been removed for post-bora, i added the ifdefs...
451 #ifdef MODEST_HAVE_HILDON0_WIDGETS
452 g_object_set(G_OBJECT(self), "allow-checkbox-mode", FALSE, NULL);
453 g_object_set(G_OBJECT(toggle_renderer), "checkbox-mode", FALSE, NULL);
454 #endif /* MODEST_HAVE_HILDON0_WIDGETS */
457 modest_signal_mgr_connect (priv->sig_handlers,
458 G_OBJECT(toggle_renderer),
460 G_CALLBACK(on_account_default_toggled),
464 column = gtk_tree_view_column_new_with_attributes (_("mcen_ti_account"), text_renderer, "text",
465 MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN, NULL);
466 gtk_tree_view_append_column (GTK_TREE_VIEW(self), column);
467 gtk_tree_view_column_set_expand (column, TRUE);
468 gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_cell_data,
471 /* last update for this account */
472 column = gtk_tree_view_column_new_with_attributes (_("mcen_ti_lastupdated"), text_renderer,"text",
473 MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, NULL);
474 gtk_tree_view_append_column (GTK_TREE_VIEW(self),column);
475 gtk_tree_view_column_set_expand (column, TRUE);
476 gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_cell_data,
479 /* Show the column headers,
480 * which does not seem to be the default on Maemo.
482 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(self), TRUE);
485 modest_signal_mgr_connect (priv->sig_handlers,
486 G_OBJECT (modest_runtime_get_account_store ()),
488 G_CALLBACK(on_account_removed),
491 modest_signal_mgr_connect (priv->sig_handlers,
492 G_OBJECT (modest_runtime_get_account_store ()),
494 G_CALLBACK(on_account_inserted),
497 modest_signal_mgr_connect (priv->sig_handlers,
498 G_OBJECT(priv->account_mgr),
499 "account_busy_changed",
500 G_CALLBACK(on_account_busy_changed),
503 modest_signal_mgr_connect (priv->sig_handlers,
504 G_OBJECT(priv->account_mgr),
505 "default_account_changed",
506 G_CALLBACK(on_default_account_changed),
509 modest_signal_mgr_connect (priv->sig_handlers,
510 G_OBJECT(priv->account_mgr),
511 "display_name_changed",
512 G_CALLBACK(on_display_name_changed),
515 modest_signal_mgr_connect (priv->sig_handlers,
516 G_OBJECT (priv->account_mgr),
518 G_CALLBACK (on_account_updated),
524 modest_account_view_new (ModestAccountMgr *account_mgr)
527 ModestAccountViewPrivate *priv;
529 g_return_val_if_fail (account_mgr, NULL);
531 obj = g_object_new(MODEST_TYPE_ACCOUNT_VIEW, NULL);
532 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
534 g_object_ref (G_OBJECT (account_mgr));
535 priv->account_mgr = account_mgr;
537 init_view (MODEST_ACCOUNT_VIEW (obj));
538 update_account_view (account_mgr, MODEST_ACCOUNT_VIEW (obj));
540 return MODEST_ACCOUNT_VIEW (obj);
544 modest_account_view_get_selected_account (ModestAccountView *self)
546 gchar *account_name = NULL;
547 GtkTreeSelection *sel;
551 g_return_val_if_fail (MODEST_IS_ACCOUNT_VIEW (self), NULL);
553 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
554 if (gtk_tree_selection_get_selected (sel, &model, &iter)) {
555 gtk_tree_model_get (model, &iter,
556 MODEST_ACCOUNT_VIEW_NAME_COLUMN,
563 /* This allows us to pass more than one piece of data to the signal handler,
564 * and get a result: */
567 ModestAccountView* self;
568 const gchar *account_name;
572 on_model_foreach_select_account(GtkTreeModel *model,
573 GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
575 ForEachData *state = (ForEachData*)(user_data);
577 /* Select the item if it has the matching account name: */
578 gchar *this_account_name = NULL;
579 gtk_tree_model_get (model, iter,
580 MODEST_ACCOUNT_VIEW_NAME_COLUMN, &this_account_name,
582 if(this_account_name && state->account_name
583 && (strcmp (this_account_name, state->account_name) == 0)) {
585 GtkTreeSelection *selection =
586 gtk_tree_view_get_selection (GTK_TREE_VIEW (state->self));
587 gtk_tree_selection_select_iter (selection, iter);
589 return TRUE; /* Stop walking the tree. */
592 return FALSE; /* Keep walking the tree. */
596 modest_account_view_select_account (ModestAccountView *account_view,
597 const gchar* account_name)
599 /* Create a state instance so we can send two items of data to the signal handler: */
600 ForEachData *state = g_new0 (ForEachData, 1);
601 state->self = account_view;
602 state->account_name = account_name;
604 GtkTreeModel *model = gtk_tree_view_get_model (
605 GTK_TREE_VIEW (account_view));
606 gtk_tree_model_foreach (model,
607 on_model_foreach_select_account, state);
613 modest_account_view_select_first_account (ModestAccountView *account_view)
616 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (account_view));
618 if (gtk_tree_model_get_iter_first (model, &iter)) {
619 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (account_view));
621 gtk_tree_selection_select_iter (selection, &iter);
626 on_default_account_changed (ModestAccountMgr *mgr,
630 gchar *default_account_name;
631 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (user_data));
633 if (!gtk_tree_model_get_iter_first(model, &iter))
636 default_account_name = modest_account_mgr_get_default_account (mgr);
642 gtk_tree_model_get (model, &iter,
643 MODEST_ACCOUNT_VIEW_NAME_COLUMN, &name,
644 MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, &is_default,
647 /* Update the default account column */
648 if ((default_account_name != NULL) && (!strcmp (name, default_account_name)))
649 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
650 MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, TRUE, -1);
652 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
653 MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, FALSE, -1);
657 } while (gtk_tree_model_iter_next(model, &iter));
659 /* Free and force a redraw */
660 g_free (default_account_name);
661 gtk_widget_queue_draw (GTK_WIDGET (user_data));
665 on_display_name_changed (ModestAccountMgr *mgr,
666 const gchar *account,
669 /* Update the view */
670 update_account_view (mgr, MODEST_ACCOUNT_VIEW (user_data));