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(). */
46 /* 'private'/'protected' functions */
47 static void modest_account_view_class_init (ModestAccountViewClass *klass);
48 static void modest_account_view_init (ModestAccountView *obj);
49 static void modest_account_view_finalize (GObject *obj);
51 static void modest_account_view_select_account (ModestAccountView *account_view,
52 const gchar* account_name);
54 static void on_default_account_changed (ModestAccountMgr *mgr,
57 static void on_display_name_changed (ModestAccountMgr *self,
61 static void modest_account_view_select_first_account (ModestAccountView *account_view);
64 MODEST_ACCOUNT_VIEW_NAME_COLUMN,
65 MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN,
66 MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN,
67 MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,
68 MODEST_ACCOUNT_VIEW_PROTO_COLUMN,
69 MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN,
71 MODEST_ACCOUNT_VIEW_COLUMN_NUM
74 typedef struct _ModestAccountViewPrivate ModestAccountViewPrivate;
75 struct _ModestAccountViewPrivate {
76 ModestAccountMgr *account_mgr;
81 #define MODEST_ACCOUNT_VIEW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
82 MODEST_TYPE_ACCOUNT_VIEW, \
83 ModestAccountViewPrivate))
85 static GtkTreeViewClass *parent_class = NULL;
88 modest_account_view_get_type (void)
90 static GType my_type = 0;
92 static const GTypeInfo my_info = {
93 sizeof(ModestAccountViewClass),
95 NULL, /* base finalize */
96 (GClassInitFunc) modest_account_view_class_init,
97 NULL, /* class finalize */
98 NULL, /* class data */
99 sizeof(ModestAccountView),
101 (GInstanceInitFunc) modest_account_view_init,
104 my_type = g_type_register_static (GTK_TYPE_TREE_VIEW,
112 modest_account_view_class_init (ModestAccountViewClass *klass)
114 GObjectClass *gobject_class;
115 gobject_class = (GObjectClass*) klass;
117 parent_class = g_type_class_peek_parent (klass);
118 gobject_class->finalize = modest_account_view_finalize;
120 g_type_class_add_private (gobject_class, sizeof(ModestAccountViewPrivate));
124 modest_account_view_init (ModestAccountView *obj)
126 ModestAccountViewPrivate *priv;
128 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
130 priv->sig_handlers = NULL;
134 modest_account_view_finalize (GObject *obj)
136 ModestAccountViewPrivate *priv;
138 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
140 /* Disconnect signals */
141 modest_signal_mgr_disconnect_all_and_destroy (priv->sig_handlers);
143 if (priv->account_mgr) {
144 g_object_unref (G_OBJECT(priv->account_mgr));
145 priv->account_mgr = NULL;
148 G_OBJECT_CLASS(parent_class)->finalize (obj);
151 /* Get the string for the last updated time. Result must NOT be g_freed */
153 get_last_updated_string(ModestAccountMgr* account_mgr, ModestAccountData *account_data)
155 /* FIXME: let's assume that 'last update' applies to the store account... */
156 const gchar* last_updated_string;
157 time_t last_updated = account_data->store_account->last_updated;
158 if (!modest_account_mgr_account_is_busy(account_mgr, account_data->account_name)) {
159 if (last_updated > 0)
160 last_updated_string = modest_text_utils_get_display_date(last_updated);
162 last_updated_string = _("mcen_va_never");
164 /* FIXME: There should be a logical name in the UI specs */
165 last_updated_string = _("mcen_va_refreshing");
168 return last_updated_string;
172 update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
174 GSList *account_names, *cursor;
177 model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(view)));
179 /* Get the ID of the currently-selected account,
180 * so we can select it again after rebuilding the list.
181 * Note that the name doesn't change even when the display name changes.
183 gchar *selected_name = modest_account_view_get_selected_account (view);
185 gtk_list_store_clear (model);
187 /* Note: We do not show disabled accounts.
188 * Of course, this means that there is no UI to enable or disable
189 * accounts. That is OK for maemo where no such feature or UI is
190 * specified, so the "enabled" property is used internally to avoid
191 * showing unfinished accounts. If a user-visible "enabled" is
192 * needed in the future, we must use a second property for the
193 * current use instead */
194 cursor = account_names = modest_account_mgr_account_names (account_mgr,
195 TRUE /* only enabled accounts. */);
199 ModestAccountData *account_data;
201 account_name = (gchar*)cursor->data;
203 account_data = modest_account_mgr_get_account_data (account_mgr, account_name);
205 g_printerr ("modest: failed to get account data for %s\n", account_name);
209 /* don't display accounts without stores */
210 if (account_data->store_account) {
215 const gchar *last_updated_string = get_last_updated_string(account_mgr, account_data);
217 if (account_data->is_enabled) {
218 const gchar *proto_name;
220 proto_name = modest_protocol_info_get_transport_store_protocol_name (account_data->store_account->proto);
221 gtk_list_store_insert_with_values (
223 MODEST_ACCOUNT_VIEW_NAME_COLUMN, account_name,
224 MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN, account_data->display_name,
225 MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN, account_data->is_enabled,
226 MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, account_data->is_default,
227 MODEST_ACCOUNT_VIEW_PROTO_COLUMN, proto_name,
228 MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, last_updated_string,
233 modest_account_mgr_free_account_data (account_mgr, account_data);
234 cursor = cursor->next;
237 modest_account_mgr_free_account_names (account_names);
238 account_names = NULL;
240 /* Try to re-select the same account: */
242 modest_account_view_select_account (view, selected_name);
243 g_free (selected_name);
248 on_account_busy_changed(ModestAccountMgr *account_mgr,
249 const gchar *account_name,
251 ModestAccountView *self)
253 GtkListStore *model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(self)));
255 gboolean found = FALSE;
257 if (!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(model), &iter))
262 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter,
263 MODEST_ACCOUNT_VIEW_NAME_COLUMN,
266 if (g_str_equal(cur_name, account_name)) {
267 ModestAccountData* account_data =
268 modest_account_mgr_get_account_data (account_mgr, account_name);
273 const gchar* last_updated_string = get_last_updated_string(account_mgr, account_data);
274 gtk_list_store_set(model, &iter,
275 MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, last_updated_string,
277 modest_account_mgr_free_account_data (account_mgr, account_data);
282 } while (!found && gtk_tree_model_iter_next(GTK_TREE_MODEL(model), &iter));
286 on_account_inserted (TnyAccountStore *account_store,
290 ModestAccountView *self;
291 ModestAccountViewPrivate *priv;
293 g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (user_data));
295 self = MODEST_ACCOUNT_VIEW (user_data);
296 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE (self);
298 /* Do not refresh the view with transport accounts */
299 if (TNY_IS_STORE_ACCOUNT (account))
300 update_account_view (priv->account_mgr, self);
304 on_account_removed (TnyAccountStore *account_store,
308 ModestAccountView *self;
309 ModestAccountViewPrivate *priv;
311 g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (user_data));
313 self = MODEST_ACCOUNT_VIEW (user_data);
314 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE (self);
316 gchar *selected_name = modest_account_view_get_selected_account (self);
317 if (selected_name == NULL) {
318 /* we select the first account if none is selected */
319 modest_account_view_select_first_account (self);
321 g_free (selected_name);
324 update_account_view (priv->account_mgr, self);
329 on_account_changed (TnyAccountStore *account_store,
333 ModestAccountView *self = NULL;
334 ModestAccountViewPrivate *priv = NULL;
336 g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (user_data));
337 g_return_if_fail (account);
338 g_return_if_fail (TNY_IS_ACCOUNT (account));
340 self = MODEST_ACCOUNT_VIEW (user_data);
341 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE (self);
343 /* Update account view */
344 /* 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 bold_if_default_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
387 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer user_data)
390 gtk_tree_model_get (tree_model, iter, MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,
392 g_object_set (G_OBJECT(renderer),
393 "weight", is_default ? 800: 400,
398 init_view (ModestAccountView *self)
400 ModestAccountViewPrivate *priv;
401 GtkCellRenderer *toggle_renderer, *text_renderer;
403 GtkTreeViewColumn *column;
405 g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (self));
406 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
408 model = gtk_list_store_new (6,
409 G_TYPE_STRING, /* account name */
410 G_TYPE_STRING, /* account display name */
411 G_TYPE_BOOLEAN, /* is-enabled */
412 G_TYPE_BOOLEAN, /* is-default */
413 G_TYPE_STRING, /* account proto (pop, imap,...) */
414 G_TYPE_STRING /* last updated (time_t) */
417 gtk_tree_sortable_set_sort_column_id (
418 GTK_TREE_SORTABLE (model), MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN,
421 gtk_tree_view_set_model (GTK_TREE_VIEW(self), GTK_TREE_MODEL(model));
422 g_object_unref (G_OBJECT (model));
424 toggle_renderer = gtk_cell_renderer_toggle_new ();
425 text_renderer = gtk_cell_renderer_text_new ();
427 /* the is_default column */
428 g_object_set (G_OBJECT(toggle_renderer), "activatable", TRUE, "radio", TRUE, NULL);
429 gtk_tree_view_append_column (GTK_TREE_VIEW(self),
430 gtk_tree_view_column_new_with_attributes (
431 _("mcen_ti_default"), toggle_renderer,
432 "active", MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, NULL));
434 /* Disable the Maemo GtkTreeView::allow-checkbox-mode Maemo modification,
435 * which causes the model column to be updated automatically when the row is clicked.
436 * Making this the default in Maemo's GTK+ is obviously a bug:
437 * https://maemo.org/bugzilla/show_bug.cgi?id=146
439 * djcb: indeed, they have been removed for post-bora, i added the ifdefs...
441 #ifdef MODEST_HAVE_HILDON0_WIDGETS
442 g_object_set(G_OBJECT(self), "allow-checkbox-mode", FALSE, NULL);
443 g_object_set(G_OBJECT(toggle_renderer), "checkbox-mode", FALSE, NULL);
444 #endif /* MODEST_HAVE_HILDON0_WIDGETS */
447 modest_signal_mgr_connect (priv->sig_handlers,
448 G_OBJECT(toggle_renderer),
450 G_CALLBACK(on_account_default_toggled),
454 column = gtk_tree_view_column_new_with_attributes (_("mcen_ti_account"), text_renderer, "text",
455 MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN, NULL);
456 gtk_tree_view_append_column (GTK_TREE_VIEW(self), column);
457 gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_cell_data,
460 /* last update for this account */
461 column = gtk_tree_view_column_new_with_attributes (_("mcen_ti_lastupdated"), text_renderer,"text",
462 MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, NULL);
463 gtk_tree_view_append_column (GTK_TREE_VIEW(self),column);
464 gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_cell_data,
467 /* Show the column headers,
468 * which does not seem to be the default on Maemo.
470 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(self), TRUE);
473 modest_signal_mgr_connect (priv->sig_handlers,
474 G_OBJECT (modest_runtime_get_account_store ()),
476 G_CALLBACK(on_account_removed),
479 modest_signal_mgr_connect (priv->sig_handlers,
480 G_OBJECT (modest_runtime_get_account_store ()),
482 G_CALLBACK(on_account_inserted),
485 modest_signal_mgr_connect (priv->sig_handlers,
486 G_OBJECT (modest_runtime_get_account_store ()),
488 G_CALLBACK(on_account_changed),
491 modest_signal_mgr_connect (priv->sig_handlers,
492 G_OBJECT(priv->account_mgr),
493 "account_busy_changed",
494 G_CALLBACK(on_account_busy_changed),
497 modest_signal_mgr_connect (priv->sig_handlers,
498 G_OBJECT(priv->account_mgr),
499 "default_account_changed",
500 G_CALLBACK(on_default_account_changed),
503 modest_signal_mgr_connect (priv->sig_handlers,
504 G_OBJECT(priv->account_mgr),
505 "display_name_changed",
506 G_CALLBACK(on_display_name_changed),
512 modest_account_view_new (ModestAccountMgr *account_mgr)
515 ModestAccountViewPrivate *priv;
517 g_return_val_if_fail (account_mgr, NULL);
519 obj = g_object_new(MODEST_TYPE_ACCOUNT_VIEW, NULL);
520 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
522 g_object_ref (G_OBJECT (account_mgr));
523 priv->account_mgr = account_mgr;
525 init_view (MODEST_ACCOUNT_VIEW (obj));
526 update_account_view (account_mgr, MODEST_ACCOUNT_VIEW (obj));
528 return MODEST_ACCOUNT_VIEW (obj);
532 modest_account_view_get_selected_account (ModestAccountView *self)
534 gchar *account_name = NULL;
535 GtkTreeSelection *sel;
539 g_return_val_if_fail (MODEST_IS_ACCOUNT_VIEW (self), NULL);
541 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
542 if (gtk_tree_selection_get_selected (sel, &model, &iter)) {
543 gtk_tree_model_get (model, &iter,
544 MODEST_ACCOUNT_VIEW_NAME_COLUMN,
551 /* This allows us to pass more than one piece of data to the signal handler,
552 * and get a result: */
555 ModestAccountView* self;
556 const gchar *account_name;
560 on_model_foreach_select_account(GtkTreeModel *model,
561 GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
563 ForEachData *state = (ForEachData*)(user_data);
565 /* Select the item if it has the matching account name: */
566 gchar *this_account_name = NULL;
567 gtk_tree_model_get (model, iter,
568 MODEST_ACCOUNT_VIEW_NAME_COLUMN, &this_account_name,
570 if(this_account_name && state->account_name
571 && (strcmp (this_account_name, state->account_name) == 0)) {
573 GtkTreeSelection *selection =
574 gtk_tree_view_get_selection (GTK_TREE_VIEW (state->self));
575 gtk_tree_selection_select_iter (selection, iter);
577 return TRUE; /* Stop walking the tree. */
580 return FALSE; /* Keep walking the tree. */
584 modest_account_view_select_account (ModestAccountView *account_view,
585 const gchar* account_name)
587 /* Create a state instance so we can send two items of data to the signal handler: */
588 ForEachData *state = g_new0 (ForEachData, 1);
589 state->self = account_view;
590 state->account_name = account_name;
592 GtkTreeModel *model = gtk_tree_view_get_model (
593 GTK_TREE_VIEW (account_view));
594 gtk_tree_model_foreach (model,
595 on_model_foreach_select_account, state);
601 modest_account_view_select_first_account (ModestAccountView *account_view)
603 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (account_view));
604 g_return_if_fail (model != NULL);
607 gtk_tree_model_get_iter_first (model, &iter);
609 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (account_view));
610 g_return_if_fail (selection != NULL);
612 gtk_tree_selection_select_iter (selection, &iter);
616 on_default_account_changed (ModestAccountMgr *mgr,
620 gchar *default_account_name;
621 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (user_data));
623 if (!gtk_tree_model_get_iter_first(model, &iter))
626 default_account_name = modest_account_mgr_get_default_account (mgr);
632 gtk_tree_model_get (model, &iter,
633 MODEST_ACCOUNT_VIEW_NAME_COLUMN, &name,
634 MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, &is_default,
637 /* Update the default account column */
638 if ((default_account_name != NULL) && (!strcmp (name, default_account_name)))
639 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
640 MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, TRUE, -1);
642 gtk_list_store_set (GTK_LIST_STORE (model), &iter,
643 MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, FALSE, -1);
647 } while (gtk_tree_model_iter_next(model, &iter));
649 /* Free and force a redraw */
650 g_free (default_account_name);
651 gtk_widget_queue_draw (GTK_WIDGET (user_data));
655 on_display_name_changed (ModestAccountMgr *mgr,
656 const gchar *account,
659 /* Update the view */
660 update_account_view (mgr, MODEST_ACCOUNT_VIEW (user_data));