* Do not try to select accounts in Fremantle
[modest] / src / widgets / modest-account-view.c
index a634895..b4e4765 100644 (file)
 #include <gtk/gtkliststore.h>
 #include <string.h> /* For strcmp(). */
 #include <modest-account-mgr-helpers.h>
+#include <modest-datetime-formatter.h>
+#ifdef MODEST_TOOLKIT_HILDON2
+#include <hildon/hildon-defines.h>
+#endif
 
 /* 'private'/'protected' functions */
 static void modest_account_view_class_init    (ModestAccountViewClass *klass);
@@ -59,10 +63,14 @@ static void on_display_name_changed            (ModestAccountMgr *self,
                                                const gchar *account,
                                                gpointer user_data);
 
+#ifndef MODEST_TOOLKIT_HILDON2
 static void modest_account_view_select_first_account (ModestAccountView *account_view);
+#endif
 
 static void on_account_updated (ModestAccountMgr* mgr, gchar* account_name,
                     gpointer user_data);
+static void update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view);
+static void on_notify_style (GObject *obj, GParamSpec *spec, gpointer userdata);
 
 typedef enum {
        MODEST_ACCOUNT_VIEW_NAME_COLUMN,
@@ -79,6 +87,8 @@ typedef struct _ModestAccountViewPrivate ModestAccountViewPrivate;
 struct _ModestAccountViewPrivate {
        ModestAccountMgr *account_mgr;
 
+       ModestDatetimeFormatter *datetime_formatter;
+
        /* Signal handlers */
        GSList *sig_handlers;
 };
@@ -125,6 +135,16 @@ modest_account_view_class_init (ModestAccountViewClass *klass)
 }
 
 static void
+datetime_format_changed (ModestDatetimeFormatter *formatter,
+                        ModestAccountView *self)
+{
+       ModestAccountViewPrivate *priv;
+       
+       priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(self);
+       update_account_view (priv->account_mgr, self);
+}
+
+static void
 modest_account_view_init (ModestAccountView *obj)
 {
        ModestAccountViewPrivate *priv;
@@ -132,6 +152,18 @@ modest_account_view_init (ModestAccountView *obj)
        priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
        
        priv->sig_handlers = NULL;
+
+       priv->datetime_formatter = modest_datetime_formatter_new ();
+       g_signal_connect (G_OBJECT (priv->datetime_formatter), "format-changed", 
+                         G_CALLBACK (datetime_format_changed), (gpointer) obj);
+#ifdef MODEST_TOOLKIT_HILDON2
+       gtk_rc_parse_string ("style \"fremantle-modest-account-view\" {\n"
+                            "  GtkWidget::hildon-mode = 1\n"
+                            "} widget \"*.fremantle-modest-account-view\" style \"fremantle-modest-account-view\""
+                            "widget_class \"*<HildonPannableArea>.ModestAccountView\" style :highest \"fremantle-modest-account-view\"");
+       
+#endif
+       g_signal_connect (G_OBJECT (obj), "notify::style", G_CALLBACK (on_notify_style), (gpointer) obj);
 }
 
 static void
@@ -141,6 +173,11 @@ modest_account_view_finalize (GObject *obj)
 
        priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
 
+       if (priv->datetime_formatter) {
+               g_object_unref (priv->datetime_formatter);
+               priv->datetime_formatter = NULL;
+       }
+
        /* Disconnect signals */
        modest_signal_mgr_disconnect_all_and_destroy (priv->sig_handlers);
 
@@ -154,7 +191,7 @@ modest_account_view_finalize (GObject *obj)
 
 /* Get the string for the last updated time. Result must NOT be g_freed */
 static const gchar*
-get_last_updated_string(ModestAccountMgr* account_mgr, ModestAccountSettings *settings)
+get_last_updated_string(ModestAccountView *self, ModestAccountMgr* account_mgr, ModestAccountSettings *settings)
 {
        /* FIXME: let's assume that 'last update' applies to the store account... */
        const gchar *last_updated_string;
@@ -162,19 +199,23 @@ get_last_updated_string(ModestAccountMgr* account_mgr, ModestAccountSettings *se
        const gchar *account_name;
        time_t last_updated;
        ModestServerAccountSettings *server_settings;
+       ModestAccountViewPrivate *priv;
 
+       priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE (self);
        server_settings = modest_account_settings_get_store_settings (settings);
        store_account_name = modest_server_account_settings_get_account_name (server_settings);
        last_updated = modest_account_mgr_get_last_updated (account_mgr, store_account_name);
        g_object_unref (server_settings);
        account_name = modest_account_settings_get_account_name (settings);
        if (!modest_account_mgr_account_is_busy(account_mgr, account_name)) {
-               if (last_updated > 0) 
-                       last_updated_string = modest_text_utils_get_display_date(last_updated);
-               else
+               if (last_updated > 0) {
+                       last_updated_string = 
+                               modest_datetime_formatter_display_datetime (priv->datetime_formatter,
+                                                                          last_updated);
+               } else {
                        last_updated_string = _("mcen_va_never");
+               }
        } else  {
-               /* FIXME: There should be a logical name in the UI specs */
                last_updated_string = _("mcen_va_refreshing");
        }
        
@@ -217,6 +258,7 @@ update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
                settings = modest_account_mgr_load_account_settings (account_mgr, account_name);
                if (!settings) {
                        g_printerr ("modest: failed to get account data for %s\n", account_name);
+                       cursor = cursor->next;
                        continue;
                }
                store_settings = modest_account_settings_get_store_settings (settings);
@@ -227,13 +269,24 @@ update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
                        GtkTreeIter iter;
 
                        /* don't free */
-                       const gchar *last_updated_string = get_last_updated_string(account_mgr, settings);
+                       const gchar *last_updated_string = get_last_updated_string(view, account_mgr, settings);
                        
                        if (modest_account_settings_get_enabled (settings)) {
+                               ModestProtocolType protocol_type;
+                               ModestProtocolRegistry *protocol_registry;
+                               ModestProtocol *protocol;
                                const gchar *proto_name;
-
-                               proto_name = modest_protocol_info_get_transport_store_protocol_name 
-                                       (modest_server_account_settings_get_protocol (store_settings));
+#ifdef MODEST_TOOLKIT_HILDON2
+                               gchar *last_updated_hildon2;
+
+                               last_updated_hildon2 = g_strconcat (_("mcen_ti_lastupdated"), "\n", 
+                                                                  last_updated_string,
+                                                                  NULL);
+#endif
+                               protocol_registry = modest_runtime_get_protocol_registry ();
+                               protocol_type = modest_server_account_settings_get_protocol (store_settings);
+                               protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, protocol_type);
+                               proto_name = modest_protocol_get_name (protocol);
                                gtk_list_store_insert_with_values (
                                        model, &iter, 0,
                                        MODEST_ACCOUNT_VIEW_NAME_COLUMN, account_name,
@@ -244,8 +297,15 @@ update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
                                        MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, 
                                        modest_account_settings_get_is_default (settings),
                                        MODEST_ACCOUNT_VIEW_PROTO_COLUMN, proto_name,
+#ifdef MODEST_TOOLKIT_HILDON2
+                                       MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN,  last_updated_hildon2,
+#else
                                        MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN,  last_updated_string,
+#endif
                                        -1);
+#ifdef MODEST_TOOLKIT_HILDON2
+                               g_free (last_updated_hildon2);
+#endif
                        }
                }
                
@@ -261,6 +321,10 @@ update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
        if (selected_name) {
                modest_account_view_select_account (view, selected_name);
                g_free (selected_name);
+       } else {
+#ifndef MODEST_TOOLKIT_HILDON2
+               modest_account_view_select_first_account (view);
+#endif
        }
 }
 
@@ -290,10 +354,25 @@ on_account_busy_changed(ModestAccountMgr *account_mgr,
                                g_free (cur_name);
                                return;
                        }
-                       const gchar* last_updated_string = get_last_updated_string(account_mgr, settings);
+                       const gchar* last_updated_string = get_last_updated_string(self, account_mgr, settings);
+#ifdef MODEST_TOOLKIT_HILDON2
+                       gchar *last_updated_hildon2;
+
+                       last_updated_hildon2 = g_strconcat (_("mcen_ti_lastupdated"), "\n", 
+                                                           last_updated_string,
+                                                           NULL);
+#endif
                        gtk_list_store_set(model, &iter, 
+#ifdef MODEST_TOOLKIT_HILDON2
+                                          MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, last_updated_hildon2,
+#else
                                           MODEST_ACCOUNT_VIEW_LAST_UPDATED_COLUMN, last_updated_string,
+#endif
                                           -1);
+
+#ifdef MODEST_TOOLKIT_HILDON2
+                       g_free (last_updated_hildon2);
+#endif
                        g_object_unref (settings);
                        found = TRUE;
                }
@@ -327,16 +406,23 @@ on_account_removed (TnyAccountStore *account_store,
 {
        ModestAccountView *self;
        ModestAccountViewPrivate *priv;
+       gchar *selected_name;
 
        g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (user_data));
 
        self = MODEST_ACCOUNT_VIEW (user_data);
        priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE (self);
+
+       /* Do not refresh the view with transport accounts */
+       if (!TNY_IS_STORE_ACCOUNT (account))
+               return;
        
-       gchar *selected_name = modest_account_view_get_selected_account (self);
+       selected_name = modest_account_view_get_selected_account (self);
        if (selected_name == NULL) {
+#ifndef MODEST_TOOLKIT_HILDON2
                /* we select the first account if none is selected */
-               modest_account_view_select_first_account (self);                
+               modest_account_view_select_first_account (self);
+#endif
        } else {
                g_free (selected_name);
        }
@@ -346,25 +432,6 @@ on_account_removed (TnyAccountStore *account_store,
 
 
 static void
-on_account_changed (TnyAccountStore *account_store, 
-                   TnyAccount *account,
-                   gpointer user_data)
-{
-       ModestAccountView *self = NULL;
-       ModestAccountViewPrivate *priv = NULL;
-
-       g_return_if_fail (MODEST_IS_ACCOUNT_VIEW (user_data));
-       g_return_if_fail (account);
-       g_return_if_fail (TNY_IS_ACCOUNT (account));
-
-       self = MODEST_ACCOUNT_VIEW (user_data);
-       priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE (self);
-       
-       /* Update account view */
-/*     update_account_view (priv->account_mgr, self); */
-}
-
-static void
 on_account_default_toggled (GtkCellRendererToggle *cell_renderer, 
                            gchar *path,
                            ModestAccountView *self)
@@ -410,16 +477,80 @@ on_account_updated (ModestAccountMgr* mgr,
        update_account_view (mgr, MODEST_ACCOUNT_VIEW (user_data));
 }
 
-void
-bold_if_default_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
-                           GtkTreeModel *tree_model,  GtkTreeIter *iter,  gpointer user_data)
+static void
+bold_if_default_account_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
+                                   GtkTreeModel *tree_model,  GtkTreeIter *iter,  gpointer user_data)
+{
+       gboolean is_default;
+#ifdef MODEST_TOOLKIT_HILDON2
+       GtkStyle *style;
+       const gchar *font_style;
+       PangoAttribute *attr;
+       PangoAttrList *attr_list = NULL;
+       GtkWidget *widget;
+#endif
+       gtk_tree_model_get (tree_model, iter, MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,
+                           &is_default, -1);
+
+#ifdef MODEST_TOOLKIT_HILDON2
+       widget = gtk_tree_view_column_get_tree_view (column);
+       font_style = is_default?"EmpSystemFont":"SystemFont";
+       style = gtk_rc_get_style_by_paths (gtk_widget_get_settings (GTK_WIDGET(widget)),
+                                          font_style, NULL,
+                                          G_TYPE_NONE);
+       attr = pango_attr_font_desc_new (pango_font_description_copy (style->font_desc));
+
+       attr_list = pango_attr_list_new ();
+       pango_attr_list_insert (attr_list, attr);
+
+       g_object_set (G_OBJECT(renderer),
+                     "attributes", attr_list, 
+                     NULL);
+
+       pango_attr_list_unref (attr_list);
+#else
+       g_object_set (G_OBJECT(renderer),
+                     "weight", is_default ? 800: 400,
+                     NULL);
+#endif
+}
+
+static void
+bold_if_default_last_updated_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
+                                        GtkTreeModel *tree_model,  GtkTreeIter *iter,  gpointer user_data)
 {
        gboolean is_default;
+#ifdef MODEST_TOOLKIT_HILDON2
+       GtkStyle *style;
+       const gchar *font_style;
+       PangoAttribute *attr;
+       PangoAttrList *attr_list = NULL;
+       GtkWidget *widget;
+#endif
        gtk_tree_model_get (tree_model, iter, MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN,
                            &is_default, -1);
+
+#ifdef MODEST_TOOLKIT_HILDON2
+       widget = gtk_tree_view_column_get_tree_view (column);
+       font_style = is_default?"EmpSmallSystemFont":"SmallSystemFont";
+       style = gtk_rc_get_style_by_paths (gtk_widget_get_settings (GTK_WIDGET(widget)),
+                                          font_style, NULL,
+                                          G_TYPE_NONE);
+       attr = pango_attr_font_desc_new (pango_font_description_copy (style->font_desc));
+
+       attr_list = pango_attr_list_new ();
+       pango_attr_list_insert (attr_list, attr);
+
+       g_object_set (G_OBJECT(renderer),
+                     "attributes", attr_list, 
+                     NULL);
+
+       pango_attr_list_unref (attr_list);
+#else
        g_object_set (G_OBJECT(renderer),
                      "weight", is_default ? 800: 400,
                      NULL);
+#endif
 }
 
 static void
@@ -450,16 +581,16 @@ init_view (ModestAccountView *self)
        g_object_unref (G_OBJECT (model));
 
        toggle_renderer = gtk_cell_renderer_toggle_new ();
-       text_renderer = gtk_cell_renderer_text_new ();
-       g_object_set (G_OBJECT (text_renderer), "ellipsize", PANGO_ELLIPSIZE_END,
-                       "ellipsize-set", TRUE, NULL);
-
        /* the is_default column */
        g_object_set (G_OBJECT(toggle_renderer), "activatable", TRUE, "radio", TRUE, NULL);
+       column = gtk_tree_view_column_new_with_attributes 
+               (_("mcen_ti_default"), toggle_renderer,
+                "active", MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, NULL);
        gtk_tree_view_append_column (GTK_TREE_VIEW(self),
-                                    gtk_tree_view_column_new_with_attributes (
-                                            _("mcen_ti_default"), toggle_renderer,
-                                            "active", MODEST_ACCOUNT_VIEW_IS_DEFAULT_COLUMN, NULL));
+                                    column);
+#ifdef MODEST_TOOLKIT_HILDON2
+       gtk_tree_view_column_set_visible (column, FALSE);
+#endif
                                        
        /* Disable the Maemo GtkTreeView::allow-checkbox-mode Maemo modification, 
         * which causes the model column to be updated automatically when the row is clicked.
@@ -481,24 +612,44 @@ init_view (ModestAccountView *self)
                                           self);
        
        /* account name */
+       text_renderer = gtk_cell_renderer_text_new ();
+       g_object_set (G_OBJECT (text_renderer), 
+                     "ellipsize", PANGO_ELLIPSIZE_END, "ellipsize-set", TRUE, 
+#ifdef MODEST_TOOLKIT_HILDON2
+                     "xpad", HILDON_MARGIN_DOUBLE,
+#endif
+                     NULL);
+
        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_expand (column, TRUE);
-       gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_cell_data,
+       gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_account_cell_data,
                                                NULL, NULL);
 
        /* last update for this account */
-       column =  gtk_tree_view_column_new_with_attributes (_("mcen_ti_lastupdated"), text_renderer,"text",
+       text_renderer = gtk_cell_renderer_text_new ();
+       g_object_set (G_OBJECT (text_renderer), 
+                     "alignment", PANGO_ALIGN_RIGHT, 
+                     "xalign", 1.0,
+#ifdef MODEST_TOOLKIT_HILDON2
+                     "xpad", HILDON_MARGIN_DOUBLE,
+#endif
+                     NULL);
+
+       column =  gtk_tree_view_column_new_with_attributes (_("mcen_ti_lastupdated"), text_renderer,"markup",
                                                            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,
+       gtk_tree_view_column_set_expand (column, FALSE);
+       gtk_tree_view_column_set_cell_data_func(column, text_renderer, bold_if_default_last_updated_cell_data,
                                                NULL, NULL);
                        
        /* Show the column headers,
         * which does not seem to be the default on Maemo.
-        */                     
+        */
+#ifndef MODEST_TOOLKIT_HILDON2
        gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(self), TRUE);
+#endif
 
        priv->sig_handlers = 
                modest_signal_mgr_connect (priv->sig_handlers, 
@@ -514,12 +665,6 @@ init_view (ModestAccountView *self)
                                           self);
        priv->sig_handlers = 
                modest_signal_mgr_connect (priv->sig_handlers, 
-                                          G_OBJECT (modest_runtime_get_account_store ()),
-                                          "account_changed",
-                                          G_CALLBACK(on_account_changed), 
-                                          self);
-       priv->sig_handlers = 
-               modest_signal_mgr_connect (priv->sig_handlers, 
                                           G_OBJECT(priv->account_mgr),
                                           "account_busy_changed",
                                           G_CALLBACK(on_account_busy_changed), 
@@ -553,7 +698,11 @@ modest_account_view_new (ModestAccountMgr *account_mgr)
        
        g_return_val_if_fail (account_mgr, NULL);
        
-       obj  = g_object_new(MODEST_TYPE_ACCOUNT_VIEW, NULL);
+       obj  = g_object_new(MODEST_TYPE_ACCOUNT_VIEW, 
+#ifdef MODEST_TOOLKIT_HILDON2
+                           "hildon-ui-mode", HILDON_UI_MODE_NORMAL,
+#endif
+                           NULL);
        priv = MODEST_ACCOUNT_VIEW_GET_PRIVATE(obj);
        
        g_object_ref (G_OBJECT (account_mgr));
@@ -585,6 +734,25 @@ modest_account_view_get_selected_account (ModestAccountView *self)
        return account_name;
 }
 
+gchar *
+modest_account_view_get_path_account (ModestAccountView *self, GtkTreePath *path)
+{
+       gchar *account_name = NULL;
+       GtkTreeModel *model;
+       GtkTreeIter iter;
+
+       g_return_val_if_fail (MODEST_IS_ACCOUNT_VIEW (self), NULL);
+
+       model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
+       if (gtk_tree_model_get_iter (model, &iter, path)) {
+               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 
@@ -621,6 +789,9 @@ static void
 modest_account_view_select_account (ModestAccountView *account_view, 
                                    const gchar* account_name)
 {      
+#ifdef MODEST_TOOLKIT_HILDON2
+       return;
+#endif
        /* 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;
@@ -634,20 +805,20 @@ modest_account_view_select_account (ModestAccountView *account_view,
        g_free (state);
 }
 
+#ifndef MODEST_TOOLKIT_HILDON2
 static void
 modest_account_view_select_first_account (ModestAccountView *account_view)
 {
-       GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (account_view));
-       g_return_if_fail (model != NULL);
-
        GtkTreeIter iter;
-       gtk_tree_model_get_iter_first (model, &iter);
+       GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (account_view));
 
-       GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (account_view));
-       g_return_if_fail (selection != NULL);
+       if (gtk_tree_model_get_iter_first (model, &iter)) {
+               GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (account_view));
 
-       gtk_tree_selection_select_iter (selection, &iter);
+               gtk_tree_selection_select_iter (selection, &iter);
+       }
 }
+#endif
 
 static void
 on_default_account_changed (ModestAccountMgr *mgr,
@@ -696,3 +867,12 @@ on_display_name_changed (ModestAccountMgr *mgr,
        /* Update the view */
        update_account_view (mgr, MODEST_ACCOUNT_VIEW (user_data));
 }
+
+static void 
+on_notify_style (GObject *obj, GParamSpec *spec, gpointer userdata)
+{
+       if (strcmp ("style", spec->name) == 0) {
+               gtk_widget_queue_draw (GTK_WIDGET (obj));
+       } 
+}
+