* src/folder.c
[claws.git] / src / folderview.c
index 23a7ad0bc4b40c9d326a096c1a0dfaefdd4ee659..8c73a2a1e3d1d8eb7ed4df2f780b127dc6577c2e 100644 (file)
 #include "gtkutils.h"
 #include "prefs_common.h"
 #include "prefs_account.h"
+#include "prefs_filter.h"
+#include "prefs_filtering.h"
+#include "prefs_scoring.h"
+#include "prefs_folder_item.h"
 #include "account.h"
 #include "folder.h"
 #include "inc.h"
@@ -94,24 +98,46 @@ static GtkStyle *bold_style;
 static GtkStyle *bold_color_style;
 static GtkStyle *bold_tgtfold_style;
 
-static GdkPixmap *inboxxpm;
+static GdkBitmap *inboxxpm;
 static GdkBitmap *inboxxpmmask;
 static GdkPixmap *inboxhrmxpm;
 static GdkBitmap *inboxhrmxpmmask;
+static GdkPixmap *inboxopenxpm;
+static GdkBitmap *inboxopenxpmmask;
+static GdkPixmap *inboxopenhrmxpm;
+static GdkBitmap *inboxopenhrmxpmmask;
 static GdkPixmap *outboxxpm;
 static GdkBitmap *outboxxpmmask;
 static GdkPixmap *outboxhrmxpm;
 static GdkBitmap *outboxhrmxpmmask;
+static GdkPixmap *outboxopenxpm;
+static GdkBitmap *outboxopenxpmmask;
+static GdkPixmap *outboxopenhrmxpm;
+static GdkBitmap *outboxopenhrmxpmmask;
 static GdkPixmap *folderxpm;
 static GdkBitmap *folderxpmmask;
+static GdkPixmap *folderhrmxpm;
+static GdkBitmap *folderhrmxpmmask;
 static GdkPixmap *folderopenxpm;
 static GdkBitmap *folderopenxpmmask;
 static GdkPixmap *folderopenhrmxpm;
 static GdkBitmap *folderopenhrmxpmmask;
+static GdkPixmap *trashopenxpm;
+static GdkBitmap *trashopenxpmmask;
+static GdkPixmap *trashopenhrmxpm;
+static GdkBitmap *trashopenhrmxpmmask;
 static GdkPixmap *trashxpm;
 static GdkBitmap *trashxpmmask;
 static GdkPixmap *trashhrmxpm;
 static GdkBitmap *trashhrmxpmmask;
+static GdkPixmap *queuexpm;
+static GdkBitmap *queuexpmmask;
+static GdkPixmap *queuehrmxpm;
+static GdkBitmap *queuehrmxpmmask;
+static GdkPixmap *queueopenxpm;
+static GdkBitmap *queueopenxpmmask;
+static GdkPixmap *queueopenhrmxpm;
+static GdkBitmap *queueopenhrmxpmmask;
 static GdkPixmap *newxpm;
 static GdkBitmap *newxpmmask;
 static GdkPixmap *unreadxpm;
@@ -132,6 +158,9 @@ static GtkCTreeNode *folderview_find_by_name        (GtkCTree       *ctree,
                                                 GtkCTreeNode   *node,
                                                 const gchar    *name);
 
+static gint folderview_clist_compare   (GtkCList       *clist,
+                                        gconstpointer   ptr1,
+                                        gconstpointer   ptr2);
 static gint folderview_compare_name    (gconstpointer   a,
                                         gconstpointer   b);
 
@@ -191,9 +220,6 @@ static void folderview_remove_mailbox_cb(FolderView *folderview,
 static void folderview_new_imap_folder_cb(FolderView   *folderview,
                                          guint          action,
                                          GtkWidget     *widget);
-static void folderview_rm_imap_folder_cb (FolderView   *folderview,
-                                         guint          action,
-                                         GtkWidget     *widget);
 static void folderview_rm_imap_server_cb (FolderView   *folderview,
                                          guint          action,
                                          GtkWidget     *widget);
@@ -212,6 +238,10 @@ static void folderview_search_cb   (FolderView     *folderview,
                                         guint           action,
                                         GtkWidget      *widget);
 
+static void folderview_property_cb     (FolderView     *folderview,
+                                        guint           action,
+                                        GtkWidget      *widget);
+
 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
                                          GdkDragContext *context,
                                          gint            x,
@@ -234,7 +264,6 @@ static void folderview_scoring_cb(FolderView *folderview, guint action,
                                  GtkWidget *widget);
 static void folderview_processing_cb(FolderView *folderview, guint action,
                                     GtkWidget *widget);
-static void folderview_property_cb(FolderView *folderview, guint action, GtkWidget *widget);
 
 static GtkItemFactoryEntry folderview_mbox_popup_entries[] =
 {
@@ -274,8 +303,8 @@ static GtkItemFactoryEntry folderview_imap_popup_entries[] =
        {N_("/Mark all _read"),         NULL, mark_all_read_cb, 0, NULL},
        {N_("/---"),                    NULL, NULL, 0, "<Separator>"},
        {N_("/Create _new folder..."),  NULL, folderview_new_imap_folder_cb, 0, NULL},
-       {N_("/_Rename folder..."),      NULL, NULL, 0, NULL},
-       {N_("/_Delete folder"),         NULL, folderview_rm_imap_folder_cb, 0, NULL},
+       {N_("/_Rename folder..."),      NULL, folderview_rename_folder_cb,   0, NULL},
+       {N_("/_Delete folder"),         NULL, folderview_delete_folder_cb,   0, NULL},
        {N_("/---"),                    NULL, NULL, 0, "<Separator>"},
        {N_("/_Check for new messages"),
                                        NULL, folderview_update_tree_cb, 0, NULL},
@@ -284,7 +313,7 @@ static GtkItemFactoryEntry folderview_imap_popup_entries[] =
        {N_("/Remove _IMAP4 account"),  NULL, folderview_rm_imap_server_cb, 0, NULL},
        {N_("/---"),                    NULL, NULL, 0, "<Separator>"},
        {N_("/_Search folder..."),      NULL, folderview_search_cb, 0, NULL},
-       {N_("/_Property..."),           NULL, NULL, 0, NULL},
+       {N_("/_Property..."),           NULL, folderview_property_cb, 0, NULL},
        {N_("/_Processing..."),         NULL, folderview_processing_cb, 0, NULL},
        {N_("/S_coring..."),            NULL, folderview_scoring_cb, 0, NULL}
 };
@@ -300,7 +329,7 @@ static GtkItemFactoryEntry folderview_news_popup_entries[] =
        {N_("/Remove _news account"),    NULL, folderview_rm_news_server_cb, 0, NULL},
        {N_("/---"),                     NULL, NULL, 0, "<Separator>"},
        {N_("/_Search folder..."),       NULL, folderview_search_cb, 0, NULL},
-       {N_("/_Property..."),            NULL, NULL, 0, NULL},
+       {N_("/_Property..."),            NULL, folderview_property_cb, 0, NULL},
        {N_("/_Processing..."),          NULL, folderview_processing_cb, 0, NULL},
        {N_("/S_coring..."),            NULL, folderview_scoring_cb, 0, NULL}
 };
@@ -311,8 +340,7 @@ FolderView *folderview_create(void)
        FolderView *folderview;
        GtkWidget *scrolledwin;
        GtkWidget *ctree;
-       gchar *titles[N_FOLDER_COLS] = {_("Folder"), _("New"),
-                                       _("Unread"), _("#")};
+       gchar *titles[N_FOLDER_COLS];
        GtkWidget *mail_popup;
        GtkWidget *news_popup;
        GtkWidget *imap_popup;
@@ -327,10 +355,16 @@ FolderView *folderview_create(void)
        debug_print(_("Creating folder view...\n"));
        folderview = g_new0(FolderView, 1);
 
+       titles[COL_FOLDER] = _("Folder");
+       titles[COL_NEW]    = _("New");
+       titles[COL_UNREAD] = _("Unread");
+       titles[COL_TOTAL]  = _("#");
+
        scrolledwin = gtk_scrolled_window_new(NULL, NULL);
-       gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
-                                      GTK_POLICY_AUTOMATIC,
-                                      GTK_POLICY_ALWAYS);
+       gtk_scrolled_window_set_policy
+               (GTK_SCROLLED_WINDOW(scrolledwin),
+                GTK_POLICY_AUTOMATIC,
+                prefs_common.folderview_vscrollbar_policy);
        gtk_widget_set_usize(scrolledwin,
                             prefs_common.folderview_width,
                             prefs_common.folderview_height);
@@ -359,6 +393,7 @@ FolderView *folderview_create(void)
        gtk_ctree_set_expander_style(GTK_CTREE(ctree),
                                     GTK_CTREE_EXPANDER_SQUARE);
        gtk_ctree_set_indent(GTK_CTREE(ctree), CTREE_INDENT);
+       gtk_clist_set_compare_func(GTK_CLIST(ctree), folderview_clist_compare);
 
        /* don't let title buttons take key focus */
        for (i = 0; i < N_FOLDER_COLS; i++)
@@ -468,22 +503,26 @@ void folderview_init(FolderView *folderview)
        GtkWidget *hbox_new;
        GtkWidget *hbox_unread;
 
-       stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX, &inboxxpm, &inboxxpmmask);
-       stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX,
-                        &outboxxpm, &outboxxpmmask);
-       stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_CLOSE,
-                        &folderxpm, &folderxpmmask);
-       stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_OPEN,
-                        &folderopenxpm, &folderopenxpmmask);
-       stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH, &trashxpm, &trashxpmmask);
-       stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_HRM, 
-                        &inboxhrmxpm, &inboxhrmxpmmask);
-       stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_HRM, 
-                        &outboxhrmxpm, &outboxhrmxpmmask);
-       stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_OPEN_HRM, 
-                        &folderopenhrmxpm, &folderopenhrmxpmmask);
-       stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_HRM, 
-                        &trashhrmxpm, &trashhrmxpmmask);
+       stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_CLOSE, &inboxxpm, &inboxxpmmask);
+       stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_CLOSE_HRM, &inboxhrmxpm, &inboxhrmxpmmask);
+       stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_OPEN, &inboxopenxpm, &inboxopenxpmmask);
+       stock_pixmap_gdk(ctree, STOCK_PIXMAP_INBOX_OPEN_HRM, &inboxopenhrmxpm, &inboxopenhrmxpmmask);
+       stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_CLOSE, &outboxxpm, &outboxxpmmask);
+       stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_CLOSE_HRM, &outboxhrmxpm, &outboxhrmxpmmask);
+       stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_OPEN, &outboxopenxpm, &outboxopenxpmmask);
+       stock_pixmap_gdk(ctree, STOCK_PIXMAP_OUTBOX_OPEN_HRM, &outboxopenhrmxpm, &outboxopenhrmxpmmask);
+       stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_CLOSE, &folderxpm, &folderxpmmask);
+       stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_CLOSE_HRM, &folderhrmxpm, &folderhrmxpmmask);
+       stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_OPEN, &folderopenxpm, &folderopenxpmmask);
+       stock_pixmap_gdk(ctree, STOCK_PIXMAP_DIR_OPEN_HRM, &folderopenhrmxpm, &folderopenhrmxpmmask);
+       stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_OPEN, &trashopenxpm, &trashopenxpmmask);
+       stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_OPEN_HRM, &trashopenhrmxpm, &trashopenhrmxpmmask);
+       stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_CLOSE, &trashxpm, &trashxpmmask);
+       stock_pixmap_gdk(ctree, STOCK_PIXMAP_TRASH_CLOSE_HRM, &trashhrmxpm, &trashhrmxpmmask);
+       stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_CLOSE, &queuexpm, &queuexpmmask);
+       stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_CLOSE_HRM, &queuehrmxpm, &queuehrmxpmmask);
+       stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_OPEN, &queueopenxpm, &queueopenxpmmask);
+       stock_pixmap_gdk(ctree, STOCK_PIXMAP_QUEUE_OPEN_HRM, &queueopenhrmxpm, &queueopenhrmxpmmask);
 
        /* CLAWS: titles for "New" and "Unread" show new & unread pixmaps
         * instead text (text overflows making them unreadable and ugly) */
@@ -651,18 +690,26 @@ void folderview_select_next_unread(FolderView *folderview)
                folderview_select_node(folderview, node);
 }
 
-void folderview_update_msg_num(FolderView *folderview, GtkCTreeNode *row,
-                              gint new, gint unread, gint total)
+void folderview_update_msg_num(FolderView *folderview, GtkCTreeNode *row)
 {
        GtkCTree *ctree = GTK_CTREE(folderview->ctree);
        static GtkCTreeNode *prev_row = NULL;
        FolderItem *item;
+       gint new, unread, total;
+       gchar *new_str, *unread_str, *total_str;
 
        if (!row) return;
 
        item = gtk_ctree_node_get_row_data(ctree, row);
        if (!item) return;
 
+       gtk_ctree_node_get_text(ctree, row, COL_NEW, &new_str);
+       gtk_ctree_node_get_text(ctree, row, COL_UNREAD, &unread_str);
+       gtk_ctree_node_get_text(ctree, row, COL_TOTAL, &total_str);
+       new = atoi(new_str);
+       unread = atoi(unread_str);
+       total = atoi(total_str);
+
        /* CLAWS: don't know why but this always seems to be true
         * when deleting messages. Somewhere claws does a folder
         * scan which sets all new, unread & total to the correct
@@ -672,16 +719,12 @@ void folderview_update_msg_num(FolderView *folderview, GtkCTreeNode *row,
        if (prev_row     == row    &&
            item->new    == new    &&
            item->unread == unread &&
-           item->total  == total) 
+           item->total  == total)
                return;
 #endif         
 
        prev_row = row;
 
-       item->new    = new;
-       item->unread = unread;
-       item->total  = total;
-
        folderview_update_node(folderview, row);
 }
 
@@ -751,7 +794,7 @@ static GtkWidget *label_window_create(const gchar *str)
        return window;
 }
 
-void folderview_update_tree(Folder *folder)
+void folderview_rescan_tree(Folder *folder)
 {
        GtkWidget *window;
 
@@ -769,11 +812,13 @@ void folderview_update_tree(Folder *folder)
        folder_write_list();
        folderview_set_all();
 
+       folderview_check_new(folder);
+       
        gtk_widget_destroy(window);
        inc_unlock();
 }
 
-void folderview_update_all(void)
+void folderview_rescan_all(void)
 {
        GList *list;
        GtkWidget *window;
@@ -785,7 +830,6 @@ void folderview_update_all(void)
        for (; list != NULL; list = list->next) {
                Folder *folder = list->data;
 
-               if (!folder->scan_tree) continue;
                folder_set_ui_func(folder, folderview_scan_tree_func, NULL);
                folder->scan_tree(folder);
                folder_set_ui_func(folder, NULL, NULL);
@@ -793,11 +837,19 @@ void folderview_update_all(void)
 
        folder_write_list();
        folderview_set_all();
+
+       list = folder_get_list();
+       for (; list != NULL; list = list->next) {
+               Folder *folder = list->data;
+
+               folderview_check_new(folder);
+       }
+
        gtk_widget_destroy(window);
        inc_unlock();
 }
 
-void folderview_update_all_node(void)
+void folderview_check_new(Folder *folder)
 {
        GList *list;
        FolderItem *item;
@@ -816,11 +868,16 @@ void folderview_update_all_node(void)
                for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
                     node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
                        item = gtk_ctree_node_get_row_data(ctree, node);
-                       if (!item || !FOLDER_IS_LOCAL(item->folder) ||
-                           !item->path)
-                               continue;
+                       if (!item || !item->path || !item->folder) continue;
+                       if (item->no_select) continue;
+                       if (folder && folder != item->folder) continue;
+                       if (!folder && !FOLDER_IS_LOCAL(item->folder)) continue;
+
                        folderview_scan_tree_func(item->folder, item, NULL);
-                       folder_item_scan(item);
+                       if (folder_item_scan(item) < 0) {
+                               if (folder && !FOLDER_IS_LOCAL(folder))
+                                       break;
+                       }
                        folderview_update_node(folderview, node);
                }
 
@@ -941,52 +998,68 @@ static void folderview_update_node(FolderView *folderview, GtkCTreeNode *node)
 
        switch (item->stype) {
        case F_INBOX:
-               xpm = inboxxpm;
-               mask = inboxxpmmask;
                if (item->hide_read_msgs) {
-                       openxpm = inboxhrmxpm;
-                       openmask = inboxhrmxpmmask;
+                       xpm = inboxhrmxpm;
+                       mask = inboxhrmxpmmask;
+                       openxpm = inboxopenhrmxpm;
+                       openmask = inboxopenhrmxpmmask;
                } else {
-                       openxpm = inboxxpm;
-                       openmask = inboxxpmmask;
+                       xpm = inboxxpm;
+                       mask = inboxxpmmask;
+                       openxpm = inboxopenxpm;
+                       openmask = inboxopenxpmmask;
                }
-               name = g_strdup(_("Inbox"));
+               name = g_strdup(FOLDER_IS_LOCAL(item->folder) &&
+                               !strcmp2(item->name, INBOX_DIR) ? _("Inbox") :
+                               item->name);
                break;
        case F_OUTBOX:
-               xpm = outboxxpm;
-               mask =outboxxpmmask;
                if (item->hide_read_msgs) {
-                       openxpm = outboxhrmxpm;
-                       openmask = outboxhrmxpmmask;
+                       xpm = outboxhrmxpm;
+                       mask = outboxhrmxpmmask;
+                       openxpm = outboxopenhrmxpm;
+                       openmask = outboxopenhrmxpmmask;
                } else {
-                       openxpm = outboxxpm;
-                       openmask = outboxxpmmask;
+                       xpm = outboxxpm;
+                       mask = outboxxpmmask;
+                       openxpm = outboxopenxpm;
+                       openmask = outboxopenxpmmask;
                }
-               name = g_strdup(_("Outbox"));
+               name = g_strdup(FOLDER_IS_LOCAL(item->folder) &&
+                               !strcmp2(item->name, OUTBOX_DIR) ? _("Sent") :
+                               item->name);
                break;
        case F_QUEUE:
-               xpm = outboxxpm;
-               mask =outboxxpmmask;
                if (item->hide_read_msgs) {
-                       openxpm = outboxhrmxpm;
-                       openmask = outboxhrmxpmmask;
+                       xpm = queuehrmxpm;
+                       mask = queuehrmxpmmask;
+                       openxpm = queueopenhrmxpm;
+                       openmask = queueopenhrmxpmmask;
                } else {
-                       openxpm = outboxxpm;
-                       openmask = outboxxpmmask;
+                       xpm = queuexpm;
+                       mask = queuexpmmask;
+                       openxpm = queueopenxpm;
+                       openmask = queueopenxpmmask;
                }
-               name = g_strdup(_("Queue"));
+               name = g_strdup(FOLDER_IS_LOCAL(item->folder) &&
+                               !strcmp2(item->name, QUEUE_DIR) ? _("Queue") :
+                               item->name);
                break;
        case F_TRASH:
-               xpm = trashxpm;
-               mask = trashxpmmask;
                if (item->hide_read_msgs) {
-                       openxpm = trashhrmxpm;
-                       openmask = trashhrmxpmmask;
+                       xpm = trashhrmxpm;
+                       mask = trashhrmxpmmask;
+                       openxpm = trashopenhrmxpm;
+                       openmask = trashopenhrmxpmmask;
                } else {
-                       openxpm = trashxpm;
-                       openmask = trashxpmmask;
+                       xpm = trashxpm;
+                       mask = trashxpmmask;
+                       openxpm = trashopenxpm;
+                       openmask = trashopenxpmmask;
                }
-               name = g_strdup(_("Trash"));
+               name = g_strdup(FOLDER_IS_LOCAL(item->folder) &&
+                               !strcmp2(item->name, TRASH_DIR) ? _("Trash") :
+                               item->name);
                break;
        case F_DRAFT:
                xpm = folderxpm;
@@ -998,15 +1071,19 @@ static void folderview_update_node(FolderView *folderview, GtkCTreeNode *node)
                        openxpm = folderopenxpm;
                        openmask = folderopenxpmmask;
                }
-               name = g_strdup(_("Draft"));
+               name = g_strdup(FOLDER_IS_LOCAL(item->folder) &&
+                               !strcmp2(item->name, DRAFT_DIR) ? _("Drafts") :
+                               item->name);
                break;
        default:
-               xpm = folderxpm;
-               mask = folderxpmmask;
                if (item->hide_read_msgs) {
+                       xpm = folderhrmxpm;
+                       mask = folderhrmxpmmask;
                        openxpm = folderopenhrmxpm;
                        openmask = folderopenhrmxpmmask;
                } else {
+                       xpm = folderxpm;
+                       mask = folderxpmmask;
                        openxpm = folderopenxpm;
                        openmask = folderopenxpmmask;
                }
@@ -1024,8 +1101,15 @@ static void folderview_update_node(FolderView *folderview, GtkCTreeNode *node)
                                name = "";
                        }
                        name = g_strconcat(item->name, name, NULL);
-               } else
-                       name = g_strdup(item->name);
+               } else {
+                       if (item->folder->type == F_NEWS &&
+                           item->path &&
+                           !strcmp2(item->name, item->path) &&
+                           prefs_common.ng_abbrev_len < strlen(item->path))
+                               name = get_abbrev_newsgroup_name(item->path);
+                       else
+                               name = g_strdup(item->name);
+               }
        }
 
        if (!GTK_CTREE_ROW(node)->expanded &&
@@ -1070,9 +1154,10 @@ static void folderview_update_node(FolderView *folderview, GtkCTreeNode *node)
                gtk_ctree_node_set_text(ctree, node, COL_TOTAL,  itos(item->total));
        }
 
-       if (item->stype == F_TRASH)
+       if (item->stype == F_OUTBOX || item->stype == F_DRAFT ||
+           item->stype == F_TRASH) {
                use_bold = use_color = FALSE;
-       if (item->stype == F_QUEUE) {
+       } else if (item->stype == F_QUEUE) {
                /* highlight queue folder if there are any messages */
                use_bold = use_color = (item->total > 0);
        } else {
@@ -1181,18 +1266,19 @@ static void folderview_expand_func(GtkCTree *ctree, GtkCTreeNode *node,
 #define SET_SPECIAL_FOLDER(ctree, item) \
 { \
        if (item) { \
-               GtkCTreeNode *node, *sibling; \
+               GtkCTreeNode *node, *parent, *sibling; \
  \
                node = gtk_ctree_find_by_row_data(ctree, root, item); \
                if (!node) \
                        g_warning("%s not found.\n", item->path); \
                else { \
-                       if (!prev) \
-                               sibling = GTK_CTREE_ROW(root)->children; \
-                       else \
+                       parent = GTK_CTREE_ROW(node)->parent; \
+                       if (prev && parent == GTK_CTREE_ROW(prev)->parent) \
                                sibling = GTK_CTREE_ROW(prev)->sibling; \
+                       else \
+                               sibling = GTK_CTREE_ROW(parent)->children; \
                        if (node != sibling) \
-                               gtk_ctree_move(ctree, node, root, sibling); \
+                               gtk_ctree_move(ctree, node, parent, sibling); \
                } \
  \
                prev = node; \
@@ -1276,9 +1362,9 @@ void folderview_rename_folder(FolderView *folderview)
                folderview_rename_mbox_folder_cb(folderview, 0, NULL);
        case F_MH:
        case F_MAILDIR:
+       case F_IMAP:
                folderview_rename_folder_cb(folderview, 0, NULL);
                break;
-       case F_IMAP:
        case F_NEWS:
        default:
                break;
@@ -1302,10 +1388,9 @@ void folderview_delete_folder(FolderView *folderview)
        case F_MH:
        case F_MBOX:
        case F_MAILDIR:
+       case F_IMAP:
                folderview_delete_folder_cb(folderview, 0, NULL);
                break;
-       case F_IMAP:
-               folderview_rm_imap_folder_cb(folderview, 0, NULL);
        case F_NEWS:
        default:
                break;
@@ -1330,10 +1415,10 @@ static void folderview_button_pressed(GtkWidget *ctree, GdkEventButton *event,
        gboolean update_tree     = FALSE;
        gboolean rescan_tree     = FALSE;
        gboolean remove_tree     = FALSE;
+       gboolean search_folder   = FALSE;
        gboolean folder_property = FALSE;
        gboolean folder_processing  = FALSE;
        gboolean folder_scoring  = FALSE;
-       gboolean search_folder = FALSE;
 
        if (!event) return;
 
@@ -1380,14 +1465,14 @@ static void folderview_button_pressed(GtkWidget *ctree, GdkEventButton *event,
                if (item->parent == NULL)
                        update_tree = remove_tree = TRUE;
                else
-                       mark_all_read = search_folder = TRUE;
+                       mark_all_read = search_folder = folder_property = TRUE;
                if (FOLDER_IS_LOCAL(folder) || FOLDER_TYPE(folder) == F_IMAP || FOLDER_TYPE(folder) == F_MBOX) {
                        if (item->parent == NULL)
                                update_tree = rescan_tree = TRUE;
                        else if (item->stype == F_NORMAL)
-                               rename_folder = delete_folder = folder_property = folder_scoring = folder_processing = TRUE;
+                               rename_folder = delete_folder = folder_scoring = folder_processing = TRUE;
                        else if (item->stype == F_INBOX)
-                               folder_property = folder_scoring = folder_processing = TRUE;
+                               folder_scoring = folder_processing = TRUE;
                        else if (item->stype == F_TRASH)
                                folder_processing = TRUE;
                        else if (item->stype == F_OUTBOX)
@@ -1416,10 +1501,10 @@ static void folderview_button_pressed(GtkWidget *ctree, GdkEventButton *event,
                SET_SENS(mail_factory, "/Check for new messages", update_tree);
                SET_SENS(mail_factory, "/Rescan folder tree", rescan_tree);
                SET_SENS(mail_factory, "/Remove mailbox", remove_tree);
+               SET_SENS(mail_factory, "/Search folder...", search_folder);
                SET_SENS(mail_factory, "/Property...", folder_property);
                SET_SENS(mail_factory, "/Processing...", folder_processing);
                SET_SENS(mail_factory, "/Scoring...", folder_scoring);
-               SET_SENS(mail_factory, "/Search folder...", search_folder);
        } else if (FOLDER_TYPE(folder) == F_IMAP) {
                popup = folderview->imap_popup;
                menu_set_insensitive_all(GTK_MENU_SHELL(popup));
@@ -1430,9 +1515,10 @@ static void folderview_button_pressed(GtkWidget *ctree, GdkEventButton *event,
                SET_SENS(imap_factory, "/Check for new messages", update_tree);
                SET_SENS(imap_factory, "/Rescan folder tree", rescan_tree);
                SET_SENS(imap_factory, "/Remove IMAP4 account", remove_tree);
+               SET_SENS(imap_factory, "/Search folder...", search_folder);
+               SET_SENS(imap_factory, "/Property...", folder_property);
                SET_SENS(imap_factory, "/Processing...", folder_processing);
                SET_SENS(imap_factory, "/Scoring...", folder_scoring);
-               SET_SENS(imap_factory, "/Search folder...", search_folder);
        } else if (FOLDER_TYPE(folder) == F_NEWS) {
                popup = folderview->news_popup;
                menu_set_insensitive_all(GTK_MENU_SHELL(popup));
@@ -1444,6 +1530,7 @@ static void folderview_button_pressed(GtkWidget *ctree, GdkEventButton *event,
 #endif
                SET_SENS(news_factory, "/Remove news account", remove_tree);
                SET_SENS(news_factory, "/Search folder...", search_folder);
+               SET_SENS(news_factory, "/Property...", folder_property);
                SET_SENS(news_factory, "/Processing...", folder_processing);
                SET_SENS(news_factory, "/Scoring...", folder_scoring);
        } else if (FOLDER_TYPE(folder) == F_MBOX) {
@@ -1452,6 +1539,7 @@ static void folderview_button_pressed(GtkWidget *ctree, GdkEventButton *event,
                SET_SENS(mbox_factory, "/Create new folder...", new_folder);
                SET_SENS(mbox_factory, "/Rename folder...", rename_folder);
                SET_SENS(mbox_factory, "/Delete folder", delete_folder);
+               SET_SENS(news_factory, "/Property...", folder_property);
                SET_SENS(mbox_factory, "/Processing...", folder_processing);
                SET_SENS(mbox_factory, "/Scoring...", folder_scoring);
        } else
@@ -1517,7 +1605,7 @@ static void folderview_selected(GtkCTree *ctree, GtkCTreeNode *row,
                return;
        }
 
-       if (!can_select) {
+       if (!can_select || summary_is_locked(folderview->summaryview)) {
                gtkut_ctree_set_focus_row(ctree, folderview->opened);
                gtk_ctree_select(ctree, folderview->opened);
                return;
@@ -1537,7 +1625,7 @@ static void folderview_selected(GtkCTree *ctree, GtkCTreeNode *row,
                        (folderview->mainwin,
                         item->folder->type == F_NEWS ? 
                         COMPOSEBUTTON_NEWS : COMPOSEBUTTON_MAIL);
-        
+
        if (item->path)
                debug_print(_("Folder %s is selected\n"), item->path);
 
@@ -1554,13 +1642,23 @@ static void folderview_selected(GtkCTree *ctree, GtkCTreeNode *row,
                        gdk_pointer_ungrab(GDK_CURRENT_TIME);
        }
 
+       if((item->folder->type == F_IMAP) || (item->folder->type == F_NEWS)) {
+               folder_item_scan(item);
+       }
+
        opened = summary_show(folderview->summaryview, item, FALSE);
 
+       folder_clean_cache_memory();
+       
        if (!opened) {
                gtkut_ctree_set_focus_row(ctree, folderview->opened);
                gtk_ctree_select(ctree, folderview->opened);
-       } else
+       } else {
                folderview->opened = row;
+               if (gtk_ctree_node_is_visible(ctree, row)
+                   != GTK_VISIBILITY_FULL)
+                       gtk_ctree_node_moveto(ctree, row, -1, 0.5, 0);
+       }
 
        folderview->open_folder = FALSE;
        can_select = TRUE;
@@ -1615,6 +1713,7 @@ static void folderview_col_resized(GtkCList *clist, gint column, gint width,
                prefs_common.folder_col_total = width;
                break;
        default:
+               break;
        }
 }
 
@@ -1656,9 +1755,9 @@ static void folderview_update_tree_cb(FolderView *folderview, guint action,
        g_return_if_fail(item->folder != NULL);
 
        if (action == 0)
-               folderview_update_all_node();
+               folderview_check_new(item->folder);
        else
-               folderview_update_tree(item->folder);
+               folderview_rescan_tree(item->folder);
 }
 
 static void folderview_new_folder_cb(FolderView *folderview, guint action,
@@ -1669,6 +1768,7 @@ static void folderview_new_folder_cb(FolderView *folderview, guint action,
        FolderItem *item;
        FolderItem *new_item;
        gchar *new_folder;
+       gchar *name, *name_;
        GtkCTreeNode *node;
 
        if (!folderview->selected) return;
@@ -1691,18 +1791,20 @@ static void folderview_new_folder_cb(FolderView *folderview, guint action,
                }
        }
 
+       name_ = trim_string(new_folder, 32);
+       Xstrdup_a(name, name_, {g_free(new_folder); return;});
+       g_free(name_);
+
        /* find whether the directory already exists */
        if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
-               alertpanel_error(_("The folder `%s' already exists."),
-                                new_folder);
+               alertpanel_error(_("The folder `%s' already exists."), name);
                g_free(new_folder);
                return;
        }
 
        new_item = item->folder->create_folder(item->folder, item, new_folder);
        if (!new_item) {
-               alertpanel_error(_("The folder `%s' could not be created."), 
-                                new_folder);
+               alertpanel_error(_("Can't create the folder `%s'."), name);
                g_free(new_folder);
                return;
        } 
@@ -1781,7 +1883,11 @@ static void folderview_rename_folder_cb(FolderView *folderview, guint action,
        GtkCTree *ctree = GTK_CTREE(folderview->ctree);
        FolderItem *item;
        gchar *new_folder;
+       gchar *name, *name_;
        gchar *message;
+       gchar *old_path;
+       gchar *old_id;
+       gchar *new_id;
 
        if (!folderview->selected) return;
 
@@ -1790,8 +1896,10 @@ static void folderview_rename_folder_cb(FolderView *folderview, guint action,
        g_return_if_fail(item->path != NULL);
        g_return_if_fail(item->folder != NULL);
 
-       message = g_strdup_printf(_("Input new name for `%s':"),
-                                 g_basename(item->path));
+       name_ = trim_string(item->name, 32);
+       Xstrdup_a(name, name_, return);
+       g_free(name_);
+       message = g_strdup_printf(_("Input new name for `%s':"), name);
        new_folder = input_dialog(_("Rename folder"), message,
                                  g_basename(item->path));
        g_free(message);
@@ -1807,28 +1915,50 @@ static void folderview_rename_folder_cb(FolderView *folderview, guint action,
        if (folderview_find_by_name
                (ctree, GTK_CTREE_ROW(folderview->selected)->parent,
                 new_folder)) {
-               alertpanel_error(_("The folder `%s' already exists."),
-                                new_folder);
+               name = trim_string(new_folder, 32);
+               alertpanel_error(_("The folder `%s' already exists."), name);
+               g_free(name);
                g_free(new_folder);
                return;
        }
 
+       Xstrdup_a(old_path, item->path, {g_free(new_folder); return;});
+       old_id = folder_item_get_identifier(item);
+
        if (item->folder->rename_folder(item->folder, item, new_folder) < 0) {
+               g_free(old_id);
                g_free(new_folder);
                return;
        }
        g_free(new_folder);
 
+       if (prefs_common.fltlist) {
+               if (folder_get_default_folder() == item->folder)
+                       prefs_filter_rename_path(old_path, item->path);
+               new_id = folder_item_get_identifier(item);
+               prefs_filter_rename_path(old_id, new_id);
+       } else {
+               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);
+       }
+       g_free(old_id);
+       g_free(new_id);
+
        gtk_clist_freeze(GTK_CLIST(ctree));
 
        folderview_update_node(folderview, folderview->selected);
        folderview_sort_folders(folderview,
                                GTK_CTREE_ROW(folderview->selected)->parent,
                                item->folder);
-       if (folderview->opened == folderview->selected) {
-               if (!GTK_CTREE_ROW(folderview->opened)->children)
-                       gtk_ctree_expand(ctree, folderview->opened);
-               summary_show(folderview->summaryview, item, FALSE);
+       if (folderview->opened == folderview->selected ||
+           gtk_ctree_is_ancestor(ctree,
+                                 folderview->selected,
+                                 folderview->opened)) {
+               GtkCTreeNode *node = folderview->opened;
+               folderview_unselect(folderview);
+               folderview_select_node(folderview, node);
        }
 
        gtk_clist_thaw(GTK_CLIST(ctree));
@@ -1896,8 +2026,10 @@ static void folderview_delete_folder_cb(FolderView *folderview, guint action,
 {
        GtkCTree *ctree = GTK_CTREE(folderview->ctree);
        FolderItem *item;
-       gchar *message;
+       gchar *message, *name, *name_;
        AlertValue avalue;
+       gchar *old_path;
+       gchar *old_id;
 
        if (!folderview->selected) return;
 
@@ -1906,21 +2038,41 @@ static void folderview_delete_folder_cb(FolderView *folderview, guint action,
        g_return_if_fail(item->path != NULL);
        g_return_if_fail(item->folder != NULL);
 
+       name_ = trim_string(item->name, 32);
+       Xstrdup_a(name, name_, return);
+       g_free(name_);
        message = g_strdup_printf
                (_("All folder(s) and message(s) under `%s' will be deleted.\n"
-                  "Do you really want to delete?"),
-                g_basename(item->path));
+                  "Do you really want to delete?"), name);
        avalue = alertpanel(_("Delete folder"), message,
                            _("Yes"), _("+No"), NULL);
        g_free(message);
        if (avalue != G_ALERTDEFAULT) return;
 
+       Xstrdup_a(old_path, item->path, return);
+       old_id = folder_item_get_identifier(item);
+
        if (item->folder->remove_folder(item->folder, item) < 0) {
-               alertpanel_error(_("Can't remove the folder `%s'."),
-                                item->path);
+               alertpanel_error(_("Can't remove the folder `%s'."), name);
+               if (folderview->opened == folderview->selected)
+                       summary_show(folderview->summaryview,
+                                    folderview->summaryview->folder_item,
+                                    FALSE);
+               g_free(old_id);
                return;
        }
 
+       if (prefs_common.fltlist) {
+               if (folder_get_default_folder() == item->folder)
+                       prefs_filter_delete_path(old_path);
+               prefs_filter_delete_path(old_id);
+               g_free(old_id);
+       } else {
+               if (FOLDER_TYPE(item->folder) == F_MH)
+                       prefs_filtering_delete_path(old_path);
+               prefs_filtering_delete_path(old_id);
+               g_free(old_id);
+       }
        if (folderview->opened == folderview->selected ||
            gtk_ctree_is_ancestor(ctree,
                                  folderview->selected,
@@ -1939,6 +2091,7 @@ static void folderview_remove_mailbox_cb(FolderView *folderview, guint action,
        GtkCTree *ctree = GTK_CTREE(folderview->ctree);
        GtkCTreeNode *node;
        FolderItem *item;
+       gchar *name, *name_;
        gchar *message;
        AlertValue avalue;
 
@@ -1949,11 +2102,13 @@ static void folderview_remove_mailbox_cb(FolderView *folderview, guint action,
        g_return_if_fail(item->folder != NULL);
        if (item->parent) return;
 
+       name_ = trim_string(item->folder->name, 32);
+       Xstrdup_a(name, name_, return);
+       g_free(name_);
        message = g_strdup_printf
                (_("Really remove the mailbox `%s' ?\n"
-                  "(The messages are NOT deleted from disk)"),
-                item->folder->name);
-       avalue = alertpanel(_("Remove folder"), message,
+                  "(The messages are NOT deleted from the disk)"), name);
+       avalue = alertpanel(_("Remove mailbox"), message,
                            _("Yes"), _("+No"), NULL);
        g_free(message);
        if (avalue != G_ALERTDEFAULT) return;
@@ -1974,6 +2129,7 @@ static void folderview_new_imap_folder_cb(FolderView *folderview, guint action,
        FolderItem *item;
        FolderItem *new_item;
        gchar *new_folder;
+       gchar *name, *name_;
        gchar *p;
 
        if (!folderview->selected) return;
@@ -2000,18 +2156,20 @@ static void folderview_new_imap_folder_cb(FolderView *folderview, guint action,
                return;
        }
 
+       name_ = trim_string(new_folder, 32);
+       Xstrdup_a(name, name_, return);
+       g_free(name_);
+
        /* find whether the directory already exists */
        if (folderview_find_by_name(ctree, folderview->selected, new_folder)) {
-               alertpanel_error(_("The folder `%s' already exists."),
-                                new_folder);
+               alertpanel_error(_("The folder `%s' already exists."), name);
                g_free(new_folder);
                return;
        }
 
        new_item = item->folder->create_folder(item->folder, item, new_folder);
        if (!new_item) {
-               alertpanel_error(_("Can't create the folder `%s'."),
-                                new_folder);
+               alertpanel_error(_("Can't create the folder `%s'."), name);
                g_free(new_folder);
                return;
        }
@@ -2034,56 +2192,12 @@ static void folderview_new_imap_folder_cb(FolderView *folderview, guint action,
        folder_write_list();
 }
 
-static void folderview_rm_imap_folder_cb(FolderView *folderview, guint action,
-                                        GtkWidget *widget)
-{
-       GtkCTree *ctree = GTK_CTREE(folderview->ctree);
-       FolderItem *item;
-       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->type == F_IMAP);
-       g_return_if_fail(item->folder->account != NULL);
-
-       message = g_strdup_printf(_("Really delete folder `%s'?"),
-                                 g_basename(item->path));
-       avalue = alertpanel(_("Delete folder"), message,
-                           _("Yes"), _("+No"), NULL);
-       g_free(message);
-       if (avalue != G_ALERTDEFAULT) return;
-
-       if (item->folder->remove_folder(item->folder, item) < 0) {
-               alertpanel_error(_("Can't remove the folder `%s'."),
-                                item->path);
-               if (folderview->opened == folderview->selected)
-                       summary_show(folderview->summaryview,
-                                    folderview->summaryview->folder_item,
-                                    FALSE);
-               return;
-       }
-
-       if (folderview->opened == folderview->selected ||
-           gtk_ctree_is_ancestor(ctree,
-                                 folderview->selected,
-                                 folderview->opened)) {
-               summary_clear_all(folderview->summaryview);
-               folderview->opened = NULL;
-       }
-
-       gtk_ctree_remove_node(ctree, folderview->selected);
-       folder_write_list();
-}
-
 static void folderview_rm_imap_server_cb(FolderView *folderview, guint action,
                                         GtkWidget *widget)
 {
        GtkCTree *ctree = GTK_CTREE(folderview->ctree);
        FolderItem *item;
+       gchar *name, *name_;
        gchar *message;
        AlertValue avalue;
 
@@ -2095,8 +2209,10 @@ static void folderview_rm_imap_server_cb(FolderView *folderview, guint action,
        g_return_if_fail(item->folder->type == F_IMAP);
        g_return_if_fail(item->folder->account != NULL);
 
-       message = g_strdup_printf(_("Really delete IMAP4 account `%s'?"),
-                                 item->folder->name);
+       name_ = trim_string(item->folder->name, 32);
+       Xstrdup_a(name, name_, return);
+       g_free(name_);
+       message = g_strdup_printf(_("Really delete IMAP4 account `%s'?"), name);
        avalue = alertpanel(_("Delete IMAP4 account"), message,
                            _("Yes"), _("+No"), NULL);
        g_free(message);
@@ -2201,7 +2317,7 @@ static void folderview_new_news_group_cb(FolderView *folderview, guint action,
                gtk_ctree_node_set_row_data(ctree, node, newitem);
        }
 
-       gtk_ctree_sort_node(ctree, servernode);
+       folderview_sort_folders(folderview, servernode, folder);
        gtk_clist_thaw(GTK_CLIST(ctree));
 
        slist_free_strings(new_subscr);
@@ -2215,6 +2331,7 @@ static void folderview_rm_news_group_cb(FolderView *folderview, guint action,
 {
        GtkCTree *ctree = GTK_CTREE(folderview->ctree);
        FolderItem *item;
+       gchar *name, *name_;
        gchar *message;
        AlertValue avalue;
 
@@ -2226,8 +2343,10 @@ static void folderview_rm_news_group_cb(FolderView *folderview, guint action,
        g_return_if_fail(item->folder->type == F_NEWS);
        g_return_if_fail(item->folder->account != NULL);
 
-       message = g_strdup_printf(_("Really delete newsgroup `%s'?"),
-                                 g_basename(item->path));
+       name_ = trim_string(item->path, 32);
+       Xstrdup_a(name, name_, return);
+       g_free(name_);
+       message = g_strdup_printf(_("Really delete newsgroup `%s'?"), name);
        avalue = alertpanel(_("Delete newsgroup"), message,
                            _("Yes"), _("+No"), NULL);
        g_free(message);
@@ -2248,6 +2367,7 @@ static void folderview_rm_news_server_cb(FolderView *folderview, guint action,
 {
        GtkCTree *ctree = GTK_CTREE(folderview->ctree);
        FolderItem *item;
+       gchar *name, *name_;
        gchar *message;
        AlertValue avalue;
 
@@ -2259,8 +2379,10 @@ static void folderview_rm_news_server_cb(FolderView *folderview, guint action,
        g_return_if_fail(item->folder->type == F_NEWS);
        g_return_if_fail(item->folder->account != NULL);
 
-       message = g_strdup_printf(_("Really delete news account `%s'?"),
-                                 item->folder->name);
+       name_ = trim_string(item->folder->name, 32);
+       Xstrdup_a(name, name_, return);
+       g_free(name_);
+       message = g_strdup_printf(_("Really delete news account `%s'?"), name);
        avalue = alertpanel(_("Delete news account"), message,
                            _("Yes"), _("+No"), NULL);
        g_free(message);
@@ -2289,6 +2411,28 @@ static void folderview_search_cb(FolderView *folderview, guint action,
        summary_search(folderview->summaryview);
 }
 
+static void folderview_property_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);
+       g_return_if_fail(item != NULL);
+       g_return_if_fail(item->folder != NULL);
+
+#if CLAWS
+       prefs_folder_item_create(folderview, item);
+#else
+       /*
+        * CLAWS: wait till Hiro has completed his stuff
+        */
+       prefs_folder_item_open(item);
+#endif 
+}
+
 static gboolean folderview_drag_motion_cb(GtkWidget      *widget,
                                          GdkDragContext *context,
                                          gint            x,
@@ -2387,6 +2531,20 @@ static void folderview_drag_received_cb(GtkWidget        *widget,
                gtk_drag_finish(drag_context, FALSE, FALSE, time);
 }
 
+static gint folderview_clist_compare(GtkCList *clist,
+                                    gconstpointer ptr1, gconstpointer ptr2)
+{
+       FolderItem *item1 = ((GtkCListRow *)ptr1)->data;
+       FolderItem *item2 = ((GtkCListRow *)ptr2)->data;
+
+       if (!item1->name)
+               return (item2->name != NULL);
+       if (!item2->name)
+               return -1;
+
+       return g_strcasecmp(item1->name, item2->name);
+}
+
 static gint folderview_compare_name(gconstpointer a, gconstpointer b)
 {
        const FolderItem *item = a;
@@ -2423,22 +2581,7 @@ static void folderview_processing_cb(FolderView *folderview, guint action,
        g_return_if_fail(item != NULL);
        g_return_if_fail(item->folder != NULL);
 
-       prefs_filtering_open(item);
-}
-
-static void folderview_property_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);
-       g_return_if_fail(item != NULL);
-       g_return_if_fail(item->folder != NULL);
-
-       prefs_folder_item_create(folderview, item);
+       prefs_filtering_open(item, NULL, NULL);
 }
 
 void folderview_set_target_folder_color(gint color_op) 
@@ -2457,3 +2600,9 @@ void folderview_set_target_folder_color(gint color_op)
                }
        }
 }
+
+void folderview_reflect_prefs_pixmap_theme(FolderView *folderview)
+{
+       folderview_init(folderview);
+       folderview_set_all();
+}