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);
63 static void on_account_updated (ModestAccountMgr* mgr, gchar* account_name,
67 MODEST_ACCOUNT_VIEW_NAME_COLUMN,
68 MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN,
69 MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN,
70 MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,
71 MODEST_ACCOUNT_VIEW_PROTO_COLUMN,
72 MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN,
74 MODEST_ACCOUNT_VIEW_COLUMN_NUM
77 typedef struct _ModestAccountViewPrivate ModestAccountViewPrivate;
78 struct _ModestAccountViewPrivate {
79 ModestAccountMgr *account_mgr;
84 #define MODEST_ACCOUNT_VIEW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
85 MODEST_TYPE_ACCOUNT_VIEW, \
86 ModestAccountViewPrivate))
88 static GtkTreeViewClass *parent_class = NULL;
91 modest_account_view_get_type (void)
93 static GType my_type = 0;
95 static const GTypeInfo my_info = {
96 sizeof(ModestAccountViewClass),
98 NULL, /* base finalize */
99 (GClassInitFunc) modest_account_view_class_init,
100 NULL, /* class finalize */
101 NULL, /* class data */
102 sizeof(ModestAccountView),
104 (GInstanceInitFunc) modest_account_view_init,
107 my_type = g_type_register_static (GTK_TYPE_TREE_VIEW,
115 modest_account_view_class_init (ModestAccountViewClass *klass)
117 GObjectClass *gobject_class;
118 gobject_class = (GObjectClass*) klass;
120 parent_class = g_type_class_peek_parent (klass);
121 gobject_class->finalize = modest_account_view_finalize;
123 g_type_class_add_private (gobject_class, sizeof(ModestAccountViewPrivate));
127 modest_account_view_init (ModestAccountView *obj)
129 ModestAccountViewPrivate *priv;
131 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
133 priv->sig_handlers = NULL;
137 modest_account_view_finalize (GObject *obj)
139 ModestAccountViewPrivate *priv;
141 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
143 /* Disconnect signals */
144 modest_signal_mgr_disconnect_all_and_destroy (priv->sig_handlers);
146 if (priv->account_mgr) {
147 g_object_unref (G_OBJECT(priv->account_mgr));
148 priv->account_mgr = NULL;
151 G_OBJECT_CLASS(parent_class)->finalize (obj);
154 /* Get the string for the last updated time. Result must NOT be g_freed */
156 get_last_updated_string(ModestAccountMgr* account_mgr, ModestAccountData *account_data)
158 /* FIXME: let's assume that 'last update' applies to the store account... */
159 const gchar* last_updated_string;
160 time_t last_updated = account_data->store_account->last_updated;
161 if (!modest_account_mgr_account_is_busy(account_mgr, account_data->account_name)) {
162 if (last_updated > 0)
163 last_updated_string = modest_text_utils_get_display_date(last_updated);
165 last_updated_string = _("mcen_va_never");
167 /* FIXME: There should be a logical name in the UI specs */
168 last_updated_string = _("mcen_va_refreshing");
171 return last_updated_string;
175 update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
177 GSList *account_names, *cursor;
180 model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(view)));
182 /* Get the ID of the currently-selected account,
183 * so we can select it again after rebuilding the list.
184 * Note that the name doesn't change even when the display name changes.
186 gchar *selected_name = modest_account_view_get_selected_account (view);
188 gtk_list_store_clear (model);
190 /* Note: We do not show disabled accounts.
191 * Of course, this means that there is no UI to enable or disable
192 * accounts. That is OK for maemo where no such feature or UI is
193 * specified, so the "enabled" property is used internally to avoid
194 * showing unfinished accounts. If a user-visible "enabled" is
195 * needed in the future, we must use a second property for the
196 * current use instead */
197 cursor = account_names = modest_account_mgr_account_names (account_mgr,
198 TRUE /* only enabled accounts. */);
202 ModestAccountData *account_data;
204 account_name = (gchar*)cursor->data;
206 account_data = modest_account_mgr_get_account_data (account_mgr, account_name);
208 g_printerr ("modest: failed to get account data for %s\n", account_name);
212 /* don't display accounts without stores */
213 if (account_data->store_account) {
218 const gchar *last_updated_string = get_last_updated_string(account_mgr, account_data);
220 if (account_data->is_enabled) {
221 const gchar *proto_name;
223 proto_name = modest_protocol_info_get_transport_store_protocol_name (account_data->store_account->proto);
224 gtk_list_store_insert_with_values (
226 MODEST_ACCOUNT_VIEW_NAME_COLUMN, account_name,
227 MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN, account_data->display_name,
228 MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN, account_data->is_enabled,
229 MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, account_data->is_default,
230 MODEST_ACCOUNT_VIEW_PROTO_COLUMN, proto_name,
231 MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, last_updated_string,
236 modest_account_mgr_free_account_data (account_mgr, account_data);
237 cursor = cursor->next;
240 modest_account_mgr_free_account_names (account_names);
241 account_names = NULL;
243 /* Try to re-select the same account: */
245 modest_account_view_select_account (view, selected_name);
246 g_free (selected_name);
251 on_account_busy_changed(ModestAccountMgr *account_mgr,
252 const gchar *account_name,
254 ModestAccountView *self)
256 GtkListStore *model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(self)));
258 gboolean found = FALSE;
260 if (!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(model), &iter))
265 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter,
266 MODEST_ACCOUNT_VIEW_NAME_COLUMN,
269 if (g_str_equal(cur_name, account_name)) {
270 ModestAccountData* account_data =
271 modest_account_mgr_get_account_data (account_mgr, account_name);
276 const gchar* last_updated_string = get_last_updated_string(account_mgr, account_data);
277 gtk_list_store_set(model, &iter,
278 MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, last_updated_string,
280 modest_account_mgr_free_account_data (account_mgr, account_data);
285 } while (!found && gtk_tree_model_iter_next(GTK_TREE_MODEL(model), &iter));
289 on_account_inserted (TnyAccountStore *account_store,
293 ModestAccountView *self;
294 ModestAccountViewPrivate *priv;
296 g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (user_data));
298 self = MODEST_ACCOUNT_VIEW (user_data);
299 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE (self);
301 /* Do not refresh the view with transport accounts */
302 if (TNY_IS_STORE_ACCOUNT (account))
303 update_account_view (priv->account_mgr, self);
307 on_account_removed (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 gchar *selected_name = modest_account_view_get_selected_account (self);
320 if (selected_name == NULL) {
321 /* we select the first account if none is selected */
322 modest_account_view_select_first_account (self);
324 g_free (selected_name);
327 update_account_view (priv->account_mgr, self);
332 on_account_changed (TnyAccountStore *account_store,
336 ModestAccountView *self = NULL;
337 ModestAccountViewPrivate *priv = NULL;
339 g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (user_data));
340 g_return_if_fail (account);
341 g_return_if_fail (TNY_IS_ACCOUNT (account));
343 self = MODEST_ACCOUNT_VIEW (user_data);
344 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE (self);
346 /* Update account view */
347 /* update_account_view (priv->account_mgr, self); */
351 on_account_default_toggled (GtkCellRendererToggle *cell_renderer,
353 ModestAccountView *self)
355 ModestAccountViewPrivate *priv;
358 gchar *account_name = NULL;
360 g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (self));
362 /* If it's active then do nothing, no need to reenable it as
364 if (gtk_cell_renderer_toggle_get_active (cell_renderer))
367 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
368 model = gtk_tree_view_get_model (GTK_TREE_VIEW(self));
369 gtk_tree_model_get_iter_from_string (model, &iter, path);
371 gtk_tree_model_get (model, &iter,
372 MODEST_ACCOUNT_VIEW_NAME_COLUMN,
375 /* Set this previously-non-default account as the
376 default. We're not updating here the value of the
377 DEFAULT_COLUMN because we'll do it in the
378 "default_account_changed" signal handler. We do it like
379 this because that way the signal handler is useful also
380 when we're inserting a new account and there is no other
381 one defined, in that case the change of account is provoked
382 by the account mgr and not by a signal toggle.*/
383 modest_account_mgr_set_default_account (priv->account_mgr, account_name);
385 g_free (account_name);
389 on_account_updated (ModestAccountMgr* mgr,
393 update_account_view (mgr, MODEST_ACCOUNT_VIEW (user_data));
397 bold_if_default_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
398 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer user_data)
401 gtk_tree_model_get (tree_model, iter, MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,
403 g_object_set (G_OBJECT(renderer),
404 "weight", is_default ? 800: 400,
409 init_view (ModestAccountView *self)
411 ModestAccountViewPrivate *priv;
412 GtkCellRenderer *toggle_renderer, *text_renderer;
414 GtkTreeViewColumn *column;
416 g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (self));
417 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
419 model = gtk_list_store_new (6,
420 G_TYPE_STRING, /* account name */
421 G_TYPE_STRING, /* account display name */
422 G_TYPE_BOOLEAN, /* is-enabled */
423 G_TYPE_BOOLEAN, /* is-default */
424 G_TYPE_STRING, /* account proto (pop, imap,...) */
425 G_TYPE_STRING /* last updated (time_t) */
428 gtk_tree_sortable_set_sort_column_id (
429 GTK_TREE_SORTABLE (model), MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN,
432 gtk_tree_view_set_model (GTK_TREE_VIEW(self), GTK_TREE_MODEL(model));
433 g_object_unref (G_OBJECT (model));
435 toggle_renderer = gtk_cell_renderer_toggle_new ();
436 text_renderer = gtk_cell_renderer_text_new ();
438 /* the is_default column */
439 g_object_set (G_OBJECT(toggle_renderer), "activatable", TRUE, "radio", TRUE, NULL);
440 gtk_tree_view_append_column (GTK_TREE_VIEW(self),
441 gtk_tree_view_column_new_with_attributes (
442 _("mcen_ti_default"), toggle_renderer,
443 "active", MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, NULL));
445 /* Disable the Maemo GtkTreeView::allow-checkbox-mode Maemo modification,
446 * which causes the model column to be updated automatically when the row is clicked.
447 * Making this the default in Maemo's GTK+ is obviously a bug:
448 * https://maemo.org/bugzilla/show_bug.cgi?id=146
450 * djcb: indeed, they have been removed for post-bora, i added the ifdefs...
452 #ifdef MODEST_HAVE_HILDON0_WIDGETS
453 g_object_set(G_OBJECT(self), "allow-checkbox-mode", FALSE, NULL);
454 g_object_set(G_OBJECT(toggle_renderer), "checkbox-mode", FALSE, NULL);
455 #endif /* MODEST_HAVE_HILDON0_WIDGETS */
458 modest_signal_mgr_connect (priv->sig_handlers,
459 G_OBJECT(toggle_renderer),
461 G_CALLBACK(on_account_default_toggled),
465 column = gtk_tree_view_column_new_with_attributes (_("mcen_ti_account"), text_renderer, "text",
466 MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN, NULL);
467 gtk_tree_view_append_column (GTK_TREE_VIEW(self), column);
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_cell_data_func(column, text_renderer, bold_if_default_cell_data,
478 /* Show the column headers,
479 * which does not seem to be the default on Maemo.
481 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(self), TRUE);
484 modest_signal_mgr_connect (priv->sig_handlers,
485 G_OBJECT (modest_runtime_get_account_store ()),
487 G_CALLBACK(on_account_removed),
490 modest_signal_mgr_connect (priv->sig_handlers,
491 G_OBJECT (modest_runtime_get_account_store ()),
493 G_CALLBACK(on_account_inserted),
496 modest_signal_mgr_connect (priv->sig_handlers,
497 G_OBJECT (modest_runtime_get_account_store ()),
499 G_CALLBACK(on_account_changed),
502 modest_signal_mgr_connect (priv->sig_handlers,
503 G_OBJECT(priv->account_mgr),
504 "account_busy_changed",
505 G_CALLBACK(on_account_busy_changed),
508 modest_signal_mgr_connect (priv->sig_handlers,
509 G_OBJECT(priv->account_mgr),
510 "default_account_changed",
511 G_CALLBACK(on_default_account_changed),
514 modest_signal_mgr_connect (priv->sig_handlers,
515 G_OBJECT(priv->account_mgr),
516 "display_name_changed",
517 G_CALLBACK(on_display_name_changed),
520 modest_signal_mgr_connect (priv->sig_handlers,
521 G_OBJECT (priv->account_mgr),
523 G_CALLBACK (on_account_updated),
529 modest_account_view_new (ModestAccountMgr *account_mgr)
532 ModestAccountViewPrivate *priv;
534 g_return_val_if_fail (account_mgr, NULL);
536 obj = g_object_new(MODEST_TYPE_ACCOUNT_VIEW, NULL);
537 priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
539 g_object_ref (G_OBJECT (account_mgr));
540 priv->account_mgr = account_mgr;
542 init_view (MODEST_ACCOUNT_VIEW (obj));
543 update_account_view (account_mgr, MODEST_ACCOUNT_VIEW (obj));
545 return MODEST_ACCOUNT_VIEW (obj);
549 modest_account_view_get_selected_account (ModestAccountView *self)
551 gchar *account_name = NULL;
552 GtkTreeSelection *sel;
556 g_return_val_if_fail (MODEST_IS_ACCOUNT_VIEW (self), NULL);
558 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
559 if (gtk_tree_selection_get_selected (sel, &model, &iter)) {
560 gtk_tree_model_get (model, &iter,
561 MODEST_ACCOUNT_VIEW_NAME_COLUMN,
568 /* This allows us to pass more than one piece of data to the signal handler,
569 * and get a result: */
572 ModestAccountView* self;
573 const gchar *account_name;
577 on_model_foreach_select_account(GtkTreeModel *model,
578 GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
580 ForEachData *state = (ForEachData*)(user_data);
582 /* Select the item if it has the matching account name: */
583 gchar *this_account_name = NULL;
584 gtk_tree_model_get (model, iter,
585 MODEST_ACCOUNT_VIEW_NAME_COLUMN, &this_account_name,
587 if(this_account_name && state->account_name
588 && (strcmp (this_account_name, state->account_name) == 0)) {
590 GtkTreeSelection *selection =
591 gtk_tree_view_get_selection (GTK_TREE_VIEW (state->self));
592 gtk_tree_selection_select_iter (selection, iter);
594 return TRUE; /* Stop walking the tree. */
597 return FALSE; /* Keep walking the tree. */
601 modest_account_view_select_account (ModestAccountView *account_view,
602 const gchar* account_name)
604 /* Create a state instance so we can send two items of data to the signal handler: */
605 ForEachData *state = g_new0 (ForEachData, 1);
606 state->self = account_view;
607 state->account_name = account_name;
609 GtkTreeModel *model = gtk_tree_view_get_model (
610 GTK_TREE_VIEW (account_view));
611 gtk_tree_model_foreach (model,
612 on_model_foreach_select_account, state);
618 modest_account_view_select_first_account (ModestAccountView *account_view)
620 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (account_view));
621 g_return_if_fail (model != NULL);
624 gtk_tree_model_get_iter_first (model, &iter);
626 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (account_view));
627 g_return_if_fail (selection != NULL);
629 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));