2005-10-04 [colin] 1.9.15cvs9
[claws.git] / src / folder.c
index bbb0e69f739343f6213f44cea16a0ea6ab5adacb..ccb93f747ae2afdc49a9442bdddfc93fb17e3008 100644 (file)
@@ -14,7 +14,7 @@
  *
  * 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.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
 #ifdef HAVE_CONFIG_H
@@ -295,6 +295,10 @@ FolderItem *folder_item_new(Folder *folder, const gchar *name, const gchar *path
        item->apply_sub = FALSE;
        item->mark_queue = NULL;
        item->data = NULL;
+       item->parent_stype = -1;
+
+       item->sort_key = SORT_BY_DATE;
+       item->sort_type = SORT_ASCENDING;
 
        item->prefs = folder_item_prefs_new();
 
@@ -312,11 +316,32 @@ void folder_item_append(FolderItem *parent, FolderItem *item)
        g_node_append(parent->node, item->node);
 }
 
-static gboolean folder_item_remove_func(GNode *node, gpointer data)
+void folder_item_remove(FolderItem *item)
 {
-       FolderItem *item = FOLDER_ITEM(node->data);
+       GNode *node, *start_node;
        FolderUpdateData hookdata;
 
+       g_return_if_fail(item != NULL);
+       g_return_if_fail(item->folder != NULL);
+       g_return_if_fail(item->folder->node != NULL);
+
+       start_node = node = item->node;
+       
+       node = item->folder->node;
+       
+       node = g_node_find(node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
+       node = node->children;
+
+       /* remove my children */
+       while (node != NULL) {
+               if (node && node->data) {
+                       FolderItem *sub_item = (FolderItem*) node->data;
+                       node = node->next;
+                       folder_item_remove(sub_item);
+               }
+       }
+       
+       /* remove myself */
        if (item->cache != NULL) {
                msgcache_destroy(item->cache);
                item->cache = NULL;
@@ -327,25 +352,13 @@ static gboolean folder_item_remove_func(GNode *node, gpointer data)
        hookdata.item = item;
        hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
 
-       folder_item_destroy(item);
-
-       return FALSE;
-}
-
-void folder_item_remove(FolderItem *item)
-{
-       GNode *node;
-
-       g_return_if_fail(item != NULL);
-       g_return_if_fail(item->folder != NULL);
-       g_return_if_fail(item->node != NULL);
-       node = item->node;
+       node = start_node;
 
        if (item->folder->node == node)
                item->folder->node = NULL;
 
-       g_node_traverse(node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
-                       folder_item_remove_func, NULL);
+       folder_item_destroy(item);
+
        g_node_destroy(node);
 }
 
@@ -1415,18 +1428,18 @@ gchar *folder_item_get_path(FolderItem *item)
 
 void folder_item_set_default_flags(FolderItem *dest, MsgFlags *flags)
 {
-       if (!(dest->stype == F_OUTBOX ||
-             dest->stype == F_QUEUE  ||
-             dest->stype == F_DRAFT  ||
-             dest->stype == F_TRASH)) {
+       if (!(folder_has_parent_of_type(dest, F_OUTBOX) ||
+             folder_has_parent_of_type(dest, F_QUEUE)  ||
+             folder_has_parent_of_type(dest, F_DRAFT) ||
+             folder_has_parent_of_type(dest, F_TRASH))) {
                flags->perm_flags = MSG_NEW|MSG_UNREAD;
        } else {
                flags->perm_flags = 0;
        }
        if (FOLDER_TYPE(dest->folder) == F_MH) {
-               if (dest->stype == F_QUEUE) {
+               if (folder_has_parent_of_type(dest, F_QUEUE)) {
                        MSG_SET_TMP_FLAGS(*flags, MSG_QUEUED);
-               } else if (dest->stype == F_DRAFT) {
+               } else if (folder_has_parent_of_type(dest, F_DRAFT)) {
                        MSG_SET_TMP_FLAGS(*flags, MSG_DRAFT);
                }
        }
@@ -1448,16 +1461,17 @@ static gint folder_sort_folder_list(gconstpointer a, gconstpointer b)
        return (gint_a - gint_b);
 }
 
-gint folder_item_open(FolderItem *item)
+void folder_item_process_open(FolderItem *item)
 {
        gchar *buf;
-       g_return_val_if_fail(item->no_select == FALSE, -1);
-
+       if (item == NULL)
+               return;
        if((item->folder->klass->scan_required != NULL) &&
           (item->folder->klass->scan_required(item->folder, item))) {
                folder_item_scan_full(item, TRUE);
+       } else {
+               folder_item_syncronize_flags(item);
        }
-       folder_item_syncronize_flags(item);
        
        /* Processing */
        buf = g_strdup_printf(_("Processing (%s)...\n"), 
@@ -1467,9 +1481,15 @@ gint folder_item_open(FolderItem *item)
        
        folder_item_apply_processing(item);
 
-       item->opened = TRUE;
-
        debug_print("done.\n");
+       return; 
+}
+
+gint folder_item_open(FolderItem *item)
+{
+       g_return_val_if_fail(item->no_select == FALSE, -1);
+
+       item->opened = TRUE;
 
        return 0;
 }
@@ -1584,9 +1604,8 @@ static gint syncronize_flags(FolderItem *item, MsgInfoList *msglist)
                                continue;
                        
                        if (msginfo->flags.perm_flags != permflags) {
-                               procmsg_msginfo_set_flags(msginfo,
-                                       permflags & ~msginfo->flags.perm_flags, 0);
-                               procmsg_msginfo_unset_flags(msginfo,
+                               procmsg_msginfo_change_flags(msginfo,
+                                       permflags & ~msginfo->flags.perm_flags, 0,
                                        ~permflags & msginfo->flags.perm_flags, 0);
                        }
                }
@@ -1771,12 +1790,16 @@ gint folder_item_scan_full(FolderItem *item, gboolean filtering)
        g_slist_free(folder_list);
 
        if (new_list != NULL) {
+               GSList *tmp_list = NULL;
                newmsg_list = get_msginfos(item, new_list);
                g_slist_free(new_list);
+               tmp_list = g_slist_concat(g_slist_copy(exists_list), g_slist_copy(newmsg_list));
+               syncronize_flags(item, tmp_list);
+               g_slist_free(tmp_list);
+       } else {
+               syncronize_flags(item, exists_list);
        }
 
-       syncronize_flags(item, exists_list);
-
        folder_item_update_freeze();
        if (newmsg_list != NULL) {
                GSList *elem;
@@ -1818,13 +1841,12 @@ gint folder_item_scan_full(FolderItem *item, gboolean filtering)
                if (MSG_IS_IGNORE_THREAD(msginfo->flags) && (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
                        procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
                if (!MSG_IS_IGNORE_THREAD(msginfo->flags) && procmsg_msg_has_flagged_parent(msginfo, MSG_IGNORE_THREAD)) {
-                       procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
-                       procmsg_msginfo_set_flags(msginfo, MSG_IGNORE_THREAD, 0);
+                       procmsg_msginfo_change_flags(msginfo, MSG_IGNORE_THREAD, 0, MSG_NEW | MSG_UNREAD, 0);
                }
-               if ((item->stype == F_OUTBOX ||
-                    item->stype == F_QUEUE  ||
-                    item->stype == F_DRAFT  ||
-                    item->stype == F_TRASH) &&
+               if ((folder_has_parent_of_type(item, F_OUTBOX) ||
+                    folder_has_parent_of_type(item, F_QUEUE)  ||
+                    folder_has_parent_of_type(item, F_DRAFT)  ||
+                    folder_has_parent_of_type(item, F_TRASH)) &&
                    (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)))
                        procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
                if (MSG_IS_NEW(msginfo->flags))
@@ -1919,6 +1941,8 @@ gint folder_item_syncronize_flags(FolderItem *item)
        for (cur = msglist; cur != NULL; cur = g_slist_next(cur))
                procmsg_msginfo_free((MsgInfo *) cur->data);
        
+       g_slist_free(msglist);
+
        return ret;
 }
 
@@ -1964,6 +1988,20 @@ void folder_item_free_cache(FolderItem *item)
        item->cache = NULL;
 }
 
+void folder_clean_cache_memory_force(void)
+{
+       int old_cache_max_mem_usage = prefs_common.cache_max_mem_usage;
+       int old_cache_min_keep_time = prefs_common.cache_min_keep_time;
+
+       prefs_common.cache_max_mem_usage = 0;
+       prefs_common.cache_min_keep_time = 0;
+
+       folder_clean_cache_memory();
+
+       prefs_common.cache_max_mem_usage = old_cache_max_mem_usage;
+       prefs_common.cache_min_keep_time = old_cache_min_keep_time;
+}
+
 void folder_clean_cache_memory(void)
 {
        gint memusage = 0;
@@ -2178,8 +2216,8 @@ gchar *folder_item_fetch_msg(FolderItem *item, gint num)
                if ((msginfo != NULL) && !MSG_IS_SCANNED(msginfo->flags)) {
                        MimeInfo *mimeinfo;
 
-                       if (msginfo->folder->stype != F_QUEUE && 
-                           msginfo->folder->stype != F_DRAFT)
+                       if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) && 
+                           !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
                                mimeinfo = procmime_scan_file(msgfile);
                        else
                                mimeinfo = procmime_scan_queue_file(msgfile);
@@ -2219,8 +2257,8 @@ gchar *folder_item_fetch_msg_full(FolderItem *item, gint num, gboolean headers,
                if ((msginfo != NULL) && !MSG_IS_SCANNED(msginfo->flags)) {
                        MimeInfo *mimeinfo;
 
-                       if (msginfo->folder->stype != F_QUEUE && 
-                           msginfo->folder->stype != F_DRAFT)
+                       if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
+                           !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
                                mimeinfo = procmime_scan_file(msgfile);
                        else
                                mimeinfo = procmime_scan_queue_file(msgfile);
@@ -2301,10 +2339,11 @@ static gint folder_item_get_msg_num_by_file(FolderItem *dest, const gchar *file)
        gint msgnum = 0;
        gchar buf[BUFFSIZE];
 
-       if ((fp = fopen(file, "rb")) == NULL)
+       if ((fp = g_fopen(file, "rb")) == NULL)
                return 0;
 
-       if ((dest->stype == F_QUEUE) || (dest->stype == F_DRAFT))
+       if ((folder_has_parent_of_type(dest, F_QUEUE)) || 
+           (folder_has_parent_of_type(dest, F_DRAFT)))
                while (fgets(buf, sizeof(buf), fp) != NULL)
                        if (buf[0] == '\r' || buf[0] == '\n') break;
 
@@ -2343,10 +2382,10 @@ static void copy_msginfo_flags(MsgInfo *source, MsgInfo *dest)
        }
 
        /* remove new, unread and deleted in special folders */
-       if (dest->folder->stype == F_OUTBOX ||
-           dest->folder->stype == F_QUEUE  ||
-           dest->folder->stype == F_DRAFT  ||
-           dest->folder->stype == F_TRASH)
+       if (folder_has_parent_of_type(dest->folder, F_OUTBOX) || 
+           folder_has_parent_of_type(dest->folder, F_QUEUE) || 
+           folder_has_parent_of_type(dest->folder, F_DRAFT) || 
+           folder_has_parent_of_type(dest->folder, F_TRASH))
                perm_flags &= ~(MSG_NEW | MSG_UNREAD | MSG_DELETED);
 
        /* set ignore flag of ignored parent exists */
@@ -2357,13 +2396,12 @@ static void copy_msginfo_flags(MsgInfo *source, MsgInfo *dest)
        tmp_flags &= ~(MSG_MOVE | MSG_COPY);
 
        /* unset flags that are set but should not */
-       procmsg_msginfo_unset_flags(dest,
-                                   dest->flags.perm_flags & ~perm_flags,
-                                   dest->flags.tmp_flags  & ~tmp_flags);
-       /* set new flags */
-       procmsg_msginfo_set_flags(dest,
+       /* and set new flags */
+       procmsg_msginfo_change_flags(dest,
                                  ~dest->flags.perm_flags & perm_flags,
-                                 ~dest->flags.tmp_flags  & tmp_flags);
+                                 ~dest->flags.tmp_flags  & tmp_flags,
+                                  dest->flags.perm_flags & ~perm_flags,
+                                  dest->flags.tmp_flags  & ~tmp_flags);
 }
 
 static void add_msginfo_to_cache(FolderItem *item, MsgInfo *newmsginfo, MsgInfo *flagsource)
@@ -2492,7 +2530,7 @@ gint folder_item_add_msgs(FolderItem *dest, GSList *file_list,
                                lastnum = num;
 
                        if (num >= 0 && remove_source) {
-                               if (unlink(fileinfo->file) < 0)
+                               if (g_unlink(fileinfo->file) < 0)
                                        FILE_OP_ERROR(fileinfo->file, "unlink");
                        }
 
@@ -2523,8 +2561,6 @@ FolderItem *folder_item_move_recursive(FolderItem *src, FolderItem *dest)
        GNode *srcnode;
        gchar *old_id, *new_id;
 
-       mlist = folder_item_get_msg_list(src);
-       
        /* move messages */
        debug_print("Moving %s to %s\n", src->path, dest->path);
        new_item = folder_create_folder(dest, src->name);
@@ -2539,8 +2575,13 @@ FolderItem *folder_item_move_recursive(FolderItem *src, FolderItem *dest)
        /* move messages */
        log_message(_("Moving %s to %s...\n"), 
                        src->name, new_item->path);
-       if (mlist != NULL)
+
+       mlist = folder_item_get_msg_list(src);
+       
+       if (mlist != NULL) {
                folder_item_move_msgs(new_item, mlist);
+               procmsg_msg_list_free(mlist);
+       }
        
        /*copy prefs*/
        folder_item_prefs_copy_prefs(src, new_item);
@@ -2562,8 +2603,9 @@ 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) == NULL) {
                                return NULL;
+                       }
                }
        }
        old_id = folder_item_get_identifier(src);
@@ -2579,7 +2621,7 @@ FolderItem *folder_item_move_recursive(FolderItem *src, FolderItem *dest)
                prefs_filtering_rename_path(old_id, new_id);
        g_free(old_id);
        g_free(new_id);
-
+       
        return new_item;
 }
 
@@ -2790,7 +2832,11 @@ static gint do_copy_msgs(FolderItem *dest, GSList *msglist, gboolean remove_sour
        }
 
        g_relation_destroy(relation);
-       return lastnum;
+       if (not_moved != NULL) {
+               g_slist_free(not_moved);
+               return -1;
+       } else
+               return lastnum;
 }
 
 /**
@@ -3150,7 +3196,7 @@ static gpointer folder_item_to_xml(gpointer nodedata, gpointer data)
        else
                tag = folder_item_get_xml(item->folder, item);
 
-       return xml_node_new(tag, NULL);;
+       return xml_node_new(tag, NULL);
 }
 
 static GNode *folder_get_xml_node(Folder *folder)
@@ -3527,6 +3573,50 @@ void folder_item_update_thaw(void)
        }
 }
 
+static void folder_item_synchronise_func(FolderItem *item, gpointer data)
+{
+       Folder *folder = (Folder *)data;
+       if (folder == NULL || item->folder == folder) {
+               if(item->prefs->offlinesync && item->folder->klass->synchronise)
+                       item->folder->klass->synchronise(item);
+       }
+}
+
+void folder_synchronise(Folder *folder)
+{
+       folder_func_to_all_folders(folder_item_synchronise_func, folder);
+}
+
+typedef struct _WantSyncData {
+       Folder *folder;
+       gboolean want_sync;
+} WantSyncData;
+
+static void folder_item_want_synchronise_func(FolderItem *item, gpointer data)
+{
+       WantSyncData *want_sync_data = (WantSyncData *)data;
+       
+       if (want_sync_data->folder == NULL || item->folder == want_sync_data->folder) {
+               if(item->prefs->offlinesync && item->folder->klass->synchronise)
+                       want_sync_data->want_sync = TRUE;
+               else
+                       want_sync_data->want_sync = FALSE;
+       }
+}
+
+gboolean folder_want_synchronise(Folder *folder)
+{
+       WantSyncData *want_sync_data = g_new0(WantSyncData, 1);
+       gboolean result;
+       want_sync_data->folder = folder;
+       
+       folder_func_to_all_folders(folder_item_want_synchronise_func, want_sync_data);
+       result = want_sync_data->want_sync;
+       g_free(want_sync_data);
+       debug_print("Folder %s wants sync: %d\n", folder->name, result);
+       return result;
+}
+
 void folder_item_set_batch (FolderItem *item, gboolean batch)
 {
        if (item->folder->klass->set_batch) {
@@ -3534,4 +3624,73 @@ void folder_item_set_batch (FolderItem *item, gboolean batch)
        }
 }
 
+gboolean folder_has_parent_of_type(FolderItem *item, 
+                                         SpecialFolderItemType type) 
+{
+       FolderItem *cur = item;
+
+       /* if we already know it, make it short */
+       if (item->parent_stype != -1) {
+               return (item->parent_stype == type);
+       }
+       
+       /* if we don't, find the type from the first possible parent,
+        * and set our parent type to be faster next time */
+       while (cur) {
+               if (cur->stype == type || cur->parent_stype == type) {
+                       item->parent_stype = type;
+                       return TRUE;
+               }
+               cur = folder_item_parent(cur);
+       }
+       
+       /* if we didn't match what was asked, we didn't return. If our
+        * parent type is unknown, we may as well find it now to be faster
+        * later. */
+       if (item->parent_stype == -1) {
+               cur = item;
+               while (cur) {
+                       /* 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, 0, cur->path);
+                               item->parent_stype = F_NORMAL;
+                               break;
+                       }
+                       /* ah, we know this parent's parent's type, we may as 
+                        * 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, cur->parent_stype, 
+                                       cur->path);
+                               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, cur->stype, cur->path);
+                               cur->parent_stype = cur->stype;
+                               item->parent_stype = cur->stype;
+                               break;
+                       }
+                       /* if we didn't find anything, go up once more */
+                       cur = folder_item_parent(cur);
+               }
+               /* 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->parent_stype = F_NORMAL;
+               }
+       }
+       return FALSE;
+}
+
 #undef PUT_ESCAPE_STR