This is a manual merge of branch drop split view intro trunk.
[modest] / src / hildon2 / modest-platform.c
index 49136a0..2a2110e 100644 (file)
@@ -33,7 +33,7 @@
 #include <modest-runtime.h>
 #include <modest-main-window.h>
 #include <modest-header-view.h>
-#include "modest-maemo-global-settings-dialog.h"
+#include "modest-hildon2-global-settings-dialog.h"
 #include "modest-widget-memory.h"
 #include <modest-hildon-includes.h>
 #include <modest-maemo-utils.h>
@@ -43,8 +43,6 @@
 #include <tny-maemo-conic-device.h>
 #include <tny-simple-list.h>
 #include <tny-folder.h>
-#include <tny-camel-imap-store-account.h>
-#include <tny-camel-pop-store-account.h>
 #include <gtk/gtkicontheme.h>
 #include <gtk/gtkmenuitem.h>
 #include <gtk/gtkmain.h>
 #include <hildon/hildon-sound.h>
 #include <osso-mem.h>
 #include "hildon2/modest-hildon2-details-dialog.h"
+#include "hildon2/modest-hildon2-window-mgr.h"
+#include <keys_nokia.h>
+#include <libprofile.h>
+#include <canberra.h>
+#include <modest-datetime-formatter.h>
+#include "modest-header-window.h"
 
 #ifdef MODEST_HAVE_MCE
 #include <mce/dbus-names.h>
 
 #define HILDON_OSSO_URI_ACTION "uri-action"
 #define URI_ACTION_COPY "copy:"
-#define MODEST_NEW_MAIL_SOUND_FILE "/usr/share/sounds/ui-new_email.wav"
 #define MODEST_NEW_MAIL_LIGHTING_PATTERN "PatternCommunicationEmail"
+#define PROFILE_MAIL_TONE PROFILEKEY_EMAIL_ALERT_TONE
+#define PROFILE_MAIL_VOLUME PROFILEKEY_EMAIL_ALERT_VOLUME
+
+static void _modest_platform_play_email_tone (void);
+
 
 static void    
 on_modest_conf_update_interval_changed (ModestConf* self, 
@@ -520,7 +528,7 @@ modest_platform_get_app_name (void)
        return _("mcen_ap_name");
 }
 
-static void 
+static void
 entry_insert_text (GtkEditable *editable,
                   const gchar *text,
                   gint         length,
@@ -542,15 +550,15 @@ entry_insert_text (GtkEditable *editable,
                if (modest_text_utils_is_forbidden_char (*text, FOLDER_NAME_FORBIDDEN_CHARS)) {
                        /* Show an error */
                        gchar *tmp, *msg;
-                       
-                       tmp = g_strndup (folder_name_forbidden_chars, 
+
+                       tmp = g_strndup (folder_name_forbidden_chars,
                                         FOLDER_NAME_FORBIDDEN_CHARS_LENGTH);
                        msg = g_strdup_printf (_CS("ckdg_ib_illegal_characters_entered"), tmp);
-                       hildon_banner_show_information  (gtk_widget_get_parent (GTK_WIDGET (data)), 
+                       hildon_banner_show_information  (gtk_widget_get_parent (GTK_WIDGET (data)),
                                                         NULL, msg);
                        g_free (msg);
                        g_free (tmp);
-               } else {        
+               } else {
                        /* Write the text in the entry if it's valid */
                        g_signal_handlers_block_by_func (editable,
                                                         (gpointer) entry_insert_text, data);
@@ -572,18 +580,18 @@ entry_changed (GtkEditable *editable,
        GList *buttons;
 
        buttons = gtk_container_get_children (GTK_CONTAINER (GTK_DIALOG (user_data)->action_area));
-       ok_button = GTK_WIDGET (buttons->next->data);
-       
+       ok_button = GTK_WIDGET (buttons->data);
+
        chars = gtk_editable_get_chars (editable, 0, -1);
        g_return_if_fail (chars != NULL);
 
-       
-       if (g_utf8_strlen (chars,-1) >= 21)
+
+       if (g_utf8_strlen (chars,-1) >= 20)
                hildon_banner_show_information  (gtk_widget_get_parent (GTK_WIDGET (user_data)), NULL,
                                                 _CS("ckdg_ib_maximum_characters_reached"));
        else
                gtk_widget_set_sensitive (ok_button, modest_text_utils_validate_folder_name(chars));
-               
+
        /* Free */
        g_list_free (buttons);
        g_free (chars);
@@ -663,21 +671,19 @@ modest_platform_run_folder_name_dialog (GtkWindow *parent_window,
        dialog = gtk_dialog_new_with_buttons (dialog_title,
                                              parent_window,
                                              GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR | GTK_DIALOG_DESTROY_WITH_PARENT,
-                                             _("mcen_bd_dialog_ok"),
+                                             _HL("wdgt_bd_done"),
                                              GTK_RESPONSE_ACCEPT,
-                                             _("mcen_bd_dialog_cancel"),
-                                             GTK_RESPONSE_REJECT,
                                              NULL);
 
        /* Add accept button (with unsensitive handler) */
        buttons = gtk_container_get_children (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area));
-       accept_btn = GTK_WIDGET (buttons->next->data);
+       accept_btn = GTK_WIDGET (buttons->data);
        /* Create label and entry */
        label = gtk_label_new (label_text);
-       /* TODO: check that the suggested name does not exist */
-       /* We set 21 as maximum because we want to show WID-INF036
-          when the user inputs more that 20 */
-       entry = gtk_entry_new_with_max_length (21);
+
+       entry = hildon_entry_new (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
+       gtk_entry_set_max_length (GTK_ENTRY (entry), 20);
+
        if (suggested_name)
                gtk_entry_set_text (GTK_ENTRY (entry), suggested_name);
        else
@@ -984,7 +990,7 @@ modest_platform_connect_and_wait (GtkWindow *parent_window,
        /* Connect the device */
        if (!device_online) {
                /* Track account connection status changes */
-               data->handler = g_signal_connect (account, "connection-status-changed",                                     
+               data->handler = g_signal_connect (account, "connection-status-changed",
                                                  G_CALLBACK (on_connection_status_changed),
                                                  data);
                /* Try to connect the device */
@@ -1029,8 +1035,7 @@ gboolean
 modest_platform_connect_and_wait_if_network_account (GtkWindow *parent_window, TnyAccount *account)
 {
        if (tny_account_get_account_type (account) == TNY_ACCOUNT_TYPE_STORE) {
-               if (!TNY_IS_CAMEL_POP_STORE_ACCOUNT (account) &&
-                   !TNY_IS_CAMEL_IMAP_STORE_ACCOUNT (account)) {
+               if (!modest_tny_folder_store_is_remote (TNY_FOLDER_STORE (account))) {
                        /* This must be a maildir account, which does not require a connection: */
                        return TRUE;
                }
@@ -1068,10 +1073,6 @@ modest_platform_create_sort_dialog       (GtkWindow *parent_window)
 
        dialog = modest_hildon2_sort_dialog_new (parent_window);
 
-       hildon_help_dialog_help_enable (GTK_DIALOG(dialog),
-                                       "applications_email_sort",
-                                       modest_maemo_utils_get_osso_context());
-
        return dialog;
 }
 
@@ -1153,29 +1154,16 @@ modest_platform_set_update_interval (guint minutes)
 void
 modest_platform_push_email_notification(void)
 {
-       gboolean play_sound;
-       ModestWindow *main_window;
        gboolean screen_on = TRUE, app_in_foreground;
 
-       /* Check whether or not we should play a sound */
-       play_sound = modest_conf_get_bool (modest_runtime_get_conf (),
-                                          MODEST_CONF_PLAY_SOUND_MSG_ARRIVE,
-                                          NULL);
-
-       /* Get the screen status */
-       main_window = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr (), FALSE);
-       if (main_window)
-               screen_on = modest_main_window_screen_is_on (MODEST_MAIN_WINDOW (main_window));
-
        /* Get the window status */
        app_in_foreground = hildon_program_get_is_topmost (hildon_program_get_instance ());
 
        /* If the screen is on and the app is in the
           foreground we don't show anything */
        if (!(screen_on && app_in_foreground)) {
-               /* Play a sound */
-               if (play_sound)
-                       hildon_play_system_sound (MODEST_NEW_MAIL_SOUND_FILE);
+
+               _modest_platform_play_email_tone ();
 
                /* Activate LED. This must be deactivated by
                   modest_platform_remove_new_mail_notifications */
@@ -1210,13 +1198,6 @@ modest_platform_on_new_headers_received (TnyList *header_list,
        }
 
 #ifdef MODEST_HAVE_HILDON_NOTIFY
-       gboolean play_sound;
-
-       /* Check whether or not we should play a sound */
-       play_sound = modest_conf_get_bool (modest_runtime_get_conf (),
-                                          MODEST_CONF_PLAY_SOUND_MSG_ARRIVE,
-                                          NULL);
-
        HildonNotification *notification;
        TnyIterator *iter;
        GSList *notifications_list = NULL;
@@ -1235,9 +1216,13 @@ modest_platform_on_new_headers_received (TnyList *header_list,
                gboolean first_notification = TRUE;
                gint notif_id;
                gchar *str;
+               ModestDatetimeFormatter *datetime_formatter;
 
                /* constant string, don't free */
-               display_date = modest_text_utils_get_display_date (tny_header_get_date_received (header));
+               datetime_formatter = modest_datetime_formatter_new ();
+               display_date = modest_datetime_formatter_display_datetime (datetime_formatter,
+                                                                          tny_header_get_date_received (header));
+               g_object_unref (datetime_formatter);
 
                display_address = tny_header_dup_from (header);
                modest_text_utils_get_display_address (display_address); /* string is changed in-place */
@@ -1268,11 +1253,25 @@ modest_platform_on_new_headers_received (TnyList *header_list,
                /* Play sound if the user wants. Show the LED
                   pattern. Show and play just one */
                if (G_UNLIKELY (first_notification)) {
+                       gchar *active_profile;
+                       gchar *mail_tone;
+                       gchar *mail_volume;
+                       gint mail_volume_int;
+
                        first_notification = FALSE;
-                       if (play_sound)  {
+
+                       active_profile = profile_get_profile ();
+                       mail_tone = profile_get_value (active_profile, PROFILE_MAIL_TONE);
+                       mail_volume = profile_get_value (active_profile, PROFILE_MAIL_VOLUME);
+                       mail_volume_int = profile_parse_int (mail_volume);
+
+                       if (mail_volume_int > 0)
                                notify_notification_set_hint_string(NOTIFY_NOTIFICATION (notification),
-                                                                   "sound-file", MODEST_NEW_MAIL_SOUND_FILE);
-                       }
+                                                                   "sound-file", mail_tone);
+
+                       g_free (mail_volume);
+                       g_free (mail_tone);
+                       g_free (active_profile);
 
                        /* Set the led pattern */
                        notify_notification_set_hint_int32 (NOTIFY_NOTIFICATION (notification),
@@ -1371,27 +1370,14 @@ modest_platform_remove_new_mail_notifications (gboolean only_visuals)
 GtkWidget * 
 modest_platform_get_global_settings_dialog ()
 {
-       return modest_maemo_global_settings_dialog_new ();
+       return modest_hildon2_global_settings_dialog_new ();
 }
 
 void
 modest_platform_show_help (GtkWindow *parent_window, 
                           const gchar *help_id)
 {
-       osso_return_t result;
-       g_return_if_fail (help_id);
-
-       result = hildon_help_show (modest_maemo_utils_get_osso_context(),
-                                  help_id, HILDON_HELP_SHOW_DIALOG);
-       
-       if (result != OSSO_OK) {
-               gchar *error_msg;
-               error_msg = g_strdup_printf ("FIXME The help topic %s could not be found", help_id); 
-               hildon_banner_show_information (GTK_WIDGET (parent_window),
-                                               NULL,
-                                               error_msg);
-               g_free (error_msg);
-       }
+       return;
 }
 
 void 
@@ -1412,7 +1398,7 @@ void
 modest_platform_show_addressbook (GtkWindow *parent_window)
 {
        osso_return_t result = OSSO_ERROR;
-       
+
        result = osso_rpc_run_with_defaults (modest_maemo_utils_get_osso_context(),
                                             "osso_addressbook",
                                             "top_application", NULL, DBUS_TYPE_INVALID);
@@ -1455,7 +1441,7 @@ modest_platform_information_banner (GtkWidget *parent,
        GtkWidget *banner, *banner_parent = NULL;
        ModestWindowMgr *mgr = modest_runtime_get_window_mgr ();
 
-       if (modest_window_mgr_num_windows (mgr) == 0)
+       if (modest_window_mgr_get_num_windows (mgr) == 0)
                return;
 
        if (parent && GTK_IS_WINDOW (parent)) {
@@ -1491,7 +1477,7 @@ modest_platform_information_banner_with_timeout (GtkWidget *parent,
 {
        GtkWidget *banner;
 
-       if (modest_window_mgr_num_windows (modest_runtime_get_window_mgr ()) == 0)
+       if (modest_window_mgr_get_num_windows (modest_runtime_get_window_mgr ()) == 0)
                return;
 
        banner = hildon_banner_show_information (parent, icon_name, text);
@@ -1507,7 +1493,7 @@ modest_platform_animation_banner (GtkWidget *parent,
 
        g_return_val_if_fail (text != NULL, NULL);
 
-       if (modest_window_mgr_num_windows (modest_runtime_get_window_mgr ()) == 0)
+       if (modest_window_mgr_get_num_windows (modest_runtime_get_window_mgr ()) == 0)
                return NULL;
 
        /* If the parent is not visible then do not show */
@@ -1585,24 +1571,17 @@ modest_platform_check_and_wait_for_account_is_online(TnyAccount *account)
        gboolean is_online;
 
        g_return_val_if_fail (account, FALSE);
-       
-       printf ("DEBUG: %s: account id=%s\n", __FUNCTION__, tny_account_get_id (account));
-       
+
        if (!tny_device_is_online (modest_runtime_get_device())) {
                printf ("DEBUG: %s: device is offline.\n", __FUNCTION__);
                return FALSE;
        }
-       
+
        /* The local_folders account never seems to leave TNY_CONNECTION_STATUS_INIT,
         * so we avoid wait unnecessarily: */
-       if (!TNY_IS_CAMEL_POP_STORE_ACCOUNT (account) && 
-               !TNY_IS_CAMEL_IMAP_STORE_ACCOUNT (account) ) {
-               return TRUE;            
-       }
-               
-       printf ("DEBUG: %s: tny_account_get_connection_status()==%d\n",
-               __FUNCTION__, tny_account_get_connection_status (account));
-       
+       if (!modest_tny_folder_store_is_remote (TNY_FOLDER_STORE (account)))
+               return TRUE;
+
        /* The POP & IMAP store accounts seem to be TNY_CONNECTION_STATUS_DISCONNECTED, 
         * and that seems to be an OK time to use them. Maybe it's just TNY_CONNECTION_STATUS_INIT that 
         * we want to avoid. */
@@ -1660,19 +1639,18 @@ modest_platform_run_certificate_confirmation_dialog (const gchar* server_name,
 {
        GtkWidget *note;
        gint response;
-       ModestWindow *main_win;
-       
-       if (!modest_window_mgr_main_window_exists (modest_runtime_get_window_mgr())) {
-               g_warning ("%s: don't show dialogs if there's no main window; assuming 'Cancel'",
+       ModestWindow *win;
+       HildonWindowStack *stack;
+
+       stack = hildon_window_stack_get_default ();
+       win = MODEST_WINDOW (hildon_window_stack_peek (stack));
+
+       if (!win) {
+         g_warning ("%s: don't show dialogs if there's no window shown; assuming 'Cancel'",
                           __FUNCTION__);
                return FALSE;
        }
 
-       /* don't create it */
-       main_win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr(), FALSE);
-       g_return_val_if_fail (main_win, FALSE); /* should not happen */
-       
-       
        gchar *question = g_strdup_printf (_("mcen_nc_unknown_certificate"),
                                           server_name);
        
@@ -1681,19 +1659,17 @@ modest_platform_run_certificate_confirmation_dialog (const gchar* server_name,
           example. With GTK_RESPONSE_HELP the view button is aligned
           to the left while the other two to the right */
        note = hildon_note_new_confirmation_add_buttons  (
-               GTK_WINDOW(main_win),
+               NULL,
                question,
-               _("mcen_bd_dialog_ok"),     GTK_RESPONSE_OK,
-               _("mcen_bd_view"),          GTK_RESPONSE_APPLY,   /* abusing this... */
-               _("mcen_bd_dialog_cancel"), GTK_RESPONSE_CANCEL,
+               _HL("wdgt_bd_yes"),     GTK_RESPONSE_OK,
+               _HL("wdgt_bd_view"),          GTK_RESPONSE_APPLY,   /* abusing this... */
+               _HL("wdgt_bd_no"), GTK_RESPONSE_CANCEL,
                NULL, NULL);
        
        g_signal_connect (G_OBJECT(note), "response", 
                          G_CALLBACK(on_cert_dialog_response),
                          (gpointer) certificate);
        
-       modest_window_mgr_set_modal (modest_runtime_get_window_mgr (),
-                                    GTK_WINDOW (note), GTK_WINDOW (main_win));
        response = gtk_dialog_run(GTK_DIALOG(note));
 
        on_destroy_dialog (note);
@@ -1966,11 +1942,8 @@ modest_platform_connect_if_remote_and_perform (GtkWindow *parent_window,
                if (callback) {
                        callback (FALSE, NULL, parent_window, NULL, user_data);
                }
-               return; 
-               
-               /* Original comment: Maybe it is something local. */
-               /* PVH's comment: maybe we should KNOW this in stead of assuming? */
-               
+               return;
+
        } else if (TNY_IS_FOLDER (folder_store)) {
                /* Get the folder's parent account: */
                account = tny_folder_get_account (TNY_FOLDER (folder_store));
@@ -1978,11 +1951,9 @@ modest_platform_connect_if_remote_and_perform (GtkWindow *parent_window,
                /* Use the folder store as an account: */
                account = TNY_ACCOUNT (g_object_ref (folder_store));
        }
+
        if (tny_account_get_account_type (account) == TNY_ACCOUNT_TYPE_STORE) {
-               if (!TNY_IS_CAMEL_POP_STORE_ACCOUNT (account) &&
-                   !TNY_IS_CAMEL_IMAP_STORE_ACCOUNT (account)) {
+               if (!modest_tny_folder_store_is_remote (TNY_FOLDER_STORE (account))) {
                        /* No need to connect a local account */
                        if (callback)
                                callback (FALSE, NULL, parent_window, account, user_data);
@@ -1991,17 +1962,17 @@ modest_platform_connect_if_remote_and_perform (GtkWindow *parent_window,
                }
        }
        modest_platform_connect_and_perform (parent_window, force, account, callback, user_data);
+
  clean:
        if (account)
                g_object_unref (account);
 }
 
 static void
-src_account_connect_performer (gboolean canceled, 
+src_account_connect_performer (gboolean canceled,
                               GError *err,
-                              GtkWindow *parent_window, 
-                              TnyAccount *src_account, 
+                              GtkWindow *parent_window,
+                              TnyAccount *src_account,
                               gpointer user_data)
 {
        DoubleConnectionInfo *info = (DoubleConnectionInfo *) user_data;
@@ -2127,7 +2098,7 @@ modest_platform_run_header_details_dialog (GtkWindow *parent_window,
                                           TnyHeader *header)
 {
        GtkWidget *dialog;
-       
+
        /* Create dialog */
        dialog = modest_hildon2_details_dialog_new_with_header (parent_window, header);
 
@@ -2141,3 +2112,111 @@ modest_platform_run_header_details_dialog (GtkWindow *parent_window,
                                  G_CALLBACK (gtk_widget_destroy),
                                  dialog);
 }
+
+osso_context_t *
+modest_platform_get_osso_context (void)
+{
+       return modest_maemo_utils_get_osso_context ();
+}
+
+static void
+_modest_platform_play_email_tone (void)
+{
+       gchar *active_profile;
+       gchar *mail_tone;
+       gchar *mail_volume;
+       gint mail_volume_int;
+       int ret;
+       ca_context *ca_con = NULL;
+       ca_proplist *pl = NULL;
+
+       active_profile = profile_get_profile ();
+       mail_tone = profile_get_value (active_profile, PROFILE_MAIL_TONE);
+       mail_volume = profile_get_value (active_profile, PROFILE_MAIL_VOLUME);
+       mail_volume_int = profile_parse_int (mail_volume);
+
+       if (mail_volume_int > 0) {
+
+               if ((ret = ca_context_create(&ca_con)) != CA_SUCCESS) {
+                       g_warning("ca_context_create: %s\n", ca_strerror(ret));
+                       return;
+               }
+
+               if ((ret = ca_context_open(ca_con)) != CA_SUCCESS) {
+                       g_warning("ca_context_open: %s\n", ca_strerror(ret));
+                       ca_context_destroy(ca_con);
+                       return;
+               }
+
+               ca_proplist_create(&pl);
+               ca_proplist_sets(pl, CA_PROP_MEDIA_FILENAME, mail_tone);
+               ca_proplist_setf(pl, CA_PROP_CANBERRA_VOLUME, "%f", (gfloat) mail_volume_int);
+
+               ret = ca_context_play_full(ca_con, 0, pl, NULL, NULL);
+               g_debug("ca_context_play_full (vol %f): %s\n", (gfloat) mail_volume_int, ca_strerror(ret));
+
+               ca_proplist_destroy(pl);
+               ca_context_destroy(ca_con);
+       }
+
+       g_free (mail_volume);
+       g_free (mail_tone);
+       g_free (active_profile);
+}
+
+static void
+on_move_to_dialog_folder_activated (GtkTreeView       *tree_view,
+                                    GtkTreePath       *path,
+                                    GtkTreeViewColumn *column,
+                                    gpointer           user_data)
+{
+        gtk_dialog_response (GTK_DIALOG (user_data), GTK_RESPONSE_OK);
+}
+
+GtkWidget *
+modest_platform_create_move_to_dialog (GtkWindow *parent_window,
+                                      GtkWidget **folder_view)
+{
+       GtkWidget *dialog, *folder_view_container;
+
+       /* Create dialog. We cannot use a touch selector because we
+          need to use here the folder view widget directly */
+       dialog = gtk_dialog_new_with_buttons (_("mcen_ti_moveto_folders_title"),
+                                             GTK_WINDOW (parent_window),
+                                             GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR |
+                                             GTK_DIALOG_DESTROY_WITH_PARENT,
+                                             _("mcen_bd_new"), MODEST_GTK_RESPONSE_NEW_FOLDER,
+                                             NULL);
+
+       /* Create folder view */
+       *folder_view = modest_platform_create_folder_view (NULL);
+
+        /* Simulate the behaviour of a HildonPickerDialog by emitting
+          a response when a folder is selected */
+        g_signal_connect (*folder_view, "row-activated",
+                          G_CALLBACK (on_move_to_dialog_folder_activated),
+                          dialog);
+
+       /* Create pannable and add it to the dialog */
+       folder_view_container = hildon_pannable_area_new ();
+       gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), folder_view_container);
+       gtk_container_add (GTK_CONTAINER (folder_view_container), *folder_view);
+
+       gtk_window_set_default_size (GTK_WINDOW (dialog), 300, 300);
+
+       gtk_widget_show (GTK_DIALOG (dialog)->vbox);
+       gtk_widget_show (folder_view_container);
+       gtk_widget_show (*folder_view);
+
+       return dialog;
+}
+
+TnyList *
+modest_platform_get_list_to_move (ModestWindow *window)
+{
+       ModestHeaderView *header_view;
+
+       header_view = modest_header_window_get_header_view (MODEST_HEADER_WINDOW (window));
+
+       return modest_header_view_get_selected_headers (header_view);
+}