This is a manual merge of branch drop split view intro trunk.
[modest] / src / widgets / modest-account-view.c
index be2c956..0ce8caf 100644 (file)
@@ -43,6 +43,7 @@
 #include <gtk/gtkliststore.h>
 #include <string.h> /* For strcmp(). */
 #include <modest-account-mgr-helpers.h>
+#include <modest-datetime-formatter.h>
 
 /* 'private'/'protected' functions */
 static void modest_account_view_class_init    (ModestAccountViewClass *klass);
@@ -63,6 +64,7 @@ static void modest_account_view_select_first_account (ModestAccountView *account
 
 static void on_account_updated (ModestAccountMgr* mgr, gchar* account_name,
                     gpointer user_data);
+static void update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view);
 
 typedef enum {
        MODEST_ACCOUNT_VIEW_NAME_COLUMN,
@@ -79,6 +81,8 @@ typedef struct _ModestAccountViewPrivate ModestAccountViewPrivate;
 struct _ModestAccountViewPrivate {
        ModestAccountMgr *account_mgr;
 
+       ModestDatetimeFormatter *datetime_formatter;
+
        /* Signal handlers */
        GSList *sig_handlers;
 };
@@ -125,6 +129,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 +146,17 @@ 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
 }
 
 static void
@@ -141,6 +166,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 +184,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,17 +192,22 @@ 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  {
                last_updated_string = _("mcen_va_refreshing");
        }
@@ -226,13 +261,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 ("<span size='x-small'>", _("mcen_ti_lastupdated"), "\n", 
+                                                                  last_updated_string, "</span>",
+                                                                  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,
@@ -243,8 +289,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
                        }
                }
                
@@ -260,6 +313,8 @@ update_account_view (ModestAccountMgr *account_mgr, ModestAccountView *view)
        if (selected_name) {
                modest_account_view_select_account (view, selected_name);
                g_free (selected_name);
+       } else {
+               modest_account_view_select_first_account (view);
        }
 }
 
@@ -289,10 +344,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 ("<span size='x-small'>", _("mcen_ti_lastupdated"), "\n", 
+                                                           last_updated_string, "</span>",
+                                                           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;
                }
@@ -326,13 +396,18 @@ 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) {
                /* we select the first account if none is selected */
                modest_account_view_select_first_account (self);                
@@ -436,10 +511,14 @@ init_view (ModestAccountView *self)
 
        /* 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.
@@ -469,17 +548,25 @@ init_view (ModestAccountView *self)
                                                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,
+                     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_expand (column, TRUE);
+       gtk_tree_view_column_set_expand (column, FALSE);
        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.
-        */                     
+        */
+#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, 
@@ -528,7 +615,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));
@@ -560,6 +651,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 
@@ -596,6 +706,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;