| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: (C) 2012 Red Hat, Inc. (www.redhat.com) |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "evolution-ews-config.h" |
| 7 |
|
| 8 |
#include "e-ews-backend.h" |
| 9 |
|
| 10 |
#include <glib/gi18n-lib.h> |
| 11 |
|
| 12 |
#include "common/e-ews-connection-utils.h" |
| 13 |
#include "common/e-source-ews-folder.h" |
| 14 |
#include "../Microsoft365/common/camel-m365-settings.h" |
| 15 |
|
| 16 |
#define EWS_HELPER_M365_RESOURCE_ID "helper-m365-calendar" |
| 17 |
|
| 18 |
typedef struct _SyncFoldersClosure SyncFoldersClosure; |
| 19 |
|
| 20 |
struct _EEwsBackendPrivate { |
| 21 |
gchar *deleted_items_folder_id; |
| 22 |
/* Folder ID -> ESource */ |
| 23 |
GHashTable *folders; |
| 24 |
GMutex folders_lock; |
| 25 |
|
| 26 |
ESource *gal_source; |
| 27 |
gchar *oal_selected; |
| 28 |
|
| 29 |
gchar *sync_state; |
| 30 |
GMutex sync_state_lock; |
| 31 |
|
| 32 |
ENamedParameters *credentials; |
| 33 |
EEwsConnection *connection; |
| 34 |
GMutex connection_lock; |
| 35 |
|
| 36 |
gboolean need_update_folders; |
| 37 |
|
| 38 |
gulong source_changed_id; |
| 39 |
}; |
| 40 |
|
| 41 |
struct _SyncFoldersClosure { |
| 42 |
EEwsBackend *backend; |
| 43 |
GSList *folders_created; |
| 44 |
GSList *folders_deleted; |
| 45 |
GSList *folders_updated; |
| 46 |
GHashTable *old_sources; /* gchar *folder_id ~> ESource * */ |
| 47 |
}; |
| 48 |
|
| 49 |
G_DEFINE_DYNAMIC_TYPE_EXTENDED (EEwsBackend, e_ews_backend, E_TYPE_COLLECTION_BACKEND, 0, |
| 50 |
G_ADD_PRIVATE_DYNAMIC (EEwsBackend)) |
| 51 |
|
| 52 |
static void |
| 53 |
sync_folders_closure_free (SyncFoldersClosure *closure) |
| 54 |
{ |
| 55 |
g_object_unref (closure->backend); |
| 56 |
|
| 57 |
/* List of EEwsFolder instances. */ |
| 58 |
g_slist_free_full (closure->folders_created, g_object_unref); |
| 59 |
|
| 60 |
/* List of folder ID strings. */ |
| 61 |
g_slist_free_full (closure->folders_deleted, g_free); |
| 62 |
|
| 63 |
/* List of EEwsFolder instances. */ |
| 64 |
g_slist_free_full (closure->folders_updated, g_object_unref); |
| 65 |
|
| 66 |
g_hash_table_destroy (closure->old_sources); |
| 67 |
|
| 68 |
g_slice_free (SyncFoldersClosure, closure); |
| 69 |
} |
| 70 |
|
| 71 |
static void |
| 72 |
ews_backend_folders_insert (EEwsBackend *backend, |
| 73 |
gchar *folder_id, /* assumes ownership */ |
| 74 |
ESource *source) |
| 75 |
{ |
| 76 |
g_return_if_fail (folder_id != NULL); |
| 77 |
g_return_if_fail (E_IS_SOURCE (source)); |
| 78 |
|
| 79 |
g_mutex_lock (&backend->priv->folders_lock); |
| 80 |
|
| 81 |
g_hash_table_insert ( |
| 82 |
backend->priv->folders, |
| 83 |
folder_id, |
| 84 |
g_object_ref (source)); |
| 85 |
|
| 86 |
g_mutex_unlock (&backend->priv->folders_lock); |
| 87 |
} |
| 88 |
|
| 89 |
static ESource * |
| 90 |
ews_backend_folders_lookup (EEwsBackend *backend, |
| 91 |
const gchar *folder_id) |
| 92 |
{ |
| 93 |
ESource *source; |
| 94 |
|
| 95 |
g_return_val_if_fail (folder_id != NULL, NULL); |
| 96 |
|
| 97 |
g_mutex_lock (&backend->priv->folders_lock); |
| 98 |
|
| 99 |
source = g_hash_table_lookup (backend->priv->folders, folder_id); |
| 100 |
|
| 101 |
if (source != NULL) |
| 102 |
g_object_ref (source); |
| 103 |
|
| 104 |
g_mutex_unlock (&backend->priv->folders_lock); |
| 105 |
|
| 106 |
return source; |
| 107 |
} |
| 108 |
|
| 109 |
static gboolean |
| 110 |
ews_backend_folders_remove (EEwsBackend *backend, |
| 111 |
const gchar *folder_id) |
| 112 |
{ |
| 113 |
gboolean removed; |
| 114 |
|
| 115 |
g_return_val_if_fail (folder_id != NULL, FALSE); |
| 116 |
|
| 117 |
g_mutex_lock (&backend->priv->folders_lock); |
| 118 |
|
| 119 |
removed = g_hash_table_remove (backend->priv->folders, folder_id); |
| 120 |
|
| 121 |
g_mutex_unlock (&backend->priv->folders_lock); |
| 122 |
|
| 123 |
return removed; |
| 124 |
} |
| 125 |
|
| 126 |
static CamelEwsSettings * |
| 127 |
ews_backend_get_settings (EEwsBackend *backend) |
| 128 |
{ |
| 129 |
ESource *source; |
| 130 |
ESourceCamel *extension; |
| 131 |
CamelSettings *settings; |
| 132 |
const gchar *extension_name; |
| 133 |
|
| 134 |
source = e_backend_get_source (E_BACKEND (backend)); |
| 135 |
extension_name = e_source_camel_get_extension_name ("ews"); |
| 136 |
extension = e_source_get_extension (source, extension_name); |
| 137 |
settings = e_source_camel_get_settings (extension); |
| 138 |
|
| 139 |
return CAMEL_EWS_SETTINGS (settings); |
| 140 |
} |
| 141 |
|
| 142 |
static void |
| 143 |
ews_backend_sync_authentication (EEwsBackend *ews_backend, |
| 144 |
ESource *child_source) |
| 145 |
{ |
| 146 |
ESourceAuthentication *coll_authentication_extension, *child_authentication_extension; |
| 147 |
ESource *collection_source; |
| 148 |
|
| 149 |
g_return_if_fail (E_IS_EWS_BACKEND (ews_backend)); |
| 150 |
g_return_if_fail (E_IS_SOURCE (child_source)); |
| 151 |
|
| 152 |
collection_source = e_backend_get_source (E_BACKEND (ews_backend)); |
| 153 |
|
| 154 |
coll_authentication_extension = e_source_get_extension (collection_source, E_SOURCE_EXTENSION_AUTHENTICATION); |
| 155 |
child_authentication_extension = e_source_get_extension (child_source, E_SOURCE_EXTENSION_AUTHENTICATION); |
| 156 |
|
| 157 |
e_source_authentication_set_host (child_authentication_extension, |
| 158 |
e_source_authentication_get_host (coll_authentication_extension)); |
| 159 |
|
| 160 |
e_source_authentication_set_user (child_authentication_extension, |
| 161 |
e_source_authentication_get_user (coll_authentication_extension)); |
| 162 |
|
| 163 |
e_source_authentication_set_method (child_authentication_extension, |
| 164 |
e_source_authentication_get_method (coll_authentication_extension)); |
| 165 |
} |
| 166 |
|
| 167 |
static ESource * |
| 168 |
ews_backend_new_child (EEwsBackend *backend, |
| 169 |
EEwsFolder *folder) |
| 170 |
{ |
| 171 |
ECollectionBackend *collection_backend; |
| 172 |
ESourceExtension *extension; |
| 173 |
ESource *source; |
| 174 |
const EwsFolderId *fid; |
| 175 |
const gchar *display_name; |
| 176 |
const gchar *extension_name; |
| 177 |
|
| 178 |
fid = e_ews_folder_get_id (folder); |
| 179 |
|
| 180 |
g_return_val_if_fail (fid != NULL, NULL); |
| 181 |
|
| 182 |
display_name = e_ews_folder_get_name (folder); |
| 183 |
|
| 184 |
/* Hide on-the-server hidden folders */ |
| 185 |
if (e_ews_folder_get_is_hidden (folder)) |
| 186 |
return NULL; |
| 187 |
|
| 188 |
collection_backend = E_COLLECTION_BACKEND (backend); |
| 189 |
source = e_collection_backend_new_child (collection_backend, fid->id); |
| 190 |
|
| 191 |
e_source_set_display_name (source, display_name); |
| 192 |
|
| 193 |
switch (e_ews_folder_get_folder_type (folder)) { |
| 194 |
case E_EWS_FOLDER_TYPE_CALENDAR: |
| 195 |
extension_name = E_SOURCE_EXTENSION_CALENDAR; |
| 196 |
break; |
| 197 |
case E_EWS_FOLDER_TYPE_TASKS: |
| 198 |
extension_name = E_SOURCE_EXTENSION_TASK_LIST; |
| 199 |
break; |
| 200 |
case E_EWS_FOLDER_TYPE_MEMOS: |
| 201 |
extension_name = E_SOURCE_EXTENSION_MEMO_LIST; |
| 202 |
break; |
| 203 |
case E_EWS_FOLDER_TYPE_CONTACTS: |
| 204 |
extension_name = E_SOURCE_EXTENSION_ADDRESS_BOOK; |
| 205 |
break; |
| 206 |
default: |
| 207 |
g_object_unref (source); |
| 208 |
g_return_val_if_reached (NULL); |
| 209 |
} |
| 210 |
extension = e_source_get_extension (source, extension_name); |
| 211 |
e_source_backend_set_backend_name ( |
| 212 |
E_SOURCE_BACKEND (extension), "ews"); |
| 213 |
ews_backend_sync_authentication (backend, source); |
| 214 |
|
| 215 |
if (e_ews_folder_get_folder_type (folder) != E_EWS_FOLDER_TYPE_CONTACTS && |
| 216 |
!e_source_has_extension (source, E_SOURCE_EXTENSION_EWS_FOLDER) && |
| 217 |
!e_source_has_extension (source, E_SOURCE_EXTENSION_ALARMS)) { |
| 218 |
/* a completely new ESource, do not notify with too old reminders */ |
| 219 |
ESourceAlarms *alarms; |
| 220 |
gchar *today; |
| 221 |
GTimeVal today_tv; |
| 222 |
GDate dt; |
| 223 |
|
| 224 |
g_date_clear (&dt, 1); |
| 225 |
g_get_current_time (&today_tv); |
| 226 |
g_date_set_time_val (&dt, &today_tv); |
| 227 |
|
| 228 |
/* midnight UTC */ |
| 229 |
today = g_strdup_printf ("%04d-%02d-%02dT00:00:00Z", g_date_get_year (&dt), g_date_get_month (&dt), g_date_get_day (&dt)); |
| 230 |
|
| 231 |
alarms = e_source_get_extension (source, E_SOURCE_EXTENSION_ALARMS); |
| 232 |
e_source_alarms_set_last_notified (alarms, today); |
| 233 |
|
| 234 |
g_free (today); |
| 235 |
} |
| 236 |
|
| 237 |
extension_name = E_SOURCE_EXTENSION_EWS_FOLDER; |
| 238 |
extension = e_source_get_extension (source, extension_name); |
| 239 |
e_source_ews_folder_set_id ( |
| 240 |
E_SOURCE_EWS_FOLDER (extension), fid->id); |
| 241 |
e_source_ews_folder_set_change_key ( |
| 242 |
E_SOURCE_EWS_FOLDER (extension), fid->change_key); |
| 243 |
e_source_ews_folder_set_name (E_SOURCE_EWS_FOLDER (extension), e_ews_folder_get_name (folder)); |
| 244 |
|
| 245 |
extension_name = E_SOURCE_EXTENSION_OFFLINE; |
| 246 |
extension = e_source_get_extension (source, extension_name); |
| 247 |
e_source_offline_set_stay_synchronized ( |
| 248 |
E_SOURCE_OFFLINE (extension), TRUE); |
| 249 |
|
| 250 |
e_server_side_source_set_remote_deletable ( |
| 251 |
E_SERVER_SIDE_SOURCE (source), TRUE); |
| 252 |
|
| 253 |
return source; |
| 254 |
} |
| 255 |
|
| 256 |
static ESource * |
| 257 |
ews_backend_new_calendar (EEwsBackend *backend, |
| 258 |
EEwsFolder *folder) |
| 259 |
{ |
| 260 |
/* No extra configuration to do. */ |
| 261 |
return ews_backend_new_child (backend, folder); |
| 262 |
} |
| 263 |
|
| 264 |
static ESource * |
| 265 |
ews_backend_new_task_list (EEwsBackend *backend, |
| 266 |
EEwsFolder *folder) |
| 267 |
{ |
| 268 |
/* No extra configuration to do. */ |
| 269 |
return ews_backend_new_child (backend, folder); |
| 270 |
} |
| 271 |
|
| 272 |
static ESource * |
| 273 |
ews_backend_new_memo_list (EEwsBackend *backend, |
| 274 |
EEwsFolder *folder) |
| 275 |
{ |
| 276 |
/* No extra configuration to do. */ |
| 277 |
return ews_backend_new_child (backend, folder); |
| 278 |
} |
| 279 |
|
| 280 |
static ESource * |
| 281 |
ews_backend_new_address_book (EEwsBackend *backend, |
| 282 |
EEwsFolder *folder) |
| 283 |
{ |
| 284 |
/* No extra configuration to do. */ |
| 285 |
return ews_backend_new_child (backend, folder); |
| 286 |
} |
| 287 |
|
| 288 |
static void |
| 289 |
ews_backend_update_folder_name (ESource *source, |
| 290 |
EEwsFolder *folder) |
| 291 |
{ |
| 292 |
if (folder && source && e_source_has_extension (source, E_SOURCE_EXTENSION_EWS_FOLDER)) { |
| 293 |
ESourceEwsFolder *folder_extension; |
| 294 |
|
| 295 |
folder_extension = e_source_get_extension (source, E_SOURCE_EXTENSION_EWS_FOLDER); |
| 296 |
|
| 297 |
/* The user did not change the folder name (old sources have stored NULL), but it changed on the server */ |
| 298 |
if ((!e_source_ews_folder_get_name (folder_extension) || |
| 299 |
g_strcmp0 (e_source_ews_folder_get_name (folder_extension), e_source_get_display_name (source)) == 0) && |
| 300 |
g_strcmp0 (e_source_get_display_name (source), e_ews_folder_get_name (folder)) != 0) { |
| 301 |
e_source_set_display_name (source, e_ews_folder_get_name (folder)); |
| 302 |
} |
| 303 |
|
| 304 |
e_source_ews_folder_set_name (folder_extension, e_ews_folder_get_name (folder)); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
static void |
| 309 |
ews_backend_sync_created_folders (EEwsBackend *backend, |
| 310 |
GSList *list, |
| 311 |
GHashTable *old_sources) |
| 312 |
{ |
| 313 |
ECollectionBackend *collection_backend; |
| 314 |
ESourceRegistryServer *server; |
| 315 |
GSList *link; |
| 316 |
|
| 317 |
collection_backend = E_COLLECTION_BACKEND (backend); |
| 318 |
server = e_collection_backend_ref_server (collection_backend); |
| 319 |
|
| 320 |
for (link = list; link != NULL; link = g_slist_next (link)) { |
| 321 |
EEwsFolder *folder = E_EWS_FOLDER (link->data); |
| 322 |
const EwsFolderId *fid, *parent_fid; |
| 323 |
ESource *source; |
| 324 |
|
| 325 |
fid = e_ews_folder_get_id (folder); |
| 326 |
if (!fid || !fid->id) |
| 327 |
continue; |
| 328 |
|
| 329 |
/* Skip those under 'Deleted Items' */ |
| 330 |
parent_fid = e_ews_folder_get_parent_id (folder); |
| 331 |
if (parent_fid && parent_fid->id && g_strcmp0 (parent_fid->id, backend->priv->deleted_items_folder_id) == 0) |
| 332 |
continue; |
| 333 |
|
| 334 |
source = ews_backend_folders_lookup (backend, fid->id); |
| 335 |
|
| 336 |
/* If we already know about this folder, skip it. */ |
| 337 |
if (source) { |
| 338 |
ews_backend_update_folder_name (source, folder); |
| 339 |
g_hash_table_remove (old_sources, fid->id); |
| 340 |
g_clear_object (&source); |
| 341 |
continue; |
| 342 |
} |
| 343 |
|
| 344 |
switch (e_ews_folder_get_folder_type (folder)) { |
| 345 |
case E_EWS_FOLDER_TYPE_CALENDAR: |
| 346 |
source = ews_backend_new_calendar ( |
| 347 |
backend, folder); |
| 348 |
break; |
| 349 |
case E_EWS_FOLDER_TYPE_TASKS: |
| 350 |
source = ews_backend_new_task_list ( |
| 351 |
backend, folder); |
| 352 |
break; |
| 353 |
case E_EWS_FOLDER_TYPE_MEMOS: |
| 354 |
source = ews_backend_new_memo_list ( |
| 355 |
backend, folder); |
| 356 |
break; |
| 357 |
case E_EWS_FOLDER_TYPE_CONTACTS: |
| 358 |
source = ews_backend_new_address_book ( |
| 359 |
backend, folder); |
| 360 |
break; |
| 361 |
default: |
| 362 |
break; |
| 363 |
} |
| 364 |
|
| 365 |
if (source != NULL) { |
| 366 |
e_source_registry_server_add_source (server, source); |
| 367 |
|
| 368 |
g_hash_table_remove (old_sources, fid->id); |
| 369 |
|
| 370 |
g_object_unref (source); |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
g_object_unref (server); |
| 375 |
} |
| 376 |
|
| 377 |
static void |
| 378 |
ews_backend_delete_folder (EEwsBackend *backend, |
| 379 |
const gchar *folder_id, |
| 380 |
GHashTable *old_sources) |
| 381 |
{ |
| 382 |
ESource *source = NULL; |
| 383 |
|
| 384 |
if (folder_id) { |
| 385 |
source = ews_backend_folders_lookup (backend, folder_id); |
| 386 |
g_hash_table_remove (old_sources, folder_id); |
| 387 |
} |
| 388 |
|
| 389 |
if (source) { |
| 390 |
/* This will trigger a "child-removed" signal and |
| 391 |
* our handler will remove the hash table entry. */ |
| 392 |
e_source_remove_sync (source, NULL, NULL); |
| 393 |
|
| 394 |
g_object_unref (source); |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
static void |
| 399 |
ews_backend_sync_deleted_folders (EEwsBackend *backend, |
| 400 |
GSList *list, |
| 401 |
GHashTable *old_sources) |
| 402 |
{ |
| 403 |
GSList *link; |
| 404 |
|
| 405 |
for (link = list; link != NULL; link = g_slist_next (link)) { |
| 406 |
const gchar *folder_id = link->data; |
| 407 |
|
| 408 |
ews_backend_delete_folder (backend, folder_id, old_sources); |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
static void |
| 413 |
ews_backend_sync_updated_folders (EEwsBackend *backend, |
| 414 |
GSList *list, |
| 415 |
GHashTable *old_sources) |
| 416 |
{ |
| 417 |
GSList *link; |
| 418 |
|
| 419 |
for (link = list; link != NULL; link = g_slist_next (link)) { |
| 420 |
EEwsFolder *folder = link->data; |
| 421 |
const EwsFolderId *id, *parent_id; |
| 422 |
|
| 423 |
if (!folder) |
| 424 |
continue; |
| 425 |
|
| 426 |
id = e_ews_folder_get_id (folder); |
| 427 |
parent_id = e_ews_folder_get_parent_id (folder); |
| 428 |
|
| 429 |
if (id && parent_id) { |
| 430 |
/* Deleted calendars are under 'Deleted Items', even they are not visible in the OWA */ |
| 431 |
if (backend->priv->deleted_items_folder_id && |
| 432 |
g_strcmp0 (parent_id->id, backend->priv->deleted_items_folder_id) == 0) { |
| 433 |
ews_backend_delete_folder (backend, id->id, old_sources); |
| 434 |
} else { |
| 435 |
ESource *source; |
| 436 |
|
| 437 |
source = ews_backend_folders_lookup (backend, id->id); |
| 438 |
|
| 439 |
if (source) { |
| 440 |
ews_backend_update_folder_name (source, folder); |
| 441 |
g_object_unref (source); |
| 442 |
} |
| 443 |
} |
| 444 |
} |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
static void |
| 449 |
ews_backend_add_gal_source (EEwsBackend *backend) |
| 450 |
{ |
| 451 |
ECollectionBackend *collection_backend; |
| 452 |
ESourceAutocomplete *autocomplete_extension; |
| 453 |
ESourceBackend *backend_extension; |
| 454 |
ESourceEwsFolder *folder_extension; |
| 455 |
ESourceOffline *offline_extension; |
| 456 |
ESourceRegistryServer *server; |
| 457 |
ESource *source = NULL; |
| 458 |
CamelEwsSettings *settings; |
| 459 |
const gchar *display_name; |
| 460 |
const gchar *extension_name; |
| 461 |
const gchar *gal_uid; |
| 462 |
const gchar *oal_id = NULL; |
| 463 |
const gchar *uid; |
| 464 |
gchar *oal_selected; |
| 465 |
|
| 466 |
settings = ews_backend_get_settings (backend); |
| 467 |
collection_backend = E_COLLECTION_BACKEND (backend); |
| 468 |
source = e_backend_get_source (E_BACKEND (backend)); |
| 469 |
gal_uid = camel_ews_settings_get_gal_uid (settings); |
| 470 |
|
| 471 |
if (gal_uid != NULL) { |
| 472 |
server = e_collection_backend_ref_server (collection_backend); |
| 473 |
source = e_source_registry_server_ref_source (server, gal_uid); |
| 474 |
g_object_unref (server); |
| 475 |
|
| 476 |
if (source != NULL) { |
| 477 |
g_object_unref (source); |
| 478 |
return; |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
oal_selected = camel_ews_settings_dup_oal_selected (settings); |
| 483 |
|
| 484 |
/* This is supposed to be in the form: ID ':' NAME */ |
| 485 |
if (oal_selected != NULL) { |
| 486 |
gchar *cp = strrchr (oal_selected, ':'); |
| 487 |
if (cp != NULL) { |
| 488 |
/* skip ':' and any leading backslash in the display name */ |
| 489 |
cp++; |
| 490 |
while (*cp && *cp == '\\') |
| 491 |
cp++; |
| 492 |
|
| 493 |
display_name = cp; |
| 494 |
oal_id = oal_selected; |
| 495 |
} else { |
| 496 |
g_free (oal_selected); |
| 497 |
oal_selected = NULL; |
| 498 |
|
| 499 |
camel_ews_settings_set_oal_selected (settings, NULL); |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
if (oal_selected == NULL) { |
| 504 |
display_name = _("Global Address List"); |
| 505 |
oal_id = "global-address-list"; |
| 506 |
} |
| 507 |
|
| 508 |
g_free (backend->priv->oal_selected); |
| 509 |
backend->priv->oal_selected = oal_selected; /* takes ownership */ |
| 510 |
|
| 511 |
source = e_collection_backend_new_child ( |
| 512 |
collection_backend, oal_id); |
| 513 |
ews_backend_sync_authentication (backend, source); |
| 514 |
|
| 515 |
e_source_set_display_name (source, display_name); |
| 516 |
|
| 517 |
/* do not re-setup previously saved ESource, |
| 518 |
* that would rewrite user's choice */ |
| 519 |
if (!e_source_has_extension (source, E_SOURCE_EXTENSION_EWS_FOLDER)) { |
| 520 |
extension_name = E_SOURCE_EXTENSION_ADDRESS_BOOK; |
| 521 |
backend_extension = e_source_get_extension (source, extension_name); |
| 522 |
e_source_backend_set_backend_name (backend_extension, "ews"); |
| 523 |
|
| 524 |
extension_name = E_SOURCE_EXTENSION_AUTOCOMPLETE; |
| 525 |
autocomplete_extension = e_source_get_extension (source, extension_name); |
| 526 |
e_source_autocomplete_set_include_me (autocomplete_extension, TRUE); |
| 527 |
|
| 528 |
extension_name = E_SOURCE_EXTENSION_EWS_FOLDER; |
| 529 |
folder_extension = e_source_get_extension (source, extension_name); |
| 530 |
e_source_ews_folder_set_id (folder_extension, oal_id); |
| 531 |
e_source_ews_folder_set_name (folder_extension, display_name); |
| 532 |
|
| 533 |
extension_name = E_SOURCE_EXTENSION_OFFLINE; |
| 534 |
offline_extension = e_source_get_extension (source, extension_name); |
| 535 |
e_source_offline_set_stay_synchronized (offline_extension, TRUE); |
| 536 |
} |
| 537 |
|
| 538 |
server = e_collection_backend_ref_server (collection_backend); |
| 539 |
e_source_registry_server_add_source (server, source); |
| 540 |
g_object_unref (server); |
| 541 |
|
| 542 |
uid = e_source_get_uid (source); |
| 543 |
camel_ews_settings_set_gal_uid (settings, uid); |
| 544 |
|
| 545 |
g_object_unref (source); |
| 546 |
} |
| 547 |
|
| 548 |
/* This source is used by the Calendar backend, when creating online meeting */ |
| 549 |
static void |
| 550 |
ews_backend_maybe_add_m365_source (EEwsBackend *ews_backend) |
| 551 |
{ |
| 552 |
CamelEwsSettings *ews_settings; |
| 553 |
ECollectionBackend *collection_backend; |
| 554 |
ESource *collection_source; |
| 555 |
ESource *m365_source; |
| 556 |
ESourceAuthentication *auth_extension; |
| 557 |
ESourceAuthentication *collection_auth_extension; |
| 558 |
ESourceExtension *source_extension; |
| 559 |
ESourceRegistryServer *server; |
| 560 |
gchar *display_name; |
| 561 |
|
| 562 |
ews_settings = ews_backend_get_settings (ews_backend); |
| 563 |
|
| 564 |
if (camel_ews_settings_get_auth_mechanism (ews_settings) != EWS_AUTH_TYPE_OAUTH2) |
| 565 |
return; |
| 566 |
|
| 567 |
collection_source = e_backend_get_source (E_BACKEND (ews_backend)); |
| 568 |
|
| 569 |
if (!collection_source) |
| 570 |
return; |
| 571 |
|
| 572 |
/* Make sure the ESourceCamel knows about it, even when no Microsoft365 mail account is created */ |
| 573 |
e_source_camel_generate_subtype ("microsoft365", CAMEL_TYPE_M365_SETTINGS); |
| 574 |
|
| 575 |
collection_backend = E_COLLECTION_BACKEND (ews_backend); |
| 576 |
m365_source = e_collection_backend_new_child (collection_backend, EWS_HELPER_M365_RESOURCE_ID); |
| 577 |
|
| 578 |
display_name = g_strconcat (e_source_get_display_name (collection_source), " (Microsoft365)", NULL); |
| 579 |
|
| 580 |
source_extension = e_source_get_extension (m365_source, e_source_camel_get_extension_name ("microsoft365")); |
| 581 |
if (source_extension) { |
| 582 |
CamelSettings *settings = e_source_camel_get_settings (E_SOURCE_CAMEL (source_extension)); |
| 583 |
|
| 584 |
if (settings) { |
| 585 |
g_object_set (settings, |
| 586 |
"host", "graph.microsoft.com", |
| 587 |
"auth-mechanism", "Microsoft365", |
| 588 |
NULL); |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
collection_auth_extension = e_source_get_extension (collection_source, E_SOURCE_EXTENSION_AUTHENTICATION); |
| 593 |
|
| 594 |
e_source_set_display_name (m365_source, display_name); |
| 595 |
|
| 596 |
auth_extension = e_source_get_extension (m365_source, E_SOURCE_EXTENSION_AUTHENTICATION); |
| 597 |
e_source_authentication_set_host (auth_extension, "graph.microsoft.com"); |
| 598 |
e_source_authentication_set_method (auth_extension, "Microsoft365"); |
| 599 |
e_source_authentication_set_user (auth_extension, |
| 600 |
e_source_authentication_get_user (collection_auth_extension)); |
| 601 |
|
| 602 |
server = e_collection_backend_ref_server (collection_backend); |
| 603 |
e_source_registry_server_add_source (server, m365_source); |
| 604 |
|
| 605 |
g_object_unref (m365_source); |
| 606 |
g_object_unref (server); |
| 607 |
g_free (display_name); |
| 608 |
} |
| 609 |
|
| 610 |
static void ews_backend_populate (ECollectionBackend *backend); |
| 611 |
|
| 612 |
static void |
| 613 |
ews_backend_source_changed_cb (ESource *source, |
| 614 |
EEwsBackend *backend) |
| 615 |
{ |
| 616 |
if (!e_collection_backend_get_part_enabled (E_COLLECTION_BACKEND (backend), E_COLLECTION_BACKEND_PART_ANY)) { |
| 617 |
backend->priv->need_update_folders = TRUE; |
| 618 |
return; |
| 619 |
} |
| 620 |
|
| 621 |
if (!backend->priv->need_update_folders) |
| 622 |
return; |
| 623 |
|
| 624 |
ews_backend_populate (E_COLLECTION_BACKEND (backend)); |
| 625 |
} |
| 626 |
|
| 627 |
static void |
| 628 |
add_remote_sources (EEwsBackend *backend, |
| 629 |
GHashTable *old_sources) |
| 630 |
{ |
| 631 |
GHashTableIter iter; |
| 632 |
ESourceRegistryServer *registry; |
| 633 |
const gchar *extension_name; |
| 634 |
gpointer value; |
| 635 |
|
| 636 |
registry = e_collection_backend_ref_server ( |
| 637 |
E_COLLECTION_BACKEND (backend)); |
| 638 |
|
| 639 |
extension_name = E_SOURCE_EXTENSION_EWS_FOLDER; |
| 640 |
|
| 641 |
g_hash_table_iter_init (&iter, old_sources); |
| 642 |
while (g_hash_table_iter_next (&iter, NULL, &value)) { |
| 643 |
ESource *source = value; |
| 644 |
ESourceEwsFolder *extension; |
| 645 |
|
| 646 |
if (!e_source_has_extension (source, extension_name)) |
| 647 |
continue; |
| 648 |
|
| 649 |
/* foreign or public folders are just added */ |
| 650 |
extension = e_source_get_extension (source, extension_name); |
| 651 |
if (e_source_ews_folder_get_foreign (extension) || |
| 652 |
e_source_ews_folder_get_public (extension)) { |
| 653 |
e_server_side_source_set_writable ( |
| 654 |
E_SERVER_SIDE_SOURCE (source), TRUE); |
| 655 |
e_server_side_source_set_remote_deletable ( |
| 656 |
E_SERVER_SIDE_SOURCE (source), TRUE); |
| 657 |
e_source_registry_server_add_source (registry, source); |
| 658 |
} else { |
| 659 |
GError *error = NULL; |
| 660 |
|
| 661 |
if (!e_source_remove_sync (source, NULL, &error)) |
| 662 |
g_warning ("%s: Failed to remove old EWS source '%s': %s", G_STRFUNC, e_source_get_uid (source), |
| 663 |
error ? error->message : "Unknown error"); |
| 664 |
|
| 665 |
g_clear_error (&error); |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
g_object_unref (registry); |
| 670 |
} |
| 671 |
|
| 672 |
static gboolean |
| 673 |
ews_backend_sync_folders_idle_cb (gpointer user_data) |
| 674 |
{ |
| 675 |
SyncFoldersClosure *closure = user_data; |
| 676 |
|
| 677 |
ews_backend_sync_deleted_folders (closure->backend, closure->folders_deleted, closure->old_sources); |
| 678 |
ews_backend_sync_updated_folders (closure->backend, closure->folders_updated, closure->old_sources); |
| 679 |
ews_backend_sync_created_folders (closure->backend, closure->folders_created, closure->old_sources); |
| 680 |
|
| 681 |
add_remote_sources (closure->backend, closure->old_sources); |
| 682 |
|
| 683 |
return FALSE; |
| 684 |
} |
| 685 |
|
| 686 |
static void |
| 687 |
ews_backend_dispose (GObject *object) |
| 688 |
{ |
| 689 |
EEwsBackend *ews_backend = E_EWS_BACKEND (object); |
| 690 |
ESource *source; |
| 691 |
|
| 692 |
source = e_backend_get_source (E_BACKEND (object)); |
| 693 |
if (source && ews_backend->priv->source_changed_id) { |
| 694 |
g_signal_handler_disconnect (source, ews_backend->priv->source_changed_id); |
| 695 |
ews_backend->priv->source_changed_id = 0; |
| 696 |
} |
| 697 |
|
| 698 |
g_hash_table_remove_all (ews_backend->priv->folders); |
| 699 |
|
| 700 |
g_mutex_lock (&ews_backend->priv->connection_lock); |
| 701 |
g_clear_object (&ews_backend->priv->connection); |
| 702 |
g_mutex_unlock (&ews_backend->priv->connection_lock); |
| 703 |
|
| 704 |
/* Chain up to parent's dispose() method. */ |
| 705 |
G_OBJECT_CLASS (e_ews_backend_parent_class)->dispose (object); |
| 706 |
} |
| 707 |
|
| 708 |
static void |
| 709 |
ews_backend_finalize (GObject *object) |
| 710 |
{ |
| 711 |
EEwsBackend *ews_backend = E_EWS_BACKEND (object); |
| 712 |
|
| 713 |
g_free (ews_backend->priv->deleted_items_folder_id); |
| 714 |
g_hash_table_destroy (ews_backend->priv->folders); |
| 715 |
g_mutex_clear (&ews_backend->priv->folders_lock); |
| 716 |
g_free (ews_backend->priv->oal_selected); |
| 717 |
g_free (ews_backend->priv->sync_state); |
| 718 |
g_mutex_clear (&ews_backend->priv->sync_state_lock); |
| 719 |
g_mutex_clear (&ews_backend->priv->connection_lock); |
| 720 |
e_named_parameters_free (ews_backend->priv->credentials); |
| 721 |
|
| 722 |
/* Chain up to parent's finalize() method. */ |
| 723 |
G_OBJECT_CLASS (e_ews_backend_parent_class)->finalize (object); |
| 724 |
} |
| 725 |
|
| 726 |
static void |
| 727 |
ews_backend_constructed (GObject *object) |
| 728 |
{ |
| 729 |
EBackend *backend; |
| 730 |
ESource *source; |
| 731 |
ESourceAuthentication *auth_extension; |
| 732 |
CamelEwsSettings *settings; |
| 733 |
const gchar *extension_name; |
| 734 |
gchar *host = NULL; |
| 735 |
guint16 port = 0; |
| 736 |
gboolean changed = FALSE; |
| 737 |
|
| 738 |
/* Chain up to parent's constructed() method. */ |
| 739 |
G_OBJECT_CLASS (e_ews_backend_parent_class)->constructed (object); |
| 740 |
|
| 741 |
backend = E_BACKEND (object); |
| 742 |
source = e_backend_get_source (backend); |
| 743 |
|
| 744 |
/* XXX Wondering if we ought to delay this until after folders |
| 745 |
* are initially populated, just to remove the possibility |
| 746 |
* of weird races with clients trying to create folders. */ |
| 747 |
e_server_side_source_set_remote_creatable ( |
| 748 |
E_SERVER_SIDE_SOURCE (source), TRUE); |
| 749 |
|
| 750 |
/* Setup the Authentication extension so |
| 751 |
* Camel can determine host reachability. */ |
| 752 |
extension_name = E_SOURCE_EXTENSION_AUTHENTICATION; |
| 753 |
auth_extension = e_source_get_extension (source, extension_name); |
| 754 |
|
| 755 |
if (e_backend_get_destination_address (backend, &host, &port)) { |
| 756 |
e_source_authentication_set_host (auth_extension, host); |
| 757 |
e_source_authentication_set_port (auth_extension, port); |
| 758 |
} |
| 759 |
|
| 760 |
g_free (host); |
| 761 |
|
| 762 |
settings = ews_backend_get_settings (E_EWS_BACKEND (backend)); |
| 763 |
|
| 764 |
/* NTLM is a fallback, for any unknown value, but ESoupSession requires it |
| 765 |
explicitly set, to use it for authentication, thus make sure it is set */ |
| 766 |
if (camel_ews_settings_get_auth_mechanism (settings) == EWS_AUTH_TYPE_NTLM && |
| 767 |
g_strcmp0 (e_source_authentication_get_method (auth_extension), "NTLM") != 0) { |
| 768 |
e_source_authentication_set_method (auth_extension, "NTLM"); |
| 769 |
changed = TRUE; |
| 770 |
} |
| 771 |
|
| 772 |
/* Reset the connectable, it steals data from Authentication extension, |
| 773 |
where is written incorrect address */ |
| 774 |
e_backend_set_connectable (backend, NULL); |
| 775 |
|
| 776 |
/* Eventually unset temporary SSL trust, but only once, when the process started. |
| 777 |
It might bee too often anywhere lease (like in the authenticate callback) */ |
| 778 |
if (e_source_has_extension (source, E_SOURCE_EXTENSION_WEBDAV_BACKEND)) { |
| 779 |
ESourceWebdav *webdav_extension; |
| 780 |
|
| 781 |
webdav_extension = e_source_get_extension (source, E_SOURCE_EXTENSION_WEBDAV_BACKEND); |
| 782 |
e_source_webdav_unset_temporary_ssl_trust (webdav_extension); |
| 783 |
} |
| 784 |
|
| 785 |
if (e_source_has_extension (source, E_SOURCE_EXTENSION_COLLECTION)) { |
| 786 |
ESourceCollection *collection_extension; |
| 787 |
|
| 788 |
collection_extension = e_source_get_extension (source, E_SOURCE_EXTENSION_COLLECTION); |
| 789 |
e_source_collection_set_allow_sources_rename (collection_extension, TRUE); |
| 790 |
} |
| 791 |
|
| 792 |
/* Make sure the auth method change is saved, maybe with temporary SSL trust unset too */ |
| 793 |
if (changed) |
| 794 |
e_source_write (source, NULL, NULL, NULL); |
| 795 |
} |
| 796 |
|
| 797 |
static void |
| 798 |
ews_backend_claim_old_resources (ECollectionBackend *backend) |
| 799 |
{ |
| 800 |
ESourceRegistryServer *registry; |
| 801 |
GList *old_resources, *iter; |
| 802 |
|
| 803 |
g_return_if_fail (E_IS_COLLECTION_BACKEND (backend)); |
| 804 |
|
| 805 |
registry = e_collection_backend_ref_server (backend); |
| 806 |
old_resources = e_collection_backend_claim_all_resources (backend); |
| 807 |
|
| 808 |
for (iter = old_resources; iter; iter = g_list_next (iter)) { |
| 809 |
ESource *source = iter->data; |
| 810 |
|
| 811 |
e_source_registry_server_add_source (registry, source); |
| 812 |
} |
| 813 |
|
| 814 |
g_list_free_full (old_resources, g_object_unref); |
| 815 |
g_clear_object (®istry); |
| 816 |
} |
| 817 |
|
| 818 |
static void |
| 819 |
ews_backend_folders_synced_cb (GObject *source, |
| 820 |
GAsyncResult *result, |
| 821 |
gpointer user_data) |
| 822 |
{ |
| 823 |
EEwsBackend *ews_backend; |
| 824 |
|
| 825 |
g_return_if_fail (E_IS_EWS_BACKEND (source)); |
| 826 |
|
| 827 |
ews_backend = E_EWS_BACKEND (source); |
| 828 |
|
| 829 |
if (!e_ews_backend_sync_folders_finish (ews_backend, result, NULL)) |
| 830 |
ews_backend_claim_old_resources (E_COLLECTION_BACKEND (ews_backend)); |
| 831 |
|
| 832 |
e_collection_backend_thaw_populate (E_COLLECTION_BACKEND (ews_backend)); |
| 833 |
} |
| 834 |
|
| 835 |
static void |
| 836 |
ews_backend_populate (ECollectionBackend *collection_backend) |
| 837 |
{ |
| 838 |
ESource *source; |
| 839 |
EEwsBackend *ews_backend = E_EWS_BACKEND (collection_backend); |
| 840 |
EBackend *backend = E_BACKEND (ews_backend); |
| 841 |
|
| 842 |
source = e_backend_get_source (backend); |
| 843 |
|
| 844 |
ews_backend->priv->need_update_folders = TRUE; |
| 845 |
|
| 846 |
if (!ews_backend->priv->source_changed_id) { |
| 847 |
ews_backend->priv->source_changed_id = g_signal_connect ( |
| 848 |
source, "changed", |
| 849 |
G_CALLBACK (ews_backend_source_changed_cb), ews_backend); |
| 850 |
} |
| 851 |
|
| 852 |
/* only claim old sources, when the account is disabled, thus |
| 853 |
those sources can be auto-enabled when the account is enabled */ |
| 854 |
if (!e_collection_backend_get_part_enabled (collection_backend, E_COLLECTION_BACKEND_PART_ANY)) { |
| 855 |
ews_backend_claim_old_resources (collection_backend); |
| 856 |
return; |
| 857 |
} |
| 858 |
|
| 859 |
if (!e_collection_backend_freeze_populate (collection_backend)) { |
| 860 |
e_collection_backend_thaw_populate (collection_backend); |
| 861 |
return; |
| 862 |
} |
| 863 |
|
| 864 |
ews_backend_claim_old_resources (collection_backend); |
| 865 |
ews_backend_add_gal_source (ews_backend); |
| 866 |
ews_backend_maybe_add_m365_source (ews_backend); |
| 867 |
|
| 868 |
if (e_backend_get_online (backend)) { |
| 869 |
CamelEwsSettings *ews_settings; |
| 870 |
|
| 871 |
ews_settings = ews_backend_get_settings (ews_backend); |
| 872 |
|
| 873 |
if (e_ews_connection_utils_get_without_password (ews_settings)) { |
| 874 |
e_backend_schedule_authenticate (backend, NULL); |
| 875 |
} else { |
| 876 |
e_backend_credentials_required_sync (backend, |
| 877 |
E_SOURCE_CREDENTIALS_REASON_REQUIRED, NULL, 0, NULL, |
| 878 |
NULL, NULL); |
| 879 |
} |
| 880 |
} |
| 881 |
|
| 882 |
e_collection_backend_thaw_populate (collection_backend); |
| 883 |
} |
| 884 |
|
| 885 |
static gchar * |
| 886 |
ews_backend_dup_resource_id (ECollectionBackend *backend, |
| 887 |
ESource *child_source) |
| 888 |
{ |
| 889 |
ESourceEwsFolder *extension; |
| 890 |
const gchar *extension_name; |
| 891 |
|
| 892 |
if (e_source_has_extension (child_source, E_SOURCE_EXTENSION_AUTHENTICATION)) { |
| 893 |
ESourceAuthentication *auth_extension; |
| 894 |
|
| 895 |
auth_extension = e_source_get_extension (child_source, E_SOURCE_EXTENSION_AUTHENTICATION); |
| 896 |
|
| 897 |
if (g_strcmp0 (e_source_authentication_get_method (auth_extension), "Microsoft365") == 0) |
| 898 |
return g_strdup (EWS_HELPER_M365_RESOURCE_ID); |
| 899 |
} |
| 900 |
|
| 901 |
extension_name = E_SOURCE_EXTENSION_EWS_FOLDER; |
| 902 |
extension = e_source_get_extension (child_source, extension_name); |
| 903 |
|
| 904 |
return e_source_ews_folder_dup_id (extension); |
| 905 |
} |
| 906 |
|
| 907 |
static void |
| 908 |
ews_backend_child_added (ECollectionBackend *backend, |
| 909 |
ESource *child_source) |
| 910 |
{ |
| 911 |
ESource *collection_source; |
| 912 |
const gchar *extension_name; |
| 913 |
|
| 914 |
collection_source = e_backend_get_source (E_BACKEND (backend)); |
| 915 |
|
| 916 |
extension_name = E_SOURCE_EXTENSION_AUTHENTICATION; |
| 917 |
if (e_source_has_extension (child_source, extension_name)) { |
| 918 |
ESourceAuthentication *auth_child_extension; |
| 919 |
|
| 920 |
auth_child_extension = e_source_get_extension (child_source, extension_name); |
| 921 |
|
| 922 |
if (g_strcmp0 (e_source_authentication_get_method (auth_child_extension), "Microsoft365") != 0) { |
| 923 |
ESourceAuthentication *auth_collection_extension; |
| 924 |
|
| 925 |
auth_collection_extension = e_source_get_extension (collection_source, extension_name); |
| 926 |
|
| 927 |
e_binding_bind_property ( |
| 928 |
auth_collection_extension, "host", |
| 929 |
auth_child_extension, "host", |
| 930 |
G_BINDING_SYNC_CREATE); |
| 931 |
|
| 932 |
e_binding_bind_property ( |
| 933 |
auth_collection_extension, "user", |
| 934 |
auth_child_extension, "user", |
| 935 |
G_BINDING_SYNC_CREATE); |
| 936 |
|
| 937 |
e_binding_bind_property ( |
| 938 |
auth_collection_extension, "method", |
| 939 |
auth_child_extension, "method", |
| 940 |
G_BINDING_SYNC_CREATE); |
| 941 |
} |
| 942 |
} |
| 943 |
|
| 944 |
/* We track EWS folders in a hash table by folder ID. */ |
| 945 |
extension_name = E_SOURCE_EXTENSION_EWS_FOLDER; |
| 946 |
if (e_source_has_extension (child_source, extension_name)) { |
| 947 |
ESourceEwsFolder *extension; |
| 948 |
gchar *folder_id; |
| 949 |
|
| 950 |
extension = e_source_get_extension ( |
| 951 |
child_source, extension_name); |
| 952 |
folder_id = e_source_ews_folder_dup_id (extension); |
| 953 |
if (folder_id != NULL) { |
| 954 |
ews_backend_folders_insert ( |
| 955 |
E_EWS_BACKEND (backend), |
| 956 |
folder_id, child_source); |
| 957 |
} |
| 958 |
} |
| 959 |
|
| 960 |
/* Chain up to parent's child_added() method. */ |
| 961 |
E_COLLECTION_BACKEND_CLASS (e_ews_backend_parent_class)-> |
| 962 |
child_added (backend, child_source); |
| 963 |
} |
| 964 |
|
| 965 |
static void |
| 966 |
ews_backend_child_removed (ECollectionBackend *backend, |
| 967 |
ESource *child_source) |
| 968 |
{ |
| 969 |
const gchar *extension_name; |
| 970 |
|
| 971 |
/* We track EWS folders in a hash table by folder ID. */ |
| 972 |
extension_name = E_SOURCE_EXTENSION_EWS_FOLDER; |
| 973 |
if (e_source_has_extension (child_source, extension_name)) { |
| 974 |
ESourceEwsFolder *extension; |
| 975 |
const gchar *folder_id; |
| 976 |
|
| 977 |
extension = e_source_get_extension ( |
| 978 |
child_source, extension_name); |
| 979 |
folder_id = e_source_ews_folder_get_id (extension); |
| 980 |
if (folder_id != NULL) |
| 981 |
ews_backend_folders_remove ( |
| 982 |
E_EWS_BACKEND (backend), folder_id); |
| 983 |
} |
| 984 |
|
| 985 |
/* Chain up to parent's child_removed() method. */ |
| 986 |
E_COLLECTION_BACKEND_CLASS (e_ews_backend_parent_class)-> |
| 987 |
child_removed (backend, child_source); |
| 988 |
} |
| 989 |
|
| 990 |
static gboolean |
| 991 |
ews_backend_create_resource_sync (ECollectionBackend *backend, |
| 992 |
ESource *source, |
| 993 |
GCancellable *cancellable, |
| 994 |
GError **error) |
| 995 |
{ |
| 996 |
EEwsConnection *connection = NULL; |
| 997 |
EwsFolderId *out_folder_id = NULL; |
| 998 |
EEwsFolderType folder_type = E_EWS_FOLDER_TYPE_UNKNOWN; |
| 999 |
const gchar *extension_name; |
| 1000 |
const gchar *parent_folder_id = NULL; |
| 1001 |
gchar *folder_name; |
| 1002 |
gboolean success = FALSE; |
| 1003 |
|
| 1004 |
extension_name = E_SOURCE_EXTENSION_EWS_FOLDER; |
| 1005 |
if (e_source_has_extension (source, extension_name)) { |
| 1006 |
ESourceEwsFolder *extension; |
| 1007 |
|
| 1008 |
/* foreign and public folders are just added */ |
| 1009 |
extension = e_source_get_extension (source, extension_name); |
| 1010 |
if (e_source_ews_folder_get_foreign (extension) || |
| 1011 |
e_source_ews_folder_get_public (extension)) |
| 1012 |
success = TRUE; |
| 1013 |
} |
| 1014 |
|
| 1015 |
if (!success) { |
| 1016 |
connection = e_ews_backend_ref_connection_sync (E_EWS_BACKEND (backend), NULL, NULL, NULL, cancellable, error); |
| 1017 |
if (connection == NULL) |
| 1018 |
return FALSE; |
| 1019 |
|
| 1020 |
extension_name = E_SOURCE_EXTENSION_ADDRESS_BOOK; |
| 1021 |
if (e_source_has_extension (source, extension_name)) { |
| 1022 |
folder_type = E_EWS_FOLDER_TYPE_CONTACTS; |
| 1023 |
parent_folder_id = "contacts"; |
| 1024 |
} |
| 1025 |
|
| 1026 |
extension_name = E_SOURCE_EXTENSION_CALENDAR; |
| 1027 |
if (e_source_has_extension (source, extension_name)) { |
| 1028 |
folder_type = E_EWS_FOLDER_TYPE_CALENDAR; |
| 1029 |
parent_folder_id = "calendar"; |
| 1030 |
} |
| 1031 |
|
| 1032 |
extension_name = E_SOURCE_EXTENSION_TASK_LIST; |
| 1033 |
if (e_source_has_extension (source, extension_name)) { |
| 1034 |
folder_type = E_EWS_FOLDER_TYPE_TASKS; |
| 1035 |
parent_folder_id = "tasks"; |
| 1036 |
} |
| 1037 |
|
| 1038 |
/* FIXME No support for memo lists. */ |
| 1039 |
|
| 1040 |
if (parent_folder_id == NULL) { |
| 1041 |
g_set_error ( |
| 1042 |
error, G_IO_ERROR, |
| 1043 |
G_IO_ERROR_INVALID_ARGUMENT, |
| 1044 |
_("Could not determine a suitable folder " |
| 1045 |
"class for a new folder named â%sâ"), |
| 1046 |
e_source_get_display_name (source)); |
| 1047 |
goto exit; |
| 1048 |
} |
| 1049 |
|
| 1050 |
folder_name = e_source_dup_display_name (source); |
| 1051 |
|
| 1052 |
success = e_ews_connection_create_folder_sync ( |
| 1053 |
connection, EWS_PRIORITY_MEDIUM, |
| 1054 |
parent_folder_id, TRUE, |
| 1055 |
folder_name, folder_type, |
| 1056 |
&out_folder_id, cancellable, error); |
| 1057 |
|
| 1058 |
/* Sanity check */ |
| 1059 |
g_warn_if_fail ( |
| 1060 |
(success && out_folder_id != NULL) || |
| 1061 |
(!success && out_folder_id == NULL)); |
| 1062 |
|
| 1063 |
if (out_folder_id != NULL) { |
| 1064 |
ESourceEwsFolder *extension; |
| 1065 |
|
| 1066 |
extension_name = E_SOURCE_EXTENSION_EWS_FOLDER; |
| 1067 |
extension = e_source_get_extension (source, extension_name); |
| 1068 |
e_source_ews_folder_set_id ( |
| 1069 |
extension, out_folder_id->id); |
| 1070 |
e_source_ews_folder_set_change_key ( |
| 1071 |
extension, out_folder_id->change_key); |
| 1072 |
e_source_ews_folder_set_name (extension, folder_name); |
| 1073 |
|
| 1074 |
e_ews_folder_id_free (out_folder_id); |
| 1075 |
} |
| 1076 |
|
| 1077 |
g_free (folder_name); |
| 1078 |
} |
| 1079 |
|
| 1080 |
if (success) { |
| 1081 |
ESourceRegistryServer *server; |
| 1082 |
ESource *parent_source; |
| 1083 |
const gchar *cache_dir; |
| 1084 |
const gchar *parent_uid; |
| 1085 |
|
| 1086 |
/* Configure the source as a collection member. */ |
| 1087 |
parent_source = e_backend_get_source (E_BACKEND (backend)); |
| 1088 |
parent_uid = e_source_get_uid (parent_source); |
| 1089 |
e_source_set_parent (source, parent_uid); |
| 1090 |
|
| 1091 |
/* Changes should be written back to the cache directory. */ |
| 1092 |
cache_dir = e_collection_backend_get_cache_dir (backend); |
| 1093 |
e_server_side_source_set_write_directory ( |
| 1094 |
E_SERVER_SIDE_SOURCE (source), cache_dir); |
| 1095 |
|
| 1096 |
/* Set permissions for clients. */ |
| 1097 |
e_server_side_source_set_writable (E_SERVER_SIDE_SOURCE (source), TRUE); |
| 1098 |
e_server_side_source_set_remote_deletable (E_SERVER_SIDE_SOURCE (source), TRUE); |
| 1099 |
|
| 1100 |
server = e_collection_backend_ref_server (backend); |
| 1101 |
e_source_registry_server_add_source (server, source); |
| 1102 |
g_object_unref (server); |
| 1103 |
} |
| 1104 |
|
| 1105 |
exit: |
| 1106 |
if (connection) |
| 1107 |
g_object_unref (connection); |
| 1108 |
|
| 1109 |
return success; |
| 1110 |
} |
| 1111 |
|
| 1112 |
static gboolean |
| 1113 |
ews_backend_delete_resource_sync (ECollectionBackend *backend, |
| 1114 |
ESource *source, |
| 1115 |
GCancellable *cancellable, |
| 1116 |
GError **error) |
| 1117 |
{ |
| 1118 |
EEwsConnection *connection; |
| 1119 |
ESourceEwsFolder *extension; |
| 1120 |
const gchar *extension_name; |
| 1121 |
gboolean success = FALSE; |
| 1122 |
|
| 1123 |
connection = e_ews_backend_ref_connection_sync (E_EWS_BACKEND (backend), NULL, NULL, NULL, cancellable, error); |
| 1124 |
if (connection == NULL) |
| 1125 |
return FALSE; |
| 1126 |
|
| 1127 |
extension_name = E_SOURCE_EXTENSION_EWS_FOLDER; |
| 1128 |
if (!e_source_has_extension (source, extension_name)) { |
| 1129 |
g_set_error ( |
| 1130 |
error, G_IO_ERROR, |
| 1131 |
G_IO_ERROR_INVALID_ARGUMENT, |
| 1132 |
_("Data source â%sâ does not represent " |
| 1133 |
"an Exchange Web Services folder"), |
| 1134 |
e_source_get_display_name (source)); |
| 1135 |
goto exit; |
| 1136 |
} |
| 1137 |
extension = e_source_get_extension (source, extension_name); |
| 1138 |
|
| 1139 |
if (e_source_ews_folder_get_foreign (extension) || |
| 1140 |
e_source_ews_folder_get_public (extension)) { |
| 1141 |
/* do not delete foreign or public folders, |
| 1142 |
* just remove them from local store */ |
| 1143 |
success = TRUE; |
| 1144 |
} else { |
| 1145 |
gchar *folder_id; |
| 1146 |
|
| 1147 |
folder_id = e_source_ews_folder_dup_id (extension); |
| 1148 |
|
| 1149 |
success = e_ews_connection_delete_folder_sync ( |
| 1150 |
connection, EWS_PRIORITY_MEDIUM, folder_id, |
| 1151 |
FALSE, "HardDelete", cancellable, error); |
| 1152 |
|
| 1153 |
g_free (folder_id); |
| 1154 |
} |
| 1155 |
|
| 1156 |
if (success) |
| 1157 |
success = e_source_remove_sync (source, cancellable, error); |
| 1158 |
|
| 1159 |
exit: |
| 1160 |
g_object_unref (connection); |
| 1161 |
|
| 1162 |
return success; |
| 1163 |
} |
| 1164 |
|
| 1165 |
static gboolean |
| 1166 |
ews_backend_get_destination_address (EBackend *backend, |
| 1167 |
gchar **host, |
| 1168 |
guint16 *port) |
| 1169 |
{ |
| 1170 |
CamelEwsSettings *ews_settings; |
| 1171 |
GUri *uri; |
| 1172 |
gchar *host_url; |
| 1173 |
gboolean result = FALSE; |
| 1174 |
|
| 1175 |
g_return_val_if_fail (port != NULL, FALSE); |
| 1176 |
g_return_val_if_fail (host != NULL, FALSE); |
| 1177 |
|
| 1178 |
ews_settings = ews_backend_get_settings (E_EWS_BACKEND (backend)); |
| 1179 |
g_return_val_if_fail (ews_settings != NULL, FALSE); |
| 1180 |
|
| 1181 |
host_url = camel_ews_settings_dup_hosturl (ews_settings); |
| 1182 |
if (!host_url) { |
| 1183 |
ESource *source; |
| 1184 |
ESourceAuthentication *auth_extension; |
| 1185 |
|
| 1186 |
source = e_backend_get_source (backend); |
| 1187 |
auth_extension = e_source_get_extension (source, E_SOURCE_EXTENSION_AUTHENTICATION); |
| 1188 |
|
| 1189 |
*host = e_source_authentication_dup_host (auth_extension); |
| 1190 |
*port = e_source_authentication_get_port (auth_extension); |
| 1191 |
|
| 1192 |
return *host && **host; |
| 1193 |
} |
| 1194 |
|
| 1195 |
uri = g_uri_parse (host_url, SOUP_HTTP_URI_FLAGS | G_URI_FLAGS_PARSE_RELAXED, NULL); |
| 1196 |
if (uri) { |
| 1197 |
*host = g_strdup (g_uri_get_host (uri)); |
| 1198 |
*port = g_uri_get_port (uri) > 0 ? g_uri_get_port (uri) : 0; |
| 1199 |
|
| 1200 |
result = *host && **host; |
| 1201 |
if (!result) { |
| 1202 |
g_free (*host); |
| 1203 |
*host = NULL; |
| 1204 |
} |
| 1205 |
|
| 1206 |
g_uri_unref (uri); |
| 1207 |
} |
| 1208 |
|
| 1209 |
g_free (host_url); |
| 1210 |
|
| 1211 |
return result; |
| 1212 |
} |
| 1213 |
|
| 1214 |
static ESourceAuthenticationResult |
| 1215 |
ews_backend_authenticate_sync (EBackend *backend, |
| 1216 |
const ENamedParameters *credentials, |
| 1217 |
gchar **out_certificate_pem, |
| 1218 |
GTlsCertificateFlags *out_certificate_errors, |
| 1219 |
GCancellable *cancellable, |
| 1220 |
GError **error) |
| 1221 |
{ |
| 1222 |
EEwsBackend *ews_backend; |
| 1223 |
EEwsConnection *connection; |
| 1224 |
CamelEwsSettings *ews_settings; |
| 1225 |
ESourceAuthenticationResult result = E_SOURCE_AUTHENTICATION_ERROR; |
| 1226 |
gboolean in_sync_folders = FALSE; |
| 1227 |
|
| 1228 |
g_return_val_if_fail (E_IS_EWS_BACKEND (backend), E_SOURCE_AUTHENTICATION_ERROR); |
| 1229 |
|
| 1230 |
ews_backend = E_EWS_BACKEND (backend); |
| 1231 |
ews_settings = ews_backend_get_settings (ews_backend); |
| 1232 |
g_return_val_if_fail (ews_settings != NULL, E_SOURCE_AUTHENTICATION_ERROR); |
| 1233 |
|
| 1234 |
e_collection_backend_freeze_populate (E_COLLECTION_BACKEND (backend)); |
| 1235 |
|
| 1236 |
g_mutex_lock (&ews_backend->priv->connection_lock); |
| 1237 |
g_clear_object (&ews_backend->priv->connection); |
| 1238 |
e_named_parameters_free (ews_backend->priv->credentials); |
| 1239 |
ews_backend->priv->credentials = e_named_parameters_new_clone (credentials); |
| 1240 |
g_mutex_unlock (&ews_backend->priv->connection_lock); |
| 1241 |
|
| 1242 |
connection = e_ews_backend_ref_connection_sync (ews_backend, &result, out_certificate_pem, out_certificate_errors, cancellable, error); |
| 1243 |
g_clear_object (&connection); |
| 1244 |
|
| 1245 |
if (result == E_SOURCE_AUTHENTICATION_ACCEPTED) { |
| 1246 |
e_collection_backend_authenticate_children (E_COLLECTION_BACKEND (backend), credentials); |
| 1247 |
|
| 1248 |
e_ews_backend_sync_folders (ews_backend, NULL, ews_backend_folders_synced_cb, NULL); |
| 1249 |
|
| 1250 |
in_sync_folders = TRUE; |
| 1251 |
} else if (e_ews_connection_utils_get_without_password (ews_settings) && |
| 1252 |
result == E_SOURCE_AUTHENTICATION_REJECTED && |
| 1253 |
!e_named_parameters_exists (credentials, E_SOURCE_CREDENTIAL_PASSWORD)) { |
| 1254 |
e_ews_connection_utils_force_off_ntlm_auth_check (); |
| 1255 |
result = E_SOURCE_AUTHENTICATION_REQUIRED; |
| 1256 |
} |
| 1257 |
|
| 1258 |
if (!in_sync_folders) |
| 1259 |
e_collection_backend_thaw_populate (E_COLLECTION_BACKEND (backend)); |
| 1260 |
|
| 1261 |
return result; |
| 1262 |
} |
| 1263 |
|
| 1264 |
static void |
| 1265 |
e_ews_backend_class_init (EEwsBackendClass *class) |
| 1266 |
{ |
| 1267 |
GObjectClass *object_class; |
| 1268 |
EBackendClass *backend_class; |
| 1269 |
ECollectionBackendClass *collection_backend_class; |
| 1270 |
|
| 1271 |
object_class = G_OBJECT_CLASS (class); |
| 1272 |
object_class->dispose = ews_backend_dispose; |
| 1273 |
object_class->finalize = ews_backend_finalize; |
| 1274 |
object_class->constructed = ews_backend_constructed; |
| 1275 |
|
| 1276 |
collection_backend_class = E_COLLECTION_BACKEND_CLASS (class); |
| 1277 |
collection_backend_class->populate = ews_backend_populate; |
| 1278 |
collection_backend_class->dup_resource_id = ews_backend_dup_resource_id; |
| 1279 |
collection_backend_class->child_added = ews_backend_child_added; |
| 1280 |
collection_backend_class->child_removed = ews_backend_child_removed; |
| 1281 |
collection_backend_class->create_resource_sync = ews_backend_create_resource_sync; |
| 1282 |
collection_backend_class->delete_resource_sync = ews_backend_delete_resource_sync; |
| 1283 |
|
| 1284 |
backend_class = E_BACKEND_CLASS (class); |
| 1285 |
backend_class->get_destination_address = ews_backend_get_destination_address; |
| 1286 |
backend_class->authenticate_sync = ews_backend_authenticate_sync; |
| 1287 |
|
| 1288 |
/* This generates an ESourceCamel subtype for CamelEwsSettings. */ |
| 1289 |
e_source_camel_generate_subtype ("ews", CAMEL_TYPE_EWS_SETTINGS); |
| 1290 |
} |
| 1291 |
|
| 1292 |
static void |
| 1293 |
e_ews_backend_class_finalize (EEwsBackendClass *class) |
| 1294 |
{ |
| 1295 |
} |
| 1296 |
|
| 1297 |
static void |
| 1298 |
e_ews_backend_init (EEwsBackend *backend) |
| 1299 |
{ |
| 1300 |
backend->priv = e_ews_backend_get_instance_private (backend); |
| 1301 |
|
| 1302 |
backend->priv->folders = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref); |
| 1303 |
|
| 1304 |
g_mutex_init (&backend->priv->folders_lock); |
| 1305 |
g_mutex_init (&backend->priv->sync_state_lock); |
| 1306 |
g_mutex_init (&backend->priv->connection_lock); |
| 1307 |
} |
| 1308 |
|
| 1309 |
void |
| 1310 |
e_ews_backend_type_register (GTypeModule *type_module) |
| 1311 |
{ |
| 1312 |
/* XXX G_DEFINE_DYNAMIC_TYPE declares a static type registration |
| 1313 |
* function, so we have to wrap it with a public function in |
| 1314 |
* order to register types from a separate compilation unit. */ |
| 1315 |
e_ews_backend_register_type (type_module); |
| 1316 |
} |
| 1317 |
|
| 1318 |
static void |
| 1319 |
ews_backend_ref_connection_thread (GSimpleAsyncResult *simple, |
| 1320 |
GObject *object, |
| 1321 |
GCancellable *cancellable) |
| 1322 |
{ |
| 1323 |
EEwsConnection *connection; |
| 1324 |
GError *error = NULL; |
| 1325 |
|
| 1326 |
connection = e_ews_backend_ref_connection_sync (E_EWS_BACKEND (object), NULL, NULL, NULL, cancellable, &error); |
| 1327 |
|
| 1328 |
/* Sanity check. */ |
| 1329 |
g_return_if_fail ( |
| 1330 |
((connection != NULL) && (error == NULL)) || |
| 1331 |
((connection == NULL) && (error != NULL))); |
| 1332 |
|
| 1333 |
if (connection != NULL) |
| 1334 |
g_simple_async_result_set_op_res_gpointer ( |
| 1335 |
simple, connection, g_object_unref); |
| 1336 |
|
| 1337 |
if (error != NULL) |
| 1338 |
g_simple_async_result_take_error (simple, error); |
| 1339 |
} |
| 1340 |
|
| 1341 |
EEwsConnection * |
| 1342 |
e_ews_backend_ref_connection_sync (EEwsBackend *backend, |
| 1343 |
ESourceAuthenticationResult *result, |
| 1344 |
gchar **out_certificate_pem, |
| 1345 |
GTlsCertificateFlags *out_certificate_errors, |
| 1346 |
GCancellable *cancellable, |
| 1347 |
GError **error) |
| 1348 |
{ |
| 1349 |
EEwsConnection *connection = NULL; |
| 1350 |
ESourceAuthenticationResult local_result; |
| 1351 |
ESource *source; |
| 1352 |
CamelEwsSettings *settings; |
| 1353 |
gchar *hosturl; |
| 1354 |
gboolean success; |
| 1355 |
|
| 1356 |
g_return_val_if_fail (E_IS_EWS_BACKEND (backend), NULL); |
| 1357 |
|
| 1358 |
g_mutex_lock (&backend->priv->connection_lock); |
| 1359 |
if (backend->priv->connection != NULL) |
| 1360 |
connection = g_object_ref (backend->priv->connection); |
| 1361 |
g_mutex_unlock (&backend->priv->connection_lock); |
| 1362 |
|
| 1363 |
/* If we already have an authenticated |
| 1364 |
* connection object, just return that. */ |
| 1365 |
if (connection != NULL || !backend->priv->credentials) |
| 1366 |
return connection; |
| 1367 |
|
| 1368 |
source = e_backend_get_source (E_BACKEND (backend)); |
| 1369 |
settings = ews_backend_get_settings (backend); |
| 1370 |
hosturl = camel_ews_settings_dup_hosturl (settings); |
| 1371 |
if (!hosturl) { |
| 1372 |
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT, "Host URL not set"); |
| 1373 |
|
| 1374 |
if (result) |
| 1375 |
*result = E_SOURCE_AUTHENTICATION_ERROR; |
| 1376 |
|
| 1377 |
return NULL; |
| 1378 |
} |
| 1379 |
|
| 1380 |
connection = e_ews_connection_new_full (source, hosturl, settings, FALSE); |
| 1381 |
g_free (hosturl); |
| 1382 |
|
| 1383 |
e_binding_bind_property ( |
| 1384 |
backend, "proxy-resolver", |
| 1385 |
connection, "proxy-resolver", |
| 1386 |
G_BINDING_SYNC_CREATE); |
| 1387 |
|
| 1388 |
local_result = e_ews_connection_try_credentials_sync (connection, backend->priv->credentials, NULL, |
| 1389 |
out_certificate_pem, out_certificate_errors, cancellable, error); |
| 1390 |
if (result) |
| 1391 |
*result = local_result; |
| 1392 |
|
| 1393 |
success = local_result == E_SOURCE_AUTHENTICATION_ACCEPTED; |
| 1394 |
|
| 1395 |
if (success) { |
| 1396 |
g_mutex_lock (&backend->priv->connection_lock); |
| 1397 |
if (backend->priv->connection != NULL) |
| 1398 |
g_object_unref (backend->priv->connection); |
| 1399 |
backend->priv->connection = g_object_ref (connection); |
| 1400 |
g_mutex_unlock (&backend->priv->connection_lock); |
| 1401 |
} else { |
| 1402 |
g_object_unref (connection); |
| 1403 |
connection = NULL; |
| 1404 |
} |
| 1405 |
|
| 1406 |
return connection; |
| 1407 |
} |
| 1408 |
|
| 1409 |
void |
| 1410 |
e_ews_backend_ref_connection (EEwsBackend *backend, |
| 1411 |
GCancellable *cancellable, |
| 1412 |
GAsyncReadyCallback callback, |
| 1413 |
gpointer user_data) |
| 1414 |
{ |
| 1415 |
GSimpleAsyncResult *simple; |
| 1416 |
|
| 1417 |
g_return_if_fail (E_IS_EWS_BACKEND (backend)); |
| 1418 |
|
| 1419 |
simple = g_simple_async_result_new ( |
| 1420 |
G_OBJECT (backend), callback, |
| 1421 |
user_data, e_ews_backend_ref_connection); |
| 1422 |
|
| 1423 |
g_simple_async_result_run_in_thread ( |
| 1424 |
simple, ews_backend_ref_connection_thread, |
| 1425 |
G_PRIORITY_DEFAULT, cancellable); |
| 1426 |
|
| 1427 |
g_object_unref (simple); |
| 1428 |
} |
| 1429 |
|
| 1430 |
EEwsConnection * |
| 1431 |
e_ews_backend_ref_connection_finish (EEwsBackend *backend, |
| 1432 |
GAsyncResult *result, |
| 1433 |
GError **error) |
| 1434 |
{ |
| 1435 |
GSimpleAsyncResult *simple; |
| 1436 |
EEwsConnection *connection; |
| 1437 |
|
| 1438 |
g_return_val_if_fail ( |
| 1439 |
g_simple_async_result_is_valid ( |
| 1440 |
result, G_OBJECT (backend), |
| 1441 |
e_ews_backend_ref_connection), NULL); |
| 1442 |
|
| 1443 |
simple = G_SIMPLE_ASYNC_RESULT (result); |
| 1444 |
|
| 1445 |
if (g_simple_async_result_propagate_error (simple, error)) |
| 1446 |
return NULL; |
| 1447 |
|
| 1448 |
connection = g_simple_async_result_get_op_res_gpointer (simple); |
| 1449 |
g_return_val_if_fail (E_IS_EWS_CONNECTION (connection), NULL); |
| 1450 |
|
| 1451 |
return g_object_ref (connection); |
| 1452 |
} |
| 1453 |
|
| 1454 |
static void |
| 1455 |
ews_backend_sync_folders_thread (GSimpleAsyncResult *simple, |
| 1456 |
GObject *object, |
| 1457 |
GCancellable *cancellable) |
| 1458 |
{ |
| 1459 |
GError *error = NULL; |
| 1460 |
|
| 1461 |
e_ews_backend_sync_folders_sync ( |
| 1462 |
E_EWS_BACKEND (object), cancellable, &error); |
| 1463 |
|
| 1464 |
if (error != NULL) |
| 1465 |
g_simple_async_result_take_error (simple, error); |
| 1466 |
} |
| 1467 |
|
| 1468 |
static void |
| 1469 |
ews_backend_delete_each_source_cb (gpointer data, |
| 1470 |
gpointer user_data) |
| 1471 |
{ |
| 1472 |
ESource *source = data; |
| 1473 |
ECollectionBackend *backend = user_data; |
| 1474 |
|
| 1475 |
e_collection_backend_delete_resource_sync (backend, source, NULL, NULL); |
| 1476 |
} |
| 1477 |
|
| 1478 |
static void |
| 1479 |
ews_backend_forget_all_sources (EEwsBackend *backend) |
| 1480 |
{ |
| 1481 |
GList *sources; |
| 1482 |
|
| 1483 |
g_return_if_fail (E_IS_EWS_BACKEND (backend)); |
| 1484 |
|
| 1485 |
sources = e_collection_backend_list_calendar_sources (E_COLLECTION_BACKEND (backend)); |
| 1486 |
g_list_foreach (sources, ews_backend_delete_each_source_cb, backend); |
| 1487 |
g_list_free_full (sources, g_object_unref); |
| 1488 |
|
| 1489 |
sources = e_collection_backend_list_contacts_sources (E_COLLECTION_BACKEND (backend)); |
| 1490 |
g_list_foreach (sources, ews_backend_delete_each_source_cb, backend); |
| 1491 |
g_list_free_full (sources, g_object_unref); |
| 1492 |
|
| 1493 |
sources = e_collection_backend_list_mail_sources (E_COLLECTION_BACKEND (backend)); |
| 1494 |
g_list_foreach (sources, ews_backend_delete_each_source_cb, backend); |
| 1495 |
g_list_free_full (sources, g_object_unref); |
| 1496 |
} |
| 1497 |
|
| 1498 |
static void |
| 1499 |
ews_backend_fill_known_sources (EEwsBackend *backend, |
| 1500 |
GHashTable *known_sources) /* gchar *folder_id ~> ESource * */ |
| 1501 |
{ |
| 1502 |
ECollectionBackend *collection_backend; |
| 1503 |
CamelEwsSettings *settings; |
| 1504 |
GList *sources, *link; |
| 1505 |
gchar *gal_source_uid; |
| 1506 |
|
| 1507 |
g_return_if_fail (E_IS_EWS_BACKEND (backend)); |
| 1508 |
g_return_if_fail (known_sources != NULL); |
| 1509 |
|
| 1510 |
settings = ews_backend_get_settings (backend); |
| 1511 |
gal_source_uid = camel_ews_settings_dup_gal_uid (settings); |
| 1512 |
|
| 1513 |
collection_backend = E_COLLECTION_BACKEND (backend); |
| 1514 |
|
| 1515 |
sources = e_collection_backend_list_calendar_sources (collection_backend); |
| 1516 |
for (link = sources; link; link = g_list_next (link)) { |
| 1517 |
ESource *source = link->data; |
| 1518 |
|
| 1519 |
if (e_source_has_extension (source, E_SOURCE_EXTENSION_EWS_FOLDER)) { |
| 1520 |
ESourceEwsFolder *extension; |
| 1521 |
gchar *folder_id; |
| 1522 |
|
| 1523 |
extension = e_source_get_extension (source, E_SOURCE_EXTENSION_EWS_FOLDER); |
| 1524 |
folder_id = e_source_ews_folder_dup_id (extension); |
| 1525 |
if (folder_id) |
| 1526 |
g_hash_table_insert (known_sources, folder_id, g_object_ref (source)); |
| 1527 |
} |
| 1528 |
} |
| 1529 |
g_list_free_full (sources, g_object_unref); |
| 1530 |
|
| 1531 |
sources = e_collection_backend_list_contacts_sources (collection_backend); |
| 1532 |
for (link = sources; link; link = g_list_next (link)) { |
| 1533 |
ESource *source = link->data; |
| 1534 |
|
| 1535 |
/* Do not include GAL in known sources, there is always at least one */ |
| 1536 |
if ((!gal_source_uid || g_strcmp0 (gal_source_uid, e_source_get_uid (source)) != 0) && |
| 1537 |
e_source_has_extension (source, E_SOURCE_EXTENSION_EWS_FOLDER)) { |
| 1538 |
ESourceEwsFolder *extension; |
| 1539 |
gchar *folder_id; |
| 1540 |
|
| 1541 |
extension = e_source_get_extension (source, E_SOURCE_EXTENSION_EWS_FOLDER); |
| 1542 |
folder_id = e_source_ews_folder_dup_id (extension); |
| 1543 |
if (folder_id) |
| 1544 |
g_hash_table_insert (known_sources, folder_id, g_object_ref (source)); |
| 1545 |
} |
| 1546 |
} |
| 1547 |
g_list_free_full (sources, g_object_unref); |
| 1548 |
|
| 1549 |
g_free (gal_source_uid); |
| 1550 |
} |
| 1551 |
|
| 1552 |
gboolean |
| 1553 |
e_ews_backend_sync_folders_sync (EEwsBackend *backend, |
| 1554 |
GCancellable *cancellable, |
| 1555 |
GError **error) |
| 1556 |
{ |
| 1557 |
EEwsConnection *connection; |
| 1558 |
GSList *folders_created = NULL; |
| 1559 |
GSList *folders_updated = NULL; |
| 1560 |
GSList *folders_deleted = NULL; |
| 1561 |
gboolean includes_last_folder = FALSE; |
| 1562 |
gchar *old_sync_state, *new_sync_state = NULL; |
| 1563 |
gboolean success; |
| 1564 |
GError *local_error = NULL; |
| 1565 |
|
| 1566 |
g_return_val_if_fail (E_IS_EWS_BACKEND (backend), FALSE); |
| 1567 |
|
| 1568 |
if (!e_backend_get_online (E_BACKEND (backend))) { |
| 1569 |
SyncFoldersClosure *closure; |
| 1570 |
|
| 1571 |
/* This takes ownership of the folder lists. */ |
| 1572 |
closure = g_slice_new0 (SyncFoldersClosure); |
| 1573 |
closure->backend = g_object_ref (backend); |
| 1574 |
closure->old_sources = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref); |
| 1575 |
|
| 1576 |
/* Process the results from an idle callback. */ |
| 1577 |
g_idle_add_full ( |
| 1578 |
G_PRIORITY_DEFAULT_IDLE, |
| 1579 |
ews_backend_sync_folders_idle_cb, closure, |
| 1580 |
(GDestroyNotify) sync_folders_closure_free); |
| 1581 |
|
| 1582 |
return TRUE; |
| 1583 |
} |
| 1584 |
|
| 1585 |
connection = e_ews_backend_ref_connection_sync (backend, NULL, NULL, NULL, cancellable, error); |
| 1586 |
|
| 1587 |
if (connection == NULL) { |
| 1588 |
backend->priv->need_update_folders = TRUE; |
| 1589 |
return FALSE; |
| 1590 |
} |
| 1591 |
|
| 1592 |
backend->priv->need_update_folders = FALSE; |
| 1593 |
|
| 1594 |
g_mutex_lock (&backend->priv->sync_state_lock); |
| 1595 |
old_sync_state = g_strdup (backend->priv->sync_state); |
| 1596 |
|
| 1597 |
if (!backend->priv->deleted_items_folder_id) { |
| 1598 |
EwsFolderId fid; |
| 1599 |
GSList in_lst, *folders = NULL; |
| 1600 |
|
| 1601 |
memset (&fid, 0, sizeof (EwsFolderId)); |
| 1602 |
fid.id = (gchar *) "deleteditems"; |
| 1603 |
fid.is_distinguished_id = TRUE; |
| 1604 |
|
| 1605 |
memset (&in_lst, 0, sizeof (GSList)); |
| 1606 |
in_lst.data = &fid; |
| 1607 |
|
| 1608 |
g_mutex_unlock (&backend->priv->sync_state_lock); |
| 1609 |
|
| 1610 |
if (e_ews_connection_get_folder_sync (connection, EWS_PRIORITY_MEDIUM, "IdOnly", NULL, &in_lst, &folders, cancellable, NULL)) { |
| 1611 |
EEwsFolder *deleteditems = folders ? folders->data : NULL; |
| 1612 |
|
| 1613 |
g_mutex_lock (&backend->priv->sync_state_lock); |
| 1614 |
|
| 1615 |
if (deleteditems) { |
| 1616 |
const EwsFolderId *id = e_ews_folder_get_id (deleteditems); |
| 1617 |
|
| 1618 |
if (id && id->id && *id->id) { |
| 1619 |
/* In case multiple threads fight on the authenticate at the same |
| 1620 |
time and one might eventually set the value before the lock |
| 1621 |
was re-acquired. */ |
| 1622 |
g_free (backend->priv->deleted_items_folder_id); |
| 1623 |
backend->priv->deleted_items_folder_id = g_strdup (id->id); |
| 1624 |
} |
| 1625 |
} |
| 1626 |
|
| 1627 |
g_slist_free_full (folders, g_object_unref); |
| 1628 |
} else { |
| 1629 |
g_mutex_lock (&backend->priv->sync_state_lock); |
| 1630 |
} |
| 1631 |
} |
| 1632 |
|
| 1633 |
g_mutex_unlock (&backend->priv->sync_state_lock); |
| 1634 |
|
| 1635 |
success = e_ews_connection_sync_folder_hierarchy_sync (connection, EWS_PRIORITY_MEDIUM, old_sync_state, |
| 1636 |
&new_sync_state, &includes_last_folder, &folders_created, &folders_updated, &folders_deleted, |
| 1637 |
cancellable, &local_error); |
| 1638 |
|
| 1639 |
if (old_sync_state && g_error_matches (local_error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_INVALIDSYNCSTATEDATA)) { |
| 1640 |
g_clear_error (&local_error); |
| 1641 |
|
| 1642 |
g_mutex_lock (&backend->priv->sync_state_lock); |
| 1643 |
g_free (backend->priv->sync_state); |
| 1644 |
backend->priv->sync_state = NULL; |
| 1645 |
g_mutex_unlock (&backend->priv->sync_state_lock); |
| 1646 |
|
| 1647 |
ews_backend_forget_all_sources (backend); |
| 1648 |
|
| 1649 |
success = e_ews_connection_sync_folder_hierarchy_sync (connection, EWS_PRIORITY_MEDIUM, NULL, |
| 1650 |
&new_sync_state, &includes_last_folder, &folders_created, &folders_updated, &folders_deleted, |
| 1651 |
cancellable, &local_error); |
| 1652 |
} else if (local_error) { |
| 1653 |
g_propagate_error (error, local_error); |
| 1654 |
local_error = NULL; |
| 1655 |
} |
| 1656 |
|
| 1657 |
g_free (old_sync_state); |
| 1658 |
old_sync_state = NULL; |
| 1659 |
|
| 1660 |
if (success) { |
| 1661 |
SyncFoldersClosure *closure; |
| 1662 |
gboolean had_sync_state; |
| 1663 |
|
| 1664 |
/* This takes ownership of the folder lists. */ |
| 1665 |
closure = g_slice_new0 (SyncFoldersClosure); |
| 1666 |
closure->backend = g_object_ref (backend); |
| 1667 |
closure->old_sources = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref); |
| 1668 |
closure->folders_created = folders_created; |
| 1669 |
closure->folders_deleted = folders_deleted; |
| 1670 |
closure->folders_updated = folders_updated; |
| 1671 |
|
| 1672 |
g_mutex_lock (&backend->priv->sync_state_lock); |
| 1673 |
had_sync_state = backend->priv->sync_state && *(backend->priv->sync_state); |
| 1674 |
|
| 1675 |
g_free (backend->priv->sync_state); |
| 1676 |
backend->priv->sync_state = g_strdup (new_sync_state); |
| 1677 |
g_mutex_unlock (&backend->priv->sync_state_lock); |
| 1678 |
|
| 1679 |
if (!had_sync_state) |
| 1680 |
ews_backend_fill_known_sources (backend, closure->old_sources); |
| 1681 |
|
| 1682 |
/* Process the results from an idle callback. */ |
| 1683 |
g_idle_add_full ( |
| 1684 |
G_PRIORITY_DEFAULT_IDLE, |
| 1685 |
ews_backend_sync_folders_idle_cb, closure, |
| 1686 |
(GDestroyNotify) sync_folders_closure_free); |
| 1687 |
|
| 1688 |
} else { |
| 1689 |
/* Make sure we're not leaking anything. */ |
| 1690 |
g_warn_if_fail (folders_created == NULL); |
| 1691 |
g_warn_if_fail (folders_updated == NULL); |
| 1692 |
g_warn_if_fail (folders_deleted == NULL); |
| 1693 |
|
| 1694 |
backend->priv->need_update_folders = TRUE; |
| 1695 |
} |
| 1696 |
|
| 1697 |
g_free (new_sync_state); |
| 1698 |
|
| 1699 |
g_object_unref (connection); |
| 1700 |
|
| 1701 |
return success; |
| 1702 |
} |
| 1703 |
|
| 1704 |
void |
| 1705 |
e_ews_backend_sync_folders (EEwsBackend *backend, |
| 1706 |
GCancellable *cancellable, |
| 1707 |
GAsyncReadyCallback callback, |
| 1708 |
gpointer user_data) |
| 1709 |
{ |
| 1710 |
GSimpleAsyncResult *simple; |
| 1711 |
|
| 1712 |
g_return_if_fail (E_IS_EWS_BACKEND (backend)); |
| 1713 |
|
| 1714 |
simple = g_simple_async_result_new ( |
| 1715 |
G_OBJECT (backend), callback, |
| 1716 |
user_data, e_ews_backend_sync_folders); |
| 1717 |
|
| 1718 |
g_simple_async_result_run_in_thread ( |
| 1719 |
simple, ews_backend_sync_folders_thread, |
| 1720 |
G_PRIORITY_DEFAULT, cancellable); |
| 1721 |
|
| 1722 |
g_object_unref (simple); |
| 1723 |
} |
| 1724 |
|
| 1725 |
gboolean |
| 1726 |
e_ews_backend_sync_folders_finish (EEwsBackend *backend, |
| 1727 |
GAsyncResult *result, |
| 1728 |
GError **error) |
| 1729 |
{ |
| 1730 |
GSimpleAsyncResult *simple; |
| 1731 |
|
| 1732 |
g_return_val_if_fail ( |
| 1733 |
g_simple_async_result_is_valid ( |
| 1734 |
result, G_OBJECT (backend), |
| 1735 |
e_ews_backend_sync_folders), FALSE); |
| 1736 |
|
| 1737 |
simple = G_SIMPLE_ASYNC_RESULT (result); |
| 1738 |
|
| 1739 |
/* Assume success unless a GError is set. */ |
| 1740 |
return !g_simple_async_result_propagate_error (simple, error); |
| 1741 |
} |
| 1742 |
|