| 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 <glib/gi18n-lib.h> |
| 9 |
#include <gtk/gtk.h> |
| 10 |
|
| 11 |
#include <libemail-engine/libemail-engine.h> |
| 12 |
|
| 13 |
#include "camel/camel-ews-store.h" |
| 14 |
#include "camel/camel-ews-store-summary.h" |
| 15 |
#include "camel/camel-ews-utils.h" |
| 16 |
|
| 17 |
#include "common/e-ews-calendar-utils.h" |
| 18 |
|
| 19 |
#include "e-ews-config-utils.h" |
| 20 |
#include "e-ews-search-user.h" |
| 21 |
#include "e-ews-subscribe-foreign-folder.h" |
| 22 |
|
| 23 |
#define STR_ACCOUNTS_COMBO "e-ews-accounts-combo" |
| 24 |
#define STR_USER_NAME_SELECTOR_ENTRY "e-ews-name-selector-entry" |
| 25 |
#define STR_FOLDER_NAME_COMBO "e-ews-folder-name-combo" |
| 26 |
#define STR_SUBFOLDERS_CHECK "e-ews-subfolders-check" |
| 27 |
#define STR_EWS_CAMEL_SESSION "e-ews-camel-session" |
| 28 |
#define STR_EWS_DIRECT_EMAIL "e-ews-direct-email" |
| 29 |
|
| 30 |
enum { |
| 31 |
COLUMN_UID = 0, |
| 32 |
COLUMN_DISPLAY_NAME, |
| 33 |
COLUMN_STORE |
| 34 |
}; |
| 35 |
|
| 36 |
static void |
| 37 |
announce_new_folder (CamelEwsStore *ews_store, |
| 38 |
const gchar *fid) |
| 39 |
{ |
| 40 |
CamelFolderInfo *fi; |
| 41 |
|
| 42 |
g_return_if_fail (CAMEL_IS_EWS_STORE (ews_store)); |
| 43 |
g_return_if_fail (fid != NULL); |
| 44 |
g_return_if_fail (camel_ews_store_summary_has_folder (ews_store->summary, fid)); |
| 45 |
|
| 46 |
fi = camel_ews_utils_build_folder_info (ews_store, fid); |
| 47 |
camel_store_folder_created (CAMEL_STORE (ews_store), fi); |
| 48 |
camel_subscribable_folder_subscribed (CAMEL_SUBSCRIBABLE (ews_store), fi); |
| 49 |
camel_folder_info_free (fi); |
| 50 |
} |
| 51 |
|
| 52 |
static gboolean |
| 53 |
add_foreign_folder_to_camel (CamelEwsStore *ews_store, |
| 54 |
const gchar *foreign_email, |
| 55 |
EEwsFolder *folder, |
| 56 |
gboolean include_subfolders, |
| 57 |
const gchar *display_username, |
| 58 |
const gchar *display_foldername, |
| 59 |
GError **perror) |
| 60 |
{ |
| 61 |
gchar *foreign_mailbox_id; |
| 62 |
gchar *mailbox, *fullname; |
| 63 |
const EwsFolderId *fid, *parent_fid; |
| 64 |
|
| 65 |
g_return_val_if_fail (ews_store != NULL, FALSE); |
| 66 |
g_return_val_if_fail (ews_store->summary != NULL, FALSE); |
| 67 |
g_return_val_if_fail (foreign_email != NULL, FALSE); |
| 68 |
g_return_val_if_fail (folder != NULL, FALSE); |
| 69 |
g_return_val_if_fail (display_username != NULL, FALSE); |
| 70 |
g_return_val_if_fail (display_foldername != NULL, FALSE); |
| 71 |
|
| 72 |
fid = e_ews_folder_get_id (folder); |
| 73 |
parent_fid = e_ews_folder_get_parent_id (folder); |
| 74 |
|
| 75 |
g_return_val_if_fail (fid != NULL, FALSE); |
| 76 |
g_return_val_if_fail (parent_fid != NULL, FALSE); |
| 77 |
g_return_val_if_fail (g_strcmp0 (fid->id, parent_fid->id) != 0, FALSE); |
| 78 |
|
| 79 |
if (camel_ews_store_summary_has_folder (ews_store->summary, fid->id)) { |
| 80 |
gchar *full_name = camel_ews_store_summary_get_folder_full_name (ews_store->summary, fid->id, NULL); |
| 81 |
|
| 82 |
g_propagate_error ( |
| 83 |
perror, |
| 84 |
g_error_new (EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_FOLDEREXISTS, |
| 85 |
_("Cannot add folder, folder already exists as “%s”"), full_name)); |
| 86 |
|
| 87 |
g_free (full_name); |
| 88 |
|
| 89 |
return FALSE; |
| 90 |
} |
| 91 |
|
| 92 |
/* Translators: The '%s' is replaced with user name, to whom the foreign mailbox belongs. |
| 93 |
* Example result: "Mailbox — John Smith" |
| 94 |
*/ |
| 95 |
mailbox = g_strdup_printf (C_("ForeignFolder", "Mailbox — %s"), display_username); |
| 96 |
|
| 97 |
foreign_mailbox_id = g_strdup_printf ("ForeignMailbox::%s", foreign_email); |
| 98 |
if (!camel_ews_store_summary_has_folder (ews_store->summary, foreign_mailbox_id)) { |
| 99 |
camel_ews_store_summary_new_folder ( |
| 100 |
ews_store->summary, |
| 101 |
foreign_mailbox_id, EWS_FOREIGN_FOLDER_ROOT_ID, NULL, |
| 102 |
mailbox, E_EWS_FOLDER_TYPE_MAILBOX, |
| 103 |
CAMEL_FOLDER_SYSTEM | CAMEL_FOLDER_NOSELECT, |
| 104 |
0, FALSE, FALSE); |
| 105 |
} |
| 106 |
|
| 107 |
if (camel_ews_store_summary_has_folder (ews_store->summary, parent_fid->id)) { |
| 108 |
camel_ews_store_summary_new_folder ( |
| 109 |
ews_store->summary, |
| 110 |
fid->id, parent_fid->id, fid->change_key, |
| 111 |
display_foldername, E_EWS_FOLDER_TYPE_MAILBOX, |
| 112 |
CAMEL_FOLDER_SUBSCRIBED, e_ews_folder_get_total_count (folder), TRUE, FALSE); |
| 113 |
} else { |
| 114 |
const gchar *displayname; |
| 115 |
gchar *escaped_name; |
| 116 |
|
| 117 |
escaped_name = e_ews_folder_utils_escape_name (display_foldername); |
| 118 |
fullname = g_strdup_printf ("%s/%s/%s", EWS_FOREIGN_FOLDER_ROOT_DISPLAY_NAME, mailbox, escaped_name); |
| 119 |
g_free (escaped_name); |
| 120 |
|
| 121 |
/* make sure the path is unique */ |
| 122 |
camel_ews_store_ensure_unique_path (ews_store, &fullname); |
| 123 |
|
| 124 |
displayname = strrchr (fullname, '/'); |
| 125 |
displayname++; |
| 126 |
|
| 127 |
camel_ews_store_summary_new_folder ( |
| 128 |
ews_store->summary, |
| 129 |
fid->id, foreign_mailbox_id, fid->change_key, |
| 130 |
displayname, E_EWS_FOLDER_TYPE_MAILBOX, |
| 131 |
CAMEL_FOLDER_SUBSCRIBED, e_ews_folder_get_total_count (folder), TRUE, FALSE); |
| 132 |
|
| 133 |
g_free (fullname); |
| 134 |
} |
| 135 |
|
| 136 |
camel_ews_store_ensure_virtual_folders (ews_store); |
| 137 |
camel_ews_store_summary_set_foreign_subfolders (ews_store->summary, fid->id, include_subfolders); |
| 138 |
camel_ews_store_summary_save (ews_store->summary, perror); |
| 139 |
|
| 140 |
announce_new_folder (ews_store, EWS_FOREIGN_FOLDER_ROOT_ID); |
| 141 |
announce_new_folder (ews_store, foreign_mailbox_id); |
| 142 |
announce_new_folder (ews_store, fid->id); |
| 143 |
|
| 144 |
g_free (foreign_mailbox_id); |
| 145 |
g_free (mailbox); |
| 146 |
|
| 147 |
if (include_subfolders) |
| 148 |
camel_ews_store_update_foreign_subfolders (ews_store, fid->id); |
| 149 |
|
| 150 |
return TRUE; |
| 151 |
} |
| 152 |
|
| 153 |
static void |
| 154 |
enable_ok_button_by_data (GObject *dialog) |
| 155 |
{ |
| 156 |
GtkEntry *entry; |
| 157 |
GtkComboBoxText *combo; |
| 158 |
const gchar *entry_text; |
| 159 |
gchar *combo_text; |
| 160 |
|
| 161 |
g_return_if_fail (dialog != NULL); |
| 162 |
|
| 163 |
entry = g_object_get_data (dialog, STR_USER_NAME_SELECTOR_ENTRY); |
| 164 |
g_return_if_fail (entry != NULL); |
| 165 |
|
| 166 |
combo = g_object_get_data (dialog, STR_FOLDER_NAME_COMBO); |
| 167 |
g_return_if_fail (combo != NULL); |
| 168 |
|
| 169 |
entry_text = gtk_entry_get_text (entry); |
| 170 |
combo_text = gtk_combo_box_text_get_active_text (combo); |
| 171 |
|
| 172 |
gtk_dialog_set_response_sensitive ( |
| 173 |
GTK_DIALOG (dialog), GTK_RESPONSE_OK, |
| 174 |
entry_text && *entry_text && *entry_text != ' ' && *entry_text != ',' && |
| 175 |
combo_text && *combo_text); |
| 176 |
|
| 177 |
g_free (combo_text); |
| 178 |
} |
| 179 |
|
| 180 |
static void |
| 181 |
name_entry_changed_cb (GObject *dialog) |
| 182 |
{ |
| 183 |
GtkEntry *entry; |
| 184 |
|
| 185 |
g_return_if_fail (dialog != NULL); |
| 186 |
|
| 187 |
entry = g_object_get_data (dialog, STR_USER_NAME_SELECTOR_ENTRY); |
| 188 |
g_return_if_fail (entry != NULL); |
| 189 |
|
| 190 |
g_object_set_data (G_OBJECT (entry), STR_EWS_DIRECT_EMAIL, NULL); |
| 191 |
|
| 192 |
enable_ok_button_by_data (dialog); |
| 193 |
} |
| 194 |
|
| 195 |
static void |
| 196 |
folder_name_combo_changed_cb (GObject *dialog, |
| 197 |
GtkComboBox *combo) |
| 198 |
{ |
| 199 |
enable_ok_button_by_data (dialog); |
| 200 |
} |
| 201 |
|
| 202 |
struct EEwsCheckForeignFolderData |
| 203 |
{ |
| 204 |
GtkWidget *dialog; |
| 205 |
gboolean include_subfolders; |
| 206 |
gchar *email; |
| 207 |
gchar *direct_email; |
| 208 |
gchar *user_displayname; |
| 209 |
gchar *orig_foldername; |
| 210 |
gchar *use_foldername; |
| 211 |
EEwsFolder *folder; |
| 212 |
}; |
| 213 |
|
| 214 |
static void |
| 215 |
e_ews_check_foreign_folder_data_free (gpointer ptr) |
| 216 |
{ |
| 217 |
struct EEwsCheckForeignFolderData *cffd = ptr; |
| 218 |
|
| 219 |
if (!cffd) |
| 220 |
return; |
| 221 |
|
| 222 |
g_free (cffd->email); |
| 223 |
g_free (cffd->direct_email); |
| 224 |
g_free (cffd->user_displayname); |
| 225 |
g_free (cffd->orig_foldername); |
| 226 |
g_free (cffd->use_foldername); |
| 227 |
|
| 228 |
/* folder tells whether successfully finished, |
| 229 |
* then the dialog can be destroyed */ |
| 230 |
if (cffd->folder && cffd->dialog) |
| 231 |
gtk_widget_destroy (cffd->dialog); |
| 232 |
|
| 233 |
if (cffd->folder) |
| 234 |
g_object_unref (cffd->folder); |
| 235 |
|
| 236 |
g_slice_free (struct EEwsCheckForeignFolderData, cffd); |
| 237 |
} |
| 238 |
|
| 239 |
static void |
| 240 |
check_foreign_folder_thread (GObject *with_object, |
| 241 |
gpointer user_data, |
| 242 |
GCancellable *cancellable, |
| 243 |
GError **perror) |
| 244 |
{ |
| 245 |
struct EEwsCheckForeignFolderData *cffd = user_data; |
| 246 |
GError *local_error = NULL; |
| 247 |
EEwsConnection *conn; |
| 248 |
EwsFolderId fid; |
| 249 |
EEwsFolder *folder = NULL; |
| 250 |
|
| 251 |
g_return_if_fail (with_object != NULL); |
| 252 |
g_return_if_fail (CAMEL_IS_EWS_STORE (with_object)); |
| 253 |
g_return_if_fail (user_data != NULL); |
| 254 |
g_return_if_fail (cffd->email != NULL); |
| 255 |
|
| 256 |
if (g_cancellable_set_error_if_cancelled (cancellable, perror)) |
| 257 |
return; |
| 258 |
|
| 259 |
conn = camel_ews_store_ref_connection (CAMEL_EWS_STORE (with_object)); |
| 260 |
if (!conn) { |
| 261 |
g_set_error_literal ( |
| 262 |
perror, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_NORESPONSE, |
| 263 |
_("Cannot test foreign folder availability when the account is offline")); |
| 264 |
return; |
| 265 |
} |
| 266 |
|
| 267 |
if (cffd->direct_email && *cffd->direct_email) { |
| 268 |
g_return_if_fail (cffd->user_displayname == NULL); |
| 269 |
|
| 270 |
cffd->user_displayname = cffd->email; |
| 271 |
cffd->email = g_strdup (cffd->direct_email); |
| 272 |
} else { |
| 273 |
gchar *display_name = NULL, *email_address = NULL; |
| 274 |
|
| 275 |
if (!e_ews_subscribe_foreign_folder_resolve_name_sync (conn, cffd->email, &display_name, &email_address, cancellable, perror)) { |
| 276 |
g_object_unref (conn); |
| 277 |
return; |
| 278 |
} |
| 279 |
|
| 280 |
if (!cffd->user_displayname) |
| 281 |
cffd->user_displayname = display_name; |
| 282 |
else |
| 283 |
g_free (display_name); |
| 284 |
|
| 285 |
if (email_address && *email_address) { |
| 286 |
g_free (cffd->email); |
| 287 |
cffd->email = email_address; |
| 288 |
} else { |
| 289 |
g_free (email_address); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
if (g_cancellable_set_error_if_cancelled (cancellable, perror)) { |
| 294 |
g_object_unref (conn); |
| 295 |
return; |
| 296 |
} |
| 297 |
|
| 298 |
if (g_strcmp0 (cffd->use_foldername, "freebusy-calendar") == 0) { |
| 299 |
EEWSFreeBusyData fbdata; |
| 300 |
GSList *free_busy = NULL; |
| 301 |
gchar *tmp; |
| 302 |
gboolean success; |
| 303 |
|
| 304 |
fbdata.period_start = time (NULL); |
| 305 |
fbdata.period_end = fbdata.period_start + (60 * 60); |
| 306 |
fbdata.user_mails = g_slist_prepend (NULL, cffd->email); |
| 307 |
|
| 308 |
success = e_ews_connection_get_free_busy_sync (conn, G_PRIORITY_DEFAULT, |
| 309 |
e_ews_cal_utils_prepare_free_busy_request, &fbdata, |
| 310 |
&free_busy, cancellable, perror); |
| 311 |
|
| 312 |
g_slist_free_full (free_busy, g_object_unref); |
| 313 |
g_slist_free (fbdata.user_mails); |
| 314 |
|
| 315 |
if (!success) { |
| 316 |
g_object_unref (conn); |
| 317 |
return; |
| 318 |
} |
| 319 |
|
| 320 |
tmp = g_strconcat (cffd->use_foldername, "::", cffd->email, NULL); |
| 321 |
|
| 322 |
folder = g_object_new (E_TYPE_EWS_FOLDER, NULL); |
| 323 |
e_ews_folder_set_id (folder, e_ews_folder_id_new (tmp, NULL, FALSE)); |
| 324 |
/* Translators: This is used as a calendar name; it constructs "User Name - Availability" string shown in UI */ |
| 325 |
e_ews_folder_set_name (folder, _("Availability")); |
| 326 |
e_ews_folder_set_folder_type (folder, E_EWS_FOLDER_TYPE_CALENDAR); |
| 327 |
e_ews_folder_set_foreign_mail (folder, cffd->email); |
| 328 |
|
| 329 |
g_free (tmp); |
| 330 |
} else { |
| 331 |
fid.id = (gchar *) (cffd->use_foldername ? cffd->use_foldername : cffd->orig_foldername); |
| 332 |
fid.change_key = NULL; |
| 333 |
fid.is_distinguished_id = cffd->use_foldername != NULL || (cffd->orig_foldername && strlen (cffd->orig_foldername) < 40); |
| 334 |
|
| 335 |
if (!e_ews_connection_get_folder_info_sync (conn, G_PRIORITY_DEFAULT, |
| 336 |
cffd->email, &fid, &folder, cancellable, &local_error)) { |
| 337 |
if (!local_error || |
| 338 |
g_error_matches (local_error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_ITEMNOTFOUND) || |
| 339 |
g_error_matches (local_error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_FOLDERNOTFOUND)) { |
| 340 |
g_clear_error (&local_error); |
| 341 |
local_error = g_error_new ( |
| 342 |
EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_FOLDERNOTFOUND, |
| 343 |
_("Folder “%s” not found. Either it does not exist or you do not have permission to access it."), |
| 344 |
cffd->orig_foldername); |
| 345 |
} |
| 346 |
|
| 347 |
g_propagate_error (perror, local_error); |
| 348 |
g_object_unref (conn); |
| 349 |
return; |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
if (g_cancellable_set_error_if_cancelled (cancellable, perror)) { |
| 354 |
g_object_unref (folder); |
| 355 |
g_object_unref (conn); |
| 356 |
return; |
| 357 |
} |
| 358 |
|
| 359 |
if (e_ews_folder_get_folder_type (folder) == E_EWS_FOLDER_TYPE_UNKNOWN) { |
| 360 |
g_propagate_error ( |
| 361 |
perror, g_error_new_literal (EWS_CONNECTION_ERROR, |
| 362 |
EWS_CONNECTION_ERROR_FOLDERNOTFOUND, _("Cannot add folder, cannot determine folder’s type"))); |
| 363 |
g_object_unref (folder); |
| 364 |
g_object_unref (conn); |
| 365 |
return; |
| 366 |
} |
| 367 |
|
| 368 |
e_ews_folder_set_foreign (folder, TRUE); |
| 369 |
|
| 370 |
cffd->folder = folder; |
| 371 |
g_object_unref (conn); |
| 372 |
} |
| 373 |
|
| 374 |
static void |
| 375 |
check_foreign_folder_idle (GObject *with_object, |
| 376 |
gpointer user_data, |
| 377 |
GCancellable *cancellable, |
| 378 |
GError **perror) |
| 379 |
{ |
| 380 |
struct EEwsCheckForeignFolderData *cffd = user_data; |
| 381 |
|
| 382 |
g_return_if_fail (with_object != NULL); |
| 383 |
g_return_if_fail (CAMEL_IS_EWS_STORE (with_object)); |
| 384 |
g_return_if_fail (user_data != NULL); |
| 385 |
g_return_if_fail (cffd->email != NULL); |
| 386 |
|
| 387 |
if (!cffd->folder) |
| 388 |
return; |
| 389 |
|
| 390 |
if (!e_ews_subscrive_foreign_folder_subscribe_sync (CAMEL_EWS_STORE (with_object), |
| 391 |
cffd->folder, cffd->user_displayname, cffd->email, cffd->orig_foldername, |
| 392 |
cffd->include_subfolders, cancellable, perror)) { |
| 393 |
/* to not destroy the dialog on error */ |
| 394 |
g_object_unref (cffd->folder); |
| 395 |
cffd->folder = NULL; |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
static gpointer |
| 400 |
ref_selected_store (GObject *dialog) |
| 401 |
{ |
| 402 |
GtkComboBox *combo_box; |
| 403 |
CamelStore *store = NULL; |
| 404 |
GtkTreeIter iter; |
| 405 |
|
| 406 |
combo_box = g_object_get_data (dialog, STR_ACCOUNTS_COMBO); |
| 407 |
g_return_val_if_fail (combo_box != NULL, NULL); |
| 408 |
|
| 409 |
if (gtk_combo_box_get_active_iter (combo_box, &iter)) { |
| 410 |
gtk_tree_model_get (gtk_combo_box_get_model (combo_box), &iter, |
| 411 |
COLUMN_STORE, &store, -1); |
| 412 |
} |
| 413 |
|
| 414 |
return store; |
| 415 |
} |
| 416 |
|
| 417 |
static void |
| 418 |
subscribe_foreign_response_cb (GObject *dialog, |
| 419 |
gint response_id) |
| 420 |
{ |
| 421 |
struct EEwsCheckForeignFolderData *cffd; |
| 422 |
ENameSelectorEntry *entry; |
| 423 |
GtkComboBoxText *combo_text; |
| 424 |
GtkToggleButton *subfolders_check; |
| 425 |
EDestinationStore *dest_store; |
| 426 |
CamelStore *cstore; |
| 427 |
gchar *description; |
| 428 |
const gchar *username; |
| 429 |
gchar *orig_foldername, *use_foldername = NULL, *show_foldername = NULL; |
| 430 |
|
| 431 |
if (response_id != GTK_RESPONSE_OK) { |
| 432 |
gtk_widget_destroy (GTK_WIDGET (dialog)); |
| 433 |
return; |
| 434 |
} |
| 435 |
|
| 436 |
g_return_if_fail (dialog != NULL); |
| 437 |
|
| 438 |
entry = g_object_get_data (dialog, STR_USER_NAME_SELECTOR_ENTRY); |
| 439 |
combo_text = g_object_get_data (dialog, STR_FOLDER_NAME_COMBO); |
| 440 |
subfolders_check = g_object_get_data (dialog, STR_SUBFOLDERS_CHECK); |
| 441 |
|
| 442 |
g_return_if_fail (entry != NULL); |
| 443 |
|
| 444 |
cstore = ref_selected_store (dialog); |
| 445 |
g_return_if_fail (cstore != NULL); |
| 446 |
|
| 447 |
username = NULL; |
| 448 |
dest_store = e_name_selector_entry_peek_destination_store (entry); |
| 449 |
if (dest_store && e_destination_store_get_destination_count (dest_store) > 0) { |
| 450 |
EDestination *dest; |
| 451 |
GList *dests = e_destination_store_list_destinations (dest_store); |
| 452 |
|
| 453 |
g_return_if_fail (dests != NULL); |
| 454 |
|
| 455 |
/* pick the first, there is no option to limit to only one destination */ |
| 456 |
dest = dests->data; |
| 457 |
if (dest) { |
| 458 |
username = e_destination_get_email (dest); |
| 459 |
if (!username || !*username) |
| 460 |
username = e_destination_get_name (dest); |
| 461 |
} |
| 462 |
|
| 463 |
g_list_free (dests); |
| 464 |
} |
| 465 |
|
| 466 |
if (!username || !*username) |
| 467 |
username = gtk_entry_get_text (GTK_ENTRY (entry)); |
| 468 |
|
| 469 |
orig_foldername = gtk_combo_box_text_get_active_text (combo_text); |
| 470 |
if (!orig_foldername) |
| 471 |
orig_foldername = g_strdup (""); |
| 472 |
|
| 473 |
/* convert well-known names to their non-localized form */ |
| 474 |
if (g_strcmp0 (orig_foldername, _("Inbox")) == 0) { |
| 475 |
use_foldername = g_strdup ("inbox"); |
| 476 |
} else if (g_strcmp0 (orig_foldername, _("Contacts")) == 0) { |
| 477 |
use_foldername = g_strdup ("contacts"); |
| 478 |
} else if (g_strcmp0 (orig_foldername, _("Calendar")) == 0) { |
| 479 |
use_foldername = g_strdup ("calendar"); |
| 480 |
} else if (g_strcmp0 (orig_foldername, _("Free/Busy as Calendar")) == 0) { |
| 481 |
use_foldername = g_strdup ("freebusy-calendar"); |
| 482 |
} else if (g_strcmp0 (orig_foldername, _("Memos")) == 0) { |
| 483 |
use_foldername = g_strdup ("notes"); |
| 484 |
} else if (g_strcmp0 (orig_foldername, _("Tasks")) == 0) { |
| 485 |
use_foldername = g_strdup ("tasks"); |
| 486 |
} else if (strlen (orig_foldername) > 13) { |
| 487 |
/* if it's a folder ID, then show only first 10 letters of it */ |
| 488 |
show_foldername = g_strdup_printf ("%.10s…", orig_foldername); |
| 489 |
} |
| 490 |
|
| 491 |
cffd = g_slice_new0 (struct EEwsCheckForeignFolderData); |
| 492 |
cffd->dialog = GTK_WIDGET (dialog); |
| 493 |
cffd->email = g_strdup (username ? username : ""); |
| 494 |
cffd->direct_email = g_strdup (g_object_get_data (G_OBJECT (entry), STR_EWS_DIRECT_EMAIL)); |
| 495 |
cffd->orig_foldername = orig_foldername; |
| 496 |
cffd->use_foldername = use_foldername; |
| 497 |
cffd->include_subfolders = gtk_toggle_button_get_active (subfolders_check); |
| 498 |
cffd->folder = NULL; |
| 499 |
|
| 500 |
description = g_strdup_printf ( |
| 501 |
_("Testing availability of folder “%s” of user “%s”, please wait…"), |
| 502 |
show_foldername ? show_foldername : cffd->orig_foldername, cffd->email); |
| 503 |
|
| 504 |
e_ews_config_utils_run_in_thread_with_feedback ( |
| 505 |
GTK_WINDOW (dialog), |
| 506 |
G_OBJECT (cstore), |
| 507 |
description, |
| 508 |
check_foreign_folder_thread, |
| 509 |
check_foreign_folder_idle, |
| 510 |
cffd, |
| 511 |
e_ews_check_foreign_folder_data_free); |
| 512 |
|
| 513 |
g_free (description); |
| 514 |
g_free (show_foldername); |
| 515 |
g_object_unref (cstore); |
| 516 |
} |
| 517 |
|
| 518 |
static void |
| 519 |
pick_gal_user_clicked_cb (GtkButton *button, |
| 520 |
GObject *dialog) |
| 521 |
{ |
| 522 |
GtkEntry *entry; |
| 523 |
CamelEwsStore *ews_store; |
| 524 |
EEwsConnection *conn; |
| 525 |
gchar *text, *display_name = NULL, *email = NULL; |
| 526 |
|
| 527 |
g_return_if_fail (dialog != NULL); |
| 528 |
|
| 529 |
entry = g_object_get_data (dialog, STR_USER_NAME_SELECTOR_ENTRY); |
| 530 |
|
| 531 |
g_return_if_fail (entry != NULL); |
| 532 |
|
| 533 |
ews_store = ref_selected_store (dialog); |
| 534 |
g_return_if_fail (ews_store != NULL); |
| 535 |
|
| 536 |
text = g_strstrip (g_strdup (gtk_entry_get_text (entry))); |
| 537 |
conn = camel_ews_store_ref_connection (ews_store); |
| 538 |
|
| 539 |
if (!conn) { |
| 540 |
e_notice (dialog, GTK_MESSAGE_ERROR, "%s", _("Cannot search for user when the account is offline")); |
| 541 |
} else if (e_ews_search_user_modal (GTK_WINDOW (dialog), conn, text, &display_name, &email)) { |
| 542 |
if (display_name && email && *email) { |
| 543 |
gtk_entry_set_text (entry, display_name); |
| 544 |
g_object_set_data_full (G_OBJECT (entry), STR_EWS_DIRECT_EMAIL, g_strdup (email), g_free); |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
g_free (text); |
| 549 |
g_free (display_name); |
| 550 |
g_free (email); |
| 551 |
g_object_unref (ews_store); |
| 552 |
g_clear_object (&conn); |
| 553 |
} |
| 554 |
|
| 555 |
static gint |
| 556 |
sort_accounts_by_display_name_cb (gconstpointer ptr1, |
| 557 |
gconstpointer ptr2) |
| 558 |
{ |
| 559 |
CamelService *service1 = (CamelService *) ptr1; |
| 560 |
CamelService *service2 = (CamelService *) ptr2; |
| 561 |
|
| 562 |
return g_utf8_collate (camel_service_get_display_name (service1), camel_service_get_display_name (service2)); |
| 563 |
} |
| 564 |
|
| 565 |
static GtkWidget * |
| 566 |
create_accounts_combo (CamelSession *session, |
| 567 |
EClientCache *client_cache, |
| 568 |
CamelStore *store) |
| 569 |
{ |
| 570 |
GtkListStore *list_store; |
| 571 |
GtkTreeIter iter; |
| 572 |
GtkComboBox *combo_box; |
| 573 |
ESourceRegistry *registry; |
| 574 |
GList *services, *link, *accounts = NULL; |
| 575 |
GtkCellRenderer *renderer; |
| 576 |
|
| 577 |
list_store = gtk_list_store_new (3, |
| 578 |
G_TYPE_STRING, /* COLUMN_UID - UID of the CamelEwsStore */ |
| 579 |
G_TYPE_STRING, /* COLUMN_DISPLAY_NAME */ |
| 580 |
CAMEL_TYPE_EWS_STORE); /* COLUMN_STORE */ |
| 581 |
|
| 582 |
registry = e_client_cache_ref_registry (client_cache); |
| 583 |
services = camel_session_list_services (session); |
| 584 |
|
| 585 |
for (link = services; link; link = g_list_next (link)) { |
| 586 |
CamelService *service = link->data; |
| 587 |
|
| 588 |
if (CAMEL_IS_EWS_STORE (service)) { |
| 589 |
ESource *source; |
| 590 |
|
| 591 |
source = e_source_registry_ref_source (registry, camel_service_get_uid (service)); |
| 592 |
if (source && e_source_registry_check_enabled (registry, source)) { |
| 593 |
accounts = g_list_prepend (accounts, service); |
| 594 |
} |
| 595 |
|
| 596 |
g_clear_object (&source); |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
accounts = g_list_sort (accounts, sort_accounts_by_display_name_cb); |
| 601 |
|
| 602 |
for (link = accounts; link; link = g_list_next (link)) { |
| 603 |
CamelService *service = link->data; |
| 604 |
|
| 605 |
gtk_list_store_append (list_store, &iter); |
| 606 |
gtk_list_store_set (list_store, &iter, |
| 607 |
COLUMN_UID, camel_service_get_uid (service), |
| 608 |
COLUMN_DISPLAY_NAME, camel_service_get_display_name (service), |
| 609 |
COLUMN_STORE, service, |
| 610 |
-1); |
| 611 |
} |
| 612 |
|
| 613 |
g_list_free_full (services, g_object_unref); |
| 614 |
g_list_free (accounts); |
| 615 |
g_clear_object (®istry); |
| 616 |
|
| 617 |
combo_box = GTK_COMBO_BOX (gtk_combo_box_new_with_model (GTK_TREE_MODEL (list_store))); |
| 618 |
g_object_unref (list_store); |
| 619 |
|
| 620 |
renderer = gtk_cell_renderer_text_new (); |
| 621 |
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, TRUE); |
| 622 |
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer, "text", COLUMN_DISPLAY_NAME, NULL); |
| 623 |
|
| 624 |
gtk_combo_box_set_id_column (combo_box, COLUMN_UID); |
| 625 |
if (store) |
| 626 |
gtk_combo_box_set_active_id (combo_box, camel_service_get_uid (CAMEL_SERVICE (store))); |
| 627 |
else if (accounts) |
| 628 |
gtk_combo_box_set_active (combo_box, 0); |
| 629 |
|
| 630 |
return GTK_WIDGET (combo_box); |
| 631 |
} |
| 632 |
|
| 633 |
/* Opens dialog to subscribe to folders of other |
| 634 |
* users in the given store */ |
| 635 |
void |
| 636 |
e_ews_subscribe_foreign_folder (GtkWindow *parent, |
| 637 |
CamelSession *session, |
| 638 |
CamelStore *store, |
| 639 |
EClientCache *client_cache) |
| 640 |
{ |
| 641 |
ENameSelector *name_selector; |
| 642 |
ENameSelectorModel *name_selector_model; |
| 643 |
ENameSelectorDialog *name_selector_dialog; |
| 644 |
GObject *dialog; |
| 645 |
GtkWidget *content; |
| 646 |
GtkWidget *label, *widget, *entry, *check, *accounts_combo; |
| 647 |
GtkGrid *grid; |
| 648 |
GtkComboBoxText *combo_text; |
| 649 |
gint row; |
| 650 |
|
| 651 |
g_return_if_fail (session != NULL); |
| 652 |
if (store) |
| 653 |
g_return_if_fail (CAMEL_IS_EWS_STORE (store)); |
| 654 |
g_return_if_fail (E_IS_CLIENT_CACHE (client_cache)); |
| 655 |
|
| 656 |
dialog = G_OBJECT (gtk_dialog_new_with_buttons ( |
| 657 |
_("Subscribe to folder of other EWS user…"), |
| 658 |
parent, |
| 659 |
GTK_DIALOG_DESTROY_WITH_PARENT, |
| 660 |
GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, |
| 661 |
GTK_STOCK_OK, GTK_RESPONSE_OK, |
| 662 |
NULL)); |
| 663 |
|
| 664 |
g_signal_connect (dialog, "response", G_CALLBACK (subscribe_foreign_response_cb), NULL); |
| 665 |
|
| 666 |
content = gtk_dialog_get_content_area (GTK_DIALOG (dialog)); |
| 667 |
|
| 668 |
grid = GTK_GRID (gtk_grid_new ()); |
| 669 |
gtk_grid_set_row_homogeneous (grid, FALSE); |
| 670 |
gtk_grid_set_row_spacing (grid, 6); |
| 671 |
gtk_grid_set_column_homogeneous (grid, FALSE); |
| 672 |
gtk_grid_set_column_spacing (grid, 6); |
| 673 |
gtk_container_set_border_width (GTK_CONTAINER (grid), 12); |
| 674 |
gtk_container_add (GTK_CONTAINER (content), GTK_WIDGET (grid)); |
| 675 |
|
| 676 |
row = 0; |
| 677 |
|
| 678 |
label = gtk_label_new (_("Account:")); |
| 679 |
g_object_set ( |
| 680 |
G_OBJECT (label), |
| 681 |
"hexpand", FALSE, |
| 682 |
"vexpand", FALSE, |
| 683 |
"xalign", 0.0, |
| 684 |
"halign", GTK_ALIGN_START, |
| 685 |
NULL); |
| 686 |
|
| 687 |
widget = create_accounts_combo (session, client_cache, store); |
| 688 |
g_object_set ( |
| 689 |
G_OBJECT (widget), |
| 690 |
"hexpand", TRUE, |
| 691 |
"vexpand", FALSE, |
| 692 |
"halign", GTK_ALIGN_START, |
| 693 |
NULL); |
| 694 |
accounts_combo = widget; |
| 695 |
|
| 696 |
gtk_grid_attach (grid, label, 0, row, 1, 1); |
| 697 |
gtk_grid_attach (grid, widget, 1, row, 2, 1); |
| 698 |
|
| 699 |
row++; |
| 700 |
|
| 701 |
name_selector = e_name_selector_new (client_cache); |
| 702 |
name_selector_model = e_name_selector_peek_model (name_selector); |
| 703 |
e_name_selector_model_add_section (name_selector_model, "User", _("User"), NULL); |
| 704 |
name_selector_dialog = e_name_selector_peek_dialog (name_selector); |
| 705 |
g_signal_connect (name_selector_dialog, "response", G_CALLBACK (gtk_widget_hide), name_selector); |
| 706 |
e_name_selector_load_books (name_selector); |
| 707 |
|
| 708 |
g_object_set_data_full (dialog, "e-ews-name-selector", name_selector, g_object_unref); |
| 709 |
|
| 710 |
label = gtk_label_new_with_mnemonic (_("_User:")); |
| 711 |
g_object_set ( |
| 712 |
G_OBJECT (label), |
| 713 |
"hexpand", FALSE, |
| 714 |
"vexpand", FALSE, |
| 715 |
"xalign", 0.0, |
| 716 |
NULL); |
| 717 |
|
| 718 |
entry = GTK_WIDGET (e_name_selector_peek_section_entry (name_selector, "User")); |
| 719 |
g_object_set ( |
| 720 |
G_OBJECT (entry), |
| 721 |
"hexpand", TRUE, |
| 722 |
"vexpand", FALSE, |
| 723 |
NULL); |
| 724 |
|
| 725 |
widget = gtk_button_new_with_mnemonic (_("C_hoose…")); |
| 726 |
g_object_set ( |
| 727 |
G_OBJECT (entry), |
| 728 |
"hexpand", TRUE, |
| 729 |
"vexpand", FALSE, |
| 730 |
NULL); |
| 731 |
|
| 732 |
gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry); |
| 733 |
g_signal_connect (widget, "clicked", G_CALLBACK (pick_gal_user_clicked_cb), dialog); |
| 734 |
|
| 735 |
gtk_grid_attach (grid, label, 0, row, 1, 1); |
| 736 |
gtk_grid_attach (grid, entry, 1, row, 1, 1); |
| 737 |
gtk_grid_attach (grid, widget, 2, row, 1, 1); |
| 738 |
|
| 739 |
row++; |
| 740 |
|
| 741 |
label = gtk_label_new_with_mnemonic (_("_Folder name:")); |
| 742 |
g_object_set ( |
| 743 |
G_OBJECT (label), |
| 744 |
"hexpand", FALSE, |
| 745 |
"vexpand", FALSE, |
| 746 |
"xalign", 0.0, |
| 747 |
NULL); |
| 748 |
|
| 749 |
widget = GTK_WIDGET ( |
| 750 |
g_object_new (gtk_combo_box_text_get_type (), |
| 751 |
"has-entry", TRUE, |
| 752 |
"entry-text-column", 0, |
| 753 |
"hexpand", TRUE, |
| 754 |
"vexpand", FALSE, |
| 755 |
NULL)); |
| 756 |
|
| 757 |
combo_text = GTK_COMBO_BOX_TEXT (widget); |
| 758 |
gtk_combo_box_text_append_text (combo_text, _("Inbox")); |
| 759 |
gtk_combo_box_text_append_text (combo_text, _("Contacts")); |
| 760 |
gtk_combo_box_text_append_text (combo_text, _("Calendar")); |
| 761 |
gtk_combo_box_text_append_text (combo_text, _("Free/Busy as Calendar")); |
| 762 |
gtk_combo_box_text_append_text (combo_text, _("Memos")); |
| 763 |
gtk_combo_box_text_append_text (combo_text, _("Tasks")); |
| 764 |
gtk_combo_box_set_active (GTK_COMBO_BOX (combo_text), 0); |
| 765 |
|
| 766 |
gtk_label_set_mnemonic_widget (GTK_LABEL (label), widget); |
| 767 |
gtk_grid_attach (grid, label, 0, row, 1, 1); |
| 768 |
gtk_grid_attach (grid, widget, 1, row, 2, 1); |
| 769 |
|
| 770 |
row++; |
| 771 |
|
| 772 |
check = gtk_check_button_new_with_mnemonic (_("Include _subfolders")); |
| 773 |
gtk_grid_attach (grid, check, 1, row, 2, 1); |
| 774 |
|
| 775 |
/* remember widgets for later use */ |
| 776 |
g_object_set_data (dialog, STR_ACCOUNTS_COMBO, accounts_combo); |
| 777 |
g_object_set_data (dialog, STR_USER_NAME_SELECTOR_ENTRY, entry); |
| 778 |
g_object_set_data (dialog, STR_FOLDER_NAME_COMBO, widget); |
| 779 |
g_object_set_data (dialog, STR_SUBFOLDERS_CHECK, check); |
| 780 |
|
| 781 |
g_object_set_data_full (dialog, STR_EWS_CAMEL_SESSION, g_object_ref (session), g_object_unref); |
| 782 |
|
| 783 |
g_signal_connect_swapped (entry, "changed", G_CALLBACK (name_entry_changed_cb), dialog); |
| 784 |
g_signal_connect_swapped (combo_text, "changed", G_CALLBACK (folder_name_combo_changed_cb), dialog); |
| 785 |
g_signal_connect_swapped (accounts_combo, "changed", G_CALLBACK (name_entry_changed_cb), dialog); |
| 786 |
|
| 787 |
name_entry_changed_cb (dialog); |
| 788 |
|
| 789 |
gtk_widget_show_all (content); |
| 790 |
gtk_widget_show (GTK_WIDGET (dialog)); |
| 791 |
} |
| 792 |
|
| 793 |
gboolean |
| 794 |
e_ews_subscribe_foreign_folder_resolve_name_sync (EEwsConnection *cnc, |
| 795 |
const gchar *name, |
| 796 |
gchar **out_display_name, |
| 797 |
gchar **out_email_address, |
| 798 |
GCancellable *cancellable, |
| 799 |
GError **error) |
| 800 |
{ |
| 801 |
GSList *mailboxes = NULL; |
| 802 |
EwsMailbox *mailbox = NULL; |
| 803 |
gboolean includes_last_item = FALSE; |
| 804 |
GError *local_error = NULL; |
| 805 |
|
| 806 |
if (!e_ews_connection_resolve_names_sync (cnc, G_PRIORITY_DEFAULT, |
| 807 |
name, EWS_SEARCH_AD, NULL, FALSE, |
| 808 |
&includes_last_item, &mailboxes, NULL, |
| 809 |
cancellable, &local_error)) { |
| 810 |
if (g_error_matches (local_error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_NAMERESOLUTIONNORESULTS) || |
| 811 |
g_error_matches (local_error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_NAMERESOLUTIONNOMAILBOX)) { |
| 812 |
g_clear_error (&local_error); |
| 813 |
mailboxes = NULL; |
| 814 |
} else { |
| 815 |
if (local_error) |
| 816 |
g_propagate_error (error, local_error); |
| 817 |
return FALSE; |
| 818 |
} |
| 819 |
} |
| 820 |
|
| 821 |
if (mailboxes) { |
| 822 |
/* is there only one result? */ |
| 823 |
if (!mailboxes->next) { |
| 824 |
mailbox = mailboxes->data; |
| 825 |
} else { |
| 826 |
GSList *iter; |
| 827 |
|
| 828 |
for (iter = mailboxes; iter; iter = iter->next) { |
| 829 |
EwsMailbox *mb = iter->data; |
| 830 |
|
| 831 |
if (!mb) |
| 832 |
continue; |
| 833 |
|
| 834 |
if (mb->name && g_utf8_collate (mb->name, name) == 0) { |
| 835 |
mailbox = mb; |
| 836 |
break; |
| 837 |
} |
| 838 |
} |
| 839 |
} |
| 840 |
|
| 841 |
if (mailbox) { |
| 842 |
if (out_display_name) |
| 843 |
*out_display_name = g_strdup (mailbox->name); |
| 844 |
|
| 845 |
if (out_email_address) |
| 846 |
*out_email_address = g_strdup (mailbox->email); |
| 847 |
} |
| 848 |
|
| 849 |
g_slist_free_full (mailboxes, (GDestroyNotify) e_ews_mailbox_free); |
| 850 |
|
| 851 |
if (!mailbox) { |
| 852 |
g_set_error ( |
| 853 |
error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_ITEMNOTFOUND, |
| 854 |
_("User name “%s” is ambiguous, specify it more precisely, please"), name); |
| 855 |
return FALSE; |
| 856 |
} |
| 857 |
} |
| 858 |
|
| 859 |
return TRUE; |
| 860 |
} |
| 861 |
|
| 862 |
gboolean |
| 863 |
e_ews_subscrive_foreign_folder_subscribe_sync (CamelEwsStore *ews_store, |
| 864 |
EEwsFolder *folder, |
| 865 |
const gchar *user_display_name, |
| 866 |
const gchar *user_email, |
| 867 |
const gchar *fallback_folder_name, |
| 868 |
gboolean include_subfolders, |
| 869 |
GCancellable *cancellable, |
| 870 |
GError **error) |
| 871 |
{ |
| 872 |
gchar *folder_name; |
| 873 |
const gchar *base_username, *base_foldername; |
| 874 |
CamelSettings *settings; |
| 875 |
CamelEwsSettings *ews_settings; |
| 876 |
ESourceRegistry *registry = NULL; |
| 877 |
CamelSession *session; |
| 878 |
EEwsFolderType folder_type; |
| 879 |
gboolean success; |
| 880 |
|
| 881 |
folder_type = e_ews_folder_get_folder_type (folder); |
| 882 |
base_username = user_display_name ? user_display_name : user_email; |
| 883 |
base_foldername = e_ews_folder_get_name (folder) ? e_ews_folder_get_name (folder) : fallback_folder_name; |
| 884 |
|
| 885 |
/* Translators: This is used to name foreign folder. |
| 886 |
* The first '%s' is replaced with user name to whom the folder belongs, |
| 887 |
* the second '%s' is replaced with folder name. |
| 888 |
* Example result: "John Smith — Calendar" |
| 889 |
*/ |
| 890 |
folder_name = g_strdup_printf (C_("ForeignFolder", "%s — %s"), base_username, base_foldername); |
| 891 |
if (folder_type != E_EWS_FOLDER_TYPE_MAILBOX) |
| 892 |
e_ews_folder_set_name (folder, folder_name); |
| 893 |
|
| 894 |
settings = camel_service_ref_settings (CAMEL_SERVICE (ews_store)); |
| 895 |
ews_settings = CAMEL_EWS_SETTINGS (settings); |
| 896 |
session = camel_service_ref_session (CAMEL_SERVICE (ews_store)); |
| 897 |
if (E_IS_MAIL_SESSION (session)) |
| 898 |
registry = e_mail_session_get_registry (E_MAIL_SESSION (session)); |
| 899 |
|
| 900 |
success = (folder_type == E_EWS_FOLDER_TYPE_MAILBOX && |
| 901 |
add_foreign_folder_to_camel (ews_store, |
| 902 |
user_email, |
| 903 |
folder, |
| 904 |
include_subfolders, |
| 905 |
base_username, |
| 906 |
base_foldername, |
| 907 |
error)) || |
| 908 |
(folder_type != E_EWS_FOLDER_TYPE_MAILBOX && e_ews_folder_utils_add_as_esource (registry, |
| 909 |
camel_ews_settings_get_hosturl (ews_settings), |
| 910 |
camel_network_settings_get_user (CAMEL_NETWORK_SETTINGS (ews_settings)), |
| 911 |
folder, |
| 912 |
(include_subfolders ? E_EWS_ESOURCE_FLAG_INCLUDE_SUBFOLDERS : 0) | E_EWS_ESOURCE_FLAG_OFFLINE_SYNC, |
| 913 |
0, |
| 914 |
cancellable, |
| 915 |
error)); |
| 916 |
|
| 917 |
g_free (folder_name); |
| 918 |
g_object_unref (session); |
| 919 |
g_object_unref (settings); |
| 920 |
|
| 921 |
return success; |
| 922 |
} |