1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
/*
* e-mail-store.c
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with the program; if not, see <http://www.gnu.org/licenses/>
*
*
* Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
*
*/
#include "e-mail-store.h"
#include <glib/gi18n.h>
#include <camel/camel.h>
#include <libedataserver/e-account.h>
#include <libedataserver/e-account-list.h>
#include "e-util/e-account-utils.h"
#include "mail/e-mail-local.h"
#include "mail/em-folder-tree-model.h"
#include "mail/mail-folder-cache.h"
#include "mail/mail-mt.h"
#include "mail/mail-session.h"
typedef struct _StoreInfo StoreInfo;
typedef void (*AddStoreCallback) (CamelStore *store,
CamelFolderInfo *info,
StoreInfo *store_info);
struct _StoreInfo {
gint ref_count;
CamelStore *store;
gchar *display_name;
/* Hold a reference to keep them alive. */
CamelFolder *vtrash;
CamelFolder *vjunk;
AddStoreCallback callback;
guint removed : 1;
};
CamelStore *vfolder_store; /* XXX write a get () function for this */
static GHashTable *store_table;
static StoreInfo *
store_info_new (CamelStore *store,
const gchar *display_name)
{
CamelService *service;
StoreInfo *store_info;
g_return_val_if_fail (CAMEL_IS_STORE (store), NULL);
service = CAMEL_SERVICE (store);
store_info = g_slice_new0 (StoreInfo);
store_info->ref_count = 1;
g_object_ref (store);
store_info->store = store;
if (display_name == NULL)
store_info->display_name =
camel_service_get_name (service, TRUE);
else
store_info->display_name = g_strdup (display_name);
/* If these are vfolders then they need to be opened now,
* otherwise they won't keep track of all folders. */
if (store->flags & CAMEL_STORE_VTRASH)
store_info->vtrash =
camel_store_get_trash_folder_sync (store, NULL, NULL);
if (store->flags & CAMEL_STORE_VJUNK)
store_info->vjunk =
camel_store_get_junk_folder_sync (store, NULL, NULL);
return store_info;
}
static StoreInfo *
store_info_ref (StoreInfo *store_info)
{
g_return_val_if_fail (store_info != NULL, store_info);
g_return_val_if_fail (store_info->ref_count > 0, store_info);
g_atomic_int_add (&store_info->ref_count, 1);
return store_info;
}
static void
store_info_unref (StoreInfo *store_info)
{
g_return_if_fail (store_info != NULL);
g_return_if_fail (store_info->ref_count > 0);
if (g_atomic_int_exchange_and_add (&store_info->ref_count, -1) > 1)
return;
g_object_unref (store_info->store);
g_free (store_info->display_name);
if (store_info->vtrash != NULL)
g_object_unref (store_info->vtrash);
if (store_info->vjunk != NULL)
g_object_unref (store_info->vjunk);
g_slice_free (StoreInfo, store_info);
}
static void
store_table_free (StoreInfo *store_info)
{
store_info->removed = 1;
store_info_unref (store_info);
}
static gboolean
mail_store_note_store_cb (CamelStore *store,
CamelFolderInfo *info,
gpointer user_data)
{
StoreInfo *store_info = user_data;
if (store_info->callback != NULL)
store_info->callback (store, info, store_info);
if (!store_info->removed) {
/* This keeps message counters up-to-date. */
if (store_info->vtrash != NULL)
mail_folder_cache_note_folder (
mail_folder_cache_get_default (),
store_info->vtrash);
if (store_info->vjunk != NULL)
mail_folder_cache_note_folder (
mail_folder_cache_get_default (),
store_info->vjunk);
}
store_info_unref (store_info);
return TRUE;
}
static void
mail_store_add (CamelStore *store,
const gchar *display_name,
AddStoreCallback callback)
{
EMFolderTreeModel *default_model;
StoreInfo *store_info;
g_return_if_fail (store_table != NULL);
default_model = em_folder_tree_model_get_default ();
store_info = store_info_new (store, display_name);
store_info->callback = callback;
g_hash_table_insert (store_table, store, store_info);
em_folder_tree_model_add_store (
default_model, store, store_info->display_name);
mail_folder_cache_note_store (
mail_folder_cache_get_default (),
store, NULL,
mail_store_note_store_cb,
store_info_ref (store_info));
}
static void
mail_store_add_local_done_cb (CamelStore *store,
CamelFolderInfo *info,
StoreInfo *store_info)
{
CamelFolder *folder;
gint ii;
for (ii = 0; ii < E_MAIL_NUM_LOCAL_FOLDERS; ii++) {
folder = e_mail_local_get_folder (ii);
if (folder != NULL)
mail_folder_cache_note_folder (
mail_folder_cache_get_default (), folder);
}
}
static void
mail_store_load_accounts (const gchar *data_dir)
{
CamelStore *local_store;
EAccountList *account_list;
EIterator *iter;
/* Set up the local store. */
e_mail_local_init (data_dir);
local_store = e_mail_local_get_store ();
mail_store_add (
local_store, _("On This Computer"),
(AddStoreCallback) mail_store_add_local_done_cb);
/* Set up remote stores. */
account_list = e_get_account_list ();
for (iter = e_list_get_iterator ((EList *) account_list);
e_iterator_is_valid (iter); e_iterator_next (iter)) {
EAccountService *service;
EAccount *account;
const gchar *display_name;
const gchar *uri;
account = (EAccount *) e_iterator_get (iter);
display_name = account->name;
service = account->source;
uri = service->url;
if (!account->enabled)
continue;
if (uri == NULL || *uri == '\0')
continue;
/* HACK: mbox URI's are handled by the local store setup
* above. Any that come through as account sources
* are really movemail sources! */
if (g_str_has_prefix (uri, "mbox:"))
continue;
e_mail_store_add_by_uri (uri, display_name);
}
g_object_unref (iter);
}
void
e_mail_store_init (const gchar *data_dir)
{
static gboolean initialized = FALSE;
g_return_if_fail (data_dir != NULL);
/* This function is idempotent, but there should
* be no need to call it more than once. */
if (initialized)
return;
/* Initialize global variables. */
store_table = g_hash_table_new_full (
g_direct_hash, g_direct_equal,
(GDestroyNotify) NULL,
(GDestroyNotify) store_table_free);
mail_store_load_accounts (data_dir);
initialized = TRUE;
}
void
e_mail_store_add (CamelStore *store,
const gchar *display_name)
{
g_return_if_fail (CAMEL_IS_STORE (store));
g_return_if_fail (display_name != NULL);
mail_store_add (store, display_name, NULL);
}
CamelStore *
e_mail_store_add_by_uri (const gchar *uri,
const gchar *display_name)
{
CamelService *service;
CamelProvider *provider;
GError *local_error = NULL;
g_return_val_if_fail (uri != NULL, NULL);
g_return_val_if_fail (display_name != NULL, NULL);
/* Load the service, but don't connect. Check its provider,
* and if this belongs in the folder tree model, add it. */
provider = camel_provider_get (uri, &local_error);
if (provider == NULL)
goto fail;
if (!(provider->flags & CAMEL_PROVIDER_IS_STORAGE))
return NULL;
service = camel_session_get_service (
session, uri, CAMEL_PROVIDER_STORE, &local_error);
if (service == NULL)
goto fail;
e_mail_store_add (CAMEL_STORE (service), display_name);
g_object_unref (service);
return CAMEL_STORE (service);
fail:
/* FIXME: Show an error dialog. */
g_warning (
"Couldn't get service: %s: %s", uri,
local_error->message);
g_error_free (local_error);
return NULL;
}
void
e_mail_store_remove (CamelStore *store)
{
EMFolderTreeModel *default_model;
g_return_if_fail (CAMEL_IS_STORE (store));
g_return_if_fail (store_table != NULL);
/* Because the store table holds a reference to each store used
* as a key in it, none of them will ever be gc'ed, meaning any
* call to camel_session_get_{service,store} with the same URL
* will always return the same object. So this works. */
if (g_hash_table_lookup (store_table, store) == NULL)
return;
g_object_ref (store);
g_hash_table_remove (store_table, store);
mail_folder_cache_note_store_remove (
mail_folder_cache_get_default (), store);
default_model = em_folder_tree_model_get_default ();
em_folder_tree_model_remove_store (default_model, store);
camel_service_disconnect_sync (CAMEL_SERVICE (store), TRUE, NULL);
g_object_unref (store);
}
void
e_mail_store_remove_by_uri (const gchar *uri)
{
CamelService *service;
CamelProvider *provider;
g_return_if_fail (uri != NULL);
provider = camel_provider_get (uri, NULL);
if (provider == NULL)
return;
if (!(provider->flags & CAMEL_PROVIDER_IS_STORAGE))
return;
service = camel_session_get_service (
session, uri, CAMEL_PROVIDER_STORE, NULL);
if (service == NULL)
return;
e_mail_store_remove (CAMEL_STORE (service));
g_object_unref (service);
}
void
e_mail_store_foreach (GHFunc func,
gpointer user_data)
{
GHashTableIter iter;
gpointer key, value;
g_return_if_fail (func != NULL);
g_return_if_fail (store_table != NULL);
g_hash_table_iter_init (&iter, store_table);
while (g_hash_table_iter_next (&iter, &key, &value)) {
StoreInfo *store_info = value;
/* Just being paranoid. */
g_return_if_fail (CAMEL_IS_STORE (key));
g_return_if_fail (store_info != NULL);
func (key, store_info->display_name, user_data);
}
}
|