2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2024 the Claws Mail team and Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "claws-features.h"
27 #include <glib/gi18n.h>
30 #include <sys/types.h>
35 #include "alertpanel.h"
45 #include "prefs_gtk.h"
47 #include "filtering.h"
48 #include "procheader.h"
51 #include "folder_item_prefs.h"
52 #include "remotefolder.h"
53 #include "partial_download.h"
54 #include "statusbar.h"
61 #include "prefs_common.h"
62 #include "prefs_migration.h"
63 #include "file-utils.h"
65 /* Dependecies to be removed ?! */
66 #include "prefs_account.h"
68 /* Define possible missing constants for Windows. */
80 static GList
*folder_list
= NULL
;
81 static GSList
*class_list
= NULL
;
82 static GSList
*folder_unloaded_list
= NULL
;
84 void folder_init (Folder
*folder
,
87 static gchar
*folder_item_get_cache_file (FolderItem
*item
);
88 static gchar
*folder_item_get_mark_file (FolderItem
*item
);
89 static gchar
*folder_item_get_tags_file (FolderItem
*item
);
90 static GNode
*folder_get_xml_node (Folder
*folder
);
91 static Folder
*folder_get_from_xml (GNode
*node
);
92 static void folder_update_op_count_rec (GNode
*node
);
95 static void folder_get_persist_prefs_recursive
96 (GNode
*node
, GHashTable
*pptable
);
97 static gboolean
persist_prefs_free (gpointer key
, gpointer val
, gpointer data
);
98 static void folder_item_read_cache (FolderItem
*item
);
99 gint
folder_item_scan_full (FolderItem
*item
, gboolean filtering
);
100 static void folder_item_update_with_msg (FolderItem
*item
, FolderItemUpdateFlags update_flags
,
102 static GHashTable
*folder_persist_prefs_new (Folder
*folder
);
103 static void folder_persist_prefs_free (GHashTable
*pptable
);
104 static void folder_item_restore_persist_prefs (FolderItem
*item
, GHashTable
*pptable
);
106 void folder_system_init(void)
108 folder_register_class(mh_get_class());
109 folder_register_class(imap_get_class());
110 folder_register_class(news_get_class());
113 static GSList
*folder_get_class_list(void)
118 void folder_register_class(FolderClass
*klass
)
120 GSList
*xmllist
, *cur
;
122 debug_print("registering folder class %s\n", klass
->idstr
);
124 class_list
= g_slist_append(class_list
, klass
);
126 xmllist
= g_slist_copy(folder_unloaded_list
);
127 for (cur
= xmllist
; cur
!= NULL
; cur
= g_slist_next(cur
)) {
128 GNode
*node
= (GNode
*) cur
->data
;
129 XMLNode
*xmlnode
= (XMLNode
*) node
->data
;
130 GList
*cur
= xmlnode
->tag
->attr
;
132 for (; cur
!= NULL
; cur
= g_list_next(cur
)) {
133 XMLAttr
*attr
= (XMLAttr
*) cur
->data
;
135 if (!attr
|| !attr
->name
|| !attr
->value
) continue;
136 if (!strcmp(attr
->name
, "type") && !strcmp(attr
->value
, klass
->idstr
)) {
139 folder
= folder_get_from_xml(node
);
142 folder_unloaded_list
= g_slist_remove(folder_unloaded_list
, node
);
150 g_slist_free(xmllist
);
153 void folder_unregister_class(FolderClass
*klass
)
155 GList
*folderlist
, *cur
;
157 debug_print("unregistering folder class %s\n", klass
->idstr
);
159 class_list
= g_slist_remove(class_list
, klass
);
161 folderlist
= g_list_copy(folder_get_list());
162 for (cur
= folderlist
; cur
!= NULL
; cur
= g_list_next(cur
)) {
163 Folder
*folder
= (Folder
*) cur
->data
;
165 if (folder
->klass
== klass
) {
166 GNode
*xmlnode
= folder_get_xml_node(folder
);
167 folder_unloaded_list
= g_slist_append(folder_unloaded_list
, xmlnode
);
168 folder_destroy(folder
);
171 g_list_free(folderlist
);
173 if (klass
->prefs_pages
)
174 g_slist_free(klass
->prefs_pages
);
177 Folder
*folder_new(FolderClass
*klass
, const gchar
*name
, const gchar
*path
)
179 Folder
*folder
= NULL
;
182 cm_return_val_if_fail(klass
!= NULL
, NULL
);
184 name
= name
? name
: path
;
185 folder
= klass
->new_folder(name
, path
);
187 /* Create root folder item */
188 item
= folder_item_new(folder
, name
, NULL
);
192 item
->folder
= folder
;
193 folder
->node
= item
->node
;
199 void folder_init(Folder
*folder
, const gchar
*name
)
201 cm_return_if_fail(folder
!= NULL
);
203 folder_set_name(folder
, name
);
205 /* Init folder data */
206 folder
->account
= NULL
;
208 folder
->inbox
= NULL
;
209 folder
->outbox
= NULL
;
210 folder
->draft
= NULL
;
211 folder
->queue
= NULL
;
212 folder
->trash
= NULL
;
215 static void reset_parent_type(FolderItem
*item
, gpointer data
) {
216 item
->parent_stype
= -1;
219 void folder_item_change_type(FolderItem
*item
, SpecialFolderItemType newtype
)
221 Folder
*folder
= NULL
;
222 FolderUpdateData hookdata
;
227 folder
= item
->folder
;
228 /* unset previous root of newtype */
231 folder_item_change_type(folder
->inbox
, F_NORMAL
);
232 folder
->inbox
= item
;
235 folder_item_change_type(folder
->outbox
, F_NORMAL
);
236 folder
->outbox
= item
;
239 folder_item_change_type(folder
->queue
, F_NORMAL
);
240 folder
->queue
= item
;
243 folder_item_change_type(folder
->draft
, F_NORMAL
);
244 folder
->draft
= item
;
247 folder_item_change_type(folder
->trash
, F_NORMAL
);
248 folder
->trash
= item
;
254 /* set new type for current folder and sons */
255 item
->stype
= newtype
;
256 folder_func_to_all_folders(reset_parent_type
, NULL
);
258 hookdata
.folder
= folder
;
259 hookdata
.update_flags
= FOLDER_TREE_CHANGED
;
260 hookdata
.item
= NULL
;
261 hookdata
.item2
= NULL
;
262 hooks_invoke(FOLDER_UPDATE_HOOKLIST
, &hookdata
);
265 void folder_destroy(Folder
*folder
)
267 cm_return_if_fail(folder
!= NULL
);
268 cm_return_if_fail(folder
->klass
->destroy_folder
!= NULL
);
270 folder_remove(folder
);
272 folder_tree_destroy(folder
);
274 folder
->klass
->destroy_folder(folder
);
276 g_free(folder
->name
);
280 void folder_set_xml(Folder
*folder
, XMLTag
*tag
)
283 FolderItem
*rootitem
= NULL
;
285 if ((folder
->node
!= NULL
) && (folder
->node
->data
!= NULL
))
286 rootitem
= (FolderItem
*) folder
->node
->data
;
288 for (cur
= tag
->attr
; cur
!= NULL
; cur
= g_list_next(cur
)) {
289 XMLAttr
*attr
= (XMLAttr
*) cur
->data
;
291 if (!attr
|| !attr
->name
|| !attr
->value
) continue;
292 if (!strcmp(attr
->name
, "name")) {
293 g_free(folder
->name
);
294 folder
->name
= g_strdup(attr
->value
);
295 if (rootitem
!= NULL
) {
296 g_free(rootitem
->name
);
297 rootitem
->name
= g_strdup(attr
->value
);
299 } else if (!strcmp(attr
->name
, "account_id")) {
300 PrefsAccount
*account
;
302 account
= account_find_from_id(atoi(attr
->value
));
304 g_warning("account_id: %s not found", attr
->value
);
306 folder
->account
= account
;
307 account
->folder
= folder
;
309 } else if (!strcmp(attr
->name
, "collapsed")) {
310 if (rootitem
!= NULL
)
311 rootitem
->collapsed
= *attr
->value
== '1' ? TRUE
: FALSE
;
312 } else if (!strcmp(attr
->name
, "sort")) {
313 folder
->sort
= atoi(attr
->value
);
318 XMLTag
*folder_get_xml(Folder
*folder
)
322 tag
= xml_tag_new("folder");
325 xml_tag_add_attr(tag
, xml_attr_new("name", folder
->name
));
327 xml_tag_add_attr(tag
, xml_attr_new_int("account_id", folder
->account
->account_id
));
328 if (folder
->node
&& folder
->node
->data
) {
329 FolderItem
*rootitem
= (FolderItem
*) folder
->node
->data
;
331 xml_tag_add_attr(tag
, xml_attr_new("collapsed", rootitem
->collapsed
? "1" : "0"));
333 xml_tag_add_attr(tag
, xml_attr_new_int("sort", folder
->sort
));
338 FolderItem
*folder_item_new(Folder
*folder
, const gchar
*name
, const gchar
*path
)
340 FolderItem
*item
= NULL
;
342 cm_return_val_if_fail(folder
!= NULL
, NULL
);
344 if (folder
->klass
->item_new
) {
345 item
= folder
->klass
->item_new(folder
);
347 item
= g_new0(FolderItem
, 1);
350 cm_return_val_if_fail(item
!= NULL
, NULL
);
352 item
->stype
= F_NORMAL
;
354 if(!g_utf8_validate(name
, -1, NULL
)) {
355 item
->name
= g_malloc(strlen(name
)*2+1);
356 conv_localetodisp(item
->name
, strlen(name
)*2+1, name
);
358 item
->name
= g_strdup(name
);
361 item
->path
= g_strdup(path
);
364 item
->unread_msgs
= 0;
365 item
->unreadmarked_msgs
= 0;
366 item
->marked_msgs
= 0;
367 item
->total_msgs
= 0;
368 item
->replied_msgs
= 0;
369 item
->forwarded_msgs
= 0;
370 item
->locked_msgs
= 0;
371 item
->ignored_msgs
= 0;
372 item
->watched_msgs
= 0;
376 item
->no_sub
= FALSE
;
377 item
->no_select
= FALSE
;
378 item
->collapsed
= FALSE
;
379 item
->thread_collapsed
= prefs_common
.folder_default_thread_collapsed
;
380 item
->threaded
= prefs_common
.folder_default_thread
;
381 item
->hide_read_threads
= prefs_common
.folder_default_hide_read_threads
;
382 item
->hide_read_msgs
= prefs_common
.folder_default_hide_read_msgs
;
383 item
->hide_del_msgs
= prefs_common
.folder_default_hide_del_msgs
;
384 item
->ret_rcpt
= FALSE
;
385 item
->opened
= FALSE
;
386 item
->node
= g_node_new(item
);
388 item
->account
= NULL
;
389 item
->apply_sub
= FALSE
;
390 item
->mark_queue
= NULL
;
392 item
->parent_stype
= -1;
394 item
->sort_key
= prefs_common
.default_sort_key
;
395 item
->sort_type
= prefs_common
.default_sort_type
;
397 item
->prefs
= folder_item_prefs_new();
402 void folder_item_append(FolderItem
*parent
, FolderItem
*item
)
404 cm_return_if_fail(parent
!= NULL
);
405 cm_return_if_fail(parent
->folder
!= NULL
);
406 cm_return_if_fail(parent
->node
!= NULL
);
407 cm_return_if_fail(item
!= NULL
);
409 item
->folder
= parent
->folder
;
410 g_node_append(parent
->node
, item
->node
);
413 void folder_item_remove(FolderItem
*item
)
415 GNode
*node
, *start_node
;
416 FolderUpdateData hookdata
;
417 gchar
*tags_file
= NULL
;
419 cm_return_if_fail(item
!= NULL
);
420 cm_return_if_fail(item
->folder
!= NULL
);
421 cm_return_if_fail(item
->folder
->node
!= NULL
);
423 start_node
= item
->node
;
425 node
= item
->folder
->node
;
427 node
= g_node_find(node
, G_PRE_ORDER
, G_TRAVERSE_ALL
, item
);
428 node
= node
->children
;
430 /* remove my children */
431 while (node
!= NULL
) {
432 if (node
&& node
->data
) {
433 FolderItem
*sub_item
= (FolderItem
*) node
->data
;
435 folder_item_remove(sub_item
);
440 if (item
->cache
!= NULL
) {
441 msgcache_destroy(item
->cache
);
445 tags_file
= folder_item_get_tags_file(item
);
449 claws_unlink(tags_file
);
451 tags_dir
= g_path_get_dirname(tags_file
);
458 hookdata
.folder
= item
->folder
;
459 hookdata
.update_flags
= FOLDER_TREE_CHANGED
| FOLDER_REMOVE_FOLDERITEM
;
460 hookdata
.item
= item
;
461 hookdata
.item2
= NULL
;
462 hooks_invoke(FOLDER_UPDATE_HOOKLIST
, &hookdata
);
466 if (item
->folder
->node
== node
)
467 item
->folder
->node
= NULL
;
469 folder_item_destroy(item
);
471 g_node_destroy(node
);
474 void folder_item_remove_children(FolderItem
*item
)
478 cm_return_if_fail(item
!= NULL
);
479 cm_return_if_fail(item
->folder
!= NULL
);
480 cm_return_if_fail(item
->node
!= NULL
);
482 node
= item
->node
->children
;
483 while (node
!= NULL
) {
485 folder_item_remove(FOLDER_ITEM(node
->data
));
490 void folder_item_destroy(FolderItem
*item
)
494 cm_return_if_fail(item
!= NULL
);
496 folder
= item
->folder
;
498 if (folder
->inbox
== item
)
499 folder
->inbox
= NULL
;
500 else if (folder
->outbox
== item
)
501 folder
->outbox
= NULL
;
502 else if (folder
->draft
== item
)
503 folder
->draft
= NULL
;
504 else if (folder
->queue
== item
)
505 folder
->queue
= NULL
;
506 else if (folder
->trash
== item
)
507 folder
->trash
= NULL
;
511 folder_item_free_cache(item
, TRUE
);
513 folder_item_prefs_free(item
->prefs
);
517 if (item
->folder
!= NULL
) {
518 if(item
->folder
->klass
->item_destroy
) {
519 item
->folder
->klass
->item_destroy(item
->folder
, item
);
526 FolderItem
*folder_item_parent(FolderItem
*item
)
528 cm_return_val_if_fail(item
!= NULL
, NULL
);
529 cm_return_val_if_fail(item
->node
!= NULL
, NULL
);
531 if (item
->node
->parent
== NULL
)
533 return (FolderItem
*) item
->node
->parent
->data
;
536 void folder_item_set_xml(Folder
*folder
, FolderItem
*item
, XMLTag
*tag
)
540 for (cur
= tag
->attr
; cur
!= NULL
; cur
= g_list_next(cur
)) {
541 XMLAttr
*attr
= (XMLAttr
*) cur
->data
;
543 if (!attr
|| !attr
->name
|| !attr
->value
) continue;
544 if (!strcmp(attr
->name
, "type")) {
545 if (!g_ascii_strcasecmp(attr
->value
, "normal"))
546 item
->stype
= F_NORMAL
;
547 else if (!g_ascii_strcasecmp(attr
->value
, "inbox"))
548 item
->stype
= F_INBOX
;
549 else if (!g_ascii_strcasecmp(attr
->value
, "outbox"))
550 item
->stype
= F_OUTBOX
;
551 else if (!g_ascii_strcasecmp(attr
->value
, "draft"))
552 item
->stype
= F_DRAFT
;
553 else if (!g_ascii_strcasecmp(attr
->value
, "queue"))
554 item
->stype
= F_QUEUE
;
555 else if (!g_ascii_strcasecmp(attr
->value
, "trash"))
556 item
->stype
= F_TRASH
;
557 } else if (!strcmp(attr
->name
, "name")) {
559 item
->name
= g_strdup(attr
->value
);
560 } else if (!strcmp(attr
->name
, "path")) {
562 item
->path
= g_strdup(attr
->value
);
563 } else if (!strcmp(attr
->name
, "mtime"))
564 item
->mtime
= strtoul(attr
->value
, NULL
, 10);
565 else if (!strcmp(attr
->name
, "new"))
566 item
->new_msgs
= atoi(attr
->value
);
567 else if (!strcmp(attr
->name
, "unread"))
568 item
->unread_msgs
= atoi(attr
->value
);
569 else if (!strcmp(attr
->name
, "unreadmarked"))
570 item
->unreadmarked_msgs
= atoi(attr
->value
);
571 else if (!strcmp(attr
->name
, "marked"))
572 item
->marked_msgs
= atoi(attr
->value
);
573 else if (!strcmp(attr
->name
, "replied"))
574 item
->replied_msgs
= atoi(attr
->value
);
575 else if (!strcmp(attr
->name
, "forwarded"))
576 item
->forwarded_msgs
= atoi(attr
->value
);
577 else if (!strcmp(attr
->name
, "locked"))
578 item
->locked_msgs
= atoi(attr
->value
);
579 else if (!strcmp(attr
->name
, "ignored"))
580 item
->ignored_msgs
= atoi(attr
->value
);
581 else if (!strcmp(attr
->name
, "watched"))
582 item
->watched_msgs
= atoi(attr
->value
);
583 else if (!strcmp(attr
->name
, "order"))
584 item
->order
= atoi(attr
->value
);
585 else if (!strcmp(attr
->name
, "total"))
586 item
->total_msgs
= atoi(attr
->value
);
587 else if (!strcmp(attr
->name
, "no_sub"))
588 item
->no_sub
= *attr
->value
== '1' ? TRUE
: FALSE
;
589 else if (!strcmp(attr
->name
, "no_select"))
590 item
->no_select
= *attr
->value
== '1' ? TRUE
: FALSE
;
591 else if (!strcmp(attr
->name
, "collapsed"))
592 item
->collapsed
= *attr
->value
== '1' ? TRUE
: FALSE
;
593 else if (!strcmp(attr
->name
, "thread_collapsed"))
594 item
->thread_collapsed
= *attr
->value
== '1' ? TRUE
: FALSE
;
595 else if (!strcmp(attr
->name
, "threaded"))
596 item
->threaded
= *attr
->value
== '1' ? TRUE
: FALSE
;
597 else if (!strcmp(attr
->name
, "hidereadmsgs"))
598 item
->hide_read_msgs
= *attr
->value
== '1' ? TRUE
: FALSE
;
599 else if (!strcmp(attr
->name
, "hidedelmsgs"))
600 item
->hide_del_msgs
= *attr
->value
== '1' ? TRUE
: FALSE
;
601 else if (!strcmp(attr
->name
, "hidereadthreads"))
602 item
->hide_read_threads
= *attr
->value
== '1' ? TRUE
: FALSE
;
603 else if (!strcmp(attr
->name
, "reqretrcpt"))
604 item
->ret_rcpt
= *attr
->value
== '1' ? TRUE
: FALSE
;
605 else if (!strcmp(attr
->name
, "sort_key")) {
606 if (!strcmp(attr
->value
, "none"))
607 item
->sort_key
= SORT_BY_NONE
;
608 else if (!strcmp(attr
->value
, "number"))
609 item
->sort_key
= SORT_BY_NUMBER
;
610 else if (!strcmp(attr
->value
, "size"))
611 item
->sort_key
= SORT_BY_SIZE
;
612 else if (!strcmp(attr
->value
, "date"))
613 item
->sort_key
= SORT_BY_DATE
;
614 else if (!strcmp(attr
->value
, "from"))
615 item
->sort_key
= SORT_BY_FROM
;
616 else if (!strcmp(attr
->value
, "subject"))
617 item
->sort_key
= SORT_BY_SUBJECT
;
618 else if (!strcmp(attr
->value
, "score"))
619 item
->sort_key
= SORT_BY_SCORE
;
620 else if (!strcmp(attr
->value
, "label"))
621 item
->sort_key
= SORT_BY_LABEL
;
622 else if (!strcmp(attr
->value
, "mark"))
623 item
->sort_key
= SORT_BY_MARK
;
624 else if (!strcmp(attr
->value
, "unread"))
625 item
->sort_key
= SORT_BY_STATUS
;
626 else if (!strcmp(attr
->value
, "mime"))
627 item
->sort_key
= SORT_BY_MIME
;
628 else if (!strcmp(attr
->value
, "to"))
629 item
->sort_key
= SORT_BY_TO
;
630 else if (!strcmp(attr
->value
, "locked"))
631 item
->sort_key
= SORT_BY_LOCKED
;
632 else if (!strcmp(attr
->value
, "tags"))
633 item
->sort_key
= SORT_BY_TAGS
;
634 else if (!strcmp(attr
->value
, "thread_date"))
635 item
->sort_key
= SORT_BY_THREAD_DATE
;
636 } else if (!strcmp(attr
->name
, "sort_type")) {
637 if (!strcmp(attr
->value
, "ascending"))
638 item
->sort_type
= SORT_ASCENDING
;
640 item
->sort_type
= SORT_DESCENDING
;
641 } else if (!strcmp(attr
->name
, "account_id")) {
642 PrefsAccount
*account
;
644 account
= account_find_from_id(atoi(attr
->value
));
646 g_warning("account_id: %s not found", attr
->value
);
648 item
->account
= account
;
649 } else if (!strcmp(attr
->name
, "apply_sub")) {
650 item
->apply_sub
= *attr
->value
== '1' ? TRUE
: FALSE
;
651 } else if (!strcmp(attr
->name
, "last_seen")) {
652 if (!claws_crashed())
653 item
->last_seen
= atoi(attr
->value
);
658 /* options without meaning in drafts */
659 if (item
->stype
== F_DRAFT
)
660 item
->hide_read_msgs
=
661 item
->hide_del_msgs
=
662 item
->hide_read_threads
= FALSE
;
665 XMLTag
*folder_item_get_xml(Folder
*folder
, FolderItem
*item
)
667 static gchar
*folder_item_stype_str
[] = {"normal", "inbox", "outbox",
668 "draft", "queue", "trash"};
669 static gchar
*sort_key_str
[] = {"none", "number", "size", "date",
670 "from", "subject", "score", "label",
671 "mark", "unread", "mime", "to",
672 "locked", "tags", "thread_date" };
676 tag
= xml_tag_new("folderitem");
678 xml_tag_add_attr(tag
, xml_attr_new("type", folder_item_stype_str
[item
->stype
]));
680 xml_tag_add_attr(tag
, xml_attr_new("name", item
->name
));
682 xml_tag_add_attr(tag
, xml_attr_new("path", item
->path
));
684 xml_tag_add_attr(tag
, xml_attr_new("no_sub", "1"));
686 xml_tag_add_attr(tag
, xml_attr_new("no_select", "1"));
687 xml_tag_add_attr(tag
, xml_attr_new("collapsed", item
->collapsed
&& item
->node
->children
? "1" : "0"));
688 xml_tag_add_attr(tag
, xml_attr_new("thread_collapsed", item
->thread_collapsed
? "1" : "0"));
689 xml_tag_add_attr(tag
, xml_attr_new("threaded", item
->threaded
? "1" : "0"));
690 xml_tag_add_attr(tag
, xml_attr_new("hidereadmsgs", item
->hide_read_msgs
? "1" : "0"));
691 xml_tag_add_attr(tag
, xml_attr_new("hidedelmsgs", item
->hide_del_msgs
? "1" : "0"));
692 xml_tag_add_attr(tag
, xml_attr_new("hidereadthreads", item
->hide_read_threads
? "1" : "0"));
694 xml_tag_add_attr(tag
, xml_attr_new("reqretrcpt", "1"));
696 if (item
->sort_key
!= SORT_BY_NONE
) {
697 xml_tag_add_attr(tag
, xml_attr_new("sort_key", sort_key_str
[item
->sort_key
]));
698 xml_tag_add_attr(tag
, xml_attr_new("sort_type", item
->sort_type
== SORT_ASCENDING
? "ascending" : "descending"));
701 value
= g_strdup_printf("%ld", (unsigned long int) item
->mtime
);
702 xml_tag_add_attr(tag
, xml_attr_new("mtime", value
));
704 xml_tag_add_attr(tag
, xml_attr_new_int("new", item
->new_msgs
));
705 xml_tag_add_attr(tag
, xml_attr_new_int("unread", item
->unread_msgs
));
706 xml_tag_add_attr(tag
, xml_attr_new_int("unreadmarked", item
->unreadmarked_msgs
));
707 xml_tag_add_attr(tag
, xml_attr_new_int("marked", item
->marked_msgs
));
708 xml_tag_add_attr(tag
, xml_attr_new_int("total", item
->total_msgs
));
709 xml_tag_add_attr(tag
, xml_attr_new_int("replied", item
->replied_msgs
));
710 xml_tag_add_attr(tag
, xml_attr_new_int("forwarded", item
->forwarded_msgs
));
711 xml_tag_add_attr(tag
, xml_attr_new_int("locked", item
->locked_msgs
));
712 xml_tag_add_attr(tag
, xml_attr_new_int("ignore", item
->ignored_msgs
));
713 xml_tag_add_attr(tag
, xml_attr_new_int("watched", item
->watched_msgs
));
714 xml_tag_add_attr(tag
, xml_attr_new_int("order", item
->order
));
717 xml_tag_add_attr(tag
, xml_attr_new_int("account_id", item
->account
->account_id
));
719 xml_tag_add_attr(tag
, xml_attr_new("apply_sub", "1"));
721 xml_tag_add_attr(tag
, xml_attr_new_int("last_seen", item
->last_seen
));
726 void folder_set_ui_func(Folder
*folder
, FolderUIFunc func
, gpointer data
)
728 cm_return_if_fail(folder
!= NULL
);
730 folder
->ui_func
= func
;
731 folder
->ui_func_data
= data
;
734 void folder_set_name(Folder
*folder
, const gchar
*name
)
736 cm_return_if_fail(folder
!= NULL
);
738 g_free(folder
->name
);
739 folder
->name
= name
? g_strdup(name
) : NULL
;
740 if (folder
->node
&& folder
->node
->data
) {
741 FolderItem
*item
= (FolderItem
*)folder
->node
->data
;
744 item
->name
= name
? g_strdup(name
) : NULL
;
748 void folder_set_sort(Folder
*folder
, guint sort
)
750 cm_return_if_fail(folder
!= NULL
);
752 if (folder
->sort
!= sort
) {
753 folder_remove(folder
);
759 static gboolean
folder_tree_destroy_func(GNode
*node
, gpointer data
) {
760 FolderItem
*item
= (FolderItem
*) node
->data
;
762 folder_item_destroy(item
);
766 void folder_tree_destroy(Folder
*folder
)
770 cm_return_if_fail(folder
!= NULL
);
774 prefs_filtering_clear_folder(folder
);
777 g_node_traverse(node
, G_POST_ORDER
, G_TRAVERSE_ALL
, -1,
778 folder_tree_destroy_func
, NULL
);
779 g_node_destroy(node
);
784 void folder_add(Folder
*folder
)
789 FolderUpdateData hookdata
;
791 cm_return_if_fail(folder
!= NULL
);
793 if ((FOLDER_TYPE(folder
) == F_IMAP
||
794 FOLDER_TYPE(folder
) == F_NEWS
) &&
795 folder
->account
== NULL
) {
799 for (i
= 0, cur
= folder_list
; cur
!= NULL
; cur
= cur
->next
, i
++) {
800 cur_folder
= FOLDER(cur
->data
);
801 if (cur_folder
->sort
< folder
->sort
)
805 folder_list
= g_list_insert(folder_list
, folder
, i
);
807 hookdata
.folder
= folder
;
808 hookdata
.update_flags
= FOLDER_ADD_FOLDER
;
809 hookdata
.item
= NULL
;
810 hookdata
.item2
= NULL
;
811 hooks_invoke(FOLDER_UPDATE_HOOKLIST
, &hookdata
);
814 void folder_remove(Folder
*folder
)
816 FolderUpdateData hookdata
;
818 cm_return_if_fail(folder
!= NULL
);
820 folder_list
= g_list_remove(folder_list
, folder
);
822 hookdata
.folder
= folder
;
823 hookdata
.update_flags
= FOLDER_REMOVE_FOLDER
;
824 hookdata
.item
= NULL
;
825 hookdata
.item2
= NULL
;
826 hooks_invoke(FOLDER_UPDATE_HOOKLIST
, &hookdata
);
829 GList
*folder_get_list(void)
834 gint
folder_read_list(void)
840 gint config_version
= -1;
842 path
= folder_get_list_path();
843 if (!is_file_exist(path
)) return -1;
844 node
= xml_parse_file(path
);
845 if (!node
) return -1;
847 xmlnode
= node
->data
;
848 if (g_strcmp0(xmlnode
->tag
->tag
, "folderlist") != 0) {
849 g_warning("wrong folder list");
854 cur
= node
->children
;
855 while (cur
!= NULL
) {
858 folder
= folder_get_from_xml(cur
);
862 folder_unloaded_list
= g_slist_append(folder_unloaded_list
,
863 (gpointer
) xml_copy_tree(cur
));
867 for (list
= xmlnode
->tag
->attr
; list
!= NULL
; list
= list
->next
) {
868 XMLAttr
*attr
= list
->data
;
870 if (!attr
|| !attr
->name
|| !attr
->value
) continue;
871 if (!strcmp(attr
->name
, "config_version")) {
872 config_version
= atoi(attr
->value
);
873 debug_print("Found folderlist config_version %d\n", config_version
);
879 if (prefs_update_config_version_folderlist(config_version
) < 0) {
880 debug_print("Folderlist configuration file version upgrade failed\n");
884 if (folder_list
|| folder_unloaded_list
)
890 void folder_write_list(void)
901 path
= folder_get_list_path();
902 if ((pfile
= prefs_write_open(path
)) == NULL
) return;
904 if (xml_file_put_xml_decl(pfile
->fp
) < 0) {
905 prefs_file_close_revert(pfile
);
906 g_warning("failed to start write folder list");
909 tag
= xml_tag_new("folderlist");
910 xml_tag_add_attr(tag
, xml_attr_new_int("config_version",
911 CLAWS_CONFIG_VERSION
));
913 xmlnode
= xml_node_new(tag
, NULL
);
915 rootnode
= g_node_new(xmlnode
);
917 for (list
= folder_list
; list
!= NULL
; list
= list
->next
) {
921 node
= folder_get_xml_node(folder
);
923 g_node_append(rootnode
, node
);
926 for (slist
= folder_unloaded_list
; slist
!= NULL
; slist
= g_slist_next(slist
)) {
927 GNode
*node
= (GNode
*) slist
->data
;
929 g_node_append(rootnode
, (gpointer
) xml_copy_tree(node
));
932 if (xml_write_tree(rootnode
, pfile
->fp
) < 0) {
933 prefs_file_close_revert(pfile
);
934 g_warning("failed to write folder list");
935 } else if (prefs_file_close(pfile
) < 0) {
936 g_warning("failed to write folder list");
938 xml_free_tree(rootnode
);
941 static gboolean
folder_scan_tree_func(GNode
*node
, gpointer data
)
943 GHashTable
*pptable
= (GHashTable
*)data
;
944 FolderItem
*item
= (FolderItem
*)node
->data
;
946 folder_item_restore_persist_prefs(item
, pptable
);
947 folder_item_scan_full(item
, FALSE
);
952 static gboolean
folder_restore_prefs_func(GNode
*node
, gpointer data
)
954 GHashTable
*pptable
= (GHashTable
*)data
;
955 FolderItem
*item
= (FolderItem
*)node
->data
;
957 folder_item_restore_persist_prefs(item
, pptable
);
962 void folder_scan_tree(Folder
*folder
, gboolean rebuild
)
965 FolderUpdateData hookdata
;
966 Folder
*old_folder
= folder
;
968 if (!folder
->klass
->scan_tree
)
971 pptable
= folder_persist_prefs_new(folder
);
974 folder_remove(folder
);
976 if (folder
->klass
->scan_tree(folder
) < 0) {
978 folder_add(old_folder
);
983 hookdata
.folder
= folder
;
984 hookdata
.update_flags
= FOLDER_TREE_CHANGED
;
985 hookdata
.item
= NULL
;
986 hookdata
.item2
= NULL
;
987 hooks_invoke(FOLDER_UPDATE_HOOKLIST
, &hookdata
);
990 g_node_traverse(folder
->node
, G_POST_ORDER
, G_TRAVERSE_ALL
, -1, folder_scan_tree_func
, pptable
);
992 g_node_traverse(folder
->node
, G_POST_ORDER
, G_TRAVERSE_ALL
, -1, folder_restore_prefs_func
, pptable
);
994 folder_persist_prefs_free(pptable
);
996 prefs_matcher_read_config();
1001 FolderItem
*folder_create_folder(FolderItem
*parent
, const gchar
*name
)
1003 FolderItem
*new_item
;
1005 cm_return_val_if_fail(parent
!= NULL
, NULL
);
1007 new_item
= parent
->folder
->klass
->create_folder(parent
->folder
, parent
, name
);
1009 FolderUpdateData hookdata
;
1011 new_item
->cache
= msgcache_new();
1012 new_item
->cache_dirty
= TRUE
;
1013 new_item
->mark_dirty
= TRUE
;
1014 new_item
->tags_dirty
= TRUE
;
1016 hookdata
.folder
= new_item
->folder
;
1017 hookdata
.update_flags
= FOLDER_TREE_CHANGED
| FOLDER_ADD_FOLDERITEM
;
1018 hookdata
.item
= new_item
;
1019 hookdata
.item2
= NULL
;
1020 hooks_invoke(FOLDER_UPDATE_HOOKLIST
, &hookdata
);
1026 gint
folder_item_rename(FolderItem
*item
, gchar
*newname
)
1030 cm_return_val_if_fail(item
!= NULL
, -1);
1031 cm_return_val_if_fail(newname
!= NULL
, -1);
1033 retval
= item
->folder
->klass
->rename_folder(item
->folder
, item
, newname
);
1036 FolderItemUpdateData hookdata
;
1037 FolderUpdateData hookdata2
;
1039 hookdata
.item
= item
;
1040 hookdata
.update_flags
= F_ITEM_UPDATE_NAME
;
1041 hookdata
.msg
= NULL
;
1042 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST
, &hookdata
);
1044 hookdata2
.folder
= item
->folder
;
1045 hookdata2
.item
= item
;
1046 hookdata2
.item2
= NULL
;
1047 hookdata2
.update_flags
= FOLDER_RENAME_FOLDERITEM
;
1048 hooks_invoke(FOLDER_UPDATE_HOOKLIST
, &hookdata2
);
1054 struct TotalMsgCount
1058 guint unreadmarked_msgs
;
1062 guint forwarded_msgs
;
1068 struct FuncToAllFoldersData
1070 FolderItemFunc function
;
1074 static gboolean
folder_func_to_all_folders_func(GNode
*node
, gpointer data
)
1077 struct FuncToAllFoldersData
*function_data
= (struct FuncToAllFoldersData
*) data
;
1079 cm_return_val_if_fail(node
->data
!= NULL
, FALSE
);
1081 item
= FOLDER_ITEM(node
->data
);
1082 cm_return_val_if_fail(item
!= NULL
, FALSE
);
1084 function_data
->function(item
, function_data
->data
);
1089 void folder_func_to_all_folders(FolderItemFunc function
, gpointer data
)
1093 struct FuncToAllFoldersData function_data
;
1095 function_data
.function
= function
;
1096 function_data
.data
= data
;
1098 for (list
= folder_list
; list
!= NULL
; list
= list
->next
) {
1099 folder
= FOLDER(list
->data
);
1101 g_node_traverse(folder
->node
, G_PRE_ORDER
,
1103 folder_func_to_all_folders_func
,
1108 static void folder_count_total_msgs_func(FolderItem
*item
, gpointer data
)
1110 struct TotalMsgCount
*count
= (struct TotalMsgCount
*)data
;
1112 count
->new_msgs
+= item
->new_msgs
;
1113 count
->unread_msgs
+= item
->unread_msgs
;
1114 count
->unreadmarked_msgs
+= item
->unreadmarked_msgs
;
1115 count
->marked_msgs
+= item
->marked_msgs
;
1116 count
->total_msgs
+= item
->total_msgs
;
1117 count
->replied_msgs
+= item
->replied_msgs
;
1118 count
->forwarded_msgs
+= item
->forwarded_msgs
;
1119 count
->locked_msgs
+= item
->locked_msgs
;
1120 count
->ignored_msgs
+= item
->ignored_msgs
;
1121 count
->watched_msgs
+= item
->watched_msgs
;
1124 struct TotalMsgStatus
1132 static gboolean
folder_get_status_full_all_func(GNode
*node
, gpointer data
)
1135 struct TotalMsgStatus
*status
= (struct TotalMsgStatus
*)data
;
1138 cm_return_val_if_fail(node
->data
!= NULL
, FALSE
);
1140 item
= FOLDER_ITEM(node
->data
);
1142 if (!item
->path
) return FALSE
;
1144 status
->new += item
->new_msgs
;
1145 status
->unread
+= item
->unread_msgs
;
1146 status
->total
+= item
->total_msgs
;
1149 id
= folder_item_get_identifier(item
);
1150 g_string_append_printf(status
->str
, "%5d %5d %5d %s\n",
1151 item
->new_msgs
, item
->unread_msgs
,
1152 item
->total_msgs
, id
);
1159 static void folder_get_status_full_all(GString
*str
, guint
*new, guint
*unread
,
1164 struct TotalMsgStatus status
;
1166 status
.new = status
.unread
= status
.total
= 0;
1169 debug_print("Counting total number of messages...\n");
1171 for (list
= folder_list
; list
!= NULL
; list
= list
->next
) {
1172 folder
= FOLDER(list
->data
);
1174 g_node_traverse(folder
->node
, G_PRE_ORDER
,
1176 folder_get_status_full_all_func
,
1181 *unread
= status
.unread
;
1182 *total
= status
.total
;
1185 gchar
*folder_get_status(GPtrArray
*folders
, gboolean full
)
1187 guint
new, unread
, total
;
1191 new = unread
= total
= 0;
1193 str
= g_string_new(NULL
);
1196 for (i
= 0; i
< folders
->len
; i
++) {
1199 item
= g_ptr_array_index(folders
, i
);
1200 new += item
->new_msgs
;
1201 unread
+= item
->unread_msgs
;
1202 total
+= item
->total_msgs
;
1207 id
= folder_item_get_identifier(item
);
1208 g_string_append_printf(str
, "%5d %5d %5d %s\n",
1209 item
->new_msgs
, item
->unread_msgs
,
1210 item
->total_msgs
, id
);
1215 folder_get_status_full_all(full
? str
: NULL
,
1216 &new, &unread
, &total
);
1220 g_string_append_printf(str
, "%5d %5d %5d\n", new, unread
, total
);
1222 g_string_append_printf(str
, "%d %d %d\n", new, unread
, total
);
1224 return g_string_free(str
, FALSE
);
1227 void folder_count_total_msgs(guint
*new_msgs
, guint
*unread_msgs
,
1228 guint
*unreadmarked_msgs
, guint
*marked_msgs
,
1229 guint
*total_msgs
, guint
*replied_msgs
,
1230 guint
*forwarded_msgs
, guint
*locked_msgs
,
1231 guint
*ignored_msgs
, guint
*watched_msgs
)
1233 struct TotalMsgCount count
;
1235 count
.new_msgs
= count
.unread_msgs
= count
.unreadmarked_msgs
= 0;
1236 count
.total_msgs
= count
.replied_msgs
= count
.forwarded_msgs
= 0;
1237 count
.locked_msgs
= count
.ignored_msgs
= count
.watched_msgs
= 0;
1238 count
.marked_msgs
= 0;
1240 debug_print("Counting total number of messages...\n");
1242 folder_func_to_all_folders(folder_count_total_msgs_func
, &count
);
1244 *new_msgs
= count
.new_msgs
;
1245 *unread_msgs
= count
.unread_msgs
;
1246 *unreadmarked_msgs
= count
.unreadmarked_msgs
;
1247 *marked_msgs
= count
.marked_msgs
;
1248 *total_msgs
= count
.total_msgs
;
1249 *replied_msgs
= count
.replied_msgs
;
1250 *forwarded_msgs
= count
.forwarded_msgs
;
1251 *locked_msgs
= count
.locked_msgs
;
1252 *ignored_msgs
= count
.ignored_msgs
;
1253 *watched_msgs
= count
.watched_msgs
;
1256 Folder
*folder_find_from_path(const gchar
*path
)
1261 for (list
= folder_list
; list
!= NULL
; list
= list
->next
) {
1262 folder
= list
->data
;
1263 if ((FOLDER_TYPE(folder
) == F_MH
||
1264 FOLDER_TYPE(folder
) == F_MBOX
) &&
1265 !path_cmp(LOCAL_FOLDER(folder
)->rootpath
, path
))
1272 Folder
*folder_find_from_name(const gchar
*name
, FolderClass
*klass
)
1277 for (list
= folder_list
; list
!= NULL
; list
= list
->next
) {
1278 folder
= list
->data
;
1279 if (folder
->klass
== klass
&&
1280 g_strcmp0(name
, folder
->name
) == 0)
1287 static gboolean
folder_item_find_func(GNode
*node
, gpointer data
)
1289 FolderItem
*item
= node
->data
;
1291 const gchar
*path
= d
[0];
1293 if (path_cmp(path
, item
->path
) != 0)
1301 FolderItem
*folder_find_item_from_path(const gchar
*path
)
1305 GList
*list
= folder_get_list();
1307 folder
= list
? list
->data
:NULL
;
1309 cm_return_val_if_fail(folder
!= NULL
, NULL
);
1311 d
[0] = (gpointer
)path
;
1313 while (d
[1] == NULL
&& list
) {
1314 folder
= FOLDER(list
->data
);
1315 g_node_traverse(folder
->node
, G_PRE_ORDER
, G_TRAVERSE_ALL
, -1,
1316 folder_item_find_func
, d
);
1322 static gboolean
folder_item_find_func_real_path(GNode
*node
, gpointer data
)
1324 FolderItem
*item
= node
->data
;
1326 const gchar
*path
= d
[0];
1327 gchar
*tmp
= folder_item_get_path(item
);
1328 if (path_cmp(path
, tmp
) != 0) {
1338 FolderItem
*folder_find_item_from_real_path(const gchar
*path
)
1342 GList
*list
= folder_get_list();
1344 folder
= list
? list
->data
:NULL
;
1346 cm_return_val_if_fail(folder
!= NULL
, NULL
);
1348 d
[0] = (gpointer
)path
;
1350 while (d
[1] == NULL
&& list
) {
1351 folder
= FOLDER(list
->data
);
1352 g_node_traverse(folder
->node
, G_PRE_ORDER
, G_TRAVERSE_ALL
, -1,
1353 folder_item_find_func_real_path
, d
);
1359 FolderItem
*folder_find_child_item_by_name(FolderItem
*item
, const gchar
*name
)
1364 for (node
= item
->node
->children
; node
!= NULL
; node
= node
->next
) {
1365 child
= FOLDER_ITEM(node
->data
);
1366 if (g_strcmp0(child
->name
, name
) == 0) {
1374 FolderClass
*folder_get_class_from_string(const gchar
*str
)
1378 classlist
= folder_get_class_list();
1379 for (; classlist
!= NULL
; classlist
= g_slist_next(classlist
)) {
1380 FolderClass
*class = (FolderClass
*) classlist
->data
;
1381 if (g_ascii_strcasecmp(class->idstr
, str
) == 0)
1388 gchar
*folder_get_identifier(Folder
*folder
)
1392 cm_return_val_if_fail(folder
!= NULL
, NULL
);
1394 type_str
= folder
->klass
->idstr
;
1395 return g_strconcat("#", type_str
, "/", folder
->name
, NULL
);
1398 gchar
*folder_item_get_identifier(FolderItem
*item
)
1401 gchar
*folder_id
= NULL
;
1403 cm_return_val_if_fail(item
!= NULL
, NULL
);
1405 if (item
->path
== NULL
)
1408 folder_id
= folder_get_identifier(item
->folder
);
1409 id
= g_strconcat(folder_id
, "/", item
->path
, NULL
);
1415 Folder
*folder_find_from_identifier(const gchar
*identifier
)
1422 cm_return_val_if_fail(identifier
!= NULL
, NULL
);
1424 if (*identifier
!= '#')
1427 Xstrdup_a(str
, identifier
, return NULL
);
1429 p
= strchr(str
, '/');
1434 class = folder_get_class_from_string(&str
[1]);
1443 return folder_find_from_name(name
, class);
1446 FolderItem
*folder_find_item_from_identifier(const gchar
*identifier
)
1456 cm_return_val_if_fail(identifier
!= NULL
, NULL
);
1458 if (*identifier
!= '#')
1459 return folder_find_item_from_path(identifier
);
1461 Xstrdup_a(str
, identifier
, return NULL
);
1463 p
= strchr(str
, '/');
1465 return folder_find_item_from_path(identifier
);
1468 class = folder_get_class_from_string(&str
[1]);
1470 return folder_find_item_from_path(identifier
);
1475 return folder_find_item_from_path(identifier
);
1479 folder
= folder_find_from_name(name
, class);
1481 return folder_find_item_from_path(identifier
);
1485 d
[0] = (gpointer
)path
;
1487 g_node_traverse(folder
->node
, G_PRE_ORDER
, G_TRAVERSE_ALL
, -1,
1488 folder_item_find_func
, d
);
1492 /** Returns the FolderItem from a given identifier
1494 * The FolderItem is created if it doesn't already exist.
1495 * If creation failed, the function returns NULL.
1497 * Identifiers are of the form #type/Mailbox/FolderA/FolderB/FolderC
1499 FolderItem
*folder_get_item_from_identifier(const gchar
*identifier
)
1501 FolderItem
*item
, *last_parent
;
1503 gchar
*p1
, *p2
, *str
;
1506 gboolean created_something
= FALSE
;
1508 item
= folder_find_item_from_identifier(identifier
);
1512 /* trivial sanity check: need at least # and two slashes */
1513 len
= strlen(identifier
);
1517 /* make sure identifier ends with a slash */
1518 if(identifier
[len
-1] == G_DIR_SEPARATOR
) {
1519 Xstrdup_a(str
, identifier
, return NULL
);
1522 Xstrndup_a(str
, identifier
, len
+1, return NULL
);
1523 str
[len
] = G_DIR_SEPARATOR
;
1526 /* find folder class */
1527 p1
= strchr(str
, G_DIR_SEPARATOR
);
1531 class = folder_get_class_from_string(&str
[1]);
1534 *p1
= G_DIR_SEPARATOR
;
1537 /* find folder from class and name */
1538 p2
= strchr(p1
, G_DIR_SEPARATOR
);
1542 folder
= folder_find_from_name(p1
, class);
1545 *p2
= G_DIR_SEPARATOR
;
1549 /* Now, move forward and make sure all sections in the path exist */
1550 last_parent
= folder
->node
->data
;
1551 while((p1
= strchr(p1
, G_DIR_SEPARATOR
)) != NULL
) {
1553 item
= folder_find_item_from_identifier(str
);
1555 item
= folder_create_folder(last_parent
, p2
);
1558 debug_print("Created folder '%s'\n", str
);
1559 created_something
= TRUE
;
1560 if(prefs_common
.inherit_folder_props
&& (last_parent
!= item
->folder
->node
->data
)) {
1561 folder_item_prefs_copy_prefs(last_parent
, item
);
1565 *p1
= G_DIR_SEPARATOR
;
1570 if(created_something
)
1571 folder_write_list();
1578 * Get a displayable name for a FolderItem
1580 * \param item FolderItem for that a name should be created
1581 * \return Displayable name for item, returned string has to
1584 gchar
*folder_item_get_name(FolderItem
*item
)
1588 cm_return_val_if_fail(item
!= NULL
, g_strdup(""));
1590 switch (item
->stype
) {
1592 name
= g_strdup(!g_strcmp0(item
->name
, INBOX_DIR
) ? _("Inbox") :
1596 name
= g_strdup(!g_strcmp0(item
->name
, OUTBOX_DIR
) ? _("Sent") :
1600 name
= g_strdup(!g_strcmp0(item
->name
, QUEUE_DIR
) ? _("Queue") :
1604 name
= g_strdup(!g_strcmp0(item
->name
, TRASH_DIR
) ? _("Trash") :
1608 name
= g_strdup(!g_strcmp0(item
->name
, DRAFT_DIR
) ? _("Drafts") :
1617 * should probably be done by a virtual function,
1618 * the folder knows the ui string and how to abbrev
1620 if (folder_item_parent(item
) == NULL
) {
1621 name
= g_strconcat(item
->name
, " (", item
->folder
->klass
->uistr
, ")", NULL
);
1623 if (FOLDER_CLASS(item
->folder
) == news_get_class() &&
1624 item
->path
&& !g_strcmp0(item
->name
, item
->path
))
1625 name
= get_abbrev_newsgroup_name
1627 prefs_common
.ng_abbrev_len
);
1629 name
= g_strdup(item
->name
);
1634 name
= g_strdup("");
1639 gboolean
folder_have_mailbox (void)
1642 for (cur
= folder_list
; cur
!= NULL
; cur
= g_list_next(cur
)) {
1643 Folder
*folder
= FOLDER(cur
->data
);
1644 if (folder
->inbox
&& folder
->outbox
)
1650 FolderItem
*folder_get_default_inbox(void)
1654 for (flist
= folder_list
; flist
!= NULL
; flist
= g_list_next(flist
)) {
1655 Folder
* folder
= FOLDER(flist
->data
);
1659 if (folder
->inbox
== NULL
)
1661 if (folder
->klass
->type
== F_UNKNOWN
)
1664 return folder
->inbox
;
1670 FolderItem
*folder_get_default_inbox_for_class(FolderType type
)
1674 for (flist
= folder_list
; flist
!= NULL
; flist
= g_list_next(flist
)) {
1675 Folder
* folder
= FOLDER(flist
->data
);
1679 if (folder
->inbox
== NULL
)
1681 if (folder
->klass
->type
!= type
)
1684 return folder
->inbox
;
1690 FolderItem
*folder_get_default_outbox(void)
1694 for (flist
= folder_list
; flist
!= NULL
; flist
= g_list_next(flist
)) {
1695 Folder
* folder
= FOLDER(flist
->data
);
1699 if (folder
->outbox
== NULL
)
1701 if (folder
->klass
->type
== F_UNKNOWN
)
1704 return folder
->outbox
;
1710 FolderItem
*folder_get_default_outbox_for_class(FolderType type
)
1714 for (flist
= folder_list
; flist
!= NULL
; flist
= g_list_next(flist
)) {
1715 Folder
* folder
= FOLDER(flist
->data
);
1719 if (folder
->outbox
== NULL
)
1721 if (folder
->klass
->type
!= type
)
1724 return folder
->outbox
;
1730 FolderItem
*folder_get_default_draft(void)
1734 for (flist
= folder_list
; flist
!= NULL
; flist
= g_list_next(flist
)) {
1735 Folder
* folder
= FOLDER(flist
->data
);
1739 if (folder
->draft
== NULL
)
1741 if (folder
->klass
->type
== F_UNKNOWN
)
1744 return folder
->draft
;
1750 FolderItem
*folder_get_default_draft_for_class(FolderType type
)
1754 for (flist
= folder_list
; flist
!= NULL
; flist
= g_list_next(flist
)) {
1755 Folder
* folder
= FOLDER(flist
->data
);
1759 if (folder
->draft
== NULL
)
1761 if (folder
->klass
->type
!= type
)
1764 return folder
->draft
;
1770 FolderItem
*folder_get_default_queue(void)
1774 for (flist
= folder_list
; flist
!= NULL
; flist
= g_list_next(flist
)) {
1775 Folder
* folder
= FOLDER(flist
->data
);
1779 if (folder
->queue
== NULL
)
1781 if (folder
->klass
->type
== F_UNKNOWN
)
1784 return folder
->queue
;
1790 FolderItem
*folder_get_default_queue_for_class(FolderType type
)
1794 for (flist
= folder_list
; flist
!= NULL
; flist
= g_list_next(flist
)) {
1795 Folder
* folder
= FOLDER(flist
->data
);
1799 if (folder
->queue
== NULL
)
1801 if (folder
->klass
->type
!= type
)
1804 return folder
->queue
;
1810 FolderItem
*folder_get_default_trash(void)
1814 for (flist
= folder_list
; flist
!= NULL
; flist
= g_list_next(flist
)) {
1815 Folder
* folder
= FOLDER(flist
->data
);
1819 if (folder
->trash
== NULL
)
1821 if (folder
->klass
->type
== F_UNKNOWN
)
1824 return folder
->trash
;
1830 FolderItem
*folder_get_default_trash_for_class(FolderType type
)
1834 for (flist
= folder_list
; flist
!= NULL
; flist
= g_list_next(flist
)) {
1835 Folder
* folder
= FOLDER(flist
->data
);
1839 if (folder
->trash
== NULL
)
1841 if (folder
->klass
->type
!= type
)
1844 return folder
->trash
;
1850 #define CREATE_FOLDER_IF_NOT_EXIST(member, dir, type) \
1852 if (!folder->member) { \
1853 item = folder_item_new(folder, dir, dir); \
1854 item->stype = type; \
1855 folder_item_append(rootitem, item); \
1856 folder->member = item; \
1860 void folder_set_missing_folders(void)
1863 FolderItem
*rootitem
;
1867 for (list
= folder_list
; list
!= NULL
; list
= list
->next
) {
1868 folder
= list
->data
;
1869 if (FOLDER_TYPE(folder
) != F_MH
) continue;
1870 rootitem
= FOLDER_ITEM(folder
->node
->data
);
1871 cm_return_if_fail(rootitem
!= NULL
);
1873 if (folder
->inbox
&& folder
->outbox
&& folder
->draft
&&
1874 folder
->queue
&& folder
->trash
)
1877 if (folder
->klass
->create_tree(folder
) < 0) {
1878 g_warning("%s: can't create the folder tree",
1879 LOCAL_FOLDER(folder
)->rootpath
);
1883 CREATE_FOLDER_IF_NOT_EXIST(inbox
, INBOX_DIR
, F_INBOX
);
1884 CREATE_FOLDER_IF_NOT_EXIST(outbox
, OUTBOX_DIR
, F_OUTBOX
);
1885 CREATE_FOLDER_IF_NOT_EXIST(draft
, DRAFT_DIR
, F_DRAFT
);
1886 CREATE_FOLDER_IF_NOT_EXIST(queue
, QUEUE_DIR
, F_QUEUE
);
1887 CREATE_FOLDER_IF_NOT_EXIST(trash
, TRASH_DIR
, F_TRASH
);
1891 static gboolean
folder_unref_account_func(GNode
*node
, gpointer data
)
1893 FolderItem
*item
= node
->data
;
1894 PrefsAccount
*account
= data
;
1896 if (item
->account
== account
)
1897 item
->account
= NULL
;
1902 void folder_unref_account_all(PrefsAccount
*account
)
1907 if (!account
) return;
1909 for (list
= folder_list
; list
!= NULL
; list
= list
->next
) {
1910 folder
= list
->data
;
1911 if (folder
->account
== account
)
1912 folder
->account
= NULL
;
1913 g_node_traverse(folder
->node
, G_PRE_ORDER
, G_TRAVERSE_ALL
, -1,
1914 folder_unref_account_func
, account
);
1918 #undef CREATE_FOLDER_IF_NOT_EXIST
1920 gchar
*folder_item_get_path(FolderItem
*item
)
1924 cm_return_val_if_fail(item
!= NULL
, NULL
);
1925 folder
= item
->folder
;
1926 cm_return_val_if_fail(folder
!= NULL
, NULL
);
1928 return folder
->klass
->item_get_path(folder
, item
);
1931 static gint
folder_sort_cache_list_by_msgnum(gconstpointer a
, gconstpointer b
)
1933 MsgInfo
*msginfo_a
= (MsgInfo
*) a
;
1934 MsgInfo
*msginfo_b
= (MsgInfo
*) b
;
1936 return (msginfo_a
->msgnum
- msginfo_b
->msgnum
);
1939 static gint
folder_sort_folder_list(gconstpointer a
, gconstpointer b
)
1941 guint gint_a
= GPOINTER_TO_INT(a
);
1942 guint gint_b
= GPOINTER_TO_INT(b
);
1944 return (gint_a
- gint_b
);
1947 static gint
syncronize_flags(FolderItem
*item
, MsgInfoList
*msglist
)
1949 GHashTable
*relation
;
1955 if(item
->folder
->klass
->get_flags
== NULL
)
1957 if (item
->no_select
)
1960 relation
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
1961 if ((ret
= item
->folder
->klass
->get_flags(
1962 item
->folder
, item
, msglist
, relation
)) == 0) {
1963 gpointer data
, old_key
;
1965 MsgPermFlags permflags
= 0;
1967 folder_item_update_freeze();
1968 folder_item_set_batch(item
, TRUE
);
1969 for (cur
= msglist
; cur
!= NULL
; cur
= g_slist_next(cur
)) {
1970 msginfo
= (MsgInfo
*) cur
->data
;
1972 if (g_hash_table_lookup_extended(relation
, msginfo
, &old_key
, &data
)) {
1973 permflags
= GPOINTER_TO_INT(data
);
1975 if (msginfo
->flags
.perm_flags
!= permflags
) {
1976 procmsg_msginfo_change_flags(msginfo
,
1977 permflags
& ~msginfo
->flags
.perm_flags
, 0,
1978 ~permflags
& msginfo
->flags
.perm_flags
, 0);
1982 folder_item_set_batch(item
, FALSE
);
1983 folder_item_update_thaw();
1985 g_hash_table_destroy(relation
);
1990 static gint
folder_item_syncronize_flags(FolderItem
*item
)
1992 MsgInfoList
*msglist
= NULL
;
1996 cm_return_val_if_fail(item
!= NULL
, -1);
1997 cm_return_val_if_fail(item
->folder
!= NULL
, -1);
1998 cm_return_val_if_fail(item
->folder
->klass
!= NULL
, -1);
1999 if (item
->no_select
)
2002 item
->scanning
= ITEM_SCANNING_WITH_FLAGS
;
2004 if (item
->cache
== NULL
)
2005 folder_item_read_cache(item
);
2007 msglist
= msgcache_get_msg_list(item
->cache
);
2009 ret
= syncronize_flags(item
, msglist
);
2011 for (cur
= msglist
; cur
!= NULL
; cur
= g_slist_next(cur
)) {
2012 procmsg_msginfo_free((MsgInfo
**)&(cur
->data
));
2015 g_slist_free(msglist
);
2017 item
->scanning
= ITEM_NOT_SCANNING
;
2022 static void folder_item_process_open (FolderItem
*item
,
2023 void (*before_proc_func
)(gpointer data
),
2024 void (*after_proc_func
)(gpointer data
),
2030 if((item
->folder
->klass
->scan_required
!= NULL
) &&
2031 (item
->folder
->klass
->scan_required(item
->folder
, item
))) {
2032 folder_item_scan_full(item
, TRUE
);
2034 folder_item_syncronize_flags(item
);
2038 if (item
->prefs
->enable_processing_when_opening
) {
2039 buf
= g_strdup_printf(_("Processing (%s)...\n"),
2040 item
->path
? item
->path
: item
->name
);
2043 if (before_proc_func
)
2044 before_proc_func(data
);
2046 folder_item_apply_processing(item
);
2048 if (after_proc_func
)
2049 after_proc_func(data
);
2051 item
->processing_pending
= FALSE
;
2055 gint
folder_item_open(FolderItem
*item
)
2058 if (item
->no_select
)
2061 if (item
->scanning
!= ITEM_NOT_SCANNING
) {
2062 debug_print("%s is scanning... \n", item
->path
? item
->path
: item
->name
);
2066 item
->processing_pending
= TRUE
;
2067 folder_item_process_open (item
, NULL
, NULL
, NULL
);
2069 item
->opened
= TRUE
;
2074 gint
folder_item_close(FolderItem
*item
)
2076 GSList
*mlist
, *cur
;
2079 cm_return_val_if_fail(item
!= NULL
, -1);
2081 if (item
->no_select
)
2084 if (item
->new_msgs
) {
2085 folder_item_update_freeze();
2086 mlist
= folder_item_get_msg_list(item
);
2087 for (cur
= mlist
; cur
!= NULL
; cur
= cur
->next
) {
2090 msginfo
= (MsgInfo
*) cur
->data
;
2091 if (MSG_IS_NEW(msginfo
->flags
))
2092 procmsg_msginfo_unset_flags(msginfo
, MSG_NEW
, 0);
2093 procmsg_msginfo_free(&msginfo
);
2095 g_slist_free(mlist
);
2096 folder_item_update_thaw();
2099 folder_item_write_cache(item
);
2101 folder_item_update(item
, F_ITEM_UPDATE_MSGCNT
);
2103 item
->opened
= FALSE
;
2104 folder
= item
->folder
;
2106 if (folder
->klass
->close
== NULL
)
2109 return folder
->klass
->close(folder
, item
);
2112 static MsgInfoList
*get_msginfos(FolderItem
*item
, MsgNumberList
*numlist
)
2114 MsgInfoList
*msglist
= NULL
;
2115 Folder
*folder
= item
->folder
;
2116 if (item
->no_select
)
2119 if (folder
->klass
->get_msginfos
!= NULL
)
2120 msglist
= folder
->klass
->get_msginfos(folder
, item
, numlist
);
2122 MsgNumberList
*elem
;
2124 for (elem
= numlist
; elem
!= NULL
; elem
= g_slist_next(elem
)) {
2128 num
= GPOINTER_TO_INT(elem
->data
);
2129 msginfo
= folder
->klass
->get_msginfo(folder
, item
, num
);
2130 if (msginfo
!= NULL
)
2131 msglist
= g_slist_prepend(msglist
, msginfo
);
2138 static MsgInfo
*get_msginfo(FolderItem
*item
, guint num
)
2140 MsgNumberList numlist
;
2141 MsgInfoList
*msglist
;
2142 MsgInfo
*msginfo
= NULL
;
2144 numlist
.data
= GINT_TO_POINTER(num
);
2145 numlist
.next
= NULL
;
2146 msglist
= get_msginfos(item
, &numlist
);
2147 if (msglist
!= NULL
)
2148 msginfo
= procmsg_msginfo_new_ref(msglist
->data
);
2149 procmsg_msg_list_free(msglist
);
2154 gint
folder_item_scan_full(FolderItem
*item
, gboolean filtering
)
2157 GSList
*folder_list
= NULL
, *cache_list
= NULL
;
2158 GSList
*folder_list_cur
, *cache_list_cur
, *new_list
= NULL
;
2159 GSList
*exists_list
= NULL
, *elem
;
2160 GSList
*newmsg_list
= NULL
;
2161 guint newcnt
= 0, unreadcnt
= 0, totalcnt
= 0;
2162 guint markedcnt
= 0, unreadmarkedcnt
= 0;
2163 guint repliedcnt
= 0, forwardedcnt
= 0;
2164 guint lockedcnt
= 0, ignoredcnt
= 0, watchedcnt
= 0;
2166 guint cache_max_num
, folder_max_num
, cache_cur_num
, folder_cur_num
;
2167 gboolean update_flags
= 0, old_uids_valid
= FALSE
;
2168 GHashTable
*subject_table
= NULL
;
2170 cm_return_val_if_fail(item
!= NULL
, -1);
2171 if (item
->path
== NULL
) return -1;
2173 folder
= item
->folder
;
2175 cm_return_val_if_fail(folder
!= NULL
, -1);
2176 cm_return_val_if_fail(folder
->klass
->get_num_list
!= NULL
, -1);
2178 item
->scanning
= ITEM_SCANNING_WITH_FLAGS
;
2180 debug_print("Scanning folder %s for cache changes.\n", item
->path
? item
->path
: "(null)");
2182 /* Get list of messages for folder and cache */
2183 if (folder
->klass
->get_num_list(item
->folder
, item
, &folder_list
, &old_uids_valid
) < 0) {
2184 debug_print("Error fetching list of message numbers\n");
2185 item
->scanning
= ITEM_NOT_SCANNING
;
2189 if(prefs_common
.thread_by_subject
) {
2190 subject_table
= g_hash_table_new(g_str_hash
, g_str_equal
);
2193 if (old_uids_valid
) {
2195 folder_item_read_cache(item
);
2196 cache_list
= msgcache_get_msg_list(item
->cache
);
2199 msgcache_destroy(item
->cache
);
2200 item
->cache
= msgcache_new();
2201 item
->cache_dirty
= TRUE
;
2202 item
->mark_dirty
= TRUE
;
2203 item
->tags_dirty
= TRUE
;
2207 /* Sort both lists */
2208 cache_list
= g_slist_sort(cache_list
, folder_sort_cache_list_by_msgnum
);
2209 folder_list
= g_slist_sort(folder_list
, folder_sort_folder_list
);
2211 cache_list_cur
= cache_list
;
2212 folder_list_cur
= folder_list
;
2214 if (cache_list_cur
!= NULL
) {
2215 GSList
*cache_list_last
;
2217 cache_cur_num
= ((MsgInfo
*)cache_list_cur
->data
)->msgnum
;
2218 cache_list_last
= g_slist_last(cache_list
);
2219 cache_max_num
= ((MsgInfo
*)cache_list_last
->data
)->msgnum
;
2221 cache_cur_num
= G_MAXUINT
;
2225 if (folder_list_cur
!= NULL
) {
2226 GSList
*folder_list_last
;
2228 folder_cur_num
= GPOINTER_TO_UINT(folder_list_cur
->data
);
2229 folder_list_last
= g_slist_last(folder_list
);
2230 folder_max_num
= GPOINTER_TO_UINT(folder_list_last
->data
);
2232 folder_cur_num
= G_MAXUINT
;
2236 while ((cache_cur_num
!= G_MAXUINT
) || (folder_cur_num
!= G_MAXUINT
)) {
2238 * Message only exists in the folder
2239 * Remember message for fetching
2241 if (folder_cur_num
< cache_cur_num
) {
2242 gboolean add
= FALSE
;
2244 switch(FOLDER_TYPE(folder
)) {
2246 if (folder_cur_num
< cache_max_num
)
2249 if (folder
->account
->max_articles
== 0) {
2253 if (folder_max_num
<= folder
->account
->max_articles
) {
2255 } else if (folder_cur_num
> (folder_max_num
- folder
->account
->max_articles
)) {
2265 new_list
= g_slist_prepend(new_list
, GUINT_TO_POINTER(folder_cur_num
));
2266 debug_print("Remembered message %u for fetching\n", folder_cur_num
);
2269 /* Move to next folder number */
2270 if (folder_list_cur
)
2271 folder_list_cur
= folder_list_cur
->next
;
2273 if (folder_list_cur
!= NULL
)
2274 folder_cur_num
= GPOINTER_TO_UINT(folder_list_cur
->data
);
2276 folder_cur_num
= G_MAXUINT
;
2282 * Message only exists in the cache
2283 * Remove the message from the cache
2285 if (cache_cur_num
< folder_cur_num
) {
2286 msgcache_remove_msg(item
->cache
, cache_cur_num
);
2287 debug_print("Removed message %u from cache.\n", cache_cur_num
);
2289 /* Move to next cache number */
2291 cache_list_cur
= cache_list_cur
->next
;
2293 if (cache_list_cur
!= NULL
)
2294 cache_cur_num
= ((MsgInfo
*)cache_list_cur
->data
)->msgnum
;
2296 cache_cur_num
= G_MAXUINT
;
2298 update_flags
|= F_ITEM_UPDATE_MSGCNT
| F_ITEM_UPDATE_CONTENT
;
2304 * Message number exists in folder and cache!
2305 * Check if the message has been modified
2307 if (cache_cur_num
== folder_cur_num
) {
2310 msginfo
= msgcache_get_msg(item
->cache
, folder_cur_num
);
2311 if (msginfo
&& folder
->klass
->is_msg_changed
&& folder
->klass
->is_msg_changed(folder
, item
, msginfo
)) {
2312 msgcache_remove_msg(item
->cache
, msginfo
->msgnum
);
2313 new_list
= g_slist_prepend(new_list
, GINT_TO_POINTER(msginfo
->msgnum
));
2314 procmsg_msginfo_free(&msginfo
);
2316 debug_print("Remembering message %u to update...\n", folder_cur_num
);
2317 } else if (msginfo
) {
2318 exists_list
= g_slist_prepend(exists_list
, msginfo
);
2320 if(prefs_common
.thread_by_subject
&&
2321 MSG_IS_IGNORE_THREAD(msginfo
->flags
) &&
2322 !subject_table_lookup(subject_table
, msginfo
->subject
)) {
2323 subject_table_insert(subject_table
, msginfo
->subject
, msginfo
);
2327 /* Move to next folder and cache number */
2329 cache_list_cur
= cache_list_cur
->next
;
2331 if (folder_list_cur
)
2332 folder_list_cur
= folder_list_cur
->next
;
2334 if (cache_list_cur
!= NULL
)
2335 cache_cur_num
= ((MsgInfo
*)cache_list_cur
->data
)->msgnum
;
2337 cache_cur_num
= G_MAXUINT
;
2339 if (folder_list_cur
!= NULL
)
2340 folder_cur_num
= GPOINTER_TO_UINT(folder_list_cur
->data
);
2342 folder_cur_num
= G_MAXUINT
;
2348 for(cache_list_cur
= cache_list
; cache_list_cur
!= NULL
; cache_list_cur
= g_slist_next(cache_list_cur
))
2349 procmsg_msginfo_free((MsgInfo
**)&(cache_list_cur
->data
));
2351 g_slist_free(cache_list
);
2352 g_slist_free(folder_list
);
2354 if (new_list
!= NULL
) {
2355 GSList
*tmp_list
= NULL
;
2356 newmsg_list
= get_msginfos(item
, new_list
);
2357 g_slist_free(new_list
);
2358 tmp_list
= g_slist_concat(g_slist_copy(exists_list
), g_slist_copy(newmsg_list
));
2359 syncronize_flags(item
, tmp_list
);
2360 g_slist_free(tmp_list
);
2362 syncronize_flags(item
, exists_list
);
2365 folder_item_update_freeze();
2367 item
->scanning
= ITEM_SCANNING
;
2369 if (newmsg_list
!= NULL
) {
2370 GSList
*elem
, *to_filter
= NULL
;
2371 gboolean do_filter
= (filtering
== TRUE
) &&
2372 (item
->folder
->account
!= NULL
) &&
2373 (item
->folder
->account
->filter_on_recv
) &&
2374 ((item
->stype
== F_INBOX
) ||
2375 ((item
->stype
== F_NORMAL
) &&
2376 (FOLDER_TYPE(item
->folder
) == F_NEWS
)));
2378 for (elem
= newmsg_list
; elem
!= NULL
; elem
= g_slist_next(elem
)) {
2379 MsgInfo
*msginfo
= (MsgInfo
*) elem
->data
;
2381 msgcache_add_msg(item
->cache
, msginfo
);
2383 exists_list
= g_slist_prepend(exists_list
, msginfo
);
2385 if(prefs_common
.thread_by_subject
&&
2386 MSG_IS_IGNORE_THREAD(msginfo
->flags
) &&
2387 !subject_table_lookup(subject_table
, msginfo
->subject
)) {
2388 subject_table_insert(subject_table
, msginfo
->subject
, msginfo
);
2396 folder_item_set_batch(item
, TRUE
);
2397 procmsg_msglist_filter(newmsg_list
, item
->folder
->account
,
2398 &to_filter
, &unfiltered
,
2400 folder_item_set_batch(item
, FALSE
);
2402 filtering_move_and_copy_msgs(newmsg_list
);
2403 if (to_filter
!= NULL
) {
2404 for (elem
= to_filter
; elem
; elem
= g_slist_next(elem
)) {
2405 MsgInfo
*msginfo
= (MsgInfo
*)elem
->data
;
2406 procmsg_msginfo_free(&msginfo
);
2408 g_slist_free(to_filter
);
2410 if (unfiltered
!= NULL
) {
2411 for (elem
= unfiltered
; elem
; elem
= g_slist_next(elem
)) {
2412 MsgInfo
*msginfo
= (MsgInfo
*)elem
->data
;
2413 exists_list
= g_slist_prepend(exists_list
, msginfo
);
2415 if(prefs_common
.thread_by_subject
&&
2416 MSG_IS_IGNORE_THREAD(msginfo
->flags
) &&
2417 !subject_table_lookup(subject_table
, msginfo
->subject
)) {
2418 subject_table_insert(subject_table
, msginfo
->subject
, msginfo
);
2421 g_slist_free(unfiltered
);
2423 if (prefs_common
.real_time_sync
)
2424 folder_item_synchronise(item
);
2426 if (prefs_common
.real_time_sync
)
2427 folder_item_synchronise(item
);
2430 g_slist_free(newmsg_list
);
2432 update_flags
|= F_ITEM_UPDATE_MSGCNT
| F_ITEM_UPDATE_CONTENT
;
2435 folder_item_set_batch(item
, TRUE
);
2436 for (elem
= exists_list
; elem
!= NULL
; elem
= g_slist_next(elem
)) {
2437 MsgInfo
*msginfo
, *parent_msginfo
;
2439 msginfo
= elem
->data
;
2440 if (MSG_IS_IGNORE_THREAD(msginfo
->flags
) && (MSG_IS_NEW(msginfo
->flags
) || MSG_IS_UNREAD(msginfo
->flags
)))
2441 procmsg_msginfo_unset_flags(msginfo
, MSG_NEW
| MSG_UNREAD
, 0);
2442 if (!MSG_IS_IGNORE_THREAD(msginfo
->flags
) && procmsg_msg_has_flagged_parent(msginfo
, MSG_IGNORE_THREAD
)) {
2443 procmsg_msginfo_change_flags(msginfo
, MSG_IGNORE_THREAD
, 0, MSG_NEW
| MSG_UNREAD
, 0);
2445 if (!MSG_IS_WATCH_THREAD(msginfo
->flags
) && procmsg_msg_has_flagged_parent(msginfo
, MSG_WATCH_THREAD
)) {
2446 procmsg_msginfo_set_flags(msginfo
, MSG_WATCH_THREAD
, 0);
2448 if(prefs_common
.thread_by_subject
&& !msginfo
->inreplyto
&&
2449 !msginfo
->references
&& !MSG_IS_IGNORE_THREAD(msginfo
->flags
) &&
2450 (parent_msginfo
= subject_table_lookup(subject_table
, msginfo
->subject
)))
2452 if(MSG_IS_IGNORE_THREAD(parent_msginfo
->flags
)) {
2453 procmsg_msginfo_change_flags(msginfo
, MSG_IGNORE_THREAD
, 0,
2454 MSG_NEW
| MSG_UNREAD
, 0);
2457 if ((folder_has_parent_of_type(item
, F_OUTBOX
) ||
2458 folder_has_parent_of_type(item
, F_QUEUE
) ||
2459 folder_has_parent_of_type(item
, F_TRASH
)) &&
2460 (MSG_IS_NEW(msginfo
->flags
) || MSG_IS_UNREAD(msginfo
->flags
)))
2461 procmsg_msginfo_unset_flags(msginfo
, MSG_NEW
| MSG_UNREAD
, 0);
2462 if (MSG_IS_NEW(msginfo
->flags
))
2464 if (MSG_IS_UNREAD(msginfo
->flags
))
2466 if (MSG_IS_UNREAD(msginfo
->flags
) && procmsg_msg_has_marked_parent(msginfo
))
2468 if (MSG_IS_MARKED(msginfo
->flags
))
2470 if (MSG_IS_REPLIED(msginfo
->flags
))
2472 if (MSG_IS_FORWARDED(msginfo
->flags
))
2474 if (MSG_IS_LOCKED(msginfo
->flags
))
2476 if (MSG_IS_IGNORE_THREAD(msginfo
->flags
))
2478 if (MSG_IS_WATCH_THREAD(msginfo
->flags
))
2483 procmsg_msginfo_free(&msginfo
);
2485 folder_item_set_batch(item
, FALSE
);
2486 g_slist_free(exists_list
);
2488 if(prefs_common
.thread_by_subject
) {
2489 g_hash_table_destroy(subject_table
);
2492 if (item
->new_msgs
!= newcnt
|| item
->unread_msgs
!= unreadcnt
2493 || item
->total_msgs
!= totalcnt
|| item
->marked_msgs
!= markedcnt
2494 || item
->unreadmarked_msgs
!= unreadmarkedcnt
2495 || item
->replied_msgs
!= repliedcnt
|| item
->forwarded_msgs
!= forwardedcnt
2496 || item
->locked_msgs
!= lockedcnt
|| item
->ignored_msgs
!= ignoredcnt
2497 || item
->watched_msgs
!= watchedcnt
) {
2498 update_flags
|= F_ITEM_UPDATE_CONTENT
;
2501 item
->new_msgs
= newcnt
;
2502 item
->unread_msgs
= unreadcnt
;
2503 item
->total_msgs
= totalcnt
;
2504 item
->unreadmarked_msgs
= unreadmarkedcnt
;
2505 item
->marked_msgs
= markedcnt
;
2506 item
->replied_msgs
= repliedcnt
;
2507 item
->forwarded_msgs
= forwardedcnt
;
2508 item
->locked_msgs
= lockedcnt
;
2509 item
->ignored_msgs
= ignoredcnt
;
2510 item
->watched_msgs
= watchedcnt
;
2512 update_flags
|= F_ITEM_UPDATE_MSGCNT
;
2514 folder_item_update(item
, update_flags
);
2515 folder_item_update_thaw();
2517 item
->scanning
= ITEM_NOT_SCANNING
;
2522 gint
folder_item_scan(FolderItem
*item
)
2524 return folder_item_scan_full(item
, TRUE
);
2527 static void folder_count_total_cache_memusage(FolderItem
*item
, gpointer data
)
2529 gint
*memusage
= (gint
*)data
;
2531 if (item
->cache
== NULL
)
2534 *memusage
+= msgcache_get_memory_usage(item
->cache
);
2537 static gint
folder_cache_time_compare_func(gconstpointer a
, gconstpointer b
)
2539 FolderItem
*fa
= (FolderItem
*)a
;
2540 FolderItem
*fb
= (FolderItem
*)b
;
2542 return (gint
) (msgcache_get_last_access_time(fa
->cache
) - msgcache_get_last_access_time(fb
->cache
));
2545 static void folder_find_expired_caches(FolderItem
*item
, gpointer data
)
2547 GSList
**folder_item_list
= (GSList
**)data
;
2548 gint difftime
, expiretime
;
2550 if (item
->cache
== NULL
)
2553 if (item
->opened
> 0)
2556 difftime
= (gint
) (time(NULL
) - msgcache_get_last_access_time(item
->cache
));
2557 expiretime
= prefs_common
.cache_min_keep_time
* 60;
2558 debug_print("Cache unused time: %d (Expire time: %d)\n", difftime
, expiretime
);
2560 if (difftime
> expiretime
&& !item
->opened
&& !item
->processing_pending
) {
2561 *folder_item_list
= g_slist_insert_sorted(*folder_item_list
, item
, folder_cache_time_compare_func
);
2565 gboolean
folder_item_free_cache(FolderItem
*item
, gboolean force
)
2567 cm_return_val_if_fail(item
!= NULL
, TRUE
);
2569 if (item
->cache
== NULL
)
2572 if (item
->opened
> 0 && !force
)
2575 folder_item_write_cache(item
);
2576 msgcache_destroy(item
->cache
);
2581 void folder_clean_cache_memory_force(void)
2583 int old_cache_max_mem_usage
= prefs_common
.cache_max_mem_usage
;
2584 int old_cache_min_keep_time
= prefs_common
.cache_min_keep_time
;
2586 prefs_common
.cache_max_mem_usage
= 0;
2587 prefs_common
.cache_min_keep_time
= 0;
2589 folder_clean_cache_memory(NULL
);
2591 prefs_common
.cache_max_mem_usage
= old_cache_max_mem_usage
;
2592 prefs_common
.cache_min_keep_time
= old_cache_min_keep_time
;
2595 void folder_clean_cache_memory(FolderItem
*protected_item
)
2599 folder_func_to_all_folders(folder_count_total_cache_memusage
, &memusage
);
2600 debug_print("Total cache memory usage: %d\n", memusage
);
2602 if (memusage
> (prefs_common
.cache_max_mem_usage
* 1024)) {
2603 GSList
*folder_item_list
= NULL
, *listitem
;
2605 debug_print("Trying to free cache memory\n");
2607 folder_func_to_all_folders(folder_find_expired_caches
, &folder_item_list
);
2608 listitem
= folder_item_list
;
2609 while((listitem
!= NULL
) && (memusage
> (prefs_common
.cache_max_mem_usage
* 1024))) {
2610 FolderItem
*item
= (FolderItem
*)(listitem
->data
);
2611 gint cache_size
= 0;
2612 if (item
== protected_item
) {
2613 listitem
= listitem
->next
;
2616 debug_print("Freeing cache memory for %s\n", item
->path
? item
->path
: item
->name
);
2617 cache_size
= msgcache_get_memory_usage(item
->cache
);
2618 if (folder_item_free_cache(item
, FALSE
))
2619 memusage
-= cache_size
;
2621 listitem
= listitem
->next
;
2623 g_slist_free(folder_item_list
);
2627 static void folder_item_remove_cached_msg(FolderItem
*item
, MsgInfo
*msginfo
)
2629 Folder
*folder
= item
->folder
;
2631 cm_return_if_fail(folder
!= NULL
);
2633 if (folder
->klass
->remove_cached_msg
== NULL
)
2636 folder
->klass
->remove_cached_msg(folder
, item
, msginfo
);
2639 static void folder_item_clean_local_files(FolderItem
*item
, gint days
)
2641 cm_return_if_fail(item
!= NULL
);
2642 cm_return_if_fail(item
->folder
!= NULL
);
2644 if (FOLDER_TYPE(item
->folder
) == F_IMAP
||
2645 FOLDER_TYPE(item
->folder
) == F_NEWS
) {
2646 GSList
*msglist
= folder_item_get_msg_list(item
);
2648 time_t t
= time(NULL
);
2649 for (cur
= msglist
; cur
; cur
= cur
->next
) {
2650 MsgInfo
*msginfo
= (MsgInfo
*)cur
->data
;
2651 gint age
= (t
- msginfo
->date_t
) / (60*60*24);
2653 folder_item_remove_cached_msg(item
, msginfo
);
2655 procmsg_msg_list_free(msglist
);
2659 static void folder_item_read_cache(FolderItem
*item
)
2661 gchar
*cache_file
, *mark_file
, *tags_file
;
2663 cm_return_if_fail(item
!= NULL
);
2665 if (item
->path
!= NULL
) {
2666 cache_file
= folder_item_get_cache_file(item
);
2667 mark_file
= folder_item_get_mark_file(item
);
2668 tags_file
= folder_item_get_tags_file(item
);
2669 item
->cache
= msgcache_read_cache(item
, cache_file
);
2670 item
->cache_dirty
= FALSE
;
2671 item
->mark_dirty
= FALSE
;
2672 item
->tags_dirty
= FALSE
;
2674 MsgInfoList
*list
, *cur
;
2675 guint newcnt
= 0, unreadcnt
= 0;
2676 guint markedcnt
= 0, unreadmarkedcnt
= 0;
2677 guint repliedcnt
= 0, forwardedcnt
= 0;
2678 guint lockedcnt
= 0, ignoredcnt
= 0;
2679 guint watchedcnt
= 0;
2682 item
->cache
= msgcache_new();
2683 item
->cache_dirty
= TRUE
;
2684 item
->mark_dirty
= TRUE
;
2685 item
->tags_dirty
= TRUE
;
2686 folder_item_scan_full(item
, TRUE
);
2688 msgcache_read_mark(item
->cache
, mark_file
);
2690 list
= msgcache_get_msg_list(item
->cache
);
2691 for (cur
= list
; cur
!= NULL
; cur
= g_slist_next(cur
)) {
2692 msginfo
= cur
->data
;
2694 if (MSG_IS_NEW(msginfo
->flags
))
2696 if (MSG_IS_UNREAD(msginfo
->flags
))
2698 if (MSG_IS_UNREAD(msginfo
->flags
) && procmsg_msg_has_marked_parent(msginfo
))
2700 if (MSG_IS_MARKED(msginfo
->flags
))
2702 if (MSG_IS_REPLIED(msginfo
->flags
))
2704 if (MSG_IS_FORWARDED(msginfo
->flags
))
2706 if (MSG_IS_LOCKED(msginfo
->flags
))
2708 if (MSG_IS_IGNORE_THREAD(msginfo
->flags
))
2710 if (MSG_IS_WATCH_THREAD(msginfo
->flags
))
2712 procmsg_msginfo_unset_flags(msginfo
, MSG_FULLY_CACHED
, 0);
2714 item
->new_msgs
= newcnt
;
2715 item
->unread_msgs
= unreadcnt
;
2716 item
->unreadmarked_msgs
= unreadmarkedcnt
;
2717 item
->marked_msgs
= markedcnt
;
2718 item
->replied_msgs
= repliedcnt
;
2719 item
->forwarded_msgs
= forwardedcnt
;
2720 item
->locked_msgs
= lockedcnt
;
2721 item
->ignored_msgs
= ignoredcnt
;
2722 item
->watched_msgs
= watchedcnt
;
2723 procmsg_msg_list_free(list
);
2725 msgcache_read_mark(item
->cache
, mark_file
);
2727 msgcache_read_tags(item
->cache
, tags_file
);
2733 item
->cache
= msgcache_new();
2734 item
->cache_dirty
= TRUE
;
2735 item
->mark_dirty
= TRUE
;
2736 item
->tags_dirty
= TRUE
;
2740 folder_clean_cache_memory(item
);
2743 void folder_item_write_cache(FolderItem
*item
)
2745 gchar
*cache_file
= NULL
, *mark_file
= NULL
, *tags_file
= NULL
;
2746 FolderItemPrefs
*prefs
;
2749 time_t last_mtime
= (time_t)0;
2750 gboolean need_scan
= FALSE
;
2752 if (!item
|| !item
->path
|| !item
->cache
)
2755 last_mtime
= item
->mtime
;
2756 if (item
->folder
->klass
->set_mtime
) {
2757 if (item
->folder
->klass
->scan_required
)
2758 need_scan
= item
->folder
->klass
->scan_required(item
->folder
, item
);
2763 id
= folder_item_get_identifier(item
);
2764 debug_print("Save cache for folder %s\n", id
);
2767 if (item
->cache_dirty
)
2768 cache_file
= folder_item_get_cache_file(item
);
2769 if (item
->cache_dirty
|| item
->mark_dirty
)
2770 mark_file
= folder_item_get_mark_file(item
);
2771 if (item
->cache_dirty
|| item
->tags_dirty
)
2772 tags_file
= folder_item_get_tags_file(item
);
2773 if (msgcache_write(cache_file
, mark_file
, tags_file
, item
->cache
) == 0) {
2774 prefs
= item
->prefs
;
2775 if (prefs
&& prefs
->enable_folder_chmod
&& prefs
->folder_chmod
) {
2776 filemode
= prefs
->folder_chmod
;
2777 if (filemode
& S_IRGRP
) filemode
|= S_IWGRP
;
2778 if (filemode
& S_IROTH
) filemode
|= S_IWOTH
;
2780 if (chmod(cache_file
, filemode
) < 0)
2781 FILE_OP_ERROR(cache_file
, "chmod");
2784 if (chmod(mark_file
, filemode
) < 0)
2785 FILE_OP_ERROR(mark_file
, "chmod");
2789 item
->cache_dirty
= TRUE
;
2790 item
->mark_dirty
= TRUE
;
2791 item
->tags_dirty
= TRUE
;
2794 if (!need_scan
&& item
->folder
->klass
->set_mtime
) {
2795 if (item
->mtime
== last_mtime
) {
2796 item
->folder
->klass
->set_mtime(item
->folder
, item
);
2805 MsgInfo
*folder_item_get_msginfo(FolderItem
*item
, gint num
)
2807 MsgInfo
*msginfo
= NULL
;
2809 cm_return_val_if_fail(item
!= NULL
, NULL
);
2810 if (item
->no_select
)
2813 folder_item_read_cache(item
);
2815 if ((msginfo
= msgcache_get_msg(item
->cache
, num
)) != NULL
)
2818 msginfo
= get_msginfo(item
, num
);
2819 if (msginfo
!= NULL
) {
2820 msgcache_add_msg(item
->cache
, msginfo
);
2827 MsgInfo
*folder_item_get_msginfo_by_msgid(FolderItem
*item
, const gchar
*msgid
)
2831 cm_return_val_if_fail(item
!= NULL
, NULL
);
2832 cm_return_val_if_fail(msgid
!= NULL
, NULL
);
2833 if (item
->no_select
)
2837 folder_item_read_cache(item
);
2839 if ((msginfo
= msgcache_get_msg_by_id(item
->cache
, msgid
)) != NULL
)
2845 GSList
*folder_item_get_msg_list(FolderItem
*item
)
2847 cm_return_val_if_fail(item
!= NULL
, NULL
);
2848 if (item
->no_select
)
2851 if (item
->cache
== 0)
2852 folder_item_read_cache(item
);
2854 cm_return_val_if_fail(item
->cache
!= NULL
, NULL
);
2856 return msgcache_get_msg_list(item
->cache
);
2859 static void msginfo_set_mime_flags(GNode
*node
, gpointer data
)
2861 MsgInfo
*msginfo
= data
;
2862 MimeInfo
*mimeinfo
= node
->data
;
2864 if (mimeinfo
->disposition
== DISPOSITIONTYPE_ATTACHMENT
&&
2865 (!mimeinfo
->subtype
|| (strcmp(mimeinfo
->subtype
, "pgp-signature") &&
2866 strcmp(mimeinfo
->subtype
, "x-pkcs7-signature") &&
2867 strcmp(mimeinfo
->subtype
, "pkcs7-signature")))) {
2868 procmsg_msginfo_set_flags(msginfo
, 0, MSG_HAS_ATTACHMENT
);
2869 } else if (mimeinfo
->disposition
== DISPOSITIONTYPE_UNKNOWN
&&
2870 mimeinfo
->id
== NULL
&&
2871 mimeinfo
->type
!= MIMETYPE_TEXT
&&
2872 mimeinfo
->type
!= MIMETYPE_MULTIPART
) {
2873 if (!mimeinfo
->subtype
2874 || (strcmp(mimeinfo
->subtype
, "pgp-signature") &&
2875 strcmp(mimeinfo
->subtype
, "x-pkcs7-signature") &&
2876 strcmp(mimeinfo
->subtype
, "pkcs7-signature")))
2877 procmsg_msginfo_set_flags(msginfo
, 0, MSG_HAS_ATTACHMENT
);
2878 } else if (mimeinfo
->disposition
== DISPOSITIONTYPE_INLINE
&&
2879 mimeinfo
->id
== NULL
&&
2880 (strcmp(mimeinfo
->subtype
, "pgp-signature") &&
2881 strcmp(mimeinfo
->subtype
, "x-pkcs7-signature") &&
2882 strcmp(mimeinfo
->subtype
, "pkcs7-signature")) &&
2883 (procmime_mimeinfo_get_parameter(mimeinfo
, "name") != NULL
||
2884 procmime_mimeinfo_get_parameter(mimeinfo
, "filename") != NULL
)) {
2885 procmsg_msginfo_set_flags(msginfo
, 0, MSG_HAS_ATTACHMENT
);
2888 /* don't descend below top level message for signed and encrypted info */
2889 if (mimeinfo
->type
== MIMETYPE_MESSAGE
)
2892 if (privacy_mimeinfo_is_signed(mimeinfo
)) {
2893 procmsg_msginfo_set_flags(msginfo
, 0, MSG_SIGNED
);
2896 if (privacy_mimeinfo_is_encrypted(mimeinfo
)) {
2897 procmsg_msginfo_set_flags(msginfo
, 0, MSG_ENCRYPTED
);
2899 /* searching inside encrypted parts doesn't really make sense */
2900 g_node_children_foreach(mimeinfo
->node
, G_TRAVERSE_ALL
, msginfo_set_mime_flags
, msginfo
);
2904 gchar
*folder_item_fetch_msg(FolderItem
*item
, gint num
)
2910 cm_return_val_if_fail(item
!= NULL
, NULL
);
2912 folder
= item
->folder
;
2914 cm_return_val_if_fail(folder
->klass
->fetch_msg
!= NULL
, NULL
);
2915 if (item
->no_select
)
2918 msgfile
= folder
->klass
->fetch_msg(folder
, item
, num
);
2920 if (msgfile
!= NULL
) {
2921 msginfo
= folder_item_get_msginfo(item
, num
);
2922 if ((msginfo
!= NULL
) && !MSG_IS_SCANNED(msginfo
->flags
)) {
2925 if (!folder_has_parent_of_type(msginfo
->folder
, F_QUEUE
) &&
2926 !folder_has_parent_of_type(msginfo
->folder
, F_DRAFT
))
2927 mimeinfo
= procmime_scan_file(msgfile
);
2929 mimeinfo
= procmime_scan_queue_file(msgfile
);
2930 /* check for attachments */
2931 if (mimeinfo
!= NULL
) {
2932 g_node_children_foreach(mimeinfo
->node
, G_TRAVERSE_ALL
, msginfo_set_mime_flags
, msginfo
);
2933 procmime_mimeinfo_free_all(&mimeinfo
);
2935 procmsg_msginfo_set_flags(msginfo
, 0, MSG_SCANNED
);
2938 procmsg_msginfo_free(&msginfo
);
2944 gchar
*folder_item_fetch_msg_full(FolderItem
*item
, gint num
, gboolean headers
,
2951 cm_return_val_if_fail(item
!= NULL
, NULL
);
2952 if (item
->no_select
)
2955 folder
= item
->folder
;
2957 if (folder
->klass
->fetch_msg_full
== NULL
)
2958 return folder_item_fetch_msg(item
, num
);
2960 if (item
->prefs
->offlinesync
&& prefs_common
.real_time_sync
)
2961 msgfile
= folder
->klass
->fetch_msg_full(folder
, item
, num
,
2964 msgfile
= folder
->klass
->fetch_msg_full(folder
, item
, num
,
2967 if (msgfile
!= NULL
) {
2968 msginfo
= folder_item_get_msginfo(item
, num
);
2969 if ((msginfo
!= NULL
) && !MSG_IS_SCANNED(msginfo
->flags
)) {
2972 if (!folder_has_parent_of_type(msginfo
->folder
, F_QUEUE
) &&
2973 !folder_has_parent_of_type(msginfo
->folder
, F_DRAFT
))
2974 mimeinfo
= procmime_scan_file(msgfile
);
2976 mimeinfo
= procmime_scan_queue_file(msgfile
);
2977 /* check for attachments */
2978 if (mimeinfo
!= NULL
) {
2979 g_node_children_foreach(mimeinfo
->node
, G_TRAVERSE_ALL
, msginfo_set_mime_flags
, msginfo
);
2980 procmime_mimeinfo_free_all(&mimeinfo
);
2982 procmsg_msginfo_set_flags(msginfo
, 0, MSG_SCANNED
);
2985 procmsg_msginfo_free(&msginfo
);
2992 static gint
folder_item_get_msg_num_by_file(FolderItem
*dest
, const gchar
*file
)
2994 static HeaderEntry hentry
[] = {{"Message-ID:", NULL
, TRUE
},
2995 {NULL
, NULL
, FALSE
}};
2999 gchar buf
[BUFFSIZE
];
3001 if ((fp
= claws_fopen(file
, "rb")) == NULL
)
3004 if ((folder_has_parent_of_type(dest
, F_QUEUE
)) ||
3005 (folder_has_parent_of_type(dest
, F_DRAFT
)))
3006 while (claws_fgets(buf
, sizeof(buf
), fp
) != NULL
) {
3008 if ((!strncmp(buf
, "X-Claws-End-Special-Headers: 1",
3009 strlen("X-Claws-End-Special-Headers:"))) ||
3010 (!strncmp(buf
, "X-Sylpheed-End-Special-Headers: 1",
3011 strlen("X-Sylpheed-End-Special-Headers:"))))
3014 if (buf
[0] == '\r' || buf
[0] == '\n') break;
3015 /* from other mailers */
3016 if (!strncmp(buf
, "Date: ", 6)
3017 || !strncmp(buf
, "To: ", 4)
3018 || !strncmp(buf
, "From: ", 6)
3019 || !strncmp(buf
, "Subject: ", 9)) {
3025 procheader_get_header_fields(fp
, hentry
);
3026 debug_print("looking for %s\n", hentry
[0].body
);
3027 if (hentry
[0].body
) {
3028 extract_parenthesis(hentry
[0].body
, '<', '>');
3029 remove_space(hentry
[0].body
);
3030 if ((msginfo
= msgcache_get_msg_by_id(dest
->cache
, hentry
[0].body
)) != NULL
) {
3031 msgnum
= msginfo
->msgnum
;
3032 procmsg_msginfo_free(&msginfo
);
3034 debug_print("found message as uid %d\n", msgnum
);
3038 g_free(hentry
[0].body
);
3039 hentry
[0].body
= NULL
;
3045 static void copy_msginfo_flags(MsgInfo
*source
, MsgInfo
*dest
)
3047 MsgPermFlags perm_flags
= 0;
3048 MsgTmpFlags tmp_flags
= 0;
3050 /* create new flags */
3051 if (source
!= NULL
) {
3052 /* copy original flags */
3053 perm_flags
= source
->flags
.perm_flags
;
3054 tmp_flags
= source
->flags
.tmp_flags
;
3056 perm_flags
= dest
->flags
.perm_flags
;
3057 tmp_flags
= dest
->flags
.tmp_flags
;
3060 /* remove new, unread and deleted in special folders */
3061 if (folder_has_parent_of_type(dest
->folder
, F_OUTBOX
) ||
3062 folder_has_parent_of_type(dest
->folder
, F_QUEUE
) ||
3063 folder_has_parent_of_type(dest
->folder
, F_DRAFT
) ||
3064 folder_has_parent_of_type(dest
->folder
, F_TRASH
))
3065 perm_flags
&= ~(MSG_NEW
| MSG_UNREAD
| MSG_DELETED
);
3067 /* set ignore flag of ignored parent exists */
3068 if (procmsg_msg_has_flagged_parent(dest
, MSG_IGNORE_THREAD
))
3069 perm_flags
|= MSG_IGNORE_THREAD
;
3071 /* unset FULLY_CACHED flags */
3072 perm_flags
&= ~MSG_FULLY_CACHED
;
3074 if (procmsg_msg_has_flagged_parent(dest
, MSG_WATCH_THREAD
))
3075 perm_flags
|= MSG_WATCH_THREAD
;
3077 /* Unset tmp flags that should not be copied */
3078 tmp_flags
&= ~(MSG_MOVE
| MSG_COPY
| MSG_MOVE_DONE
);
3080 /* unset flags that are set but should not */
3081 /* and set new flags */
3082 procmsg_msginfo_change_flags(dest
,
3083 ~dest
->flags
.perm_flags
& perm_flags
,
3084 ~dest
->flags
.tmp_flags
& tmp_flags
,
3085 dest
->flags
.perm_flags
& ~perm_flags
,
3086 dest
->flags
.tmp_flags
& ~tmp_flags
);
3088 if (source
&& source
->tags
) {
3089 g_slist_free(dest
->tags
);
3090 dest
->tags
= g_slist_copy(source
->tags
);
3091 folder_item_commit_tags(dest
->folder
, dest
, dest
->tags
, NULL
);
3095 static void add_msginfo_to_cache(FolderItem
*item
, MsgInfo
*newmsginfo
, MsgInfo
*flagsource
)
3097 /* update folder stats */
3098 if (MSG_IS_NEW(newmsginfo
->flags
))
3100 if (MSG_IS_UNREAD(newmsginfo
->flags
))
3101 item
->unread_msgs
++;
3102 if (MSG_IS_UNREAD(newmsginfo
->flags
) && procmsg_msg_has_marked_parent(newmsginfo
))
3103 item
->unreadmarked_msgs
++;
3104 if (MSG_IS_MARKED(newmsginfo
->flags
))
3105 item
->marked_msgs
++;
3106 if (MSG_IS_REPLIED(newmsginfo
->flags
))
3107 item
->replied_msgs
++;
3108 if (MSG_IS_FORWARDED(newmsginfo
->flags
))
3109 item
->forwarded_msgs
++;
3110 if (MSG_IS_LOCKED(newmsginfo
->flags
))
3111 item
->locked_msgs
++;
3112 if (MSG_IS_IGNORE_THREAD(newmsginfo
->flags
))
3113 item
->ignored_msgs
++;
3114 if (MSG_IS_WATCH_THREAD(newmsginfo
->flags
))
3115 item
->watched_msgs
++;
3118 folder_item_update_freeze();
3121 folder_item_read_cache(item
);
3123 msgcache_add_msg(item
->cache
, newmsginfo
);
3124 copy_msginfo_flags(flagsource
, newmsginfo
);
3125 folder_item_update_with_msg(item
, F_ITEM_UPDATE_MSGCNT
| F_ITEM_UPDATE_CONTENT
| F_ITEM_UPDATE_ADDMSG
, newmsginfo
);
3126 folder_item_update_thaw();
3129 static void remove_msginfo_from_cache(FolderItem
*item
, MsgInfo
*msginfo
)
3131 MsgInfoUpdate msginfo_update
;
3134 folder_item_read_cache(item
);
3136 if (MSG_IS_NEW(msginfo
->flags
) && !MSG_IS_IGNORE_THREAD(msginfo
->flags
))
3137 msginfo
->folder
->new_msgs
--;
3138 if (MSG_IS_UNREAD(msginfo
->flags
) && !MSG_IS_IGNORE_THREAD(msginfo
->flags
))
3139 msginfo
->folder
->unread_msgs
--;
3140 if (MSG_IS_UNREAD(msginfo
->flags
) && procmsg_msg_has_marked_parent(msginfo
))
3141 msginfo
->folder
->unreadmarked_msgs
--;
3142 if (MSG_IS_MARKED(msginfo
->flags
))
3143 item
->marked_msgs
--;
3144 if (MSG_IS_REPLIED(msginfo
->flags
))
3145 item
->replied_msgs
--;
3146 if (MSG_IS_FORWARDED(msginfo
->flags
))
3147 item
->forwarded_msgs
--;
3148 if (MSG_IS_LOCKED(msginfo
->flags
))
3149 item
->locked_msgs
--;
3150 if (MSG_IS_IGNORE_THREAD(msginfo
->flags
))
3151 item
->ignored_msgs
--;
3152 if (MSG_IS_WATCH_THREAD(msginfo
->flags
))
3153 item
->watched_msgs
--;
3155 msginfo
->folder
->total_msgs
--;
3157 msginfo_update
.msginfo
= msginfo
;
3158 msginfo_update
.flags
= MSGINFO_UPDATE_DELETED
;
3159 hooks_invoke(MSGINFO_UPDATE_HOOKLIST
, &msginfo_update
);
3161 msgcache_remove_msg(item
->cache
, msginfo
->msgnum
);
3162 folder_item_update_with_msg(msginfo
->folder
, F_ITEM_UPDATE_MSGCNT
| F_ITEM_UPDATE_CONTENT
| F_ITEM_UPDATE_REMOVEMSG
, msginfo
);
3165 gint
folder_item_add_msg(FolderItem
*dest
, const gchar
*file
,
3166 MsgFlags
*flags
, gboolean remove_source
)
3169 MsgFileInfo fileinfo
;
3171 cm_return_val_if_fail(dest
!= NULL
, -1);
3172 cm_return_val_if_fail(file
!= NULL
, -1);
3174 fileinfo
.msginfo
= NULL
;
3175 fileinfo
.file
= (gchar
*)file
;
3176 fileinfo
.flags
= flags
;
3177 file_list
.data
= &fileinfo
;
3178 file_list
.next
= NULL
;
3180 return folder_item_add_msgs(dest
, &file_list
, remove_source
);
3183 gint
folder_item_add_msgs(FolderItem
*dest
, GSList
*file_list
,
3184 gboolean remove_source
)
3187 gint ret
, num
, lastnum
= -1;
3189 GHashTable
*relation
;
3190 MsgFileInfo
*fileinfo
= NULL
;
3191 gboolean folderscan
= FALSE
;
3193 cm_return_val_if_fail(dest
!= NULL
, -1);
3194 cm_return_val_if_fail(file_list
!= NULL
, -1);
3195 cm_return_val_if_fail(dest
->folder
!= NULL
, -1);
3196 if (dest
->no_select
)
3199 folder
= dest
->folder
;
3201 relation
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
3203 if (folder
->klass
->add_msgs
!= NULL
) {
3204 ret
= folder
->klass
->add_msgs(folder
, dest
, file_list
, relation
);
3206 g_hash_table_destroy(relation
);
3210 for (file_cur
= file_list
; file_cur
!= NULL
; file_cur
= g_slist_next(file_cur
)) {
3211 fileinfo
= (MsgFileInfo
*) file_cur
->data
;
3213 ret
= folder
->klass
->add_msg(folder
, dest
, fileinfo
->file
, fileinfo
->flags
);
3215 g_hash_table_destroy(relation
);
3218 g_hash_table_insert(relation
, fileinfo
, GINT_TO_POINTER(ret
));
3222 for (file_cur
= file_list
; file_cur
!= NULL
; file_cur
= g_slist_next(file_cur
)) {
3223 gpointer data
, old_key
;
3225 fileinfo
= (MsgFileInfo
*) file_cur
->data
;
3226 if (g_hash_table_lookup_extended(relation
, fileinfo
, &old_key
, &data
))
3227 num
= GPOINTER_TO_INT(data
);
3232 MsgInfo
*newmsginfo
;
3236 folder_item_scan_full(dest
, FALSE
);
3239 num
= folder_item_get_msg_num_by_file(dest
, fileinfo
->file
);
3240 debug_print("got num %d\n", num
);
3246 if (num
>= 0 && remove_source
) {
3247 if (claws_unlink(fileinfo
->file
) < 0)
3248 FILE_OP_ERROR(fileinfo
->file
, "unlink");
3255 ((newmsginfo
= get_msginfo(dest
, num
)) != NULL
)) {
3256 add_msginfo_to_cache(dest
, newmsginfo
, NULL
);
3257 procmsg_msginfo_free(&newmsginfo
);
3258 } else if ((newmsginfo
= msgcache_get_msg(dest
->cache
, num
)) != NULL
) {
3259 /* TODO: set default flags */
3260 procmsg_msginfo_free(&newmsginfo
);
3265 g_hash_table_destroy(relation
);
3270 static FolderItem
*folder_item_move_recursive(FolderItem
*src
, FolderItem
*dest
, gboolean copy
)
3273 FolderItem
*new_item
;
3274 FolderItem
*next_item
;
3276 gchar
*old_id
, *new_id
;
3277 FolderUpdateData hookdata
;
3280 debug_print("%s %s to %s\n", copy
?"Copying":"Moving", src
->path
, dest
->path
);
3281 new_item
= folder_create_folder(dest
, src
->name
);
3282 if (new_item
== NULL
) {
3283 g_print("Can't create folder\n");
3287 if (new_item
->folder
== NULL
)
3288 new_item
->folder
= dest
->folder
;
3291 log_message(LOG_PROTOCOL
, copy
?_("Copying %s to %s...\n"):_("Moving %s to %s...\n"),
3292 src
->name
, new_item
->path
);
3295 folder_item_prefs_copy_prefs(src
, new_item
);
3297 /* copy internal data */
3298 if (src
->folder
->klass
== new_item
->folder
->klass
&&
3299 src
->folder
->klass
->copy_private_data
!= NULL
)
3300 src
->folder
->klass
->copy_private_data(src
->folder
,
3302 new_item
->collapsed
= src
->collapsed
;
3303 new_item
->thread_collapsed
= src
->thread_collapsed
;
3304 new_item
->threaded
= src
->threaded
;
3305 new_item
->ret_rcpt
= src
->ret_rcpt
;
3306 new_item
->hide_read_msgs
= src
->hide_read_msgs
;
3307 new_item
->hide_del_msgs
= src
->hide_del_msgs
;
3308 new_item
->hide_read_threads
= src
->hide_read_threads
;
3309 new_item
->sort_key
= src
->sort_key
;
3310 new_item
->sort_type
= src
->sort_type
;
3312 mlist
= folder_item_get_msg_list(src
);
3314 if (mlist
!= NULL
) {
3316 folder_item_copy_msgs(new_item
, mlist
);
3318 folder_item_move_msgs(new_item
, mlist
);
3319 procmsg_msg_list_free(mlist
);
3322 prefs_matcher_write_config();
3325 srcnode
= src
->folder
->node
;
3326 srcnode
= g_node_find(srcnode
, G_PRE_ORDER
, G_TRAVERSE_ALL
, src
);
3327 srcnode
= srcnode
->children
;
3328 while (srcnode
!= NULL
) {
3329 if (srcnode
&& srcnode
->data
) {
3330 next_item
= (FolderItem
*) srcnode
->data
;
3331 srcnode
= srcnode
->next
;
3332 if (folder_item_move_recursive(next_item
, new_item
, copy
) == NULL
) {
3337 old_id
= folder_item_get_identifier(src
);
3338 new_id
= folder_item_get_identifier(new_item
);
3340 hookdata
.folder
= src
->folder
;
3341 hookdata
.update_flags
= FOLDER_TREE_CHANGED
| FOLDER_MOVE_FOLDERITEM
;
3342 hookdata
.item
= src
;
3343 hookdata
.item2
= new_item
;
3344 hooks_invoke(FOLDER_UPDATE_HOOKLIST
, &hookdata
);
3346 /* if src supports removing, otherwise only copy folder */
3347 if (src
->folder
->klass
->remove_folder
!= NULL
&& !copy
)
3348 src
->folder
->klass
->remove_folder(src
->folder
, src
);
3349 folder_write_list();
3352 debug_print("updating rules : %s => %s\n", old_id
, new_id
);
3353 if (old_id
!= NULL
&& new_id
!= NULL
) {
3354 prefs_filtering_rename_path(old_id
, new_id
);
3355 account_rename_path(old_id
, new_id
);
3364 gint
folder_item_move_to(FolderItem
*src
, FolderItem
*dest
, FolderItem
**new_item
, gboolean copy
)
3366 FolderItem
*tmp
= folder_item_parent(dest
);
3367 gchar
* src_identifier
, * dst_identifier
;
3368 gchar
* phys_srcpath
, * phys_dstpath
, *tmppath
, *tmpname
;
3372 return F_MOVE_FAILED_DEST_IS_CHILD
;
3374 tmp
= folder_item_parent(tmp
);
3377 /* both dst and src can be root folders */
3378 src_identifier
= folder_item_get_identifier(src
);
3379 if (src_identifier
== NULL
&& src
->folder
&& folder_item_parent(src
) == NULL
) {
3380 src_identifier
= folder_get_identifier(src
->folder
);
3383 dst_identifier
= folder_item_get_identifier(dest
);
3384 if(dst_identifier
== NULL
&& dest
->folder
&& folder_item_parent(dest
) == NULL
) {
3385 dst_identifier
= folder_get_identifier(dest
->folder
);
3388 if (src_identifier
== NULL
|| dst_identifier
== NULL
) {
3389 debug_print("Can't get identifiers\n");
3391 g_free(src_identifier
);
3393 g_free(dst_identifier
);
3394 return F_MOVE_FAILED
;
3397 if (src
->folder
!= dest
->folder
&& !copy
) {
3398 g_free(src_identifier
);
3399 g_free(dst_identifier
);
3400 return F_MOVE_FAILED_DEST_OUTSIDE_MAILBOX
;
3402 g_free(src_identifier
);
3403 g_free(dst_identifier
);
3405 phys_srcpath
= folder_item_get_path(src
);
3406 tmppath
= folder_item_get_path(dest
);
3407 tmpname
= g_path_get_basename(phys_srcpath
);
3408 phys_dstpath
= g_strconcat(tmppath
,
3415 if (folder_item_parent(src
) == dest
|| src
== dest
) {
3416 g_free(phys_srcpath
);
3417 g_free(phys_dstpath
);
3418 return F_MOVE_FAILED_DEST_IS_PARENT
;
3420 debug_print("moving \"%s\" to \"%s\"\n", phys_srcpath
, phys_dstpath
);
3421 if ((tmp
= folder_item_move_recursive(src
, dest
, copy
)) == NULL
) {
3422 g_free(phys_srcpath
);
3423 g_free(phys_dstpath
);
3424 return F_MOVE_FAILED
;
3427 g_free(phys_srcpath
);
3428 g_free(phys_dstpath
);
3439 static void find_num(gpointer key
, gpointer value
, gpointer data
)
3441 struct find_data
*fdata
= (struct find_data
*)data
;
3442 if (GPOINTER_TO_INT(value
) == 0)
3443 fdata
->found
= TRUE
;
3446 static gboolean
some_msgs_have_zero_num(GHashTable
*hashtable
)
3448 struct find_data fdata
;
3450 fdata
.found
= FALSE
;
3451 g_hash_table_foreach(hashtable
, find_num
, &fdata
);
3457 * Copy a list of message to a new folder and remove
3458 * source messages if wanted
3460 static gint
do_copy_msgs(FolderItem
*dest
, GSList
*msglist
, gboolean remove_source
)
3464 gint num
, lastnum
= -1;
3465 gboolean folderscan
= FALSE
;
3466 GHashTable
*relation
;
3467 GSList
*not_moved
= NULL
;
3468 gint total
= 0, curmsg
= 0;
3469 MsgInfo
*msginfo
= NULL
;
3471 cm_return_val_if_fail(dest
!= NULL
, -1);
3472 cm_return_val_if_fail(msglist
!= NULL
, -1);
3474 folder
= dest
->folder
;
3476 cm_return_val_if_fail(folder
->klass
->copy_msg
!= NULL
, -1);
3477 if (dest
->no_select
)
3480 msginfo
= (MsgInfo
*)msglist
->data
;
3485 if (!MSG_IS_QUEUED(msginfo
->flags
) &&
3486 MSG_IS_DRAFT(msginfo
->flags
) &&
3487 folder_has_parent_of_type(dest
, F_QUEUE
)) {
3488 GSList
*cur
= msglist
;
3489 gboolean queue_err
= FALSE
;
3490 for (; cur
; cur
= cur
->next
) {
3491 Compose
*compose
= NULL
;
3492 FolderItem
*queue
= dest
;
3493 ComposeQueueResult val
= COMPOSE_QUEUE_SUCCESS
;
3495 msginfo
= (MsgInfo
*)cur
->data
;
3496 compose
= compose_reedit(msginfo
, TRUE
);
3497 if (compose
== NULL
) {
3501 val
= compose_queue(compose
, NULL
, &queue
, NULL
,
3503 if (val
!= COMPOSE_QUEUE_SUCCESS
) {
3505 } else if (remove_source
) {
3506 folder_item_remove_msg(msginfo
->folder
, msginfo
->msgnum
);
3508 if (val
== COMPOSE_QUEUE_SUCCESS
)
3509 compose_close(compose
);
3511 return queue_err
? -1:0;
3514 relation
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
3516 for (l
= msglist
; l
!= NULL
; l
= g_slist_next(l
)) {
3517 MsgInfo
* msginfo
= (MsgInfo
*) l
->data
;
3519 if (msginfo
->planned_download
!= 0) {
3520 int old_planned
= msginfo
->planned_download
;
3521 partial_unmark(msginfo
);
3522 /* little hack to reenable after */
3523 msginfo
->planned_download
= old_planned
;
3528 * Copy messages to destination folder and
3529 * store new message numbers in newmsgnums
3531 if (folder
->klass
->copy_msgs
!= NULL
) {
3532 if (folder
->klass
->copy_msgs(folder
, dest
, msglist
, relation
) < 0) {
3533 g_hash_table_destroy(relation
);
3540 /* immediately stop if src and dest folders are identical */
3542 msginfo
= (MsgInfo
*) l
->data
;
3543 if (msginfo
!= NULL
&& msginfo
->folder
== dest
) {
3544 g_hash_table_destroy(relation
);
3549 for (; l
!= NULL
; l
= g_slist_next(l
)) {
3550 msginfo
= (MsgInfo
*) l
->data
;
3552 num
= folder
->klass
->copy_msg(folder
, dest
, msginfo
);
3554 g_hash_table_insert(relation
, msginfo
, GINT_TO_POINTER(num
));
3556 not_moved
= g_slist_prepend(not_moved
, msginfo
);
3560 if (remove_source
) {
3561 MsgInfo
*msginfo
= (MsgInfo
*) msglist
->data
;
3562 FolderItem
*item
= msginfo
->folder
;
3564 * Remove source messages from their folders if
3565 * copying was successfull and update folder
3568 if (not_moved
== NULL
&& item
->folder
->klass
->remove_msgs
) {
3569 item
->folder
->klass
->remove_msgs(item
->folder
,
3574 for (l
= msglist
; l
!= NULL
; l
= g_slist_next(l
)) {
3575 gpointer old_key
, data
;
3576 msginfo
= (MsgInfo
*) l
->data
;
3577 item
= msginfo
->folder
;
3579 if (g_hash_table_lookup_extended(relation
, msginfo
, &old_key
, &data
))
3580 num
= GPOINTER_TO_INT(data
);
3584 if (g_slist_find(not_moved
, msginfo
))
3587 if ((num
>= 0) && (item
->folder
->klass
->remove_msg
!= NULL
)) {
3588 if (!item
->folder
->klass
->remove_msgs
)
3589 item
->folder
->klass
->remove_msg(item
->folder
,
3592 remove_msginfo_from_cache(item
, msginfo
);
3597 /* Read cache for dest folder */
3598 if (!dest
->cache
) folder_item_read_cache(dest
);
3601 * Fetch new MsgInfos for new messages in dest folder,
3602 * add them to the msgcache and update folder message counts
3604 if (some_msgs_have_zero_num(relation
)) {
3605 folder_item_scan_full(dest
, FALSE
);
3609 statusbar_print_all(_("Updating cache for %s..."), dest
->path
? dest
->path
: "(null)");
3610 total
= g_slist_length(msglist
);
3612 if (FOLDER_TYPE(dest
->folder
) == F_IMAP
&& total
> 1) {
3613 folder_item_scan_full(dest
, FALSE
);
3616 folder_item_set_batch(dest
, TRUE
);
3617 for (l
= msglist
; l
!= NULL
; l
= g_slist_next(l
)) {
3618 MsgInfo
*msginfo
= (MsgInfo
*) l
->data
;
3619 gpointer data
, old_key
;
3624 if (g_hash_table_lookup_extended(relation
, msginfo
, &old_key
, &data
))
3625 num
= GPOINTER_TO_INT(data
);
3629 statusbar_progress_all(curmsg
++,total
, 100);
3630 if (curmsg
% 100 == 0)
3634 MsgInfo
*newmsginfo
= NULL
;
3636 if (!folderscan
&& num
> 0) {
3637 newmsginfo
= get_msginfo(dest
, num
);
3638 if (newmsginfo
!= NULL
) {
3639 add_msginfo_to_cache(dest
, newmsginfo
, msginfo
);
3642 if (newmsginfo
== NULL
) {
3644 folder_item_scan_full(dest
, FALSE
);
3647 if (msginfo
->msgid
!= NULL
) {
3648 newmsginfo
= folder_item_get_msginfo_by_msgid(dest
, msginfo
->msgid
);
3649 if (newmsginfo
!= NULL
) {
3650 copy_msginfo_flags(msginfo
, newmsginfo
);
3651 num
= newmsginfo
->msgnum
;
3655 if (newmsginfo
!= NULL
3656 && msginfo
->planned_download
== POP3_PARTIAL_DLOAD_DELE
) {
3657 partial_mark_for_delete(newmsginfo
);
3659 if (newmsginfo
!= NULL
3660 && msginfo
->planned_download
== POP3_PARTIAL_DLOAD_DLOAD
) {
3661 partial_mark_for_download(newmsginfo
);
3663 if (!MSG_IS_POSTFILTERED (msginfo
->flags
)) {
3664 procmsg_msginfo_set_flags ( msginfo
, MSG_POSTFILTERED
, 0);
3666 procmsg_msginfo_set_flags (newmsginfo
, MSG_POSTFILTERED
, 0);
3667 hooks_invoke (MAIL_POSTFILTERING_HOOKLIST
, newmsginfo
);
3670 procmsg_msginfo_free(&newmsginfo
);
3677 folder_item_set_batch(dest
, FALSE
);
3678 statusbar_progress_all(0,0,0);
3679 statusbar_pop_all();
3681 g_hash_table_destroy(relation
);
3682 if (not_moved
!= NULL
) {
3683 g_slist_free(not_moved
);
3690 * Move a message to a new folder.
3692 * \param dest Destination folder
3693 * \param msginfo The message
3695 gint
folder_item_move_msg(FolderItem
*dest
, MsgInfo
*msginfo
)
3699 cm_return_val_if_fail(dest
!= NULL
, -1);
3700 cm_return_val_if_fail(msginfo
!= NULL
, -1);
3702 list
.data
= msginfo
;
3705 return do_copy_msgs(dest
, &list
, TRUE
);
3709 * Move a list of messages to a new folder.
3711 * \param dest Destination folder
3712 * \param msglist List of messages
3714 gint
folder_item_move_msgs(FolderItem
*dest
, GSList
*msglist
)
3717 cm_return_val_if_fail(dest
!= NULL
, -1);
3718 cm_return_val_if_fail(msglist
!= NULL
, -1);
3720 result
= do_copy_msgs(dest
, msglist
, TRUE
);
3726 * Copy a message to a new folder.
3728 * \param dest Destination folder
3729 * \param msginfo The message
3731 gint
folder_item_copy_msg(FolderItem
*dest
, MsgInfo
*msginfo
)
3735 cm_return_val_if_fail(dest
!= NULL
, -1);
3736 cm_return_val_if_fail(msginfo
!= NULL
, -1);
3738 list
.data
= msginfo
;
3741 return do_copy_msgs(dest
, &list
, FALSE
);
3745 * Copy a list of messages to a new folder.
3747 * \param dest Destination folder
3748 * \param msglist List of messages
3750 gint
folder_item_copy_msgs(FolderItem
*dest
, GSList
*msglist
)
3753 cm_return_val_if_fail(dest
!= NULL
, -1);
3754 cm_return_val_if_fail(msglist
!= NULL
, -1);
3757 result
= do_copy_msgs(dest
, msglist
, FALSE
);
3763 gint
folder_item_remove_msg(FolderItem
*item
, gint num
)
3769 cm_return_val_if_fail(item
!= NULL
, -1);
3770 folder
= item
->folder
;
3771 cm_return_val_if_fail(folder
->klass
->remove_msg
!= NULL
, -1);
3772 if (item
->no_select
)
3775 if (!item
->cache
) folder_item_read_cache(item
);
3777 msginfo
= msgcache_get_msg(item
->cache
, num
);
3778 if (msginfo
&& MSG_IS_LOCKED(msginfo
->flags
)) {
3779 procmsg_msginfo_free(&msginfo
);
3782 ret
= folder
->klass
->remove_msg(folder
, item
, num
);
3784 if (msginfo
!= NULL
) {
3786 remove_msginfo_from_cache(item
, msginfo
);
3787 procmsg_msginfo_free(&msginfo
);
3793 gint
folder_item_remove_msgs(FolderItem
*item
, GSList
*msglist
)
3797 GSList
*real_list
= NULL
, *cur
= NULL
;
3799 cm_return_val_if_fail(item
!= NULL
, -1);
3800 folder
= item
->folder
;
3801 cm_return_val_if_fail(folder
!= NULL
, -1);
3802 if (item
->no_select
)
3805 if (!item
->cache
) folder_item_read_cache(item
);
3807 folder_item_update_freeze();
3809 /* filter out locked mails */
3810 for (cur
= msglist
; cur
; cur
= cur
->next
) {
3811 MsgInfo
*info
= (MsgInfo
*)cur
->data
;
3812 if (!MSG_IS_LOCKED(info
->flags
))
3813 real_list
= g_slist_prepend(real_list
, info
);
3816 real_list
= g_slist_reverse(real_list
);
3818 if (item
->folder
->klass
->remove_msgs
) {
3819 ret
= item
->folder
->klass
->remove_msgs(item
->folder
,
3825 while (ret
== 0 && cur
!= NULL
) {
3826 MsgInfo
*msginfo
= (MsgInfo
*)cur
->data
;
3827 if (msginfo
&& MSG_IS_LOCKED(msginfo
->flags
)) {
3831 if (!item
->folder
->klass
->remove_msgs
)
3832 ret
= folder_item_remove_msg(item
, msginfo
->msgnum
);
3833 if (ret
!= 0) break;
3834 msgcache_remove_msg(item
->cache
, msginfo
->msgnum
);
3837 g_slist_free(real_list
);
3838 folder_item_scan_full(item
, FALSE
);
3839 folder_item_update_thaw();
3844 gint
folder_item_expunge(FolderItem
*item
)
3846 Folder
*folder
= item
->folder
;
3850 if (folder
->klass
->expunge
) {
3851 GSList
*msglist
= folder_item_get_msg_list(item
);
3853 result
= folder
->klass
->expunge(folder
, item
);
3855 for (cur
= msglist
; cur
; cur
= cur
->next
) {
3856 MsgInfo
*msginfo
= (MsgInfo
*)cur
->data
;
3857 if (MSG_IS_DELETED(msginfo
->flags
)) {
3858 remove_msginfo_from_cache(item
, msginfo
);
3862 procmsg_msg_list_free(msglist
);
3867 gint
folder_item_remove_all_msg(FolderItem
*item
)
3872 cm_return_val_if_fail(item
!= NULL
, -1);
3873 if (item
->no_select
)
3876 folder
= item
->folder
;
3879 if (folder
->klass
->remove_all_msg
!= NULL
) {
3880 result
= folder
->klass
->remove_all_msg(folder
, item
);
3883 folder_item_free_cache(item
, TRUE
);
3884 item
->cache
= msgcache_new();
3885 item
->cache_dirty
= TRUE
;
3886 item
->mark_dirty
= TRUE
;
3887 item
->tags_dirty
= TRUE
;
3890 MsgInfoList
*msglist
;
3892 msglist
= folder_item_get_msg_list(item
);
3893 result
= folder_item_remove_msgs(item
, msglist
);
3894 procmsg_msg_list_free(msglist
);
3899 item
->unread_msgs
= 0;
3900 item
->unreadmarked_msgs
= 0;
3901 item
->marked_msgs
= 0;
3902 item
->total_msgs
= 0;
3903 item
->replied_msgs
= 0;
3904 item
->forwarded_msgs
= 0;
3905 item
->locked_msgs
= 0;
3906 item
->ignored_msgs
= 0;
3907 item
->watched_msgs
= 0;
3908 folder_item_update(item
, F_ITEM_UPDATE_MSGCNT
| F_ITEM_UPDATE_CONTENT
);
3915 void folder_item_change_msg_flags(FolderItem
*item
, MsgInfo
*msginfo
, MsgPermFlags newflags
)
3917 cm_return_if_fail(item
!= NULL
);
3918 cm_return_if_fail(msginfo
!= NULL
);
3920 item
->mark_dirty
= TRUE
;
3922 if (item
->no_select
)
3925 if (item
->folder
->klass
->change_flags
!= NULL
&& item
->scanning
!= ITEM_SCANNING_WITH_FLAGS
) {
3926 item
->folder
->klass
->change_flags(item
->folder
, item
, msginfo
, newflags
);
3928 msginfo
->flags
.perm_flags
= newflags
;
3932 void folder_item_commit_tags(FolderItem
*item
, MsgInfo
*msginfo
, GSList
*tags_set
, GSList
*tags_unset
)
3934 Folder
*folder
= NULL
;
3940 if (!tags_set
&& !tags_unset
)
3943 folder
= item
->folder
;
3947 item
->tags_dirty
= TRUE
;
3949 if (folder
->klass
->commit_tags
== NULL
)
3952 folder
->klass
->commit_tags(item
, msginfo
, tags_set
, tags_unset
);
3955 gboolean
folder_item_is_msg_changed(FolderItem
*item
, MsgInfo
*msginfo
)
3959 cm_return_val_if_fail(item
!= NULL
, FALSE
);
3960 if (item
->no_select
)
3963 folder
= item
->folder
;
3965 cm_return_val_if_fail(folder
->klass
->is_msg_changed
!= NULL
, -1);
3967 return folder
->klass
->is_msg_changed(folder
, item
, msginfo
);
3970 void folder_item_discard_cache(FolderItem
*item
)
3979 msgcache_destroy(item
->cache
);
3982 dir
= folder_item_get_path(item
);
3983 if (is_dir_exist(dir
))
3984 remove_all_numbered_files(dir
);
3987 cache
= folder_item_get_cache_file(item
);
3988 if (is_file_exist(cache
))
3989 claws_unlink(cache
);
3994 static gchar
*folder_item_get_cache_file(FolderItem
*item
)
4000 cm_return_val_if_fail(item
!= NULL
, NULL
);
4002 path
= folder_item_get_path(item
);
4003 cm_return_val_if_fail(path
!= NULL
, NULL
);
4004 if (!is_dir_exist(path
))
4005 make_dir_hier(path
);
4006 file
= g_strconcat(path
, G_DIR_SEPARATOR_S
, CACHE_FILE
, NULL
);
4007 old_file
= g_strconcat(path
, G_DIR_SEPARATOR_S
, OLD_CACHE_FILE
, NULL
);
4009 if (!is_file_exist(file
) && is_file_exist(old_file
))
4010 move_file(old_file
, file
, FALSE
);
4017 static gchar
*folder_item_get_mark_file(FolderItem
*item
)
4023 cm_return_val_if_fail(item
!= NULL
, NULL
);
4024 cm_return_val_if_fail(item
->path
!= NULL
, NULL
);
4026 path
= folder_item_get_path(item
);
4027 cm_return_val_if_fail(path
!= NULL
, NULL
);
4028 if (!is_dir_exist(path
))
4029 make_dir_hier(path
);
4030 file
= g_strconcat(path
, G_DIR_SEPARATOR_S
, MARK_FILE
, NULL
);
4031 old_file
= g_strconcat(path
, G_DIR_SEPARATOR_S
, OLD_MARK_FILE
, NULL
);
4033 if (!is_file_exist(file
) && is_file_exist(old_file
))
4034 move_file(old_file
, file
, FALSE
);
4041 static gchar
*folder_item_get_tags_file(FolderItem
*item
)
4047 /* we save tags files in rc_dir, because tagsrc is there too,
4048 * and storing tags directly in the mailboxes would give strange
4049 * result when using another Claws mailbox from another install
4050 * with different tags. */
4052 cm_return_val_if_fail(item
!= NULL
, NULL
);
4054 identifier
= folder_item_get_identifier(item
);
4055 cm_return_val_if_fail(identifier
!= NULL
, NULL
);
4058 while (strchr(identifier
, '/'))
4059 *strchr(identifier
, '/') = '\\';
4062 path
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
,
4063 TAGS_DIR
, G_DIR_SEPARATOR_S
,
4068 if (!is_dir_exist(path
))
4069 make_dir_hier(path
);
4071 file
= g_strconcat(path
, G_DIR_SEPARATOR_S
, TAGS_FILE
, NULL
);
4078 static gpointer
xml_to_folder_item(gpointer nodedata
, gpointer data
)
4080 XMLNode
*xmlnode
= (XMLNode
*) nodedata
;
4081 Folder
*folder
= (Folder
*) data
;
4084 cm_return_val_if_fail(xmlnode
!= NULL
, NULL
);
4085 cm_return_val_if_fail(folder
!= NULL
, NULL
);
4087 if (g_strcmp0(xmlnode
->tag
->tag
, "folderitem") != 0) {
4088 g_warning("tag name != \"folderitem\"");
4092 item
= folder_item_new(folder
, "", "");
4093 g_node_destroy(item
->node
);
4094 if (folder
->klass
->item_set_xml
!= NULL
)
4095 folder
->klass
->item_set_xml(folder
, item
, xmlnode
->tag
);
4097 folder_item_set_xml(folder
, item
, xmlnode
->tag
);
4099 item
->folder
= folder
;
4101 switch (item
->stype
) {
4102 case F_INBOX
: folder
->inbox
= item
; break;
4103 case F_OUTBOX
: folder
->outbox
= item
; break;
4104 case F_DRAFT
: folder
->draft
= item
; break;
4105 case F_QUEUE
: folder
->queue
= item
; break;
4106 case F_TRASH
: folder
->trash
= item
; break;
4109 folder_item_prefs_read_config(item
);
4114 static gboolean
folder_item_set_node(GNode
*node
, gpointer data
)
4116 cm_return_val_if_fail(node
->data
!= NULL
, -1);
4118 FolderItem
*item
= (FolderItem
*) node
->data
;
4124 static Folder
*folder_get_from_xml(GNode
*node
)
4129 FolderClass
*klass
= NULL
;
4132 cm_return_val_if_fail(node
->data
!= NULL
, NULL
);
4134 xmlnode
= node
->data
;
4135 if (g_strcmp0(xmlnode
->tag
->tag
, "folder") != 0) {
4136 g_warning("tag name != \"folder\"");
4139 list
= xmlnode
->tag
->attr
;
4140 for (; list
!= NULL
; list
= list
->next
) {
4141 XMLAttr
*attr
= list
->data
;
4143 if (!attr
|| !attr
->name
|| !attr
->value
) continue;
4144 if (!strcmp(attr
->name
, "type"))
4145 klass
= folder_get_class_from_string(attr
->value
);
4150 folder
= folder_new(klass
, "", "");
4151 cm_return_val_if_fail(folder
!= NULL
, NULL
);
4154 klass
->set_xml(folder
, xmlnode
->tag
);
4156 folder_set_xml(folder
, xmlnode
->tag
);
4158 cur
= node
->children
;
4159 while (cur
!= NULL
) {
4162 itemnode
= g_node_map(cur
, xml_to_folder_item
, (gpointer
) folder
);
4163 g_node_append(folder
->node
, itemnode
);
4166 g_node_traverse(folder
->node
, G_IN_ORDER
, G_TRAVERSE_ALL
, -1, folder_item_set_node
, NULL
);
4171 gchar
*folder_get_list_path(void)
4173 static gchar
*filename
= NULL
;
4176 filename
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
,
4182 static gpointer
folder_item_to_xml(gpointer nodedata
, gpointer data
)
4184 FolderItem
*item
= (FolderItem
*) nodedata
;
4187 cm_return_val_if_fail(item
!= NULL
, NULL
);
4189 if (item
->folder
->klass
->item_get_xml
!= NULL
)
4190 tag
= item
->folder
->klass
->item_get_xml(item
->folder
, item
);
4192 tag
= folder_item_get_xml(item
->folder
, item
);
4194 return xml_node_new(tag
, NULL
);
4197 static GNode
*folder_get_xml_node(Folder
*folder
)
4203 cm_return_val_if_fail(folder
!= NULL
, NULL
);
4205 if (folder
->klass
->get_xml
!= NULL
)
4206 tag
= folder
->klass
->get_xml(folder
);
4208 tag
= folder_get_xml(folder
);
4210 xml_tag_add_attr(tag
, xml_attr_new("type", folder
->klass
->idstr
));
4212 xmlnode
= xml_node_new(tag
, NULL
);
4214 node
= g_node_new(xmlnode
);
4216 cm_return_val_if_fail (folder
->node
!= NULL
, NULL
);
4218 if (folder
->node
->children
) {
4221 cur
= folder
->node
->children
;
4225 xmlnode
= g_node_map(cur
, folder_item_to_xml
, (gpointer
) folder
);
4226 g_node_append(node
, xmlnode
);
4234 static void folder_update_op_count_rec(GNode
*node
)
4236 FolderItem
*fitem
= FOLDER_ITEM(node
->data
);
4238 if (g_node_depth(node
) > 0) {
4239 if (fitem
->op_count
> 0) {
4240 fitem
->op_count
= 0;
4241 folder_item_update(fitem
, F_ITEM_UPDATE_MSGCNT
);
4243 if (node
->children
) {
4246 child
= node
->children
;
4252 folder_update_op_count_rec(cur
);
4258 void folder_update_op_count(void)
4263 for (cur
= folder_list
; cur
!= NULL
; cur
= cur
->next
) {
4265 folder_update_op_count_rec(folder
->node
);
4269 typedef struct _type_str
{
4276 static gchar * folder_item_get_tree_identifier(FolderItem * item)
4278 if (item->parent != NULL) {
4282 path = folder_item_get_tree_identifier(item->parent);
4286 id = g_strconcat(path, "/", item->name, NULL);
4292 return g_strconcat("/", item->name, NULL);
4297 /* CLAWS: temporary local folder for filtering */
4298 #define TEMP_FOLDER "TEMP_FOLDER"
4299 #define PROCESSING_FOLDER_ITEM "processing"
4301 static FolderItem
*folder_create_processing_folder(int account_id
)
4303 static Folder
*processing_folder
= NULL
;
4304 FolderItem
*processing_folder_item
;
4306 gchar
*processing_folder_item_name
= NULL
;
4308 processing_folder_item_name
= g_strdup_printf("%s-%d", PROCESSING_FOLDER_ITEM
, account_id
);
4310 if (processing_folder
== NULL
) {
4312 g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
,
4313 "tempfolder", NULL
);
4315 folder_new(mh_get_class(), TEMP_FOLDER
, tmppath
);
4319 g_assert(processing_folder
!= NULL
);
4320 processing_folder
->klass
->scan_tree(processing_folder
);
4322 g_assert(processing_folder
!= NULL
);
4324 processing_folder_item
= folder_find_child_item_by_name(FOLDER_ITEM(processing_folder
->node
->data
),
4325 processing_folder_item_name
);
4326 if (processing_folder_item
) {
4327 debug_print("*TMP* already created %s\n", folder_item_get_path(processing_folder_item
));
4329 processing_folder_item
= processing_folder
->klass
->create_folder(processing_folder
,
4330 processing_folder
->node
->data
,
4331 processing_folder_item_name
);
4332 folder_item_append(FOLDER_ITEM(processing_folder
->node
->data
), processing_folder_item
);
4333 debug_print("*TMP* creating %s\n", folder_item_get_path(processing_folder_item
));
4335 g_free(processing_folder_item_name
);
4336 g_assert(processing_folder_item
!= NULL
);
4338 return(processing_folder_item
);
4341 FolderItem
*folder_get_default_processing(int account_id
)
4343 return folder_create_processing_folder(account_id
);
4346 /* folder_persist_prefs_new() - return hash table with persistent
4347 * settings (and folder name as key).
4348 * (note that in claws other options are in the folder_item_prefs_RC
4349 * file, so those don't need to be included in PersistPref yet)
4351 static GHashTable
*folder_persist_prefs_new(Folder
*folder
)
4353 GHashTable
*pptable
;
4355 cm_return_val_if_fail(folder
, NULL
);
4356 pptable
= g_hash_table_new(g_str_hash
, g_str_equal
);
4357 folder_get_persist_prefs_recursive(folder
->node
, pptable
);
4361 static void folder_persist_prefs_free(GHashTable
*pptable
)
4363 cm_return_if_fail(pptable
);
4364 g_hash_table_foreach_remove(pptable
, persist_prefs_free
, NULL
);
4365 g_hash_table_destroy(pptable
);
4368 static const PersistPrefs
*folder_get_persist_prefs(GHashTable
*pptable
, const char *name
)
4370 if (pptable
== NULL
|| name
== NULL
) return NULL
;
4371 return g_hash_table_lookup(pptable
, name
);
4374 static void folder_item_restore_persist_prefs(FolderItem
*item
, GHashTable
*pptable
)
4376 const PersistPrefs
*pp
;
4377 gchar
*id
= folder_item_get_identifier(item
);
4379 pp
= folder_get_persist_prefs(pptable
, id
);
4384 /* CLAWS: since not all folder properties have been migrated to
4385 * folderlist.xml, we need to call the old stuff first before
4386 * setting things that apply both to Main and Claws. */
4387 folder_item_prefs_read_config(item
);
4389 item
->collapsed
= pp
->collapsed
;
4390 item
->thread_collapsed
= pp
->thread_collapsed
;
4391 item
->threaded
= pp
->threaded
;
4392 item
->ret_rcpt
= pp
->ret_rcpt
;
4393 item
->hide_read_msgs
= pp
->hide_read_msgs
;
4394 item
->hide_del_msgs
= pp
->hide_del_msgs
;
4395 item
->hide_read_threads
= pp
->hide_read_threads
;
4396 item
->sort_key
= pp
->sort_key
;
4397 item
->sort_type
= pp
->sort_type
;
4400 static void folder_get_persist_prefs_recursive(GNode
*node
, GHashTable
*pptable
)
4402 FolderItem
*item
= FOLDER_ITEM(node
->data
);
4407 cm_return_if_fail(node
!= NULL
);
4408 cm_return_if_fail(item
!= NULL
);
4410 /* NOTE: item->path == NULL means top level folder; not interesting
4411 * to store preferences of that one. */
4413 id
= folder_item_get_identifier(item
);
4414 pp
= g_new0(PersistPrefs
, 1);
4419 pp
->collapsed
= item
->collapsed
;
4420 pp
->thread_collapsed
= item
->thread_collapsed
;
4421 pp
->threaded
= item
->threaded
;
4422 pp
->ret_rcpt
= item
->ret_rcpt
;
4423 pp
->hide_read_msgs
= item
->hide_read_msgs
;
4424 pp
->hide_del_msgs
= item
->hide_del_msgs
;
4425 pp
->hide_read_threads
= item
->hide_read_threads
;
4426 pp
->sort_key
= item
->sort_key
;
4427 pp
->sort_type
= item
->sort_type
;
4428 g_hash_table_insert(pptable
, id
, pp
);
4431 if (node
->children
) {
4432 child
= node
->children
;
4436 folder_get_persist_prefs_recursive(cur
, pptable
);
4441 static gboolean
persist_prefs_free(gpointer key
, gpointer val
, gpointer data
)
4448 void folder_item_apply_processing(FolderItem
*item
)
4450 GSList
*processing_list
;
4451 GSList
*mlist
, *cur
;
4452 guint total
= 0, curmsg
= 0;
4453 gint last_apply_per_account
;
4455 cm_return_if_fail(item
!= NULL
);
4457 if (item
->no_select
)
4460 processing_list
= item
->prefs
->processing
;
4462 if (!processing_enabled(pre_global_processing
) &&
4463 !processing_enabled(processing_list
) &&
4464 !processing_enabled(post_global_processing
))
4467 debug_print("processing %s\n", item
->name
);
4468 folder_item_update_freeze();
4472 mlist
= folder_item_get_msg_list(item
);
4473 total
= g_slist_length(mlist
);
4474 statusbar_print_all(_("Processing messages..."));
4476 last_apply_per_account
= prefs_common
.apply_per_account_filtering_rules
;
4477 prefs_common
.apply_per_account_filtering_rules
= FILTERING_ACCOUNT_RULES_SKIP
;
4479 folder_item_set_batch(item
, TRUE
);
4480 for (cur
= mlist
; cur
!= NULL
; cur
= cur
->next
) {
4483 msginfo
= (MsgInfo
*) cur
->data
;
4485 /* reset parameters that can be modified by processing */
4486 msginfo
->hidden
= 0;
4489 statusbar_progress_all(curmsg
++,total
, 10);
4491 /* apply pre global rules */
4492 filter_message_by_msginfo(pre_global_processing
, msginfo
, NULL
,
4493 FILTERING_PRE_PROCESSING
, NULL
);
4495 /* apply rules of the folder */
4496 filter_message_by_msginfo(processing_list
, msginfo
, NULL
,
4497 FILTERING_FOLDER_PROCESSING
, item
->name
);
4499 /* apply post global rules */
4500 filter_message_by_msginfo(post_global_processing
, msginfo
, NULL
,
4501 FILTERING_POST_PROCESSING
, NULL
);
4502 if (curmsg
% 1000 == 0)
4505 folder_item_set_batch(item
, FALSE
);
4507 prefs_common
.apply_per_account_filtering_rules
= last_apply_per_account
;
4509 if (pre_global_processing
|| processing_list
4510 || post_global_processing
)
4511 filtering_move_and_copy_msgs(mlist
);
4512 for (cur
= mlist
; cur
!= NULL
; cur
= cur
->next
) {
4513 procmsg_msginfo_free((MsgInfo
**)&(cur
->data
));
4515 g_slist_free(mlist
);
4517 statusbar_progress_all(0,0,0);
4518 statusbar_pop_all();
4522 folder_item_update_thaw();
4526 * functions for handling FolderItem content changes
4528 static gint folder_item_update_freeze_cnt
= 0;
4530 static void folder_item_update_with_msg(FolderItem
*item
, FolderItemUpdateFlags update_flags
, MsgInfo
*msg
)
4532 if (folder_item_update_freeze_cnt
== 0 /* || (msg != NULL && item->opened) */) {
4533 FolderItemUpdateData source
;
4536 source
.update_flags
= update_flags
;
4538 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST
, &source
);
4540 item
->update_flags
|= update_flags
& ~(F_ITEM_UPDATE_ADDMSG
| F_ITEM_UPDATE_REMOVEMSG
);
4545 * Notify the folder system about changes to a folder. If the
4546 * update system is not frozen the FOLDER_ITEM_UPDATE_HOOKLIST will
4547 * be invoked, otherwise the changes will be remebered until
4548 * the folder system is thawed.
4550 * \param item The FolderItem that was changed
4551 * \param update_flags Type of changed that was made
4553 void folder_item_update(FolderItem
*item
, FolderItemUpdateFlags update_flags
)
4555 folder_item_update_with_msg(item
, update_flags
, NULL
);
4558 void folder_item_update_recursive(FolderItem
*item
, FolderItemUpdateFlags update_flags
)
4560 GNode
*node
= item
->folder
->node
;
4562 node
= g_node_find(node
, G_PRE_ORDER
, G_TRAVERSE_ALL
, item
);
4563 node
= node
->children
;
4565 folder_item_update(item
, update_flags
);
4566 while (node
!= NULL
) {
4567 if (node
&& node
->data
) {
4568 FolderItem
*next_item
= (FolderItem
*) node
->data
;
4570 folder_item_update(next_item
, update_flags
);
4576 void folder_item_update_freeze(void)
4578 folder_item_update_freeze_cnt
++;
4581 static void folder_item_update_func(FolderItem
*item
, gpointer data
)
4583 FolderItemUpdateData source
;
4585 if (item
->update_flags
) {
4587 source
.update_flags
= item
->update_flags
;
4589 hooks_invoke(FOLDER_ITEM_UPDATE_HOOKLIST
, &source
);
4590 item
->update_flags
= 0;
4594 void folder_item_update_thaw(void)
4596 if (folder_item_update_freeze_cnt
> 0)
4597 folder_item_update_freeze_cnt
--;
4598 if (folder_item_update_freeze_cnt
== 0) {
4599 /* Update all folders */
4600 folder_func_to_all_folders(folder_item_update_func
, NULL
);
4604 void folder_item_synchronise(FolderItem
*item
)
4608 if (item
->prefs
->offlinesync
&& item
->folder
->klass
->synchronise
) {
4609 statusbar_print_all(_("Synchronising %s for offline use...\n"), item
->path
? item
->path
: "(null)");
4610 item
->folder
->klass
->synchronise(item
,
4611 item
->prefs
->offlinesync_days
);
4612 if (item
->prefs
->offlinesync_days
> 0 &&
4613 item
->prefs
->remove_old_bodies
)
4614 folder_item_clean_local_files(item
, item
->prefs
->offlinesync_days
);
4615 statusbar_pop_all();
4619 static void folder_item_synchronise_func(FolderItem
*item
, gpointer data
)
4621 Folder
*folder
= (Folder
*)data
;
4622 if (folder
== NULL
|| item
->folder
== folder
) {
4623 folder_item_synchronise(item
);
4627 void folder_synchronise(Folder
*folder
)
4629 folder_func_to_all_folders(folder_item_synchronise_func
, folder
);
4632 typedef struct _WantSyncData
{
4637 static void folder_item_want_synchronise_func(FolderItem
*item
, gpointer data
)
4639 WantSyncData
*want_sync_data
= (WantSyncData
*)data
;
4641 if (want_sync_data
->folder
== NULL
|| item
->folder
== want_sync_data
->folder
) {
4642 if (item
->prefs
->offlinesync
&& item
->folder
->klass
->synchronise
)
4643 want_sync_data
->want_sync
|= TRUE
;
4647 gboolean
folder_want_synchronise(Folder
*folder
)
4649 WantSyncData
*want_sync_data
= g_new0(WantSyncData
, 1);
4651 want_sync_data
->folder
= folder
;
4652 want_sync_data
->want_sync
= FALSE
;
4654 folder_func_to_all_folders(folder_item_want_synchronise_func
, want_sync_data
);
4655 result
= want_sync_data
->want_sync
;
4656 g_free(want_sync_data
);
4658 debug_print("Folder %s wants sync\n", folder
->name
);
4662 void folder_item_set_batch (FolderItem
*item
, gboolean batch
)
4664 if (!item
|| !item
->folder
)
4666 if (item
->folder
->klass
->set_batch
) {
4667 item
->folder
->klass
->set_batch(item
->folder
, item
, batch
);
4671 gboolean
folder_has_parent_of_type(FolderItem
*item
,
4672 SpecialFolderItemType type
)
4674 FolderItem
*cur
= item
;
4678 /* if we already know it, make it short */
4679 if (item
->parent_stype
!= -1) {
4680 return (item
->parent_stype
== type
);
4683 /* if we don't, find the type from the first possible parent,
4684 * and set our parent type to be faster next time */
4686 if (cur
->stype
== type
|| cur
->parent_stype
== type
) {
4687 item
->parent_stype
= type
;
4690 cur
= folder_item_parent(cur
);
4693 /* if we didn't match what was asked, we didn't return. If our
4694 * parent type is unknown, we may as well find it now to be faster
4696 if (item
->parent_stype
== -1) {
4699 /* here's an exception: Inbox subfolders are normal. */
4700 if (item
->parent_stype
== -1 && cur
->stype
== F_INBOX
4702 item
->parent_stype
= F_NORMAL
;
4705 /* ah, we know this parent's parent's type, we may as
4706 * well copy it instead of going up the full way */
4707 if (cur
->parent_stype
!= -1) {
4708 item
->parent_stype
= cur
->parent_stype
;
4711 /* we found a parent that has a special type. That's
4712 * our parent type. */
4713 if (cur
->stype
!= F_NORMAL
) {
4714 cur
->parent_stype
= cur
->stype
;
4715 item
->parent_stype
= cur
->stype
;
4718 /* if we didn't find anything, go up once more */
4719 cur
= folder_item_parent(cur
);
4721 /* as we still didn't find anything, our parents must all be
4723 if (item
->parent_stype
== -1) {
4724 item
->parent_stype
= F_NORMAL
;
4730 gboolean
folder_is_child_of(FolderItem
*item
, FolderItem
*parent
)
4732 if (item
== NULL
|| parent
== NULL
)
4735 while (item
!= NULL
) {
4739 item
= folder_item_parent(item
);
4746 gboolean
folder_subscribe (const gchar
*uri
)
4749 for (cur
= folder_get_list(); cur
!= NULL
; cur
= g_list_next(cur
)) {
4750 Folder
*folder
= (Folder
*) cur
->data
;
4752 if (folder
->klass
->subscribe
4753 && folder
->klass
->subscribe(folder
, uri
)) {
4761 gboolean
folder_get_sort_type (Folder
*folder
,
4762 FolderSortKey
*sort_key
,
4763 FolderSortType
*sort_type
)
4765 if (!folder
|| !sort_key
|| !sort_type
)
4767 if (folder
->klass
->get_sort_type
== NULL
)
4769 folder
->klass
->get_sort_type(folder
, sort_key
, sort_type
);
4773 gint
folder_item_search_msgs (Folder
*folder
,
4774 FolderItem
*container
,
4775 MsgNumberList
**msgs
,
4776 gboolean
*on_server
,
4777 MatcherList
*predicate
,
4778 SearchProgressNotify progress_cb
,
4779 gpointer progress_data
)
4783 folder_item_update_freeze();
4785 if (folder
->klass
->search_msgs
)
4786 result
= folder
->klass
->search_msgs(folder
, container
,
4787 msgs
, on_server
, predicate
, progress_cb
, progress_data
);
4789 result
= folder_item_search_msgs_local(folder
, container
,
4790 msgs
, on_server
, predicate
, progress_cb
, progress_data
);
4792 folder_item_update_thaw();
4797 MsgNumberList
*folder_item_get_number_list(FolderItem
*item
)
4799 GSList
*nums
= NULL
;
4800 GSList
*msglist
= folder_item_get_msg_list(item
);
4802 nums
= procmsg_get_number_list_for_msgs(msglist
);
4803 procmsg_msg_list_free(msglist
);
4808 gint
folder_item_search_msgs_local (Folder
*folder
,
4809 FolderItem
*container
,
4810 MsgNumberList
**msgs
,
4811 gboolean
*on_server
,
4812 MatcherList
*predicate
,
4813 SearchProgressNotify progress_cb
,
4814 gpointer progress_data
)
4816 GSList
*result
= NULL
;
4818 gint matched_count
= 0;
4819 guint processed_count
= 0;
4821 GSList
*nums
= NULL
;
4823 if (*msgs
== NULL
) {
4824 nums
= folder_item_get_number_list(container
);
4829 msgcount
= g_slist_length(nums
);
4834 for (cur
= nums
; cur
!= NULL
; cur
= cur
->next
) {
4835 guint msgnum
= GPOINTER_TO_UINT(cur
->data
);
4836 MsgInfo
*msg
= folder_item_get_msginfo(container
, msgnum
);
4839 g_slist_free(result
);
4843 if (matcherlist_match(predicate
, msg
)) {
4844 result
= g_slist_prepend(result
, GUINT_TO_POINTER(msg
->msgnum
));
4849 procmsg_msginfo_free(&msg
);
4851 if (progress_cb
!= NULL
4852 && !progress_cb(progress_data
, FALSE
, processed_count
,
4853 matched_count
, msgcount
))
4858 *msgs
= g_slist_reverse(result
);
4860 return matched_count
;
4863 /* Tests if a local (on disk) folder name is acceptable. */
4864 gboolean
folder_local_name_ok(const gchar
*name
)
4867 if (name
[0] == '.' || name
[strlen(name
) - 1] == '.') {
4868 alertpanel_error(_("A folder name cannot begin or end with a dot."));
4871 if (name
[strlen(name
) - 1] == ' ') {
4872 alertpanel_error(_("A folder name can not end with a space."));