2007-01-17 [colin] 2.7.1cvs16
[claws.git] / src / folder.c
index 2ff4c77130400eced1d1c34db8d6506632adbab8..7ecf5631e7dc5ca490190cc2c685147bd1ade689 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
+ * Copyright (C) 1999-2007 Hiroyuki Yamamoto and 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
@@ -861,6 +861,44 @@ void folder_scan_tree(Folder *folder, gboolean rebuild)
        folder_write_list();
 }
 
+gboolean folder_restore_prefs_func(GNode *node, gpointer data)
+{
+       GHashTable *pptable = (GHashTable *)data;
+       FolderItem *item = (FolderItem *)node->data;
+       
+       folder_item_restore_persist_prefs(item, pptable);
+
+       return FALSE;
+}
+
+void folder_fast_scan_tree(Folder *folder)
+{
+       GHashTable *pptable;
+       FolderUpdateData hookdata;
+       Folder *old_folder = folder;
+
+       if (!folder->klass->scan_tree)
+               return;
+       
+       pptable = folder_persist_prefs_new(folder);
+
+       if (folder->klass->scan_tree(folder) < 0) {
+               return;
+       } 
+
+       hookdata.folder = folder;
+       hookdata.update_flags = FOLDER_TREE_CHANGED;
+       hookdata.item = NULL;
+       hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
+
+       g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1, folder_restore_prefs_func, pptable);
+       folder_persist_prefs_free(pptable);
+
+       prefs_matcher_read_config();
+
+       folder_write_list();
+}
+
 FolderItem *folder_create_folder(FolderItem *parent, const gchar *name)
 {
        FolderItem *new_item;
@@ -1142,14 +1180,20 @@ FolderItem *folder_find_item_from_path(const gchar *path)
 {
        Folder *folder;
        gpointer d[2];
-
-       folder = folder_get_default_folder();
+       GList *list = folder_get_list();
+       
+       folder = list ? list->data:NULL;
+       
        g_return_val_if_fail(folder != NULL, NULL);
 
        d[0] = (gpointer)path;
        d[1] = NULL;
-       g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
+       while (d[1] == NULL && list) {
+               folder = FOLDER(list->data);
+               g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
                        folder_item_find_func, d);
+               list = list->next;
+       }
        return d[1];
 }
 
@@ -1202,7 +1246,9 @@ gchar *folder_item_get_identifier(FolderItem *item)
        gchar *folder_id = NULL;
 
        g_return_val_if_fail(item != NULL, NULL);
-       g_return_val_if_fail(item->path != NULL, NULL);
+
+       if (item->path == NULL)
+               return NULL;
 
        folder_id = folder_get_identifier(item->folder);
        id = g_strconcat(folder_id, "/", item->path, NULL);
@@ -1569,7 +1615,6 @@ void folder_item_process_open (FolderItem *item,
        /* Processing */
        buf = g_strdup_printf(_("Processing (%s)...\n"), 
                              item->path ? item->path : item->name);
-       debug_print("%s\n", buf);
        g_free(buf);
 
        if (before_proc_func)
@@ -1580,14 +1625,13 @@ void folder_item_process_open (FolderItem *item,
        if (after_proc_func)
                after_proc_func(data);
 
-       debug_print("done.\n");
        item->processing_pending = FALSE;
        return; 
 }
 
 gint folder_item_open(FolderItem *item)
 {
-       START_TIMING("folder_item_open"); 
+       START_TIMING(""); 
        if (item->no_select)
                return -1;
 
@@ -1960,8 +2004,8 @@ gint folder_item_scan_full(FolderItem *item, gboolean filtering)
                                        &to_filter, &unfiltered, 
                                        TRUE);
                        
+                       filtering_move_and_copy_msgs(newmsg_list);
                        if (to_filter != NULL) {
-                               filtering_move_and_copy_msgs(to_filter);
                                for (elem = to_filter; elem; elem = g_slist_next(elem)) {
                                        MsgInfo *msginfo = (MsgInfo *)elem->data;
                                        procmsg_msginfo_free(msginfo);
@@ -2220,7 +2264,7 @@ void folder_clean_cache_memory(FolderItem *protected_item)
 void folder_item_read_cache(FolderItem *item)
 {
        gchar *cache_file, *mark_file;
-       START_TIMING("folder_item_read_cache");
+       START_TIMING("");
        g_return_if_fail(item != NULL);
 
        if (item->path != NULL) {
@@ -2303,8 +2347,9 @@ void folder_item_write_cache(FolderItem *item)
         }
 
        if (!need_scan && FOLDER_TYPE(item->folder) == F_MH) {
-               if (item->mtime == last_mtime)
-                       item->mtime = time(NULL);
+               if (item->mtime == last_mtime) {
+                       mh_set_mtime(item);
+               }
        }
 
        g_free(cache_file);
@@ -2772,8 +2817,8 @@ gint folder_item_add_msgs(FolderItem *dest, GSList *file_list,
 
         return lastnum;
 }
-               
-FolderItem *folder_item_move_recursive(FolderItem *src, FolderItem *dest
+
+static FolderItem *folder_item_move_recursive(FolderItem *src, FolderItem *dest, gboolean copy
 {
        GSList *mlist;
        FolderItem *new_item;
@@ -2782,7 +2827,7 @@ FolderItem *folder_item_move_recursive(FolderItem *src, FolderItem *dest)
        gchar *old_id, *new_id;
 
        /* move messages */
-       debug_print("Moving %s to %s\n", src->path, dest->path);
+       debug_print("%s %s to %s\n", copy?"Copying":"Moving", src->path, dest->path);
        new_item = folder_create_folder(dest, src->name);
        if (new_item == NULL) {
                printf("Can't create folder\n");
@@ -2793,18 +2838,27 @@ FolderItem *folder_item_move_recursive(FolderItem *src, FolderItem *dest)
                new_item->folder = dest->folder;
 
        /* move messages */
-       log_message(_("Moving %s to %s...\n"), 
+       log_message(copy ?_("Copying %s to %s...\n"):_("Moving %s to %s...\n"), 
                        src->name, new_item->path);
 
        mlist = folder_item_get_msg_list(src);
        
        if (mlist != NULL) {
-               folder_item_move_msgs(new_item, mlist);
+               if (copy)
+                       folder_item_copy_msgs(new_item, mlist);
+               else
+                       folder_item_move_msgs(new_item, mlist);
                procmsg_msg_list_free(mlist);
        }
        
        /*copy prefs*/
        folder_item_prefs_copy_prefs(src, new_item);
+       
+       /* copy internal data */
+       if (src->folder->klass == new_item->folder->klass &&
+           src->folder->klass->copy_private_data != NULL)
+               src->folder->klass->copy_private_data(src->folder,
+                                       src, new_item);
        new_item->collapsed = src->collapsed;
        new_item->thread_collapsed = src->thread_collapsed;
        new_item->threaded  = src->threaded;
@@ -2823,23 +2877,25 @@ FolderItem *folder_item_move_recursive(FolderItem *src, FolderItem *dest)
                if (srcnode && srcnode->data) {
                        next_item = (FolderItem*) srcnode->data;
                        srcnode = srcnode->next;
-                       if (folder_item_move_recursive(next_item, new_item) == NULL) {
+                       if (folder_item_move_recursive(next_item, new_item, copy) == NULL) {
                                return NULL;
                        }
                }
        }
        old_id = folder_item_get_identifier(src);
        new_id = folder_item_get_identifier(new_item);
-       debug_print("updating rules : %s => %s\n", old_id, new_id);
 
        /* if src supports removing, otherwise only copy folder */
-       if (src->folder->klass->remove_folder != NULL 
+       if (src->folder->klass->remove_folder != NULL && !copy) 
                src->folder->klass->remove_folder(src->folder, src);
        folder_write_list();
 
-       if (old_id != NULL && new_id != NULL) {
-               prefs_filtering_rename_path(old_id, new_id);
-               account_rename_path(old_id, new_id);
+       if (!copy) {
+               debug_print("updating rules : %s => %s\n", old_id, new_id);
+               if (old_id != NULL && new_id != NULL) {
+                       prefs_filtering_rename_path(old_id, new_id);
+                       account_rename_path(old_id, new_id);
+               }
        }
        g_free(old_id);
        g_free(new_id);
@@ -2847,12 +2903,12 @@ FolderItem *folder_item_move_recursive(FolderItem *src, FolderItem *dest)
        return new_item;
 }
 
-gint folder_item_move_to(FolderItem *src, FolderItem *dest, FolderItem **new_item)
+gint folder_item_move_to(FolderItem *src, FolderItem *dest, FolderItem **new_item, gboolean copy)
 {
        FolderItem *tmp = folder_item_parent(dest);
        gchar * src_identifier, * dst_identifier;
        gchar * phys_srcpath, * phys_dstpath, *tmppath;
-       
+
        while (tmp) {
                if (tmp == src) {
                        return F_MOVE_FAILED_DEST_IS_CHILD;
@@ -2874,7 +2930,7 @@ gint folder_item_move_to(FolderItem *src, FolderItem *dest, FolderItem **new_ite
                return F_MOVE_FAILED;
        }
 
-       if (src->folder != dest->folder) {
+       if (src->folder != dest->folder && !copy) {
                return F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX;
        }
 
@@ -2894,7 +2950,7 @@ gint folder_item_move_to(FolderItem *src, FolderItem *dest, FolderItem **new_ite
                return F_MOVE_FAILED_DEST_IS_PARENT;
        }
        debug_print("moving \"%s\" to \"%s\"\n", phys_srcpath, phys_dstpath);
-       if ((tmp = folder_item_move_recursive(src, dest)) == NULL) {
+       if ((tmp = folder_item_move_recursive(src, dest, copy)) == NULL) {
                return F_MOVE_FAILED;
        }
        
@@ -3034,8 +3090,15 @@ static gint do_copy_msgs(FolderItem *dest, GSList *msglist, gboolean remove_sour
                        item = msginfo->folder;
 
                        tuples = g_relation_select(relation, msginfo, 0);
-                       num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
-                       g_tuples_destroy(tuples);
+                       if (tuples) {
+                               if (tuples->len)
+                                       num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
+                               else
+                                       num = 0;
+                               g_tuples_destroy(tuples);
+                       } else {
+                               num = -1;
+                       }
 
                        if (g_slist_find(not_moved, msginfo))
                                continue;
@@ -3069,9 +3132,14 @@ static gint do_copy_msgs(FolderItem *dest, GSList *msglist, gboolean remove_sour
                 GTuples *tuples;
 
                 tuples = g_relation_select(relation, msginfo, 0);
-                num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
-                g_tuples_destroy(tuples);
-
+               if (tuples->len > 0) {
+                       num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
+                       g_tuples_destroy(tuples);
+               } else {
+                       num = 0;
+                       if (tuples)
+                               g_tuples_destroy(tuples);
+               }
                statusbar_progress_all(curmsg++,total, 100);
                if (curmsg % 100 == 0)
                        GTK_EVENTS_FLUSH();
@@ -3750,6 +3818,7 @@ void folder_item_apply_processing(FolderItem *item)
        GSList *processing_list;
        GSList *mlist, *cur;
        guint total = 0, curmsg = 0;
+       gint last_apply_per_account;
 
        g_return_if_fail(item != NULL);
 
@@ -3762,11 +3831,16 @@ void folder_item_apply_processing(FolderItem *item)
        &&  !post_global_processing)
                return;
 
+       debug_print("processing %s\n", item->name);
        folder_item_update_freeze();
 
        mlist = folder_item_get_msg_list(item);
        total = g_slist_length(mlist);
        statusbar_print_all(_("Processing messages..."));
+
+       last_apply_per_account = prefs_common.apply_per_account_filtering_rules;
+       prefs_common.apply_per_account_filtering_rules = FILTERING_ACCOUNT_RULES_SKIP;
+
        for (cur = mlist ; cur != NULL ; cur = cur->next) {
                MsgInfo * msginfo;
 
@@ -3788,6 +3862,8 @@ void folder_item_apply_processing(FolderItem *item)
                filter_message_by_msginfo(post_global_processing, msginfo, NULL);
                 
        }
+       prefs_common.apply_per_account_filtering_rules = last_apply_per_account;
+
        if (pre_global_processing || processing_list
            || post_global_processing)
                filtering_move_and_copy_msgs(mlist);
@@ -3961,10 +4037,6 @@ gboolean folder_has_parent_of_type(FolderItem *item,
                        /* here's an exception: Inbox subfolders are normal. */
                        if (item->parent_stype == -1 && cur->stype == F_INBOX 
                        && item != cur) {
-                               debug_print("set item %s parent type to %d "
-                                       "even if %s is F_INBOX\n",
-                                       item->path ? item->path : "(null)",
-                                       0, cur->path);
                                item->parent_stype = F_NORMAL;
                                break;
                        }
@@ -3972,21 +4044,11 @@ gboolean folder_has_parent_of_type(FolderItem *item,
                         * well copy it instead of going up the full way */
                        if (cur->parent_stype != -1) {
                                item->parent_stype = cur->parent_stype;
-                               debug_print("set item %s parent type to %d "
-                                       "from %s's parent type\n",
-                                       item->path ? item->path : "(null)",
-                                       cur->parent_stype ? cur->parent_stype : 0, 
-                                       cur->path ? cur->path : "(null)");
                                break;
                        }
                        /* we found a parent that has a special type. That's 
                         * our parent type. */
                        if (cur->stype != F_NORMAL) {
-                               debug_print("set item %s parent type to %d "
-                                       "from %s's type\n",
-                                       item->path ? item->path : "(null)",
-                                       cur->stype ? cur->stype : 0, 
-                                       cur->path ? cur->path : "(null)");
                                cur->parent_stype = cur->stype;
                                item->parent_stype = cur->stype;
                                break;
@@ -3997,8 +4059,6 @@ gboolean folder_has_parent_of_type(FolderItem *item,
                /* as we still didn't find anything, our parents must all be 
                 * normal. */
                if (item->parent_stype == -1) {
-                       debug_print("set item %s to 0 from default\n", 
-                               item->path ? item->path : "(null)");
                        item->parent_stype = F_NORMAL;
                }
        }
@@ -4019,4 +4079,17 @@ gboolean folder_subscribe (const gchar *uri)
        return FALSE;
 
 }
+
+gboolean folder_get_sort_type          (Folder         *folder,
+                                        FolderSortKey  *sort_key,
+                                        FolderSortType *sort_type)
+{
+       if (!folder || !sort_key || !sort_type)
+               return FALSE;
+       if (folder->klass->get_sort_type == NULL)
+               return FALSE;
+       folder->klass->get_sort_type(folder, sort_key, sort_type); 
+       return TRUE;
+}
+
 #undef PUT_ESCAPE_STR