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 ModestProtocolType protocol_type;
233 ModestProtocolRegistry *protocol_registry;
234 ModestProtocol *protocol;
235 const gchar *proto_name;
237 protocol_registry = modest_runtime_get_protocol_registry ();
238 protocol_type = modest_server_account_settings_get_protocol (store_settings);
239 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, protocol_type);
240 proto_name = modest_protocol_get_name (protocol);
241 gtk_list_store_insert_with_values (
243 MODEST_ACCOUNT_VIEW_NAME_COLUMN, account_name,
244 MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN,
245 modest_account_settings_get_display_name (settings),
246 MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN,
247 modest_account_settings_get_enabled (settings),
248 MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,
249 modest_account_settings_get_is_default (settings),
250 MODEST_ACCOUNT_VIEW_PROTO_COLUMN, proto_name,
251 MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, last_updated_string,
256 g_object_unref (store_settings);
257 g_object_unref (settings);
258 cursor = cursor->next;
261 modest_account_mgr_free_account_names (account_names);
262 account_names = NULL;
264 /* Try to re-select the same account: */
266 modest_account_view_select_account (view, selected_name);
267 g_free (selected_name);
269 modest_account_view_select_first_account (view);
274 on_account_busy_changed(ModestAccountMgr *account_mgr,
275 const gchar *account_name,
277 ModestAccountView *self)
279 GtkListStore *model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(self)));
281 gboolean found = FALSE;
283 if (!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(model), &iter))
288 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter,
289 MODEST_ACCOUNT_VIEW_NAME_COLUMN,
292 if (g_str_equal(cur_name, account_name)) {
293 ModestAccountSettings* settings =
294 modest_account_mgr_load_account_settings (account_mgr, account_name);
299 const gchar* last_updated_string = get_last_updated_string(account_mgr, settings);
300 gtk_list_store_set(model, &iter,
301 MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, last_updated_string,
303 g_object_unref (settings);
308 } while (!found && gtk_tree_model_iter_next(GTK_TREE_MODEL(model), &iter));
312 on_account_inserted (TnyAccountStore *account_store,
316 ModestAccountView *self;
317 ModestAccountViewPrivate *priv;
319 g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (user_data));
321 self = MODEST_ACCOUNT_VIEW (user_data);
322 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE (self);
324 /* Do not refresh the view with transport accounts */
325 if (TNY_IS_STORE_ACCOUNT (account))
326 update_account_view (priv->account_mgr, self);
330 on_account_removed (TnyAccountStore *account_store,
334 ModestAccountView *self;
335 ModestAccountViewPrivate *priv;
336 gchar *selected_name;
338 g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (user_data));
340 self = MODEST_ACCOUNT_VIEW (user_data);
341 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE (self);
343 /* Do not refresh the view with transport accounts */
344 if (!TNY_IS_STORE_ACCOUNT (account))
347 selected_name = modest_account_view_get_selected_account (self);
348 if (selected_name == NULL) {
349 /* we select the first account if none is selected */
350 modest_account_view_select_first_account (self);
352 g_free (selected_name);
355 update_account_view (priv->account_mgr, self);
360 on_account_default_toggled (GtkCellRendererToggle *cell_renderer,
362 ModestAccountView *self)
364 ModestAccountViewPrivate *priv;
367 gchar *account_name = NULL;
369 g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (self));
371 /* If it's active then do nothing, no need to reenable it as
373 if (gtk_cell_renderer_toggle_get_active (cell_renderer))
376 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
377 model = gtk_tree_view_get_model (GTK_TREE_VIEW(self));
378 gtk_tree_model_get_iter_from_string (model, &iter, path);
380 gtk_tree_model_get (model, &iter,
381 MODEST_ACCOUNT_VIEW_NAME_COLUMN,
384 /* Set this previously-non-default account as the
385 default. We're not updating here the value of the
386 DEFAULT_COLUMN because we'll do it in the
387 "default_account_changed" signal handler. We do it like
388 this because that way the signal handler is useful also
389 when we're inserting a new account and there is no other
390 one defined, in that case the change of account is provoked
391 by the account mgr and not by a signal toggle.*/
392 modest_account_mgr_set_default_account (priv->account_mgr, account_name);
394 g_free (account_name);
398 on_account_updated (ModestAccountMgr* mgr,
402 update_account_view (mgr, MODEST_ACCOUNT_VIEW (user_data));
406 bold_if_default_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
407 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer user_data)
410 gtk_tree_model_get (tree_model, iter, MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,
412 g_object_set (G_OBJECT(renderer),
413 "weight", is_default ? 800: 400,
418 init_view (ModestAccountView *self)
420 ModestAccountViewPrivate *priv;
421 GtkCellRenderer *toggle_renderer, *text_renderer;
423 GtkTreeViewColumn *column;
425 g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (self));
426 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
428 model = gtk_list_store_new (6,
429 G_TYPE_STRING, /* account name */
430 G_TYPE_STRING, /* account display name */
431 G_TYPE_BOOLEAN, /* is-enabled */
432 G_TYPE_BOOLEAN, /* is-default */
433 G_TYPE_STRING, /* account proto (pop, imap,...) */
434 G_TYPE_STRING /* last updated (time_t) */
437 gtk_tree_sortable_set_sort_column_id (
438 GTK_TREE_SORTABLE (model), MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN,
441 gtk_tree_view_set_model (GTK_TREE_VIEW(self), GTK_TREE_MODEL(model));
442 g_object_unref (G_OBJECT (model));
444 toggle_renderer = gtk_cell_renderer_toggle_new ();
445 text_renderer = gtk_cell_renderer_text_new ();
446 g_object_set (G_OBJECT (text_renderer), "ellipsize", PANGO_ELLIPSIZE_END,
447 "ellipsize-set", TRUE, NULL);
449 /* the is_default column */
450 g_object_set (G_OBJECT(toggle_renderer), "activatable", TRUE, "radio", TRUE, NULL);
451 gtk_tree_view_append_column (GTK_TREE_VIEW(self),
452 gtk_tree_view_column_new_with_attributes (
453 _("mcen_ti_default"), toggle_renderer,
454 "active", MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, NULL));
456 /* Disable the Maemo GtkTreeView::allow-checkbox-mode Maemo modification,
457 * which causes the model column to be updated automatically when the row is clicked.
458 * Making this the default in Maemo's GTK+ is obviously a bug:
459 * https://maemo.org/bugzilla/show_bug.cgi?id=146
461 * djcb: indeed, they have been removed for post-bora, i added the ifdefs...
463 #ifdef MODEST_HAVE_HILDON0_WIDGETS
464 g_object_set(G_OBJECT(self), "allow-checkbox-mode", FALSE, NULL);
465 g_object_set(G_OBJECT(toggle_renderer), "checkbox-mode", FALSE, NULL);
466 #endif /* MODEST_HAVE_HILDON0_WIDGETS */
469 modest_signal_mgr_connect (priv->sig_handlers,
470 G_OBJECT(toggle_renderer),
472 G_CALLBACK(on_account_default_toggled),
476 column = gtk_tree_view_column_new_with_attributes (_("mcen_ti_account"), text_renderer, "text",
477 MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN, NULL);
478 gtk_tree_view_append_column (GTK_TREE_VIEW(self), column);
479 gtk_tree_view_column_set_expand (column, TRUE);
480 gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_cell_data,
483 /* last update for this account */
484 column = gtk_tree_view_column_new_with_attributes (_("mcen_ti_lastupdated"), text_renderer,"text",
485 MODEST_ACCOUNT_VIEW_LAST_UPDATED_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 /* Show the column headers,
492 * which does not seem to be the default on Maemo.
494 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(self), TRUE);
497 modest_signal_mgr_connect (priv->sig_handlers,
498 G_OBJECT (modest_runtime_get_account_store ()),
500 G_CALLBACK(on_account_removed),
503 modest_signal_mgr_connect (priv->sig_handlers,
504 G_OBJECT (modest_runtime_get_account_store ()),
506 G_CALLBACK(on_account_inserted),
509 modest_signal_mgr_connect (priv->sig_handlers,
510 G_OBJECT(priv->account_mgr),
511 "account_busy_changed",
512 G_CALLBACK(on_account_busy_changed),
515 modest_signal_mgr_connect (priv->sig_handlers,
516 G_OBJECT(priv->account_mgr),
517 "default_account_changed",
518 G_CALLBACK(on_default_account_changed),
521 modest_signal_mgr_connect (priv->sig_handlers,
522 G_OBJECT(priv->account_mgr),
523 "display_name_changed",
524 G_CALLBACK(on_display_name_changed),
527 modest_signal_mgr_connect (priv->sig_handlers,
528 G_OBJECT (priv->account_mgr),
530 G_CALLBACK (on_account_updated),
536 modest_account_view_new (ModestAccountMgr *account_mgr)
539 ModestAccountViewPrivate *priv;
541 g_return_val_if_fail (account_mgr, NULL);
543 obj = g_object_new(MODEST_TYPE_ACCOUNT_VIEW, NULL);
544 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
546 g_object_ref (G_OBJECT (account_mgr));
547 priv->account_mgr = account_mgr;
549 init_view (MODEST_ACCOUNT_VIEW (obj));
550 update_account_view (account_mgr, MODEST_ACCOUNT_VIEW (obj));
552 return MODEST_ACCOUNT_VIEW (obj);
556 modest_account_view_get_selected_account (ModestAccountView *self)
558 gchar *account_name = NULL;
559 GtkTreeSelection *sel;
563 g_return_val_if_fail (MODEST_IS_ACCOUNT_VIEW (self), NULL);
565 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
566 if (gtk_tree_selection_get_selected (sel, &model, &iter)) {
567 gtk_tree_model_get (model, &iter,
568 MODEST_ACCOUNT_VIEW_NAME_COLUMN,
575 /* This allows us to pass more than one piece of data to the signal handler,
576 * and get a result: */
579 ModestAccountView* self;
580 const gchar *account_name;
584 on_model_foreach_select_account(GtkTreeModel *model,
585 GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
587 ForEachData *state = (ForEachData*)(user_data);
589 /* Select the item if it has the matching account name: */
590 gchar *this_account_name = NULL;
591 gtk_tree_model_get (model, iter,
592 MODEST_ACCOUNT_VIEW_NAME_COLUMN, &this_account_name,
594 if(this_account_name && state->account_name
595 && (strcmp (this_account_name, state->account_name) == 0)) {
597 GtkTreeSelection *selection =
598 gtk_tree_view_get_selection (GTK_TREE_VIEW (state->self));
599 gtk_tree_selection_select_iter (selection, iter);
601 return TRUE; /* Stop walking the tree. */
604 return FALSE; /* Keep walking the tree. */
608 modest_account_view_select_account (ModestAccountView *account_view,
609 const gchar* account_name)
611 /* Create a state instance so we can send two items of data to the signal handler: */
612 ForEachData *state = g_new0 (ForEachData, 1);
613 state->self = account_view;
614 state->account_name = account_name;
616 GtkTreeModel *model = gtk_tree_view_get_model (
617 GTK_TREE_VIEW (account_view));
618 gtk_tree_model_foreach (model,
619 on_model_foreach_select_account, state);
625 modest_account_view_select_first_account (ModestAccountView *account_view)
628 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (account_view));
630 if (gtk_tree_model_get_iter_first (model, &iter)) {
631 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (account_view));
633 gtk_tree_selection_select_iter (selection, &iter);
638 on_default_account_changed (ModestAccountMgr *mgr,
642 gchar *default_account_name;
643 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (user_data));
645 if (!gtk_tree_model_get_iter_first(model, &iter))
648 default_account_name = modest_account_mgr_get_default_account (mgr);
654 gtk_tree_model_get (model, &iter,
655 MODEST_ACCOUNT_VIEW_NAME_COLUMN, &name,
656 MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, &is_default,
659 /* Update the default account column */
660 if ((default_account_name != NULL) && (!strcmp (name, default_account_name)))
661 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
662 MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, TRUE, -1);
664 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
665 MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, FALSE, -1);
669 } while (gtk_tree_model_iter_next(model, &iter));
671 /* Free and force a redraw */
672 g_free (default_account_name);
673 gtk_widget_queue_draw (GTK_WIDGET (user_data));
677 on_display_name_changed (ModestAccountMgr *mgr,
678 const gchar *account,
681 /* Update the view */
682 update_account_view (mgr, MODEST_ACCOUNT_VIEW (user_data));