+typedef struct _FolderChooserData {
+ TnyFolderStore *store;
+ GtkWidget *dialog;
+} FolderChooserData;
+
+static void
+folder_chooser_activated (ModestFolderView *folder_view,
+ TnyFolderStore *folder,
+ FolderChooserData *userdata)
+{
+ userdata->store = folder;
+ gtk_dialog_response (GTK_DIALOG (userdata->dialog), GTK_RESPONSE_OK);
+}
+
+static TnyFolderStore *
+folder_chooser_dialog_run (ModestFolderView *original)
+{
+ GtkWidget *folder_view;
+ FolderChooserData userdata = {NULL, NULL};
+ GtkWidget *pannable;
+ const gchar *visible_id = NULL;
+
+ userdata.dialog = hildon_dialog_new ();
+ pannable = hildon_pannable_area_new ();
+ folder_view = modest_platform_create_folder_view (NULL);
+
+ gtk_window_set_title (GTK_WINDOW (userdata.dialog), _FM("ckdg_ti_change_folder"));
+
+ modest_folder_view_copy_model (MODEST_FOLDER_VIEW (original),
+ MODEST_FOLDER_VIEW (folder_view));
+
+ visible_id =
+ modest_folder_view_get_account_id_of_visible_server_account (MODEST_FOLDER_VIEW(original));
+ modest_folder_view_set_account_id_of_visible_server_account (MODEST_FOLDER_VIEW(folder_view),
+ visible_id);
+
+ gtk_container_add (GTK_CONTAINER (GTK_DIALOG (userdata.dialog)->vbox), pannable);
+ gtk_container_add (GTK_CONTAINER (pannable), folder_view);
+ gtk_widget_set_size_request (pannable, -1, 320);
+
+ gtk_widget_show (folder_view);
+ gtk_widget_show (pannable);
+ gtk_widget_show (userdata.dialog);
+ g_signal_connect (G_OBJECT (folder_view), "folder-activated",
+ G_CALLBACK (folder_chooser_activated),
+ (gpointer) &userdata);
+
+ gtk_dialog_run (GTK_DIALOG (userdata.dialog));
+ gtk_widget_destroy (userdata.dialog);
+
+ return userdata.store;
+}
+
+static gchar *
+folder_store_get_display_name (TnyFolderStore *store)
+{
+ if (TNY_IS_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;
+
+ fname = g_strdup (tny_folder_get_name (TNY_FOLDER (store)));
+ type = tny_folder_get_folder_type (TNY_FOLDER (store));
+ if (modest_tny_folder_is_local_folder (TNY_FOLDER (store)) ||
+ modest_tny_folder_is_memory_card_folder (TNY_FOLDER (store))) {
+ type = modest_tny_folder_get_local_or_mmc_folder_type (TNY_FOLDER (store));
+ if (type != TNY_FOLDER_TYPE_UNKNOWN) {
+ g_free (fname);
+ fname = g_strdup (modest_local_folder_info_get_type_display_name (type));
+ }
+ } else {
+ /* Sometimes an special folder is reported by the server as
+ NORMAL, like some versions of Dovecot */
+ if (type == TNY_FOLDER_TYPE_NORMAL ||
+ type == TNY_FOLDER_TYPE_UNKNOWN) {
+ type = modest_tny_folder_guess_folder_type (TNY_FOLDER (store));
+ }
+ }
+
+ if (type == TNY_FOLDER_TYPE_INBOX) {
+ g_free (fname);
+ fname = g_strdup (_("mcen_me_folder_inbox"));
+ }
+ return fname;
+ }
+}
+
+GtkWidget *
+get_image_for_folder_store (TnyFolderStore *store,
+ gint size)
+{
+ GdkPixbuf *pixbuf;
+ const gchar *icon_name = NULL;
+ GtkWidget *image = NULL;
+
+ if (TNY_IS_ACCOUNT (store)) {
+ if (modest_tny_account_is_virtual_local_folders (TNY_ACCOUNT (store)))
+ icon_name = MODEST_FOLDER_ICON_LOCAL_FOLDERS;
+ else if (modest_tny_account_is_memory_card_account (TNY_ACCOUNT (store)))
+ icon_name = MODEST_FOLDER_ICON_MMC;
+ 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))) {
+ switch (type) {
+ case TNY_FOLDER_TYPE_INBOX:
+ icon_name = MODEST_FOLDER_ICON_INBOX;
+ break;
+ default:
+ icon_name = MODEST_FOLDER_ICON_ACCOUNT;
+ }
+ } 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 */
+ pixbuf = modest_platform_get_icon (icon_name, size);
+
+ if (pixbuf) {
+ image = gtk_image_new_from_pixbuf (pixbuf);
+ g_object_unref (pixbuf);
+ }
+
+ return image;
+}
+
+static void
+folder_picker_set_store (GtkButton *button, TnyFolderStore *store)
+{
+ gchar *name;
+
+ if (store == NULL) {
+ g_object_set_data (G_OBJECT (button), FOLDER_PICKER_CURRENT_FOLDER, NULL);
+ } else {
+ GtkWidget *image;
+
+ g_object_set_data_full (G_OBJECT (button), FOLDER_PICKER_CURRENT_FOLDER,
+ g_object_ref (store),
+ (GDestroyNotify) g_object_unref);
+ name = folder_store_get_display_name (store);
+ hildon_button_set_value (HILDON_BUTTON (button), name);
+ g_free (name);
+
+ /* Select icon */
+ image = get_image_for_folder_store (store, MODEST_ICON_SIZE_SMALL);
+ if (image)
+ hildon_button_set_image (HILDON_BUTTON (button), image);
+ }
+}
+
+/* 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,
+ FolderPickerHelper *helper)
+{
+ TnyFolderStore *store;
+
+ store = folder_chooser_dialog_run (helper->folder_view);
+ if (store) {
+ const gchar *current_name;
+ gboolean exists = FALSE;
+
+ folder_picker_set_store (GTK_BUTTON (button), store);
+
+ /* Update the name of the folder */
+ current_name = gtk_entry_get_text (helper->entry);
+
+ if (TNY_IS_FOLDER_STORE (store))
+ 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 (TnyFolderStore *suggested, FolderPickerHelper *helper)
+{
+ GtkWidget *button;
+
+ button = hildon_button_new (MODEST_EDITABLE_SIZE,
+ HILDON_BUTTON_ARRANGEMENT_HORIZONTAL);
+
+ hildon_button_set_alignment (HILDON_BUTTON (button), 0.0, 0.5, 1.0, 1.0);
+
+ if (suggested)
+ folder_picker_set_store (GTK_BUTTON (button), suggested);
+
+ g_signal_connect (G_OBJECT (button), "clicked",
+ G_CALLBACK (folder_picker_clicked),
+ helper);
+
+ return button;
+}