2007-08-11 [paul] 2.10.0cvs113
[claws.git] / src / imap_gtk.c
index 02aa4f1c9bdef18d6e3ec65072c43b43417d00f1..ad247b7458b32a31f8f22d07589a078adbc5b910 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2004 Hiroyuki Yamamoto & the Sylpheed-Claws Team
+ * Copyright (C) 1999-2007 Hiroyuki Yamamoto & the Claws Mail Team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
@@ -13,8 +13,8 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 #ifdef HAVE_CONFIG_H
 #include "imap.h"
 #include "inc.h"
 #include "prefs_common.h"
+#include "statusbar.h"
 #include "summaryview.h"
 
 static void new_folder_cb(FolderView *folderview, guint action, GtkWidget *widget);
 static void rename_folder_cb(FolderView *folderview, guint action, GtkWidget *widget);
 static void move_folder_cb(FolderView *folderview, guint action, GtkWidget *widget);
-static void imap_settings_cb(FolderView *folderview, guint action, GtkWidget *widget);
-static void remove_server_cb(FolderView *folderview, guint action, GtkWidget *widget);
 static void delete_folder_cb(FolderView *folderview, guint action, GtkWidget *widget);
 static void update_tree_cb(FolderView *folderview, guint action, GtkWidget *widget);
 static void download_cb(FolderView *folderview, guint action, GtkWidget *widget);
+static void sync_cb(FolderView *folderview, guint action, GtkWidget *widget);
+static void subscribed_cb(FolderView *folderview, guint action, GtkWidget *widget);
+static void subscribe_cb(FolderView *folderview, guint action, GtkWidget *widget);
 
 static GtkItemFactoryEntry imap_popup_entries[] =
 {
        {N_("/Create _new folder..."),   NULL, new_folder_cb,    0, NULL},
+       {"/---",                         NULL, NULL,             0, "<Separator>"},
        {N_("/_Rename folder..."),       NULL, rename_folder_cb, 0, NULL},
        {N_("/M_ove folder..."),         NULL, move_folder_cb,   0, NULL},
-       {N_("/_Delete folder"),          NULL, delete_folder_cb, 0, NULL},
-       {N_("/---"),                     NULL, NULL,             0, "<Separator>"},
+       {N_("/Cop_y folder..."),         NULL, move_folder_cb,   1, NULL},
+       {"/---",                         NULL, NULL,             0, "<Separator>"},
+       {N_("/_Delete folder..."),       NULL, delete_folder_cb, 0, NULL},
+       {"/---",                         NULL, NULL,             0, "<Separator>"},
+       {N_("/_Synchronise"),            NULL, sync_cb,          0, NULL},
        {N_("/Down_load messages"),      NULL, download_cb,      0, NULL},
-       {N_("/---"),                     NULL, NULL,             0, "<Separator>"},
+       {"/---",                         NULL, NULL,             0, "<Separator>"},
+       {N_("/S_ubscriptions"),          NULL, NULL,             0, "<Branch>"},
+       {N_("/Subscriptions/Show only subscribed _folders"),    
+                                        NULL, subscribed_cb,    0, "<ToggleItem>"},
+       {N_("/Subscriptions/---"),       NULL, NULL,             0, "<Separator>"},
+       {N_("/Subscriptions/_Subscribe..."),NULL, subscribe_cb,          1, NULL},
+       {N_("/Subscriptions/_Unsubscribe..."),    
+                                        NULL, subscribe_cb,     0, NULL},
+       {"/---",                         NULL, NULL,             0, "<Separator>"},
        {N_("/_Check for new messages"), NULL, update_tree_cb,   0, NULL},
-       {N_("/R_ebuild folder tree"),    NULL, update_tree_cb,   1, NULL},
-       {N_("/---"),                     NULL, NULL,             0, "<Separator>"},
-       {N_("/IMAP4 _account settings"), NULL, imap_settings_cb, 0, NULL},
-       {N_("/Remove _IMAP4 account"),   NULL, remove_server_cb, 0, NULL},
-       {N_("/---"),                     NULL, NULL,             0, "<Separator>"},
+       {N_("/C_heck for new folders"),  NULL, update_tree_cb,   1, NULL},
+       {N_("/R_ebuild folder tree"),    NULL, update_tree_cb,   2, NULL},
+       {"/---",                         NULL, NULL,             0, "<Separator>"},
 };
 
 static void set_sensitivity(GtkItemFactory *factory, FolderItem *item);
@@ -92,18 +104,31 @@ void imap_gtk_init(void)
 
 static void set_sensitivity(GtkItemFactory *factory, FolderItem *item)
 {
+       gboolean folder_is_normal = 
+                       item != NULL &&
+                       item->stype == F_NORMAL &&
+                       !folder_has_parent_of_type(item, F_OUTBOX) &&
+                       !folder_has_parent_of_type(item, F_DRAFT) &&
+                       !folder_has_parent_of_type(item, F_QUEUE) &&
+                       !folder_has_parent_of_type(item, F_TRASH);
+
 #define SET_SENS(name, sens) \
        menu_set_sensitive(factory, name, sens)
 
-       SET_SENS("/Create new folder...",   TRUE);
+       SET_SENS("/Create new folder...",   item->no_sub == FALSE);
        SET_SENS("/Rename folder...",       item->stype == F_NORMAL && folder_item_parent(item) != NULL);
-       SET_SENS("/Move folder...",         item->stype == F_NORMAL && folder_item_parent(item) != NULL);
-       SET_SENS("/Delete folder",          item->stype == F_NORMAL && folder_item_parent(item) != NULL);
+       SET_SENS("/Move folder...",         folder_is_normal && folder_item_parent(item) != NULL);
+       SET_SENS("/Delete folder...",       item->stype == F_NORMAL && folder_item_parent(item) != NULL);
 
+       SET_SENS("/Synchronise",            item ? (folder_item_parent(item) == NULL && folder_want_synchronise(item->folder))
+                                               : FALSE);
        SET_SENS("/Check for new messages", folder_item_parent(item) == NULL);
+       SET_SENS("/Check for new folders",  folder_item_parent(item) == NULL);
        SET_SENS("/Rebuild folder tree",    folder_item_parent(item) == NULL);
-
-       SET_SENS("/Remove IMAP4 account",   folder_item_parent(item) == NULL);
+       
+       SET_SENS("/Subscriptions/Unsubscribe...",    item->stype == F_NORMAL && folder_item_parent(item) != NULL);
+       SET_SENS("/Subscriptions/Subscribe...",    TRUE);
+       menu_set_active(factory, "/Subscriptions/Show only subscribed folders", item->folder->account->imap_subsonly);
 
 #undef SET_SENS
 }
@@ -117,7 +142,8 @@ static void new_folder_cb(FolderView *folderview, guint action,
        gchar *new_folder;
        gchar *name;
        gchar *p;
-
+       gchar separator = '/';
+       
        if (!folderview->selected) return;
 
        item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
@@ -128,16 +154,24 @@ static void new_folder_cb(FolderView *folderview, guint action,
        new_folder = input_dialog
                (_("New folder"),
                 _("Input the name of new folder:\n"
-                  "(if you want to create a folder to store subfolders,\n"
-                  " append `/' at the end of the name)"),
+                  "(if you want to create a folder to store subfolders\n"
+                  "only and no mail, append '/' to the folder name)"),
                 _("NewFolder"));
        if (!new_folder) return;
        AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
 
-       p = strchr(new_folder, G_DIR_SEPARATOR);
+       separator = imap_get_path_separator_for_item(item);
+
+       p = strchr(new_folder, separator);
        if (p && *(p + 1) != '\0') {
-               alertpanel_error(_("`%c' can't be included in folder name."),
-                                G_DIR_SEPARATOR);
+               alertpanel_error(_("'%c' can't be included in folder name."),
+                                separator);
+               return;
+       }
+       p = strchr(new_folder, '/');
+       if (p && *(p + 1) != '\0') {
+               alertpanel_error(_("'%c' can't be included in folder name."),
+                                '/');
                return;
        }
 
@@ -146,13 +180,13 @@ static void new_folder_cb(FolderView *folderview, guint action,
 
        /* find whether the directory already exists */
        if (folder_find_child_item_by_name(item, new_folder)) {
-               alertpanel_error(_("The folder `%s' already exists."), name);
+               alertpanel_error(_("The folder '%s' already exists."), name);
                return;
        }
 
        new_item = folder_create_folder(item, new_folder);
        if (!new_item) {
-               alertpanel_error(_("Can't create the folder `%s'."), name);
+               alertpanel_error(_("Can't create the folder '%s'."), name);
                return;
        }
        folder_write_list();
@@ -169,6 +203,7 @@ static void rename_folder_cb(FolderView *folderview, guint action,
        gchar *old_id;
        gchar *new_id;
        gchar *base;
+       gchar separator = '/';
 
        item = folderview_get_selected_item(folderview);
        g_return_if_fail(item != NULL);
@@ -176,7 +211,7 @@ static void rename_folder_cb(FolderView *folderview, guint action,
        g_return_if_fail(item->folder != NULL);
 
        name = trim_string(item->name, 32);
-       message = g_strdup_printf(_("Input new name for `%s':"), name);
+       message = g_strdup_printf(_("Input new name for '%s':"), name);
        base = g_path_get_basename(item->path);
        new_folder = input_dialog(_("Rename folder"), message, base);
        g_free(base);
@@ -185,17 +220,21 @@ static void rename_folder_cb(FolderView *folderview, guint action,
        if (!new_folder) return;
        AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
 
-/*
-       TODO: check new name for IMAP namespace separator
-       if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
+       separator = imap_get_path_separator_for_item(item);
+       if (strchr(new_folder, separator) != NULL) {
+               alertpanel_error(_("'%c' can't be included in folder name."),
+                                separator);
+               return;
+       }
+       if (strchr(new_folder, '/') != NULL) {
                alertpanel_error(_("`%c' can't be included in folder name."),
-                                G_DIR_SEPARATOR);
+                                '/');
                return;
        }
-*/
+
        if (folder_find_child_item_by_name(folder_item_parent(item), new_folder)) {
                name = trim_string(new_folder, 32);
-               alertpanel_error(_("The folder `%s' already exists."), name);
+               alertpanel_error(_("The folder '%s' already exists."), name);
                g_free(name);
                return;
        }
@@ -211,10 +250,9 @@ static void rename_folder_cb(FolderView *folderview, guint action,
                return;
        }
 
-       /* if (FOLDER_TYPE(item->folder) == F_MH)
-               prefs_filtering_rename_path(old_path, item->path); */
        new_id = folder_item_get_identifier(item);
        prefs_filtering_rename_path(old_id, new_id);
+       account_rename_path(old_id, new_id);
 
        g_free(old_id);
        g_free(new_id);
@@ -235,61 +273,7 @@ static void move_folder_cb(FolderView *folderview, guint action, GtkWidget *widg
        if (!to_folder)
                return;
        
-       folderview_move_folder(folderview, from_folder, to_folder);
-}
-
-static void imap_settings_cb(FolderView *folderview, guint action, GtkWidget *widget)
-{
-       FolderItem *item;
-
-       item = folderview_get_selected_item(folderview);
-       if (item == NULL)
-               return;
-
-       account_open(item->folder->account);
-}
-
-static void remove_server_cb(FolderView *folderview, guint action, GtkWidget *widget)
-{
-       GtkCTree *ctree = GTK_CTREE(folderview->ctree);
-       FolderItem *item;
-       PrefsAccount *account;
-       gchar *name;
-       gchar *message;
-       AlertValue avalue;
-
-       if (!folderview->selected) return;
-
-       item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
-       g_return_if_fail(item != NULL);
-       g_return_if_fail(item->folder != NULL);
-       g_return_if_fail(item->folder->account != NULL);
-
-       name = trim_string(item->folder->name, 32);
-       message = g_strdup_printf(_("Really delete IMAP4 account `%s'?"), name);
-       avalue = alertpanel(_("Delete IMAP4 account"), message,
-                           _("Yes"), _("+No"), NULL);
-       g_free(message);
-       g_free(name);
-
-       if (avalue != G_ALERTDEFAULT) return;
-
-       if (folderview->opened == folderview->selected ||
-           gtk_ctree_is_ancestor(ctree,
-                                 folderview->selected,
-                                 folderview->opened)) {
-               summary_clear_all(folderview->summaryview);
-               folderview->opened = NULL;
-       }
-
-       account = item->folder->account;
-       folderview_unselect(folderview);
-       summary_clear_all(folderview->summaryview);
-       folder_destroy(item->folder);
-       account_destroy(account);
-       account_set_menu();
-       main_window_reflect_prefs_all();
-       folder_write_list();
+       folderview_move_folder(folderview, from_folder, to_folder, action);
 }
 
 static void delete_folder_cb(FolderView *folderview, guint action,
@@ -312,12 +296,14 @@ static void delete_folder_cb(FolderView *folderview, guint action,
        name = trim_string(item->name, 32);
        AUTORELEASE_STR(name, {g_free(name); return;});
        message = g_strdup_printf
-               (_("All folders and messages under `%s' will be deleted.\n"
+               (_("All folders and messages under '%s' will be permanently deleted. "
+                  "Recovery will not be possible.\n\n"
                   "Do you really want to delete?"), name);
-       avalue = alertpanel(_("Delete folder"), message,
-                           _("Yes"), _("+No"), NULL);
+       avalue = alertpanel_full(_("Delete folder"), message,
+                                GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, FALSE,
+                                NULL, ALERT_WARNING, G_ALERTDEFAULT);
        g_free(message);
-       if (avalue != G_ALERTDEFAULT) return;
+       if (avalue != G_ALERTALTERNATE) return;
 
        Xstrdup_a(old_path, item->path, return);
        old_id = folder_item_get_identifier(item);
@@ -332,7 +318,7 @@ static void delete_folder_cb(FolderView *folderview, guint action,
 
        if (item->folder->klass->remove_folder(item->folder, item) < 0) {
                folder_item_scan(item);
-               alertpanel_error(_("Can't remove the folder `%s'."), name);
+               alertpanel_error(_("Can't remove the folder '%s'."), name);
                g_free(old_id);
                return;
        }
@@ -358,20 +344,27 @@ static void update_tree_cb(FolderView *folderview, guint action,
 
        if (action == 0)
                folderview_check_new(item->folder);
-       else
-               folderview_rescan_tree(item->folder);
+       else if (action == 1)
+               folderview_rescan_tree(item->folder, FALSE);
+       else if (action == 2)
+               folderview_rescan_tree(item->folder, TRUE);
 }
 
-static void download_cb(FolderView *folderview, guint action,
-                       GtkWidget *widget)
+static void sync_cb(FolderView *folderview, guint action,
+                          GtkWidget *widget)
 {
-       GtkCTree *ctree = GTK_CTREE(folderview->ctree);
-       MainWindow *mainwin = folderview->mainwin;
        FolderItem *item;
 
-       if (!folderview->selected) return;
+       item = folderview_get_selected_item(folderview);
+       g_return_if_fail(item != NULL);
+       folder_synchronise(item->folder);
+}
 
-       item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
+void imap_gtk_synchronise(FolderItem *item, gint days)
+{
+       MainWindow *mainwin = mainwindow_get_mainwindow();
+       FolderView *folderview = mainwin->folderview;
+       
        g_return_if_fail(item != NULL);
        g_return_if_fail(item->folder != NULL);
 
@@ -380,37 +373,181 @@ static void download_cb(FolderView *folderview, guint action,
        main_window_lock(mainwin);
        gtk_widget_set_sensitive(folderview->ctree, FALSE);
        main_window_progress_on(mainwin);
-       summaryview_lock(folderview->summaryview, item);
        GTK_EVENTS_FLUSH();
-       if (folder_item_fetch_all_msg(item) < 0) {
-               gchar *name;
-
-               name = trim_string(item->name, 32);
-               alertpanel_error(_("Error occurred while downloading messages in `%s'."), name);
-               g_free(name);
+       if (item->no_select == FALSE) {
+               GSList *mlist;
+               GSList *cur;
+               gint num = 0;
+               gint total = item->total_msgs;
+               time_t t = time(NULL);
+
+               mlist = folder_item_get_msg_list(item);
+               for (cur = mlist; cur != NULL; cur = cur->next) {
+                       MsgInfo *msginfo = (MsgInfo *)cur->data;
+                       gint age = (t - msginfo->date_t) / (60*60*24);
+                       if (days == 0 || age <= days)
+                               imap_cache_msg(msginfo->folder, msginfo->msgnum);
+                       statusbar_progress_all(num++,total, 100);
+                       if (num % 100 == 0)
+                               GTK_EVENTS_FLUSH();
+               }
+
+               statusbar_progress_all(0,0,0);
+               procmsg_msg_list_free(mlist);
        }
+
        folder_set_ui_func(item->folder, NULL, NULL);
        main_window_progress_off(mainwin);
-       summaryview_unlock(folderview->summaryview, item);
        gtk_widget_set_sensitive(folderview->ctree, TRUE);
        main_window_unlock(mainwin);
        inc_unlock();
        main_window_cursor_normal(mainwin);
 }
 
-gboolean imap_gtk_should_override(void)
+static void chk_update_val(GtkWidget *widget, gpointer data)
+{
+        gboolean *val = (gboolean *)data;
+       *val = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
+}
+
+static gboolean imap_gtk_subscribe_func(GNode *node, gpointer data)
 {
-       static time_t overridden = 0;
-       if (prefs_common.work_offline) {
-               if (time(NULL) - overridden < 600)
-                        return TRUE;
-               else if (alertpanel(_("Offline warning"), 
-                              _("You're working offline. Override during 10 minutes?"),
-                              _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
-                       return FALSE;
-
-               overridden = time(NULL);
+       FolderItem *item = node->data;
+       gboolean action = GPOINTER_TO_INT(data);
+       
+       if (item->path)
+               imap_subscribe(item->folder, item, NULL, action);
+
+       return FALSE;
+}
+
+static void subscribe_cb(FolderView *folderview, guint action,
+                          GtkWidget *widget)
+{
+       GtkCTree *ctree = GTK_CTREE(folderview->ctree);
+       FolderItem *item;
+       gchar *message, *name;
+       AlertValue avalue;
+       GtkWidget *rec_chk;
+       gboolean recurse = FALSE;
+
+       if (!folderview->selected) return;
+
+       item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
+       g_return_if_fail(item != NULL);
+       g_return_if_fail(item->folder != NULL);
+
+       name = trim_string(item->name, 32);
+       AUTORELEASE_STR(name, {g_free(name); return;});
+       
+       if (action && item->folder->account->imap_subsonly) {
+               GList *child_list = NULL;
+               
+               message = g_strdup_printf
+                       (_("Do you want to search for unsubscribed subfolders of '%s'?"),
+                        name);
+
+               rec_chk = gtk_check_button_new_with_label(_("Search recursively"));
+
+               g_signal_connect(G_OBJECT(rec_chk), "toggled", 
+                               G_CALLBACK(chk_update_val), &recurse);
+
+               avalue = alertpanel_full(_("Subscriptions"), message,
+                                        GTK_STOCK_CANCEL, _("+_Search"), NULL, FALSE,
+                                        rec_chk, ALERT_QUESTION, G_ALERTDEFAULT);
+               g_free(message);
+               if (avalue != G_ALERTALTERNATE) return;
+               
+               child_list = imap_scan_subtree(item->folder, item, TRUE, recurse);
+               if (child_list) {
+                       GList *cur;
+                       int r = -1;
+                       gchar *msg = g_strdup_printf(_("Choose a subfolder of %s to subscribe to: "),
+                                       item->name); 
+                       gchar *child_folder = input_dialog_combo(_("Subscribe"), 
+                                       msg,
+                                       child_list->next?_("All of them"):child_list->data, child_list, TRUE);
+                       g_free(msg);
+                       if (child_folder && strcmp(child_folder, _("All of them"))) {
+                               r = imap_subscribe(item->folder, NULL, child_folder, TRUE);
+                       } else if (child_folder) {
+                               for (cur = child_list; cur; cur = cur->next) 
+                                       r = imap_subscribe(item->folder, NULL, (gchar *)cur->data, TRUE);
+                       }
+                       g_free(child_folder);
+                       for (cur = child_list; cur; cur = cur->next) 
+                               g_free((gchar *)cur->data);
+                       if (r == 0)
+                               folderview_fast_rescan_tree(item->folder);
+               } else {
+                       alertpanel_notice(_("This folder is already subscribed and "
+                                 "has no unsubscribed subfolders.\n\nIf there are new folders, "
+                                 "created and subscribed to from another client, use \"Check "
+                                 "for new folders\" at the mailbox's root folder."));
+               }
+               g_list_free(child_list);
+               return;
        }
-       return TRUE;
+       message = g_strdup_printf
+               (_("Do you want to %s the '%s' folder?"),
+                  action?_("subscribe"):_("unsubscribe"), name);
        
+       rec_chk = gtk_check_button_new_with_label(_("Apply to subfolders"));
+       
+       g_signal_connect(G_OBJECT(rec_chk), "toggled", 
+                       G_CALLBACK(chk_update_val), &recurse);
+
+       avalue = alertpanel_full(_("Subscriptions"), message,
+                                GTK_STOCK_CANCEL, action?_("+_Subscribe"):_("+_Unsubscribe"), NULL, FALSE,
+                                rec_chk, ALERT_QUESTION, G_ALERTDEFAULT);
+       g_free(message);
+       if (avalue != G_ALERTALTERNATE) return;
+       
+       
+       if (!action) {
+               if (folderview->opened == folderview->selected ||
+                   gtk_ctree_is_ancestor(ctree,
+                                         folderview->selected,
+                                         folderview->opened)) {
+                       summary_clear_all(folderview->summaryview);
+                       folderview->opened = NULL;
+               }
+       }
+
+       if (recurse) {
+               g_node_traverse(item->node, G_PRE_ORDER,
+                       G_TRAVERSE_ALL, -1, imap_gtk_subscribe_func, GINT_TO_POINTER(action));
+       } else {
+               imap_subscribe(item->folder, item, NULL, action);
+       }
+
+       if (!action && item->folder->account->imap_subsonly)
+               folderview_fast_rescan_tree(item->folder);
+}
+
+static void subscribed_cb(FolderView *folderview, guint action,
+                          GtkWidget *widget)
+{
+       GtkCTree *ctree = GTK_CTREE(folderview->ctree);
+       FolderItem *item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
+       
+       if (!item || !item->folder || !item->folder->account)
+               return;
+       if (item->folder->account->imap_subsonly == GTK_CHECK_MENU_ITEM(widget)->active)
+               return;
+
+       item->folder->account->imap_subsonly = GTK_CHECK_MENU_ITEM(widget)->active;
+       folderview_fast_rescan_tree(item->folder);
+}
+
+static void download_cb(FolderView *folderview, guint action,
+                       GtkWidget *widget)
+{
+       GtkCTree *ctree = GTK_CTREE(folderview->ctree);
+       FolderItem *item;
+
+       if (!folderview->selected) return;
+
+       item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
+       imap_gtk_synchronise(item, 0);
 }