Updated Spanish translation
[evolution.git] / libemail-engine / mail-vfolder.c
blobaed9668c4c7b046d38d001489a9df156e2ac8ebd
1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2 of the License, or (at your option) version 3.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with the program; if not, see <http://www.gnu.org/licenses/>
16 * Authors:
17 * Michael Zucchi <notzed@ximian.com>
19 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
27 #include <string.h>
29 #include <glib/gi18n.h>
31 #include "libevolution-utils/e-alert-dialog.h"
33 #include "libemail-utils/mail-mt.h"
34 #include "libemail-engine/mail-folder-cache.h"
35 #include "libemail-engine/e-mail-folder-utils.h"
36 #include "libemail-engine/e-mail-session.h"
37 #include "libemail-engine/e-mail-utils.h"
38 #include "libemail-engine/mail-ops.h"
39 #include "libemail-engine/mail-tools.h"
41 #include <libemail-utils/em-vfolder-context.h>
42 #include <libemail-utils/em-vfolder-rule.h>
43 #include "mail-vfolder.h"
45 #define d(x) /* (printf("%s:%s: ", G_STRLOC, G_STRFUNC), (x))*/
47 /* Note: Once we completely move mail to EDS, this context wont be available for UI.
48 * and vfoldertypes.xml should be moved here really. */
49 EMVFolderContext *context; /* context remains open all time */
51 /* lock for accessing shared resources (below) */
52 G_LOCK_DEFINE_STATIC (vfolder);
54 static GHashTable *vfolder_hash;
55 /* This is a slightly hacky solution to shutting down, we poll this variable in various
56 * loops, and just quit processing if it is set. */
57 static volatile gint vfolder_shutdown; /* are we shutting down? */
59 static void rule_changed (EFilterRule *rule, CamelFolder *folder);
61 /* ********************************************************************** */
63 struct _setup_msg {
64 MailMsg base;
66 EMailSession *session;
67 CamelFolder *folder;
68 gchar *query;
69 GList *sources_uri;
70 GList *sources_folder;
73 static gchar *
74 vfolder_setup_desc (struct _setup_msg *m)
76 return g_strdup_printf (
77 _("Setting up Search Folder: %s"),
78 camel_folder_get_full_name (m->folder));
81 static void
82 vfolder_setup_exec (struct _setup_msg *m,
83 GCancellable *cancellable,
84 GError **error)
86 GList *l, *list = NULL;
87 CamelFolder *folder;
89 camel_vee_folder_set_expression ((CamelVeeFolder *) m->folder, m->query);
91 l = m->sources_uri;
92 while (l && !vfolder_shutdown) {
93 d(printf(" Adding uri: %s\n", (gchar *)l->data));
95 /* FIXME Not passing a GCancellable or GError here. */
96 folder = e_mail_session_uri_to_folder_sync (
97 m->session, l->data, 0, NULL, NULL);
98 if (folder != NULL)
99 list = g_list_append (list, folder);
100 l = l->next;
103 l = m->sources_folder;
104 while (l && !vfolder_shutdown) {
105 g_object_ref (l->data);
106 list = g_list_append (list, l->data);
107 l = l->next;
110 if (!vfolder_shutdown)
111 camel_vee_folder_set_folders ((CamelVeeFolder *) m->folder, list);
113 l = list;
114 while (l) {
115 g_object_unref (l->data);
116 l = l->next;
118 g_list_free (list);
121 static void
122 vfolder_setup_done (struct _setup_msg *m)
126 static void
127 vfolder_setup_free (struct _setup_msg *m)
129 GList *l;
131 g_object_unref (m->session);
132 g_object_unref (m->folder);
133 g_free (m->query);
135 l = m->sources_uri;
136 while (l) {
137 g_free (l->data);
138 l = l->next;
140 g_list_free (m->sources_uri);
142 l = m->sources_folder;
143 while (l) {
144 g_object_unref (l->data);
145 l = l->next;
147 g_list_free (m->sources_folder);
150 static MailMsgInfo vfolder_setup_info = {
151 sizeof (struct _setup_msg),
152 (MailMsgDescFunc) vfolder_setup_desc,
153 (MailMsgExecFunc) vfolder_setup_exec,
154 (MailMsgDoneFunc) vfolder_setup_done,
155 (MailMsgFreeFunc) vfolder_setup_free
158 /* sources_uri should be camel uri's */
159 static gint
160 vfolder_setup (EMailSession *session,
161 CamelFolder *folder,
162 const gchar *query,
163 GList *sources_uri,
164 GList *sources_folder)
166 struct _setup_msg *m;
167 gint id;
169 m = mail_msg_new (&vfolder_setup_info);
170 m->session = g_object_ref (session);
171 m->folder = g_object_ref (folder);
172 m->query = g_strdup (query);
173 m->sources_uri = sources_uri;
174 m->sources_folder = sources_folder;
176 id = m->base.seq;
177 mail_msg_slow_ordered_push (m);
179 return id;
182 /* ********************************************************************** */
184 struct _adduri_msg {
185 MailMsg base;
187 EMailSession *session;
188 gchar *uri;
189 GList *folders;
190 gint remove;
193 static gchar *
194 vfolder_adduri_desc (struct _adduri_msg *m)
196 CamelStore *store;
197 CamelService *service;
198 const gchar *display_name;
199 gchar *folder_name;
200 gchar *description;
201 gboolean success;
203 success = e_mail_folder_uri_parse (
204 CAMEL_SESSION (m->session), m->uri,
205 &store, &folder_name, NULL);
207 if (!success)
208 return NULL;
210 service = CAMEL_SERVICE (store);
211 display_name = camel_service_get_display_name (service);
213 description = g_strdup_printf (
214 _("Updating Search Folders for '%s' - %s"),
215 display_name, folder_name);
217 g_object_unref (store);
218 g_free (folder_name);
220 return description;
223 static void
224 vfolder_adduri_exec (struct _adduri_msg *m,
225 GCancellable *cancellable,
226 GError **error)
228 GList *l;
229 CamelFolder *folder = NULL;
230 MailFolderCache *folder_cache;
232 if (vfolder_shutdown)
233 return;
235 folder_cache = e_mail_session_get_folder_cache (m->session);
237 /* we dont try lookup the cache if we are removing it, its no longer there */
239 if (!m->remove &&
240 !mail_folder_cache_get_folder_from_uri (folder_cache, m->uri, NULL)) {
241 g_warning (
242 "Folder '%s' disappeared while I was "
243 "adding/removing it to/from my vfolder", m->uri);
244 return;
247 /* always pick fresh folders - they are
248 * from CamelStore's folders bag anyway */
249 folder = e_mail_session_uri_to_folder_sync (
250 m->session, m->uri, 0, cancellable, error);
252 if (folder != NULL) {
253 l = m->folders;
254 while (l && !vfolder_shutdown) {
255 if (m->remove)
256 camel_vee_folder_remove_folder (
257 CAMEL_VEE_FOLDER (l->data), folder);
258 else
259 camel_vee_folder_add_folder ((CamelVeeFolder *) l->data, folder);
260 l = l->next;
262 g_object_unref (folder);
266 static void
267 vfolder_adduri_done (struct _adduri_msg *m)
271 static void
272 vfolder_adduri_free (struct _adduri_msg *m)
274 g_object_unref (m->session);
275 g_list_foreach (m->folders, (GFunc) g_object_unref, NULL);
276 g_list_free (m->folders);
277 g_free (m->uri);
280 static MailMsgInfo vfolder_adduri_info = {
281 sizeof (struct _adduri_msg),
282 (MailMsgDescFunc) vfolder_adduri_desc,
283 (MailMsgExecFunc) vfolder_adduri_exec,
284 (MailMsgDoneFunc) vfolder_adduri_done,
285 (MailMsgFreeFunc) vfolder_adduri_free
288 /* uri should be a camel uri */
289 static gint
290 vfolder_adduri (EMailSession *session,
291 const gchar *uri,
292 GList *folders,
293 gint remove)
295 struct _adduri_msg *m;
296 gint id;
298 m = mail_msg_new (&vfolder_adduri_info);
299 m->session = g_object_ref (session);
300 m->folders = folders;
301 m->uri = g_strdup (uri);
302 m->remove = remove;
304 id = m->base.seq;
305 mail_msg_slow_ordered_push (m);
307 return id;
310 /* ********************************************************************** */
312 /* so special we never use it */
313 static gint
314 folder_is_spethal (CamelStore *store,
315 const gchar *folder_name)
317 /* This is a bit of a hack, but really the only way it can be done
318 * at the moment. */
320 if (store->flags & CAMEL_STORE_VTRASH)
321 if (g_strcmp0 (folder_name, CAMEL_VTRASH_NAME) == 0)
322 return TRUE;
324 if (store->flags & CAMEL_STORE_VJUNK)
325 if (g_strcmp0 (folder_name, CAMEL_VJUNK_NAME) == 0)
326 return TRUE;
328 return FALSE;
332 * mail_vfolder_add_folder:
333 * @store: a #CamelStore
334 * @folder: a folder name
335 * @remove: whether the folder should be removed or added
337 * Called when a new folder becomes (un)available. If @store is not a
338 * CamelVeeStore, the folder is added/removed from the list of cached source
339 * folders. Then each vfolder rule is checked to see if the specified folder
340 * matches a source of the rule. It builds a list of vfolders that use (or
341 * would use) the specified folder as a source. It then adds (or removes)
342 * this folder to (from) those vfolders via camel_vee_folder_add/
343 * remove_folder() but does not modify the actual filters or write changes
344 * to disk.
346 * NOTE: This function must be called from the main thread.
348 static void
349 mail_vfolder_add_folder (CamelStore *store,
350 const gchar *folder_name,
351 gint remove)
353 CamelService *service;
354 CamelSession *session;
355 EFilterRule *rule;
356 const gchar *source;
357 CamelVeeFolder *vf;
358 CamelProvider *provider;
359 GList *folders = NULL;
360 gint remote;
361 gchar *uri;
363 g_return_if_fail (CAMEL_IS_STORE (store));
364 g_return_if_fail (folder_name != NULL);
366 service = CAMEL_SERVICE (store);
367 session = camel_service_get_session (service);
368 provider = camel_service_get_provider (service);
370 remote = (provider->flags & CAMEL_PROVIDER_IS_REMOTE) != 0;
372 if (folder_is_spethal (store, folder_name))
373 return;
375 g_return_if_fail (mail_in_main_thread ());
377 uri = e_mail_folder_uri_build (store, folder_name);
379 G_LOCK (vfolder);
381 if (context == NULL)
382 goto done;
384 rule = NULL;
385 while ((rule = e_rule_context_next_rule ((ERuleContext *) context, rule, NULL))) {
386 gint found = FALSE;
388 if (!rule->name) {
389 d(printf("invalid rule (%p): rule->name is set to NULL\n", rule));
390 continue;
392 /* Don't auto-add any sent/drafts folders etc,
393 * they must be explictly listed as a source. */
394 if (rule->source
395 && !CAMEL_IS_VEE_STORE (store)
396 && ((((EMVFolderRule *) rule)->with ==
397 EM_VFOLDER_RULE_WITH_LOCAL && !remote)
398 || (((EMVFolderRule *) rule)->with ==
399 EM_VFOLDER_RULE_WITH_REMOTE_ACTIVE && remote)
400 || (((EMVFolderRule *) rule)->with ==
401 EM_VFOLDER_RULE_WITH_LOCAL_REMOTE_ACTIVE)))
402 found = TRUE;
404 source = NULL;
405 while (!found && (source = em_vfolder_rule_next_source (
406 (EMVFolderRule *) rule, source))) {
407 found = e_mail_folder_uri_equal (session, uri, source);
410 if (found) {
411 vf = g_hash_table_lookup (vfolder_hash, rule->name);
412 if (!vf) {
413 g_warning ("vf is NULL for %s\n", rule->name);
414 continue;
416 g_object_ref (vf);
417 folders = g_list_prepend (folders, vf);
421 done:
422 G_UNLOCK (vfolder);
424 if (folders != NULL)
425 vfolder_adduri (
426 E_MAIL_SESSION (session),
427 uri, folders, remove);
429 g_free (uri);
433 * mail_vfolder_delete_folder:
434 * @store: a #CamelStore
435 * @folder_name: a folder name
437 * Looks through all vfolder rules to see if @folder_name is listed as a
438 * source for any vfolder rules. If the folder is found in the source for
439 * any rule, it is removed and the user is alerted to the fact that the
440 * vfolder rules have been updated. The new vfolder rules are written
441 * to disk.
443 * XXX: It doesn't appear that the changes to the vfolder rules are sent
444 * down to the camel level, however. So the actual vfolders will not change
445 * behavior until evolution is restarted (?)
447 * NOTE: This function must be called from the main thread.
449 static void
450 mail_vfolder_delete_folder (CamelStore *store,
451 const gchar *folder_name)
453 ERuleContext *rule_context;
454 EFilterRule *rule;
455 CamelService *service;
456 CamelSession *session;
457 const gchar *source;
458 CamelVeeFolder *vf;
459 GString *changed;
460 guint changed_count;
461 gchar *uri;
463 g_return_if_fail (CAMEL_IS_STORE (store));
464 g_return_if_fail (folder_name != NULL);
466 if (folder_is_spethal (store, folder_name))
467 return;
469 d(printf ("Deleting uri to check: %s\n", uri));
471 g_return_if_fail (mail_in_main_thread ());
473 service = CAMEL_SERVICE (store);
474 session = camel_service_get_session (service);
476 uri = e_mail_folder_uri_build (store, folder_name);
478 changed_count = 0;
479 changed = g_string_new ("");
481 G_LOCK (vfolder);
483 if (context == NULL)
484 goto done;
486 rule_context = E_RULE_CONTEXT (context);
488 /* see if any rules directly reference this removed uri */
489 rule = NULL;
490 while ((rule = e_rule_context_next_rule (rule_context, rule, NULL))) {
491 EMVFolderRule *vf_rule = EM_VFOLDER_RULE (rule);
493 if (!rule->name)
494 continue;
496 source = NULL;
497 while ((source = em_vfolder_rule_next_source (vf_rule, source))) {
498 /* Remove all sources that match, ignore changed events though
499 * because the adduri call above does the work async */
500 if (e_mail_folder_uri_equal (session, uri, source)) {
501 vf = g_hash_table_lookup (
502 vfolder_hash, rule->name);
504 if (!vf) {
505 g_warning ("vf is NULL for %s\n", rule->name);
506 continue;
509 g_signal_handlers_disconnect_matched (
510 rule, G_SIGNAL_MATCH_FUNC |
511 G_SIGNAL_MATCH_DATA, 0, 0, NULL,
512 rule_changed, vf);
514 em_vfolder_rule_remove_source (vf_rule, source);
516 g_signal_connect (
517 rule, "changed",
518 G_CALLBACK (rule_changed), vf);
520 if (changed_count == 0) {
521 g_string_append (changed, rule->name);
522 } else {
523 if (changed_count == 1) {
524 g_string_prepend (changed, " ");
525 g_string_append (changed, "\n");
527 g_string_append_printf (
528 changed, " %s\n",
529 rule->name);
532 changed_count++;
533 source = NULL;
538 done:
539 G_UNLOCK (vfolder);
541 if (changed_count > 0) {
542 EAlertSink *alert_sink;
543 const gchar *config_dir;
544 gchar *user, *info;
546 alert_sink = mail_msg_get_alert_sink ();
548 info = g_strdup_printf (ngettext (
549 /* Translators: The first %s is name of the affected
550 * search folder(s), the second %s is the URI of the
551 * removed folder. For more than one search folder is
552 * each of them on a separate line, with four spaces
553 * in front of its name, without quotes. */
554 "The Search Folder \"%s\" has been modified to "
555 "account for the deleted folder\n\"%s\".",
556 "The following Search Folders\n%s have been modified "
557 "to account for the deleted folder\n\"%s\".",
558 changed_count), changed->str, uri);
559 e_alert_submit (
560 alert_sink, "mail:vfolder-updated", info, NULL);
561 g_free (info);
563 config_dir = mail_session_get_config_dir ();
564 user = g_build_filename (config_dir, "vfolders.xml", NULL);
565 e_rule_context_save ((ERuleContext *) context, user);
566 g_free (user);
569 g_string_free (changed, TRUE);
571 g_free (uri);
574 /* called when a uri is renamed in a store */
575 static void
576 mail_vfolder_rename_folder (CamelStore *store,
577 const gchar *old_folder_name,
578 const gchar *new_folder_name)
580 ERuleContext *rule_context;
581 EFilterRule *rule;
582 const gchar *source;
583 CamelVeeFolder *vf;
584 CamelService *service;
585 CamelSession *session;
586 gint changed = 0;
587 gchar *old_uri;
588 gchar *new_uri;
590 d(printf("vfolder rename uri: %s to %s\n", cfrom, cto));
592 if (context == NULL)
593 return;
595 if (folder_is_spethal (store, old_folder_name))
596 return;
598 if (folder_is_spethal (store, new_folder_name))
599 return;
601 g_return_if_fail (mail_in_main_thread ());
603 service = CAMEL_SERVICE (store);
604 session = camel_service_get_session (service);
606 old_uri = e_mail_folder_uri_build (store, old_folder_name);
607 new_uri = e_mail_folder_uri_build (store, new_folder_name);
609 G_LOCK (vfolder);
611 rule_context = E_RULE_CONTEXT (context);
613 /* see if any rules directly reference this removed uri */
614 rule = NULL;
615 while ((rule = e_rule_context_next_rule (rule_context, rule, NULL))) {
616 EMVFolderRule *vf_rule = EM_VFOLDER_RULE (rule);
618 source = NULL;
619 while ((source = em_vfolder_rule_next_source (vf_rule, source))) {
620 /* Remove all sources that match, ignore changed events though
621 * because the adduri call above does the work async */
622 if (e_mail_folder_uri_equal (session, old_uri, source)) {
623 vf = g_hash_table_lookup (vfolder_hash, rule->name);
624 if (!vf) {
625 g_warning ("vf is NULL for %s\n", rule->name);
626 continue;
629 g_signal_handlers_disconnect_matched (
630 rule, G_SIGNAL_MATCH_FUNC |
631 G_SIGNAL_MATCH_DATA, 0, 0, NULL,
632 rule_changed, vf);
634 em_vfolder_rule_remove_source (vf_rule, source);
635 em_vfolder_rule_add_source (vf_rule, new_uri);
637 g_signal_connect (
638 vf_rule, "changed",
639 G_CALLBACK (rule_changed), vf);
641 changed++;
642 source = NULL;
647 G_UNLOCK (vfolder);
649 if (changed) {
650 const gchar *config_dir;
651 gchar *user;
653 d(printf("Vfolders updated from renamed folder\n"));
654 config_dir = mail_session_get_config_dir ();
655 user = g_build_filename (config_dir, "vfolders.xml", NULL);
656 e_rule_context_save ((ERuleContext *) context, user);
657 g_free (user);
660 g_free (old_uri);
661 g_free (new_uri);
664 /* ********************************************************************** */
666 static void context_rule_added (ERuleContext *ctx, EFilterRule *rule, EMailSession *session);
668 static void
669 rule_add_sources (EMailSession *session,
670 GQueue *queue,
671 GList **sources_folderp,
672 GList **sources_urip)
674 GList *sources_folder = *sources_folderp;
675 GList *sources_uri = *sources_urip;
676 MailFolderCache *folder_cache;
677 GList *head, *link;
679 folder_cache = e_mail_session_get_folder_cache (session);
681 head = g_queue_peek_head_link (queue);
682 for (link = head; link != NULL; link = g_list_next (link)) {
683 const gchar *uri = link->data;
685 /* always pick fresh folders - they are
686 * from CamelStore's folders bag anyway */
687 if (mail_folder_cache_get_folder_from_uri (folder_cache, uri, NULL)) {
688 sources_uri = g_list_append (sources_uri, g_strdup (uri));
692 *sources_folderp = sources_folder;
693 *sources_urip = sources_uri;
696 static EMailSession *
697 get_session (CamelFolder *folder)
699 CamelStore *store;
701 store = camel_folder_get_parent_store (folder);
703 return (EMailSession *) camel_service_get_session (CAMEL_SERVICE (store));
706 static void
707 rule_changed (EFilterRule *rule,
708 CamelFolder *folder)
710 EMailSession *session;
711 CamelService *service;
712 GList *sources_uri = NULL;
713 GList *sources_folder = NULL;
714 GString *query;
715 const gchar *full_name;
717 full_name = camel_folder_get_full_name (folder);
718 session = get_session (folder);
720 service = camel_session_get_service (
721 CAMEL_SESSION (session), E_MAIL_SESSION_VFOLDER_UID);
722 g_return_if_fail (CAMEL_IS_SERVICE (service));
724 /* If the folder has changed name, then
725 * add it, then remove the old manually. */
726 if (strcmp (full_name, rule->name) != 0) {
727 gchar *oldname;
729 gpointer key;
730 gpointer oldfolder;
732 G_LOCK (vfolder);
733 if (g_hash_table_lookup_extended (
734 vfolder_hash, full_name, &key, &oldfolder)) {
735 g_hash_table_remove (vfolder_hash, key);
736 g_free (key);
737 g_hash_table_insert (
738 vfolder_hash, g_strdup (rule->name), folder);
739 G_UNLOCK (vfolder);
740 } else {
741 G_UNLOCK (vfolder);
742 g_warning (
743 "couldn't find a vfolder rule "
744 "in our table? %s", full_name);
747 oldname = g_strdup (full_name);
748 /* FIXME Not passing a GCancellable or GError. */
749 camel_store_rename_folder_sync (
750 CAMEL_STORE (service),
751 oldname, rule->name, NULL, NULL);
752 g_free (oldname);
755 d(printf("Filter rule changed? for folder '%s'!!\n", folder->name));
757 /* find any (currently available) folders, and add them to the ones to open */
758 rule_add_sources (
759 session, &((EMVFolderRule *) rule)->sources,
760 &sources_folder, &sources_uri);
762 G_LOCK (vfolder);
764 if (((EMVFolderRule *) rule)->with ==
765 EM_VFOLDER_RULE_WITH_LOCAL ||
766 ((EMVFolderRule *) rule)->with ==
767 EM_VFOLDER_RULE_WITH_LOCAL_REMOTE_ACTIVE) {
769 MailFolderCache *cache;
770 GQueue queue = G_QUEUE_INIT;
772 cache = e_mail_session_get_folder_cache (session);
773 mail_folder_cache_get_local_folder_uris (cache, &queue);
775 rule_add_sources (
776 session, &queue, &sources_folder, &sources_uri);
778 while (!g_queue_is_empty (&queue))
779 g_free (g_queue_pop_head (&queue));
782 if (((EMVFolderRule *) rule)->with ==
783 EM_VFOLDER_RULE_WITH_REMOTE_ACTIVE ||
784 ((EMVFolderRule *) rule)->with ==
785 EM_VFOLDER_RULE_WITH_LOCAL_REMOTE_ACTIVE) {
787 MailFolderCache *cache;
788 GQueue queue = G_QUEUE_INIT;
790 cache = e_mail_session_get_folder_cache (session);
791 mail_folder_cache_get_remote_folder_uris (cache, &queue);
793 rule_add_sources (
794 session, &queue, &sources_folder, &sources_uri);
796 while (!g_queue_is_empty (&queue))
797 g_free (g_queue_pop_head (&queue));
800 G_UNLOCK (vfolder);
802 query = g_string_new("");
803 e_filter_rule_build_code (rule, query);
805 vfolder_setup (session, folder, query->str, sources_uri, sources_folder);
807 g_string_free (query, TRUE);
810 static void
811 context_rule_added (ERuleContext *ctx,
812 EFilterRule *rule,
813 EMailSession *session)
815 CamelFolder *folder;
816 CamelService *service;
818 d(printf("rule added: %s\n", rule->name));
820 service = camel_session_get_service (
821 CAMEL_SESSION (session), E_MAIL_SESSION_VFOLDER_UID);
822 g_return_if_fail (CAMEL_IS_SERVICE (service));
824 /* this always runs quickly */
825 /* FIXME Not passing a GCancellable or GError. */
826 folder = camel_store_get_folder_sync (
827 CAMEL_STORE (service), rule->name, 0, NULL, NULL);
828 if (folder) {
829 g_signal_connect (
830 rule, "changed",
831 G_CALLBACK (rule_changed), folder);
833 G_LOCK (vfolder);
834 g_hash_table_insert (vfolder_hash, g_strdup (rule->name), folder);
835 G_UNLOCK (vfolder);
837 rule_changed (rule, folder);
841 static void
842 context_rule_removed (ERuleContext *ctx,
843 EFilterRule *rule,
844 EMailSession *session)
846 CamelService *service;
847 gpointer key, folder = NULL;
849 d(printf("rule removed; %s\n", rule->name));
851 service = camel_session_get_service (
852 CAMEL_SESSION (session), E_MAIL_SESSION_VFOLDER_UID);
853 g_return_if_fail (CAMEL_IS_SERVICE (service));
855 /* TODO: remove from folder info cache? */
857 G_LOCK (vfolder);
858 if (g_hash_table_lookup_extended (vfolder_hash, rule->name, &key, &folder)) {
859 g_hash_table_remove (vfolder_hash, key);
860 g_free (key);
862 G_UNLOCK (vfolder);
864 /* FIXME Not passing a GCancellable or GError. */
865 camel_store_delete_folder_sync (
866 CAMEL_STORE (service), rule->name, NULL, NULL);
867 /* this must be unref'd after its deleted */
868 if (folder)
869 g_object_unref ((CamelFolder *) folder);
872 static void
873 store_folder_deleted_cb (CamelStore *store,
874 CamelFolderInfo *info)
876 EFilterRule *rule;
877 gchar *user;
879 d(printf("Folder deleted: %s\n", info->name));
880 store = store;
882 /* Warning not thread safe, but might be enough */
884 G_LOCK (vfolder);
886 /* delete it from our list */
887 rule = e_rule_context_find_rule ((ERuleContext *) context, info->full_name, NULL);
888 if (rule) {
889 const gchar *config_dir;
891 /* We need to stop listening to removed events,
892 * otherwise we'll try and remove it again. */
893 g_signal_handlers_disconnect_matched (
894 context, G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA,
895 0, 0, NULL, context_rule_removed, context);
896 e_rule_context_remove_rule ((ERuleContext *) context, rule);
897 g_object_unref (rule);
898 g_signal_connect (
899 context, "rule_removed",
900 G_CALLBACK (context_rule_removed), context);
902 config_dir = mail_session_get_config_dir ();
903 user = g_build_filename (config_dir, "vfolders.xml", NULL);
904 e_rule_context_save ((ERuleContext *) context, user);
905 g_free (user);
906 } else {
907 g_warning (
908 "Cannot find rule for deleted vfolder '%s'",
909 info->display_name);
912 G_UNLOCK (vfolder);
915 static void
916 store_folder_renamed_cb (CamelStore *store,
917 const gchar *old_name,
918 CamelFolderInfo *info)
920 EFilterRule *rule;
921 gchar *user;
923 gpointer key, folder;
925 /* This should be more-or-less thread-safe */
927 d(printf("Folder renamed to '%s' from '%s'\n", info->full_name, old_name));
929 /* Folder is already renamed? */
930 G_LOCK (vfolder);
931 d(printf("Changing folder name in hash table to '%s'\n", info->full_name));
932 if (g_hash_table_lookup_extended (vfolder_hash, old_name, &key, &folder)) {
933 const gchar *config_dir;
935 g_hash_table_remove (vfolder_hash, key);
936 g_free (key);
937 g_hash_table_insert (vfolder_hash, g_strdup (info->full_name), folder);
939 rule = e_rule_context_find_rule ((ERuleContext *) context, old_name, NULL);
940 if (!rule) {
941 G_UNLOCK (vfolder);
942 g_warning ("Rule shouldn't be NULL\n");
943 return;
946 g_signal_handlers_disconnect_matched (
947 rule, G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA,
948 0, 0, NULL, rule_changed, folder);
949 e_filter_rule_set_name (rule, info->full_name);
950 g_signal_connect (
951 rule, "changed",
952 G_CALLBACK (rule_changed), folder);
954 config_dir = mail_session_get_config_dir ();
955 user = g_build_filename (config_dir, "vfolders.xml", NULL);
956 e_rule_context_save ((ERuleContext *) context, user);
957 g_free (user);
959 G_UNLOCK (vfolder);
960 } else {
961 G_UNLOCK (vfolder);
962 g_warning("couldn't find a vfolder rule in our table? %s", info->full_name);
966 static void
967 folder_available_cb (MailFolderCache *cache,
968 CamelStore *store,
969 const gchar *folder_name)
971 mail_vfolder_add_folder (store, folder_name, FALSE);
974 static void
975 folder_unavailable_cb (MailFolderCache *cache,
976 CamelStore *store,
977 const gchar *folder_name)
979 mail_vfolder_add_folder (store, folder_name, TRUE);
982 static void
983 folder_deleted_cb (MailFolderCache *cache,
984 CamelStore *store,
985 const gchar *folder_name)
987 mail_vfolder_delete_folder (store, folder_name);
990 static void
991 folder_renamed_cb (MailFolderCache *cache,
992 CamelStore *store,
993 const gchar *old_folder_name,
994 const gchar *new_folder_name,
995 gpointer user_data)
997 mail_vfolder_rename_folder (store, old_folder_name, new_folder_name);
1000 void
1001 vfolder_load_storage (EMailSession *session)
1003 /* lock for loading storage, it is safe to call it more than once */
1004 G_LOCK_DEFINE_STATIC (vfolder_hash);
1006 CamelStore *vfolder_store;
1007 const gchar *config_dir;
1008 gchar *user;
1009 EFilterRule *rule;
1010 MailFolderCache *folder_cache;
1011 gchar *xmlfile;
1012 GSettings *settings;
1014 G_LOCK (vfolder_hash);
1016 if (vfolder_hash) {
1017 /* we have already initialized */
1018 G_UNLOCK (vfolder_hash);
1019 return;
1022 vfolder_hash = g_hash_table_new (g_str_hash, g_str_equal);
1024 G_UNLOCK (vfolder_hash);
1026 config_dir = mail_session_get_config_dir ();
1027 vfolder_store = e_mail_session_get_vfolder_store (E_MAIL_SESSION (session));
1029 g_signal_connect (
1030 vfolder_store, "folder-deleted",
1031 G_CALLBACK (store_folder_deleted_cb), NULL);
1033 g_signal_connect (
1034 vfolder_store, "folder-renamed",
1035 G_CALLBACK (store_folder_renamed_cb), NULL);
1037 /* load our rules */
1038 user = g_build_filename (config_dir, "vfolders.xml", NULL);
1039 /* This needs editor context which is only in the mail/. But really to run here we dont need editor context.
1040 * So till we split this to EDS, we would let mail/ create this and later one it is any ways two separate
1041 * contexts. */
1042 context = e_mail_session_create_vfolder_context (session);
1044 xmlfile = g_build_filename (EVOLUTION_PRIVDATADIR, "vfoldertypes.xml", NULL);
1045 if (e_rule_context_load ((ERuleContext *) context,
1046 xmlfile, user) != 0) {
1047 g_warning("cannot load vfolders: %s\n", ((ERuleContext *)context)->error);
1049 g_free (xmlfile);
1050 g_free (user);
1052 g_signal_connect (
1053 context, "rule_added",
1054 G_CALLBACK (context_rule_added), session);
1055 g_signal_connect (
1056 context, "rule_removed",
1057 G_CALLBACK (context_rule_removed), session);
1059 /* and setup the rules we have */
1060 rule = NULL;
1061 while ((rule = e_rule_context_next_rule ((ERuleContext *) context, rule, NULL))) {
1062 if (rule->name) {
1063 d(printf("rule added: %s\n", rule->name));
1064 context_rule_added ((ERuleContext *) context, rule, session);
1065 } else {
1066 d(printf("invalid rule (%p) encountered: rule->name is NULL\n", rule));
1070 /* reenable the feature if required */
1071 settings = g_settings_new ("org.gnome.evolution.mail");
1072 g_settings_set_boolean (settings, "enable-vfolders", TRUE);
1073 g_object_unref (settings);
1075 folder_cache = e_mail_session_get_folder_cache (session);
1077 g_signal_connect (
1078 folder_cache, "folder-available",
1079 G_CALLBACK (folder_available_cb), NULL);
1080 g_signal_connect (
1081 folder_cache, "folder-unavailable",
1082 G_CALLBACK (folder_unavailable_cb), NULL);
1083 g_signal_connect (
1084 folder_cache, "folder-deleted",
1085 G_CALLBACK (folder_deleted_cb), NULL);
1086 g_signal_connect (
1087 folder_cache, "folder-renamed",
1088 G_CALLBACK (folder_renamed_cb), NULL);
1091 static void
1092 vfolder_foreach_cb (gpointer key,
1093 gpointer data,
1094 gpointer user_data)
1096 CamelFolder *folder = CAMEL_FOLDER (data);
1098 if (folder)
1099 g_object_unref (folder);
1101 g_free (key);
1104 void
1105 mail_vfolder_shutdown (void)
1107 vfolder_shutdown = 1;
1109 if (vfolder_hash) {
1110 g_hash_table_foreach (vfolder_hash, vfolder_foreach_cb, NULL);
1111 g_hash_table_destroy (vfolder_hash);
1112 vfolder_hash = NULL;
1115 if (context) {
1116 g_object_unref (context);
1117 context = NULL;