X-Git-Url: http://git.maemo.org/git/?a=blobdiff_plain;f=src%2Fwidgets%2Fmodest-account-view.c;h=fbac3d9248123a1dc2fbfe120bbb5cf58b7b3b69;hb=fce141104a93d994fa67163492c07a1ea2cc8f72;hp=e388d7e98428dfdd6d29f761e34b18cf1a437d17;hpb=0942ff0db43cd65b3930247990f2ef9fab925793;p=modest diff --git a/src/widgets/modest-account-view.c b/src/widgets/modest-account-view.c index e388d7e..fbac3d9 100644 --- a/src/widgets/modest-account-view.c +++ b/src/widgets/modest-account-view.c @@ -27,22 +27,38 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include #include "modest-account-view.h" -/* include other impl specific header files */ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include /* For strcmp(). */ /* 'private'/'protected' functions */ static void modest_account_view_class_init (ModestAccountViewClass *klass); static void modest_account_view_init (ModestAccountView *obj); static void modest_account_view_finalize (GObject *obj); +static void modest_account_view_select_account (ModestAccountView *account_view, + const gchar* account_name); -enum _AccountViewColumns { - ENABLED_COLUMN, - NAME_COLUMN, - PROTO_COLUMN, - N_COLUMNS -}; -typedef enum _AccountViewColumns AccountViewColumns; +typedef enum { + MODEST_ACCOUNT_VIEW_NAME_COLUMN, + MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN, + MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN, + MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, + MODEST_ACCOUNT_VIEW_PROTO_COLUMN, + MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, + + MODEST_ACCOUNT_VIEW_COLUMN_NUM +} AccountViewColumns; /* list my signals */ @@ -55,6 +71,12 @@ enum { typedef struct _ModestAccountViewPrivate ModestAccountViewPrivate; struct _ModestAccountViewPrivate { ModestAccountMgr *account_mgr; + gulong sig1, sig2, sig3; + + /* When this is TRUE, we ignore configuration key changes. + * This is useful when making many changes. */ + gboolean block_conf_updates; + }; #define MODEST_ACCOUNT_VIEW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \ MODEST_TYPE_ACCOUNT_VIEW, \ @@ -80,6 +102,7 @@ modest_account_view_get_type (void) sizeof(ModestAccountView), 1, /* n_preallocs */ (GInstanceInitFunc) modest_account_view_init, + NULL }; my_type = g_type_register_static (GTK_TYPE_TREE_VIEW, "ModestAccountView", @@ -98,13 +121,6 @@ modest_account_view_class_init (ModestAccountViewClass *klass) gobject_class->finalize = modest_account_view_finalize; g_type_class_add_private (gobject_class, sizeof(ModestAccountViewPrivate)); - - /* signal definitions go here, e.g.: */ -/* signals[MY_SIGNAL_1] = */ -/* g_signal_new ("my_signal_1",....); */ -/* signals[MY_SIGNAL_2] = */ -/* g_signal_new ("my_signal_2",....); */ -/* etc. */ } static void @@ -115,6 +131,8 @@ modest_account_view_init (ModestAccountView *obj) priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj); priv->account_mgr = NULL; + priv->sig1 = 0; + priv->sig2 = 0; } static void @@ -123,14 +141,46 @@ modest_account_view_finalize (GObject *obj) ModestAccountViewPrivate *priv; priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj); - + if (priv->account_mgr) { + if (priv->sig1) + g_signal_handler_disconnect (modest_runtime_get_account_store (), + priv->sig1); + + if (priv->sig2) + g_signal_handler_disconnect (priv->account_mgr, priv->sig2); + + if (priv->sig3) + g_signal_handler_disconnect (priv->account_mgr, priv->sig3); + g_object_unref (G_OBJECT(priv->account_mgr)); priv->account_mgr = NULL; } + + G_OBJECT_CLASS(parent_class)->finalize (obj); } - +/* Get the string for the last updated time. Result must be g_freed */ +static gchar* +get_last_updated_string(ModestAccountMgr* account_mgr, ModestAccountData *account_data) +{ + /* FIXME: let's assume that 'last update' applies to the store account... */ + gchar* last_updated_string; + time_t last_updated = account_data->store_account->last_updated; + if (!modest_account_mgr_account_is_busy(account_mgr, account_data->account_name)) + { + if (last_updated > 0) + last_updated_string = modest_text_utils_get_display_date(last_updated); + else + last_updated_string = g_strdup (_("mcen_va_never")); + } + else + { + /* FIXME: There should be a logical name in the UI specs */ + last_updated_string = g_strdup(_("Refreshing...")); + } + return last_updated_string; +} static void update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view) @@ -138,73 +188,147 @@ update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view) GSList *account_names, *cursor; GtkListStore *model; - model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(view))); + model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(view))); + + /* Get the ID of the currently-selected account, + * so we can select it again after rebuilding the list. + * Note that the name doesn't change even when the display name changes. + */ + gchar *selected_name = modest_account_view_get_selected_account (view); + gtk_list_store_clear (model); - cursor = account_names = - modest_account_mgr_account_names (account_mgr, NULL); + /* Note: We do not show disabled accounts. + * Of course, this means that there is no UI to enable or disable + * accounts. That is OK for maemo where no such feature or UI is + * specified, so the "enabled" property is used internally to avoid + * showing unfinished accounts. If a user-visible "enabled" is + * needed in the future, we must use a second property for the + * current use instead */ + cursor = account_names = modest_account_mgr_account_names (account_mgr, + TRUE /* only enabled accounts. */); - while (cursor) { - gchar *proto = NULL; - gchar *store, *account_name; - gboolean enabled; + if (!account_names) + g_warning ("debug: modest_account_mgr_account_names() returned NULL\n"); + while (cursor) { + gchar *account_name; + ModestAccountData *account_data; + account_name = (gchar*)cursor->data; - store = modest_account_mgr_get_string (account_mgr, - account_name, - MODEST_ACCOUNT_STORE_ACCOUNT, - FALSE, NULL); - if (store) { - proto = modest_account_mgr_get_string (account_mgr, - store, - MODEST_ACCOUNT_PROTO, - TRUE, NULL); - g_free(store); + account_data = modest_account_mgr_get_account_data (account_mgr, account_name); + if (!account_data) { + g_printerr ("modest: failed to get account data for %s\n", account_name); + continue; } - - enabled = modest_account_mgr_account_get_enabled (account_mgr, - account_name, - FALSE); - gtk_list_store_insert_with_values ( - model, NULL, 0, - ENABLED_COLUMN, enabled, - NAME_COLUMN, account_name, - PROTO_COLUMN, proto, - -1); - - g_free (account_name); - g_free (proto); - + + /* don't display accounts without stores */ + if (account_data->store_account) { + + GtkTreeIter iter; + + gchar *last_updated_string = get_last_updated_string(account_mgr, account_data); + + if (account_data->is_enabled) { + gtk_list_store_insert_with_values ( + model, &iter, 0, + MODEST_ACCOUNT_VIEW_NAME_COLUMN, account_name, + MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN, account_data->display_name, + MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN, account_data->is_enabled, + MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, account_data->is_default, + + MODEST_ACCOUNT_VIEW_PROTO_COLUMN, + modest_protocol_info_get_transport_store_protocol_name (account_data->store_account->proto), + + MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, last_updated_string, + -1); + } + g_free (last_updated_string); + } + + modest_account_mgr_free_account_data (account_mgr, account_data); cursor = cursor->next; } - g_slist_free (account_names); + + modest_account_mgr_free_account_names (account_names); + account_names = NULL; + + /* Try to re-select the same account: */ + if (selected_name) { + modest_account_view_select_account (view, selected_name); + g_free (selected_name); + } } static void on_account_changed (ModestAccountMgr *account_mgr, - const gchar* account, const gchar* key, + const gchar* account, GSList *keys, gboolean server_account, ModestAccountView *self) -{ - ModestAccountViewPrivate *priv; - priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self); - +{ + /* Never update the view in response to gconf changes. + * Always do it explicitly instead. + * This is because we have no way to avoid 10 updates when changing + * 10 items, and this blocks the UI. + * + * But this block/unblock API might be useful on platforms where the + * notification does not happen so long after the key was set. + * (We have no way to know when the last key was set, to do a final update).. + */ + update_account_view (account_mgr, self); } +static void +on_account_busy_changed(ModestAccountMgr *account_mgr, const gchar *account_name, + gboolean busy, ModestAccountView *self) +{ + GtkListStore *model = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(self))); + GtkTreeIter iter; + g_message(__FUNCTION__); + if (!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(model), &iter)) + return; + do + { + gchar* cur_name; + gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, MODEST_ACCOUNT_VIEW_NAME_COLUMN, + &cur_name, -1); + if (g_str_equal(cur_name, account_name)) + { + ModestAccountData* account_data = + modest_account_mgr_get_account_data (account_mgr, account_name); + if (!account_data) + return; + gchar* last_updated_string = get_last_updated_string(account_mgr, account_data); + gtk_list_store_set(model, &iter, + MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, last_updated_string, + -1); + g_free (last_updated_string); + modest_account_mgr_free_account_data (account_mgr, account_data); + return; + } + } + while (gtk_tree_model_iter_next(GTK_TREE_MODEL(model), &iter)); +} static void -on_account_removed (ModestAccountMgr *account_mgr, - const gchar* account, gboolean server_account, +on_account_removed (TnyAccountStore *account_store, + TnyAccount *account, gpointer user_data) { - on_account_changed (account_mgr, account, NULL, server_account, user_data); -} + ModestAccountView *self; + ModestAccountViewPrivate *priv; + self = MODEST_ACCOUNT_VIEW (user_data); + priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE (self); + update_account_view (priv->account_mgr, MODEST_ACCOUNT_VIEW (user_data)); +} +/* currently unused */ +#if 0 static void on_account_enable_toggled (GtkCellRendererToggle *cell_renderer, gchar *path, ModestAccountView *self) @@ -222,66 +346,164 @@ on_account_enable_toggled (GtkCellRendererToggle *cell_renderer, gchar *path, g_printerr ("modest: cannot find iterator\n"); return; } - - gtk_tree_model_get (model, &iter, - ENABLED_COLUMN, &enabled, - NAME_COLUMN, &account_name, + gtk_tree_model_get (model, &iter, MODEST_ACCOUNT_VIEW_IS_ENABLED_COLUMN, &enabled, + MODEST_ACCOUNT_VIEW_NAME_COLUMN, &account_name, -1); /* toggle enabled / disabled */ - modest_account_mgr_account_set_enabled (priv->account_mgr, account_name, - FALSE, !enabled); + modest_account_mgr_set_enabled (priv->account_mgr, account_name, !enabled); g_free (account_name); } +#endif + +static gboolean +find_default_account(ModestAccountView *self, GtkTreeIter *iter) +{ + GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (self)); + gboolean result; + for (result = gtk_tree_model_get_iter_first(model, iter); + result == TRUE; result = gtk_tree_model_iter_next(model, iter)) + { + gboolean is_default; + gtk_tree_model_get (model, iter, MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, &is_default, -1); + if(is_default) + return TRUE; + } + return FALSE; +} + +static void +on_account_default_toggled (GtkCellRendererToggle *cell_renderer, gchar *path, + ModestAccountView *self) +{ + gboolean is_default = gtk_cell_renderer_toggle_get_active (cell_renderer); + if (is_default) { + /* Do not allow an account to be marked non-default. + * Only allow this to be changed by setting another account to default: */ + gtk_cell_renderer_toggle_set_active (cell_renderer, TRUE); + return; + } + + ModestAccountViewPrivate *priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self); + GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW(self)); + + GtkTreeIter iter; + if (!gtk_tree_model_get_iter_from_string (model, &iter, path)) { + g_printerr ("modest: cannot find iterator\n"); + return; + } + + gchar *account_name = NULL; + gtk_tree_model_get (model, &iter, MODEST_ACCOUNT_VIEW_NAME_COLUMN, &account_name, + -1); + + /* Set this previously-non-default account as the default: */ + if (modest_account_mgr_set_default_account (priv->account_mgr, account_name)) + { + /* Explicitely set default column because we are ignoring gconf changes */ + GtkTreeIter old_default_iter; + if (find_default_account (self, &old_default_iter)) { + gtk_list_store_set (GTK_LIST_STORE (model), &old_default_iter, + MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, FALSE, -1); + } else { + g_warning ("%s: Did not find old default account in view", __FUNCTION__); + } + + gtk_list_store_set (GTK_LIST_STORE (model), &iter, + MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, TRUE, -1); + } + + g_free (account_name); +} + +void +bold_if_default_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer, + GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer user_data) +{ + gboolean is_default; + gtk_tree_model_get (tree_model, iter, MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, + &is_default, -1); + g_object_set (G_OBJECT(renderer), + "weight", is_default ? 800: 400, + NULL); +} static void init_view (ModestAccountView *self) { ModestAccountViewPrivate *priv; - GtkTreeSelection *sel; - GtkCellRenderer *renderer; + GtkCellRenderer *toggle_renderer, *text_renderer; GtkListStore *model; + GtkTreeViewColumn *column; priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self); - model = gtk_list_store_new (3, - G_TYPE_BOOLEAN, /* checkbox */ + model = gtk_list_store_new (6, G_TYPE_STRING, /* account name */ - G_TYPE_STRING); /* account type (pop, imap,...) */ + G_TYPE_STRING, /* account display name */ + G_TYPE_BOOLEAN, /* is-enabled */ + G_TYPE_BOOLEAN, /* is-default */ + G_TYPE_STRING, /* account proto (pop, imap,...) */ + G_TYPE_STRING /* last updated (time_t) */ + ); + + gtk_tree_sortable_set_sort_column_id ( + GTK_TREE_SORTABLE (model), MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN, + GTK_SORT_ASCENDING); gtk_tree_view_set_model (GTK_TREE_VIEW(self), GTK_TREE_MODEL(model)); + g_object_unref (G_OBJECT (model)); - renderer = gtk_cell_renderer_toggle_new (); - g_object_set (G_OBJECT(renderer), "activatable", TRUE,"radio", FALSE, NULL); + toggle_renderer = gtk_cell_renderer_toggle_new (); + text_renderer = gtk_cell_renderer_text_new (); - g_signal_connect (G_OBJECT(renderer), "toggled", - G_CALLBACK(on_account_enable_toggled), - self); - + /* the is_default column */ + g_object_set (G_OBJECT(toggle_renderer), "activatable", TRUE, "radio", TRUE, NULL); gtk_tree_view_append_column (GTK_TREE_VIEW(self), gtk_tree_view_column_new_with_attributes ( - _("Enabled"), renderer, - "active", ENABLED_COLUMN, NULL)); - gtk_tree_view_append_column (GTK_TREE_VIEW(self), - gtk_tree_view_column_new_with_attributes ( - _("Account"), - gtk_cell_renderer_text_new (), - "text", NAME_COLUMN, NULL)); - gtk_tree_view_append_column (GTK_TREE_VIEW(self), - gtk_tree_view_column_new_with_attributes ( - _("Type"), - gtk_cell_renderer_text_new (), - "text", PROTO_COLUMN, NULL)); - - sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(self)); + _("mcen_ti_default"), toggle_renderer, + "active", MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, NULL)); + + /* Disable the Maemo GtkTreeView::allow-checkbox-mode Maemo modification, + * which causes the model column to be updated automatically when the row is clicked. + * Making this the default in Maemo's GTK+ is obviously a bug: + * https://maemo.org/bugzilla/show_bug.cgi?id=146 + * + * djcb: indeed, they have been removed for post-bora, i added the ifdefs... + */ +#ifdef MODEST_HILDON_VERSION_0 + g_object_set(G_OBJECT(self), "allow-checkbox-mode", FALSE, NULL); + g_object_set(G_OBJECT(toggle_renderer), "checkbox-mode", FALSE, NULL); +#endif /*MODEST_HILDON_VERSION_0 */ + g_signal_connect (G_OBJECT(toggle_renderer), "toggled", G_CALLBACK(on_account_default_toggled), + self); - g_signal_connect (G_OBJECT(priv->account_mgr), - "account_removed", - G_CALLBACK(on_account_removed), self); - g_signal_connect (G_OBJECT(priv->account_mgr), - "account_changed", - G_CALLBACK(on_account_changed), self); + /* account name */ + column = gtk_tree_view_column_new_with_attributes (_("mcen_ti_account"), text_renderer, "text", + MODEST_ACCOUNT_VIEW_DISPLAY_NAME_COLUMN, NULL); + gtk_tree_view_append_column (GTK_TREE_VIEW(self), column); + gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_cell_data, + NULL, NULL); + + /* last update for this account */ + column = gtk_tree_view_column_new_with_attributes (_("mcen_ti_lastupdated"), text_renderer,"text", + MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, NULL); + gtk_tree_view_append_column (GTK_TREE_VIEW(self),column); + gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_cell_data, + NULL, NULL); + + /* Show the column headers, + * which does not seem to be the default on Maemo. + */ + gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(self), TRUE); + + priv->sig1 = g_signal_connect (G_OBJECT (modest_runtime_get_account_store ()),"account_removed", + G_CALLBACK(on_account_removed), self); + priv->sig2 = g_signal_connect (G_OBJECT(priv->account_mgr), "account_changed", + G_CALLBACK(on_account_changed), self); + priv->sig3 = g_signal_connect (G_OBJECT(priv->account_mgr), "account_busy_changed", + G_CALLBACK(on_account_busy_changed), self); } @@ -297,11 +519,80 @@ modest_account_view_new (ModestAccountMgr *account_mgr) obj = g_object_new(MODEST_TYPE_ACCOUNT_VIEW, NULL); priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj); - g_object_ref (G_OBJECT(account_mgr)); + g_object_ref (G_OBJECT (account_mgr)); priv->account_mgr = account_mgr; init_view (MODEST_ACCOUNT_VIEW (obj)); + update_account_view (account_mgr, MODEST_ACCOUNT_VIEW (obj)); + + return MODEST_ACCOUNT_VIEW (obj); +} + +gchar * +modest_account_view_get_selected_account (ModestAccountView *self) +{ + gchar *account_name = NULL; + GtkTreeSelection *sel; + GtkTreeModel *model; + GtkTreeIter iter; + + g_return_val_if_fail (MODEST_IS_ACCOUNT_VIEW (self), NULL); - return MODEST_ACCOUNT_VIEW(obj); + sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self)); + if (gtk_tree_selection_get_selected (sel, &model, &iter)) { + gtk_tree_model_get (model, &iter, + MODEST_ACCOUNT_VIEW_NAME_COLUMN, + &account_name, -1); + } + + return account_name; +} + +/* This allows us to pass more than one piece of data to the signal handler, + * and get a result: */ +typedef struct +{ + ModestAccountView* self; + const gchar *account_name; +} ForEachData; + +static gboolean +on_model_foreach_select_account(GtkTreeModel *model, + GtkTreePath *path, GtkTreeIter *iter, gpointer user_data) +{ + ForEachData *state = (ForEachData*)(user_data); + + /* Select the item if it has the matching account name: */ + gchar *this_account_name = NULL; + gtk_tree_model_get (model, iter, + MODEST_ACCOUNT_VIEW_NAME_COLUMN, &this_account_name, + -1); + if(this_account_name && state->account_name + && (strcmp (this_account_name, state->account_name) == 0)) { + + GtkTreeSelection *selection = + gtk_tree_view_get_selection (GTK_TREE_VIEW (state->self)); + gtk_tree_selection_select_iter (selection, iter); + + return TRUE; /* Stop walking the tree. */ + } + + return FALSE; /* Keep walking the tree. */ +} + +static void modest_account_view_select_account (ModestAccountView *account_view, + const gchar* account_name) +{ + /* Create a state instance so we can send two items of data to the signal handler: */ + ForEachData *state = g_new0 (ForEachData, 1); + state->self = account_view; + state->account_name = account_name; + + GtkTreeModel *model = gtk_tree_view_get_model ( + GTK_TREE_VIEW (account_view)); + gtk_tree_model_foreach (model, + on_model_foreach_select_account, state); + + g_free (state); }