| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: (C) 2020 Red Hat (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 |
|
| 10 |
#include <e-util/e-util.h> |
| 11 |
#include <mail/e-mail-display.h> |
| 12 |
#include <shell/e-shell-window.h> |
| 13 |
|
| 14 |
#include "camel/camel-ews-folder.h" |
| 15 |
#include "camel/camel-ews-store.h" |
| 16 |
|
| 17 |
#include "e-ews-subscribe-foreign-folder.h" |
| 18 |
|
| 19 |
#include "e-mail-part-ews-sharing-metadata.h" |
| 20 |
|
| 21 |
G_DEFINE_DYNAMIC_TYPE (EMailPartEwsSharingMetadata, e_mail_part_ews_sharing_metadata, E_TYPE_MAIL_PART) |
| 22 |
|
| 23 |
typedef struct _SubscribeData { |
| 24 |
CamelEwsStore *ews_store; |
| 25 |
EEwsConnection *cnc; |
| 26 |
ENamedParameters *params; |
| 27 |
} SubscribeData; |
| 28 |
|
| 29 |
static void |
| 30 |
subscribe_data_free (gpointer ptr) |
| 31 |
{ |
| 32 |
SubscribeData *sd = ptr; |
| 33 |
|
| 34 |
if (sd) { |
| 35 |
g_clear_object (&sd->ews_store); |
| 36 |
g_clear_object (&sd->cnc); |
| 37 |
e_named_parameters_free (sd->params); |
| 38 |
g_slice_free (SubscribeData, sd); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
static void |
| 43 |
ews_sharing_metadata_subscribe_thread (EAlertSinkThreadJobData *job_data, |
| 44 |
gpointer user_data, |
| 45 |
GCancellable *cancellable, |
| 46 |
GError **error) |
| 47 |
{ |
| 48 |
SubscribeData *sd = user_data; |
| 49 |
EwsFolderId fid; |
| 50 |
EEwsFolder *folder = NULL; |
| 51 |
const gchar *email; |
| 52 |
gchar *folder_id = NULL; |
| 53 |
gchar *display_name = NULL; |
| 54 |
GError *local_error = NULL; |
| 55 |
|
| 56 |
g_return_if_fail (sd != NULL); |
| 57 |
|
| 58 |
if (!sd->cnc) { |
| 59 |
g_set_error_literal (error, CAMEL_SERVICE_ERROR, CAMEL_SERVICE_ERROR_UNAVAILABLE, |
| 60 |
_("Cannot subscribe EWS folders in offline mode")); |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
email = e_named_parameters_get (sd->params, "email"); |
| 65 |
|
| 66 |
if (!e_ews_connection_convert_id_sync (sd->cnc, G_PRIORITY_DEFAULT, email, |
| 67 |
e_named_parameters_get (sd->params, "folder_id"), |
| 68 |
"HexEntryId", "EwsId", &folder_id, cancellable, error)) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
fid.id = folder_id; |
| 73 |
fid.change_key = NULL; |
| 74 |
fid.is_distinguished_id = FALSE; |
| 75 |
|
| 76 |
if (e_ews_connection_get_folder_info_sync (sd->cnc, G_PRIORITY_DEFAULT, email, &fid, &folder, cancellable, &local_error)) { |
| 77 |
if (e_ews_folder_get_folder_type (folder) == E_EWS_FOLDER_TYPE_UNKNOWN) { |
| 78 |
local_error = g_error_new_literal (EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_FOLDERNOTFOUND, |
| 79 |
_("Cannot add folder, cannot determine folder’s type")); |
| 80 |
} else { |
| 81 |
e_ews_folder_set_foreign (folder, TRUE); |
| 82 |
|
| 83 |
if (!e_ews_subscribe_foreign_folder_resolve_name_sync (sd->cnc, email, &display_name, NULL, cancellable, NULL)) |
| 84 |
display_name = NULL; |
| 85 |
|
| 86 |
e_ews_subscrive_foreign_folder_subscribe_sync (sd->ews_store, folder, |
| 87 |
display_name, email, _("Folder"), FALSE, cancellable, &local_error); |
| 88 |
} |
| 89 |
} else if (!local_error || |
| 90 |
g_error_matches (local_error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_ITEMNOTFOUND) || |
| 91 |
g_error_matches (local_error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_FOLDERNOTFOUND)) { |
| 92 |
g_clear_error (&local_error); |
| 93 |
local_error = g_error_new ( |
| 94 |
EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_FOLDERNOTFOUND, |
| 95 |
_("Folder “%s” not found. Either it does not exist or you do not have permission to access it."), |
| 96 |
e_named_parameters_get (sd->params, "folder_id")); |
| 97 |
} |
| 98 |
|
| 99 |
if (local_error) { |
| 100 |
g_propagate_error (error, local_error); |
| 101 |
} else { |
| 102 |
e_alert_sink_thread_job_set_alert_ident (job_data, "ews:folder-subscribe-info"); |
| 103 |
e_alert_sink_thread_job_set_alert_arg_0 (job_data, display_name ? display_name : email); |
| 104 |
|
| 105 |
/* Create a fake error, to give a feedback about successful subscribe of the folder */ |
| 106 |
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, ""); |
| 107 |
} |
| 108 |
|
| 109 |
g_clear_object (&folder); |
| 110 |
g_free (display_name); |
| 111 |
g_free (folder_id); |
| 112 |
} |
| 113 |
|
| 114 |
static void |
| 115 |
ews_sharing_metadata_btn_clicked_cb (EWebView *web_view, |
| 116 |
const gchar *iframe_id, |
| 117 |
const gchar *element_id, |
| 118 |
const gchar *element_class, |
| 119 |
const gchar *element_value, |
| 120 |
const GtkAllocation *element_position, |
| 121 |
gpointer user_data) |
| 122 |
{ |
| 123 |
EMailPartList *part_list; |
| 124 |
EAlertSink *alert_sink = NULL; |
| 125 |
EActivity *activity; |
| 126 |
CamelFolder *folder; |
| 127 |
CamelStore *store; |
| 128 |
GtkWidget *widget; |
| 129 |
SubscribeData *sd; |
| 130 |
|
| 131 |
if (!element_value || !*element_value || !E_IS_MAIL_DISPLAY (web_view)) |
| 132 |
return; |
| 133 |
|
| 134 |
part_list = e_mail_display_get_part_list (E_MAIL_DISPLAY (web_view)); |
| 135 |
folder = part_list ? e_mail_part_list_get_folder (part_list) : NULL; |
| 136 |
|
| 137 |
if (!CAMEL_IS_EWS_FOLDER (folder)) |
| 138 |
return; |
| 139 |
|
| 140 |
store = camel_folder_get_parent_store (folder); |
| 141 |
|
| 142 |
if (!CAMEL_IS_EWS_STORE (store)) |
| 143 |
return; |
| 144 |
|
| 145 |
widget = gtk_widget_get_toplevel (GTK_WIDGET (web_view)); |
| 146 |
|
| 147 |
if (E_IS_SHELL_WINDOW (widget)) |
| 148 |
alert_sink = E_ALERT_SINK (widget); |
| 149 |
|
| 150 |
if (!alert_sink) |
| 151 |
alert_sink = E_ALERT_SINK (web_view); |
| 152 |
|
| 153 |
sd = g_slice_new (SubscribeData); |
| 154 |
sd->ews_store = CAMEL_EWS_STORE (g_object_ref (store)); |
| 155 |
sd->cnc = camel_ews_store_ref_connection (CAMEL_EWS_STORE (store)); |
| 156 |
sd->params = e_named_parameters_new_string (element_value); |
| 157 |
|
| 158 |
activity = e_alert_sink_submit_thread_job (alert_sink, |
| 159 |
_("Subscribing EWS folder…"), "ews:folder-subscribe-error", NULL, |
| 160 |
ews_sharing_metadata_subscribe_thread, sd, subscribe_data_free); |
| 161 |
|
| 162 |
g_clear_object (&activity); |
| 163 |
} |
| 164 |
|
| 165 |
static void |
| 166 |
mail_part_ews_sharing_metadata_content_loaded (EMailPart *part, |
| 167 |
EWebView *web_view, |
| 168 |
const gchar *iframe_id) |
| 169 |
{ |
| 170 |
g_return_if_fail (E_IS_MAIL_PART_EWS_SHARING_METADATA (part)); |
| 171 |
g_return_if_fail (E_IS_WEB_VIEW (web_view)); |
| 172 |
|
| 173 |
if (g_strcmp0 ((iframe_id && *iframe_id) ? iframe_id : NULL, e_mail_part_get_id (part)) != 0) |
| 174 |
return; |
| 175 |
|
| 176 |
e_web_view_register_element_clicked (web_view, "ews-sharing-metadata-btn", |
| 177 |
ews_sharing_metadata_btn_clicked_cb, NULL); |
| 178 |
} |
| 179 |
|
| 180 |
static void |
| 181 |
mail_part_ews_sharing_metadata_finalize (GObject *object) |
| 182 |
{ |
| 183 |
EMailPartEwsSharingMetadata *part = E_MAIL_PART_EWS_SHARING_METADATA (object); |
| 184 |
|
| 185 |
g_clear_pointer (&part->xml, g_free); |
| 186 |
|
| 187 |
/* Chain up to parent's method. */ |
| 188 |
G_OBJECT_CLASS (e_mail_part_ews_sharing_metadata_parent_class)->finalize (object); |
| 189 |
} |
| 190 |
|
| 191 |
static void |
| 192 |
e_mail_part_ews_sharing_metadata_class_init (EMailPartEwsSharingMetadataClass *klass) |
| 193 |
{ |
| 194 |
GObjectClass *object_class; |
| 195 |
EMailPartClass *mail_part_class; |
| 196 |
|
| 197 |
object_class = G_OBJECT_CLASS (klass); |
| 198 |
object_class->finalize = mail_part_ews_sharing_metadata_finalize; |
| 199 |
|
| 200 |
mail_part_class = E_MAIL_PART_CLASS (klass); |
| 201 |
mail_part_class->content_loaded = mail_part_ews_sharing_metadata_content_loaded; |
| 202 |
} |
| 203 |
|
| 204 |
static void |
| 205 |
e_mail_part_ews_sharing_metadata_class_finalize (EMailPartEwsSharingMetadataClass *klass) |
| 206 |
{ |
| 207 |
} |
| 208 |
|
| 209 |
static void |
| 210 |
e_mail_part_ews_sharing_metadata_init (EMailPartEwsSharingMetadata *part) |
| 211 |
{ |
| 212 |
} |
| 213 |
|
| 214 |
void |
| 215 |
e_mail_part_ews_sharing_metadata_type_register (GTypeModule *type_module) |
| 216 |
{ |
| 217 |
/* XXX G_DEFINE_DYNAMIC_TYPE declares a static type registration |
| 218 |
* function, so we have to wrap it with a public function in |
| 219 |
* order to register types from a separate compilation unit. */ |
| 220 |
e_mail_part_ews_sharing_metadata_register_type (type_module); |
| 221 |
} |
| 222 |
|
| 223 |
EMailPart * |
| 224 |
e_mail_part_ews_sharing_metadata_new (CamelMimePart *mime_part, |
| 225 |
const gchar *id) |
| 226 |
{ |
| 227 |
g_return_val_if_fail (id != NULL, NULL); |
| 228 |
|
| 229 |
return g_object_new ( |
| 230 |
E_TYPE_MAIL_PART_EWS_SHARING_METADATA, |
| 231 |
"id", id, "mime-part", mime_part, NULL); |
| 232 |
} |