Updated Traditional Chinese translation(Hong Kong). Updated Traditional
[evolution.git] / mail / mail-folder-cache.c
blob598141657e28610b3511454ff2c03525fed2ada7
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Authors: Peter Williams <peterw@ximian.com>
4 * Michael Zucchi <notzed@ximian.com>
6 * Copyright 2000,2001 Ximian, Inc. (www.ximian.com)
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 * USA
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
27 #ifdef G_LOG_DOMAIN
28 #undef G_LOG_DOMAIN
29 #endif
30 #define G_LOG_DOMAIN "folder tree"
32 #include <pthread.h>
33 #include <string.h>
34 #include <time.h>
36 #include <glib.h>
37 #include <glib/gstdio.h>
39 #include <libgnome/gnome-sound.h>
40 #include <libgnome/gnome-i18n.h>
41 #include <bonobo/bonobo-exception.h>
42 #include <camel/camel-store.h>
43 #include <camel/camel-folder.h>
44 #include <camel/camel-vtrash-folder.h>
45 #include <camel/camel-vee-store.h>
46 #include <camel/camel-offline-store.h>
47 #include <camel/camel-disco-store.h>
49 #include <libedataserver/e-data-server-util.h>
50 #include "e-util/e-util.h"
52 #include "mail-mt.h"
53 #include "mail-folder-cache.h"
54 #include "mail-ops.h"
55 #include "mail-session.h"
56 #include "mail-component.h"
58 /* For notifications of changes */
59 #include "mail-vfolder.h"
60 #include "mail-autofilter.h"
61 #include "mail-config.h"
62 #include "em-folder-tree-model.h"
64 #include "em-event.h"
66 #define w(x)
67 #define d(x)
69 /* This code is a mess, there is no reason it should be so complicated. */
71 /* note that many things are effectively serialised by having them run in
72 the main loop thread which they need to do because of corba/gtk calls */
73 #define LOCK(x) pthread_mutex_lock(&x)
74 #define UNLOCK(x) pthread_mutex_unlock(&x)
76 static pthread_mutex_t info_lock = PTHREAD_MUTEX_INITIALIZER;
78 struct _folder_info {
79 struct _store_info *store_info; /* 'parent' link */
81 char *full_name; /* full name of folder/folderinfo */
82 char *uri; /* uri of folder */
84 guint32 flags;
86 CamelFolder *folder; /* if known */
89 /* pending list of updates */
90 struct _folder_update {
91 struct _folder_update *next;
92 struct _folder_update *prev;
94 unsigned int remove:1; /* removing from vfolders */
95 unsigned int delete:1; /* deleting as well? */
96 unsigned int add:1; /* add to vfolder */
97 unsigned int unsub:1; /* unsubcribing? */
98 unsigned int new:1; /* new mail arrived? */
100 char *full_name;
101 char *uri;
102 char *oldfull;
103 char *olduri;
105 int unread;
106 CamelStore *store;
109 struct _store_info {
110 GHashTable *folders; /* by full_name */
111 GHashTable *folders_uri; /* by uri */
113 CamelStore *store; /* the store for these folders */
115 /* Outstanding folderinfo requests */
116 EDList folderinfo_updates;
119 static void folder_changed(CamelObject *o, gpointer event_data, gpointer user_data);
120 static void folder_renamed(CamelObject *o, gpointer event_data, gpointer user_data);
121 static void folder_finalised(CamelObject *o, gpointer event_data, gpointer user_data);
123 static guint ping_id = 0;
124 static gboolean ping_cb (gpointer user_data);
126 static guint notify_id = 0;
127 static int notify_type = -1;
129 static time_t last_notify = 0;
130 static guint notify_idle_id = 0;
131 static gboolean notify_idle_cb (gpointer user_data);
134 /* Store to storeinfo table, active stores */
135 static GHashTable *stores = NULL;
137 /* List of folder changes to be executed in gui thread */
138 static EDList updates = E_DLIST_INITIALISER(updates);
139 static int update_id = -1;
141 /* hack for people who LIKE to have unsent count */
142 static int count_sent = FALSE;
143 static int count_trash = FALSE;
145 static void
146 free_update(struct _folder_update *up)
148 g_free(up->full_name);
149 g_free(up->uri);
150 if (up->store)
151 camel_object_unref(up->store);
152 g_free(up->oldfull);
153 g_free(up->olduri);
154 g_free(up);
157 static gboolean
158 notify_idle_cb (gpointer user_data)
160 GConfClient *gconf;
161 char *filename;
163 gconf = mail_config_get_gconf_client ();
165 switch (notify_type) {
166 case MAIL_CONFIG_NOTIFY_PLAY_SOUND:
167 filename = gconf_client_get_string (gconf, "/apps/evolution/mail/notify/sound", NULL);
168 if (filename != NULL) {
169 gnome_sound_play (filename);
170 g_free (filename);
172 break;
173 case MAIL_CONFIG_NOTIFY_BEEP:
174 gdk_beep ();
175 break;
176 default:
177 break;
180 time (&last_notify);
182 notify_idle_id = 0;
184 return FALSE;
187 static void
188 notify_type_changed (GConfClient *client, guint cnxn_id,
189 GConfEntry *entry, gpointer user_data)
191 notify_type = gconf_client_get_int (client, "/apps/evolution/mail/notify/type", NULL);
194 static void
195 real_flush_updates(void *o, void *event_data, void *data)
197 struct _MailComponent *component;
198 struct _EMFolderTreeModel *model;
199 struct _folder_update *up;
200 time_t now;
202 component = mail_component_peek ();
203 model = mail_component_peek_tree_model (component);
205 LOCK(info_lock);
206 while ((up = (struct _folder_update *)e_dlist_remhead(&updates))) {
207 UNLOCK(info_lock);
209 if (up->remove) {
210 if (up->delete) {
211 mail_vfolder_delete_uri(up->store, up->uri);
212 mail_filter_delete_uri(up->store, up->uri);
213 mail_config_uri_deleted(CAMEL_STORE_CLASS(CAMEL_OBJECT_GET_CLASS(up->store))->compare_folder_name, up->uri);
215 } else
216 mail_vfolder_add_uri(up->store, up->uri, TRUE);
217 } else {
218 /* We can tell the vfolder code though */
219 if (up->olduri && up->add) {
220 d(printf("renaming folder '%s' to '%s'\n", up->olduri, up->uri));
221 mail_vfolder_rename_uri(up->store, up->olduri, up->uri);
222 mail_filter_rename_uri(up->store, up->olduri, up->uri);
223 mail_config_uri_renamed(CAMEL_STORE_CLASS(CAMEL_OBJECT_GET_CLASS(up->store))->compare_folder_name,
224 up->olduri, up->uri);
227 if (!up->olduri && up->add)
228 mail_vfolder_add_uri(up->store, up->uri, FALSE);
231 /* update unread counts */
232 em_folder_tree_model_set_unread_count (model, up->store, up->full_name, up->unread);
234 /* new mail notification */
235 if (notify_type == -1) {
236 /* need to track the user's new-mail-notification settings... */
237 GConfClient *gconf;
239 gconf = mail_config_get_gconf_client ();
240 gconf_client_add_dir (gconf, "/apps/evolution/mail/notify",
241 GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);
242 notify_id = gconf_client_notify_add (gconf, "/apps/evolution/mail/notify",
243 notify_type_changed, NULL, NULL, NULL);
244 notify_type = gconf_client_get_int (gconf, "/apps/evolution/mail/notify/type", NULL);
247 time (&now);
248 if (notify_type != 0 && up->new && notify_idle_id == 0 && (now - last_notify >= 5))
249 notify_idle_id = g_idle_add_full (G_PRIORITY_LOW, notify_idle_cb, NULL, NULL);
251 if (up->uri) {
252 EMEvent *e = em_event_peek();
253 EMEventTargetFolder *t = em_event_target_new_folder(e, up->uri, up->new?EM_EVENT_FOLDER_NEWMAIL:0);
255 /** @Event: folder.changed
256 * @Title: Folder changed
257 * @Target: EMEventTargetFolder
259 * folder.changed is emitted whenever a folder changes. There is no detail on how the folder has changed.
261 e_event_emit((EEvent *)e, "folder.changed", (EEventTarget *)t);
264 free_update(up);
266 LOCK(info_lock);
268 update_id = -1;
269 UNLOCK(info_lock);
272 static void
273 flush_updates(void)
275 if (update_id == -1 && !e_dlist_empty(&updates))
276 update_id = mail_async_event_emit(mail_async_event, MAIL_ASYNC_GUI, (MailAsyncFunc)real_flush_updates, 0, 0, 0);
279 static void
280 unset_folder_info(struct _folder_info *mfi, int delete, int unsub)
282 struct _folder_update *up;
284 d(printf("unset folderinfo '%s'\n", mfi->uri));
286 if (mfi->folder) {
287 CamelFolder *folder = mfi->folder;
289 camel_object_unhook_event(folder, "folder_changed", folder_changed, NULL);
290 camel_object_unhook_event(folder, "renamed", folder_renamed, NULL);
291 camel_object_unhook_event(folder, "finalize", folder_finalised, NULL);
294 if ((mfi->flags & CAMEL_FOLDER_NOSELECT) == 0) {
295 up = g_malloc0(sizeof(*up));
297 up->remove = TRUE;
298 up->delete = delete;
299 up->unsub = unsub;
300 up->store = mfi->store_info->store;
301 up->full_name = g_strdup (mfi->full_name);
302 camel_object_ref(up->store);
303 up->uri = g_strdup(mfi->uri);
305 e_dlist_addtail(&updates, (EDListNode *)up);
306 flush_updates();
310 static void
311 free_folder_info(struct _folder_info *mfi)
313 g_free(mfi->full_name);
314 g_free(mfi->uri);
315 g_free(mfi);
318 /* This is how unread counts work (and don't work):
320 * camel_folder_unread_message_count() only gives a correct answer if
321 * the store is paying attention to the folder. (Some stores always
322 * pay attention to all folders, but IMAP can only pay attention to
323 * one folder at a time.) But it doesn't have any way to know when
324 * it's lying, so it's only safe to call it when you know for sure
325 * that the store is paying attention to the folder, such as when it's
326 * just been created, or you get a folder_changed signal on it.
328 * camel_store_get_folder_info() always gives correct answers for the
329 * folders it checks, but it can also return -1 for a folder, meaning
330 * it didn't check, and so you should stick with your previous answer.
332 * update_1folder is called from three places: with info != NULL when
333 * the folder is created (or get_folder_info), with info == NULL when
334 * a folder changed event is emitted.
336 * So if info is NULL, camel_folder_unread_message_count is correct,
337 * and if it's not NULL and its unread_message_count isn't -1, then
338 * it's correct. */
340 static void
341 update_1folder(struct _folder_info *mfi, int new, CamelFolderInfo *info)
343 struct _folder_update *up;
344 CamelFolder *folder;
345 int unread = -1;
346 int deleted;
348 folder = mfi->folder;
349 if (folder) {
350 d(printf("update 1 folder '%s'\n", folder->full_name));
351 if ((count_trash && (CAMEL_IS_VTRASH_FOLDER (folder)))
352 || folder == mail_component_get_folder(NULL, MAIL_COMPONENT_FOLDER_OUTBOX)
353 || folder == mail_component_get_folder(NULL, MAIL_COMPONENT_FOLDER_DRAFTS)
354 || (count_sent && folder == mail_component_get_folder(NULL, MAIL_COMPONENT_FOLDER_SENT))) {
355 d(printf(" total count\n"));
356 unread = camel_folder_get_message_count (folder);
357 if (folder == mail_component_get_folder(NULL, MAIL_COMPONENT_FOLDER_OUTBOX)
358 || folder == mail_component_get_folder(NULL, MAIL_COMPONENT_FOLDER_DRAFTS)) {
359 if ((deleted = camel_folder_get_deleted_message_count (folder)) > 0)
360 unread -= deleted;
362 } else {
363 d(printf(" unread count\n"));
364 if (info)
365 unread = info->unread;
366 else
367 unread = camel_folder_get_unread_message_count (folder);
369 } else if (info)
370 unread = info->unread;
372 d(printf("folder updated: unread %d: '%s'\n", unread, mfi->full_name));
374 if (unread == -1)
375 return;
377 up = g_malloc0(sizeof(*up));
378 up->full_name = g_strdup(mfi->full_name);
379 up->unread = unread;
380 up->new = new ? 1 : 0;
381 up->store = mfi->store_info->store;
382 up->uri = g_strdup(mfi->uri);
383 camel_object_ref(up->store);
384 e_dlist_addtail(&updates, (EDListNode *)up);
385 flush_updates();
388 static void
389 setup_folder(CamelFolderInfo *fi, struct _store_info *si)
391 struct _folder_info *mfi;
392 struct _folder_update *up;
394 mfi = g_hash_table_lookup(si->folders, fi->full_name);
395 if (mfi) {
396 update_1folder(mfi, 0, fi);
397 } else {
398 d(printf("Adding new folder: %s (%s)\n", fi->full_name, fi->uri));
399 mfi = g_malloc0(sizeof(*mfi));
400 mfi->full_name = g_strdup(fi->full_name);
401 mfi->uri = g_strdup(fi->uri);
402 mfi->store_info = si;
403 mfi->flags = fi->flags;
405 g_hash_table_insert(si->folders, mfi->full_name, mfi);
406 g_hash_table_insert(si->folders_uri, mfi->uri, mfi);
408 up = g_malloc0(sizeof(*up));
409 up->full_name = g_strdup(mfi->full_name);
410 up->uri = g_strdup(fi->uri);
411 up->unread = fi->unread;
412 up->store = si->store;
413 camel_object_ref(up->store);
415 if ((fi->flags & CAMEL_FOLDER_NOSELECT) == 0)
416 up->add = TRUE;
418 e_dlist_addtail(&updates, (EDListNode *)up);
419 flush_updates();
423 static void
424 create_folders(CamelFolderInfo *fi, struct _store_info *si)
426 d(printf("Setup new folder: %s\n %s\n", fi->uri, fi->full_name));
428 while (fi) {
429 setup_folder(fi, si);
431 if (fi->child)
432 create_folders(fi->child, si);
434 fi = fi->next;
438 static void
439 folder_changed (CamelObject *o, gpointer event_data, gpointer user_data)
441 CamelFolderChangeInfo *changes = event_data;
442 CamelFolder *folder = (CamelFolder *)o;
443 CamelStore *store = folder->parent_store;
444 struct _store_info *si;
445 struct _folder_info *mfi;
446 int new = 0;
448 d(printf("folder '%s' changed\n", folder->full_name));
450 if (!CAMEL_IS_VEE_FOLDER(folder)
451 && folder != mail_component_get_folder(NULL, MAIL_COMPONENT_FOLDER_OUTBOX)
452 && folder != mail_component_get_folder(NULL, MAIL_COMPONENT_FOLDER_DRAFTS)
453 && folder != mail_component_get_folder(NULL, MAIL_COMPONENT_FOLDER_SENT)
454 && changes && changes->uid_added)
455 new = changes->uid_added->len;
457 LOCK(info_lock);
458 if (stores != NULL
459 && (si = g_hash_table_lookup(stores, store)) != NULL
460 && (mfi = g_hash_table_lookup(si->folders, folder->full_name)) != NULL
461 && mfi->folder == folder) {
462 update_1folder(mfi, new, NULL);
464 UNLOCK(info_lock);
467 static void
468 folder_finalised(CamelObject *o, gpointer event_data, gpointer user_data)
470 CamelFolder *folder = (CamelFolder *)o;
471 CamelStore *store = folder->parent_store;
472 struct _store_info *si;
473 struct _folder_info *mfi;
475 d(printf("Folder finalised '%s'!\n", ((CamelFolder *)o)->full_name));
476 LOCK(info_lock);
477 if (stores != NULL
478 && (si = g_hash_table_lookup(stores, store)) != NULL
479 && (mfi = g_hash_table_lookup(si->folders, folder->full_name)) != NULL
480 && mfi->folder == folder) {
481 mfi->folder = NULL;
483 UNLOCK(info_lock);
486 static void
487 folder_renamed(CamelObject *o, gpointer event_data, gpointer user_data)
489 CamelFolder *folder = (CamelFolder *)o;
490 char *old = event_data;
492 d(printf("Folder renamed from '%s' to '%s'\n", old, folder->full_name));
494 old = old;
495 folder = folder;
496 /* Dont do anything, do it from the store rename event? */
499 void mail_note_folder(CamelFolder *folder)
501 CamelStore *store = folder->parent_store;
502 struct _store_info *si;
503 struct _folder_info *mfi;
505 d(printf("noting folder '%s'\n", folder->full_name));
507 LOCK(info_lock);
508 if (stores == NULL
509 || (si = g_hash_table_lookup(stores, store)) == NULL
510 || (mfi = g_hash_table_lookup(si->folders, folder->full_name)) == NULL) {
511 w(g_warning("Noting folder before store initialised"));
512 UNLOCK(info_lock);
513 return;
516 /* dont do anything if we already have this */
517 if (mfi->folder == folder) {
518 UNLOCK(info_lock);
519 return;
522 mfi->folder = folder;
524 update_1folder(mfi, 0, NULL);
526 UNLOCK(info_lock);
528 camel_object_hook_event(folder, "folder_changed", folder_changed, NULL);
529 camel_object_hook_event(folder, "renamed", folder_renamed, NULL);
530 camel_object_hook_event(folder, "finalize", folder_finalised, NULL);
533 static void
534 store_folder_subscribed(CamelObject *o, void *event_data, void *data)
536 struct _store_info *si;
537 CamelFolderInfo *fi = event_data;
539 d(printf("Store folder subscribed '%s' store '%s' \n", fi->full_name, camel_url_to_string(((CamelService *)o)->url, 0)));
541 LOCK(info_lock);
542 si = g_hash_table_lookup(stores, o);
543 if (si)
544 setup_folder(fi, si);
545 UNLOCK(info_lock);
548 static void
549 store_folder_created(CamelObject *o, void *event_data, void *data)
551 /* we only want created events to do more work if we dont support subscriptions */
552 if (!camel_store_supports_subscriptions(CAMEL_STORE(o)))
553 store_folder_subscribed(o, event_data, data);
556 static void
557 store_folder_opened(CamelObject *o, void *event_data, void *data)
559 CamelFolder *folder = event_data;
561 mail_note_folder(folder);
564 static void
565 store_folder_unsubscribed(CamelObject *o, void *event_data, void *data)
567 struct _store_info *si;
568 CamelFolderInfo *fi = event_data;
569 struct _folder_info *mfi;
570 CamelStore *store = (CamelStore *)o;
572 d(printf("Store Folder deleted: %s\n", fi->full_name));
574 LOCK(info_lock);
575 si = g_hash_table_lookup(stores, store);
576 if (si) {
577 mfi = g_hash_table_lookup(si->folders, fi->full_name);
578 if (mfi) {
579 g_hash_table_remove(si->folders, mfi->full_name);
580 g_hash_table_remove(si->folders_uri, mfi->uri);
581 unset_folder_info(mfi, TRUE, TRUE);
582 free_folder_info(mfi);
585 UNLOCK(info_lock);
588 static void
589 store_folder_deleted(CamelObject *o, void *event_data, void *data)
591 /* we only want deleted events to do more work if we dont support subscriptions */
592 if (!camel_store_supports_subscriptions(CAMEL_STORE(o)))
593 store_folder_unsubscribed(o, event_data, data);
596 static char *
597 folder_to_url(CamelStore *store, const char *full_name)
599 CamelURL *url;
600 char *out;
602 url = camel_url_copy(((CamelService *)store)->url);
603 if (((CamelService *)store)->provider->url_flags & CAMEL_URL_FRAGMENT_IS_PATH) {
604 camel_url_set_fragment(url, full_name);
605 } else {
606 char *name = g_alloca(strlen(full_name)+2);
608 sprintf(name, "/%s", full_name);
609 camel_url_set_path(url, name);
612 out = camel_url_to_string(url, CAMEL_URL_HIDE_ALL);
613 camel_url_free(url);
615 return out;
618 static void
619 rename_folders(struct _store_info *si, const char *oldbase, const char *newbase, CamelFolderInfo *fi)
621 char *old, *olduri, *oldfile, *newuri, *newfile;
622 struct _folder_info *mfi;
623 struct _folder_update *up;
625 up = g_malloc0(sizeof(*up));
627 d(printf("oldbase '%s' newbase '%s' new '%s'\n", oldbase, newbase, fi->full_name));
629 /* Form what was the old name, and try and look it up */
630 old = g_strdup_printf("%s%s", oldbase, fi->full_name + strlen(newbase));
631 mfi = g_hash_table_lookup(si->folders, old);
632 if (mfi) {
633 d(printf("Found old folder '%s' renaming to '%s'\n", mfi->full_name, fi->full_name));
635 up->oldfull = mfi->full_name;
636 up->olduri = mfi->uri;
638 /* Its a rename op */
639 g_hash_table_remove(si->folders, mfi->full_name);
640 g_hash_table_remove(si->folders, mfi->uri);
641 mfi->full_name = g_strdup(fi->full_name);
642 mfi->uri = g_strdup(fi->uri);
643 mfi->flags = fi->flags;
645 g_hash_table_insert(si->folders, mfi->full_name, mfi);
646 g_hash_table_insert(si->folders_uri, mfi->uri, mfi);
647 } else {
648 d(printf("Rename found a new folder? old '%s' new '%s'\n", old, fi->full_name));
649 /* Its a new op */
650 mfi = g_malloc0(sizeof(*mfi));
651 mfi->full_name = g_strdup(fi->full_name);
652 mfi->uri = g_strdup(fi->uri);
653 mfi->store_info = si;
654 mfi->flags = fi->flags;
656 g_hash_table_insert(si->folders, mfi->full_name, mfi);
657 g_hash_table_insert(si->folders_uri, mfi->uri, mfi);
660 up->full_name = g_strdup(mfi->full_name);
661 up->uri = g_strdup(mfi->uri);
662 up->unread = fi->unread==-1?0:fi->unread;
663 up->store = si->store;
664 camel_object_ref(up->store);
666 if ((fi->flags & CAMEL_FOLDER_NOSELECT) == 0)
667 up->add = TRUE;
669 e_dlist_addtail(&updates, (EDListNode *)up);
670 flush_updates();
671 #if 0
672 if (fi->sibling)
673 rename_folders(si, oldbase, newbase, fi->sibling, folders);
674 if (fi->child)
675 rename_folders(si, oldbase, newbase, fi->child, folders);
676 #endif
678 /* rename the meta-data we maintain ourselves */
679 olduri = folder_to_url(si->store, old);
680 e_filename_make_safe(olduri);
681 newuri = folder_to_url(si->store, fi->full_name);
682 e_filename_make_safe(newuri);
683 oldfile = g_strdup_printf("%s/mail/config/custom_view-%s.xml", mail_component_peek_base_directory(NULL), olduri);
684 newfile = g_strdup_printf("%s/mail/config/custom_view-%s.xml", mail_component_peek_base_directory(NULL), newuri);
685 g_rename(oldfile, newfile);
686 g_free(oldfile);
687 g_free(newfile);
688 oldfile = g_strdup_printf("%s/mail/config/current_view-%s.xml", mail_component_peek_base_directory(NULL), olduri);
689 newfile = g_strdup_printf("%s/mail/config/current_view-%s.xml", mail_component_peek_base_directory(NULL), newuri);
690 g_rename(oldfile, newfile);
691 g_free(oldfile);
692 g_free(newfile);
693 g_free(olduri);
694 g_free(newuri);
696 g_free(old);
699 static void
700 get_folders(CamelFolderInfo *fi, GPtrArray *folders)
702 while (fi) {
703 g_ptr_array_add(folders, fi);
705 if (fi->child)
706 get_folders(fi->child, folders);
708 fi = fi->next;
712 static int
713 folder_cmp(const void *ap, const void *bp)
715 const CamelFolderInfo *a = ((CamelFolderInfo **)ap)[0];
716 const CamelFolderInfo *b = ((CamelFolderInfo **)bp)[0];
718 return strcmp(a->full_name, b->full_name);
721 static void
722 store_folder_renamed(CamelObject *o, void *event_data, void *data)
724 CamelStore *store = (CamelStore *)o;
725 CamelRenameInfo *info = event_data;
726 struct _store_info *si;
728 d(printf("Folder renamed: oldbase = '%s' new->full = '%s'\n", info->old_base, info->new->full_name));
730 LOCK(info_lock);
731 si = g_hash_table_lookup(stores, store);
732 if (si) {
733 GPtrArray *folders = g_ptr_array_new();
734 CamelFolderInfo *top;
735 int i;
737 /* Ok, so for some reason the folderinfo we have comes in all messed up from
738 imap, should find out why ... this makes it workable */
739 get_folders(info->new, folders);
740 qsort(folders->pdata, folders->len, sizeof(folders->pdata[0]), folder_cmp);
742 top = folders->pdata[0];
743 for (i=0;i<folders->len;i++) {
744 rename_folders(si, info->old_base, top->full_name, folders->pdata[i]);
747 g_ptr_array_free(folders, TRUE);
750 UNLOCK(info_lock);
753 struct _update_data {
754 struct _update_data *next;
755 struct _update_data *prev;
757 int id; /* id for cancellation */
758 guint cancel:1; /* also tells us we're cancelled */
760 void (*done)(CamelStore *store, CamelFolderInfo *info, void *data);
761 void *data;
764 static void
765 unset_folder_info_hash(char *path, struct _folder_info *mfi, void *data)
767 unset_folder_info(mfi, FALSE, FALSE);
770 static void
771 free_folder_info_hash(char *path, struct _folder_info *mfi, void *data)
773 free_folder_info(mfi);
776 void
777 mail_note_store_remove(CamelStore *store)
779 struct _update_data *ud;
780 struct _store_info *si;
782 g_assert(CAMEL_IS_STORE(store));
784 if (stores == NULL)
785 return;
787 d(printf("store removed!!\n"));
788 LOCK(info_lock);
789 si = g_hash_table_lookup(stores, store);
790 if (si) {
791 g_hash_table_remove(stores, store);
793 camel_object_unhook_event(store, "folder_opened", store_folder_opened, NULL);
794 camel_object_unhook_event(store, "folder_created", store_folder_created, NULL);
795 camel_object_unhook_event(store, "folder_deleted", store_folder_deleted, NULL);
796 camel_object_unhook_event(store, "folder_renamed", store_folder_renamed, NULL);
797 camel_object_unhook_event(store, "folder_subscribed", store_folder_subscribed, NULL);
798 camel_object_unhook_event(store, "folder_unsubscribed", store_folder_unsubscribed, NULL);
799 g_hash_table_foreach(si->folders, (GHFunc)unset_folder_info_hash, NULL);
801 ud = (struct _update_data *)si->folderinfo_updates.head;
802 while (ud->next) {
803 d(printf("Cancelling outstanding folderinfo update %d\n", ud->id));
804 mail_msg_cancel(ud->id);
805 ud->cancel = 1;
806 ud = ud->next;
809 camel_object_unref(si->store);
810 g_hash_table_foreach(si->folders, (GHFunc)free_folder_info_hash, NULL);
811 g_hash_table_destroy(si->folders);
812 g_hash_table_destroy(si->folders_uri);
813 g_free(si);
816 UNLOCK(info_lock);
819 static void
820 update_folders(CamelStore *store, CamelFolderInfo *fi, void *data)
822 struct _update_data *ud = data;
823 struct _store_info *si;
825 d(printf("Got folderinfo for store %s\n", store->parent_object.provider->protocol));
827 LOCK(info_lock);
828 si = g_hash_table_lookup(stores, store);
829 if (si && !ud->cancel) {
830 /* the 'si' is still there, so we can remove ourselves from its list */
831 /* otherwise its not, and we're on our own and free anyway */
832 e_dlist_remove((EDListNode *)ud);
834 if (fi)
835 create_folders(fi, si);
837 UNLOCK(info_lock);
839 if (ud->done)
840 ud->done(store, fi, ud->data);
841 g_free(ud);
845 struct _ping_store_msg {
846 struct _mail_msg msg;
848 CamelStore *store;
851 static char *
852 ping_store_desc (struct _mail_msg *mm, int done)
854 struct _ping_store_msg *m = (struct _ping_store_msg *) mm;
855 char *service_name = camel_service_get_name (CAMEL_SERVICE (m->store), TRUE);
856 char *msg;
858 msg = g_strdup_printf (_("Pinging %s"), service_name);
859 g_free (service_name);
861 return msg;
864 static void
865 ping_store_ping (struct _mail_msg *mm)
867 gboolean online = FALSE;
868 struct _ping_store_msg *m = (struct _ping_store_msg *) mm;
870 if (CAMEL_SERVICE (m->store)->status == CAMEL_SERVICE_CONNECTED) {
871 if (CAMEL_IS_DISCO_STORE (m->store) &&
872 camel_disco_store_status (CAMEL_DISCO_STORE (m->store)) != CAMEL_DISCO_STORE_OFFLINE)
873 online = TRUE;
874 else if (CAMEL_IS_OFFLINE_STORE (m->store) &&
875 CAMEL_OFFLINE_STORE (m->store)->state != CAMEL_OFFLINE_STORE_NETWORK_UNAVAIL)
876 online = TRUE;
878 if (online)
879 camel_store_noop (m->store, &mm->ex);
882 static void
883 ping_store_free (struct _mail_msg *mm)
885 struct _ping_store_msg *m = (struct _ping_store_msg *) mm;
887 camel_object_unref (m->store);
890 static struct _mail_msg_op ping_store_op = {
891 ping_store_desc,
892 ping_store_ping,
893 NULL,
894 ping_store_free
897 static void
898 ping_store (gpointer key, gpointer val, gpointer user_data)
900 CamelStore *store = (CamelStore *) key;
901 struct _ping_store_msg *m;
903 if (CAMEL_SERVICE (store)->status != CAMEL_SERVICE_CONNECTED)
904 return;
906 m = mail_msg_new (&ping_store_op, NULL, sizeof (struct _ping_store_msg));
907 m->store = store;
908 camel_object_ref (store);
910 e_thread_put (mail_thread_queued_slow, (EMsg *) m);
913 static gboolean
914 ping_cb (gpointer user_data)
916 LOCK (info_lock);
918 g_hash_table_foreach (stores, ping_store, NULL);
920 UNLOCK (info_lock);
922 return TRUE;
925 static void
926 store_online_cb (CamelStore *store, void *data)
928 struct _update_data *ud = data;
930 LOCK(info_lock);
932 if (g_hash_table_lookup(stores, store) != NULL && !ud->cancel) {
933 /* re-use the cancel id. we're already in the store update list too */
934 ud->id = mail_get_folderinfo(store, NULL, update_folders, ud);
935 } else {
936 /* the store vanished, that means we were probably cancelled, or at any rate,
937 need to clean ourselves up */
938 g_free(ud);
941 UNLOCK(info_lock);
944 void
945 mail_note_store(CamelStore *store, CamelOperation *op,
946 void (*done)(CamelStore *store, CamelFolderInfo *info, void *data), void *data)
948 struct _store_info *si;
949 struct _update_data *ud;
950 const char *buf;
951 guint timeout;
952 int hook = 0;
954 g_assert(CAMEL_IS_STORE(store));
955 g_assert(pthread_equal(pthread_self(), mail_gui_thread));
957 LOCK(info_lock);
959 if (stores == NULL) {
960 stores = g_hash_table_new(NULL, NULL);
961 count_sent = getenv("EVOLUTION_COUNT_SENT") != NULL;
962 count_trash = getenv("EVOLUTION_COUNT_TRASH") != NULL;
963 buf = getenv ("EVOLUTION_PING_TIMEOUT");
964 timeout = buf ? strtoul (buf, NULL, 10) * 1000 : 600000;
965 ping_id = g_timeout_add (timeout, ping_cb, NULL);
968 si = g_hash_table_lookup(stores, store);
969 if (si == NULL) {
970 d(printf("Noting a new store: %p: %s\n", store, camel_url_to_string(((CamelService *)store)->url, 0)));
972 si = g_malloc0(sizeof(*si));
973 si->folders = g_hash_table_new(g_str_hash, g_str_equal);
974 si->folders_uri = g_hash_table_new(CAMEL_STORE_CLASS(CAMEL_OBJECT_GET_CLASS(store))->hash_folder_name,
975 CAMEL_STORE_CLASS(CAMEL_OBJECT_GET_CLASS(store))->compare_folder_name);
976 si->store = store;
977 camel_object_ref((CamelObject *)store);
978 g_hash_table_insert(stores, store, si);
979 e_dlist_init(&si->folderinfo_updates);
980 hook = TRUE;
983 ud = g_malloc(sizeof(*ud));
984 ud->done = done;
985 ud->data = data;
986 ud->cancel = 0;
988 /* We might get a race when setting up a store, such that it is still left in offline mode,
989 after we've gone online. This catches and fixes it up when the shell opens us */
990 if (CAMEL_IS_DISCO_STORE (store)) {
991 if (camel_session_is_online (session)
992 && camel_disco_store_status (CAMEL_DISCO_STORE (store)) == CAMEL_DISCO_STORE_OFFLINE) {
993 /* Note: we use the 'id' here, even though its not the right id, its still ok */
994 ud->id = mail_store_set_offline (store, FALSE, store_online_cb, ud);
995 } else {
996 goto normal_setup;
998 } else if (CAMEL_IS_OFFLINE_STORE (store)) {
999 if (camel_session_is_online (session) && CAMEL_OFFLINE_STORE (store)->state == CAMEL_OFFLINE_STORE_NETWORK_UNAVAIL) {
1000 /* Note: we use the 'id' here, even though its not the right id, its still ok */
1001 ud->id = mail_store_set_offline (store, FALSE, store_online_cb, ud);
1002 } else {
1003 goto normal_setup;
1005 } else {
1006 normal_setup:
1007 ud->id = mail_get_folderinfo (store, op, update_folders, ud);
1010 e_dlist_addtail (&si->folderinfo_updates, (EDListNode *) ud);
1012 UNLOCK(info_lock);
1014 /* there is potential for race here, but it is safe as we check for the store anyway */
1015 if (hook) {
1016 camel_object_hook_event(store, "folder_opened", store_folder_opened, NULL);
1017 camel_object_hook_event(store, "folder_created", store_folder_created, NULL);
1018 camel_object_hook_event(store, "folder_deleted", store_folder_deleted, NULL);
1019 camel_object_hook_event(store, "folder_renamed", store_folder_renamed, NULL);
1020 camel_object_hook_event(store, "folder_subscribed", store_folder_subscribed, NULL);
1021 camel_object_hook_event(store, "folder_unsubscribed", store_folder_unsubscribed, NULL);
1025 struct _find_info {
1026 const char *uri;
1027 struct _folder_info *fi;
1028 CamelURL *url;
1031 /* look up on each storeinfo using proper hash function for that stores uri's */
1032 static void storeinfo_find_folder_info(CamelStore *store, struct _store_info *si, struct _find_info *fi)
1034 if (fi->fi == NULL) {
1035 if (((CamelService *)store)->provider->url_equal(fi->url, ((CamelService *)store)->url)) {
1036 char *path = fi->url->fragment?fi->url->fragment:fi->url->path;
1038 if (path[0] == '/')
1039 path++;
1040 fi->fi = g_hash_table_lookup(si->folders, path);
1045 /* returns TRUE if the uri is available, folderp is set to a
1046 reffed folder if the folder has also already been opened */
1047 int mail_note_get_folder_from_uri(const char *uri, CamelFolder **folderp)
1049 struct _find_info fi = { uri, NULL, NULL };
1051 if (stores == NULL)
1052 return FALSE;
1054 fi.url = camel_url_new(uri, NULL);
1056 LOCK(info_lock);
1057 g_hash_table_foreach(stores, (GHFunc)storeinfo_find_folder_info, &fi);
1058 if (folderp) {
1059 if (fi.fi && fi.fi->folder) {
1060 *folderp = fi.fi->folder;
1061 camel_object_ref(*folderp);
1062 } else {
1063 *folderp = NULL;
1066 UNLOCK(info_lock);
1068 camel_url_free(fi.url);
1070 return fi.fi != NULL;