Fixes NB#104083, show the proper folders in move to dialog
[modest] / src / hildon2 / modest-platform.c
index 04328c2..77a74e9 100644 (file)
@@ -90,6 +90,7 @@
 
 #define HILDON_OSSO_URI_ACTION "uri-action"
 #define URI_ACTION_COPY "copy:"
+#define MODEST_NOTIFICATION_CATEGORY "email-message"
 #define MODEST_NEW_MAIL_LIGHTING_PATTERN "PatternCommunicationEmail"
 #define PROFILE_MAIL_TONE PROFILEKEY_EMAIL_ALERT_TONE
 #define PROFILE_MAIL_VOLUME PROFILEKEY_EMAIL_ALERT_VOLUME
@@ -124,7 +125,7 @@ on_modest_conf_update_interval_changed (ModestConf* self,
 static gboolean
 check_required_files (void)
 {
-       FILE *mcc_file = modest_maemo_open_mcc_mapping_file ();
+       FILE *mcc_file = modest_maemo_open_mcc_mapping_file (NULL);
        if (!mcc_file) {
                g_printerr ("modest: check for mcc file failed\n");
                return FALSE;
@@ -723,7 +724,11 @@ static gchar *
 folder_store_get_display_name (TnyFolderStore *store)
 {
        if (TNY_IS_ACCOUNT (store)) {
-               return g_strdup (tny_account_get_name (TNY_ACCOUNT (store)));
+               if (modest_tny_account_is_virtual_local_folders (TNY_ACCOUNT (store)))
+                       return modest_conf_get_string (modest_runtime_get_conf(),
+                                                      MODEST_CONF_DEVICE_NAME, NULL);
+               else
+                       return g_strdup (tny_account_get_name (TNY_ACCOUNT (store)));
        } else {
                gchar *fname;
                TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
@@ -781,8 +786,8 @@ folder_picker_set_store (GtkButton *button, TnyFolderStore *store)
                        else
                                icon_name = MODEST_FOLDER_ICON_ACCOUNT;
                } else {
+                       TnyFolderType type = modest_tny_folder_guess_folder_type (TNY_FOLDER (store));
                        if (modest_tny_folder_is_remote_folder (TNY_FOLDER (store))) {
-                               TnyFolderType type = modest_tny_folder_guess_folder_type (TNY_FOLDER (store));
                                switch (type) {
                                case TNY_FOLDER_TYPE_INBOX:
                                        icon_name = MODEST_FOLDER_ICON_INBOX;
@@ -790,10 +795,23 @@ folder_picker_set_store (GtkButton *button, TnyFolderStore *store)
                                default:
                                        icon_name = MODEST_FOLDER_ICON_ACCOUNT;
                                }
-                       } else if (modest_tny_folder_is_local_folder (TNY_FOLDER (store)))
-                               icon_name = MODEST_FOLDER_ICON_NORMAL;
-                       else if (modest_tny_folder_is_memory_card_folder (TNY_FOLDER (store)))
+                       } else if (modest_tny_folder_is_local_folder (TNY_FOLDER (store))) {
+                               switch (type) {
+                               case TNY_FOLDER_TYPE_OUTBOX:
+                                       icon_name = MODEST_FOLDER_ICON_OUTBOX;
+                                       break;
+                               case TNY_FOLDER_TYPE_DRAFTS:
+                                       icon_name = MODEST_FOLDER_ICON_DRAFTS;
+                                       break;
+                               case TNY_FOLDER_TYPE_SENT:
+                                       icon_name = MODEST_FOLDER_ICON_SENT;
+                                       break;
+                               default:
+                                       icon_name = MODEST_FOLDER_ICON_NORMAL;
+                               }
+                       } else if (modest_tny_folder_is_memory_card_folder (TNY_FOLDER (store))) {
                                icon_name = MODEST_FOLDER_ICON_MMC_FOLDER;
+                       }
                }
 
                /* Set icon */
@@ -807,20 +825,78 @@ folder_picker_set_store (GtkButton *button, TnyFolderStore *store)
        }
 }
 
+/* Always returns DUPs so you must free the returned value */
+static gchar *
+get_next_folder_name (const gchar *suggested_name, 
+                     TnyFolderStore *suggested_folder)
+{
+       const gchar *default_name = _FM("ckdg_va_new_folder_name_stub");
+       unsigned int i;
+       gchar *real_suggested_name;
+
+       if (suggested_name !=NULL) {
+               return g_strdup (suggested_name);
+       }
+
+       for(i = 0; i < 100; ++ i) {
+               gboolean exists = FALSE;
+
+               if (i == 0)
+                       real_suggested_name = g_strdup (default_name);
+               else
+                       real_suggested_name = g_strdup_printf ("%s(%d)",
+                                                              _FM("ckdg_va_new_folder_name_stub"),
+                                                              i);
+               exists = modest_tny_folder_has_subfolder_with_name (suggested_folder,
+                                                                   real_suggested_name,
+                                                                   TRUE);
+
+               if (!exists)
+                       break;
+
+               g_free (real_suggested_name);
+       }
+
+       /* Didn't find a free number */
+       if (i == 100)
+               real_suggested_name = g_strdup (default_name);
+
+       return real_suggested_name;
+}
+
+typedef struct {
+       ModestFolderView *folder_view;
+       GtkEntry *entry;
+} FolderPickerHelper;
+
 static void
 folder_picker_clicked (GtkButton *button,
-                      ModestFolderView *folder_view)
+                      FolderPickerHelper *helper)
 {
        TnyFolderStore *store;
 
-       store = folder_chooser_dialog_run (folder_view);
+       store = folder_chooser_dialog_run (helper->folder_view);
        if (store) {
+               const gchar *current_name;
+               gboolean exists;
+
                folder_picker_set_store (GTK_BUTTON (button), store);
+
+               /* Update the name of the folder */
+               current_name = gtk_entry_get_text (helper->entry);
+               exists = modest_tny_folder_has_subfolder_with_name (store,
+                                                                   current_name,
+                                                                   TRUE);
+               if (exists) {
+                       gchar *new_name = get_next_folder_name (NULL, store);
+                       gtk_entry_set_text (helper->entry, new_name);
+                       g_free (new_name);
+               }
        }
 }
 
 static GtkWidget *
-folder_picker_new (ModestFolderView *folder_view, TnyFolderStore *suggested)
+folder_picker_new (TnyFolderStore *suggested, FolderPickerHelper *helper)
 {
        GtkWidget *button;
 
@@ -833,7 +909,9 @@ folder_picker_new (ModestFolderView *folder_view, TnyFolderStore *suggested)
                folder_picker_set_store (GTK_BUTTON (button), suggested);
        }
 
-       g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (folder_picker_clicked), folder_view);
+       g_signal_connect (G_OBJECT (button), "clicked",
+                         G_CALLBACK (folder_picker_clicked),
+                         helper);
 
        return button;
 }
@@ -859,6 +937,7 @@ modest_platform_run_folder_common_dialog (GtkWindow *parent_window,
        ModestFolderView *folder_view;
        ModestWindow *folder_window;
        ModestHildon2WindowMgr *window_mgr;
+       FolderPickerHelper *helper = NULL;
 
        window_mgr = (ModestHildon2WindowMgr *) modest_runtime_get_window_mgr ();
        folder_window = modest_hildon2_window_mgr_get_folder_window (window_mgr);
@@ -905,7 +984,11 @@ modest_platform_run_folder_common_dialog (GtkWindow *parent_window,
                gtk_misc_set_alignment (GTK_MISC (label_location), 0.0, 0.5);
                gtk_size_group_add_widget (sizegroup, label_location);
 
-               account_picker = folder_picker_new (folder_view, suggested_parent);
+               helper = g_slice_new0 (FolderPickerHelper);
+               helper->folder_view = folder_view;
+               helper->entry = (GtkEntry *) entry;
+
+               account_picker = folder_picker_new (suggested_parent, helper);
        }
 
        g_object_unref (sizegroup);
@@ -973,6 +1056,9 @@ modest_platform_run_folder_common_dialog (GtkWindow *parent_window,
 
        gtk_widget_destroy (dialog);
 
+       if (helper)
+               g_slice_free (FolderPickerHelper, helper);
+
        while (gtk_events_pending ())
                gtk_main_iteration ();
 
@@ -991,54 +1077,26 @@ modest_platform_run_new_folder_dialog (GtkWindow *parent_window,
        ModestTnyAccountStore *acc_store;
        TnyAccount *account;
 
-       if(suggested_name == NULL)
-       {
-               const gchar *default_name = _FM("ckdg_va_new_folder_name_stub");
-               unsigned int i;
-
-               for(i = 0; i < 100; ++ i) {
-                       gboolean exists = FALSE;
-
-                       if (i == 0)
-                               real_suggested_name = g_strdup (default_name);
-                       else
-                               real_suggested_name = g_strdup_printf ("%s(%d)",
-                                                                      _FM("ckdg_va_new_folder_name_stub"),
-                                                                      i);
-                       exists = modest_tny_folder_has_subfolder_with_name (suggested_folder,
-                                                                           real_suggested_name,
-                                                                           TRUE);
-
-                       if (!exists)
-                               break;
-
-                       g_free (real_suggested_name);
-               }
-
-               /* Didn't find a free number */
-               if (i == 100)
-                       real_suggested_name = g_strdup (default_name);
-       } else {
-               real_suggested_name = suggested_name;
-       }
+       real_suggested_name = get_next_folder_name ((const gchar *) suggested_name,
+                                                   suggested_folder);
 
        /* In hildon 2.2 we always suggest the archive folder as parent */
        acc_store = modest_runtime_get_account_store ();
        account = modest_tny_account_store_get_mmc_folders_account (acc_store);
        if (account) {
                suggested_folder = (TnyFolderStore *)
-                       modest_tny_account_get_special_folder (account, 
+                       modest_tny_account_get_special_folder (account,
                                                               TNY_FOLDER_TYPE_ARCHIVE);
                g_object_unref (account);
                account = NULL;
        }
 
-       /* If there is not archive folder then fallback to local folders account */ 
+       /* If there is not archive folder then fallback to local folders account */
        if (!suggested_folder)
                suggested_folder = (TnyFolderStore *)
                        modest_tny_account_store_get_local_folders_account (acc_store);
 
-       result = modest_platform_run_folder_common_dialog (parent_window, 
+       result = modest_platform_run_folder_common_dialog (parent_window,
                                                           suggested_folder,
                                                           _HL("ckdg_ti_new_folder"),
                                                           _FM("ckdg_fi_new_folder_name"),
@@ -1048,8 +1106,7 @@ modest_platform_run_new_folder_dialog (GtkWindow *parent_window,
                                                           folder_name,
                                                           parent_folder);
 
-       if (suggested_name == NULL)
-               g_free(real_suggested_name);
+       g_free(real_suggested_name);
 
        return result;
 }
@@ -1454,7 +1511,7 @@ modest_platform_on_new_headers_received (TnyList *header_list,
                g_warning ("%s: header list is empty", __FUNCTION__);
                return;
        }
-       
+
        if (!show_visual) {
                 modest_platform_push_email_notification ();
                /* We do a return here to avoid indentation with an else */
@@ -1467,8 +1524,8 @@ modest_platform_on_new_headers_received (TnyList *header_list,
        GSList *notifications_list = NULL;
 
        /* Get previous notifications ids */
-       notifications_list = modest_conf_get_list (modest_runtime_get_conf (), 
-                                                  MODEST_CONF_NOTIFICATION_IDS, 
+       notifications_list = modest_conf_get_list (modest_runtime_get_conf (),
+                                                  MODEST_CONF_NOTIFICATION_IDS,
                                                   MODEST_CONF_VALUE_INT, NULL);
 
        iter = tny_list_create_iterator (header_list);
@@ -1488,7 +1545,7 @@ modest_platform_on_new_headers_received (TnyList *header_list,
                notification = hildon_notification_new (display_address,
                                                        str,
                                                        "qgn_list_messagin",
-                                                       "email-message");
+                                                       MODEST_NOTIFICATION_CATEGORY);
                g_free (str);
                /* Create the message URL */
                str = tny_header_dup_uid (header);
@@ -1509,32 +1566,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;
+                       TnyAccount *account;
 
                        first_notification = FALSE;
 
-                       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", 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),
                                                            "dialog-type", 4);
                        notify_notification_set_hint_string(NOTIFY_NOTIFICATION (notification),
                                                            "led-pattern",
-                                                           MODEST_NEW_MAIL_LIGHTING_PATTERN);                  
+                                                           MODEST_NEW_MAIL_LIGHTING_PATTERN);
+
+                       /* Set the account of the headers */
+                       account = tny_folder_get_account (folder);
+                       if (account) {
+                               notify_notification_set_hint_string(NOTIFY_NOTIFICATION (notification),
+                                                                   "email-account",
+                                                                   tny_account_get_id (account));
+                               g_object_unref (account);
+                       }
                }
 
                /* Notify. We need to do this in an idle because this function
@@ -1881,7 +1931,7 @@ on_cert_dialog_response (GtkDialog *dialog, gint response_id,  const gchar* cert
                g_signal_stop_emission_by_name (dialog, "response");
 
                msg = g_strdup_printf (_("mcen_ni_view_unknown_certificate"), cert);    
-               note = hildon_note_new_information (GTK_WINDOW(dialog), msg);
+               note = hildon_note_new_information (NULL, msg);
                gtk_dialog_run (GTK_DIALOG(note));
                gtk_widget_destroy (note);
        }
@@ -2398,6 +2448,14 @@ _modest_platform_play_email_tone (void)
        mail_volume = profile_get_value (active_profile, PROFILE_MAIL_VOLUME);
        mail_volume_int = profile_parse_int (mail_volume);
 
+       if (mail_tone && !strstr (mail_tone, "/")) {
+               gchar *tmp;
+
+               tmp = g_strconcat ("/usr/share/sounds", mail_tone, NULL);
+               g_free (mail_tone);
+               mail_tone = tmp;
+       }
+
        if (mail_volume_int > 0) {
 
                if ((ret = ca_context_create(&ca_con)) != CA_SUCCESS) {
@@ -2445,14 +2503,13 @@ move_to_dialog_show_accounts (GtkWidget *dialog)
         back_button = GTK_WIDGET (g_object_get_data (G_OBJECT (dialog), MOVE_TO_DIALOG_BACK_BUTTON));
         folder_view = GTK_WIDGET (g_object_get_data (G_OBJECT (dialog), MOVE_TO_DIALOG_FOLDER_VIEW));
         pannable = GTK_WIDGET (g_object_get_data (G_OBJECT (dialog), MOVE_TO_DIALOG_PANNABLE));
-       
+
        gtk_widget_set_sensitive (back_button, FALSE);
 
        gtk_label_set_text (GTK_LABEL (selection_label), "");
        modest_folder_view_show_non_move_folders (MODEST_FOLDER_VIEW (folder_view), TRUE);
        modest_folder_view_set_style (MODEST_FOLDER_VIEW (folder_view), MODEST_FOLDER_VIEW_STYLE_SHOW_ALL);
        modest_folder_view_set_filter (MODEST_FOLDER_VIEW (folder_view), MODEST_FOLDER_VIEW_FILTER_HIDE_FOLDERS);
-       modest_folder_view_unset_filter (MODEST_FOLDER_VIEW (folder_view), MODEST_FOLDER_VIEW_FILTER_HIDE_LOCAL_FOLDERS);
        hildon_pannable_area_jump_to (HILDON_PANNABLE_AREA (pannable), 0, 0);
 
        g_object_set_data (G_OBJECT (dialog), MOVE_TO_DIALOG_SHOWING_FOLDERS, GINT_TO_POINTER (FALSE));
@@ -2473,22 +2530,36 @@ move_to_dialog_show_folders (GtkWidget *dialog, TnyFolderStore *folder_store)
         back_button = GTK_WIDGET (g_object_get_data (G_OBJECT (dialog), MOVE_TO_DIALOG_BACK_BUTTON));
         folder_view = GTK_WIDGET (g_object_get_data (G_OBJECT (dialog), MOVE_TO_DIALOG_FOLDER_VIEW));
         pannable = GTK_WIDGET (g_object_get_data (G_OBJECT (dialog), MOVE_TO_DIALOG_PANNABLE));
-       
+
        gtk_widget_set_sensitive (back_button, TRUE);
 
        account = TNY_ACCOUNT (folder_store);
        if (modest_tny_account_is_virtual_local_folders (account)) {
+               gchar *device_name;
                account_id = tny_account_get_id (account);
-               selection_label_text = g_strconcat (tny_account_get_name (account), "/", NULL);
+               device_name = modest_conf_get_string (modest_runtime_get_conf(),
+                                                     MODEST_CONF_DEVICE_NAME, NULL);
+               if (device_name) {
+                       selection_label_text = g_strconcat (device_name, "/", NULL);
+                       g_free (device_name);
+               } else {
+                       selection_label_text = g_strconcat (tny_account_get_name (account), "/", NULL);
+               }
+               modest_folder_view_set_filter (MODEST_FOLDER_VIEW (folder_view),
+                                              MODEST_FOLDER_VIEW_FILTER_HIDE_MCC_FOLDERS);
        } else if (modest_tny_account_is_memory_card_account (account)) {
                account_id = tny_account_get_id (account);
                selection_label_text = g_strconcat (tny_account_get_name (account), "/", NULL);
+               modest_folder_view_set_filter (MODEST_FOLDER_VIEW (folder_view),
+                                              MODEST_FOLDER_VIEW_FILTER_HIDE_LOCAL_FOLDERS);
        } else {
                account_id = tny_account_get_id (account);
 
                selection_label_text = g_strconcat (tny_account_get_name (account), "/", NULL);
-               modest_folder_view_set_filter (MODEST_FOLDER_VIEW (folder_view), 
+               modest_folder_view_set_filter (MODEST_FOLDER_VIEW (folder_view),
                                               MODEST_FOLDER_VIEW_FILTER_HIDE_LOCAL_FOLDERS);
+               modest_folder_view_set_filter (MODEST_FOLDER_VIEW (folder_view),
+                                              MODEST_FOLDER_VIEW_FILTER_HIDE_MCC_FOLDERS);
        }
        gtk_label_set_text (GTK_LABEL (selection_label), selection_label_text);
        g_free (selection_label_text);
@@ -2509,7 +2580,16 @@ on_move_to_dialog_back_clicked (GtkButton *button,
                                gpointer userdata)
 {
        GtkWidget *dialog = (GtkWidget *) userdata;
+       ModestFolderView *folder_view;
+
+       /* Revert the effect of show_folders filters */
+        folder_view = MODEST_FOLDER_VIEW (g_object_get_data (G_OBJECT (dialog), MOVE_TO_DIALOG_FOLDER_VIEW));
+       modest_folder_view_set_account_id_of_visible_server_account (folder_view, NULL);
+       modest_folder_view_show_non_move_folders (MODEST_FOLDER_VIEW (folder_view), TRUE);
+       modest_folder_view_unset_filter (folder_view, MODEST_FOLDER_VIEW_FILTER_HIDE_LOCAL_FOLDERS);
+       modest_folder_view_unset_filter (folder_view, MODEST_FOLDER_VIEW_FILTER_HIDE_MCC_FOLDERS);
 
+       /* Back to show accounts */
        move_to_dialog_show_accounts (dialog);
 }