Debian Codegraph @

src/EWS/camel/camel-ews-store.c

raw
1 /*
2 * SPDX-FileCopyrightText: (C) 1999-2008 Novell, Inc. (www.novell.com)
3 * SPDX-FileContributor: Chenthill Palanisamy <[email protected]>
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6
7 #include "evolution-ews-config.h"
8
9 #include <errno.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/stat.h>
14 #include <sys/types.h>
15 #include <unistd.h>
16
17 #include <glib/gi18n-lib.h>
18 #include <glib/gstdio.h>
19
20 #include "common/camel-ews-settings.h"
21 #include "common/e-ews-item-change.h"
22 #include "common/e-ews-request.h"
23 #include "common/e-ews-oof-settings.h"
24
25 #include "camel-ews-folder.h"
26 #include "camel-ews-store.h"
27 #include "camel-ews-summary.h"
28 #include "camel-ews-utils.h"
29 #include "camel-ews-enumtypes.h"
30
31 #ifdef G_OS_WIN32
32 #include <winsock2.h>
33 #include <ws2tcpip.h>
34 #endif
35
36 #define d(x) x
37 #define CURSOR_ITEM_LIMIT 100
38
39 #define FINFO_REFRESH_INTERVAL 60
40
41 #define UPDATE_LOCK(x) (g_rec_mutex_lock(&(x)->priv->update_lock))
42 #define UPDATE_UNLOCK(x) (g_rec_mutex_unlock(&(x)->priv->update_lock))
43
44 struct _CamelEwsStorePrivate {
45 time_t last_refresh_time;
46 GMutex get_finfo_lock;
47 EEwsConnection *connection;
48 GMutex connection_lock;
49 gboolean has_ooo_set;
50 CamelEwsStoreOooAlertState ooo_alert_state;
51 gint password_expires_in_days;
52
53 gboolean listen_notifications;
54 guint subscription_key;
55
56 /* The subscription ID is not tight to the actual connection, it survives
57 disconnects, thus remember it and pass it back to the new connection,
58 thus it can eventually unsubscribe from it. */
59 gchar *last_subscription_id;
60
61 guint update_folder_id;
62 guint update_folder_list_id;
63 GCancellable *updates_cancellable;
64 GSList *update_folder_names;
65 GRecMutex update_lock;
66
67 GSList *public_folders; /* EEwsFolder * objects */
68 };
69
70 static gboolean ews_store_construct (CamelService *service, CamelSession *session,
71 CamelProvider *provider, GError **error);
72
73 static void camel_ews_store_initable_init (GInitableIface *iface);
74 static void camel_ews_subscribable_init (CamelSubscribableInterface *iface);
75 static GInitableIface *parent_initable_interface;
76
77 static CamelFolderInfo *folder_info_from_store_summary (CamelEwsStore *store,
78 const gchar *top,
79 guint32 flags,
80 GCancellable *cancellable,
81 GError **error);
82
83 enum {
84 PROP_0,
85 PROP_HAS_OOO_SET,
86 PROP_OOO_ALERT_STATE,
87 PROP_CONNECTABLE,
88 PROP_HOST_REACHABLE
89 };
90
91 G_DEFINE_TYPE_WITH_CODE (CamelEwsStore, camel_ews_store, CAMEL_TYPE_OFFLINE_STORE,
92 G_ADD_PRIVATE (CamelEwsStore)
93 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, camel_ews_store_initable_init)
94 G_IMPLEMENT_INTERFACE (CAMEL_TYPE_NETWORK_SERVICE, NULL)
95 G_IMPLEMENT_INTERFACE (CAMEL_TYPE_SUBSCRIBABLE, camel_ews_subscribable_init))
96
97 static void
98 ews_store_set_property (GObject *object,
99 guint property_id,
100 const GValue *value,
101 GParamSpec *pspec)
102 {
103 switch (property_id) {
104 case PROP_HAS_OOO_SET:
105 camel_ews_store_set_has_ooo_set (
106 CAMEL_EWS_STORE (object),
107 g_value_get_boolean (value));
108 return;
109 case PROP_OOO_ALERT_STATE:
110 camel_ews_store_set_ooo_alert_state (
111 CAMEL_EWS_STORE (object),
112 g_value_get_enum (value));
113 return;
114 case PROP_CONNECTABLE:
115 camel_network_service_set_connectable (
116 CAMEL_NETWORK_SERVICE (object),
117 g_value_get_object (value));
118 return;
119 }
120
121 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
122 }
123
124 static void
125 ews_store_get_property (GObject *object,
126 guint property_id,
127 GValue *value,
128 GParamSpec *pspec)
129 {
130 switch (property_id) {
131 case PROP_HAS_OOO_SET:
132 g_value_set_boolean (
133 value,
134 camel_ews_store_get_has_ooo_set (
135 CAMEL_EWS_STORE (object)));
136 return;
137 case PROP_OOO_ALERT_STATE:
138 g_value_set_enum (
139 value,
140 camel_ews_store_get_ooo_alert_state (
141 CAMEL_EWS_STORE (object)));
142 return;
143 case PROP_CONNECTABLE:
144 g_value_take_object (
145 value,
146 camel_network_service_ref_connectable (
147 CAMEL_NETWORK_SERVICE (object)));
148 return;
149 case PROP_HOST_REACHABLE:
150 g_value_set_boolean (
151 value,
152 camel_network_service_get_host_reachable (
153 CAMEL_NETWORK_SERVICE (object)));
154 return;
155 }
156
157 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
158 }
159
160 void
161 camel_ews_store_set_has_ooo_set (CamelEwsStore *ews_store,
162 gboolean has_ooo_set)
163 {
164 g_return_if_fail (CAMEL_IS_EWS_STORE (ews_store));
165
166 if ((ews_store->priv->has_ooo_set ? 1 : 0) == (has_ooo_set ? 1 : 0))
167 return;
168
169 ews_store->priv->has_ooo_set = has_ooo_set;
170 g_object_notify (G_OBJECT (ews_store), "has-ooo-set");
171 }
172
173 gboolean
174 camel_ews_store_get_has_ooo_set (const CamelEwsStore *ews_store)
175 {
176 g_return_val_if_fail (CAMEL_IS_EWS_STORE (ews_store), FALSE);
177
178 return ews_store->priv->has_ooo_set;
179 }
180
181 void
182 camel_ews_store_set_ooo_alert_state (CamelEwsStore *ews_store,
183 CamelEwsStoreOooAlertState state)
184 {
185 g_return_if_fail (CAMEL_IS_EWS_STORE (ews_store));
186
187 if (ews_store->priv->ooo_alert_state == state)
188 return;
189
190 ews_store->priv->ooo_alert_state = state;
191 g_object_notify (G_OBJECT (ews_store), "ooo-alert-state");
192 }
193
194 CamelEwsStoreOooAlertState
195 camel_ews_store_get_ooo_alert_state (const CamelEwsStore *ews_store)
196 {
197 g_return_val_if_fail (
198 CAMEL_IS_EWS_STORE (ews_store),
199 CAMEL_EWS_STORE_OOO_ALERT_STATE_UNKNOWN);
200
201 return ews_store->priv->ooo_alert_state;
202 }
203
204 static void
205 ews_migrate_to_user_cache_dir (CamelService *service)
206 {
207 const gchar *user_data_dir, *user_cache_dir;
208
209 g_return_if_fail (service != NULL);
210 g_return_if_fail (CAMEL_IS_SERVICE (service));
211
212 user_data_dir = camel_service_get_user_data_dir (service);
213 user_cache_dir = camel_service_get_user_cache_dir (service);
214
215 g_return_if_fail (user_data_dir != NULL);
216 g_return_if_fail (user_cache_dir != NULL);
217
218 /* migrate only if the source directory exists and the destination doesn't */
219 if (g_file_test (user_data_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) &&
220 !g_file_test (user_cache_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
221 gchar *parent_dir;
222
223 parent_dir = g_path_get_dirname (user_cache_dir);
224 g_mkdir_with_parents (parent_dir, S_IRWXU);
225 g_free (parent_dir);
226
227 if (g_rename (user_data_dir, user_cache_dir) == -1) {
228 g_debug ("%s: Failed to migrate '%s' to '%s': %s", G_STRFUNC, user_data_dir, user_cache_dir, g_strerror (errno));
229 } else {
230 gchar *old_summary_file = g_build_filename (user_cache_dir, "folder-tree-v2", NULL);
231
232 if (old_summary_file && g_file_test (old_summary_file, G_FILE_TEST_EXISTS)) {
233 gchar *new_summary_file = g_build_filename (user_cache_dir, "folder-tree", NULL);
234
235 if (new_summary_file && g_rename (old_summary_file, new_summary_file) == -1)
236 g_debug ("%s: Failed to migrate '%s' to '%s': %s", G_STRFUNC, old_summary_file, new_summary_file, g_strerror (errno));
237
238 g_free (new_summary_file);
239 }
240
241 g_free (old_summary_file);
242 }
243 }
244 }
245
246 static gboolean
247 ews_store_initable_init (GInitable *initable,
248 GCancellable *cancellable,
249 GError **error)
250 {
251 CamelService *service;
252 CamelSession *session;
253 CamelStore *store;
254 gboolean ret;
255
256 store = CAMEL_STORE (initable);
257 service = CAMEL_SERVICE (initable);
258
259 camel_store_set_flags (store, camel_store_get_flags (store) | CAMEL_STORE_USE_CACHE_DIR | CAMEL_STORE_SUPPORTS_INITIAL_SETUP);
260 ews_migrate_to_user_cache_dir (service);
261
262 camel_store_set_flags (store, camel_store_get_flags (store) | CAMEL_STORE_CAN_DELETE_FOLDERS_AT_ONCE);
263
264 /* Chain up to parent interface's init() method. */
265 if (!parent_initable_interface->init (initable, cancellable, error))
266 return FALSE;
267
268 session = camel_service_ref_session (service);
269
270 ret = ews_store_construct (service, session, NULL, error);
271
272 g_object_unref (session);
273
274 /* Add transport here ? */
275
276 return ret;
277 }
278
279 static void
280 camel_ews_store_initable_init (GInitableIface *iface)
281 {
282 parent_initable_interface = g_type_interface_peek_parent (iface);
283
284 iface->init = ews_store_initable_init;
285 }
286
287 static gboolean
288 ews_store_construct (CamelService *service,
289 CamelSession *session,
290 CamelProvider *provider,
291 GError **error)
292 {
293 CamelEwsStore *ews_store;
294 gchar *summary_file, *session_storage_path;
295 guint32 store_flags;
296
297 ews_store = (CamelEwsStore *) service;
298
299 store_flags = camel_store_get_flags (CAMEL_STORE (ews_store));
300
301 /* Disable virtual trash and junk folders. Exchange has real
302 * folders for that */
303 store_flags &= ~(CAMEL_STORE_VTRASH | CAMEL_STORE_VJUNK);
304 store_flags |= CAMEL_STORE_REAL_JUNK_FOLDER;
305 camel_store_set_flags (CAMEL_STORE (ews_store), store_flags);
306
307 /*storage path*/
308 session_storage_path = g_strdup (camel_service_get_user_cache_dir (service));
309 if (!session_storage_path) {
310 g_set_error (
311 error, CAMEL_STORE_ERROR,
312 CAMEL_STORE_ERROR_INVALID,
313 _("Session has no storage path"));
314 return FALSE;
315 }
316 ews_store->storage_path = session_storage_path;
317
318 /* Note. update account-listener plugin if filename is changed here, as it would remove the summary
319 * by forming the path itself */
320 g_mkdir_with_parents (ews_store->storage_path, 0700);
321 summary_file = g_build_filename (ews_store->storage_path, "folder-tree", NULL);
322 ews_store->summary = camel_ews_store_summary_new (summary_file);
323 camel_ews_store_summary_load (ews_store->summary, NULL);
324
325 g_free (summary_file);
326 return TRUE;
327 }
328
329 /* returns NULL when it's safe to use the default "Public Folders" name; otherwise g_free() it */
330 static gchar *
331 ews_store_get_public_folders_name (CamelEwsStore *ews_store)
332 {
333 gchar *use_name = NULL;
334 gchar *tmp_fid;
335 gint counter = 0;
336
337 tmp_fid = camel_ews_store_summary_get_folder_id_from_name (
338 ews_store->summary, EWS_PUBLIC_FOLDER_ROOT_DISPLAY_NAME);
339 while (tmp_fid) {
340 counter++;
341
342 g_free (tmp_fid);
343 g_free (use_name);
344
345 use_name = g_strdup_printf (
346 /* Translators: This composes a "Public Folders" folder name for case when
347 * user has such in his store already. The %s is replaced with "Public Folders",
348 * the %d with counter, thus it composes name like "Public Folders_1"
349 */
350 C_("PublicFolders", "%s_%d"),
351 EWS_PUBLIC_FOLDER_ROOT_DISPLAY_NAME, counter);
352
353 tmp_fid = camel_ews_store_summary_get_folder_id_from_name (ews_store->summary, use_name);
354 }
355
356 return use_name;
357 }
358
359 static gboolean
360 ews_store_show_public_folders (CamelEwsStore *ews_store)
361 {
362 CamelSettings *settings;
363 gboolean show;
364
365 settings = camel_service_ref_settings (CAMEL_SERVICE (ews_store));
366
367 show = settings && camel_ews_settings_get_show_public_folders (CAMEL_EWS_SETTINGS (settings));
368
369 g_clear_object (&settings);
370
371 return show;
372 }
373
374 void
375 camel_ews_store_ensure_virtual_folders (CamelEwsStore *ews_store)
376 {
377 gboolean needs_foreign = FALSE, has_foreign = FALSE;
378 gboolean needs_public = FALSE, has_public = FALSE;
379 gboolean show_public_folders;
380 CamelFolderInfo *fi;
381 GSList *folders, *iter;
382 GHashTable *children_count;
383 GHashTableIter tab_iter;
384 gpointer key, value;
385
386 g_return_if_fail (CAMEL_IS_EWS_STORE (ews_store));
387
388 folders = camel_ews_store_summary_get_folders (ews_store->summary, NULL, FALSE);
389 if (!folders)
390 return;
391
392 show_public_folders = ews_store_show_public_folders (ews_store);
393 children_count = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
394
395 for (iter = folders; iter; iter = iter->next) {
396 const gchar *fid = iter->data;
397 GError *error = NULL;
398
399 if (!fid)
400 continue;
401
402 if (g_str_has_prefix (fid, "ForeignMailbox::") &&
403 !g_hash_table_contains (children_count, fid))
404 g_hash_table_insert (children_count, g_strdup (fid), GINT_TO_POINTER (0));
405
406 if (g_str_equal (fid, EWS_PUBLIC_FOLDER_ROOT_ID) &&
407 !g_hash_table_contains (children_count, fid))
408 g_hash_table_insert (children_count, g_strdup (fid), GINT_TO_POINTER (0));
409
410 if (!has_foreign && g_str_equal (fid, EWS_FOREIGN_FOLDER_ROOT_ID))
411 has_foreign = TRUE;
412 else if (camel_ews_store_summary_get_foreign (ews_store->summary, fid, &error) && !error) {
413 gchar *pfid;
414
415 needs_foreign = TRUE;
416
417 pfid = camel_ews_store_summary_get_parent_folder_id (ews_store->summary, fid, NULL);
418 if (pfid && g_str_has_prefix (pfid, "ForeignMailbox::")) {
419 gint count = GPOINTER_TO_INT (g_hash_table_lookup (children_count, pfid));
420
421 g_hash_table_insert (children_count, pfid, GINT_TO_POINTER (count + 1));
422 } else {
423 g_free (pfid);
424 }
425 }
426
427 g_clear_error (&error);
428
429 if (!has_public && g_str_equal (fid, EWS_PUBLIC_FOLDER_ROOT_ID))
430 has_public = TRUE;
431 else if (camel_ews_store_summary_get_public (ews_store->summary, fid, &error) && !error) {
432 EEwsFolderType ftype;
433 gchar *pfid;
434
435 ftype = camel_ews_store_summary_get_folder_type (ews_store->summary, fid, &error);
436 if (ftype == E_EWS_FOLDER_TYPE_MAILBOX && !error) {
437 guint64 fflags;
438
439 fflags = camel_ews_store_summary_get_folder_flags (ews_store->summary, fid, &error);
440 if (show_public_folders || ((fflags & CAMEL_FOLDER_SUBSCRIBED) != 0 && !error)) {
441 needs_public = TRUE;
442
443 pfid = camel_ews_store_summary_get_parent_folder_id (ews_store->summary, fid, NULL);
444 if (pfid && g_str_equal (pfid, EWS_PUBLIC_FOLDER_ROOT_ID)) {
445 gint count = GPOINTER_TO_INT (g_hash_table_lookup (children_count, pfid));
446
447 g_hash_table_insert (children_count, pfid, GINT_TO_POINTER (count + 1));
448 } else {
449 g_free (pfid);
450 }
451 }
452 }
453 }
454
455 g_clear_error (&error);
456 }
457
458 needs_public = needs_public || show_public_folders;
459
460 g_hash_table_iter_init (&tab_iter, children_count);
461 while (g_hash_table_iter_next (&tab_iter, &key, &value)) {
462 gint count = GPOINTER_TO_INT (value);
463
464 if (!count) {
465 if (needs_public && g_str_equal (key, EWS_PUBLIC_FOLDER_ROOT_ID))
466 continue;
467
468 if (has_foreign && g_str_equal (key, EWS_FOREIGN_FOLDER_ROOT_ID))
469 has_foreign = FALSE;
470
471 if (has_public && g_str_equal (key, EWS_PUBLIC_FOLDER_ROOT_ID))
472 has_public = FALSE;
473
474 fi = camel_ews_utils_build_folder_info (ews_store, key);
475 camel_ews_store_summary_remove_folder (ews_store->summary, key, NULL);
476
477 camel_subscribable_folder_unsubscribed (CAMEL_SUBSCRIBABLE (ews_store), fi);
478 camel_store_folder_deleted (CAMEL_STORE (ews_store), fi);
479 camel_folder_info_free (fi);
480 }
481 }
482
483 g_hash_table_destroy (children_count);
484
485 if (needs_foreign && !has_foreign) {
486 gchar *use_name = NULL;
487 gchar *tmp_fid;
488 gint counter = 0;
489
490 tmp_fid = camel_ews_store_summary_get_folder_id_from_name (
491 ews_store->summary, EWS_FOREIGN_FOLDER_ROOT_DISPLAY_NAME);
492 while (tmp_fid) {
493 counter++;
494
495 g_free (tmp_fid);
496 g_free (use_name);
497
498 use_name = g_strdup_printf (
499 /* Translators: This composes a "Foreign Folders" folder name for case when
500 * user has such in his store already. The %s is replaced with "Foreign Folders",
501 * the %d with counter, thus it composes name like "Foreign Folders_1"
502 */
503 C_("ForeignFolders", "%s_%d"),
504 EWS_FOREIGN_FOLDER_ROOT_DISPLAY_NAME, counter);
505
506 tmp_fid = camel_ews_store_summary_get_folder_id_from_name (ews_store->summary, use_name);
507 }
508
509 camel_ews_store_summary_new_folder (
510 ews_store->summary,
511 EWS_FOREIGN_FOLDER_ROOT_ID, NULL, NULL,
512 use_name ? use_name : EWS_FOREIGN_FOLDER_ROOT_DISPLAY_NAME,
513 E_EWS_FOLDER_TYPE_MAILBOX,
514 CAMEL_FOLDER_SYSTEM | CAMEL_FOLDER_NOSELECT,
515 0, FALSE, FALSE);
516
517 g_free (use_name);
518
519 fi = camel_ews_utils_build_folder_info (ews_store, EWS_FOREIGN_FOLDER_ROOT_ID);
520 camel_store_folder_created (CAMEL_STORE (ews_store), fi);
521 camel_subscribable_folder_subscribed (CAMEL_SUBSCRIBABLE (ews_store), fi);
522 camel_folder_info_free (fi);
523 } else if (has_foreign && !needs_foreign) {
524 fi = camel_ews_utils_build_folder_info (ews_store, EWS_FOREIGN_FOLDER_ROOT_ID);
525 camel_ews_store_summary_remove_folder (ews_store->summary, EWS_FOREIGN_FOLDER_ROOT_ID, NULL);
526
527 camel_subscribable_folder_unsubscribed (CAMEL_SUBSCRIBABLE (ews_store), fi);
528 camel_store_folder_deleted (CAMEL_STORE (ews_store), fi);
529 camel_folder_info_free (fi);
530 }
531
532 if (needs_public && !has_public) {
533 gchar *use_name;
534
535 use_name = ews_store_get_public_folders_name (ews_store);
536
537 camel_ews_store_summary_new_folder (
538 ews_store->summary,
539 EWS_PUBLIC_FOLDER_ROOT_ID, NULL, NULL,
540 use_name ? use_name : EWS_PUBLIC_FOLDER_ROOT_DISPLAY_NAME,
541 E_EWS_FOLDER_TYPE_MAILBOX,
542 CAMEL_FOLDER_SYSTEM | CAMEL_FOLDER_NOSELECT,
543 0, FALSE, FALSE);
544
545 g_free (use_name);
546
547 fi = camel_ews_utils_build_folder_info (ews_store, EWS_PUBLIC_FOLDER_ROOT_ID);
548 camel_store_folder_created (CAMEL_STORE (ews_store), fi);
549 camel_subscribable_folder_subscribed (CAMEL_SUBSCRIBABLE (ews_store), fi);
550 camel_folder_info_free (fi);
551 } else if (has_public && !needs_public) {
552 fi = camel_ews_utils_build_folder_info (ews_store, EWS_PUBLIC_FOLDER_ROOT_ID);
553 camel_ews_store_summary_remove_folder (ews_store->summary, EWS_PUBLIC_FOLDER_ROOT_ID, NULL);
554
555 camel_subscribable_folder_unsubscribed (CAMEL_SUBSCRIBABLE (ews_store), fi);
556 camel_store_folder_deleted (CAMEL_STORE (ews_store), fi);
557 camel_folder_info_free (fi);
558 }
559
560 camel_ews_store_summary_rebuild_hashes (ews_store->summary);
561 camel_ews_store_summary_save (ews_store->summary, NULL);
562
563 g_slist_free_full (folders, g_free);
564 }
565
566 void
567 camel_ews_store_ensure_unique_path (CamelEwsStore *ews_store,
568 gchar **ppath)
569 {
570 gboolean done;
571 guint counter = 0;
572 gchar *base_path = NULL;
573
574 g_return_if_fail (CAMEL_IS_EWS_STORE (ews_store));
575 g_return_if_fail (ews_store->summary != NULL);
576 g_return_if_fail (ppath != NULL);
577 g_return_if_fail (*ppath != NULL);
578
579 done = FALSE;
580 while (!done) {
581 gchar *fid;
582
583 done = TRUE;
584
585 fid = camel_ews_store_summary_get_folder_id_from_name (ews_store->summary, *ppath);
586 if (fid) {
587 g_free (fid);
588
589 done = FALSE;
590 counter++;
591 if (!counter) {
592 g_debug ("%s: Counter overflow", G_STRFUNC);
593 break;
594 }
595
596 if (!base_path)
597 base_path = *ppath;
598 else
599 g_free (*ppath);
600
601 *ppath = g_strdup_printf ("%s_%u", base_path, counter);
602 }
603 }
604
605 g_free (base_path);
606 }
607
608 static void
609 ews_update_folder_hierarchy (CamelEwsStore *ews_store,
610 gchar *sync_state,
611 gboolean includes_last_folder,
612 GSList *folders_created,
613 GSList *folders_deleted,
614 GSList *folders_updated,
615 GSList **created_folder_ids)
616 {
617 ews_utils_sync_folders (ews_store, folders_created, folders_deleted, folders_updated, created_folder_ids);
618 camel_ews_store_ensure_virtual_folders (ews_store);
619
620 camel_ews_store_summary_store_string_val (ews_store->summary, "sync_state", sync_state);
621 camel_ews_store_summary_save (ews_store->summary, NULL);
622
623 g_slist_free_full (folders_created, g_object_unref);
624 g_slist_free_full (folders_updated, g_object_unref);
625 g_slist_free_full (folders_deleted, g_free);
626 g_free (sync_state);
627 }
628
629 static void
630 ews_update_has_ooo_set (CamelSession *session,
631 GCancellable *cancellable,
632 gpointer user_data,
633 GError **error)
634 {
635 CamelEwsStore *ews_store = user_data;
636 EEwsOofSettings *oof_settings;
637 EEwsOofState oof_state;
638 EEwsConnection *cnc;
639 GError *local_error = NULL;
640
641 cnc = camel_ews_store_ref_connection (ews_store);
642 if (!cnc)
643 return;
644
645 camel_operation_push_message (cancellable, _("Checking “Out of Office” settings"));
646
647 oof_settings = e_ews_oof_settings_new_sync (cnc, cancellable, &local_error);
648
649 g_clear_object (&cnc);
650
651 if (local_error != NULL) {
652 g_propagate_error (error, local_error);
653 camel_operation_pop_message (cancellable);
654 return;
655 }
656
657 oof_state = e_ews_oof_settings_get_state (oof_settings);
658 switch (oof_state) {
659 case E_EWS_OOF_STATE_ENABLED:
660 camel_ews_store_set_has_ooo_set (ews_store, TRUE);
661 break;
662 case E_EWS_OOF_STATE_DISABLED:
663 case E_EWS_OOF_STATE_SCHEDULED:
664 camel_ews_store_set_has_ooo_set (ews_store, FALSE);
665 break;
666 default:
667 break;
668 }
669
670 camel_operation_pop_message (cancellable);
671 g_clear_object (&oof_settings);
672 }
673
674 static void
675 ews_exchange_server_categories_cb (CamelSession *session,
676 GCancellable *cancellable,
677 gpointer user_data,
678 GError **error)
679 {
680 CamelEwsStore *ews_store = user_data;
681 EEwsConnection *cnc;
682 EwsFolderId fid = { 0 };
683 gchar *properties = NULL;
684 GError *local_error = NULL;
685
686 cnc = camel_ews_store_ref_connection (ews_store);
687 if (!cnc)
688 return;
689
690 fid.id = (gchar *) "calendar";
691 fid.is_distinguished_id = TRUE;
692
693 if (e_ews_connection_get_user_configuration_sync (cnc, G_PRIORITY_DEFAULT, &fid, "CategoryList",
694 E_EWS_USER_CONFIGURATION_PROPERTIES_XMLDATA, &properties, cancellable, &local_error) && properties) {
695 guchar *data;
696 gsize data_len = 0;
697
698 data = g_base64_decode (properties, &data_len);
699
700 if (data && data_len > 0)
701 camel_ews_utils_merge_category_list (ews_store, data, data_len);
702
703 g_free (data);
704 }
705
706 g_clear_error (&local_error);
707 g_clear_object (&cnc);
708 g_free (properties);
709 }
710
711 struct ScheduleUpdateData
712 {
713 GCancellable *cancellable;
714 CamelEwsStore *ews_store;
715 guint expected_id;
716 };
717
718 static void
719 free_schedule_update_data (gpointer ptr)
720 {
721 struct ScheduleUpdateData *sud = ptr;
722
723 if (sud == NULL)
724 return;
725
726 g_clear_object (&sud->cancellable);
727 g_clear_object (&sud->ews_store);
728
729 g_slice_free (struct ScheduleUpdateData, sud);
730 }
731
732 static GHashTable * /* gchar *id ~> gchar *change_key */
733 ews_store_get_existing_folders_in_path (CamelEwsStore *ews_store,
734 const gchar *in_path)
735 {
736 GHashTable *folders;
737 GSList *stored_folders, *link;
738
739 g_return_val_if_fail (CAMEL_IS_EWS_STORE (ews_store), NULL);
740 g_return_val_if_fail (in_path != NULL, NULL);
741
742 folders = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
743
744 stored_folders = camel_ews_store_summary_get_folders (ews_store->summary, in_path, TRUE);
745
746 for (link = stored_folders; link; link = g_slist_next (link)) {
747 g_hash_table_insert (folders, link->data,
748 camel_ews_store_summary_get_change_key (ews_store->summary, link->data, NULL));
749 }
750
751 /* The ::data is used in 'folders', thus do not free it here */
752 g_slist_free (stored_folders);
753
754 return folders;
755 }
756
757 static gboolean
758 ews_store_sync_public_folders (CamelEwsStore *ews_store,
759 EEwsConnection *connection,
760 const gchar *top,
761 GSList **pfolders_created,
762 GSList **pfolders_updated,
763 GSList **pfolders_deleted,
764 GCancellable *cancellable,
765 GError **error)
766 {
767 gboolean includes_last_folder = TRUE;
768 GHashTable *existing_folders;
769 GSList *folders = NULL;
770 gchar *fid_str;
771 EwsFolderId *folder_id;
772 GError *local_error = NULL;
773
774 g_return_val_if_fail (pfolders_created != NULL, FALSE);
775 g_return_val_if_fail (pfolders_updated != NULL, FALSE);
776 g_return_val_if_fail (pfolders_deleted != NULL, FALSE);
777
778 /* Read only explicit 'Public Folders' hierarchy, and not recursively,
779 due to possibly deep/large Public Folders hierarchy */
780 if (!top || !*top || !g_str_has_prefix (top, EWS_PUBLIC_FOLDER_ROOT_DISPLAY_NAME))
781 return TRUE;
782
783 existing_folders = ews_store_get_existing_folders_in_path (ews_store, top);
784 fid_str = camel_ews_store_summary_get_folder_id_from_name (ews_store->summary, top);
785
786 /* This should never be removed in this function */
787 g_hash_table_remove (existing_folders, EWS_PUBLIC_FOLDER_ROOT_ID);
788
789 if (!fid_str || g_strcmp0 (fid_str, EWS_PUBLIC_FOLDER_ROOT_ID) == 0)
790 folder_id = e_ews_folder_id_new ("publicfoldersroot", NULL, TRUE);
791 else
792 folder_id = e_ews_folder_id_new (fid_str, NULL, FALSE);
793
794 while (e_ews_connection_find_folder_sync (connection, EWS_PRIORITY_MEDIUM, folder_id, &includes_last_folder, &folders,
795 cancellable, &local_error) && !local_error &&
796 !g_cancellable_is_cancelled (cancellable)) {
797 GSList *fiter;
798
799 if (!folders)
800 break;
801
802 for (fiter = folders; fiter != NULL; fiter = fiter->next) {
803 EEwsFolder *folder = fiter->data;
804 const EwsFolderId *fid;
805 const gchar *existing_change_key;
806
807 if (!folder || e_ews_folder_is_error (folder))
808 continue;
809
810 fid = e_ews_folder_get_id (folder);
811
812 if (!fid)
813 continue;
814
815 if (!e_ews_folder_get_parent_id (folder)) {
816 if (!folder_id->is_distinguished_id) {
817 e_ews_folder_set_parent_id (folder,
818 e_ews_folder_id_new (folder_id->id, folder_id->change_key, folder_id->is_distinguished_id));
819 } else {
820 e_ews_folder_set_parent_id (folder, e_ews_folder_id_new (EWS_PUBLIC_FOLDER_ROOT_ID, NULL, FALSE));
821 }
822 }
823
824 e_ews_folder_set_public (folder, TRUE);
825
826 existing_change_key = g_hash_table_lookup (existing_folders, fid->id);
827
828 if (!existing_change_key) {
829 *pfolders_created = g_slist_prepend (*pfolders_created, g_object_ref (folder));
830 } else {
831 /* Always claim the folder as updated, thus the summary updates also folder
832 flags with information about child folders */
833 *pfolders_updated = g_slist_prepend (*pfolders_updated, g_object_ref (folder));
834 }
835
836 g_hash_table_remove (existing_folders, fid->id);
837 }
838
839 g_slist_free_full (folders, g_object_unref);
840
841 if (includes_last_folder)
842 break;
843 }
844
845 e_ews_folder_id_free (folder_id);
846 g_free (fid_str);
847
848 if (!local_error && g_hash_table_size (existing_folders) > 0) {
849 GHashTableIter iter;
850 gpointer key;
851
852 g_hash_table_iter_init (&iter, existing_folders);
853
854 while (g_hash_table_iter_next (&iter, &key, NULL)) {
855 const gchar *id = key;
856 gchar *full_name;
857 GSList *removed_folders;
858
859 full_name = camel_ews_store_summary_get_folder_full_name (ews_store->summary, id, NULL);
860
861 if (!full_name) {
862 *pfolders_deleted = g_slist_prepend (*pfolders_deleted, g_strdup (id));
863 continue;
864 }
865
866 removed_folders = camel_ews_store_summary_get_folders (ews_store->summary, full_name, FALSE);
867
868 if (removed_folders)
869 *pfolders_deleted = g_slist_concat (*pfolders_deleted, removed_folders);
870
871 g_free (full_name);
872 }
873 }
874
875 g_hash_table_destroy (existing_folders);
876
877 if (local_error) {
878 if (g_error_matches (local_error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_FOLDERNOTFOUND) &&
879 !g_str_equal (top, EWS_PUBLIC_FOLDER_ROOT_DISPLAY_NAME)) {
880 GSList *removed_folders;
881
882 removed_folders = camel_ews_store_summary_get_folders (ews_store->summary, top, FALSE);
883
884 if (removed_folders)
885 *pfolders_deleted = g_slist_concat (*pfolders_deleted, removed_folders);
886 }
887
888 camel_ews_store_maybe_disconnect (ews_store, local_error);
889 g_propagate_error (error, local_error);
890
891 return FALSE;
892 }
893
894 return TRUE;
895 }
896
897 static gpointer
898 camel_ews_folder_list_update_thread (gpointer user_data)
899 {
900 struct ScheduleUpdateData *sud = user_data;
901 CamelEwsStore *ews_store = sud->ews_store;
902 EEwsConnection *cnc = NULL;
903 GSList *created = NULL;
904 GSList *updated = NULL;
905 GSList *deleted = NULL;
906 gchar *old_sync_state = NULL;
907 gchar *new_sync_state;
908 gboolean includes_last;
909 GError *local_error = NULL;
910
911 if (g_cancellable_is_cancelled (sud->cancellable))
912 goto exit;
913
914 cnc = camel_ews_store_ref_connection (ews_store);
915 if (!cnc)
916 goto exit;
917
918 old_sync_state = camel_ews_store_summary_get_string_val (ews_store->summary, "sync_state", NULL);
919 if (!e_ews_connection_sync_folder_hierarchy_sync (
920 cnc,
921 EWS_PRIORITY_LOW,
922 old_sync_state,
923 &new_sync_state,
924 &includes_last,
925 &created,
926 &updated,
927 &deleted,
928 sud->cancellable,
929 &local_error))
930 goto exit;
931
932 if (g_cancellable_is_cancelled (sud->cancellable)) {
933 g_slist_free_full (created, g_object_unref);
934 g_slist_free_full (updated, g_object_unref);
935 g_slist_free_full (deleted, g_free);
936 g_free (new_sync_state);
937
938 goto exit;
939 }
940
941 if (ews_store_show_public_folders (ews_store))
942 ews_store_sync_public_folders (ews_store, cnc, EWS_PUBLIC_FOLDER_ROOT_DISPLAY_NAME, &created, &updated, &deleted, sud->cancellable, NULL);
943
944 if (created != NULL || updated != NULL || deleted != NULL) {
945 ews_update_folder_hierarchy (
946 ews_store,
947 new_sync_state, /* freed in the function */
948 includes_last,
949 created, /* freed in the function */
950 deleted, /* freed in the function */
951 updated, /* freed in the function */
952 NULL);
953 } else {
954 g_slist_free_full (created, g_object_unref);
955 g_slist_free_full (updated, g_object_unref);
956 g_slist_free_full (deleted, g_free);
957 g_free (new_sync_state);
958 }
959
960 exit:
961 if (local_error) {
962 camel_ews_store_maybe_disconnect (ews_store, local_error);
963 g_clear_error (&local_error);
964
965 g_mutex_lock (&ews_store->priv->get_finfo_lock);
966 ews_store->priv->last_refresh_time -= FINFO_REFRESH_INTERVAL;
967 g_mutex_unlock (&ews_store->priv->get_finfo_lock);
968 } else {
969 g_mutex_lock (&ews_store->priv->get_finfo_lock);
970 ews_store->priv->last_refresh_time = time (NULL);
971 g_mutex_unlock (&ews_store->priv->get_finfo_lock);
972 }
973
974 g_free (old_sync_state);
975 g_clear_object (&cnc);
976 free_schedule_update_data (sud);
977 return NULL;
978 }
979
980 static gpointer
981 camel_ews_folder_update_thread (gpointer user_data)
982 {
983 struct ScheduleUpdateData *sud = user_data;
984 CamelEwsStore *ews_store = sud->ews_store;
985 GSList *update_folder_names, *l;
986
987 g_return_val_if_fail (sud != NULL, NULL);
988
989 UPDATE_LOCK (ews_store);
990 update_folder_names = ews_store->priv->update_folder_names;
991 ews_store->priv->update_folder_names = NULL;
992 UPDATE_UNLOCK (ews_store);
993
994 for (l = update_folder_names; l != NULL && !g_cancellable_is_cancelled (sud->cancellable); l = l->next) {
995 const gchar *folder_name = l->data;
996 CamelFolder *folder;
997 GError *error = NULL;
998
999 folder = camel_store_get_folder_sync (CAMEL_STORE (ews_store), folder_name, 0, sud->cancellable, NULL);
1000 if (folder == NULL)
1001 continue;
1002
1003 camel_folder_refresh_info_sync (folder, sud->cancellable, &error);
1004 g_object_unref (folder);
1005
1006 if (error != NULL) {
1007 g_warning ("%s: %s\n", G_STRFUNC, error->message);
1008 g_clear_error (&error);
1009 break;
1010 }
1011 }
1012
1013 g_slist_free_full (update_folder_names, g_free);
1014 update_folder_names = NULL;
1015 free_schedule_update_data (sud);
1016
1017 return NULL;
1018 }
1019
1020 static void
1021 run_update_thread (CamelEwsStore *ews_store,
1022 gboolean folder_list,
1023 GCancellable *cancellable)
1024 {
1025 GThread *thread;
1026 struct ScheduleUpdateData *sud;
1027
1028 g_return_if_fail (ews_store != NULL);
1029 g_return_if_fail (cancellable != NULL);
1030
1031 sud = g_slice_new0 (struct ScheduleUpdateData);
1032 sud->ews_store = g_object_ref (ews_store);
1033 sud->cancellable = g_object_ref (cancellable);
1034
1035 thread = g_thread_new (
1036 NULL,
1037 folder_list ? camel_ews_folder_list_update_thread : camel_ews_folder_update_thread,
1038 sud);
1039 g_thread_unref (thread);
1040 }
1041
1042 static gboolean
1043 folder_update_cb (gpointer user_data)
1044 {
1045 struct ScheduleUpdateData *sud = user_data;
1046
1047 g_return_val_if_fail (sud != NULL, FALSE);
1048
1049 if (g_cancellable_is_cancelled (sud->cancellable))
1050 return FALSE;
1051
1052 g_return_val_if_fail (sud->ews_store != NULL, FALSE);
1053 g_return_val_if_fail (sud->ews_store->priv != NULL, FALSE);
1054
1055 UPDATE_LOCK (sud->ews_store);
1056 if (sud->expected_id != sud->ews_store->priv->update_folder_id)
1057 goto exit;
1058
1059 sud->ews_store->priv->update_folder_id = 0;
1060
1061 if (!g_cancellable_is_cancelled (sud->cancellable))
1062 run_update_thread (sud->ews_store, FALSE, sud->cancellable);
1063
1064 exit:
1065 UPDATE_UNLOCK (sud->ews_store);
1066 return FALSE;
1067 }
1068
1069 static void
1070 get_folder_names_to_update (gpointer key,
1071 gpointer value,
1072 gpointer user_data)
1073 {
1074 CamelEwsStore *ews_store = user_data;
1075 const gchar *folder_id = key;
1076 gchar *folder_name;
1077
1078 folder_name = camel_ews_store_summary_get_folder_full_name (ews_store->summary, folder_id, NULL);
1079 if (folder_name != NULL)
1080 ews_store->priv->update_folder_names = g_slist_prepend (ews_store->priv->update_folder_names, folder_name);
1081 }
1082
1083 static void
1084 schedule_folder_update (CamelEwsStore *ews_store,
1085 GHashTable *folder_ids)
1086 {
1087 struct ScheduleUpdateData *sud;
1088 CamelSettings *settings;
1089
1090 g_return_if_fail (ews_store != NULL);
1091 g_return_if_fail (ews_store->priv != NULL);
1092
1093 UPDATE_LOCK (ews_store);
1094 g_hash_table_foreach (folder_ids, get_folder_names_to_update, ews_store);
1095
1096 if (ews_store->priv->update_folder_names == NULL)
1097 goto exit;
1098
1099 sud = g_slice_new0 (struct ScheduleUpdateData);
1100 sud->ews_store = g_object_ref (ews_store);
1101 sud->cancellable = g_object_ref (ews_store->priv->updates_cancellable);
1102
1103 if (ews_store->priv->update_folder_id > 0)
1104 g_source_remove (ews_store->priv->update_folder_id);
1105
1106 settings = camel_service_ref_settings (CAMEL_SERVICE (ews_store));
1107
1108 ews_store->priv->update_folder_id = e_named_timeout_add_seconds_full (
1109 G_PRIORITY_LOW,
1110 1,
1111 folder_update_cb,
1112 sud,
1113 free_schedule_update_data);
1114 sud->expected_id = ews_store->priv->update_folder_id;
1115
1116 g_object_unref (settings);
1117
1118 exit:
1119 UPDATE_UNLOCK (ews_store);
1120 }
1121
1122 static gboolean
1123 folder_list_update_cb (gpointer user_data)
1124 {
1125 struct ScheduleUpdateData *sud = user_data;
1126
1127 g_return_val_if_fail (sud != NULL, FALSE);
1128
1129 if (g_cancellable_is_cancelled (sud->cancellable))
1130 return FALSE;
1131
1132 g_return_val_if_fail (sud->ews_store != NULL, FALSE);
1133 g_return_val_if_fail (sud->ews_store->priv != NULL, FALSE);
1134
1135 UPDATE_LOCK (sud->ews_store);
1136 if (sud->expected_id != sud->ews_store->priv->update_folder_list_id)
1137 goto exit;
1138
1139 sud->ews_store->priv->update_folder_list_id = 0;
1140
1141 if (!g_cancellable_is_cancelled (sud->cancellable))
1142 run_update_thread (sud->ews_store, TRUE, sud->cancellable);
1143
1144 exit:
1145 UPDATE_UNLOCK (sud->ews_store);
1146
1147 return FALSE;
1148 }
1149
1150 static void
1151 schedule_folder_list_update (CamelEwsStore *ews_store)
1152 {
1153 struct ScheduleUpdateData *sud;
1154 CamelSettings *settings;
1155
1156 g_return_if_fail (ews_store != NULL);
1157 g_return_if_fail (ews_store->priv != NULL);
1158
1159 UPDATE_LOCK (ews_store);
1160 if (!ews_store->priv->updates_cancellable)
1161 goto exit;
1162
1163 sud = g_slice_new0 (struct ScheduleUpdateData);
1164 sud->ews_store = g_object_ref (ews_store);
1165 sud->cancellable = g_object_ref (ews_store->priv->updates_cancellable);
1166
1167 if (ews_store->priv->update_folder_list_id > 0)
1168 g_source_remove (ews_store->priv->update_folder_list_id);
1169
1170 settings = camel_service_ref_settings (CAMEL_SERVICE (ews_store));
1171
1172 ews_store->priv->update_folder_list_id = e_named_timeout_add_seconds_full (
1173 G_PRIORITY_LOW,
1174 1,
1175 folder_list_update_cb,
1176 sud,
1177 free_schedule_update_data);
1178 sud->expected_id = ews_store->priv->update_folder_list_id;
1179
1180 g_object_unref (settings);
1181
1182 exit:
1183 UPDATE_UNLOCK (ews_store);
1184 }
1185
1186 static void
1187 camel_ews_store_server_notification_cb (CamelEwsStore *ews_store,
1188 GSList *events,
1189 EEwsConnection *cnc)
1190 {
1191 GSList *l;
1192 gboolean update_folder = FALSE;
1193 gboolean update_folder_list = FALSE;
1194 GHashTable *folder_ids;
1195
1196 g_return_if_fail (ews_store != NULL);
1197 g_return_if_fail (ews_store->priv != NULL);
1198
1199 folder_ids = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1200
1201 for (l = events; l != NULL; l = l->next) {
1202 EEwsNotificationEvent *event = l->data;
1203
1204 switch (event->type) {
1205 case E_EWS_NOTIFICATION_EVENT_CREATED:
1206 case E_EWS_NOTIFICATION_EVENT_DELETED:
1207 case E_EWS_NOTIFICATION_EVENT_MODIFIED:
1208 UPDATE_LOCK (ews_store);
1209 if (event->is_item) {
1210 update_folder = TRUE;
1211 if (!g_hash_table_lookup (folder_ids, event->folder_id))
1212 g_hash_table_insert (
1213 folder_ids, g_strdup (event->folder_id), GINT_TO_POINTER (1));
1214 } else {
1215 update_folder_list = TRUE;
1216 }
1217 UPDATE_UNLOCK (ews_store);
1218 break;
1219 case E_EWS_NOTIFICATION_EVENT_MOVED:
1220 case E_EWS_NOTIFICATION_EVENT_COPIED:
1221 UPDATE_LOCK (ews_store);
1222 if (event->is_item) {
1223 update_folder = TRUE;
1224 if (!g_hash_table_lookup (folder_ids, event->old_folder_id))
1225 g_hash_table_insert (
1226 folder_ids, g_strdup (event->old_folder_id), GINT_TO_POINTER (1));
1227
1228 if (!g_hash_table_lookup (folder_ids, event->folder_id))
1229 g_hash_table_insert (
1230 folder_ids, g_strdup (event->folder_id), GINT_TO_POINTER (1));
1231 } else {
1232 update_folder_list = TRUE;
1233 }
1234 UPDATE_UNLOCK (ews_store);
1235 break;
1236 default:
1237 break;
1238 }
1239 }
1240
1241 if (update_folder)
1242 schedule_folder_update (ews_store, folder_ids);
1243 if (update_folder_list)
1244 schedule_folder_list_update (ews_store);
1245
1246 g_hash_table_destroy (folder_ids);
1247 }
1248
1249 struct HandleNotificationsData {
1250 CamelEwsStore *ews_store;
1251 GSList *folders;
1252 };
1253
1254 static void
1255 handle_notifications_data_free (struct HandleNotificationsData *hnd)
1256 {
1257 if (hnd == NULL)
1258 return;
1259
1260 if (hnd->ews_store)
1261 g_object_unref (hnd->ews_store);
1262
1263 g_slist_free_full (hnd->folders, g_free);
1264 g_slice_free (struct HandleNotificationsData, hnd);
1265 }
1266
1267 static gpointer
1268 start_notifications_thread (gpointer data)
1269 {
1270 struct HandleNotificationsData *hnd = data;
1271 CamelEwsStore *ews_store = hnd->ews_store;
1272 EEwsConnection *cnc;
1273
1274 cnc = camel_ews_store_ref_connection (ews_store);
1275 if (!cnc)
1276 goto exit;
1277
1278 if (ews_store->priv->listen_notifications) {
1279 if (ews_store->priv->subscription_key != 0)
1280 goto exit;
1281
1282 e_ews_connection_enable_notifications_sync (
1283 cnc,
1284 hnd->folders,
1285 &ews_store->priv->subscription_key);
1286 } else {
1287 if (ews_store->priv->subscription_key == 0)
1288 goto exit;
1289
1290 e_ews_connection_disable_notifications_sync (
1291 cnc,
1292 ews_store->priv->subscription_key);
1293
1294 ews_store->priv->subscription_key = 0;
1295 }
1296
1297 exit:
1298 handle_notifications_data_free (hnd);
1299 g_clear_object (&cnc);
1300
1301 return NULL;
1302 }
1303
1304 static void
1305 folder_ids_populate (CamelFolderInfo *folder_info,
1306 gpointer data)
1307 {
1308 struct HandleNotificationsData *hnd = data;
1309
1310 while (folder_info != NULL) {
1311 gchar *id;
1312
1313 id = camel_ews_store_summary_get_folder_id_from_name (hnd->ews_store->summary, folder_info->full_name);
1314 if (id && !g_str_has_prefix (id, "ForeignMailbox::") &&
1315 !g_str_equal (id, EWS_PUBLIC_FOLDER_ROOT_ID) &&
1316 !g_str_equal (id, EWS_FOREIGN_FOLDER_ROOT_ID) &&
1317 !camel_ews_store_summary_get_foreign (hnd->ews_store->summary, id, NULL) &&
1318 !camel_ews_store_summary_get_public (hnd->ews_store->summary, id, NULL))
1319 hnd->folders = g_slist_prepend (hnd->folders, id);
1320 else
1321 g_free (id);
1322
1323 if (folder_info->child != NULL)
1324 folder_ids_populate (folder_info->child, hnd);
1325
1326 folder_info = folder_info->next;
1327 }
1328 }
1329
1330 static void
1331 camel_ews_store_handle_notifications (CamelEwsStore *ews_store,
1332 CamelEwsSettings *ews_settings)
1333 {
1334 GThread *thread;
1335 EEwsConnection *cnc;
1336 struct HandleNotificationsData *hnd;
1337
1338 cnc = camel_ews_store_ref_connection (ews_store);
1339
1340 if (!cnc)
1341 return;
1342
1343 if (!e_ews_connection_satisfies_server_version (cnc, E_EWS_EXCHANGE_2010_SP1)) {
1344 g_clear_object (&cnc);
1345 return;
1346 }
1347
1348 hnd = g_slice_new0 (struct HandleNotificationsData);
1349 hnd->ews_store = g_object_ref (ews_store);
1350
1351 if (!camel_ews_settings_get_check_all (ews_settings)) {
1352 gchar *inbox;
1353
1354 inbox = camel_ews_store_summary_get_folder_id_from_folder_type (
1355 ews_store->summary, CAMEL_FOLDER_TYPE_INBOX);
1356 hnd->folders = g_slist_prepend (hnd->folders, inbox);
1357 } else {
1358 CamelFolderInfo *fi;
1359
1360 fi = folder_info_from_store_summary (
1361 ews_store,
1362 NULL,
1363 CAMEL_STORE_FOLDER_INFO_RECURSIVE,
1364 NULL,
1365 NULL);
1366
1367 folder_ids_populate (fi, hnd);
1368
1369 camel_folder_info_free (fi);
1370 }
1371
1372 g_clear_object (&cnc);
1373
1374 thread = g_thread_new (NULL, start_notifications_thread, hnd);
1375 g_thread_unref (thread);
1376 }
1377
1378 static void
1379 camel_ews_store_listen_notifications_cb (CamelEwsStore *ews_store,
1380 GParamSpec *spec,
1381 CamelEwsSettings *ews_settings)
1382 {
1383 if (ews_store->priv->listen_notifications == camel_ews_settings_get_listen_notifications (ews_settings))
1384 return;
1385
1386 ews_store->priv->listen_notifications = !ews_store->priv->listen_notifications;
1387
1388 camel_ews_store_handle_notifications (ews_store, ews_settings);
1389 }
1390
1391 static void
1392 camel_ews_store_check_all_cb (CamelEwsStore *ews_store,
1393 GParamSpec *spec,
1394 CamelEwsSettings *ews_settings)
1395 {
1396 if (!ews_store->priv->listen_notifications)
1397 return;
1398
1399 camel_ews_store_handle_notifications (ews_store, ews_settings);
1400 }
1401
1402 static void
1403 ews_camel_subscription_id_changed_cb (EEwsConnection *cnc,
1404 const gchar *subscription_id,
1405 gpointer user_data)
1406 {
1407 CamelEwsStore *ews_store = user_data;
1408
1409 g_return_if_fail (CAMEL_IS_EWS_STORE (ews_store));
1410
1411 g_mutex_lock (&ews_store->priv->connection_lock);
1412
1413 if (g_strcmp0 (ews_store->priv->last_subscription_id, subscription_id) != 0) {
1414 g_free (ews_store->priv->last_subscription_id);
1415 ews_store->priv->last_subscription_id = g_strdup (subscription_id);
1416 }
1417
1418 g_mutex_unlock (&ews_store->priv->connection_lock);
1419 }
1420
1421 static gboolean
1422 ews_connect_sync (CamelService *service,
1423 GCancellable *cancellable,
1424 GError **error)
1425 {
1426 EEwsConnection *connection;
1427 CamelEwsStore *ews_store;
1428 CamelEwsStorePrivate *priv;
1429 CamelEwsSettings *ews_settings;
1430 CamelSession *session;
1431 CamelSettings *settings;
1432 const gchar *auth_mech;
1433 gboolean success;
1434
1435 /* Chain up to parent's method. */
1436 if (!CAMEL_SERVICE_CLASS (camel_ews_store_parent_class)->connect_sync (service, cancellable, error))
1437 return FALSE;
1438
1439 ews_store = CAMEL_EWS_STORE (service);
1440 priv = ews_store->priv;
1441
1442 if (camel_service_get_connection_status (service) == CAMEL_SERVICE_DISCONNECTED)
1443 return FALSE;
1444
1445 connection = camel_ews_store_ref_connection (ews_store);
1446 if (connection != NULL) {
1447 g_object_unref (connection);
1448 return TRUE;
1449 }
1450
1451 session = camel_service_ref_session (service);
1452 settings = camel_service_ref_settings (service);
1453 ews_settings = CAMEL_EWS_SETTINGS (settings);
1454
1455 /* Try running an operation that requires authentication
1456 * to make sure we have valid credentials available. */
1457 auth_mech = camel_ews_settings_get_auth_mechanism_string (ews_settings);
1458 success = camel_session_authenticate_sync (session, service,
1459 auth_mech ? auth_mech : "NTLM", cancellable, error);
1460
1461 priv->listen_notifications = FALSE;
1462
1463 if (success) {
1464 CamelEwsStoreOooAlertState state;
1465
1466 state = camel_ews_store_get_ooo_alert_state (ews_store);
1467 if (state == CAMEL_EWS_STORE_OOO_ALERT_STATE_UNKNOWN)
1468 camel_session_submit_job (
1469 session, _("Checking “Out of Office” settings"),
1470 ews_update_has_ooo_set,
1471 g_object_ref (ews_store),
1472 g_object_unref);
1473
1474 camel_session_submit_job (
1475 session, _("Look up Exchange server categories"),
1476 ews_exchange_server_categories_cb,
1477 g_object_ref (ews_store),
1478 g_object_unref);
1479
1480 if (!priv->updates_cancellable)
1481 priv->updates_cancellable = g_cancellable_new ();
1482
1483 if (camel_ews_settings_get_listen_notifications (ews_settings))
1484 camel_ews_store_listen_notifications_cb (ews_store, NULL, ews_settings);
1485
1486 camel_offline_store_set_online_sync (
1487 CAMEL_OFFLINE_STORE (ews_store),
1488 TRUE, cancellable, NULL);
1489
1490 connection = camel_ews_store_ref_connection (ews_store);
1491 if (connection) {
1492 g_signal_connect_swapped (
1493 connection,
1494 "server-notification",
1495 G_CALLBACK (camel_ews_store_server_notification_cb),
1496 ews_store);
1497
1498 e_ews_connection_set_last_subscription_id (connection, ews_store->priv->last_subscription_id);
1499
1500 g_signal_connect_object (connection, "subscription-id-changed",
1501 G_CALLBACK (ews_camel_subscription_id_changed_cb), ews_store, 0);
1502
1503 g_clear_object (&connection);
1504 }
1505 }
1506
1507 g_signal_connect_swapped (
1508 ews_settings,
1509 "notify::listen-notifications",
1510 G_CALLBACK (camel_ews_store_listen_notifications_cb),
1511 ews_store);
1512
1513 g_signal_connect_swapped (
1514 ews_settings,
1515 "notify::check-all",
1516 G_CALLBACK (camel_ews_store_check_all_cb),
1517 ews_store);
1518
1519 g_object_unref (session);
1520 g_object_unref (settings);
1521
1522 return success;
1523 }
1524
1525 static void
1526 camel_ews_store_password_will_expire_cb (EEwsConnection *connection,
1527 gint in_days,
1528 const gchar *service_url,
1529 gpointer user_data)
1530 {
1531 CamelEwsStore *ews_store = user_data;
1532
1533 g_return_if_fail (CAMEL_IS_EWS_STORE (ews_store));
1534
1535 if (ews_store->priv->password_expires_in_days < 0 ||
1536 ews_store->priv->password_expires_in_days > in_days) {
1537 CamelService *service;
1538 CamelSession *session;
1539
1540 ews_store->priv->password_expires_in_days = in_days;
1541
1542 service = CAMEL_SERVICE (ews_store);
1543 session = camel_service_ref_session (service);
1544
1545 if (session) {
1546 gchar *msg;
1547
1548 if (service_url) {
1549 msg = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE,
1550 /* Translators: The "%s" is a service URL, provided by the server */
1551 "Password will expire in %d day. Open “%s” to change it.",
1552 "Password will expire in %d days. Open “%s” to change it.",
1553 in_days),
1554 in_days, service_url);
1555 } else {
1556 msg = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE,
1557 "Password will expire in one day.",
1558 "Password will expire in %d days.",
1559 in_days),
1560 in_days);
1561 }
1562
1563 camel_session_user_alert (session, service, CAMEL_SESSION_ALERT_WARNING, msg);
1564
1565 g_object_unref (session);
1566 g_free (msg);
1567 }
1568 }
1569 }
1570
1571 static void
1572 stop_pending_updates (CamelEwsStore *ews_store)
1573 {
1574 CamelEwsStorePrivate *priv;
1575
1576 g_return_if_fail (ews_store != NULL);
1577 g_return_if_fail (ews_store->priv != NULL);
1578
1579 priv = ews_store->priv;
1580
1581 UPDATE_LOCK (ews_store);
1582 if (priv->updates_cancellable) {
1583 g_cancellable_cancel (priv->updates_cancellable);
1584 g_object_unref (priv->updates_cancellable);
1585 priv->updates_cancellable = NULL;
1586 }
1587
1588 g_slist_free_full (priv->update_folder_names, g_free);
1589 priv->update_folder_names = NULL;
1590 UPDATE_UNLOCK (ews_store);
1591 }
1592
1593 static void
1594 ews_store_unset_connection_locked (CamelEwsStore *ews_store,
1595 gboolean is_disconnect)
1596 {
1597 g_return_if_fail (CAMEL_IS_EWS_STORE (ews_store));
1598
1599 /* TODO cancel all operations in the connection */
1600 if (ews_store->priv->connection != NULL) {
1601 CamelSettings *settings;
1602
1603 /* FIXME This is somewhat broken, since the CamelSettings
1604 * instance returned here may not be the same instance
1605 * that we connected a signal handler to. Need to keep
1606 * our own reference to that CamelSettings instance, or
1607 * better yet avoid connecting signal handlers to it in
1608 * the first place. */
1609 settings = camel_service_ref_settings (CAMEL_SERVICE (ews_store));
1610 g_signal_handlers_disconnect_by_data (settings, ews_store);
1611 g_signal_handlers_disconnect_by_func (
1612 ews_store->priv->connection, camel_ews_store_server_notification_cb, ews_store);
1613
1614 g_object_unref (settings);
1615
1616 if (ews_store->priv->listen_notifications) {
1617 stop_pending_updates (ews_store);
1618
1619 if (ews_store->priv->subscription_key != 0) {
1620 e_ews_connection_disable_notifications_sync (
1621 ews_store->priv->connection,
1622 ews_store->priv->subscription_key);
1623
1624 ews_store->priv->subscription_key = 0;
1625 }
1626
1627 ews_store->priv->listen_notifications = FALSE;
1628 }
1629
1630 if (is_disconnect) {
1631 e_ews_connection_set_password (ews_store->priv->connection, NULL);
1632 e_ews_connection_set_disconnected_flag (ews_store->priv->connection, TRUE);
1633 }
1634
1635 g_signal_handlers_disconnect_by_func (ews_store->priv->connection,
1636 G_CALLBACK (camel_ews_store_password_will_expire_cb), ews_store);
1637 g_object_unref (ews_store->priv->connection);
1638 ews_store->priv->connection = NULL;
1639 }
1640 }
1641
1642 static gboolean
1643 ews_disconnect_sync (CamelService *service,
1644 gboolean clean,
1645 GCancellable *cancellable,
1646 GError **error)
1647 {
1648 CamelEwsStore *ews_store = CAMEL_EWS_STORE (service);
1649 CamelServiceClass *service_class;
1650
1651 g_mutex_lock (&ews_store->priv->connection_lock);
1652 ews_store_unset_connection_locked (ews_store, TRUE);
1653 g_mutex_unlock (&ews_store->priv->connection_lock);
1654
1655 service_class = CAMEL_SERVICE_CLASS (camel_ews_store_parent_class);
1656 return service_class->disconnect_sync (service, clean, cancellable, error);
1657 }
1658
1659 typedef struct {
1660 const gchar *dist_folder_id;
1661 gint info_flags;
1662 } SystemFolder;
1663
1664 static SystemFolder system_folder[] = {
1665 {"calendar", CAMEL_FOLDER_SYSTEM | CAMEL_FOLDER_TYPE_EVENTS},
1666 {"contacts", CAMEL_FOLDER_SYSTEM | CAMEL_FOLDER_TYPE_CONTACTS},
1667 {"deleteditems", CAMEL_FOLDER_SYSTEM | CAMEL_FOLDER_TYPE_TRASH},
1668 {"drafts", CAMEL_FOLDER_SYSTEM | CAMEL_FOLDER_TYPE_DRAFTS},
1669 {"inbox", CAMEL_FOLDER_SYSTEM | CAMEL_FOLDER_TYPE_INBOX},
1670 {"journal", CAMEL_FOLDER_SYSTEM | CAMEL_EWS_FOLDER_TYPE_JOURNAL},
1671 {"notes", CAMEL_FOLDER_SYSTEM | CAMEL_FOLDER_TYPE_MEMOS},
1672 {"outbox", CAMEL_FOLDER_SYSTEM | CAMEL_FOLDER_TYPE_OUTBOX},
1673 {"sentitems", CAMEL_FOLDER_SYSTEM | CAMEL_FOLDER_TYPE_SENT},
1674 {"tasks", CAMEL_FOLDER_SYSTEM | CAMEL_FOLDER_TYPE_TASKS},
1675 {"msgfolderroot", CAMEL_FOLDER_SYSTEM},
1676 {"root", CAMEL_FOLDER_SYSTEM},
1677 {"junkemail", CAMEL_FOLDER_SYSTEM | CAMEL_FOLDER_TYPE_JUNK},
1678 {"searchfolders", CAMEL_FOLDER_SYSTEM},
1679 };
1680
1681 static void
1682 ews_store_set_flags (CamelEwsStore *ews_store,
1683 GSList *folders)
1684 {
1685 GSList *temp = NULL;
1686 EEwsFolder *folder = NULL;
1687 const EwsFolderId *fid = NULL;
1688 gint n = 0;
1689
1690 temp = folders;
1691 while (temp != NULL) {
1692 folder = (EEwsFolder *) temp->data;
1693
1694 if (folder && !e_ews_folder_is_error (folder)) {
1695 fid = e_ews_folder_get_id (folder);
1696
1697 if (camel_ews_store_summary_has_folder (ews_store->summary, fid->id)) {
1698 camel_ews_store_summary_set_folder_flags (ews_store->summary, fid->id, system_folder[n].info_flags |
1699 (camel_ews_store_summary_get_folder_flags (ews_store->summary, fid->id, NULL) & (CAMEL_FOLDER_CHILDREN | CAMEL_FOLDER_NOCHILDREN)));
1700 }
1701 }
1702
1703 temp = temp->next;
1704 n++;
1705 }
1706 }
1707
1708 static void
1709 ews_store_forget_all_folders (CamelEwsStore *ews_store)
1710 {
1711 CamelStore *store;
1712 CamelSubscribable *subscribable;
1713 GSList *folders, *l;
1714
1715 g_return_if_fail (CAMEL_IS_EWS_STORE (ews_store));
1716
1717 store = CAMEL_STORE (ews_store);
1718 subscribable = CAMEL_SUBSCRIBABLE (ews_store);
1719 folders = camel_ews_store_summary_get_folders (ews_store->summary, NULL, FALSE);
1720
1721 if (!folders)
1722 return;
1723
1724 for (l = folders; l != NULL; l = g_slist_next (l)) {
1725 CamelFolderInfo *fi;
1726 EEwsFolderType ftype;
1727
1728 ftype = camel_ews_store_summary_get_folder_type (ews_store->summary, l->data, NULL);
1729 if (ftype != E_EWS_FOLDER_TYPE_MAILBOX)
1730 continue;
1731
1732 fi = camel_ews_utils_build_folder_info (ews_store, l->data);
1733 camel_subscribable_folder_unsubscribed (subscribable, fi);
1734 camel_store_folder_deleted (store, fi);
1735 camel_folder_info_free (fi);
1736 }
1737
1738 g_slist_free_full (folders, g_free);
1739 }
1740
1741 struct EwsUpdateForeignSubfoldersData
1742 {
1743 CamelEwsStore *ews_store;
1744 gchar *folder_id;
1745 };
1746
1747 static void
1748 ews_update_foreign_subfolders_data_free (gpointer data)
1749 {
1750 struct EwsUpdateForeignSubfoldersData *euf = data;
1751
1752 if (euf) {
1753 g_object_unref (euf->ews_store);
1754 g_free (euf->folder_id);
1755 g_slice_free (struct EwsUpdateForeignSubfoldersData, euf);
1756 }
1757 }
1758
1759 static void
1760 ews_store_update_foreign_subfolders (CamelSession *session,
1761 GCancellable *cancellable,
1762 gpointer user_data,
1763 GError **error)
1764 {
1765 struct EwsUpdateForeignSubfoldersData *euf = user_data;
1766 CamelEwsStore *ews_store;
1767 EEwsConnection *conn;
1768 GSList *tocheck = NULL, *remote_folders = NULL, *local_folders = NULL;
1769 const gchar *fid;
1770 GError *local_error = NULL;
1771
1772 g_return_if_fail (euf != NULL);
1773
1774 ews_store = euf->ews_store;
1775 fid = euf->folder_id;
1776
1777 if (!camel_offline_store_get_online (CAMEL_OFFLINE_STORE (ews_store)))
1778 return;
1779
1780 conn = camel_ews_store_ref_connection (ews_store);
1781 g_return_if_fail (conn != NULL);
1782
1783 camel_operation_push_message (cancellable, _("Updating foreign folder structure"));
1784
1785 /* read remote folder structure at the server */
1786 while (fid && !g_cancellable_is_cancelled (cancellable) && !local_error) {
1787 gboolean includes_last_item = FALSE;
1788 EwsFolderId *folder_id = e_ews_folder_id_new (fid, NULL, FALSE);
1789
1790 while (!includes_last_item && !g_cancellable_is_cancelled (cancellable) && !local_error) {
1791 GSList *folders = NULL, *ff;
1792
1793 if (!e_ews_connection_find_folder_sync (conn, EWS_PRIORITY_MEDIUM, folder_id,
1794 &includes_last_item, &folders, cancellable, &local_error))
1795 break;
1796
1797 for (ff = folders; ff != NULL; ff = ff->next) {
1798 EEwsFolder *folder = ff->data;
1799
1800 e_ews_folder_set_parent_id (folder, e_ews_folder_id_new (fid, NULL, FALSE));
1801
1802 remote_folders = g_slist_prepend (remote_folders, folder);
1803
1804 if (e_ews_folder_get_child_count (folder) > 0 && e_ews_folder_get_id (folder))
1805 tocheck = g_slist_prepend (tocheck, e_ews_folder_get_id (folder)->id);
1806 }
1807
1808 g_slist_free (folders);
1809 }
1810
1811 e_ews_folder_id_free (folder_id);
1812
1813 if (tocheck) {
1814 fid = g_slist_last (tocheck)->data;
1815 tocheck = g_slist_remove (tocheck, fid);
1816 } else {
1817 fid = NULL;
1818 }
1819 }
1820
1821 /* get local folder structure */
1822 if (!local_error && !g_cancellable_is_cancelled (cancellable)) {
1823 gchar *full_name = camel_ews_store_summary_get_folder_full_name (ews_store->summary, euf->folder_id, NULL);
1824 if (full_name) {
1825 local_folders = camel_ews_store_summary_get_folders (ews_store->summary, full_name, FALSE);
1826 }
1827 g_free (full_name);
1828 }
1829
1830 /* merge local and remote folder structures */
1831 if (!local_error && !g_cancellable_is_cancelled (cancellable)) {
1832 GHashTable *locals = g_hash_table_new (g_str_hash, g_str_equal);
1833 GSList *ii;
1834
1835 remote_folders = g_slist_reverse (remote_folders);
1836
1837 for (ii = local_folders; ii; ii = ii->next) {
1838 g_hash_table_insert (locals, ii->data, ii->data);
1839 }
1840
1841 for (ii = remote_folders; ii; ii = ii->next) {
1842 EEwsFolder *folder = ii->data;
1843 const EwsFolderId *folder_id = e_ews_folder_get_id (folder);
1844 const EwsFolderId *parent_fid = e_ews_folder_get_parent_id (folder);
1845
1846 if (e_ews_folder_get_folder_type (folder) == E_EWS_FOLDER_TYPE_MAILBOX &&
1847 folder_id && folder_id->id) {
1848 if (!g_hash_table_remove (locals, folder_id->id)) {
1849 CamelFolderInfo *fi;
1850
1851 /* it's a new folder, add it */
1852 camel_ews_store_summary_new_folder (
1853 ews_store->summary,
1854 folder_id->id, parent_fid ? parent_fid->id : euf->folder_id, folder_id->change_key,
1855 e_ews_folder_get_escaped_name (folder), E_EWS_FOLDER_TYPE_MAILBOX,
1856 CAMEL_FOLDER_SUBSCRIBED, e_ews_folder_get_total_count (folder), TRUE, FALSE);
1857
1858 fi = camel_ews_utils_build_folder_info (ews_store, folder_id->id);
1859 camel_store_folder_created (CAMEL_STORE (ews_store), fi);
1860 camel_subscribable_folder_subscribed (CAMEL_SUBSCRIBABLE (ews_store), fi);
1861 camel_folder_info_free (fi);
1862 }
1863 }
1864 }
1865
1866 /* to not remove the parent */
1867 g_hash_table_remove (locals, euf->folder_id);
1868
1869 /* and now the locals contains only folders which were removed */
1870 if (g_hash_table_size (locals) > 0) {
1871 CamelSubscribable *subscribable = CAMEL_SUBSCRIBABLE (ews_store);
1872 CamelStore *store = CAMEL_STORE (ews_store);
1873 GHashTableIter iter;
1874 gpointer key, value;
1875
1876 g_hash_table_iter_init (&iter, locals);
1877 while (g_hash_table_iter_next (&iter, &key, &value)) {
1878 CamelFolderInfo *fi;
1879
1880 fi = camel_ews_utils_build_folder_info (ews_store, key);
1881 camel_subscribable_folder_unsubscribed (subscribable, fi);
1882 camel_store_folder_deleted (store, fi);
1883 camel_folder_info_free (fi);
1884 }
1885 }
1886
1887 g_hash_table_destroy (locals);
1888
1889 camel_ews_store_summary_save (ews_store->summary, &local_error);
1890 }
1891
1892 if (local_error)
1893 g_propagate_error (error, local_error);
1894
1895 camel_operation_pop_message (cancellable);
1896
1897 g_slist_free_full (remote_folders, g_object_unref);
1898 g_slist_free_full (local_folders, g_free);
1899 g_slist_free (tocheck);
1900 g_object_unref (conn);
1901 }
1902
1903 void
1904 camel_ews_store_update_foreign_subfolders (CamelEwsStore *ews_store,
1905 const gchar *fid)
1906 {
1907 struct EwsUpdateForeignSubfoldersData *euf;
1908 CamelSession *session;
1909
1910 g_return_if_fail (CAMEL_IS_EWS_STORE (ews_store));
1911 g_return_if_fail (fid != NULL);
1912
1913 session = camel_service_ref_session (CAMEL_SERVICE (ews_store));
1914 g_return_if_fail (session != NULL);
1915
1916 euf = g_slice_new0 (struct EwsUpdateForeignSubfoldersData);
1917 euf->ews_store = g_object_ref (ews_store);
1918 euf->folder_id = g_strdup (fid);
1919
1920 camel_session_submit_job (
1921 session, _("Updating foreign folders"), ews_store_update_foreign_subfolders,
1922 euf, ews_update_foreign_subfolders_data_free);
1923
1924 g_object_unref (session);
1925 }
1926
1927 static gboolean
1928 ews_initial_setup_with_connection_sync (CamelStore *store,
1929 GHashTable *save_setup,
1930 EEwsConnection *connection,
1931 GCancellable *cancellable,
1932 GError **error)
1933 {
1934 CamelEwsStore *ews_store;
1935 GSList *folders = NULL, *folder_ids = NULL;
1936 gint nn;
1937 GError *local_error = NULL;
1938
1939 g_return_val_if_fail (CAMEL_IS_EWS_STORE (store), FALSE);
1940
1941 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1942 return FALSE;
1943
1944 ews_store = CAMEL_EWS_STORE (store);
1945 if (connection) {
1946 g_object_ref (connection);
1947 } else {
1948 if (!camel_ews_store_connected (ews_store, cancellable, error))
1949 return FALSE;
1950
1951 connection = camel_ews_store_ref_connection (ews_store);
1952 g_return_val_if_fail (connection != NULL, FALSE);
1953 }
1954
1955 for (nn = 0; nn < G_N_ELEMENTS (system_folder); nn++) {
1956 EwsFolderId *fid = NULL;
1957
1958 fid = g_new0 (EwsFolderId, 1);
1959 fid->id = g_strdup (system_folder[nn].dist_folder_id);
1960 fid->is_distinguished_id = TRUE;
1961
1962 folder_ids = g_slist_append (folder_ids, fid);
1963 }
1964
1965 /* fetch system folders first using getfolder operation */
1966 if (!e_ews_connection_get_folder_sync (
1967 connection, EWS_PRIORITY_MEDIUM, "IdOnly",
1968 NULL, folder_ids, &folders,
1969 cancellable, &local_error)) {
1970 g_clear_object (&connection);
1971 g_propagate_error (error, local_error);
1972 return FALSE;
1973 }
1974
1975 if (folders && (g_slist_length (folders) != G_N_ELEMENTS (system_folder)))
1976 d (printf ("Error : not all folders are returned by getfolder operation"));
1977 else if (!local_error && folders)
1978 ews_store_set_flags (ews_store, folders);
1979 else if (local_error) {
1980 /* report error and make sure we are not leaking anything */
1981 g_warn_if_fail (folders == NULL);
1982 } else
1983 d (printf ("folders for respective distinguished ids don't exist"));
1984
1985 if (save_setup) {
1986 gchar *folder_id;
1987
1988 folder_id = camel_ews_store_summary_get_folder_id_from_folder_type (ews_store->summary, CAMEL_FOLDER_TYPE_SENT);
1989 if (folder_id) {
1990 gchar *fullname;
1991
1992 fullname = camel_ews_store_summary_get_folder_full_name (ews_store->summary, folder_id, NULL);
1993 if (fullname && *fullname) {
1994 g_hash_table_insert (save_setup,
1995 g_strdup (CAMEL_STORE_SETUP_SENT_FOLDER),
1996 g_strdup (fullname));
1997 }
1998
1999 g_free (fullname);
2000 g_free (folder_id);
2001 }
2002
2003 if (g_slist_length (folders) == G_N_ELEMENTS (system_folder)) {
2004 gint ii;
2005
2006 for (ii = 0; ii < G_N_ELEMENTS (system_folder); ii++) {
2007 if (g_str_equal ("drafts", system_folder[ii].dist_folder_id)) {
2008 break;
2009 }
2010 }
2011
2012 if (ii < G_N_ELEMENTS (system_folder)) {
2013 EEwsFolder *drafts = g_slist_nth (folders, ii)->data;
2014 if (drafts && !e_ews_folder_is_error (drafts)) {
2015 const EwsFolderId *fid = e_ews_folder_get_id (drafts);
2016
2017 if (fid && fid->id) {
2018 gchar *fullname;
2019
2020 fullname = camel_ews_store_summary_get_folder_full_name (ews_store->summary, fid->id, NULL);
2021 if (fullname && *fullname) {
2022 g_hash_table_insert (save_setup,
2023 g_strdup (CAMEL_STORE_SETUP_DRAFTS_FOLDER),
2024 g_strdup (fullname));
2025 }
2026
2027 g_free (fullname);
2028 }
2029 }
2030 }
2031 }
2032 }
2033
2034 g_slist_free_full (folders, g_object_unref);
2035 g_slist_free_full (folder_ids, (GDestroyNotify) e_ews_folder_id_free);
2036 g_clear_object (&connection);
2037 g_clear_error (&local_error);
2038
2039 return TRUE;
2040 }
2041
2042 static gboolean
2043 ews_initial_setup_sync (CamelStore *store,
2044 GHashTable *save_setup,
2045 GCancellable *cancellable,
2046 GError **error)
2047 {
2048 return ews_initial_setup_with_connection_sync (store, save_setup, NULL, cancellable, error);
2049 }
2050
2051 static CamelAuthenticationResult
2052 ews_authenticate_sync (CamelService *service,
2053 const gchar *mechanism,
2054 GCancellable *cancellable,
2055 GError **error)
2056 {
2057 CamelAuthenticationResult result;
2058 CamelEwsStore *ews_store;
2059 CamelSettings *settings;
2060 CamelEwsSettings *ews_settings;
2061 EEwsConnection *connection;
2062 ESource *source;
2063 GSList *folders_created = NULL;
2064 GSList *folders_updated = NULL;
2065 GSList *folders_deleted = NULL;
2066 GSList *folder_ids = NULL;
2067 GSList *created_folder_ids = NULL;
2068 gboolean includes_last_folder = FALSE;
2069 gboolean initial_setup = FALSE;
2070 const gchar *password;
2071 gchar *hosturl;
2072 gchar *old_sync_state = NULL, *new_sync_state = NULL;
2073 gchar *certificate_pem = NULL;
2074 GTlsCertificateFlags certificate_errors = 0;
2075 GError *local_error = NULL;
2076
2077 ews_store = CAMEL_EWS_STORE (service);
2078
2079 password = camel_service_get_password (service);
2080
2081 settings = camel_service_ref_settings (service);
2082
2083 ews_settings = CAMEL_EWS_SETTINGS (settings);
2084 hosturl = camel_ews_settings_dup_hosturl (ews_settings);
2085 source = camel_ews_utils_ref_corresponding_source (service, cancellable);
2086
2087 connection = e_ews_connection_new (source, hosturl, ews_settings);
2088 e_ews_connection_set_password (connection, password);
2089
2090 g_clear_object (&source);
2091 g_free (hosturl);
2092
2093 g_object_unref (settings);
2094
2095 e_binding_bind_property (
2096 service, "proxy-resolver",
2097 connection, "proxy-resolver",
2098 G_BINDING_SYNC_CREATE);
2099
2100 /* XXX We need to run some operation that requires authentication
2101 * but does not change any server-side state, so we can check
2102 * the error status and determine if our password is valid.
2103 * David suggested e_ews_connection_sync_folder_hierarchy(),
2104 * since we have to do that eventually anyway. */
2105
2106 /*use old sync_state from summary*/
2107 old_sync_state = camel_ews_store_summary_get_string_val (ews_store->summary, "sync_state", NULL);
2108 if (!old_sync_state) {
2109 initial_setup = TRUE;
2110 } else {
2111 gchar *folder_id;
2112
2113 folder_id = camel_ews_store_summary_get_folder_id_from_folder_type (ews_store->summary, CAMEL_FOLDER_TYPE_INBOX);
2114 if (!folder_id || !*folder_id)
2115 initial_setup = TRUE;
2116
2117 g_free (folder_id);
2118
2119 if (!initial_setup) {
2120 folder_id = camel_ews_store_summary_get_folder_id_from_folder_type (ews_store->summary, CAMEL_FOLDER_TYPE_DRAFTS);
2121 if (!folder_id || !*folder_id)
2122 initial_setup = TRUE;
2123
2124 g_free (folder_id);
2125 }
2126 }
2127
2128 e_ews_connection_sync_folder_hierarchy_sync (connection, EWS_PRIORITY_MEDIUM, old_sync_state,
2129 &new_sync_state, &includes_last_folder, &folders_created, &folders_updated, &folders_deleted,
2130 cancellable, &local_error);
2131
2132 g_free (old_sync_state);
2133 old_sync_state = NULL;
2134
2135 if (g_error_matches (local_error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_UNAVAILABLE)) {
2136 local_error->domain = CAMEL_SERVICE_ERROR;
2137 local_error->code = CAMEL_SERVICE_ERROR_UNAVAILABLE;
2138 }
2139
2140 if (!initial_setup && g_error_matches (local_error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_INVALIDSYNCSTATEDATA)) {
2141 g_clear_error (&local_error);
2142 ews_store_forget_all_folders (ews_store);
2143 camel_ews_store_summary_store_string_val (ews_store->summary, "sync_state", "");
2144 camel_ews_store_summary_clear (ews_store->summary);
2145
2146 initial_setup = TRUE;
2147
2148 e_ews_connection_sync_folder_hierarchy_sync (connection, EWS_PRIORITY_MEDIUM, NULL,
2149 &new_sync_state, &includes_last_folder, &folders_created, &folders_updated, &folders_deleted,
2150 cancellable, &local_error);
2151 }
2152
2153 if (local_error == NULL) {
2154 GSList *foreign_fids, *ff;
2155
2156 g_mutex_lock (&ews_store->priv->connection_lock);
2157 ews_store_unset_connection_locked (ews_store, FALSE);
2158 ews_store->priv->connection = g_object_ref (connection);
2159 g_signal_connect (ews_store->priv->connection, "password-will-expire",
2160 G_CALLBACK (camel_ews_store_password_will_expire_cb), ews_store);
2161 g_mutex_unlock (&ews_store->priv->connection_lock);
2162
2163 /* This consumes all allocated result data. */
2164 ews_update_folder_hierarchy (
2165 ews_store, new_sync_state, includes_last_folder,
2166 folders_created, folders_deleted, folders_updated, &created_folder_ids);
2167
2168 /* Also update folder structures of foreign folders,
2169 those which are subscribed with subfolders */
2170 foreign_fids = camel_ews_store_summary_get_foreign_folders (ews_store->summary, NULL);
2171 for (ff = foreign_fids; ff != NULL; ff = ff->next) {
2172 const gchar *fid = ff->data;
2173
2174 if (camel_ews_store_summary_get_foreign_subfolders (ews_store->summary, fid, NULL)) {
2175 camel_ews_store_update_foreign_subfolders (ews_store, fid);
2176 }
2177 }
2178
2179 g_slist_free_full (foreign_fids, g_free);
2180 } else {
2181 g_mutex_lock (&ews_store->priv->connection_lock);
2182 ews_store_unset_connection_locked (ews_store, FALSE);
2183 g_mutex_unlock (&ews_store->priv->connection_lock);
2184
2185 g_free (new_sync_state);
2186
2187 /* Make sure we're not leaking anything. */
2188 g_warn_if_fail (folders_created == NULL);
2189 g_warn_if_fail (folders_updated == NULL);
2190 g_warn_if_fail (folders_deleted == NULL);
2191 }
2192
2193 /*get folders using distinguished id by GetFolder operation and set system flags to folders, only for first time*/
2194 if (!local_error && initial_setup && connection) {
2195 ews_initial_setup_with_connection_sync (CAMEL_STORE (ews_store), NULL, connection, cancellable, NULL);
2196 }
2197
2198 /* postpone notification of new folders to time when also folder flags are known,
2199 thus the view in evolution sows Inbox with an Inbox icon. */
2200 for (folder_ids = created_folder_ids; folder_ids; folder_ids = folder_ids->next) {
2201 CamelFolderInfo *fi;
2202
2203 fi = camel_ews_utils_build_folder_info (ews_store, folder_ids->data);
2204 camel_store_folder_created (CAMEL_STORE (ews_store), fi);
2205 camel_subscribable_folder_subscribed (CAMEL_SUBSCRIBABLE (ews_store), fi);
2206 camel_folder_info_free (fi);
2207 }
2208
2209 g_slist_free_full (created_folder_ids, g_free);
2210
2211 if (g_error_matches (local_error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE) &&
2212 e_ews_connection_get_ssl_error_details (connection, &certificate_pem, &certificate_errors)) {
2213 source = e_ews_connection_get_source (connection);
2214
2215 if (source) {
2216 e_source_emit_credentials_required (source, E_SOURCE_CREDENTIALS_REASON_SSL_FAILED,
2217 certificate_pem, certificate_errors, local_error);
2218 }
2219
2220 g_free (certificate_pem);
2221 }
2222
2223 if (local_error == NULL) {
2224 result = CAMEL_AUTHENTICATION_ACCEPTED;
2225 } else if (g_error_matches (local_error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_AUTHENTICATION_FAILED)) {
2226 g_clear_error (&local_error);
2227 result = CAMEL_AUTHENTICATION_REJECTED;
2228 } else {
2229 g_propagate_error (error, local_error);
2230 result = CAMEL_AUTHENTICATION_ERROR;
2231 }
2232
2233 g_object_unref (connection);
2234
2235 return result;
2236 }
2237
2238 static GList *
2239 ews_store_query_auth_types_sync (CamelService *service,
2240 GCancellable *cancellable,
2241 GError **error)
2242 {
2243 EEwsConnection *connection;
2244 ESource *source;
2245 CamelSettings *settings;
2246 CamelEwsSettings *ews_settings;
2247 GList *auth_types = NULL;
2248 GSList *auth_methods = NULL, *aiter;
2249 gchar *hosturl;
2250
2251 g_return_val_if_fail (CAMEL_IS_EWS_STORE (service), NULL);
2252
2253 settings = camel_service_ref_settings (service);
2254 ews_settings = CAMEL_EWS_SETTINGS (settings);
2255 hosturl = camel_ews_settings_dup_hosturl (ews_settings);
2256 source = camel_ews_utils_ref_corresponding_source (service, cancellable);
2257
2258 connection = e_ews_connection_new_full (source, hosturl, ews_settings, FALSE);
2259
2260 g_clear_object (&source);
2261 g_free (hosturl);
2262 g_object_unref (settings);
2263
2264 e_binding_bind_property (
2265 service, "proxy-resolver",
2266 connection, "proxy-resolver",
2267 G_BINDING_SYNC_CREATE);
2268
2269 if (e_ews_connection_query_auth_methods_sync (connection, G_PRIORITY_DEFAULT, &auth_methods, cancellable, error)) {
2270 CamelProvider *provider;
2271 CamelServiceAuthType *authtype;
2272
2273 provider = camel_service_get_provider (service);
2274 g_return_val_if_fail (provider != NULL, NULL);
2275
2276 for (aiter = auth_methods; aiter; aiter = aiter->next) {
2277 GList *siter;
2278 const gchar *auth = aiter->data;
2279
2280 if (!auth)
2281 continue;
2282
2283 if (g_ascii_strcasecmp (auth, "NTLM") == 0)
2284 auth = "";
2285 else if (g_ascii_strcasecmp (auth, "Basic") == 0)
2286 auth = "PLAIN";
2287 else if (g_ascii_strcasecmp (auth, "Negotiate") == 0)
2288 auth = "GSSAPI";
2289 else if (e_oauth2_services_is_supported () &&
2290 g_ascii_strcasecmp (auth, "Bearer") == 0) {
2291 /* Use Camel name for OAuth2. It's up to the caller to decide whether
2292 it can be used or not. */
2293 authtype = camel_sasl_authtype ("XOAUTH2");
2294 if (authtype)
2295 auth_types = g_list_prepend (auth_types, authtype);
2296
2297 continue;
2298 }
2299
2300 for (siter = provider->authtypes; siter; siter = siter->next) {
2301 authtype = siter->data;
2302
2303 if (g_ascii_strcasecmp (authtype->authproto, auth) == 0)
2304 auth_types = g_list_prepend (auth_types, authtype);
2305 }
2306 }
2307
2308 g_slist_free_full (auth_methods, g_free);
2309 }
2310
2311 g_object_unref (connection);
2312
2313 return g_list_reverse (auth_types);
2314 }
2315
2316 static CamelFolderInfo * ews_create_folder_sync (CamelStore *store, const gchar *parent_name,const gchar *folder_name, GCancellable *cancellable, GError **error);
2317
2318 static CamelFolder *
2319 ews_get_folder_sync (CamelStore *store,
2320 const gchar *folder_name,
2321 guint32 flags,
2322 GCancellable *cancellable,
2323 GError **error)
2324 {
2325 CamelEwsStore *ews_store;
2326 CamelFolder *folder = NULL;
2327 gchar *fid, *folder_dir;
2328
2329 ews_store = (CamelEwsStore *) store;
2330
2331 fid = camel_ews_store_summary_get_folder_id_from_name (ews_store->summary, folder_name);
2332
2333 if (!fid) {
2334 g_set_error (
2335 error, CAMEL_STORE_ERROR,
2336 CAMEL_STORE_ERROR_NO_FOLDER,
2337 _("No such folder: %s"), folder_name);
2338 return NULL;
2339 } else {
2340 /* We don't actually care what it is; only that it exists */
2341 g_free (fid);
2342 }
2343
2344 folder_dir = g_build_filename (ews_store->storage_path, "folders", folder_name, NULL);
2345 folder = camel_ews_folder_new (store, folder_name, folder_dir, cancellable, error);
2346
2347 g_free (folder_dir);
2348
2349 if ((flags & CAMEL_STORE_FOLDER_INFO_REFRESH) != 0)
2350 camel_folder_prepare_content_refresh (folder);
2351
2352 return folder;
2353 }
2354
2355 static gchar *
2356 get_public_folder_full_name (EEwsFolder *folder,
2357 GHashTable *folders_by_id)
2358 {
2359 const EwsFolderId *parent_fid;
2360 GString *full_name;
2361
2362 g_return_val_if_fail (folder != NULL, NULL);
2363 g_return_val_if_fail (folders_by_id != NULL, NULL);
2364
2365 full_name = g_string_new (e_ews_folder_get_escaped_name (folder));
2366 while (folder) {
2367 parent_fid = e_ews_folder_get_parent_id (folder);
2368 if (!parent_fid || !parent_fid->id)
2369 break;
2370
2371 folder = g_hash_table_lookup (folders_by_id, parent_fid->id);
2372 if (folder) {
2373 g_string_prepend_c (full_name, '/');
2374 g_string_prepend (full_name, e_ews_folder_get_escaped_name (folder));
2375 }
2376 }
2377
2378 g_string_prepend_c (full_name, '/');
2379 g_string_prepend (full_name, EWS_PUBLIC_FOLDER_ROOT_DISPLAY_NAME);
2380
2381 return g_string_free (full_name, FALSE);
2382 }
2383
2384 static gboolean
2385 ews_store_has_as_parent_id (CamelEwsStoreSummary *ews_summary,
2386 const gchar *fid,
2387 const gchar *mailroot_fid)
2388 {
2389 gchar *parent = NULL;
2390 gboolean found = FALSE;
2391
2392 g_return_val_if_fail (CAMEL_IS_EWS_STORE_SUMMARY (ews_summary), FALSE);
2393 g_return_val_if_fail (fid != NULL, FALSE);
2394 g_return_val_if_fail (mailroot_fid != NULL, FALSE);
2395
2396 while (found = g_strcmp0 (fid, mailroot_fid) == 0, fid && !found) {
2397 gchar *tmp = parent;
2398
2399 parent = camel_ews_store_summary_get_parent_folder_id (ews_summary, fid, NULL);
2400 fid = parent;
2401
2402 g_free (tmp);
2403 }
2404
2405 g_free (parent);
2406
2407 return found;
2408 }
2409
2410 static gboolean
2411 ews_store_folder_is_direct_subfolder (CamelEwsStore *ews_store,
2412 const gchar *fid,
2413 const gchar *path)
2414 {
2415 gchar *fid_path;
2416 gboolean res;
2417
2418 g_return_val_if_fail (CAMEL_IS_EWS_STORE (ews_store), FALSE);
2419 g_return_val_if_fail (fid != NULL, FALSE);
2420 g_return_val_if_fail (path != NULL, FALSE);
2421
2422 fid_path = camel_ews_store_summary_get_folder_full_name (ews_store->summary, fid, NULL);
2423
2424 if (!fid_path)
2425 return FALSE;
2426
2427 res = g_str_has_prefix (fid_path, path);
2428
2429 if (res) {
2430 gint path_len;
2431
2432 path_len = strlen (path);
2433
2434 res = fid_path[path_len] == '/' && !strchr (fid_path + path_len + 1, '/');
2435 }
2436
2437 g_free (fid_path);
2438
2439 return res;
2440 }
2441
2442 static CamelFolderInfo *
2443 folder_info_from_store_summary (CamelEwsStore *store,
2444 const gchar *top,
2445 guint32 flags,
2446 GCancellable *cancellable,
2447 GError **error)
2448 {
2449 CamelEwsStoreSummary *ews_summary;
2450 GPtrArray *folder_infos = NULL;
2451 CamelFolderInfo *root_fi = NULL, *fi;
2452
2453 /* search in public folders */
2454 if ((flags & CAMEL_STORE_FOLDER_INFO_SUBSCRIPTION_LIST) != 0) {
2455 GHashTable *folders_by_id;
2456 GSList *fiter;
2457 GList *esources = NULL;
2458 gchar *hosturl = NULL, *username = NULL;
2459
2460 g_mutex_lock (&store->priv->get_finfo_lock);
2461
2462 if (!store->priv->public_folders) {
2463 g_mutex_unlock (&store->priv->get_finfo_lock);
2464 return NULL;
2465 }
2466
2467 folder_infos = g_ptr_array_new ();
2468 folders_by_id = g_hash_table_new (g_str_hash, g_str_equal);
2469
2470 for (fiter = store->priv->public_folders; fiter != NULL; fiter = g_slist_next (fiter)) {
2471 EEwsFolder *folder = fiter->data;
2472 const EwsFolderId *fid;
2473
2474 if (!folder)
2475 continue;
2476
2477 fid = e_ews_folder_get_id (folder);
2478 if (!fid || !fid->id)
2479 continue;
2480
2481 g_hash_table_insert (folders_by_id, fid->id, folder);
2482 }
2483
2484 fi = camel_folder_info_new ();
2485 fi->full_name = g_strdup (EWS_PUBLIC_FOLDER_ROOT_DISPLAY_NAME);
2486 fi->display_name = g_strdup (fi->full_name);
2487 fi->flags = CAMEL_FOLDER_SYSTEM | CAMEL_FOLDER_NOSELECT;
2488 fi->unread = -1;
2489 fi->total = -1;
2490
2491 g_ptr_array_add (folder_infos, fi);
2492
2493 for (fiter = store->priv->public_folders; fiter != NULL; fiter = g_slist_next (fiter)) {
2494 EEwsFolder *folder = fiter->data;
2495 const EwsFolderId *fid;
2496
2497 if (!folder)
2498 continue;
2499
2500 fid = e_ews_folder_get_id (folder);
2501 if (!fid || !fid->id)
2502 continue;
2503
2504 fi = camel_folder_info_new ();
2505 fi->full_name = get_public_folder_full_name (folder, folders_by_id);
2506 fi->display_name = g_strdup (e_ews_folder_get_name (folder));
2507 fi->flags = 0;
2508 fi->unread = e_ews_folder_get_unread_count (folder);
2509 fi->total = e_ews_folder_get_total_count (folder);
2510
2511 switch (e_ews_folder_get_folder_type (folder)) {
2512 case E_EWS_FOLDER_TYPE_CALENDAR:
2513 fi->flags |= CAMEL_FOLDER_TYPE_EVENTS;
2514 break;
2515 case E_EWS_FOLDER_TYPE_CONTACTS:
2516 fi->flags |= CAMEL_FOLDER_TYPE_CONTACTS;
2517 break;
2518 case E_EWS_FOLDER_TYPE_TASKS:
2519 fi->flags |= CAMEL_FOLDER_TYPE_TASKS;
2520 break;
2521 case E_EWS_FOLDER_TYPE_MEMOS:
2522 fi->flags |= CAMEL_FOLDER_TYPE_MEMOS;
2523 break;
2524 default:
2525 break;
2526 }
2527
2528 if (camel_ews_store_summary_has_folder (store->summary, fid->id)) {
2529 guint64 fflags = camel_ews_store_summary_get_folder_flags (store->summary, fid->id, NULL);
2530
2531 if ((fflags & CAMEL_FOLDER_SUBSCRIBED) != 0)
2532 fi->flags |= CAMEL_FOLDER_SUBSCRIBED;
2533 }
2534
2535 if (!(fi->flags & CAMEL_FOLDER_SUBSCRIBED) &&
2536 e_ews_folder_get_folder_type (folder) != E_EWS_FOLDER_TYPE_MAILBOX) {
2537 if (!hosturl && !username && !esources) {
2538 CamelSettings *settings;
2539 CamelEwsSettings *ews_settings;
2540
2541 settings = camel_service_ref_settings (CAMEL_SERVICE (store));
2542 ews_settings = CAMEL_EWS_SETTINGS (settings);
2543
2544 hosturl = camel_ews_settings_dup_hosturl (ews_settings);
2545 username = camel_network_settings_dup_user (CAMEL_NETWORK_SETTINGS (ews_settings));
2546 esources = e_ews_folder_utils_get_esources (hosturl, username, cancellable, NULL);
2547
2548 g_object_unref (settings);
2549 }
2550
2551 if (e_ews_folder_utils_is_subscribed_as_esource (esources, hosturl, username, fid->id))
2552 fi->flags |= CAMEL_FOLDER_SUBSCRIBED;
2553 }
2554
2555 g_ptr_array_add (folder_infos, fi);
2556 }
2557
2558 g_list_free_full (esources, g_object_unref);
2559 g_hash_table_destroy (folders_by_id);
2560 g_free (hosturl);
2561 g_free (username);
2562 g_mutex_unlock (&store->priv->get_finfo_lock);
2563
2564 /* search in regular/subscribed folders */
2565 } else {
2566 GSList *folders, *fiter;
2567 gchar *mailroot_fid = NULL, *inbox_fid;
2568 gboolean show_public_folders, is_in_public_folders_root;
2569 gboolean is_online;
2570
2571 ews_summary = store->summary;
2572 folders = camel_ews_store_summary_get_folders (ews_summary, top, FALSE);
2573 if (!folders)
2574 return NULL;
2575
2576 show_public_folders = ews_store_show_public_folders (store);
2577 is_in_public_folders_root = top && g_str_has_prefix (top, EWS_PUBLIC_FOLDER_ROOT_DISPLAY_NAME);
2578 is_online = camel_offline_store_get_online (CAMEL_OFFLINE_STORE (store));
2579 inbox_fid = camel_ews_store_summary_get_folder_id_from_folder_type (ews_summary, CAMEL_FOLDER_TYPE_INBOX);
2580 if (inbox_fid) {
2581 mailroot_fid = camel_ews_store_summary_get_parent_folder_id (ews_summary, inbox_fid, NULL);
2582 }
2583 g_free (inbox_fid);
2584 inbox_fid = NULL;
2585
2586 folder_infos = g_ptr_array_new ();
2587
2588 for (fiter = folders; fiter != NULL; fiter = g_slist_next (fiter)) {
2589 EEwsFolderType ftype;
2590 const gchar *fid = fiter->data;
2591
2592 ftype = camel_ews_store_summary_get_folder_type (ews_summary, fid, NULL);
2593 if (ftype != E_EWS_FOLDER_TYPE_MAILBOX)
2594 continue;
2595
2596 if (camel_ews_store_summary_get_public (ews_summary, fid, NULL) ||
2597 (show_public_folders && g_strcmp0 (fid, EWS_PUBLIC_FOLDER_ROOT_ID) == 0)) {
2598 if (!show_public_folders || (!is_in_public_folders_root && g_strcmp0 (fid, EWS_PUBLIC_FOLDER_ROOT_ID) != 0)) {
2599 guint64 fflags;
2600
2601 fflags = camel_ews_store_summary_get_folder_flags (ews_summary, fid, NULL);
2602 if (!(fflags & CAMEL_FOLDER_SUBSCRIBED))
2603 continue;
2604 }
2605 } else if (!camel_ews_store_summary_get_foreign (ews_summary, fid, NULL) &&
2606 mailroot_fid && !ews_store_has_as_parent_id (ews_summary, fid, mailroot_fid)) {
2607 /* Skip mail folders out of the msgfolderroot hierarchy */
2608 continue;
2609 }
2610
2611 if (is_in_public_folders_root && is_online && show_public_folders &&
2612 g_strcmp0 (fid, EWS_PUBLIC_FOLDER_ROOT_ID) != 0 && !ews_store_folder_is_direct_subfolder (store, fid, top))
2613 continue;
2614
2615 fi = camel_ews_utils_build_folder_info (store, fid);
2616 g_ptr_array_add (folder_infos, fi);
2617 }
2618
2619 g_slist_free_full (folders, g_free);
2620 g_free (mailroot_fid);
2621 }
2622
2623 root_fi = camel_folder_info_build (folder_infos, top, '/', TRUE);
2624 g_ptr_array_free (folder_infos, TRUE);
2625
2626 return root_fi;
2627 }
2628
2629 static gboolean
2630 ews_refresh_finfo (CamelEwsStore *ews_store)
2631 {
2632 if (!camel_offline_store_get_online (CAMEL_OFFLINE_STORE (ews_store)))
2633 return FALSE;
2634
2635 if (!ews_store->priv->updates_cancellable)
2636 ews_store->priv->updates_cancellable = g_cancellable_new ();
2637
2638 run_update_thread (ews_store, TRUE, ews_store->priv->updates_cancellable);
2639
2640 return TRUE;
2641 }
2642
2643 static CamelFolderInfo *
2644 ews_get_folder_info_sync (CamelStore *store,
2645 const gchar *top,
2646 guint32 flags,
2647 GCancellable *cancellable,
2648 GError **error)
2649 {
2650 CamelEwsStore *ews_store;
2651 CamelEwsStorePrivate *priv;
2652 CamelFolderInfo *fi = NULL;
2653 EEwsConnection *connection;
2654 gchar *old_sync_state, *new_sync_state = NULL;
2655 gboolean initial_setup = FALSE;
2656 GSList *folders_created = NULL, *folders_updated = NULL;
2657 GSList *folders_deleted = NULL;
2658 gboolean includes_last_folder = TRUE;
2659 gboolean success;
2660 GError *local_error = NULL;
2661
2662 ews_store = (CamelEwsStore *) store;
2663 priv = ews_store->priv;
2664
2665 if ((flags & CAMEL_STORE_FOLDER_INFO_REFRESH) != 0 &&
2666 camel_offline_store_get_online (CAMEL_OFFLINE_STORE (ews_store))) {
2667 CamelSession *session;
2668
2669 session = camel_service_ref_session (CAMEL_SERVICE (ews_store));
2670 if (session) {
2671 ews_exchange_server_categories_cb (session, cancellable, ews_store, NULL);
2672 g_object_unref (session);
2673 }
2674 }
2675
2676 if ((flags & CAMEL_STORE_FOLDER_INFO_SUBSCRIPTION_LIST) != 0) {
2677 GSList *folders = NULL, *to_check = NULL;
2678 EwsFolderId *folder_id;
2679
2680 if (!camel_offline_store_get_online (CAMEL_OFFLINE_STORE (ews_store))) {
2681 g_set_error_literal (
2682 error, CAMEL_SERVICE_ERROR, CAMEL_SERVICE_ERROR_UNAVAILABLE,
2683 _("Cannot list EWS public folders in offline mode"));
2684 return NULL;
2685 }
2686
2687 g_mutex_lock (&priv->get_finfo_lock);
2688
2689 g_slist_free_full (priv->public_folders, g_object_unref);
2690 priv->public_folders = NULL;
2691
2692 connection = camel_ews_store_ref_connection (ews_store);
2693 folder_id = e_ews_folder_id_new ("publicfoldersroot", NULL, TRUE);
2694 to_check = g_slist_append (to_check, folder_id);
2695
2696 while (!local_error && !g_cancellable_is_cancelled (cancellable) && to_check) {
2697 folder_id = to_check->data;
2698 to_check = g_slist_remove (to_check, folder_id);
2699
2700 while (e_ews_connection_find_folder_sync (connection, EWS_PRIORITY_MEDIUM, folder_id, &includes_last_folder, &folders,
2701 cancellable, &local_error) && !local_error &&
2702 !g_cancellable_is_cancelled (cancellable)) {
2703 GSList *fiter;
2704
2705 if (!folders)
2706 break;
2707
2708 for (fiter = folders; fiter != NULL; fiter = fiter->next) {
2709 EEwsFolder *folder = fiter->data;
2710
2711 if (e_ews_folder_get_child_count (folder) > 0) {
2712 const EwsFolderId *fid = e_ews_folder_get_id (folder);
2713
2714 if (fid)
2715 to_check = g_slist_prepend (to_check,
2716 e_ews_folder_id_new (fid->id, fid->change_key, fid->is_distinguished_id));
2717 }
2718
2719 if (!e_ews_folder_get_parent_id (folder)) {
2720 if (!folder_id->is_distinguished_id) {
2721 e_ews_folder_set_parent_id (folder,
2722 e_ews_folder_id_new (folder_id->id, folder_id->change_key, folder_id->is_distinguished_id));
2723 } else {
2724 e_ews_folder_set_parent_id (folder, e_ews_folder_id_new (EWS_PUBLIC_FOLDER_ROOT_ID, NULL, FALSE));
2725 }
2726 }
2727 }
2728
2729 priv->public_folders = g_slist_concat (priv->public_folders, folders);
2730 folders = NULL;
2731
2732 if (includes_last_folder)
2733 break;
2734 }
2735
2736 e_ews_folder_id_free (folder_id);
2737 }
2738
2739 g_mutex_unlock (&priv->get_finfo_lock);
2740
2741 g_object_unref (connection);
2742 g_slist_free_full (to_check, (GDestroyNotify) e_ews_folder_id_free);
2743
2744 camel_ews_store_ensure_virtual_folders (ews_store);
2745
2746 if (local_error) {
2747 camel_ews_store_maybe_disconnect (ews_store, local_error);
2748 g_propagate_error (error, local_error);
2749
2750 return NULL;
2751 }
2752
2753 if (!priv->public_folders) {
2754 g_set_error_literal (
2755 error, CAMEL_SERVICE_ERROR, CAMEL_SERVICE_ERROR_UNAVAILABLE,
2756 _("Cannot find any EWS public folders"));
2757 return NULL;
2758 }
2759
2760 goto offline;
2761 }
2762
2763 g_mutex_lock (&priv->get_finfo_lock);
2764 if (!camel_offline_store_get_online (CAMEL_OFFLINE_STORE (store))) {
2765 camel_ews_store_ensure_virtual_folders (ews_store);
2766 g_mutex_unlock (&priv->get_finfo_lock);
2767 goto offline;
2768 }
2769
2770 if (!camel_service_connect_sync ((CamelService *) store, cancellable, error)) {
2771 camel_offline_store_set_online_sync (CAMEL_OFFLINE_STORE (store), FALSE, NULL, NULL);
2772 camel_ews_store_ensure_virtual_folders (ews_store);
2773 g_mutex_unlock (&priv->get_finfo_lock);
2774 return NULL;
2775 }
2776
2777 old_sync_state = camel_ews_store_summary_get_string_val (ews_store->summary, "sync_state", NULL);
2778 if (!old_sync_state)
2779 initial_setup = TRUE;
2780
2781 if (!initial_setup && (flags & CAMEL_STORE_FOLDER_INFO_REFRESH) != 0) {
2782 time_t now = time (NULL);
2783
2784 g_free (old_sync_state);
2785 if (now - priv->last_refresh_time > FINFO_REFRESH_INTERVAL && ews_refresh_finfo (ews_store))
2786 ews_store->priv->last_refresh_time = time (NULL);
2787
2788 g_mutex_unlock (&priv->get_finfo_lock);
2789 goto offline;
2790 }
2791
2792 connection = camel_ews_store_ref_connection (ews_store);
2793 includes_last_folder = TRUE;
2794
2795 success = e_ews_connection_sync_folder_hierarchy_sync (connection, EWS_PRIORITY_MEDIUM, old_sync_state,
2796 &new_sync_state, &includes_last_folder, &folders_created, &folders_updated, &folders_deleted,
2797 cancellable, &local_error);
2798
2799 g_free (old_sync_state);
2800 old_sync_state = NULL;
2801
2802 if (!initial_setup && g_error_matches (local_error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_INVALIDSYNCSTATEDATA)) {
2803 g_clear_error (&local_error);
2804 ews_store_forget_all_folders (ews_store);
2805 camel_ews_store_summary_store_string_val (ews_store->summary, "sync_state", "");
2806 camel_ews_store_summary_clear (ews_store->summary);
2807
2808 success = e_ews_connection_sync_folder_hierarchy_sync (connection, EWS_PRIORITY_MEDIUM, NULL,
2809 &new_sync_state, &includes_last_folder, &folders_created, &folders_updated, &folders_deleted,
2810 cancellable, &local_error);
2811 }
2812
2813 if (success && ews_store_show_public_folders (ews_store))
2814 ews_store_sync_public_folders (ews_store, connection, top, &folders_created, &folders_updated, &folders_deleted, cancellable, NULL);
2815
2816 g_object_unref (connection);
2817
2818 if (!success) {
2819 if (local_error)
2820 g_warning (
2821 "Unable to fetch the folder hierarchy: %s :%d \n",
2822 local_error->message, local_error->code);
2823 else
2824 g_warning ("Unable to fetch the folder hierarchy.\n");
2825
2826 camel_ews_store_maybe_disconnect (ews_store, local_error);
2827 g_propagate_error (error, local_error);
2828
2829 g_mutex_unlock (&priv->get_finfo_lock);
2830 return NULL;
2831 }
2832 ews_update_folder_hierarchy (
2833 ews_store, new_sync_state, includes_last_folder,
2834 folders_created, folders_deleted, folders_updated, NULL);
2835 g_mutex_unlock (&priv->get_finfo_lock);
2836
2837 offline:
2838 fi = folder_info_from_store_summary (ews_store, top, flags, cancellable, error);
2839 return fi;
2840 }
2841
2842 static CamelFolderInfo *
2843 ews_create_folder_sync (CamelStore *store,
2844 const gchar *parent_name,
2845 const gchar *folder_name,
2846 GCancellable *cancellable,
2847 GError **error)
2848 {
2849 CamelEwsStore *ews_store = CAMEL_EWS_STORE (store);
2850 CamelEwsStoreSummary *ews_summary = ews_store->summary;
2851 gchar *fid = NULL;
2852 gchar *full_name;
2853 EwsFolderId *folder_id;
2854 EEwsConnection *connection;
2855 CamelFolderInfo *fi = NULL;
2856 gboolean success, is_foreign = FALSE, is_public = FALSE;
2857 GError *local_error = NULL;
2858
2859 if (parent_name && *parent_name)
2860 full_name = g_strdup_printf ("%s/%s", parent_name, folder_name);
2861 else
2862 full_name = g_strdup (folder_name);
2863
2864 fid = camel_ews_store_summary_get_folder_id_from_name (ews_summary, full_name);
2865 if (fid) {
2866 g_free (fid);
2867 g_set_error (
2868 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
2869 _("Cannot create folder “%s”, folder already exists"),
2870 full_name);
2871 g_free (full_name);
2872 return NULL;
2873 }
2874
2875 g_free (full_name);
2876
2877 /* Get Parent folder ID */
2878 if (parent_name && parent_name[0]) {
2879 fid = camel_ews_store_summary_get_folder_id_from_name (
2880 ews_summary, parent_name);
2881 if (!fid) {
2882 g_set_error (
2883 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
2884 _("Parent folder %s does not exist"),
2885 parent_name);
2886 return NULL;
2887 }
2888
2889 if (g_str_equal (fid, EWS_FOREIGN_FOLDER_ROOT_ID)) {
2890 g_free (fid);
2891
2892 g_set_error (
2893 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
2894 _("Cannot create folder under “%s”, it is used for folders of other users only"),
2895 parent_name);
2896 return NULL;
2897 }
2898
2899 if (g_str_equal (fid, EWS_PUBLIC_FOLDER_ROOT_ID)) {
2900 g_free (fid);
2901
2902 g_set_error (
2903 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
2904 _("Cannot create folder under “%s”, it is used for public folders only"),
2905 parent_name);
2906 return NULL;
2907 }
2908
2909 is_foreign = camel_ews_store_summary_get_foreign (ews_summary, fid, NULL);
2910 is_public = camel_ews_store_summary_get_public (ews_summary, fid, NULL);
2911 }
2912
2913 if (!camel_ews_store_connected (ews_store, cancellable, error)) {
2914 if (fid) g_free (fid);
2915 return NULL;
2916 }
2917
2918 connection = camel_ews_store_ref_connection (ews_store);
2919
2920 success = e_ews_connection_create_folder_sync (
2921 connection,
2922 EWS_PRIORITY_MEDIUM, fid,
2923 FALSE, folder_name, E_EWS_FOLDER_TYPE_MAILBOX,
2924 &folder_id, cancellable, &local_error);
2925
2926 g_object_unref (connection);
2927
2928 if (!success) {
2929 camel_ews_store_maybe_disconnect (ews_store, local_error);
2930 g_propagate_error (error, local_error);
2931 g_free (fid);
2932 return NULL;
2933 }
2934
2935 /* Translate & store returned folder id */
2936 if (fid)
2937 full_name = g_strdup_printf ("%s/%s", parent_name, folder_name);
2938 else
2939 full_name = g_strdup (folder_name);
2940
2941 camel_ews_store_summary_new_folder (
2942 ews_summary, folder_id->id,
2943 fid, folder_id->change_key,
2944 folder_name,
2945 E_EWS_FOLDER_TYPE_MAILBOX,
2946 0, 0, is_foreign, is_public);
2947 fi = camel_ews_utils_build_folder_info (ews_store, folder_id->id);
2948 e_ews_folder_id_free (folder_id);
2949
2950 camel_store_folder_created (store, fi);
2951 camel_subscribable_folder_subscribed (CAMEL_SUBSCRIBABLE (ews_store), fi);
2952
2953 g_free (full_name);
2954 g_free (fid);
2955 return fi;
2956 }
2957
2958 static void
2959 ews_update_store_move_recursive (CamelEwsStore *ews_store,
2960 CamelFolderInfo *folder_info)
2961 {
2962 while (folder_info != NULL) {
2963 if (folder_info->child != NULL)
2964 ews_update_store_move_recursive (ews_store, folder_info->child);
2965
2966 camel_store_folder_created (CAMEL_STORE (ews_store), folder_info);
2967 camel_subscribable_folder_subscribed (CAMEL_SUBSCRIBABLE (ews_store), folder_info);
2968
2969 folder_info = folder_info->next;
2970 }
2971 }
2972
2973 static gboolean
2974 ews_delete_folder_sync (CamelStore *store,
2975 const gchar *folder_name,
2976 GCancellable *cancellable,
2977 GError **error)
2978 {
2979 CamelEwsStore *ews_store = CAMEL_EWS_STORE (store);
2980 gchar *fid;
2981 gchar *trash_fid;
2982 gchar *trash_name;
2983 CamelFolderInfo *folder_info;
2984 CamelFolderInfo *to_update;
2985 gboolean success;
2986 gboolean is_under_trash_folder;
2987 GError *local_error = NULL;
2988
2989 folder_info = camel_store_get_folder_info_sync (
2990 store, folder_name,
2991 CAMEL_STORE_FOLDER_INFO_RECURSIVE |
2992 CAMEL_STORE_FOLDER_INFO_SUBSCRIBED,
2993 cancellable, &local_error);
2994
2995 if (folder_info == NULL) {
2996 g_propagate_error (error, local_error);
2997
2998 return FALSE;
2999 }
3000
3001 to_update = folder_info;
3002
3003 fid = camel_ews_store_summary_get_folder_id_from_name (
3004 ews_store->summary, folder_name);
3005
3006 if (!fid) {
3007 camel_folder_info_free (folder_info);
3008
3009 g_set_error (
3010 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
3011 _("Folder does not exist"));
3012 return FALSE;
3013 }
3014
3015 if (g_str_equal (fid, EWS_FOREIGN_FOLDER_ROOT_ID)) {
3016 g_free (fid);
3017 camel_folder_info_free (folder_info);
3018
3019 g_set_error (
3020 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
3021 _("Cannot remove folder “%s”, it is used for folders of other users only"),
3022 folder_name);
3023 return FALSE;
3024 }
3025
3026 if (g_str_equal (fid, EWS_PUBLIC_FOLDER_ROOT_ID)) {
3027 g_free (fid);
3028 camel_folder_info_free (folder_info);
3029
3030 g_set_error (
3031 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
3032 _("Cannot remove folder “%s”, it is used for public folders only"),
3033 folder_name);
3034 return FALSE;
3035 }
3036
3037 trash_fid = camel_ews_store_summary_get_folder_id_from_folder_type (ews_store->summary, CAMEL_FOLDER_TYPE_TRASH);
3038 trash_name = camel_ews_store_summary_get_folder_full_name (ews_store->summary, trash_fid, &local_error);
3039 if (!trash_name) {
3040 g_free (trash_fid);
3041 g_free (fid);
3042 camel_folder_info_free (folder_info);
3043
3044 g_propagate_error (error, local_error);
3045 return FALSE;
3046 }
3047
3048 is_under_trash_folder = g_str_has_prefix (folder_name, trash_name);
3049
3050 g_free (trash_name);
3051
3052 if (!camel_ews_store_connected (ews_store, cancellable, &local_error)) {
3053 g_free (trash_fid);
3054 g_free (fid);
3055 camel_folder_info_free (folder_info);
3056
3057 g_propagate_error (error, local_error);
3058
3059 return FALSE;
3060 }
3061
3062 if (camel_ews_store_summary_get_foreign (ews_store->summary, fid, NULL) ||
3063 camel_ews_store_summary_get_public (ews_store->summary, fid, NULL)) {
3064 /* do not delete foreign or public folders,
3065 * only remove them from local store */
3066 success = TRUE;
3067 } else {
3068 EEwsConnection *connection;
3069
3070 connection = camel_ews_store_ref_connection (ews_store);
3071
3072 success = e_ews_connection_delete_folder_sync (
3073 connection,
3074 EWS_PRIORITY_MEDIUM,
3075 fid, FALSE, is_under_trash_folder ? "HardDelete" : "MoveToDeletedItems",
3076 cancellable, &local_error);
3077
3078 g_object_unref (connection);
3079 }
3080
3081 if (!success) {
3082 g_free (trash_fid);
3083 g_free (fid);
3084 camel_folder_info_free (folder_info);
3085
3086 camel_ews_store_maybe_disconnect (ews_store, local_error);
3087 g_propagate_error (error, local_error);
3088
3089 return FALSE;
3090 }
3091
3092 if (is_under_trash_folder) {
3093 success = camel_ews_utils_delete_folders_from_summary_recursive (ews_store, to_update, FALSE, error);
3094
3095 if (!success) {
3096 g_free (trash_fid);
3097 g_free (fid);
3098 camel_folder_info_free (folder_info);
3099
3100 g_propagate_error (error, local_error);
3101 return FALSE;
3102 }
3103 } else {
3104 camel_ews_store_summary_set_parent_folder_id (ews_store->summary, fid, trash_fid);
3105 }
3106
3107 camel_subscribable_folder_unsubscribed (CAMEL_SUBSCRIBABLE (ews_store), folder_info);
3108 camel_store_folder_deleted (CAMEL_STORE (ews_store), folder_info);
3109
3110 camel_folder_info_free (folder_info);
3111
3112 if (!is_under_trash_folder) {
3113 camel_ews_store_summary_rebuild_hashes (ews_store->summary);
3114
3115 folder_info = camel_ews_utils_build_folder_info (ews_store, fid);
3116
3117 to_update = folder_info;
3118 ews_update_store_move_recursive (ews_store, to_update);
3119 camel_folder_info_free (folder_info);
3120 }
3121
3122 camel_ews_store_ensure_virtual_folders (ews_store);
3123 camel_ews_store_summary_save (ews_store->summary, NULL);
3124
3125 g_free (trash_fid);
3126 g_free (fid);
3127
3128 return TRUE;
3129 }
3130
3131 struct _rename_cb_data {
3132 const gchar *display_name;
3133 const gchar *change_key;
3134 const gchar *folder_id;
3135 };
3136
3137 static gboolean
3138 rename_folder_cb (ESoapRequest *request,
3139 gpointer user_data,
3140 GError **error)
3141 {
3142 struct _rename_cb_data *rename_data = user_data;
3143
3144 e_ews_request_start_item_change (
3145 request, E_EWS_ITEMCHANGE_TYPE_FOLDER,
3146 rename_data->folder_id, rename_data->change_key, 0);
3147 e_soap_request_start_element (request, "SetFolderField", NULL, NULL);
3148 e_ews_request_write_string_parameter_with_attribute (
3149 request, "FieldURI", NULL, NULL,
3150 "FieldURI", "folder:DisplayName");
3151
3152 e_soap_request_start_element (request, "Folder", NULL, NULL);
3153 e_ews_request_write_string_parameter (request, "DisplayName", NULL, rename_data->display_name);
3154 e_soap_request_end_element (request); /* Folder */
3155
3156 e_soap_request_end_element (request); /* SetFolderField */
3157
3158 e_ews_request_end_item_change (request);
3159
3160 return TRUE;
3161 }
3162
3163 static gboolean
3164 ews_rename_folder_sync (CamelStore *store,
3165 const gchar *old_name,
3166 const gchar *new_name,
3167 GCancellable *cancellable,
3168 GError **error)
3169 {
3170 CamelEwsStore *ews_store = CAMEL_EWS_STORE (store);
3171 CamelEwsStoreSummary *ews_summary = ews_store->summary;
3172 EEwsConnection *connection;
3173 const gchar *old_slash, *new_slash;
3174 gchar *fid;
3175 gchar *changekey;
3176 gboolean res = FALSE;
3177 GError *local_error = NULL;
3178
3179 if (!strcmp (old_name, new_name))
3180 return TRUE;
3181
3182 if (!camel_ews_store_connected (ews_store, cancellable, error)) {
3183 return FALSE;
3184 }
3185
3186 fid = camel_ews_store_summary_get_folder_id_from_name (ews_summary, old_name);
3187 if (!fid) {
3188 g_set_error (
3189 error, CAMEL_STORE_ERROR,
3190 CAMEL_STORE_ERROR_NO_FOLDER,
3191 _("Folder %s does not exist"), old_name);
3192 return FALSE;
3193 }
3194
3195 changekey = camel_ews_store_summary_get_change_key (ews_summary, fid, error);
3196 if (!changekey) {
3197 g_set_error (
3198 error, CAMEL_STORE_ERROR,
3199 CAMEL_STORE_ERROR_NO_FOLDER,
3200 _("No change key record for folder %s"), fid);
3201 g_free (fid);
3202 return FALSE;
3203 }
3204
3205 connection = camel_ews_store_ref_connection (ews_store);
3206
3207 old_slash = g_strrstr (old_name, "/");
3208 new_slash = g_strrstr (new_name, "/");
3209
3210 if (old_slash)
3211 old_slash++;
3212 else
3213 old_slash = old_name;
3214
3215 if (new_slash)
3216 new_slash++;
3217 else
3218 new_slash = new_name;
3219
3220 if (strcmp (old_slash, new_slash)) {
3221 gint parent_len = old_slash - old_name;
3222 struct _rename_cb_data rename_data;
3223
3224 /* Folder basename changed (i.e. UpdateFolder needed).
3225 * Therefore, we can only do it if the folder hasn't also
3226 * been moved from one parent folder to another.
3227 *
3228 * Strictly speaking, we could probably handle this, even
3229 * if there are name collisions. We could UpdateFolder to
3230 * a new temporary name that doesn't exist in either the
3231 * old or new parent folders, then MoveFolder, then issue
3232 * another UpdateFolder to the name we actually wanted.
3233 * But since the Evolution UI doesn't seem to let us
3234 * make both changes at the same time anyway, we'll just
3235 * bail out for now; we can deal with it later if we need
3236 * to.
3237 */
3238 if (new_slash - new_name != parent_len ||
3239 strncmp (old_name, new_name, parent_len)) {
3240 g_set_error (
3241 error, CAMEL_STORE_ERROR,
3242 CAMEL_STORE_ERROR_INVALID,
3243 _("Cannot both rename and move a folder at the same time"));
3244 goto out;
3245 }
3246
3247 rename_data.display_name = new_slash;
3248 rename_data.folder_id = fid;
3249 rename_data.change_key = changekey;
3250
3251 res = e_ews_connection_update_folder_sync (
3252 connection, EWS_PRIORITY_MEDIUM,
3253 rename_folder_cb, &rename_data,
3254 cancellable, &local_error);
3255
3256 if (!res) {
3257 goto out;
3258 }
3259 camel_ews_store_summary_set_folder_name (ews_summary, fid, new_slash);
3260 } else {
3261 gchar *pfid = NULL;
3262 gchar *parent_name;
3263
3264 /* If we are not moving to the root folder, work out the ItemId of
3265 * the new parent folder */
3266 if (new_slash != new_name) {
3267 parent_name = g_strndup (new_name, new_slash - new_name - 1);
3268 pfid = camel_ews_store_summary_get_folder_id_from_name (
3269 ews_summary, parent_name);
3270 if (!pfid) {
3271 g_set_error (
3272 error, CAMEL_STORE_ERROR,
3273 CAMEL_STORE_ERROR_NO_FOLDER,
3274 _("Cannot find folder ID for parent folder %s"),
3275 parent_name);
3276 g_free (parent_name);
3277 goto out;
3278 }
3279 g_free (parent_name);
3280 } else {
3281 gchar *inbox_id;
3282
3283 inbox_id = camel_ews_store_summary_get_folder_id_from_folder_type (ews_summary, CAMEL_FOLDER_TYPE_INBOX);
3284
3285 if (!inbox_id) {
3286 g_set_error (error, CAMEL_STORE_ERROR, CAMEL_STORE_ERROR_NO_FOLDER,
3287 _("Folder %s does not exist"), _("Inbox"));
3288 goto out;
3289 }
3290
3291 pfid = camel_ews_store_summary_get_parent_folder_id (ews_summary, inbox_id, NULL);
3292
3293 g_free (inbox_id);
3294
3295 if (!pfid) {
3296 g_set_error (error, CAMEL_STORE_ERROR, CAMEL_STORE_ERROR_NO_FOLDER,
3297 _("Cannot find folder ID for parent folder %s"), _("Inbox"));
3298 goto out;
3299 }
3300 }
3301
3302 res = e_ews_connection_move_folder_sync (
3303 connection, EWS_PRIORITY_MEDIUM,
3304 pfid, fid, cancellable, &local_error);
3305
3306 if (!res) {
3307 g_free (pfid);
3308 goto out;
3309 }
3310 camel_ews_store_summary_set_parent_folder_id (ews_summary, fid, pfid);
3311 g_free (pfid);
3312 }
3313
3314 res = TRUE;
3315 out:
3316 if (local_error) {
3317 camel_ews_store_maybe_disconnect (ews_store, local_error);
3318 g_propagate_error (error, local_error);
3319 }
3320
3321 g_object_unref (connection);
3322
3323 g_free (changekey);
3324 g_free (fid);
3325 return res;
3326 }
3327
3328 gchar *
3329 ews_get_name (CamelService *service,
3330 gboolean brief)
3331 {
3332 CamelSettings *settings;
3333 gchar *name;
3334 gchar *host;
3335 gchar *user;
3336
3337 settings = camel_service_ref_settings (service);
3338
3339 user = camel_network_settings_dup_user (CAMEL_NETWORK_SETTINGS (settings));
3340 host = camel_ews_utils_get_host_name (settings);
3341
3342 g_object_unref (settings);
3343
3344 if (brief)
3345 name = g_strdup_printf (
3346 _("Exchange server %s"), host);
3347 else
3348 name = g_strdup_printf (
3349 _("Exchange service for %s on %s"), user, host);
3350
3351 g_free (host);
3352 g_free (user);
3353
3354 return name;
3355 }
3356
3357 EEwsConnection *
3358 camel_ews_store_ref_connection (CamelEwsStore *ews_store)
3359 {
3360 EEwsConnection *connection = NULL;
3361
3362 g_return_val_if_fail (CAMEL_IS_EWS_STORE (ews_store), NULL);
3363
3364 g_mutex_lock (&ews_store->priv->connection_lock);
3365
3366 if (ews_store->priv->connection != NULL)
3367 connection = g_object_ref (ews_store->priv->connection);
3368
3369 g_mutex_unlock (&ews_store->priv->connection_lock);
3370
3371 return connection;
3372 }
3373
3374 static CamelFolder *
3375 ews_get_trash_folder_sync (CamelStore *store,
3376 GCancellable *cancellable,
3377 GError **error)
3378 {
3379 CamelEwsStore *ews_store;
3380 CamelFolder *folder = NULL;
3381 gchar *folder_id, *folder_name;
3382
3383 g_return_val_if_fail (CAMEL_IS_EWS_STORE (store), NULL);
3384
3385 ews_store = CAMEL_EWS_STORE (store);
3386 folder_id = camel_ews_store_summary_get_folder_id_from_folder_type (
3387 ews_store->summary, CAMEL_FOLDER_TYPE_TRASH);
3388
3389 if (folder_id == NULL) {
3390 g_set_error (
3391 error, CAMEL_STORE_ERROR,
3392 CAMEL_STORE_ERROR_NO_FOLDER,
3393 _("Could not locate Trash folder"));
3394 return NULL;
3395 }
3396
3397 folder_name = camel_ews_store_summary_get_folder_full_name (
3398 ews_store->summary, folder_id, NULL);
3399
3400 folder = camel_store_get_folder_sync (
3401 store, folder_name, 0, cancellable, error);
3402
3403 g_free (folder_name);
3404 g_free (folder_id);
3405
3406 if (folder) {
3407 GPtrArray *folders;
3408 gboolean can = TRUE;
3409 gint ii;
3410
3411 /* Save content of all opened folders, thus any messages deleted in them
3412 are moved to the Deleted Items folder first, thus in case of the trash
3413 folder instance being used to expunge messages will contain all of them.
3414 */
3415 folders = camel_store_dup_opened_folders (store);
3416 for (ii = 0; ii < folders->len; ii++) {
3417 CamelFolder *secfolder = folders->pdata[ii];
3418
3419 if (secfolder != folder && can)
3420 can = camel_folder_synchronize_sync (secfolder, FALSE, cancellable, NULL);
3421
3422 g_object_unref (secfolder);
3423 }
3424 g_ptr_array_free (folders, TRUE);
3425
3426 /* To return 'Deleted Items' folder with current content,
3427 not with possibly stale locally cached copy. */
3428 camel_folder_refresh_info_sync (folder, cancellable, NULL);
3429 }
3430
3431 return folder;
3432 }
3433
3434 static CamelFolder *
3435 ews_get_junk_folder_sync (CamelStore *store,
3436 GCancellable *cancellable,
3437 GError **error)
3438 {
3439 CamelEwsStore *ews_store;
3440 CamelFolder *folder = NULL;
3441 gchar *folder_id, *folder_name;
3442
3443 g_return_val_if_fail (CAMEL_IS_EWS_STORE (store), NULL);
3444
3445 ews_store = CAMEL_EWS_STORE (store);
3446 folder_id = camel_ews_store_summary_get_folder_id_from_folder_type (
3447 ews_store->summary, CAMEL_FOLDER_TYPE_JUNK);
3448
3449 if (folder_id == NULL) {
3450 g_set_error (
3451 error, CAMEL_STORE_ERROR,
3452 CAMEL_STORE_ERROR_NO_FOLDER,
3453 _("Could not locate Junk folder"));
3454 return NULL;
3455 }
3456
3457 folder_name = camel_ews_store_summary_get_folder_full_name (
3458 ews_store->summary, folder_id, NULL);
3459
3460 folder = camel_store_get_folder_sync (
3461 store, folder_name, 0, cancellable, error);
3462
3463 g_free (folder_name);
3464 g_free (folder_id);
3465
3466 return folder;
3467 }
3468
3469 static gboolean
3470 ews_can_refresh_folder (CamelStore *store,
3471 CamelFolderInfo *info,
3472 GError **error)
3473 {
3474 CamelFolder *folder;
3475 CamelSettings *settings;
3476 CamelEwsSettings *ews_settings;
3477 gboolean check_all, res = FALSE;
3478 GError *local_error = NULL;
3479
3480 /* Skip unselectable folders from automatic refresh */
3481 if (info && (info->flags & CAMEL_FOLDER_NOSELECT) != 0)
3482 return FALSE;
3483
3484 settings = camel_service_ref_settings (CAMEL_SERVICE (store));
3485
3486 ews_settings = CAMEL_EWS_SETTINGS (settings);
3487 check_all = camel_ews_settings_get_check_all (ews_settings);
3488
3489 g_object_unref (settings);
3490
3491 if (check_all || CAMEL_STORE_CLASS (camel_ews_store_parent_class)->can_refresh_folder (store, info, &local_error))
3492 return TRUE;
3493
3494 if (local_error) {
3495 g_propagate_error (error, local_error);
3496 return FALSE;
3497 }
3498
3499 folder = camel_store_get_folder_sync (store, info->full_name, 0, NULL, &local_error);
3500 if (folder && CAMEL_IS_EWS_FOLDER (folder))
3501 res = camel_ews_folder_get_check_folder (CAMEL_EWS_FOLDER (folder));
3502
3503 g_clear_object (&folder);
3504
3505 if (local_error)
3506 g_propagate_error (error, local_error);
3507
3508 return res;
3509 }
3510
3511 static gboolean
3512 ews_store_folder_is_subscribed (CamelSubscribable *subscribable,
3513 const gchar *folder_name)
3514 {
3515 CamelEwsStore *ews_store = CAMEL_EWS_STORE (subscribable);
3516 gchar *fid;
3517 gboolean truth = FALSE;
3518 GError *error = NULL;
3519
3520 fid = camel_ews_store_summary_get_folder_id_from_name (ews_store->summary, folder_name);
3521 if (!fid)
3522 return FALSE;
3523
3524 if (camel_ews_store_summary_get_foreign (ews_store->summary, fid, &error) && !error) {
3525 truth = TRUE;
3526 }
3527 g_clear_error (&error);
3528
3529 if (!truth && camel_ews_store_summary_get_public (ews_store->summary, fid, &error) && !error) {
3530 truth = TRUE;
3531 }
3532 g_clear_error (&error);
3533
3534 g_free (fid);
3535
3536 return truth;
3537 }
3538
3539 /* caller should hold ews_store->priv->get_finfo_lock already */
3540 static EEwsFolder *
3541 ews_store_find_public_folder (CamelEwsStore *ews_store,
3542 const gchar *folder_name)
3543 {
3544 EEwsFolder *folder = NULL;
3545 GSList *piter;
3546 gchar **folders;
3547 gint ii;
3548
3549 g_return_val_if_fail (CAMEL_IS_EWS_STORE (ews_store), NULL);
3550 g_return_val_if_fail (folder_name != NULL, NULL);
3551
3552 folders = g_strsplit (folder_name, "/", -1);
3553 if (!folders || !folders[0] || g_strcmp0 (folders[0], EWS_PUBLIC_FOLDER_ROOT_DISPLAY_NAME) != 0) {
3554 g_strfreev (folders);
3555 return NULL;
3556 }
3557
3558 /* they are stored in public_folders from top level to bottom level */
3559 piter = ews_store->priv->public_folders;
3560 for (ii = 1; folders[ii] && piter; ii++) {
3561 const gchar *fname = folders[ii];
3562
3563 while (piter) {
3564 EEwsFolder *subf = piter->data;
3565 const EwsFolderId *parent_id;
3566
3567 if (!subf) {
3568 piter = NULL;
3569 break;
3570 }
3571
3572 if (g_strcmp0 (e_ews_folder_get_name (subf), fname) == 0 ||
3573 g_strcmp0 (e_ews_folder_get_escaped_name (subf), fname) == 0) {
3574 parent_id = e_ews_folder_get_parent_id (subf);
3575 if (!folder && (!parent_id || g_strcmp0 (parent_id->id, EWS_PUBLIC_FOLDER_ROOT_ID) == 0)) {
3576 folder = subf;
3577 break;
3578 } else if (parent_id && folder) {
3579 const EwsFolderId *fid = e_ews_folder_get_id (folder);
3580
3581 if (fid && g_strcmp0 (fid->id, parent_id->id) == 0) {
3582 folder = subf;
3583 break;
3584 }
3585 }
3586 }
3587
3588 piter = piter->next;
3589 }
3590 }
3591
3592 if (!piter || folders[ii])
3593 folder = NULL;
3594
3595 g_strfreev (folders);
3596
3597 return folder;
3598 }
3599
3600 /* ppath contains proposed path, this only makes sure that it's a unique path */
3601 static void
3602 ews_store_ensure_unique_path (CamelEwsStore *ews_store,
3603 gchar **ppath)
3604 {
3605 gboolean done;
3606 guint counter = 0;
3607 gchar *base_path = NULL;
3608
3609 g_return_if_fail (ews_store != NULL);
3610 g_return_if_fail (ews_store->summary != NULL);
3611 g_return_if_fail (ppath != NULL);
3612 g_return_if_fail (*ppath != NULL);
3613
3614 done = FALSE;
3615 while (!done) {
3616 gchar *fid;
3617
3618 done = TRUE;
3619
3620 fid = camel_ews_store_summary_get_folder_id_from_name (ews_store->summary, *ppath);
3621 if (fid) {
3622 g_free (fid);
3623
3624 done = FALSE;
3625 counter++;
3626 if (!counter) {
3627 g_debug ("%s: Counter overflow", G_STRFUNC);
3628 break;
3629 }
3630
3631 if (!base_path)
3632 base_path = *ppath;
3633 else
3634 g_free (*ppath);
3635
3636 *ppath = g_strdup_printf ("%s_%u", base_path, counter);
3637 }
3638 }
3639
3640 g_free (base_path);
3641 }
3642
3643 static gboolean
3644 ews_store_subscribe_folder_sync (CamelSubscribable *subscribable,
3645 const gchar *folder_name,
3646 GCancellable *cancellable,
3647 GError **error)
3648 {
3649 CamelEwsStore *ews_store = CAMEL_EWS_STORE (subscribable);
3650 EEwsFolder *folder;
3651 const EwsFolderId *fid;
3652 gboolean res = TRUE;
3653 gchar *tmp;
3654
3655 if (!camel_offline_store_get_online (CAMEL_OFFLINE_STORE (ews_store))) {
3656 g_set_error_literal (
3657 error, CAMEL_SERVICE_ERROR, CAMEL_SERVICE_ERROR_UNAVAILABLE,
3658 _("Cannot subscribe EWS folders in offline mode"));
3659 return FALSE;
3660 }
3661
3662 /* Folder's name is not stored with the "/" in the beginning, even when the
3663 folder is in the root (same level as "Inbox", "Drafts", etc. */
3664 if (*folder_name == '/')
3665 folder_name++;
3666
3667 /* can subscribe only public folders,
3668 thus skip anything known */
3669 tmp = camel_ews_store_summary_get_folder_id_from_name (ews_store->summary, folder_name);
3670 if (tmp) {
3671 g_free (tmp);
3672 return TRUE;
3673 }
3674
3675 g_mutex_lock (&ews_store->priv->get_finfo_lock);
3676 if (!ews_store->priv->public_folders) {
3677 g_mutex_unlock (&ews_store->priv->get_finfo_lock);
3678
3679 g_set_error (
3680 error, CAMEL_STORE_ERROR, CAMEL_STORE_ERROR_NO_FOLDER,
3681 _("Cannot subscribe folder “%s”, no public folder available"), folder_name);
3682 return FALSE;
3683 }
3684
3685 folder = ews_store_find_public_folder (ews_store, folder_name);
3686 if (!folder) {
3687 g_mutex_unlock (&ews_store->priv->get_finfo_lock);
3688
3689 g_set_error (
3690 error, CAMEL_STORE_ERROR, CAMEL_STORE_ERROR_NO_FOLDER,
3691 _("Cannot subscribe folder “%s”, folder not found"), folder_name);
3692 return FALSE;
3693 }
3694
3695 fid = e_ews_folder_get_id (folder);
3696 if (!fid) {
3697 g_mutex_unlock (&ews_store->priv->get_finfo_lock);
3698 g_set_error (
3699 error, CAMEL_STORE_ERROR, CAMEL_STORE_ERROR_NO_FOLDER,
3700 _("Cannot subscribe folder “%s”, folder ID not found"), folder_name);
3701 return FALSE;
3702 }
3703
3704 if (camel_ews_store_summary_has_folder (ews_store->summary, EWS_PUBLIC_FOLDER_ROOT_ID)) {
3705 gchar *parent_name = camel_ews_store_summary_get_folder_name (ews_store->summary, EWS_PUBLIC_FOLDER_ROOT_ID, NULL);
3706
3707 if (!parent_name) {
3708 g_mutex_unlock (&ews_store->priv->get_finfo_lock);
3709 g_set_error (
3710 error, CAMEL_STORE_ERROR, CAMEL_STORE_ERROR_NO_FOLDER,
3711 _("Cannot subscribe folder “%s”, public folder root not found"), folder_name);
3712 return FALSE;
3713 }
3714
3715 tmp = g_strconcat (parent_name, "/", e_ews_folder_get_escaped_name (folder), NULL);
3716 g_free (parent_name);
3717 } else {
3718 tmp = g_strconcat (EWS_PUBLIC_FOLDER_ROOT_DISPLAY_NAME, "/", e_ews_folder_get_escaped_name (folder), NULL);
3719 }
3720
3721 if (e_ews_folder_get_folder_type (folder) != E_EWS_FOLDER_TYPE_MAILBOX) {
3722 CamelSettings *settings;
3723 CamelEwsSettings *ews_settings;
3724
3725 settings = camel_service_ref_settings (CAMEL_SERVICE (ews_store));
3726 ews_settings = CAMEL_EWS_SETTINGS (settings);
3727
3728 res = e_ews_folder_utils_add_as_esource (NULL,
3729 camel_ews_settings_get_hosturl (ews_settings),
3730 camel_network_settings_get_user (CAMEL_NETWORK_SETTINGS (ews_settings)),
3731 folder,
3732 E_EWS_ESOURCE_FLAG_OFFLINE_SYNC | E_EWS_ESOURCE_FLAG_PUBLIC_FOLDER,
3733 0,
3734 cancellable,
3735 error);
3736
3737 g_object_unref (settings);
3738 }
3739
3740 if (res) {
3741 ews_store_ensure_unique_path (ews_store, &tmp);
3742
3743 camel_ews_store_summary_new_folder (ews_store->summary, fid->id, EWS_PUBLIC_FOLDER_ROOT_ID,
3744 NULL,
3745 strrchr (tmp, '/') + 1,
3746 e_ews_folder_get_folder_type (folder),
3747 CAMEL_FOLDER_SUBSCRIBED,
3748 e_ews_folder_get_total_count (folder),
3749 FALSE, TRUE);
3750
3751 if (e_ews_folder_get_folder_type (folder) == E_EWS_FOLDER_TYPE_MAILBOX) {
3752 CamelFolderInfo *fi;
3753
3754 camel_ews_store_ensure_virtual_folders (ews_store);
3755
3756 fi = camel_ews_utils_build_folder_info (ews_store, fid->id);
3757 camel_store_folder_created (CAMEL_STORE (ews_store), fi);
3758 camel_subscribable_folder_subscribed (CAMEL_SUBSCRIBABLE (ews_store), fi);
3759 camel_folder_info_free (fi);
3760 }
3761 }
3762
3763 camel_ews_store_summary_save (ews_store->summary, NULL);
3764
3765 g_free (tmp);
3766 g_mutex_unlock (&ews_store->priv->get_finfo_lock);
3767
3768 return res;
3769 }
3770
3771 static gboolean
3772 ews_store_unsubscribe_folder_sync (CamelSubscribable *subscribable,
3773 const gchar *folder_name,
3774 GCancellable *cancellable,
3775 GError **error)
3776 {
3777 CamelEwsStore *ews_store = CAMEL_EWS_STORE (subscribable);
3778 EEwsFolderType folder_type;
3779 EEwsFolder *folder;
3780 gboolean is_public;
3781 gboolean res = TRUE;
3782 gchar *fid = NULL;
3783
3784 if (!camel_offline_store_get_online (CAMEL_OFFLINE_STORE (ews_store))) {
3785 g_set_error_literal (
3786 error, CAMEL_SERVICE_ERROR, CAMEL_SERVICE_ERROR_UNAVAILABLE,
3787 _("Cannot unsubscribe EWS folders in offline mode"));
3788 return FALSE;
3789 }
3790
3791 folder = ews_store_find_public_folder (ews_store, folder_name);
3792 if (folder) {
3793 const EwsFolderId *folder_id = e_ews_folder_get_id (folder);
3794
3795 if (folder_id) {
3796 fid = g_strdup (folder_id->id);
3797 folder_type = e_ews_folder_get_folder_type (folder);
3798 }
3799 }
3800
3801 if (!fid) {
3802 fid = camel_ews_store_summary_get_folder_id_from_name (ews_store->summary, folder_name);
3803 if (fid)
3804 folder_type = camel_ews_store_summary_get_folder_type (ews_store->summary, fid, NULL);
3805 }
3806
3807 if (!fid) {
3808 /* no such folder in the cache, might be unsubscribed already */
3809 return TRUE;
3810 }
3811
3812 is_public = camel_ews_store_summary_get_public (ews_store->summary, fid, NULL);
3813 if (!is_public && !camel_ews_store_summary_get_foreign (ews_store->summary, fid, NULL)) {
3814 /* nothing to do for regular folders */
3815 res = TRUE;
3816 } else {
3817 CamelFolderInfo *fi;
3818
3819 if (!is_public && camel_ews_store_summary_get_foreign_subfolders (ews_store->summary, fid, NULL)) {
3820 /* when subscribed with subfolders, then unsubscribe with subfolders as well */
3821 GSList *local_folders = NULL, *ii;
3822 gchar *full_name = camel_ews_store_summary_get_folder_full_name (ews_store->summary, fid, NULL);
3823 if (full_name) {
3824 local_folders = camel_ews_store_summary_get_folders (ews_store->summary, full_name, FALSE);
3825 }
3826 g_free (full_name);
3827
3828 for (ii = local_folders; ii != NULL; ii = ii->next) {
3829 const gchar *lfid = ii->data;
3830 EEwsFolderType ftype;
3831
3832 if (g_strcmp0 (lfid, fid) == 0)
3833 continue;
3834
3835 ftype = camel_ews_store_summary_get_folder_type (ews_store->summary, lfid, NULL);
3836 if (ftype != E_EWS_FOLDER_TYPE_MAILBOX)
3837 continue;
3838
3839 fi = camel_ews_utils_build_folder_info (ews_store, lfid);
3840 camel_ews_store_summary_remove_folder (ews_store->summary, lfid, NULL);
3841
3842 camel_subscribable_folder_unsubscribed (CAMEL_SUBSCRIBABLE (ews_store), fi);
3843 camel_store_folder_deleted (CAMEL_STORE (ews_store), fi);
3844 camel_folder_info_free (fi);
3845 }
3846
3847 g_slist_free_full (local_folders, g_free);
3848 }
3849
3850 if (folder_type != E_EWS_FOLDER_TYPE_MAILBOX) {
3851 CamelSettings *settings;
3852 CamelEwsSettings *ews_settings;
3853
3854 settings = camel_service_ref_settings (CAMEL_SERVICE (ews_store));
3855 ews_settings = CAMEL_EWS_SETTINGS (settings);
3856
3857 res = e_ews_folder_utils_remove_as_esource (
3858 camel_ews_settings_get_hosturl (ews_settings),
3859 camel_network_settings_get_user (CAMEL_NETWORK_SETTINGS (ews_settings)),
3860 fid,
3861 cancellable,
3862 error);
3863
3864 g_object_unref (settings);
3865 }
3866
3867 if (res) {
3868 fi = camel_ews_utils_build_folder_info (ews_store, fid);
3869 camel_ews_store_summary_remove_folder (ews_store->summary, fid, error);
3870
3871 if (folder_type == E_EWS_FOLDER_TYPE_MAILBOX) {
3872 camel_subscribable_folder_unsubscribed (CAMEL_SUBSCRIBABLE (ews_store), fi);
3873 camel_store_folder_deleted (CAMEL_STORE (ews_store), fi);
3874 camel_folder_info_free (fi);
3875
3876 camel_ews_store_ensure_virtual_folders (ews_store);
3877 }
3878 }
3879
3880 camel_ews_store_summary_save (ews_store->summary, NULL);
3881 }
3882
3883 g_free (fid);
3884
3885 return res;
3886 }
3887
3888 gboolean
3889 camel_ews_store_connected (CamelEwsStore *ews_store,
3890 GCancellable *cancellable,
3891 GError **error)
3892 {
3893
3894 if (!camel_offline_store_get_online (CAMEL_OFFLINE_STORE (ews_store))) {
3895 g_set_error (
3896 error, CAMEL_SERVICE_ERROR,
3897 CAMEL_SERVICE_ERROR_UNAVAILABLE,
3898 _("You must be working online to complete this operation"));
3899 return FALSE;
3900 }
3901
3902 if (!camel_service_connect_sync ((CamelService *) ews_store, cancellable, error))
3903 return FALSE;
3904
3905 return TRUE;
3906 }
3907
3908 void
3909 camel_ews_store_maybe_disconnect (CamelEwsStore *store,
3910 GError *error)
3911 {
3912 CamelService *service;
3913 gboolean is_auth_failed;
3914
3915 g_return_if_fail (store != NULL);
3916
3917 if (!error)
3918 return;
3919
3920 service = CAMEL_SERVICE (store);
3921
3922 if (camel_service_get_connection_status (service) != CAMEL_SERVICE_CONNECTED)
3923 return;
3924
3925 is_auth_failed = g_error_matches (error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_AUTHENTICATION_FAILED);
3926
3927 if (is_auth_failed || g_error_matches (error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_NORESPONSE))
3928 camel_service_disconnect_sync (service, FALSE, NULL, NULL);
3929
3930 if (is_auth_failed) {
3931 ESourceRegistry *registry;
3932
3933 error->domain = CAMEL_SERVICE_ERROR;
3934 error->code = CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE;
3935
3936 registry = e_source_registry_new_sync (NULL, NULL);
3937
3938 if (registry) {
3939 ESource *source, *collection = NULL;
3940
3941 source = e_source_registry_ref_source (registry, camel_service_get_uid (service));
3942
3943 if (source) {
3944 collection = e_source_registry_find_extension (registry, source, E_SOURCE_EXTENSION_COLLECTION);
3945
3946 if (collection)
3947 e_source_emit_credentials_required (collection, E_SOURCE_CREDENTIALS_REASON_REJECTED, NULL, 0, error);
3948 }
3949
3950 g_clear_object (&collection);
3951 g_clear_object (&source);
3952 g_clear_object (&registry);
3953 }
3954 }
3955 }
3956
3957 static void
3958 ews_store_unset_oof_settings_state (CamelSession *session,
3959 GCancellable *cancellable,
3960 gpointer user_data,
3961 GError **error)
3962 {
3963
3964 CamelEwsStore *ews_store = user_data;
3965 EEwsConnection *connection;
3966 EEwsOofSettings *oof_settings;
3967 EEwsOofState state;
3968 GError *local_error = NULL;
3969
3970 camel_operation_push_message (cancellable, _("Unsetting the “Out of Office” status"));
3971
3972 connection = camel_ews_store_ref_connection (ews_store);
3973 oof_settings = e_ews_oof_settings_new_sync (connection, cancellable, &local_error);
3974 g_object_unref (connection);
3975 if (local_error != NULL) {
3976 g_propagate_error (error, local_error);
3977 camel_operation_pop_message (cancellable);
3978 return;
3979 }
3980
3981 state = e_ews_oof_settings_get_state (oof_settings);
3982 if (state == E_EWS_OOF_STATE_DISABLED) {
3983 g_object_unref (oof_settings);
3984 camel_operation_pop_message (cancellable);
3985 return;
3986 }
3987
3988 e_ews_oof_settings_set_state (oof_settings, E_EWS_OOF_STATE_DISABLED);
3989 e_ews_oof_settings_submit_sync (oof_settings, cancellable, error);
3990 g_object_unref (oof_settings);
3991
3992 camel_operation_pop_message (cancellable);
3993 }
3994
3995 void
3996 camel_ews_store_unset_oof_settings_state (CamelEwsStore *ews_store)
3997 {
3998 CamelService *service;
3999 CamelSession *session;
4000
4001 g_return_if_fail (CAMEL_IS_EWS_STORE (ews_store));
4002
4003 service = CAMEL_SERVICE (ews_store);
4004 session = camel_service_ref_session (service);
4005
4006 camel_session_submit_job (
4007 session, _("Unsetting the “Out of Office” status"),
4008 ews_store_unset_oof_settings_state,
4009 g_object_ref (ews_store),
4010 g_object_unref);
4011
4012 g_object_unref (session);
4013 }
4014
4015 static void
4016 ews_store_dispose (GObject *object)
4017 {
4018 CamelEwsStore *ews_store;
4019 CamelEwsSettings *ews_settings;
4020
4021 ews_store = CAMEL_EWS_STORE (object);
4022
4023 ews_settings = CAMEL_EWS_SETTINGS (camel_service_ref_settings (CAMEL_SERVICE (ews_store)));
4024 g_signal_handlers_disconnect_by_func (ews_settings, camel_ews_store_listen_notifications_cb, ews_store);
4025 g_signal_handlers_disconnect_by_func (ews_settings, camel_ews_store_check_all_cb, ews_store);
4026 g_object_unref (ews_settings);
4027
4028 if (ews_store->summary != NULL) {
4029 camel_ews_store_summary_save (ews_store->summary, NULL);
4030 g_object_unref (ews_store->summary);
4031 ews_store->summary = NULL;
4032 }
4033
4034 g_mutex_lock (&ews_store->priv->connection_lock);
4035 ews_store_unset_connection_locked (ews_store, FALSE);
4036 g_mutex_unlock (&ews_store->priv->connection_lock);
4037
4038 g_slist_free_full (ews_store->priv->update_folder_names, g_free);
4039 ews_store->priv->update_folder_names = NULL;
4040
4041 g_slist_free_full (ews_store->priv->public_folders, g_object_unref);
4042 ews_store->priv->public_folders = NULL;
4043
4044 /* Chain up to parent's dispose() method. */
4045 G_OBJECT_CLASS (camel_ews_store_parent_class)->dispose (object);
4046 }
4047
4048 static void
4049 ews_store_finalize (GObject *object)
4050 {
4051 CamelEwsStore *ews_store;
4052
4053 ews_store = CAMEL_EWS_STORE (object);
4054
4055 g_free (ews_store->storage_path);
4056 g_free (ews_store->priv->last_subscription_id);
4057 g_mutex_clear (&ews_store->priv->get_finfo_lock);
4058 g_mutex_clear (&ews_store->priv->connection_lock);
4059 g_rec_mutex_clear (&ews_store->priv->update_lock);
4060
4061 /* Chain up to parent's finalize() method. */
4062 G_OBJECT_CLASS (camel_ews_store_parent_class)->finalize (object);
4063 }
4064
4065 static void
4066 camel_ews_store_class_init (CamelEwsStoreClass *class)
4067 {
4068 GObjectClass *object_class;
4069 CamelServiceClass *service_class;
4070 CamelStoreClass *store_class;
4071
4072 object_class = G_OBJECT_CLASS (class);
4073 object_class->set_property = ews_store_set_property;
4074 object_class->get_property = ews_store_get_property;
4075 object_class->dispose = ews_store_dispose;
4076 object_class->finalize = ews_store_finalize;
4077
4078 g_object_class_install_property (
4079 object_class,
4080 PROP_HAS_OOO_SET,
4081 g_param_spec_boolean (
4082 "has-ooo-set",
4083 "Has OOO Set",
4084 "Has Out of Office state set",
4085 FALSE,
4086 G_PARAM_READWRITE |
4087 G_PARAM_CONSTRUCT |
4088 G_PARAM_STATIC_STRINGS));
4089
4090 g_object_class_install_property (
4091 object_class,
4092 PROP_OOO_ALERT_STATE,
4093 g_param_spec_enum (
4094 "ooo-alert-state",
4095 "Out of Office Alert State",
4096 "The state of the Out of Office Alert",
4097 CAMEL_TYPE_EWS_STORE_OOO_ALERT_STATE,
4098 CAMEL_EWS_STORE_OOO_ALERT_STATE_UNKNOWN,
4099 G_PARAM_READWRITE |
4100 G_PARAM_CONSTRUCT |
4101 G_PARAM_STATIC_STRINGS));
4102
4103 /* Inherited from CamelNetworkService */
4104 g_object_class_override_property (
4105 object_class,
4106 PROP_CONNECTABLE,
4107 "connectable");
4108
4109 /* Inherited from CamelNetworkService */
4110 g_object_class_override_property (
4111 object_class,
4112 PROP_HOST_REACHABLE,
4113 "host-reachable");
4114
4115 service_class = CAMEL_SERVICE_CLASS (class);
4116 service_class->settings_type = CAMEL_TYPE_EWS_SETTINGS;
4117 service_class->query_auth_types_sync = ews_store_query_auth_types_sync;
4118 service_class->get_name = ews_get_name;
4119 service_class->connect_sync = ews_connect_sync;
4120 service_class->disconnect_sync = ews_disconnect_sync;
4121 service_class->authenticate_sync = ews_authenticate_sync;
4122
4123 store_class = CAMEL_STORE_CLASS (class);
4124 store_class->get_folder_sync = ews_get_folder_sync;
4125 store_class->create_folder_sync = ews_create_folder_sync;
4126 store_class->delete_folder_sync = ews_delete_folder_sync;
4127 store_class->rename_folder_sync = ews_rename_folder_sync;
4128 store_class->get_folder_info_sync = ews_get_folder_info_sync;
4129 store_class->initial_setup_sync = ews_initial_setup_sync;
4130
4131 store_class->get_trash_folder_sync = ews_get_trash_folder_sync;
4132 store_class->get_junk_folder_sync = ews_get_junk_folder_sync;
4133 store_class->can_refresh_folder = ews_can_refresh_folder;
4134 }
4135
4136 static void
4137 camel_ews_subscribable_init (CamelSubscribableInterface *iface)
4138 {
4139 iface->folder_is_subscribed = ews_store_folder_is_subscribed;
4140 iface->subscribe_folder_sync = ews_store_subscribe_folder_sync;
4141 iface->unsubscribe_folder_sync = ews_store_unsubscribe_folder_sync;
4142 }
4143
4144 static void
4145 camel_ews_store_init (CamelEwsStore *ews_store)
4146 {
4147 ews_store->priv = camel_ews_store_get_instance_private (ews_store);
4148
4149 ews_store->priv->last_refresh_time = time (NULL) - (FINFO_REFRESH_INTERVAL + 10);
4150 ews_store->priv->updates_cancellable = NULL;
4151 ews_store->priv->update_folder_names = NULL;
4152 ews_store->priv->subscription_key = 0;
4153 ews_store->priv->update_folder_id = 0;
4154 ews_store->priv->update_folder_list_id = 0;
4155 ews_store->priv->password_expires_in_days = -1;
4156 g_mutex_init (&ews_store->priv->get_finfo_lock);
4157 g_mutex_init (&ews_store->priv->connection_lock);
4158 g_rec_mutex_init (&ews_store->priv->update_lock);
4159 }