| 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 <string.h> |
| 9 |
#include <glib.h> |
| 10 |
#include <glib/gi18n-lib.h> |
| 11 |
#include <json-glib/json-glib.h> |
| 12 |
|
| 13 |
#include "camel-m365-settings.h" |
| 14 |
#include "e-ews-common-utils.h" |
| 15 |
#include "e-m365-json-utils.h" |
| 16 |
|
| 17 |
#include "e-m365-connection.h" |
| 18 |
|
| 19 |
#define LOCK(x) g_rec_mutex_lock (&(x->priv->property_lock)) |
| 20 |
#define UNLOCK(x) g_rec_mutex_unlock (&(x->priv->property_lock)) |
| 21 |
|
| 22 |
#define M365_HOSTNAME "graph.microsoft.com" |
| 23 |
#define M365_RETRY_IO_ERROR_SECONDS 3 |
| 24 |
#define X_EVO_M365_DATA "X-EVO-M365-DATA" |
| 25 |
|
| 26 |
#define ORG_CONTACTS_PROPS "addresses,companyName,department,displayName,givenName,id,jobTitle,mail,mailNickname,phones,proxyAddresses,surname" |
| 27 |
#define USERS_PROPS "aboutMe,birthday,businessPhones,city,companyName,country,createdDateTime,department,displayName,faxNumber,givenName," \ |
| 28 |
"id,imAddresses,jobTitle,mail,mailNickname,mobilePhone,mySite,officeLocation,otherMails,postalCode,proxyAddresses," \ |
| 29 |
"state,streetAddress,surname" |
| 30 |
|
| 31 |
G_DEFINE_QUARK (e-m365-error-quark, e_m365_error) |
| 32 |
|
| 33 |
typedef enum _CSMFlags { |
| 34 |
CSM_DEFAULT = 0, |
| 35 |
CSM_DISABLE_RESPONSE = 1 << 0 |
| 36 |
} CSMFlags; |
| 37 |
|
| 38 |
struct _EM365ConnectionPrivate { |
| 39 |
GRecMutex property_lock; |
| 40 |
|
| 41 |
ESource *source; |
| 42 |
CamelM365Settings *settings; |
| 43 |
SoupSession *soup_session; |
| 44 |
GProxyResolver *proxy_resolver; |
| 45 |
|
| 46 |
gchar *user; /* The default user for the URL */ |
| 47 |
gchar *impersonate_user; |
| 48 |
|
| 49 |
gchar *hash_key; /* in the opened connections hash */ |
| 50 |
|
| 51 |
/* How many microseconds to wait, until can execute a new request. |
| 52 |
This is to cover throttling and server unavailable responses. |
| 53 |
https://docs.microsoft.com/en-us/graph/best-practices-concept#handling-expected-errors */ |
| 54 |
gint64 backoff_for_usec; |
| 55 |
|
| 56 |
guint concurrent_connections; |
| 57 |
}; |
| 58 |
|
| 59 |
enum { |
| 60 |
PROP_0, |
| 61 |
PROP_PROXY_RESOLVER, |
| 62 |
PROP_SETTINGS, |
| 63 |
PROP_SOURCE, |
| 64 |
PROP_CONCURRENT_CONNECTIONS, |
| 65 |
PROP_USER, /* This one is hidden, write only */ |
| 66 |
PROP_USE_IMPERSONATION, /* This one is hidden, write only */ |
| 67 |
PROP_IMPERSONATE_USER /* This one is hidden, write only */ |
| 68 |
}; |
| 69 |
|
| 70 |
G_DEFINE_TYPE_WITH_PRIVATE (EM365Connection, e_m365_connection, G_TYPE_OBJECT) |
| 71 |
|
| 72 |
static GHashTable *opened_connections = NULL; |
| 73 |
G_LOCK_DEFINE_STATIC (opened_connections); |
| 74 |
|
| 75 |
static gboolean |
| 76 |
m365_log_enabled (void) |
| 77 |
{ |
| 78 |
static gint log_enabled = -1; |
| 79 |
|
| 80 |
if (log_enabled == -1) |
| 81 |
log_enabled = g_strcmp0 (g_getenv ("M365_DEBUG"), "1") == 0 ? 1 : 0; |
| 82 |
|
| 83 |
return log_enabled == 1; |
| 84 |
} |
| 85 |
|
| 86 |
static gchar * |
| 87 |
m365_connection_construct_hash_key (CamelM365Settings *settings) |
| 88 |
{ |
| 89 |
gchar *user; |
| 90 |
gchar *hash_key = NULL; |
| 91 |
|
| 92 |
user = camel_network_settings_dup_user (CAMEL_NETWORK_SETTINGS (settings)); |
| 93 |
|
| 94 |
if (camel_m365_settings_get_use_impersonation (settings)) { |
| 95 |
gchar *impersonate_user; |
| 96 |
|
| 97 |
impersonate_user = camel_m365_settings_dup_impersonate_user (settings); |
| 98 |
|
| 99 |
if (impersonate_user && *impersonate_user) |
| 100 |
hash_key = g_strdup_printf ("%s#%s", impersonate_user, user ? user : "no-user"); |
| 101 |
|
| 102 |
g_free (impersonate_user); |
| 103 |
} |
| 104 |
|
| 105 |
if (!hash_key) |
| 106 |
hash_key = g_steal_pointer (&user); |
| 107 |
|
| 108 |
if (!hash_key) |
| 109 |
hash_key = g_strdup ("no-user"); |
| 110 |
|
| 111 |
g_free (user); |
| 112 |
|
| 113 |
return hash_key; |
| 114 |
} |
| 115 |
|
| 116 |
static void |
| 117 |
m365_connection_set_settings (EM365Connection *cnc, |
| 118 |
CamelM365Settings *settings) |
| 119 |
{ |
| 120 |
g_return_if_fail (E_IS_M365_CONNECTION (cnc)); |
| 121 |
g_return_if_fail (CAMEL_IS_M365_SETTINGS (settings)); |
| 122 |
g_return_if_fail (cnc->priv->settings == NULL); |
| 123 |
|
| 124 |
cnc->priv->settings = g_object_ref (settings); |
| 125 |
|
| 126 |
e_binding_bind_property ( |
| 127 |
cnc->priv->settings, "user", |
| 128 |
cnc, "user", |
| 129 |
G_BINDING_DEFAULT | |
| 130 |
G_BINDING_SYNC_CREATE); |
| 131 |
|
| 132 |
e_binding_bind_property ( |
| 133 |
cnc->priv->settings, "use-impersonation", |
| 134 |
cnc, "use-impersonation", |
| 135 |
G_BINDING_DEFAULT | |
| 136 |
G_BINDING_SYNC_CREATE); |
| 137 |
|
| 138 |
/* No need to G_BINDING_SYNC_CREATE, because the 'use-impersonation' already updated the value */ |
| 139 |
e_binding_bind_property ( |
| 140 |
cnc->priv->settings, "impersonate-user", |
| 141 |
cnc, "impersonate-user", |
| 142 |
G_BINDING_DEFAULT); |
| 143 |
|
| 144 |
e_binding_bind_property ( |
| 145 |
cnc->priv->settings, "concurrent-connections", |
| 146 |
cnc, "concurrent-connections", |
| 147 |
G_BINDING_DEFAULT | |
| 148 |
G_BINDING_SYNC_CREATE); |
| 149 |
} |
| 150 |
|
| 151 |
static void |
| 152 |
m365_connection_set_source (EM365Connection *cnc, |
| 153 |
ESource *source) |
| 154 |
{ |
| 155 |
g_return_if_fail (E_IS_M365_CONNECTION (cnc)); |
| 156 |
g_return_if_fail (E_IS_SOURCE (source)); |
| 157 |
g_return_if_fail (cnc->priv->source == NULL); |
| 158 |
|
| 159 |
cnc->priv->source = g_object_ref (source); |
| 160 |
} |
| 161 |
|
| 162 |
static void |
| 163 |
m365_connection_take_user (EM365Connection *cnc, |
| 164 |
gchar *user) |
| 165 |
{ |
| 166 |
g_return_if_fail (E_IS_M365_CONNECTION (cnc)); |
| 167 |
|
| 168 |
LOCK (cnc); |
| 169 |
|
| 170 |
if (!user || !*user) |
| 171 |
g_clear_pointer (&user, g_free); |
| 172 |
|
| 173 |
g_free (cnc->priv->user); |
| 174 |
cnc->priv->user = user; |
| 175 |
|
| 176 |
UNLOCK (cnc); |
| 177 |
} |
| 178 |
|
| 179 |
static void |
| 180 |
m365_connection_take_impersonate_user (EM365Connection *cnc, |
| 181 |
gchar *impersonate_user) |
| 182 |
{ |
| 183 |
g_return_if_fail (E_IS_M365_CONNECTION (cnc)); |
| 184 |
|
| 185 |
LOCK (cnc); |
| 186 |
|
| 187 |
if (!impersonate_user || !*impersonate_user || |
| 188 |
!camel_m365_settings_get_use_impersonation (cnc->priv->settings)) { |
| 189 |
g_clear_pointer (&impersonate_user, g_free); |
| 190 |
} |
| 191 |
|
| 192 |
if (g_strcmp0 (impersonate_user, cnc->priv->impersonate_user) != 0) { |
| 193 |
g_free (cnc->priv->impersonate_user); |
| 194 |
cnc->priv->impersonate_user = impersonate_user; |
| 195 |
} else { |
| 196 |
g_clear_pointer (&impersonate_user, g_free); |
| 197 |
} |
| 198 |
|
| 199 |
UNLOCK (cnc); |
| 200 |
} |
| 201 |
|
| 202 |
static void |
| 203 |
m365_connection_set_use_impersonation (EM365Connection *cnc, |
| 204 |
gboolean use_impersonation) |
| 205 |
{ |
| 206 |
g_return_if_fail (E_IS_M365_CONNECTION (cnc)); |
| 207 |
|
| 208 |
LOCK (cnc); |
| 209 |
|
| 210 |
if (!use_impersonation) |
| 211 |
m365_connection_take_impersonate_user (cnc, NULL); |
| 212 |
else |
| 213 |
m365_connection_take_impersonate_user (cnc, camel_m365_settings_dup_impersonate_user (cnc->priv->settings)); |
| 214 |
|
| 215 |
UNLOCK (cnc); |
| 216 |
} |
| 217 |
|
| 218 |
static void |
| 219 |
m365_connection_set_property (GObject *object, |
| 220 |
guint property_id, |
| 221 |
const GValue *value, |
| 222 |
GParamSpec *pspec) |
| 223 |
{ |
| 224 |
switch (property_id) { |
| 225 |
case PROP_PROXY_RESOLVER: |
| 226 |
e_m365_connection_set_proxy_resolver ( |
| 227 |
E_M365_CONNECTION (object), |
| 228 |
g_value_get_object (value)); |
| 229 |
return; |
| 230 |
|
| 231 |
case PROP_SETTINGS: |
| 232 |
m365_connection_set_settings ( |
| 233 |
E_M365_CONNECTION (object), |
| 234 |
g_value_get_object (value)); |
| 235 |
return; |
| 236 |
|
| 237 |
case PROP_SOURCE: |
| 238 |
m365_connection_set_source ( |
| 239 |
E_M365_CONNECTION (object), |
| 240 |
g_value_get_object (value)); |
| 241 |
return; |
| 242 |
|
| 243 |
case PROP_CONCURRENT_CONNECTIONS: |
| 244 |
e_m365_connection_set_concurrent_connections ( |
| 245 |
E_M365_CONNECTION (object), |
| 246 |
g_value_get_uint (value)); |
| 247 |
return; |
| 248 |
|
| 249 |
case PROP_USER: |
| 250 |
m365_connection_take_user ( |
| 251 |
E_M365_CONNECTION (object), |
| 252 |
g_value_dup_string (value)); |
| 253 |
return; |
| 254 |
|
| 255 |
case PROP_USE_IMPERSONATION: |
| 256 |
m365_connection_set_use_impersonation ( |
| 257 |
E_M365_CONNECTION (object), |
| 258 |
g_value_get_boolean (value)); |
| 259 |
return; |
| 260 |
|
| 261 |
case PROP_IMPERSONATE_USER: |
| 262 |
m365_connection_take_impersonate_user ( |
| 263 |
E_M365_CONNECTION (object), |
| 264 |
g_value_dup_string (value)); |
| 265 |
return; |
| 266 |
} |
| 267 |
|
| 268 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); |
| 269 |
} |
| 270 |
|
| 271 |
static void |
| 272 |
m365_connection_get_property (GObject *object, |
| 273 |
guint property_id, |
| 274 |
GValue *value, |
| 275 |
GParamSpec *pspec) |
| 276 |
{ |
| 277 |
switch (property_id) { |
| 278 |
case PROP_PROXY_RESOLVER: |
| 279 |
g_value_take_object ( |
| 280 |
value, |
| 281 |
e_m365_connection_ref_proxy_resolver ( |
| 282 |
E_M365_CONNECTION (object))); |
| 283 |
return; |
| 284 |
|
| 285 |
case PROP_SETTINGS: |
| 286 |
g_value_set_object ( |
| 287 |
value, |
| 288 |
e_m365_connection_get_settings ( |
| 289 |
E_M365_CONNECTION (object))); |
| 290 |
return; |
| 291 |
|
| 292 |
case PROP_SOURCE: |
| 293 |
g_value_set_object ( |
| 294 |
value, |
| 295 |
e_m365_connection_get_source ( |
| 296 |
E_M365_CONNECTION (object))); |
| 297 |
return; |
| 298 |
|
| 299 |
case PROP_CONCURRENT_CONNECTIONS: |
| 300 |
g_value_set_uint ( |
| 301 |
value, |
| 302 |
e_m365_connection_get_concurrent_connections ( |
| 303 |
E_M365_CONNECTION (object))); |
| 304 |
return; |
| 305 |
} |
| 306 |
|
| 307 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); |
| 308 |
} |
| 309 |
|
| 310 |
static void |
| 311 |
m365_connection_constructed (GObject *object) |
| 312 |
{ |
| 313 |
EM365Connection *cnc = E_M365_CONNECTION (object); |
| 314 |
ESourceExtension *extension; |
| 315 |
|
| 316 |
/* Chain up to parent's method. */ |
| 317 |
G_OBJECT_CLASS (e_m365_connection_parent_class)->constructed (object); |
| 318 |
|
| 319 |
cnc->priv->soup_session = g_object_new (E_TYPE_SOUP_SESSION, |
| 320 |
"source", cnc->priv->source, |
| 321 |
"handle-backoff-responses", FALSE, |
| 322 |
"max-conns", cnc->priv->concurrent_connections, |
| 323 |
"max-conns-per-host", cnc->priv->concurrent_connections, |
| 324 |
NULL); |
| 325 |
|
| 326 |
if (m365_log_enabled ()) { |
| 327 |
SoupLogger *logger = soup_logger_new (SOUP_LOGGER_LOG_BODY); |
| 328 |
|
| 329 |
soup_session_add_feature (cnc->priv->soup_session, SOUP_SESSION_FEATURE (logger)); |
| 330 |
|
| 331 |
g_object_unref (logger); |
| 332 |
} |
| 333 |
|
| 334 |
soup_session_add_feature_by_type (cnc->priv->soup_session, SOUP_TYPE_COOKIE_JAR); |
| 335 |
soup_session_add_feature_by_type (cnc->priv->soup_session, E_TYPE_SOUP_AUTH_BEARER); |
| 336 |
|
| 337 |
/* This can use only OAuth2 authentication, which should be set on the ESource */ |
| 338 |
if (soup_session_has_feature (cnc->priv->soup_session, SOUP_TYPE_AUTH_BASIC)) |
| 339 |
soup_session_remove_feature_by_type (cnc->priv->soup_session, SOUP_TYPE_AUTH_BASIC); |
| 340 |
if (soup_session_has_feature (cnc->priv->soup_session, SOUP_TYPE_AUTH_NTLM)) |
| 341 |
soup_session_remove_feature_by_type (cnc->priv->soup_session, SOUP_TYPE_AUTH_NTLM); |
| 342 |
if (soup_session_has_feature (cnc->priv->soup_session, SOUP_TYPE_AUTH_NEGOTIATE)) |
| 343 |
soup_session_remove_feature_by_type (cnc->priv->soup_session, SOUP_TYPE_AUTH_NEGOTIATE); |
| 344 |
soup_session_add_feature_by_type (cnc->priv->soup_session, E_TYPE_SOUP_AUTH_BEARER); |
| 345 |
|
| 346 |
cnc->priv->hash_key = m365_connection_construct_hash_key (cnc->priv->settings); |
| 347 |
|
| 348 |
e_binding_bind_property ( |
| 349 |
cnc, "proxy-resolver", |
| 350 |
cnc->priv->soup_session, "proxy-resolver", |
| 351 |
G_BINDING_SYNC_CREATE); |
| 352 |
|
| 353 |
/* the ESoupSession can read timeout from the WebDAV Backend extension, |
| 354 |
thus make sure the options are in sync */ |
| 355 |
extension = e_source_get_extension (cnc->priv->source, E_SOURCE_EXTENSION_WEBDAV_BACKEND); |
| 356 |
e_binding_bind_property ( |
| 357 |
cnc->priv->settings, "timeout", |
| 358 |
extension, "timeout", |
| 359 |
G_BINDING_SYNC_CREATE); |
| 360 |
|
| 361 |
e_binding_bind_property ( |
| 362 |
cnc->priv->settings, "timeout", |
| 363 |
cnc->priv->soup_session, "timeout", |
| 364 |
G_BINDING_SYNC_CREATE); |
| 365 |
} |
| 366 |
|
| 367 |
static void |
| 368 |
m365_connection_dispose (GObject *object) |
| 369 |
{ |
| 370 |
EM365Connection *cnc = E_M365_CONNECTION (object); |
| 371 |
|
| 372 |
G_LOCK (opened_connections); |
| 373 |
|
| 374 |
/* Remove the connection from the opened connections */ |
| 375 |
if (opened_connections && |
| 376 |
g_hash_table_lookup (opened_connections, cnc->priv->hash_key) == (gpointer) object) { |
| 377 |
g_hash_table_remove (opened_connections, cnc->priv->hash_key); |
| 378 |
if (g_hash_table_size (opened_connections) == 0) { |
| 379 |
g_hash_table_destroy (opened_connections); |
| 380 |
opened_connections = NULL; |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
G_UNLOCK (opened_connections); |
| 385 |
|
| 386 |
LOCK (cnc); |
| 387 |
|
| 388 |
g_clear_object (&cnc->priv->source); |
| 389 |
g_clear_object (&cnc->priv->settings); |
| 390 |
g_clear_object (&cnc->priv->soup_session); |
| 391 |
g_clear_object (&cnc->priv->proxy_resolver); |
| 392 |
|
| 393 |
UNLOCK (cnc); |
| 394 |
|
| 395 |
/* Chain up to parent's method. */ |
| 396 |
G_OBJECT_CLASS (e_m365_connection_parent_class)->dispose (object); |
| 397 |
} |
| 398 |
|
| 399 |
static void |
| 400 |
m365_connection_finalize (GObject *object) |
| 401 |
{ |
| 402 |
EM365Connection *cnc = E_M365_CONNECTION (object); |
| 403 |
|
| 404 |
g_rec_mutex_clear (&cnc->priv->property_lock); |
| 405 |
g_clear_pointer (&cnc->priv->user, g_free); |
| 406 |
g_clear_pointer (&cnc->priv->impersonate_user, g_free); |
| 407 |
g_free (cnc->priv->hash_key); |
| 408 |
|
| 409 |
/* Chain up to parent's method. */ |
| 410 |
G_OBJECT_CLASS (e_m365_connection_parent_class)->finalize (object); |
| 411 |
} |
| 412 |
|
| 413 |
static void |
| 414 |
e_m365_connection_class_init (EM365ConnectionClass *class) |
| 415 |
{ |
| 416 |
GObjectClass *object_class; |
| 417 |
|
| 418 |
object_class = G_OBJECT_CLASS (class); |
| 419 |
object_class->set_property = m365_connection_set_property; |
| 420 |
object_class->get_property = m365_connection_get_property; |
| 421 |
object_class->constructed = m365_connection_constructed; |
| 422 |
object_class->dispose = m365_connection_dispose; |
| 423 |
object_class->finalize = m365_connection_finalize; |
| 424 |
|
| 425 |
g_object_class_install_property ( |
| 426 |
object_class, |
| 427 |
PROP_PROXY_RESOLVER, |
| 428 |
g_param_spec_object ( |
| 429 |
"proxy-resolver", |
| 430 |
"Proxy Resolver", |
| 431 |
"The proxy resolver for this backend", |
| 432 |
G_TYPE_PROXY_RESOLVER, |
| 433 |
G_PARAM_READWRITE | |
| 434 |
G_PARAM_STATIC_STRINGS)); |
| 435 |
|
| 436 |
g_object_class_install_property ( |
| 437 |
object_class, |
| 438 |
PROP_SETTINGS, |
| 439 |
g_param_spec_object ( |
| 440 |
"settings", |
| 441 |
"Settings", |
| 442 |
"Connection settings", |
| 443 |
CAMEL_TYPE_M365_SETTINGS, |
| 444 |
G_PARAM_READWRITE | |
| 445 |
G_PARAM_CONSTRUCT_ONLY | |
| 446 |
G_PARAM_STATIC_STRINGS)); |
| 447 |
|
| 448 |
g_object_class_install_property ( |
| 449 |
object_class, |
| 450 |
PROP_SOURCE, |
| 451 |
g_param_spec_object ( |
| 452 |
"source", |
| 453 |
"Source", |
| 454 |
"Corresponding ESource", |
| 455 |
E_TYPE_SOURCE, |
| 456 |
G_PARAM_READWRITE | |
| 457 |
G_PARAM_CONSTRUCT_ONLY | |
| 458 |
G_PARAM_STATIC_STRINGS)); |
| 459 |
|
| 460 |
g_object_class_install_property ( |
| 461 |
object_class, |
| 462 |
PROP_CONCURRENT_CONNECTIONS, |
| 463 |
g_param_spec_uint ( |
| 464 |
"concurrent-connections", |
| 465 |
"Concurrent Connections", |
| 466 |
"Number of concurrent connections to use", |
| 467 |
MIN_CONCURRENT_CONNECTIONS, |
| 468 |
MAX_CONCURRENT_CONNECTIONS, |
| 469 |
1, |
| 470 |
/* Do not construct it, otherwise it overrides the value derived from CamelM365Settings */ |
| 471 |
G_PARAM_READWRITE | |
| 472 |
G_PARAM_EXPLICIT_NOTIFY | |
| 473 |
G_PARAM_STATIC_STRINGS)); |
| 474 |
|
| 475 |
g_object_class_install_property ( |
| 476 |
object_class, |
| 477 |
PROP_USER, |
| 478 |
g_param_spec_string ( |
| 479 |
"user", |
| 480 |
NULL, |
| 481 |
NULL, |
| 482 |
NULL, |
| 483 |
G_PARAM_WRITABLE | |
| 484 |
G_PARAM_STATIC_STRINGS)); |
| 485 |
|
| 486 |
g_object_class_install_property ( |
| 487 |
object_class, |
| 488 |
PROP_USE_IMPERSONATION, |
| 489 |
g_param_spec_boolean ( |
| 490 |
"use-impersonation", |
| 491 |
NULL, |
| 492 |
NULL, |
| 493 |
FALSE, |
| 494 |
G_PARAM_WRITABLE | |
| 495 |
G_PARAM_STATIC_STRINGS)); |
| 496 |
|
| 497 |
g_object_class_install_property ( |
| 498 |
object_class, |
| 499 |
PROP_IMPERSONATE_USER, |
| 500 |
g_param_spec_string ( |
| 501 |
"impersonate-user", |
| 502 |
NULL, |
| 503 |
NULL, |
| 504 |
NULL, |
| 505 |
G_PARAM_WRITABLE | |
| 506 |
G_PARAM_STATIC_STRINGS)); |
| 507 |
} |
| 508 |
|
| 509 |
static void |
| 510 |
e_m365_connection_init (EM365Connection *cnc) |
| 511 |
{ |
| 512 |
cnc->priv = e_m365_connection_get_instance_private (cnc); |
| 513 |
|
| 514 |
g_rec_mutex_init (&cnc->priv->property_lock); |
| 515 |
|
| 516 |
cnc->priv->backoff_for_usec = 0; |
| 517 |
} |
| 518 |
|
| 519 |
gboolean |
| 520 |
e_m365_connection_util_delta_token_failed (const GError *error) |
| 521 |
{ |
| 522 |
return g_error_matches (error, E_SOUP_SESSION_ERROR, SOUP_STATUS_UNAUTHORIZED) || |
| 523 |
g_error_matches (error, E_SOUP_SESSION_ERROR, SOUP_STATUS_BAD_REQUEST) || |
| 524 |
g_error_matches (error, E_M365_ERROR, E_M365_ERROR_SYNC_STATE_NOT_FOUND) || |
| 525 |
/* Returned by the OAuth2 service when the token cannot be refreshed */ |
| 526 |
g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_REFUSED); |
| 527 |
} |
| 528 |
|
| 529 |
void |
| 530 |
e_m365_connection_util_set_message_status_code (SoupMessage *message, |
| 531 |
gint status_code) |
| 532 |
{ |
| 533 |
g_return_if_fail (SOUP_IS_MESSAGE (message)); |
| 534 |
|
| 535 |
g_object_set_data (G_OBJECT (message), "m365-batch-status-code", |
| 536 |
GINT_TO_POINTER (status_code)); |
| 537 |
} |
| 538 |
|
| 539 |
gint |
| 540 |
e_m365_connection_util_get_message_status_code (SoupMessage *message) |
| 541 |
{ |
| 542 |
gint status_code; |
| 543 |
|
| 544 |
g_return_val_if_fail (SOUP_IS_MESSAGE (message), -1); |
| 545 |
|
| 546 |
status_code = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (message), "m365-batch-status-code")); |
| 547 |
|
| 548 |
if (!status_code) |
| 549 |
status_code = soup_message_get_status (message); |
| 550 |
|
| 551 |
return status_code; |
| 552 |
} |
| 553 |
|
| 554 |
/* An EM365ConnectionRawDataFunc function, which writes the received data into the provided CamelStream */ |
| 555 |
gboolean |
| 556 |
e_m365_connection_util_read_raw_data_cb (EM365Connection *cnc, |
| 557 |
SoupMessage *message, |
| 558 |
GInputStream *raw_data_stream, |
| 559 |
gpointer user_data, /* CamelStream * */ |
| 560 |
GCancellable *cancellable, |
| 561 |
GError **error) |
| 562 |
{ |
| 563 |
CamelStream *cache_stream = user_data; |
| 564 |
gssize expected_size = 0, wrote_size = 0, last_percent = -1; |
| 565 |
gint last_progress_notify = 0; |
| 566 |
gsize buffer_size = 65535; |
| 567 |
gchar *buffer; |
| 568 |
gboolean success; |
| 569 |
|
| 570 |
g_return_val_if_fail (CAMEL_IS_STREAM (cache_stream), FALSE); |
| 571 |
g_return_val_if_fail (G_IS_INPUT_STREAM (raw_data_stream), FALSE); |
| 572 |
|
| 573 |
if (message && soup_message_get_response_headers (message)) { |
| 574 |
const gchar *content_length_str; |
| 575 |
|
| 576 |
content_length_str = soup_message_headers_get_one (soup_message_get_response_headers (message), "Content-Length"); |
| 577 |
|
| 578 |
if (content_length_str && *content_length_str) |
| 579 |
expected_size = (gssize) g_ascii_strtoll (content_length_str, NULL, 10); |
| 580 |
} |
| 581 |
|
| 582 |
buffer = g_malloc (buffer_size); |
| 583 |
|
| 584 |
do { |
| 585 |
success = !g_cancellable_set_error_if_cancelled (cancellable, error); |
| 586 |
|
| 587 |
if (success) { |
| 588 |
gssize n_read, n_wrote; |
| 589 |
|
| 590 |
n_read = g_input_stream_read (raw_data_stream, buffer, buffer_size, cancellable, error); |
| 591 |
|
| 592 |
if (n_read == -1) { |
| 593 |
success = FALSE; |
| 594 |
} else if (!n_read) { |
| 595 |
break; |
| 596 |
} else { |
| 597 |
n_wrote = camel_stream_write (cache_stream, buffer, n_read, cancellable, error); |
| 598 |
success = n_read == n_wrote; |
| 599 |
|
| 600 |
if (success && expected_size > 0) { |
| 601 |
gssize percent; |
| 602 |
|
| 603 |
wrote_size += n_wrote; |
| 604 |
|
| 605 |
percent = wrote_size * 100.0 / expected_size; |
| 606 |
|
| 607 |
if (percent > 100) |
| 608 |
percent = 100; |
| 609 |
|
| 610 |
if (percent != last_percent) { |
| 611 |
gint64 now = g_get_monotonic_time (); |
| 612 |
|
| 613 |
/* Notify only 10 times per second, not more */ |
| 614 |
if (percent == 100 || now - last_progress_notify > G_USEC_PER_SEC / 10) { |
| 615 |
last_progress_notify = now; |
| 616 |
last_percent = percent; |
| 617 |
|
| 618 |
camel_operation_progress (cancellable, percent); |
| 619 |
} |
| 620 |
} |
| 621 |
} |
| 622 |
} |
| 623 |
} |
| 624 |
} while (success); |
| 625 |
|
| 626 |
g_free (buffer); |
| 627 |
|
| 628 |
if (success) |
| 629 |
camel_stream_flush (cache_stream, cancellable, NULL); |
| 630 |
|
| 631 |
return success; |
| 632 |
} |
| 633 |
|
| 634 |
static gboolean |
| 635 |
m365_connection_util_reencode_one_part_to_base64_sync (CamelMimePart *part, |
| 636 |
CamelDataWrapper *containee, |
| 637 |
GCancellable *cancellable, |
| 638 |
GError **error) |
| 639 |
{ |
| 640 |
CamelStream *stream; |
| 641 |
gboolean success; |
| 642 |
|
| 643 |
if (!CAMEL_IS_MIME_MESSAGE (containee)) { |
| 644 |
switch (camel_mime_part_get_encoding (part)) { |
| 645 |
case CAMEL_TRANSFER_ENCODING_DEFAULT: |
| 646 |
case CAMEL_TRANSFER_ENCODING_BASE64: |
| 647 |
/* no need to re-encode */ |
| 648 |
return TRUE; |
| 649 |
default: |
| 650 |
break; |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
stream = camel_stream_mem_new (); |
| 655 |
success = camel_data_wrapper_decode_to_stream_sync (containee, stream, cancellable, error) != -1; |
| 656 |
|
| 657 |
if (success) { |
| 658 |
GByteArray *byte_array; |
| 659 |
CamelContentType *ct; |
| 660 |
gchar *mime_type; |
| 661 |
|
| 662 |
ct = camel_data_wrapper_get_mime_type_field (CAMEL_DATA_WRAPPER (part)); |
| 663 |
mime_type = camel_content_type_format (ct); |
| 664 |
byte_array = camel_stream_mem_get_byte_array (CAMEL_STREAM_MEM (stream)); |
| 665 |
|
| 666 |
camel_mime_part_set_encoding (part, CAMEL_TRANSFER_ENCODING_BASE64); |
| 667 |
camel_mime_part_set_content (part, (const gchar *) byte_array->data, (gint) byte_array->len, mime_type); |
| 668 |
|
| 669 |
g_free (mime_type); |
| 670 |
} |
| 671 |
|
| 672 |
g_object_unref (stream); |
| 673 |
|
| 674 |
return success; |
| 675 |
} |
| 676 |
|
| 677 |
/* The server re-encodes the HTML messages into base64, but as of 2024-07-11 they do not |
| 678 |
decode quoted-printable encoding properly, thus re-encode to base64 instead. |
| 679 |
The multipart/signed messages are not affected, luckily (it would break the signature anyway). */ |
| 680 |
gboolean |
| 681 |
e_m365_connection_util_reencode_parts_to_base64_sync (CamelMimePart *part, /* it can be a CamelMimeMessage */ |
| 682 |
GCancellable *cancellable, |
| 683 |
GError **error) |
| 684 |
{ |
| 685 |
CamelDataWrapper *containee; |
| 686 |
|
| 687 |
if (CAMEL_IS_MULTIPART_SIGNED (part)) { |
| 688 |
/* Microsoft does not re-encode these */ |
| 689 |
return TRUE; |
| 690 |
} |
| 691 |
|
| 692 |
if (g_cancellable_set_error_if_cancelled (cancellable, error)) |
| 693 |
return FALSE; |
| 694 |
|
| 695 |
containee = camel_medium_get_content (CAMEL_MEDIUM (part)); |
| 696 |
|
| 697 |
if (containee == NULL) |
| 698 |
return TRUE; |
| 699 |
|
| 700 |
/* using the object types is more accurate than using the mime/types */ |
| 701 |
if (CAMEL_IS_MULTIPART_SIGNED (containee)) { |
| 702 |
/* Microsoft does not re-encode these */ |
| 703 |
return TRUE; |
| 704 |
} else if (CAMEL_IS_MULTIPART (containee)) { |
| 705 |
gint ii, parts; |
| 706 |
|
| 707 |
parts = camel_multipart_get_number (CAMEL_MULTIPART (containee)); |
| 708 |
for (ii = 0; ii < parts; ii++) { |
| 709 |
CamelMimePart *mpart = camel_multipart_get_part (CAMEL_MULTIPART (containee), ii); |
| 710 |
|
| 711 |
if (!e_m365_connection_util_reencode_parts_to_base64_sync (mpart, cancellable, error)) |
| 712 |
return FALSE; |
| 713 |
} |
| 714 |
} else if (CAMEL_IS_MIME_MESSAGE (containee)) { |
| 715 |
if (!e_m365_connection_util_reencode_parts_to_base64_sync (CAMEL_MIME_PART (containee), cancellable, error)) |
| 716 |
return FALSE; |
| 717 |
} else { |
| 718 |
if (!m365_connection_util_reencode_one_part_to_base64_sync (part, containee, cancellable, error)) |
| 719 |
return FALSE; |
| 720 |
} |
| 721 |
|
| 722 |
return TRUE; |
| 723 |
} |
| 724 |
|
| 725 |
EM365Connection * |
| 726 |
e_m365_connection_new (ESource *source, |
| 727 |
CamelM365Settings *settings) |
| 728 |
{ |
| 729 |
g_return_val_if_fail (E_IS_SOURCE (source), NULL); |
| 730 |
g_return_val_if_fail (CAMEL_IS_M365_SETTINGS (settings), NULL); |
| 731 |
|
| 732 |
return e_m365_connection_new_full (source, settings, TRUE); |
| 733 |
} |
| 734 |
|
| 735 |
EM365Connection * |
| 736 |
e_m365_connection_new_for_backend (EBackend *backend, |
| 737 |
ESourceRegistry *registry, |
| 738 |
ESource *source, |
| 739 |
CamelM365Settings *settings) |
| 740 |
{ |
| 741 |
ESource *backend_source, *parent_source; |
| 742 |
|
| 743 |
g_return_val_if_fail (E_IS_BACKEND (backend), NULL); |
| 744 |
g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL); |
| 745 |
g_return_val_if_fail (E_IS_SOURCE (source), NULL); |
| 746 |
g_return_val_if_fail (CAMEL_IS_M365_SETTINGS (settings), NULL); |
| 747 |
|
| 748 |
backend_source = e_backend_get_source (backend); |
| 749 |
|
| 750 |
if (!backend_source) |
| 751 |
return e_m365_connection_new (source, settings); |
| 752 |
|
| 753 |
parent_source = e_source_registry_find_extension (registry, source, E_SOURCE_EXTENSION_COLLECTION); |
| 754 |
|
| 755 |
if (parent_source) { |
| 756 |
EM365Connection *cnc; |
| 757 |
|
| 758 |
cnc = e_m365_connection_new (parent_source, settings); |
| 759 |
|
| 760 |
g_object_unref (parent_source); |
| 761 |
|
| 762 |
return cnc; |
| 763 |
} |
| 764 |
|
| 765 |
return e_m365_connection_new (source, settings); |
| 766 |
} |
| 767 |
|
| 768 |
EM365Connection * |
| 769 |
e_m365_connection_new_full (ESource *source, |
| 770 |
CamelM365Settings *settings, |
| 771 |
gboolean allow_reuse) |
| 772 |
{ |
| 773 |
EM365Connection *cnc; |
| 774 |
|
| 775 |
if (allow_reuse) { |
| 776 |
gchar *hash_key = m365_connection_construct_hash_key (settings); |
| 777 |
|
| 778 |
if (hash_key) { |
| 779 |
G_LOCK (opened_connections); |
| 780 |
|
| 781 |
if (opened_connections) { |
| 782 |
cnc = g_hash_table_lookup (opened_connections, hash_key); |
| 783 |
|
| 784 |
if (cnc) { |
| 785 |
g_object_ref (cnc); |
| 786 |
G_UNLOCK (opened_connections); |
| 787 |
|
| 788 |
g_free (hash_key); |
| 789 |
|
| 790 |
return cnc; |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
G_UNLOCK (opened_connections); |
| 795 |
} |
| 796 |
|
| 797 |
g_free (hash_key); |
| 798 |
} |
| 799 |
|
| 800 |
cnc = g_object_new (E_TYPE_M365_CONNECTION, |
| 801 |
"source", source, |
| 802 |
"settings", settings, |
| 803 |
NULL); |
| 804 |
|
| 805 |
if (allow_reuse && cnc->priv->hash_key) { |
| 806 |
G_LOCK (opened_connections); |
| 807 |
|
| 808 |
if (!opened_connections) |
| 809 |
opened_connections = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); |
| 810 |
|
| 811 |
g_hash_table_insert (opened_connections, g_strdup (cnc->priv->hash_key), cnc); |
| 812 |
|
| 813 |
G_UNLOCK (opened_connections); |
| 814 |
} |
| 815 |
|
| 816 |
return cnc; |
| 817 |
} |
| 818 |
|
| 819 |
ESource * |
| 820 |
e_m365_connection_get_source (EM365Connection *cnc) |
| 821 |
{ |
| 822 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), NULL); |
| 823 |
|
| 824 |
return cnc->priv->source; |
| 825 |
} |
| 826 |
|
| 827 |
CamelM365Settings * |
| 828 |
e_m365_connection_get_settings (EM365Connection *cnc) |
| 829 |
{ |
| 830 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), NULL); |
| 831 |
|
| 832 |
return cnc->priv->settings; |
| 833 |
} |
| 834 |
|
| 835 |
guint |
| 836 |
e_m365_connection_get_concurrent_connections (EM365Connection *cnc) |
| 837 |
{ |
| 838 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), 1); |
| 839 |
|
| 840 |
return cnc->priv->concurrent_connections; |
| 841 |
} |
| 842 |
|
| 843 |
void |
| 844 |
e_m365_connection_set_concurrent_connections (EM365Connection *cnc, |
| 845 |
guint concurrent_connections) |
| 846 |
{ |
| 847 |
guint current_cc; |
| 848 |
|
| 849 |
g_return_if_fail (E_IS_M365_CONNECTION (cnc)); |
| 850 |
|
| 851 |
concurrent_connections = CLAMP (concurrent_connections, MIN_CONCURRENT_CONNECTIONS, MAX_CONCURRENT_CONNECTIONS); |
| 852 |
|
| 853 |
current_cc = e_m365_connection_get_concurrent_connections (cnc); |
| 854 |
|
| 855 |
if (current_cc == concurrent_connections) |
| 856 |
return; |
| 857 |
|
| 858 |
/* Will be updated in the priv->soup_session the next time it's created, |
| 859 |
because "max-conns" is a construct-only property */ |
| 860 |
cnc->priv->concurrent_connections = concurrent_connections; |
| 861 |
|
| 862 |
g_object_notify (G_OBJECT (cnc), "concurrent-connections"); |
| 863 |
} |
| 864 |
|
| 865 |
GProxyResolver * |
| 866 |
e_m365_connection_ref_proxy_resolver (EM365Connection *cnc) |
| 867 |
{ |
| 868 |
GProxyResolver *proxy_resolver = NULL; |
| 869 |
|
| 870 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), NULL); |
| 871 |
|
| 872 |
LOCK (cnc); |
| 873 |
|
| 874 |
if (cnc->priv->proxy_resolver) |
| 875 |
proxy_resolver = g_object_ref (cnc->priv->proxy_resolver); |
| 876 |
|
| 877 |
UNLOCK (cnc); |
| 878 |
|
| 879 |
return proxy_resolver; |
| 880 |
} |
| 881 |
|
| 882 |
void |
| 883 |
e_m365_connection_set_proxy_resolver (EM365Connection *cnc, |
| 884 |
GProxyResolver *proxy_resolver) |
| 885 |
{ |
| 886 |
gboolean notify = FALSE; |
| 887 |
|
| 888 |
g_return_if_fail (E_IS_M365_CONNECTION (cnc)); |
| 889 |
|
| 890 |
LOCK (cnc); |
| 891 |
|
| 892 |
/* Emitting a "notify" signal unnecessarily might have |
| 893 |
* unwanted side effects like cancelling a SoupMessage. |
| 894 |
* Only emit if we now have a different GProxyResolver. */ |
| 895 |
|
| 896 |
if (proxy_resolver != cnc->priv->proxy_resolver) { |
| 897 |
g_clear_object (&cnc->priv->proxy_resolver); |
| 898 |
cnc->priv->proxy_resolver = proxy_resolver; |
| 899 |
|
| 900 |
if (proxy_resolver) |
| 901 |
g_object_ref (proxy_resolver); |
| 902 |
|
| 903 |
notify = TRUE; |
| 904 |
} |
| 905 |
|
| 906 |
UNLOCK (cnc); |
| 907 |
|
| 908 |
if (notify) |
| 909 |
g_object_notify (G_OBJECT (cnc), "proxy-resolver"); |
| 910 |
} |
| 911 |
|
| 912 |
static void |
| 913 |
m365_connection_request_cancelled_cb (GCancellable *cancellable, |
| 914 |
gpointer user_data) |
| 915 |
{ |
| 916 |
EFlag *flag = user_data; |
| 917 |
|
| 918 |
g_return_if_fail (flag != NULL); |
| 919 |
|
| 920 |
e_flag_set (flag); |
| 921 |
} |
| 922 |
|
| 923 |
/* An example error response: |
| 924 |
|
| 925 |
{ |
| 926 |
"error": { |
| 927 |
"code": "BadRequest", |
| 928 |
"message": "Parsing Select and Expand failed.", |
| 929 |
"innerError": { |
| 930 |
"request-id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", |
| 931 |
"date": "2020-06-10T13:44:43" |
| 932 |
} |
| 933 |
} |
| 934 |
} |
| 935 |
|
| 936 |
*/ |
| 937 |
static gboolean |
| 938 |
m365_connection_extract_error (JsonNode *node, |
| 939 |
guint status_code, |
| 940 |
GError **error) |
| 941 |
{ |
| 942 |
JsonObject *object; |
| 943 |
GQuark domain = E_SOUP_SESSION_ERROR; |
| 944 |
const gchar *code, *message; |
| 945 |
|
| 946 |
if (!node || !JSON_NODE_HOLDS_OBJECT (node)) |
| 947 |
return FALSE; |
| 948 |
|
| 949 |
object = e_m365_json_get_object_member (json_node_get_object (node), "error"); |
| 950 |
|
| 951 |
if (!object) |
| 952 |
return FALSE; |
| 953 |
|
| 954 |
code = e_m365_json_get_string_member (object, "code", NULL); |
| 955 |
message = e_m365_json_get_string_member (object, "message", NULL); |
| 956 |
|
| 957 |
if (!code && !message) |
| 958 |
return FALSE; |
| 959 |
|
| 960 |
if (!status_code || status_code == -1 || SOUP_STATUS_IS_SUCCESSFUL (status_code)) { |
| 961 |
domain = G_IO_ERROR; |
| 962 |
status_code = G_IO_ERROR_INVALID_DATA; |
| 963 |
} else if (g_strcmp0 (code, "ErrorInvalidUser") == 0) { |
| 964 |
status_code = SOUP_STATUS_UNAUTHORIZED; |
| 965 |
} else if (g_strcmp0 (code, "ErrorItemNotFound") == 0) { |
| 966 |
domain = E_M365_ERROR; |
| 967 |
status_code = E_M365_ERROR_ITEM_NOT_FOUND; |
| 968 |
} else if (g_strcmp0 (code, "ErrorInvalidIdMalformed") == 0) { |
| 969 |
domain = E_M365_ERROR; |
| 970 |
status_code = E_M365_ERROR_ID_MALFORMED; |
| 971 |
} else if (g_strcmp0 (code, "SyncStateNotFound") == 0) { |
| 972 |
domain = E_M365_ERROR; |
| 973 |
status_code = E_M365_ERROR_SYNC_STATE_NOT_FOUND; |
| 974 |
} |
| 975 |
|
| 976 |
if (code && message) |
| 977 |
g_set_error (error, domain, status_code, "%s: %s", code, message); |
| 978 |
else |
| 979 |
g_set_error_literal (error, domain, status_code, code ? code : message); |
| 980 |
|
| 981 |
return TRUE; |
| 982 |
} |
| 983 |
|
| 984 |
typedef gboolean (* EM365ResponseFunc) (EM365Connection *cnc, |
| 985 |
SoupMessage *message, |
| 986 |
GInputStream *input_stream, |
| 987 |
JsonNode *node, |
| 988 |
gpointer user_data, |
| 989 |
gchar **out_next_link, |
| 990 |
GCancellable *cancellable, |
| 991 |
GError **error); |
| 992 |
|
| 993 |
/* (transfer full) (nullable): Free the *out_node with json_node_unref(), if not NULL; |
| 994 |
It can return 'success', even when the *out_node is NULL. */ |
| 995 |
gboolean |
| 996 |
e_m365_connection_json_node_from_message (SoupMessage *message, |
| 997 |
GInputStream *input_stream, |
| 998 |
JsonNode **out_node, |
| 999 |
GCancellable *cancellable, |
| 1000 |
GError **error) |
| 1001 |
{ |
| 1002 |
JsonObject *message_json_object; |
| 1003 |
gboolean success = TRUE; |
| 1004 |
GError *local_error = NULL; |
| 1005 |
|
| 1006 |
g_return_val_if_fail (SOUP_IS_MESSAGE (message), FALSE); |
| 1007 |
g_return_val_if_fail (out_node != NULL, FALSE); |
| 1008 |
|
| 1009 |
*out_node = NULL; |
| 1010 |
|
| 1011 |
message_json_object = g_object_get_data (G_OBJECT (message), X_EVO_M365_DATA); |
| 1012 |
|
| 1013 |
if (message_json_object) { |
| 1014 |
*out_node = json_node_init_object (json_node_new (JSON_NODE_OBJECT), message_json_object); |
| 1015 |
|
| 1016 |
success = !m365_connection_extract_error (*out_node, e_m365_connection_util_get_message_status_code (message), &local_error); |
| 1017 |
} else { |
| 1018 |
const gchar *content_type; |
| 1019 |
|
| 1020 |
content_type = soup_message_get_response_headers (message) ? |
| 1021 |
soup_message_headers_get_content_type (soup_message_get_response_headers (message), NULL) : NULL; |
| 1022 |
|
| 1023 |
if (content_type && g_ascii_strcasecmp (content_type, "application/json") == 0) { |
| 1024 |
JsonParser *json_parser; |
| 1025 |
GByteArray *msg_bytes = NULL; |
| 1026 |
|
| 1027 |
json_parser = json_parser_new_immutable (); |
| 1028 |
|
| 1029 |
if (!input_stream) |
| 1030 |
msg_bytes = e_soup_session_util_get_message_bytes (message); |
| 1031 |
|
| 1032 |
if (input_stream) { |
| 1033 |
success = json_parser_load_from_stream (json_parser, input_stream, cancellable, error); |
| 1034 |
} else if (msg_bytes) { |
| 1035 |
success = json_parser_load_from_data (json_parser, (const gchar *) msg_bytes->data, msg_bytes->len, error); |
| 1036 |
} else { |
| 1037 |
/* This should not happen, it's for safety check only, thus the string is not localized */ |
| 1038 |
success = FALSE; |
| 1039 |
g_set_error_literal (&local_error, G_IO_ERROR, G_IO_ERROR_FAILED, "No JSON data found"); |
| 1040 |
} |
| 1041 |
|
| 1042 |
if (success) { |
| 1043 |
*out_node = json_parser_steal_root (json_parser); |
| 1044 |
|
| 1045 |
success = !m365_connection_extract_error (*out_node, e_m365_connection_util_get_message_status_code (message), &local_error); |
| 1046 |
} |
| 1047 |
|
| 1048 |
g_object_unref (json_parser); |
| 1049 |
} |
| 1050 |
} |
| 1051 |
|
| 1052 |
if (!success && *out_node) { |
| 1053 |
json_node_unref (*out_node); |
| 1054 |
*out_node = NULL; |
| 1055 |
} |
| 1056 |
|
| 1057 |
if (local_error) |
| 1058 |
g_propagate_error (error, local_error); |
| 1059 |
|
| 1060 |
return success; |
| 1061 |
} |
| 1062 |
|
| 1063 |
static gboolean |
| 1064 |
m365_connection_send_request_sync (EM365Connection *cnc, |
| 1065 |
SoupMessage *message, |
| 1066 |
EM365ResponseFunc response_func, |
| 1067 |
EM365ConnectionRawDataFunc raw_data_func, |
| 1068 |
gpointer func_user_data, |
| 1069 |
GCancellable *cancellable, |
| 1070 |
GError **error) |
| 1071 |
{ |
| 1072 |
SoupSession *soup_session; |
| 1073 |
gint need_retry_seconds = 5; |
| 1074 |
gboolean did_io_error_retry = FALSE; |
| 1075 |
gboolean success = FALSE, need_retry = TRUE; |
| 1076 |
|
| 1077 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 1078 |
g_return_val_if_fail (SOUP_IS_MESSAGE (message), FALSE); |
| 1079 |
g_return_val_if_fail (response_func != NULL || raw_data_func != NULL, FALSE); |
| 1080 |
g_return_val_if_fail (response_func == NULL || raw_data_func == NULL, FALSE); |
| 1081 |
|
| 1082 |
while (need_retry && !g_cancellable_is_cancelled (cancellable)) { |
| 1083 |
need_retry = FALSE; |
| 1084 |
|
| 1085 |
LOCK (cnc); |
| 1086 |
|
| 1087 |
if (cnc->priv->backoff_for_usec) { |
| 1088 |
EFlag *flag; |
| 1089 |
gint64 wait_ms; |
| 1090 |
gulong handler_id = 0; |
| 1091 |
|
| 1092 |
wait_ms = cnc->priv->backoff_for_usec / G_TIME_SPAN_MILLISECOND; |
| 1093 |
|
| 1094 |
UNLOCK (cnc); |
| 1095 |
|
| 1096 |
flag = e_flag_new (); |
| 1097 |
|
| 1098 |
if (cancellable) { |
| 1099 |
handler_id = g_cancellable_connect (cancellable, G_CALLBACK (m365_connection_request_cancelled_cb), |
| 1100 |
flag, NULL); |
| 1101 |
} |
| 1102 |
|
| 1103 |
while (wait_ms > 0 && !g_cancellable_is_cancelled (cancellable)) { |
| 1104 |
gint64 now = g_get_monotonic_time (); |
| 1105 |
gint left_minutes, left_seconds; |
| 1106 |
|
| 1107 |
left_minutes = wait_ms / 60000; |
| 1108 |
left_seconds = (wait_ms / 1000) % 60; |
| 1109 |
|
| 1110 |
if (left_minutes > 0) { |
| 1111 |
camel_operation_push_message (cancellable, |
| 1112 |
g_dngettext (GETTEXT_PACKAGE, |
| 1113 |
"Microsoft 365 server is busy, waiting to retry (%d:%02d minute)", |
| 1114 |
"Microsoft 365 server is busy, waiting to retry (%d:%02d minutes)", left_minutes), |
| 1115 |
left_minutes, left_seconds); |
| 1116 |
} else { |
| 1117 |
camel_operation_push_message (cancellable, |
| 1118 |
g_dngettext (GETTEXT_PACKAGE, |
| 1119 |
"Microsoft 365 server is busy, waiting to retry (%d second)", |
| 1120 |
"Microsoft 365 server is busy, waiting to retry (%d seconds)", left_seconds), |
| 1121 |
left_seconds); |
| 1122 |
} |
| 1123 |
|
| 1124 |
e_flag_wait_until (flag, now + (G_TIME_SPAN_MILLISECOND * (wait_ms > 1000 ? 1000 : wait_ms))); |
| 1125 |
e_flag_clear (flag); |
| 1126 |
|
| 1127 |
now = g_get_monotonic_time () - now; |
| 1128 |
now = now / G_TIME_SPAN_MILLISECOND; |
| 1129 |
|
| 1130 |
if (now >= wait_ms) |
| 1131 |
wait_ms = 0; |
| 1132 |
wait_ms -= now; |
| 1133 |
|
| 1134 |
camel_operation_pop_message (cancellable); |
| 1135 |
} |
| 1136 |
|
| 1137 |
if (handler_id) |
| 1138 |
g_cancellable_disconnect (cancellable, handler_id); |
| 1139 |
|
| 1140 |
e_flag_free (flag); |
| 1141 |
|
| 1142 |
LOCK (cnc); |
| 1143 |
|
| 1144 |
cnc->priv->backoff_for_usec = 0; |
| 1145 |
} |
| 1146 |
|
| 1147 |
if (g_cancellable_set_error_if_cancelled (cancellable, error)) { |
| 1148 |
UNLOCK (cnc); |
| 1149 |
|
| 1150 |
e_m365_connection_util_set_message_status_code (message, -1); |
| 1151 |
|
| 1152 |
return FALSE; |
| 1153 |
} |
| 1154 |
|
| 1155 |
soup_session = cnc->priv->soup_session ? g_object_ref (cnc->priv->soup_session) : NULL; |
| 1156 |
|
| 1157 |
UNLOCK (cnc); |
| 1158 |
|
| 1159 |
if (soup_session) { |
| 1160 |
GInputStream *input_stream; |
| 1161 |
GError *local_error = NULL; |
| 1162 |
gboolean is_io_error; |
| 1163 |
|
| 1164 |
input_stream = e_soup_session_send_message_sync (E_SOUP_SESSION (soup_session), message, cancellable, &local_error); |
| 1165 |
|
| 1166 |
success = input_stream != NULL; |
| 1167 |
is_io_error = !did_io_error_retry && g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT); |
| 1168 |
|
| 1169 |
if (local_error) { |
| 1170 |
g_propagate_error (error, local_error); |
| 1171 |
local_error = NULL; |
| 1172 |
} |
| 1173 |
|
| 1174 |
if (is_io_error || |
| 1175 |
/* Throttling - https://docs.microsoft.com/en-us/graph/throttling */ |
| 1176 |
e_m365_connection_util_get_message_status_code (message) == 429 || |
| 1177 |
/* https://docs.microsoft.com/en-us/graph/best-practices-concept#handling-expected-errors */ |
| 1178 |
e_m365_connection_util_get_message_status_code (message) == SOUP_STATUS_SERVICE_UNAVAILABLE) { |
| 1179 |
did_io_error_retry = did_io_error_retry || is_io_error; |
| 1180 |
need_retry = TRUE; |
| 1181 |
} |
| 1182 |
|
| 1183 |
if (need_retry) { |
| 1184 |
const gchar *retry_after_str; |
| 1185 |
gint64 retry_after; |
| 1186 |
|
| 1187 |
retry_after_str = soup_message_get_response_headers (message) ? |
| 1188 |
soup_message_headers_get_one (soup_message_get_response_headers (message), "Retry-After") : NULL; |
| 1189 |
|
| 1190 |
if (retry_after_str && *retry_after_str) |
| 1191 |
retry_after = g_ascii_strtoll (retry_after_str, NULL, 10); |
| 1192 |
else if (is_io_error) |
| 1193 |
retry_after = M365_RETRY_IO_ERROR_SECONDS; |
| 1194 |
else |
| 1195 |
retry_after = 0; |
| 1196 |
|
| 1197 |
if (retry_after > 0) |
| 1198 |
need_retry_seconds = retry_after; |
| 1199 |
else if (need_retry_seconds < 120) |
| 1200 |
need_retry_seconds *= 2; |
| 1201 |
|
| 1202 |
LOCK (cnc); |
| 1203 |
|
| 1204 |
if (cnc->priv->backoff_for_usec < need_retry_seconds * G_USEC_PER_SEC) |
| 1205 |
cnc->priv->backoff_for_usec = need_retry_seconds * G_USEC_PER_SEC; |
| 1206 |
|
| 1207 |
if (e_m365_connection_util_get_message_status_code (message) == SOUP_STATUS_SERVICE_UNAVAILABLE) |
| 1208 |
soup_session_abort (soup_session); |
| 1209 |
|
| 1210 |
UNLOCK (cnc); |
| 1211 |
|
| 1212 |
success = FALSE; |
| 1213 |
} else if (success && raw_data_func && SOUP_STATUS_IS_SUCCESSFUL (e_m365_connection_util_get_message_status_code (message))) { |
| 1214 |
success = raw_data_func (cnc, message, input_stream, func_user_data, cancellable, error); |
| 1215 |
} else if (success) { |
| 1216 |
JsonNode *node = NULL; |
| 1217 |
|
| 1218 |
success = e_m365_connection_json_node_from_message (message, input_stream, &node, cancellable, error); |
| 1219 |
|
| 1220 |
if (success) { |
| 1221 |
gchar *next_link = NULL; |
| 1222 |
|
| 1223 |
success = response_func && response_func (cnc, message, input_stream, node, func_user_data, &next_link, cancellable, error); |
| 1224 |
|
| 1225 |
if (success && next_link && *next_link) { |
| 1226 |
GUri *uri; |
| 1227 |
|
| 1228 |
uri = g_uri_parse (next_link, SOUP_HTTP_URI_FLAGS | G_URI_FLAGS_PARSE_RELAXED, NULL); |
| 1229 |
|
| 1230 |
/* Check whether the server returned correct nextLink URI */ |
| 1231 |
g_warn_if_fail (uri != NULL); |
| 1232 |
|
| 1233 |
if (uri) { |
| 1234 |
need_retry = TRUE; |
| 1235 |
|
| 1236 |
soup_message_set_uri (message, uri); |
| 1237 |
g_uri_unref (uri); |
| 1238 |
} |
| 1239 |
} |
| 1240 |
|
| 1241 |
g_free (next_link); |
| 1242 |
} else if (error && !*error && e_m365_connection_util_get_message_status_code (message) && !SOUP_STATUS_IS_SUCCESSFUL (e_m365_connection_util_get_message_status_code (message))) { |
| 1243 |
if (e_m365_connection_util_get_message_status_code (message) == -1) |
| 1244 |
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA, _("Invalid data")); |
| 1245 |
else |
| 1246 |
g_set_error_literal (error, E_SOUP_SESSION_ERROR, e_m365_connection_util_get_message_status_code (message), |
| 1247 |
soup_message_get_reason_phrase (message) ? soup_message_get_reason_phrase (message) : |
| 1248 |
soup_status_get_phrase (e_m365_connection_util_get_message_status_code (message))); |
| 1249 |
} |
| 1250 |
|
| 1251 |
if (node) |
| 1252 |
json_node_unref (node); |
| 1253 |
} else if (e_soup_session_util_get_message_bytes (message)) { |
| 1254 |
/* Try to extract detailed error */ |
| 1255 |
JsonNode *node = NULL; |
| 1256 |
|
| 1257 |
if (!e_m365_connection_json_node_from_message (message, input_stream, &node, cancellable, &local_error) && local_error) { |
| 1258 |
g_clear_error (error); |
| 1259 |
g_propagate_error (error, local_error); |
| 1260 |
local_error = NULL; |
| 1261 |
} |
| 1262 |
|
| 1263 |
g_clear_pointer (&node, json_node_unref); |
| 1264 |
} |
| 1265 |
|
| 1266 |
g_clear_object (&input_stream); |
| 1267 |
} else { |
| 1268 |
if (!e_m365_connection_util_get_message_status_code (message)) { |
| 1269 |
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CANCELLED, _("Operation was cancelled")); |
| 1270 |
} else if (e_m365_connection_util_get_message_status_code (message) == -1) { |
| 1271 |
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA, _("Invalid data")); |
| 1272 |
} else { |
| 1273 |
g_set_error_literal (error, E_SOUP_SESSION_ERROR, e_m365_connection_util_get_message_status_code (message), |
| 1274 |
soup_message_get_reason_phrase (message) ? soup_message_get_reason_phrase (message) : |
| 1275 |
soup_status_get_phrase (e_m365_connection_util_get_message_status_code (message))); |
| 1276 |
} |
| 1277 |
} |
| 1278 |
|
| 1279 |
g_clear_object (&soup_session); |
| 1280 |
|
| 1281 |
if (need_retry) { |
| 1282 |
success = FALSE; |
| 1283 |
g_clear_error (error); |
| 1284 |
} |
| 1285 |
} |
| 1286 |
|
| 1287 |
return success; |
| 1288 |
} |
| 1289 |
|
| 1290 |
static gboolean |
| 1291 |
e_m365_read_no_response_cb (EM365Connection *cnc, |
| 1292 |
SoupMessage *message, |
| 1293 |
GInputStream *raw_data_stream, |
| 1294 |
gpointer user_data, |
| 1295 |
GCancellable *cancellable, |
| 1296 |
GError **error) |
| 1297 |
{ |
| 1298 |
/* This is used when no response is expected from the server. |
| 1299 |
Read the data stream only if debugging is on, in case |
| 1300 |
the server returns anything interesting. */ |
| 1301 |
|
| 1302 |
if (m365_log_enabled ()) { |
| 1303 |
gchar buffer[4096]; |
| 1304 |
|
| 1305 |
while (g_input_stream_read (raw_data_stream, buffer, sizeof (buffer), cancellable, error) > 0) { |
| 1306 |
/* Do nothing, just read it, thus it's shown in the debug output */ |
| 1307 |
} |
| 1308 |
} |
| 1309 |
|
| 1310 |
return TRUE; |
| 1311 |
} |
| 1312 |
|
| 1313 |
static gboolean |
| 1314 |
e_m365_read_to_byte_array_cb (EM365Connection *cnc, |
| 1315 |
SoupMessage *message, |
| 1316 |
GInputStream *raw_data_stream, |
| 1317 |
gpointer user_data, |
| 1318 |
GCancellable *cancellable, |
| 1319 |
GError **error) |
| 1320 |
{ |
| 1321 |
GByteArray **out_byte_array = user_data; |
| 1322 |
gchar buffer[4096]; |
| 1323 |
gssize n_read; |
| 1324 |
|
| 1325 |
g_return_val_if_fail (message != NULL, FALSE); |
| 1326 |
g_return_val_if_fail (out_byte_array != NULL, FALSE); |
| 1327 |
|
| 1328 |
if (!*out_byte_array) { |
| 1329 |
goffset content_length; |
| 1330 |
|
| 1331 |
content_length = soup_message_headers_get_content_length (soup_message_get_response_headers (message)); |
| 1332 |
|
| 1333 |
if (!content_length || content_length > 65536) |
| 1334 |
content_length = 65535; |
| 1335 |
|
| 1336 |
*out_byte_array = g_byte_array_sized_new (content_length); |
| 1337 |
} |
| 1338 |
|
| 1339 |
while (n_read = g_input_stream_read (raw_data_stream, buffer, sizeof (buffer), cancellable, error), n_read > 0) { |
| 1340 |
g_byte_array_append (*out_byte_array, (const guint8 *) buffer, n_read); |
| 1341 |
} |
| 1342 |
|
| 1343 |
return !n_read; |
| 1344 |
} |
| 1345 |
|
| 1346 |
typedef struct _EM365ResponseData { |
| 1347 |
EM365ConnectionJsonFunc json_func; |
| 1348 |
gpointer func_user_data; |
| 1349 |
gboolean read_only_once; /* To be able to just try authentication */ |
| 1350 |
GSList **out_items; /* JsonObject * */ |
| 1351 |
GPtrArray *out_array_items; /* JsonObject * */ |
| 1352 |
gchar **out_delta_link; /* set only if available and not NULL */ |
| 1353 |
GPtrArray *inout_requests; /* SoupMessage *, for the batch request */ |
| 1354 |
} EM365ResponseData; |
| 1355 |
|
| 1356 |
static gboolean |
| 1357 |
e_m365_read_valued_response_cb (EM365Connection *cnc, |
| 1358 |
SoupMessage *message, |
| 1359 |
GInputStream *input_stream, |
| 1360 |
JsonNode *node, |
| 1361 |
gpointer user_data, |
| 1362 |
gchar **out_next_link, |
| 1363 |
GCancellable *cancellable, |
| 1364 |
GError **error) |
| 1365 |
{ |
| 1366 |
EM365ResponseData *response_data = user_data; |
| 1367 |
JsonObject *object; |
| 1368 |
JsonArray *value; |
| 1369 |
const gchar *delta_link; |
| 1370 |
GSList *items = NULL; |
| 1371 |
gboolean can_continue = TRUE; |
| 1372 |
guint ii, len; |
| 1373 |
|
| 1374 |
g_return_val_if_fail (response_data != NULL, FALSE); |
| 1375 |
g_return_val_if_fail (out_next_link != NULL, FALSE); |
| 1376 |
g_return_val_if_fail (JSON_NODE_HOLDS_OBJECT (node), FALSE); |
| 1377 |
|
| 1378 |
object = json_node_get_object (node); |
| 1379 |
g_return_val_if_fail (object != NULL, FALSE); |
| 1380 |
|
| 1381 |
if (!response_data->read_only_once) |
| 1382 |
*out_next_link = g_strdup (e_m365_json_get_string_member (object, "@odata.nextLink", NULL)); |
| 1383 |
|
| 1384 |
delta_link = e_m365_json_get_string_member (object, "@odata.deltaLink", NULL); |
| 1385 |
|
| 1386 |
if (delta_link && response_data->out_delta_link) |
| 1387 |
*response_data->out_delta_link = g_strdup (delta_link); |
| 1388 |
|
| 1389 |
value = e_m365_json_get_array_member (object, "value"); |
| 1390 |
g_return_val_if_fail (value != NULL, FALSE); |
| 1391 |
|
| 1392 |
len = json_array_get_length (value); |
| 1393 |
|
| 1394 |
for (ii = 0; ii < len; ii++) { |
| 1395 |
JsonNode *elem = json_array_get_element (value, ii); |
| 1396 |
|
| 1397 |
g_warn_if_fail (JSON_NODE_HOLDS_OBJECT (elem)); |
| 1398 |
|
| 1399 |
if (JSON_NODE_HOLDS_OBJECT (elem)) { |
| 1400 |
JsonObject *elem_object = json_node_get_object (elem); |
| 1401 |
|
| 1402 |
if (elem_object) { |
| 1403 |
if (response_data->out_items) |
| 1404 |
*response_data->out_items = g_slist_prepend (*response_data->out_items, json_object_ref (elem_object)); |
| 1405 |
else if (response_data->out_array_items) |
| 1406 |
g_ptr_array_add (response_data->out_array_items, json_object_ref (elem_object)); |
| 1407 |
else |
| 1408 |
items = g_slist_prepend (items, json_object_ref (elem_object)); |
| 1409 |
} |
| 1410 |
} |
| 1411 |
} |
| 1412 |
|
| 1413 |
if (response_data->json_func) |
| 1414 |
can_continue = response_data->json_func (cnc, items, response_data->func_user_data, cancellable, error); |
| 1415 |
|
| 1416 |
g_slist_free_full (items, (GDestroyNotify) json_object_unref); |
| 1417 |
|
| 1418 |
return can_continue; |
| 1419 |
} |
| 1420 |
|
| 1421 |
static gboolean |
| 1422 |
e_m365_read_json_object_response_cb (EM365Connection *cnc, |
| 1423 |
SoupMessage *message, |
| 1424 |
GInputStream *input_stream, |
| 1425 |
JsonNode *node, |
| 1426 |
gpointer user_data, |
| 1427 |
gchar **out_next_link, |
| 1428 |
GCancellable *cancellable, |
| 1429 |
GError **error) |
| 1430 |
{ |
| 1431 |
JsonObject **out_json_object = user_data; |
| 1432 |
JsonObject *object; |
| 1433 |
|
| 1434 |
g_return_val_if_fail (out_json_object != NULL, FALSE); |
| 1435 |
g_return_val_if_fail (out_next_link != NULL, FALSE); |
| 1436 |
g_return_val_if_fail (JSON_NODE_HOLDS_OBJECT (node), FALSE); |
| 1437 |
|
| 1438 |
object = json_node_get_object (node); |
| 1439 |
g_return_val_if_fail (object != NULL, FALSE); |
| 1440 |
|
| 1441 |
*out_json_object = json_object_ref (object); |
| 1442 |
|
| 1443 |
return TRUE; |
| 1444 |
} |
| 1445 |
|
| 1446 |
static SoupMessage * |
| 1447 |
m365_connection_new_soup_message (const gchar *method, |
| 1448 |
const gchar *uri, |
| 1449 |
CSMFlags csm_flags, |
| 1450 |
GError **error) |
| 1451 |
{ |
| 1452 |
SoupMessage *message; |
| 1453 |
|
| 1454 |
g_return_val_if_fail (method != NULL, NULL); |
| 1455 |
g_return_val_if_fail (uri != NULL, NULL); |
| 1456 |
|
| 1457 |
message = soup_message_new (method, uri); |
| 1458 |
|
| 1459 |
if (message) { |
| 1460 |
SoupMessageHeaders *request_headers = soup_message_get_request_headers (message); |
| 1461 |
|
| 1462 |
soup_message_headers_append (request_headers, "Connection", "Close"); |
| 1463 |
soup_message_headers_append (request_headers, "User-Agent", "Evolution-M365/" VERSION); |
| 1464 |
|
| 1465 |
/* Disable caching for proxies (RFC 4918, section 10.4.5) */ |
| 1466 |
soup_message_headers_append (request_headers, "Cache-Control", "no-cache"); |
| 1467 |
soup_message_headers_append (request_headers, "Pragma", "no-cache"); |
| 1468 |
|
| 1469 |
if ((csm_flags & CSM_DISABLE_RESPONSE) != 0) |
| 1470 |
soup_message_headers_append (request_headers, "Prefer", "return=minimal"); |
| 1471 |
} else { |
| 1472 |
g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA, _("Malformed URI: â%sâ"), uri); |
| 1473 |
} |
| 1474 |
|
| 1475 |
return message; |
| 1476 |
} |
| 1477 |
|
| 1478 |
ESourceAuthenticationResult |
| 1479 |
e_m365_connection_authenticate_sync (EM365Connection *cnc, |
| 1480 |
const gchar *user_override, |
| 1481 |
EM365FolderKind kind, |
| 1482 |
const gchar *group_id, |
| 1483 |
const gchar *folder_id, |
| 1484 |
gchar **out_certificate_pem, |
| 1485 |
GTlsCertificateFlags *out_certificate_errors, |
| 1486 |
GCancellable *cancellable, |
| 1487 |
GError **error) |
| 1488 |
{ |
| 1489 |
ESourceAuthenticationResult result = E_SOURCE_AUTHENTICATION_ERROR; |
| 1490 |
JsonObject *object = NULL; |
| 1491 |
gboolean success = FALSE; |
| 1492 |
GError *local_error = NULL; |
| 1493 |
|
| 1494 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), result); |
| 1495 |
|
| 1496 |
switch (kind) { |
| 1497 |
default: |
| 1498 |
g_warn_if_reached (); |
| 1499 |
/* Falls through */ |
| 1500 |
case E_M365_FOLDER_KIND_UNKNOWN: |
| 1501 |
case E_M365_FOLDER_KIND_MAIL: |
| 1502 |
if (!folder_id || !*folder_id) |
| 1503 |
folder_id = "inbox"; |
| 1504 |
|
| 1505 |
success = e_m365_connection_get_mail_folder_sync (cnc, user_override, folder_id, "displayName", &object, cancellable, &local_error); |
| 1506 |
break; |
| 1507 |
case E_M365_FOLDER_KIND_CONTACTS: |
| 1508 |
if (!folder_id || !*folder_id) |
| 1509 |
folder_id = "contacts"; |
| 1510 |
|
| 1511 |
success = e_m365_connection_get_contacts_folder_sync (cnc, user_override, folder_id, "displayName", &object, cancellable, &local_error); |
| 1512 |
break; |
| 1513 |
case E_M365_FOLDER_KIND_ORG_CONTACTS: |
| 1514 |
success = e_m365_connection_get_org_contacts_accessible_sync (cnc, user_override, cancellable, &local_error); |
| 1515 |
break; |
| 1516 |
case E_M365_FOLDER_KIND_USERS: |
| 1517 |
success = e_m365_connection_get_users_accessible_sync (cnc, user_override, cancellable, &local_error); |
| 1518 |
break; |
| 1519 |
case E_M365_FOLDER_KIND_PEOPLE: |
| 1520 |
success = e_m365_connection_get_people_accessible_sync (cnc, user_override, cancellable, &local_error); |
| 1521 |
break; |
| 1522 |
case E_M365_FOLDER_KIND_CALENDAR: |
| 1523 |
if (folder_id && !*folder_id) |
| 1524 |
folder_id = NULL; |
| 1525 |
|
| 1526 |
success = e_m365_connection_get_calendar_folder_sync (cnc, user_override, group_id, folder_id, "name", &object, cancellable, error); |
| 1527 |
break; |
| 1528 |
case E_M365_FOLDER_KIND_TASKS: |
| 1529 |
if (!folder_id || !*folder_id) |
| 1530 |
folder_id = "tasks"; |
| 1531 |
|
| 1532 |
success = e_m365_connection_get_task_list_sync (cnc, user_override, folder_id, &object, cancellable, error); |
| 1533 |
break; |
| 1534 |
} |
| 1535 |
|
| 1536 |
if (success) { |
| 1537 |
result = E_SOURCE_AUTHENTICATION_ACCEPTED; |
| 1538 |
} else { |
| 1539 |
if (g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { |
| 1540 |
/* Nothing to do */ |
| 1541 |
} else if (e_soup_session_get_ssl_error_details (E_SOUP_SESSION (cnc->priv->soup_session), out_certificate_pem, out_certificate_errors)) { |
| 1542 |
result = E_SOURCE_AUTHENTICATION_ERROR_SSL_FAILED; |
| 1543 |
} else if (g_error_matches (local_error, E_SOUP_SESSION_ERROR, SOUP_STATUS_UNAUTHORIZED) || |
| 1544 |
/* Returned by the OAuth2 service when the token cannot be refreshed */ |
| 1545 |
g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_CONNECTION_REFUSED)) { |
| 1546 |
LOCK (cnc); |
| 1547 |
|
| 1548 |
if (cnc->priv->impersonate_user) { |
| 1549 |
g_propagate_error (error, local_error); |
| 1550 |
local_error = NULL; |
| 1551 |
} else { |
| 1552 |
result = E_SOURCE_AUTHENTICATION_REJECTED; |
| 1553 |
} |
| 1554 |
|
| 1555 |
UNLOCK (cnc); |
| 1556 |
|
| 1557 |
g_clear_error (&local_error); |
| 1558 |
} |
| 1559 |
|
| 1560 |
if (local_error) { |
| 1561 |
g_propagate_error (error, local_error); |
| 1562 |
local_error = NULL; |
| 1563 |
} |
| 1564 |
} |
| 1565 |
|
| 1566 |
if (object) |
| 1567 |
json_object_unref (object); |
| 1568 |
|
| 1569 |
g_clear_error (&local_error); |
| 1570 |
|
| 1571 |
return result; |
| 1572 |
} |
| 1573 |
|
| 1574 |
gboolean |
| 1575 |
e_m365_connection_disconnect_sync (EM365Connection *cnc, |
| 1576 |
GCancellable *cancellable, |
| 1577 |
GError **error) |
| 1578 |
{ |
| 1579 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 1580 |
|
| 1581 |
LOCK (cnc); |
| 1582 |
|
| 1583 |
soup_session_abort (cnc->priv->soup_session); |
| 1584 |
|
| 1585 |
UNLOCK (cnc); |
| 1586 |
|
| 1587 |
return TRUE; |
| 1588 |
} |
| 1589 |
|
| 1590 |
/* Expects NULL-terminated pair of parameters 'name', 'value'; if 'value' is NULL, the parameter is skipped. |
| 1591 |
An empty 'name' can add the 'value' into the path. These can be only before query parameters. */ |
| 1592 |
gchar * |
| 1593 |
e_m365_connection_construct_uri (EM365Connection *cnc, |
| 1594 |
gboolean include_user, |
| 1595 |
const gchar *user_override, |
| 1596 |
EM365ApiVersion api_version, |
| 1597 |
const gchar *api_part, |
| 1598 |
const gchar *resource, |
| 1599 |
const gchar *id, |
| 1600 |
const gchar *path, |
| 1601 |
...) |
| 1602 |
{ |
| 1603 |
va_list args; |
| 1604 |
const gchar *name, *value; |
| 1605 |
gboolean first_param = TRUE; |
| 1606 |
GString *uri; |
| 1607 |
|
| 1608 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), NULL); |
| 1609 |
|
| 1610 |
if (!api_part) |
| 1611 |
api_part = "users"; |
| 1612 |
|
| 1613 |
uri = g_string_sized_new (128); |
| 1614 |
|
| 1615 |
/* https://graph.microsoft.com/v1.0/users/XUSERX/mailFolders */ |
| 1616 |
|
| 1617 |
g_string_append (uri, "https://" M365_HOSTNAME); |
| 1618 |
|
| 1619 |
switch (api_version) { |
| 1620 |
case E_M365_API_V1_0: |
| 1621 |
g_string_append_c (uri, '/'); |
| 1622 |
g_string_append (uri, "v1.0"); |
| 1623 |
break; |
| 1624 |
case E_M365_API_BETA: |
| 1625 |
g_string_append_c (uri, '/'); |
| 1626 |
g_string_append (uri, "beta"); |
| 1627 |
break; |
| 1628 |
} |
| 1629 |
|
| 1630 |
if (*api_part) { |
| 1631 |
g_string_append_c (uri, '/'); |
| 1632 |
g_string_append (uri, api_part); |
| 1633 |
} |
| 1634 |
|
| 1635 |
if (include_user) { |
| 1636 |
const gchar *use_user; |
| 1637 |
|
| 1638 |
LOCK (cnc); |
| 1639 |
|
| 1640 |
if (user_override) |
| 1641 |
use_user = user_override; |
| 1642 |
else if (cnc->priv->impersonate_user) |
| 1643 |
use_user = cnc->priv->impersonate_user; |
| 1644 |
else |
| 1645 |
use_user = cnc->priv->user; |
| 1646 |
|
| 1647 |
if (use_user) { |
| 1648 |
gchar *encoded; |
| 1649 |
|
| 1650 |
encoded = g_uri_escape_string (use_user, NULL, FALSE); |
| 1651 |
|
| 1652 |
g_string_append_c (uri, '/'); |
| 1653 |
g_string_append (uri, encoded); |
| 1654 |
|
| 1655 |
g_free (encoded); |
| 1656 |
} |
| 1657 |
|
| 1658 |
UNLOCK (cnc); |
| 1659 |
} |
| 1660 |
|
| 1661 |
if (resource && *resource) { |
| 1662 |
g_string_append_c (uri, '/'); |
| 1663 |
g_string_append (uri, resource); |
| 1664 |
} |
| 1665 |
|
| 1666 |
if (id && *id) { |
| 1667 |
g_string_append_c (uri, '/'); |
| 1668 |
g_string_append (uri, id); |
| 1669 |
} |
| 1670 |
|
| 1671 |
if (path && *path) { |
| 1672 |
g_string_append_c (uri, '/'); |
| 1673 |
g_string_append (uri, path); |
| 1674 |
} |
| 1675 |
|
| 1676 |
va_start (args, path); |
| 1677 |
|
| 1678 |
name = va_arg (args, const gchar *); |
| 1679 |
|
| 1680 |
while (name) { |
| 1681 |
value = va_arg (args, const gchar *); |
| 1682 |
|
| 1683 |
if (*name && value) { |
| 1684 |
g_string_append_c (uri, first_param ? '?' : '&'); |
| 1685 |
|
| 1686 |
first_param = FALSE; |
| 1687 |
|
| 1688 |
g_string_append (uri, name); |
| 1689 |
g_string_append_c (uri, '='); |
| 1690 |
|
| 1691 |
if (*value) { |
| 1692 |
gchar *encoded; |
| 1693 |
|
| 1694 |
encoded = g_uri_escape_string (value, NULL, FALSE); |
| 1695 |
|
| 1696 |
g_string_append (uri, encoded); |
| 1697 |
|
| 1698 |
g_free (encoded); |
| 1699 |
} |
| 1700 |
} else if (!*name && value && *value) { |
| 1701 |
/* Warn when adding path after additional query parameters */ |
| 1702 |
g_warn_if_fail (first_param); |
| 1703 |
|
| 1704 |
g_string_append_c (uri, '/'); |
| 1705 |
g_string_append (uri, value); |
| 1706 |
} |
| 1707 |
|
| 1708 |
name = va_arg (args, const gchar *); |
| 1709 |
} |
| 1710 |
|
| 1711 |
va_end (args); |
| 1712 |
|
| 1713 |
return g_string_free (uri, FALSE); |
| 1714 |
} |
| 1715 |
|
| 1716 |
static void |
| 1717 |
e_m365_connection_set_json_body (SoupMessage *message, |
| 1718 |
JsonBuilder *builder) |
| 1719 |
{ |
| 1720 |
JsonGenerator *generator; |
| 1721 |
JsonNode *node; |
| 1722 |
gchar *data; |
| 1723 |
gsize data_length = 0; |
| 1724 |
|
| 1725 |
g_return_if_fail (SOUP_IS_MESSAGE (message)); |
| 1726 |
g_return_if_fail (builder != NULL); |
| 1727 |
|
| 1728 |
node = json_builder_get_root (builder); |
| 1729 |
|
| 1730 |
generator = json_generator_new (); |
| 1731 |
json_generator_set_root (generator, node); |
| 1732 |
|
| 1733 |
data = json_generator_to_data (generator, &data_length); |
| 1734 |
|
| 1735 |
soup_message_headers_set_content_type (soup_message_get_request_headers (message), "application/json", NULL); |
| 1736 |
|
| 1737 |
if (data) |
| 1738 |
e_soup_session_util_set_message_request_body_from_data (message, FALSE, "application/json", data, data_length, g_free); |
| 1739 |
|
| 1740 |
g_object_unref (generator); |
| 1741 |
json_node_unref (node); |
| 1742 |
} |
| 1743 |
|
| 1744 |
static void |
| 1745 |
e_m365_fill_message_headers_cb (JsonObject *object, |
| 1746 |
const gchar *member_name, |
| 1747 |
JsonNode *member_node, |
| 1748 |
gpointer user_data) |
| 1749 |
{ |
| 1750 |
SoupMessage *message = user_data; |
| 1751 |
|
| 1752 |
g_return_if_fail (message != NULL); |
| 1753 |
g_return_if_fail (member_name != NULL); |
| 1754 |
g_return_if_fail (member_node != NULL); |
| 1755 |
|
| 1756 |
if (JSON_NODE_HOLDS_VALUE (member_node)) { |
| 1757 |
const gchar *value; |
| 1758 |
|
| 1759 |
value = json_node_get_string (member_node); |
| 1760 |
|
| 1761 |
if (value) |
| 1762 |
soup_message_headers_replace (soup_message_get_response_headers (message), member_name, value); |
| 1763 |
} |
| 1764 |
} |
| 1765 |
|
| 1766 |
static void |
| 1767 |
e_m365_connection_fill_batch_response (SoupMessage *message, |
| 1768 |
JsonObject *object) |
| 1769 |
{ |
| 1770 |
JsonObject *subobject; |
| 1771 |
|
| 1772 |
g_return_if_fail (SOUP_IS_MESSAGE (message)); |
| 1773 |
g_return_if_fail (object != NULL); |
| 1774 |
|
| 1775 |
e_m365_connection_util_set_message_status_code (message, e_m365_json_get_int_member (object, "status", -1)); |
| 1776 |
|
| 1777 |
subobject = e_m365_json_get_object_member (object, "headers"); |
| 1778 |
|
| 1779 |
if (subobject) |
| 1780 |
json_object_foreach_member (subobject, e_m365_fill_message_headers_cb, message); |
| 1781 |
|
| 1782 |
subobject = e_m365_json_get_object_member (object, "body"); |
| 1783 |
|
| 1784 |
if (subobject) |
| 1785 |
g_object_set_data_full (G_OBJECT (message), X_EVO_M365_DATA, json_object_ref (subobject), (GDestroyNotify) json_object_unref); |
| 1786 |
} |
| 1787 |
|
| 1788 |
static gboolean |
| 1789 |
e_m365_read_batch_response_cb (EM365Connection *cnc, |
| 1790 |
SoupMessage *message, |
| 1791 |
GInputStream *input_stream, |
| 1792 |
JsonNode *node, |
| 1793 |
gpointer user_data, |
| 1794 |
gchar **out_next_link, |
| 1795 |
GCancellable *cancellable, |
| 1796 |
GError **error) |
| 1797 |
{ |
| 1798 |
GPtrArray *requests = user_data; |
| 1799 |
JsonObject *object; |
| 1800 |
JsonArray *responses; |
| 1801 |
guint ii, len; |
| 1802 |
|
| 1803 |
g_return_val_if_fail (requests != NULL, FALSE); |
| 1804 |
g_return_val_if_fail (out_next_link != NULL, FALSE); |
| 1805 |
g_return_val_if_fail (JSON_NODE_HOLDS_OBJECT (node), FALSE); |
| 1806 |
|
| 1807 |
object = json_node_get_object (node); |
| 1808 |
g_return_val_if_fail (object != NULL, FALSE); |
| 1809 |
|
| 1810 |
*out_next_link = g_strdup (e_m365_json_get_string_member (object, "@odata.nextLink", NULL)); |
| 1811 |
|
| 1812 |
responses = e_m365_json_get_array_member (object, "responses"); |
| 1813 |
g_return_val_if_fail (responses != NULL, FALSE); |
| 1814 |
|
| 1815 |
len = json_array_get_length (responses); |
| 1816 |
|
| 1817 |
for (ii = 0; ii < len; ii++) { |
| 1818 |
JsonNode *elem = json_array_get_element (responses, ii); |
| 1819 |
|
| 1820 |
g_warn_if_fail (JSON_NODE_HOLDS_OBJECT (elem)); |
| 1821 |
|
| 1822 |
if (JSON_NODE_HOLDS_OBJECT (elem)) { |
| 1823 |
JsonObject *elem_object = json_node_get_object (elem); |
| 1824 |
|
| 1825 |
if (elem_object) { |
| 1826 |
const gchar *id_str; |
| 1827 |
|
| 1828 |
id_str = e_m365_json_get_string_member (elem_object, "id", NULL); |
| 1829 |
|
| 1830 |
if (id_str) { |
| 1831 |
guint id; |
| 1832 |
|
| 1833 |
id = (guint) g_ascii_strtoull (id_str, NULL, 10); |
| 1834 |
|
| 1835 |
if (id < requests->len) |
| 1836 |
e_m365_connection_fill_batch_response (g_ptr_array_index (requests, id), elem_object); |
| 1837 |
} |
| 1838 |
} |
| 1839 |
} |
| 1840 |
} |
| 1841 |
|
| 1842 |
return TRUE; |
| 1843 |
} |
| 1844 |
|
| 1845 |
/* https://docs.microsoft.com/en-us/graph/json-batching */ |
| 1846 |
|
| 1847 |
static gboolean |
| 1848 |
e_m365_connection_batch_request_internal_sync (EM365Connection *cnc, |
| 1849 |
EM365ApiVersion api_version, |
| 1850 |
GPtrArray *requests, /* SoupMessage * */ |
| 1851 |
GCancellable *cancellable, |
| 1852 |
GError **error) |
| 1853 |
{ |
| 1854 |
SoupMessage *message; |
| 1855 |
JsonBuilder *builder; |
| 1856 |
gboolean success = TRUE; |
| 1857 |
gchar *uri, buff[128]; |
| 1858 |
guint ii; |
| 1859 |
|
| 1860 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 1861 |
g_return_val_if_fail (requests != NULL, FALSE); |
| 1862 |
g_return_val_if_fail (requests->len > 0, FALSE); |
| 1863 |
g_return_val_if_fail (requests->len <= E_M365_BATCH_MAX_REQUESTS, FALSE); |
| 1864 |
|
| 1865 |
uri = e_m365_connection_construct_uri (cnc, FALSE, NULL, api_version, "", |
| 1866 |
"$batch", NULL, NULL, NULL); |
| 1867 |
|
| 1868 |
message = m365_connection_new_soup_message (SOUP_METHOD_POST, uri, CSM_DEFAULT, error); |
| 1869 |
|
| 1870 |
if (!message) { |
| 1871 |
g_free (uri); |
| 1872 |
|
| 1873 |
return FALSE; |
| 1874 |
} |
| 1875 |
|
| 1876 |
g_free (uri); |
| 1877 |
|
| 1878 |
builder = json_builder_new_immutable (); |
| 1879 |
|
| 1880 |
e_m365_json_begin_object_member (builder, NULL); |
| 1881 |
e_m365_json_begin_array_member (builder, "requests"); |
| 1882 |
|
| 1883 |
for (ii = 0; success && ii < requests->len; ii++) { |
| 1884 |
SoupMessageHeadersIter iter; |
| 1885 |
SoupMessage *submessage; |
| 1886 |
GUri *guri; |
| 1887 |
GInputStream *request_body; |
| 1888 |
gssize request_body_length = 0; |
| 1889 |
gboolean has_headers = FALSE; |
| 1890 |
const gchar *hdr_name, *hdr_value, *use_uri; |
| 1891 |
gboolean is_application_json = FALSE; |
| 1892 |
|
| 1893 |
submessage = g_ptr_array_index (requests, ii); |
| 1894 |
|
| 1895 |
if (!submessage) |
| 1896 |
continue; |
| 1897 |
|
| 1898 |
e_m365_connection_util_set_message_status_code (submessage, -1); |
| 1899 |
|
| 1900 |
guri = soup_message_get_uri (submessage); |
| 1901 |
uri = guri ? g_uri_to_string_partial (guri, G_URI_HIDE_PASSWORD) : NULL; |
| 1902 |
|
| 1903 |
if (!uri) { |
| 1904 |
g_warning ("%s: Batch message ignored due to no URI", G_STRFUNC); |
| 1905 |
continue; |
| 1906 |
} |
| 1907 |
|
| 1908 |
/* Only the path with the query can be used in the batch request */ |
| 1909 |
use_uri = strstr (uri, M365_HOSTNAME); |
| 1910 |
if (use_uri) |
| 1911 |
use_uri = strchr (use_uri, '/'); |
| 1912 |
if (!use_uri) |
| 1913 |
use_uri = uri; |
| 1914 |
|
| 1915 |
/* The 'url' is without the API part, it is derived from the main request */ |
| 1916 |
if (g_str_has_prefix (use_uri, "/v1.0/") || |
| 1917 |
g_str_has_prefix (use_uri, "/beta/")) |
| 1918 |
use_uri += 5; |
| 1919 |
|
| 1920 |
g_snprintf (buff, sizeof (buff), "%d", ii); |
| 1921 |
|
| 1922 |
e_m365_json_begin_object_member (builder, NULL); |
| 1923 |
|
| 1924 |
e_m365_json_add_string_member (builder, "id", buff); |
| 1925 |
e_m365_json_add_string_member (builder, "method", soup_message_get_method (submessage)); |
| 1926 |
e_m365_json_add_string_member (builder, "url", use_uri); |
| 1927 |
|
| 1928 |
g_free (uri); |
| 1929 |
|
| 1930 |
soup_message_headers_iter_init (&iter, soup_message_get_request_headers (submessage)); |
| 1931 |
|
| 1932 |
while (soup_message_headers_iter_next (&iter, &hdr_name, &hdr_value)) { |
| 1933 |
if (hdr_name && *hdr_name && hdr_value && |
| 1934 |
!camel_strcase_equal (hdr_name, "Connection") && |
| 1935 |
!camel_strcase_equal (hdr_name, "User-Agent")) { |
| 1936 |
if (camel_strcase_equal (hdr_name, "Content-Type") && |
| 1937 |
camel_strcase_equal (hdr_value, "application/json")) |
| 1938 |
is_application_json = TRUE; |
| 1939 |
|
| 1940 |
if (!has_headers) { |
| 1941 |
has_headers = TRUE; |
| 1942 |
|
| 1943 |
e_m365_json_begin_object_member (builder, "headers"); |
| 1944 |
} |
| 1945 |
|
| 1946 |
e_m365_json_add_string_member (builder, hdr_name, hdr_value); |
| 1947 |
} |
| 1948 |
} |
| 1949 |
|
| 1950 |
if (has_headers) |
| 1951 |
e_m365_json_end_object_member (builder); /* headers */ |
| 1952 |
|
| 1953 |
request_body = e_soup_session_util_ref_message_request_body (submessage, &request_body_length); |
| 1954 |
|
| 1955 |
if (request_body && request_body_length > 0) { |
| 1956 |
if (is_application_json) { |
| 1957 |
/* The server needs it unpacked, not as a plain string */ |
| 1958 |
JsonParser *parser; |
| 1959 |
JsonNode *node; |
| 1960 |
|
| 1961 |
parser = json_parser_new_immutable (); |
| 1962 |
|
| 1963 |
success = json_parser_load_from_stream (parser, request_body, cancellable, error); |
| 1964 |
|
| 1965 |
if (!success) |
| 1966 |
g_prefix_error (error, "%s", _("Failed to parse own Json data")); |
| 1967 |
|
| 1968 |
node = success ? json_parser_steal_root (parser) : NULL; |
| 1969 |
|
| 1970 |
if (node) { |
| 1971 |
json_builder_set_member_name (builder, "body"); |
| 1972 |
json_builder_add_value (builder, node); |
| 1973 |
} |
| 1974 |
|
| 1975 |
g_clear_object (&parser); |
| 1976 |
} else { |
| 1977 |
GByteArray *array; |
| 1978 |
guint8 *buffer; |
| 1979 |
gsize n_buffer_sz = 16384; |
| 1980 |
gsize n_read; |
| 1981 |
|
| 1982 |
buffer = g_new0 (guint8, n_buffer_sz); |
| 1983 |
array = g_byte_array_sized_new (request_body_length + 1); |
| 1984 |
|
| 1985 |
while (g_input_stream_read_all (request_body, buffer, n_buffer_sz, &n_read, cancellable, NULL)) { |
| 1986 |
if (n_read > 0) |
| 1987 |
g_byte_array_append (array, buffer, n_read); |
| 1988 |
} |
| 1989 |
|
| 1990 |
/* null-terminate the data, to use it as a string */ |
| 1991 |
buffer[0] = '\0'; |
| 1992 |
g_byte_array_append (array, buffer, 1); |
| 1993 |
|
| 1994 |
e_m365_json_add_string_member (builder, "body", (const gchar *) array->data); |
| 1995 |
|
| 1996 |
g_byte_array_unref (array); |
| 1997 |
g_free (buffer); |
| 1998 |
} |
| 1999 |
} |
| 2000 |
|
| 2001 |
e_m365_json_end_object_member (builder); /* unnamed object */ |
| 2002 |
|
| 2003 |
g_clear_object (&request_body); |
| 2004 |
} |
| 2005 |
|
| 2006 |
e_m365_json_end_array_member (builder); |
| 2007 |
e_m365_json_end_object_member (builder); |
| 2008 |
|
| 2009 |
e_m365_connection_set_json_body (message, builder); |
| 2010 |
|
| 2011 |
soup_message_headers_append (soup_message_get_request_headers (message), "Accept", "application/json"); |
| 2012 |
|
| 2013 |
g_object_unref (builder); |
| 2014 |
|
| 2015 |
success = success && m365_connection_send_request_sync (cnc, message, e_m365_read_batch_response_cb, NULL, requests, cancellable, error); |
| 2016 |
|
| 2017 |
g_clear_object (&message); |
| 2018 |
|
| 2019 |
return success; |
| 2020 |
} |
| 2021 |
|
| 2022 |
/* The 'requests' contains a SoupMessage * objects, from which are read |
| 2023 |
the request data and on success the SoupMessage's 'response' properties |
| 2024 |
are filled accordingly. |
| 2025 |
*/ |
| 2026 |
gboolean |
| 2027 |
e_m365_connection_batch_request_sync (EM365Connection *cnc, |
| 2028 |
EM365ApiVersion api_version, |
| 2029 |
GPtrArray *requests, /* SoupMessage * */ |
| 2030 |
GCancellable *cancellable, |
| 2031 |
GError **error) |
| 2032 |
{ |
| 2033 |
GPtrArray *use_requests; |
| 2034 |
gint need_retry_seconds = 5; |
| 2035 |
gboolean did_io_error_retry = FALSE; |
| 2036 |
gboolean success, need_retry = TRUE; |
| 2037 |
|
| 2038 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 2039 |
g_return_val_if_fail (requests != NULL, FALSE); |
| 2040 |
g_return_val_if_fail (requests->len > 0, FALSE); |
| 2041 |
g_return_val_if_fail (requests->len <= E_M365_BATCH_MAX_REQUESTS, FALSE); |
| 2042 |
|
| 2043 |
use_requests = requests; |
| 2044 |
|
| 2045 |
while (need_retry) { |
| 2046 |
GError *local_error = NULL; |
| 2047 |
|
| 2048 |
need_retry = FALSE; |
| 2049 |
g_clear_error (error); |
| 2050 |
|
| 2051 |
success = e_m365_connection_batch_request_internal_sync (cnc, api_version, use_requests, cancellable, &local_error); |
| 2052 |
|
| 2053 |
if (!did_io_error_retry && g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT)) { |
| 2054 |
did_io_error_retry = TRUE; |
| 2055 |
success = FALSE; |
| 2056 |
need_retry = TRUE; |
| 2057 |
|
| 2058 |
LOCK (cnc); |
| 2059 |
|
| 2060 |
if (cnc->priv->backoff_for_usec < M365_RETRY_IO_ERROR_SECONDS * G_USEC_PER_SEC) |
| 2061 |
cnc->priv->backoff_for_usec = M365_RETRY_IO_ERROR_SECONDS * G_USEC_PER_SEC; |
| 2062 |
|
| 2063 |
UNLOCK (cnc); |
| 2064 |
} |
| 2065 |
|
| 2066 |
if (local_error) { |
| 2067 |
g_propagate_error (error, local_error); |
| 2068 |
local_error = NULL; |
| 2069 |
} |
| 2070 |
|
| 2071 |
if (success) { |
| 2072 |
GPtrArray *new_requests = NULL; |
| 2073 |
gint delay_seconds = 0; |
| 2074 |
gint ii; |
| 2075 |
|
| 2076 |
for (ii = 0; ii < use_requests->len; ii++) { |
| 2077 |
SoupMessage *message = g_ptr_array_index (use_requests, ii); |
| 2078 |
|
| 2079 |
if (!message) |
| 2080 |
continue; |
| 2081 |
|
| 2082 |
/* Throttling - https://docs.microsoft.com/en-us/graph/throttling */ |
| 2083 |
if (e_m365_connection_util_get_message_status_code (message) == 429 || |
| 2084 |
/* https://docs.microsoft.com/en-us/graph/best-practices-concept#handling-expected-errors */ |
| 2085 |
e_m365_connection_util_get_message_status_code (message) == SOUP_STATUS_SERVICE_UNAVAILABLE) { |
| 2086 |
const gchar *retry_after_str; |
| 2087 |
gint64 retry_after; |
| 2088 |
|
| 2089 |
need_retry = TRUE; |
| 2090 |
|
| 2091 |
if (!new_requests) |
| 2092 |
new_requests = g_ptr_array_sized_new (use_requests->len); |
| 2093 |
|
| 2094 |
g_ptr_array_add (new_requests, message); |
| 2095 |
|
| 2096 |
retry_after_str = soup_message_get_response_headers (message) ? |
| 2097 |
soup_message_headers_get_one (soup_message_get_response_headers (message), "Retry-After") : NULL; |
| 2098 |
|
| 2099 |
if (retry_after_str && *retry_after_str) |
| 2100 |
retry_after = g_ascii_strtoll (retry_after_str, NULL, 10); |
| 2101 |
else |
| 2102 |
retry_after = 0; |
| 2103 |
|
| 2104 |
if (retry_after > 0) |
| 2105 |
delay_seconds = MAX (delay_seconds, retry_after); |
| 2106 |
else |
| 2107 |
delay_seconds = MAX (delay_seconds, need_retry_seconds); |
| 2108 |
} |
| 2109 |
} |
| 2110 |
|
| 2111 |
if (new_requests) { |
| 2112 |
if (delay_seconds) |
| 2113 |
need_retry_seconds = delay_seconds; |
| 2114 |
else if (need_retry_seconds < 120) |
| 2115 |
need_retry_seconds *= 2; |
| 2116 |
|
| 2117 |
LOCK (cnc); |
| 2118 |
|
| 2119 |
if (cnc->priv->backoff_for_usec < need_retry_seconds * G_USEC_PER_SEC) |
| 2120 |
cnc->priv->backoff_for_usec = need_retry_seconds * G_USEC_PER_SEC; |
| 2121 |
|
| 2122 |
UNLOCK (cnc); |
| 2123 |
|
| 2124 |
if (use_requests != requests) |
| 2125 |
g_ptr_array_free (use_requests, TRUE); |
| 2126 |
|
| 2127 |
use_requests = new_requests; |
| 2128 |
} |
| 2129 |
} |
| 2130 |
} |
| 2131 |
|
| 2132 |
if (use_requests != requests) |
| 2133 |
g_ptr_array_free (use_requests, TRUE); |
| 2134 |
|
| 2135 |
return success; |
| 2136 |
} |
| 2137 |
|
| 2138 |
/* This can be used as an EM365ConnectionJsonFunc function, it only |
| 2139 |
copies items of 'results' into 'user_data', which is supposed |
| 2140 |
to be a pointer to a GSList *. */ |
| 2141 |
gboolean |
| 2142 |
e_m365_connection_call_gather_into_slist (EM365Connection *cnc, |
| 2143 |
const GSList *results, /* JsonObject * - the returned objects from the server */ |
| 2144 |
gpointer user_data, /* expects GSList **, aka pointer to a GSList *, where it copies the 'results' */ |
| 2145 |
GCancellable *cancellable, |
| 2146 |
GError **error) |
| 2147 |
{ |
| 2148 |
GSList **out_results = user_data, *link; |
| 2149 |
|
| 2150 |
g_return_val_if_fail (out_results != NULL, FALSE); |
| 2151 |
|
| 2152 |
for (link = (GSList *) results; link; link = g_slist_next (link)) { |
| 2153 |
JsonObject *obj = link->data; |
| 2154 |
|
| 2155 |
if (obj) |
| 2156 |
*out_results = g_slist_prepend (*out_results, json_object_ref (obj)); |
| 2157 |
} |
| 2158 |
|
| 2159 |
return TRUE; |
| 2160 |
} |
| 2161 |
|
| 2162 |
/* https://docs.microsoft.com/en-us/graph/api/outlookuser-list-mastercategories?view=graph-rest-1.0&tabs=http */ |
| 2163 |
|
| 2164 |
gboolean |
| 2165 |
e_m365_connection_get_categories_sync (EM365Connection *cnc, |
| 2166 |
const gchar *user_override, |
| 2167 |
GSList **out_categories, /* EM365Category * */ |
| 2168 |
GCancellable *cancellable, |
| 2169 |
GError **error) |
| 2170 |
{ |
| 2171 |
EM365ResponseData rd; |
| 2172 |
SoupMessage *message; |
| 2173 |
gchar *uri; |
| 2174 |
gboolean success; |
| 2175 |
|
| 2176 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 2177 |
g_return_val_if_fail (out_categories != NULL, FALSE); |
| 2178 |
|
| 2179 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 2180 |
"outlook", |
| 2181 |
"masterCategories", |
| 2182 |
NULL, |
| 2183 |
NULL); |
| 2184 |
|
| 2185 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 2186 |
|
| 2187 |
if (!message) { |
| 2188 |
g_free (uri); |
| 2189 |
|
| 2190 |
return FALSE; |
| 2191 |
} |
| 2192 |
|
| 2193 |
g_free (uri); |
| 2194 |
|
| 2195 |
memset (&rd, 0, sizeof (EM365ResponseData)); |
| 2196 |
|
| 2197 |
rd.out_items = out_categories; |
| 2198 |
|
| 2199 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_valued_response_cb, NULL, &rd, cancellable, error); |
| 2200 |
|
| 2201 |
g_clear_object (&message); |
| 2202 |
|
| 2203 |
return success; |
| 2204 |
} |
| 2205 |
|
| 2206 |
/* https://docs.microsoft.com/en-us/graph/api/user-list-mailfolders?view=graph-rest-1.0&tabs=http */ |
| 2207 |
|
| 2208 |
gboolean |
| 2209 |
e_m365_connection_list_mail_folders_sync (EM365Connection *cnc, |
| 2210 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 2211 |
const gchar *from_path, /* path for the folder to read, NULL for top user folder */ |
| 2212 |
const gchar *select, /* properties to select, nullable */ |
| 2213 |
GSList **out_folders, /* EM365MailFolder * - the returned mailFolder objects */ |
| 2214 |
GCancellable *cancellable, |
| 2215 |
GError **error) |
| 2216 |
{ |
| 2217 |
EM365ResponseData rd; |
| 2218 |
SoupMessage *message; |
| 2219 |
gchar *uri; |
| 2220 |
gboolean success; |
| 2221 |
|
| 2222 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 2223 |
g_return_val_if_fail (out_folders != NULL, FALSE); |
| 2224 |
|
| 2225 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 2226 |
"mailFolders", |
| 2227 |
NULL, |
| 2228 |
from_path, |
| 2229 |
"$select", select, |
| 2230 |
NULL); |
| 2231 |
|
| 2232 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 2233 |
|
| 2234 |
if (!message) { |
| 2235 |
g_free (uri); |
| 2236 |
|
| 2237 |
return FALSE; |
| 2238 |
} |
| 2239 |
|
| 2240 |
g_free (uri); |
| 2241 |
|
| 2242 |
memset (&rd, 0, sizeof (EM365ResponseData)); |
| 2243 |
|
| 2244 |
rd.out_items = out_folders; |
| 2245 |
|
| 2246 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_valued_response_cb, NULL, &rd, cancellable, error); |
| 2247 |
|
| 2248 |
g_clear_object (&message); |
| 2249 |
|
| 2250 |
return success; |
| 2251 |
} |
| 2252 |
|
| 2253 |
gboolean |
| 2254 |
e_m365_connection_get_folders_delta_sync (EM365Connection *cnc, |
| 2255 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 2256 |
EM365FolderKind kind, |
| 2257 |
const gchar *select, /* properties to select, nullable */ |
| 2258 |
const gchar *delta_link, /* previous delta link */ |
| 2259 |
guint max_page_size, /* 0 for default by the server */ |
| 2260 |
EM365ConnectionJsonFunc func, /* function to call with each result set */ |
| 2261 |
gpointer func_user_data, /* user data passed into the 'func' */ |
| 2262 |
gchar **out_delta_link, |
| 2263 |
GCancellable *cancellable, |
| 2264 |
GError **error) |
| 2265 |
{ |
| 2266 |
EM365ResponseData rd; |
| 2267 |
SoupMessage *message = NULL; |
| 2268 |
gboolean success; |
| 2269 |
|
| 2270 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 2271 |
g_return_val_if_fail (out_delta_link != NULL, FALSE); |
| 2272 |
g_return_val_if_fail (func != NULL, FALSE); |
| 2273 |
|
| 2274 |
if (delta_link) |
| 2275 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, delta_link, CSM_DEFAULT, NULL); |
| 2276 |
|
| 2277 |
if (!message) { |
| 2278 |
const gchar *kind_str = NULL; |
| 2279 |
gchar *uri; |
| 2280 |
|
| 2281 |
switch (kind) { |
| 2282 |
case E_M365_FOLDER_KIND_CONTACTS: |
| 2283 |
kind_str = "contactFolders"; |
| 2284 |
break; |
| 2285 |
case E_M365_FOLDER_KIND_MAIL: |
| 2286 |
kind_str = "mailFolders"; |
| 2287 |
break; |
| 2288 |
default: |
| 2289 |
g_warn_if_reached (); |
| 2290 |
break; |
| 2291 |
} |
| 2292 |
|
| 2293 |
g_return_val_if_fail (kind_str != NULL, FALSE); |
| 2294 |
|
| 2295 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 2296 |
kind_str, |
| 2297 |
NULL, |
| 2298 |
"delta", |
| 2299 |
"$select", select, |
| 2300 |
NULL); |
| 2301 |
|
| 2302 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 2303 |
|
| 2304 |
if (!message) { |
| 2305 |
g_free (uri); |
| 2306 |
|
| 2307 |
return FALSE; |
| 2308 |
} |
| 2309 |
|
| 2310 |
g_free (uri); |
| 2311 |
} |
| 2312 |
|
| 2313 |
if (max_page_size > 0) { |
| 2314 |
gchar *prefer_value; |
| 2315 |
|
| 2316 |
prefer_value = g_strdup_printf ("odata.maxpagesize=%u", max_page_size); |
| 2317 |
|
| 2318 |
soup_message_headers_append (soup_message_get_request_headers (message), "Prefer", prefer_value); |
| 2319 |
|
| 2320 |
g_free (prefer_value); |
| 2321 |
} |
| 2322 |
|
| 2323 |
memset (&rd, 0, sizeof (EM365ResponseData)); |
| 2324 |
|
| 2325 |
rd.json_func = func; |
| 2326 |
rd.func_user_data = func_user_data; |
| 2327 |
rd.out_delta_link = out_delta_link; |
| 2328 |
|
| 2329 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_valued_response_cb, NULL, &rd, cancellable, error); |
| 2330 |
|
| 2331 |
g_clear_object (&message); |
| 2332 |
|
| 2333 |
return success; |
| 2334 |
} |
| 2335 |
|
| 2336 |
/* https://docs.microsoft.com/en-us/graph/api/mailfolder-get?view=graph-rest-1.0&tabs=http */ |
| 2337 |
|
| 2338 |
gboolean |
| 2339 |
e_m365_connection_get_mail_folder_sync (EM365Connection *cnc, |
| 2340 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 2341 |
const gchar *folder_id, /* nullable - then the 'inbox' is used */ |
| 2342 |
const gchar *select, /* nullable - properties to select */ |
| 2343 |
EM365MailFolder **out_folder, |
| 2344 |
GCancellable *cancellable, |
| 2345 |
GError **error) |
| 2346 |
{ |
| 2347 |
SoupMessage *message; |
| 2348 |
gchar *uri; |
| 2349 |
gboolean success; |
| 2350 |
|
| 2351 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 2352 |
g_return_val_if_fail (out_folder != NULL, FALSE); |
| 2353 |
|
| 2354 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 2355 |
"mailFolders", |
| 2356 |
folder_id ? folder_id : "inbox", |
| 2357 |
NULL, |
| 2358 |
"$select", select, |
| 2359 |
NULL); |
| 2360 |
|
| 2361 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 2362 |
|
| 2363 |
if (!message) { |
| 2364 |
g_free (uri); |
| 2365 |
|
| 2366 |
return FALSE; |
| 2367 |
} |
| 2368 |
|
| 2369 |
g_free (uri); |
| 2370 |
|
| 2371 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, out_folder, cancellable, error); |
| 2372 |
|
| 2373 |
g_clear_object (&message); |
| 2374 |
|
| 2375 |
return success; |
| 2376 |
} |
| 2377 |
|
| 2378 |
/* https://docs.microsoft.com/en-us/graph/api/user-post-mailfolders?view=graph-rest-1.0&tabs=http |
| 2379 |
https://docs.microsoft.com/en-us/graph/api/mailfolder-post-childfolders?view=graph-rest-1.0&tabs=http */ |
| 2380 |
|
| 2381 |
gboolean |
| 2382 |
e_m365_connection_create_mail_folder_sync (EM365Connection *cnc, |
| 2383 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 2384 |
const gchar *parent_folder_id, /* NULL for the folder root */ |
| 2385 |
const gchar *display_name, |
| 2386 |
EM365MailFolder **out_mail_folder, |
| 2387 |
GCancellable *cancellable, |
| 2388 |
GError **error) |
| 2389 |
{ |
| 2390 |
SoupMessage *message; |
| 2391 |
JsonBuilder *builder; |
| 2392 |
gboolean success; |
| 2393 |
gchar *uri; |
| 2394 |
|
| 2395 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 2396 |
g_return_val_if_fail (display_name != NULL, FALSE); |
| 2397 |
g_return_val_if_fail (out_mail_folder != NULL, FALSE); |
| 2398 |
|
| 2399 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 2400 |
"mailFolders", |
| 2401 |
parent_folder_id, |
| 2402 |
parent_folder_id ? "childFolders" : NULL, |
| 2403 |
NULL); |
| 2404 |
|
| 2405 |
message = m365_connection_new_soup_message (SOUP_METHOD_POST, uri, CSM_DEFAULT, error); |
| 2406 |
|
| 2407 |
if (!message) { |
| 2408 |
g_free (uri); |
| 2409 |
|
| 2410 |
return FALSE; |
| 2411 |
} |
| 2412 |
|
| 2413 |
g_free (uri); |
| 2414 |
|
| 2415 |
builder = json_builder_new_immutable (); |
| 2416 |
|
| 2417 |
e_m365_json_begin_object_member (builder, NULL); |
| 2418 |
e_m365_json_add_string_member (builder, "displayName", display_name); |
| 2419 |
e_m365_json_end_object_member (builder); |
| 2420 |
|
| 2421 |
e_m365_connection_set_json_body (message, builder); |
| 2422 |
|
| 2423 |
g_object_unref (builder); |
| 2424 |
|
| 2425 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, out_mail_folder, cancellable, error); |
| 2426 |
|
| 2427 |
g_clear_object (&message); |
| 2428 |
|
| 2429 |
return success; |
| 2430 |
} |
| 2431 |
|
| 2432 |
/* https://docs.microsoft.com/en-us/graph/api/mailfolder-delete?view=graph-rest-1.0&tabs=http */ |
| 2433 |
|
| 2434 |
gboolean |
| 2435 |
e_m365_connection_delete_mail_folder_sync (EM365Connection *cnc, |
| 2436 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 2437 |
const gchar *folder_id, |
| 2438 |
GCancellable *cancellable, |
| 2439 |
GError **error) |
| 2440 |
{ |
| 2441 |
SoupMessage *message; |
| 2442 |
gboolean success; |
| 2443 |
gchar *uri; |
| 2444 |
|
| 2445 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 2446 |
g_return_val_if_fail (folder_id != NULL, FALSE); |
| 2447 |
|
| 2448 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 2449 |
"mailFolders", folder_id, NULL, NULL); |
| 2450 |
|
| 2451 |
message = m365_connection_new_soup_message (SOUP_METHOD_DELETE, uri, CSM_DEFAULT, error); |
| 2452 |
|
| 2453 |
if (!message) { |
| 2454 |
g_free (uri); |
| 2455 |
|
| 2456 |
return FALSE; |
| 2457 |
} |
| 2458 |
|
| 2459 |
g_free (uri); |
| 2460 |
|
| 2461 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_no_response_cb, NULL, cancellable, error); |
| 2462 |
|
| 2463 |
g_clear_object (&message); |
| 2464 |
|
| 2465 |
return success; |
| 2466 |
} |
| 2467 |
|
| 2468 |
/* https://docs.microsoft.com/en-us/graph/api/mailfolder-copy?view=graph-rest-1.0&tabs=http |
| 2469 |
https://docs.microsoft.com/en-us/graph/api/mailfolder-move?view=graph-rest-1.0&tabs=http |
| 2470 |
*/ |
| 2471 |
gboolean |
| 2472 |
e_m365_connection_copy_move_mail_folder_sync (EM365Connection *cnc, |
| 2473 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 2474 |
const gchar *src_folder_id, |
| 2475 |
const gchar *des_folder_id, |
| 2476 |
gboolean do_copy, |
| 2477 |
EM365MailFolder **out_mail_folder, |
| 2478 |
GCancellable *cancellable, |
| 2479 |
GError **error) |
| 2480 |
{ |
| 2481 |
SoupMessage *message; |
| 2482 |
JsonBuilder *builder; |
| 2483 |
gboolean success; |
| 2484 |
gchar *uri; |
| 2485 |
|
| 2486 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 2487 |
g_return_val_if_fail (src_folder_id != NULL, FALSE); |
| 2488 |
g_return_val_if_fail (des_folder_id != NULL, FALSE); |
| 2489 |
|
| 2490 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 2491 |
"mailFolders", |
| 2492 |
src_folder_id, |
| 2493 |
do_copy ? "copy" : "move", |
| 2494 |
NULL); |
| 2495 |
|
| 2496 |
message = m365_connection_new_soup_message (SOUP_METHOD_POST, uri, CSM_DEFAULT, error); |
| 2497 |
|
| 2498 |
if (!message) { |
| 2499 |
g_free (uri); |
| 2500 |
|
| 2501 |
return FALSE; |
| 2502 |
} |
| 2503 |
|
| 2504 |
g_free (uri); |
| 2505 |
|
| 2506 |
builder = json_builder_new_immutable (); |
| 2507 |
|
| 2508 |
e_m365_json_begin_object_member (builder, NULL); |
| 2509 |
e_m365_json_add_string_member (builder, "destinationId", des_folder_id); |
| 2510 |
e_m365_json_end_object_member (builder); |
| 2511 |
|
| 2512 |
e_m365_connection_set_json_body (message, builder); |
| 2513 |
|
| 2514 |
g_object_unref (builder); |
| 2515 |
|
| 2516 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, out_mail_folder, cancellable, error); |
| 2517 |
|
| 2518 |
g_clear_object (&message); |
| 2519 |
|
| 2520 |
return success; |
| 2521 |
} |
| 2522 |
|
| 2523 |
/* https://docs.microsoft.com/en-us/graph/api/mailfolder-update?view=graph-rest-1.0&tabs=http */ |
| 2524 |
|
| 2525 |
gboolean |
| 2526 |
e_m365_connection_rename_mail_folder_sync (EM365Connection *cnc, |
| 2527 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 2528 |
const gchar *folder_id, |
| 2529 |
const gchar *display_name, |
| 2530 |
EM365MailFolder **out_mail_folder, |
| 2531 |
GCancellable *cancellable, |
| 2532 |
GError **error) |
| 2533 |
{ |
| 2534 |
SoupMessage *message; |
| 2535 |
JsonBuilder *builder; |
| 2536 |
gboolean success; |
| 2537 |
gchar *uri; |
| 2538 |
|
| 2539 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 2540 |
g_return_val_if_fail (folder_id != NULL, FALSE); |
| 2541 |
g_return_val_if_fail (display_name != NULL, FALSE); |
| 2542 |
|
| 2543 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 2544 |
"mailFolders", |
| 2545 |
folder_id, |
| 2546 |
NULL, |
| 2547 |
NULL); |
| 2548 |
|
| 2549 |
message = m365_connection_new_soup_message ("PATCH", uri, CSM_DEFAULT, error); |
| 2550 |
|
| 2551 |
if (!message) { |
| 2552 |
g_free (uri); |
| 2553 |
|
| 2554 |
return FALSE; |
| 2555 |
} |
| 2556 |
|
| 2557 |
g_free (uri); |
| 2558 |
|
| 2559 |
builder = json_builder_new_immutable (); |
| 2560 |
|
| 2561 |
e_m365_json_begin_object_member (builder, NULL); |
| 2562 |
e_m365_json_add_string_member (builder, "displayName", display_name); |
| 2563 |
e_m365_json_end_object_member (builder); |
| 2564 |
|
| 2565 |
e_m365_connection_set_json_body (message, builder); |
| 2566 |
|
| 2567 |
g_object_unref (builder); |
| 2568 |
|
| 2569 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, out_mail_folder, cancellable, error); |
| 2570 |
|
| 2571 |
g_clear_object (&message); |
| 2572 |
|
| 2573 |
return success; |
| 2574 |
} |
| 2575 |
|
| 2576 |
gboolean |
| 2577 |
e_m365_connection_list_messages_sync (EM365Connection *cnc, |
| 2578 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 2579 |
const gchar *folder_id, |
| 2580 |
const gchar *select, /* nullable - properties to select */ |
| 2581 |
const gchar *filter, /* nullable - filter which events to list */ |
| 2582 |
GSList **out_messages, /* EM365MailMessage * - the returned objects */ |
| 2583 |
GCancellable *cancellable, |
| 2584 |
GError **error) |
| 2585 |
{ |
| 2586 |
EM365ResponseData rd; |
| 2587 |
SoupMessage *message; |
| 2588 |
gchar *uri; |
| 2589 |
gboolean success; |
| 2590 |
|
| 2591 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 2592 |
g_return_val_if_fail (folder_id != NULL, FALSE); |
| 2593 |
g_return_val_if_fail (out_messages != NULL, FALSE); |
| 2594 |
|
| 2595 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 2596 |
"mailFolders", |
| 2597 |
folder_id, |
| 2598 |
"messages", |
| 2599 |
"$select", select, |
| 2600 |
"$filter", filter, |
| 2601 |
NULL); |
| 2602 |
|
| 2603 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 2604 |
|
| 2605 |
if (!message) { |
| 2606 |
g_free (uri); |
| 2607 |
|
| 2608 |
return FALSE; |
| 2609 |
} |
| 2610 |
|
| 2611 |
g_free (uri); |
| 2612 |
|
| 2613 |
memset (&rd, 0, sizeof (EM365ResponseData)); |
| 2614 |
|
| 2615 |
rd.out_items = out_messages; |
| 2616 |
|
| 2617 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_valued_response_cb, NULL, &rd, cancellable, error); |
| 2618 |
|
| 2619 |
g_clear_object (&message); |
| 2620 |
|
| 2621 |
return success; |
| 2622 |
} |
| 2623 |
|
| 2624 |
/* https://docs.microsoft.com/en-us/graph/api/message-delta?view=graph-rest-1.0&tabs=http |
| 2625 |
https://docs.microsoft.com/en-us/graph/api/contact-delta?view=graph-rest-1.0&tabs=http |
| 2626 |
https://learn.microsoft.com/en-us/graph/api/orgcontact-delta?view=graph-rest-1.0&tabs=http |
| 2627 |
https://learn.microsoft.com/en-us/graph/api/user-delta?view=graph-rest-1.0&tabs=http */ |
| 2628 |
|
| 2629 |
gboolean |
| 2630 |
e_m365_connection_get_objects_delta_sync (EM365Connection *cnc, |
| 2631 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 2632 |
EM365FolderKind kind, |
| 2633 |
const gchar *folder_id, /* folder ID to get delta messages in, nullable for orgContacts, users */ |
| 2634 |
const gchar *select, /* properties to select, nullable */ |
| 2635 |
const gchar *delta_link, /* previous delta link */ |
| 2636 |
guint max_page_size, /* 0 for default by the server */ |
| 2637 |
EM365ConnectionJsonFunc func, /* function to call with each result set */ |
| 2638 |
gpointer func_user_data, /* user data passed into the 'func' */ |
| 2639 |
gchar **out_delta_link, |
| 2640 |
GCancellable *cancellable, |
| 2641 |
GError **error) |
| 2642 |
{ |
| 2643 |
EM365ResponseData rd; |
| 2644 |
SoupMessage *message = NULL; |
| 2645 |
gboolean success; |
| 2646 |
|
| 2647 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 2648 |
g_return_val_if_fail (out_delta_link != NULL, FALSE); |
| 2649 |
g_return_val_if_fail (func != NULL, FALSE); |
| 2650 |
|
| 2651 |
if (delta_link) |
| 2652 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, delta_link, CSM_DEFAULT, NULL); |
| 2653 |
|
| 2654 |
if (!message) { |
| 2655 |
const gchar *api_part = NULL; |
| 2656 |
const gchar *kind_str = NULL; |
| 2657 |
const gchar *kind_path_str = NULL; |
| 2658 |
gchar *uri; |
| 2659 |
gboolean with_size; |
| 2660 |
|
| 2661 |
switch (kind) { |
| 2662 |
case E_M365_FOLDER_KIND_CONTACTS: |
| 2663 |
g_return_val_if_fail (folder_id != NULL, FALSE); |
| 2664 |
kind_str = "contactFolders"; |
| 2665 |
kind_path_str = "contacts"; |
| 2666 |
break; |
| 2667 |
case E_M365_FOLDER_KIND_ORG_CONTACTS: |
| 2668 |
api_part = "contacts"; |
| 2669 |
break; |
| 2670 |
case E_M365_FOLDER_KIND_USERS: |
| 2671 |
api_part = "users"; |
| 2672 |
break; |
| 2673 |
case E_M365_FOLDER_KIND_MAIL: |
| 2674 |
g_return_val_if_fail (folder_id != NULL, FALSE); |
| 2675 |
kind_str = "mailFolders"; |
| 2676 |
kind_path_str = "messages"; |
| 2677 |
break; |
| 2678 |
default: |
| 2679 |
g_warn_if_reached (); |
| 2680 |
break; |
| 2681 |
} |
| 2682 |
|
| 2683 |
with_size = select && g_str_has_prefix (select, "|size|"); |
| 2684 |
if (with_size) |
| 2685 |
select += 6; |
| 2686 |
|
| 2687 |
uri = e_m365_connection_construct_uri (cnc, api_part == NULL, user_override, E_M365_API_V1_0, api_part, |
| 2688 |
kind_str, |
| 2689 |
folder_id, |
| 2690 |
kind_path_str, |
| 2691 |
"", "delta", |
| 2692 |
"$select", select, |
| 2693 |
with_size ? "expand" : NULL, |
| 2694 |
with_size ? "singleValueExtendedProperties($filter=id eq '" E_M365_PT_MESSAGE_SIZE_NAME "')" : NULL, |
| 2695 |
NULL); |
| 2696 |
|
| 2697 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 2698 |
|
| 2699 |
if (!message) { |
| 2700 |
g_free (uri); |
| 2701 |
|
| 2702 |
return FALSE; |
| 2703 |
} |
| 2704 |
|
| 2705 |
g_free (uri); |
| 2706 |
} |
| 2707 |
|
| 2708 |
if (max_page_size > 0) { |
| 2709 |
gchar *prefer_value; |
| 2710 |
|
| 2711 |
prefer_value = g_strdup_printf ("odata.maxpagesize=%u", max_page_size); |
| 2712 |
|
| 2713 |
soup_message_headers_append (soup_message_get_request_headers (message), "Prefer", prefer_value); |
| 2714 |
|
| 2715 |
g_free (prefer_value); |
| 2716 |
} |
| 2717 |
|
| 2718 |
memset (&rd, 0, sizeof (EM365ResponseData)); |
| 2719 |
|
| 2720 |
rd.json_func = func; |
| 2721 |
rd.func_user_data = func_user_data; |
| 2722 |
rd.out_delta_link = out_delta_link; |
| 2723 |
|
| 2724 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_valued_response_cb, NULL, &rd, cancellable, error); |
| 2725 |
|
| 2726 |
g_clear_object (&message); |
| 2727 |
|
| 2728 |
return success; |
| 2729 |
} |
| 2730 |
|
| 2731 |
/* https://docs.microsoft.com/en-us/graph/api/message-get?view=graph-rest-1.0&tabs=http */ |
| 2732 |
|
| 2733 |
gboolean |
| 2734 |
e_m365_connection_get_mail_message_sync (EM365Connection *cnc, |
| 2735 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 2736 |
const gchar *folder_id, |
| 2737 |
const gchar *message_id, |
| 2738 |
EM365ConnectionRawDataFunc func, |
| 2739 |
gpointer func_user_data, |
| 2740 |
GCancellable *cancellable, |
| 2741 |
GError **error) |
| 2742 |
{ |
| 2743 |
SoupMessage *message; |
| 2744 |
gboolean success; |
| 2745 |
gchar *uri; |
| 2746 |
|
| 2747 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 2748 |
g_return_val_if_fail (folder_id != NULL, FALSE); |
| 2749 |
g_return_val_if_fail (message_id != NULL, FALSE); |
| 2750 |
g_return_val_if_fail (func != NULL, FALSE); |
| 2751 |
|
| 2752 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 2753 |
"messages", |
| 2754 |
message_id, |
| 2755 |
"$value", |
| 2756 |
NULL); |
| 2757 |
|
| 2758 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 2759 |
|
| 2760 |
if (!message) { |
| 2761 |
g_free (uri); |
| 2762 |
|
| 2763 |
return FALSE; |
| 2764 |
} |
| 2765 |
|
| 2766 |
g_free (uri); |
| 2767 |
|
| 2768 |
success = m365_connection_send_request_sync (cnc, message, NULL, func, func_user_data, cancellable, error); |
| 2769 |
|
| 2770 |
g_clear_object (&message); |
| 2771 |
|
| 2772 |
return success; |
| 2773 |
} |
| 2774 |
|
| 2775 |
/* https://docs.microsoft.com/en-us/graph/api/user-post-messages?view=graph-rest-1.0&tabs=http */ |
| 2776 |
|
| 2777 |
gboolean |
| 2778 |
e_m365_connection_create_mail_message_sync (EM365Connection *cnc, |
| 2779 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 2780 |
const gchar *folder_id, /* if NULL, then goes to the Drafts folder */ |
| 2781 |
JsonBuilder *mail_message, /* filled mailMessage object */ |
| 2782 |
EM365MailMessage **out_created_message, /* free with json_object_unref() */ |
| 2783 |
GCancellable *cancellable, |
| 2784 |
GError **error) |
| 2785 |
{ |
| 2786 |
SoupMessage *message; |
| 2787 |
gboolean success; |
| 2788 |
gchar *uri; |
| 2789 |
|
| 2790 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 2791 |
g_return_val_if_fail (mail_message != NULL, FALSE); |
| 2792 |
g_return_val_if_fail (out_created_message != NULL, FALSE); |
| 2793 |
|
| 2794 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 2795 |
folder_id ? "mailFolders" : "messages", |
| 2796 |
folder_id, |
| 2797 |
folder_id ? "messages" : NULL, |
| 2798 |
NULL); |
| 2799 |
|
| 2800 |
message = m365_connection_new_soup_message (SOUP_METHOD_POST, uri, CSM_DEFAULT, error); |
| 2801 |
|
| 2802 |
if (!message) { |
| 2803 |
g_free (uri); |
| 2804 |
|
| 2805 |
return FALSE; |
| 2806 |
} |
| 2807 |
|
| 2808 |
g_free (uri); |
| 2809 |
|
| 2810 |
e_m365_connection_set_json_body (message, mail_message); |
| 2811 |
|
| 2812 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, out_created_message, cancellable, error); |
| 2813 |
|
| 2814 |
g_clear_object (&message); |
| 2815 |
|
| 2816 |
return success; |
| 2817 |
} |
| 2818 |
|
| 2819 |
gboolean |
| 2820 |
e_m365_connection_upload_mail_message_sync (EM365Connection *cnc, |
| 2821 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 2822 |
const gchar *folder_id, /* if NULL, then goes to the Drafts folder */ |
| 2823 |
CamelMimeMessage *mime_message, |
| 2824 |
EM365MailMessage **out_created_message, /* free with json_object_unref() */ |
| 2825 |
GCancellable *cancellable, |
| 2826 |
GError **error) |
| 2827 |
{ |
| 2828 |
SoupMessage *message; |
| 2829 |
CamelStream *mem_stream; |
| 2830 |
gboolean success; |
| 2831 |
gchar *uri; |
| 2832 |
|
| 2833 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 2834 |
g_return_val_if_fail (CAMEL_IS_MIME_MESSAGE (mime_message), FALSE); |
| 2835 |
g_return_val_if_fail (out_created_message != NULL, FALSE); |
| 2836 |
|
| 2837 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 2838 |
folder_id ? "mailFolders" : "messages", |
| 2839 |
folder_id, |
| 2840 |
folder_id ? "messages" : NULL, |
| 2841 |
NULL); |
| 2842 |
|
| 2843 |
message = m365_connection_new_soup_message (SOUP_METHOD_POST, uri, CSM_DEFAULT, error); |
| 2844 |
|
| 2845 |
if (!message) { |
| 2846 |
g_free (uri); |
| 2847 |
|
| 2848 |
return FALSE; |
| 2849 |
} |
| 2850 |
|
| 2851 |
g_free (uri); |
| 2852 |
|
| 2853 |
mem_stream = camel_stream_mem_new (); |
| 2854 |
|
| 2855 |
success = camel_data_wrapper_write_to_stream_sync (CAMEL_DATA_WRAPPER (mime_message), mem_stream, cancellable, error) >= 0 && |
| 2856 |
camel_stream_flush (mem_stream, cancellable, error) != -1; |
| 2857 |
|
| 2858 |
if (success) { |
| 2859 |
GInputStream *input_stream; |
| 2860 |
GByteArray *mime_data; |
| 2861 |
gchar *base64_data; |
| 2862 |
gssize base64_data_len; |
| 2863 |
|
| 2864 |
mime_data = camel_stream_mem_get_byte_array (CAMEL_STREAM_MEM (mem_stream)); |
| 2865 |
base64_data = g_base64_encode (mime_data->data, mime_data->len); |
| 2866 |
base64_data_len = strlen (base64_data); |
| 2867 |
|
| 2868 |
input_stream = g_memory_input_stream_new_from_data (g_steal_pointer (&base64_data), base64_data_len, g_free); |
| 2869 |
|
| 2870 |
e_soup_session_util_set_message_request_body (message, "text/plain", input_stream, base64_data_len); |
| 2871 |
|
| 2872 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, out_created_message, cancellable, error); |
| 2873 |
|
| 2874 |
g_clear_object (&input_stream); |
| 2875 |
} |
| 2876 |
|
| 2877 |
g_clear_object (&mem_stream); |
| 2878 |
g_clear_object (&message); |
| 2879 |
|
| 2880 |
return success; |
| 2881 |
} |
| 2882 |
|
| 2883 |
/* https://docs.microsoft.com/en-us/graph/api/message-post-attachments?view=graph-rest-1.0&tabs=http */ |
| 2884 |
|
| 2885 |
gboolean |
| 2886 |
e_m365_connection_add_mail_message_attachment_sync (EM365Connection *cnc, |
| 2887 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 2888 |
const gchar *message_id, /* the message to add it to */ |
| 2889 |
JsonBuilder *attachment, /* filled attachment object */ |
| 2890 |
gchar **out_attachment_id, |
| 2891 |
GCancellable *cancellable, |
| 2892 |
GError **error) |
| 2893 |
{ |
| 2894 |
SoupMessage *message; |
| 2895 |
JsonObject *added_attachment = NULL; |
| 2896 |
gboolean success; |
| 2897 |
gchar *uri; |
| 2898 |
|
| 2899 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 2900 |
g_return_val_if_fail (attachment != NULL, FALSE); |
| 2901 |
|
| 2902 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 2903 |
"messages", |
| 2904 |
message_id, |
| 2905 |
"attachments", |
| 2906 |
NULL); |
| 2907 |
|
| 2908 |
message = m365_connection_new_soup_message (SOUP_METHOD_POST, uri, CSM_DEFAULT, error); |
| 2909 |
|
| 2910 |
if (!message) { |
| 2911 |
g_free (uri); |
| 2912 |
|
| 2913 |
return FALSE; |
| 2914 |
} |
| 2915 |
|
| 2916 |
g_free (uri); |
| 2917 |
|
| 2918 |
e_m365_connection_set_json_body (message, attachment); |
| 2919 |
|
| 2920 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, &added_attachment, cancellable, error); |
| 2921 |
|
| 2922 |
if (success && added_attachment && out_attachment_id) |
| 2923 |
*out_attachment_id = g_strdup (e_m365_attachment_get_id (added_attachment)); |
| 2924 |
|
| 2925 |
if (added_attachment) |
| 2926 |
json_object_unref (added_attachment); |
| 2927 |
|
| 2928 |
g_clear_object (&message); |
| 2929 |
|
| 2930 |
return success; |
| 2931 |
} |
| 2932 |
|
| 2933 |
/* https://docs.microsoft.com/en-us/graph/api/message-update?view=graph-rest-1.0&tabs=http */ |
| 2934 |
|
| 2935 |
SoupMessage * |
| 2936 |
e_m365_connection_prepare_update_mail_message (EM365Connection *cnc, |
| 2937 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 2938 |
const gchar *message_id, |
| 2939 |
JsonBuilder *mail_message, /* values to update, as a mailMessage object */ |
| 2940 |
GError **error) |
| 2941 |
{ |
| 2942 |
SoupMessage *message; |
| 2943 |
gchar *uri; |
| 2944 |
|
| 2945 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), NULL); |
| 2946 |
g_return_val_if_fail (message_id != NULL, NULL); |
| 2947 |
g_return_val_if_fail (mail_message != NULL, NULL); |
| 2948 |
|
| 2949 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 2950 |
"messages", |
| 2951 |
message_id, |
| 2952 |
NULL, |
| 2953 |
NULL); |
| 2954 |
|
| 2955 |
/* The server returns the mailMessage object back, but it can be ignored here */ |
| 2956 |
message = m365_connection_new_soup_message ("PATCH", uri, CSM_DISABLE_RESPONSE, error); |
| 2957 |
|
| 2958 |
if (!message) { |
| 2959 |
g_free (uri); |
| 2960 |
|
| 2961 |
return NULL; |
| 2962 |
} |
| 2963 |
|
| 2964 |
g_free (uri); |
| 2965 |
|
| 2966 |
e_m365_connection_set_json_body (message, mail_message); |
| 2967 |
|
| 2968 |
return message; |
| 2969 |
} |
| 2970 |
|
| 2971 |
gboolean |
| 2972 |
e_m365_connection_update_mail_message_sync (EM365Connection *cnc, |
| 2973 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 2974 |
const gchar *message_id, |
| 2975 |
JsonBuilder *mail_message, /* values to update, as a mailMessage object */ |
| 2976 |
GCancellable *cancellable, |
| 2977 |
GError **error) |
| 2978 |
{ |
| 2979 |
SoupMessage *message; |
| 2980 |
gboolean success; |
| 2981 |
|
| 2982 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 2983 |
g_return_val_if_fail (message_id != NULL, FALSE); |
| 2984 |
g_return_val_if_fail (mail_message != NULL, FALSE); |
| 2985 |
|
| 2986 |
message = e_m365_connection_prepare_update_mail_message (cnc, user_override, message_id, mail_message, error); |
| 2987 |
|
| 2988 |
if (!message) |
| 2989 |
return FALSE; |
| 2990 |
|
| 2991 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_no_response_cb, NULL, cancellable, error); |
| 2992 |
|
| 2993 |
g_clear_object (&message); |
| 2994 |
|
| 2995 |
return success; |
| 2996 |
} |
| 2997 |
|
| 2998 |
/* https://docs.microsoft.com/en-us/graph/api/message-copy?view=graph-rest-1.0&tabs=http |
| 2999 |
https://docs.microsoft.com/en-us/graph/api/message-move?view=graph-rest-1.0&tabs=http |
| 3000 |
*/ |
| 3001 |
static SoupMessage * |
| 3002 |
e_m365_connection_prepare_copy_move_mail_message (EM365Connection *cnc, |
| 3003 |
const gchar *user_override, |
| 3004 |
const gchar *message_id, |
| 3005 |
const gchar *des_folder_id, |
| 3006 |
gboolean do_copy, |
| 3007 |
GError **error) |
| 3008 |
{ |
| 3009 |
SoupMessage *message; |
| 3010 |
JsonBuilder *builder; |
| 3011 |
gchar *uri; |
| 3012 |
|
| 3013 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), NULL); |
| 3014 |
g_return_val_if_fail (message_id != NULL, NULL); |
| 3015 |
|
| 3016 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 3017 |
"messages", |
| 3018 |
message_id, |
| 3019 |
do_copy ? "copy" : "move", |
| 3020 |
NULL); |
| 3021 |
|
| 3022 |
message = m365_connection_new_soup_message (SOUP_METHOD_POST, uri, CSM_DEFAULT, error); |
| 3023 |
|
| 3024 |
if (!message) { |
| 3025 |
g_free (uri); |
| 3026 |
|
| 3027 |
return FALSE; |
| 3028 |
} |
| 3029 |
|
| 3030 |
g_free (uri); |
| 3031 |
|
| 3032 |
builder = json_builder_new_immutable (); |
| 3033 |
|
| 3034 |
e_m365_json_begin_object_member (builder, NULL); |
| 3035 |
e_m365_json_add_string_member (builder, "destinationId", des_folder_id); |
| 3036 |
e_m365_json_end_object_member (builder); |
| 3037 |
|
| 3038 |
e_m365_connection_set_json_body (message, builder); |
| 3039 |
|
| 3040 |
g_object_unref (builder); |
| 3041 |
|
| 3042 |
return message; |
| 3043 |
} |
| 3044 |
|
| 3045 |
/* out_des_message_ids: Camel-pooled gchar *, new ids, in the same order as in message_ids; can be partial */ |
| 3046 |
gboolean |
| 3047 |
e_m365_connection_copy_move_mail_messages_sync (EM365Connection *cnc, |
| 3048 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 3049 |
const GSList *message_ids, /* const gchar * */ |
| 3050 |
const gchar *des_folder_id, |
| 3051 |
gboolean do_copy, |
| 3052 |
GSList **out_des_message_ids, /* Camel-pooled gchar * */ |
| 3053 |
GCancellable *cancellable, |
| 3054 |
GError **error) |
| 3055 |
{ |
| 3056 |
gboolean success = TRUE; |
| 3057 |
|
| 3058 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 3059 |
g_return_val_if_fail (message_ids != NULL, FALSE); |
| 3060 |
g_return_val_if_fail (des_folder_id != NULL, FALSE); |
| 3061 |
g_return_val_if_fail (out_des_message_ids != NULL, FALSE); |
| 3062 |
|
| 3063 |
*out_des_message_ids = NULL; |
| 3064 |
|
| 3065 |
if (g_slist_next (message_ids)) { |
| 3066 |
GPtrArray *requests; |
| 3067 |
GSList *link; |
| 3068 |
guint total, done = 0; |
| 3069 |
|
| 3070 |
total = g_slist_length ((GSList *) message_ids); |
| 3071 |
requests = g_ptr_array_new_full (MIN (E_M365_BATCH_MAX_REQUESTS, MIN (total, 50)), g_object_unref); |
| 3072 |
|
| 3073 |
for (link = (GSList *) message_ids; link && success; link = g_slist_next (link)) { |
| 3074 |
const gchar *id = link->data; |
| 3075 |
SoupMessage *message; |
| 3076 |
|
| 3077 |
message = e_m365_connection_prepare_copy_move_mail_message (cnc, user_override, id, des_folder_id, do_copy, error); |
| 3078 |
|
| 3079 |
if (!message) { |
| 3080 |
success = FALSE; |
| 3081 |
break; |
| 3082 |
} |
| 3083 |
|
| 3084 |
g_ptr_array_add (requests, message); |
| 3085 |
|
| 3086 |
if (requests->len == E_M365_BATCH_MAX_REQUESTS || !link->next) { |
| 3087 |
if (requests->len == 1) { |
| 3088 |
JsonObject *response = NULL; |
| 3089 |
|
| 3090 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, &response, cancellable, error); |
| 3091 |
|
| 3092 |
if (response) { |
| 3093 |
*out_des_message_ids = g_slist_prepend (*out_des_message_ids, |
| 3094 |
(gpointer) camel_pstring_strdup (e_m365_mail_message_get_id (response))); |
| 3095 |
json_object_unref (response); |
| 3096 |
} else { |
| 3097 |
success = FALSE; |
| 3098 |
} |
| 3099 |
} else { |
| 3100 |
success = e_m365_connection_batch_request_sync (cnc, E_M365_API_V1_0, requests, cancellable, error); |
| 3101 |
|
| 3102 |
if (success) { |
| 3103 |
guint ii; |
| 3104 |
|
| 3105 |
for (ii = 0; success && ii < requests->len; ii++) { |
| 3106 |
JsonNode *node = NULL; |
| 3107 |
|
| 3108 |
message = g_ptr_array_index (requests, ii); |
| 3109 |
|
| 3110 |
success = e_m365_connection_json_node_from_message (message, NULL, &node, cancellable, error); |
| 3111 |
|
| 3112 |
if (success && node && JSON_NODE_HOLDS_OBJECT (node)) { |
| 3113 |
JsonObject *response; |
| 3114 |
|
| 3115 |
response = json_node_get_object (node); |
| 3116 |
|
| 3117 |
if (response) { |
| 3118 |
*out_des_message_ids = g_slist_prepend (*out_des_message_ids, |
| 3119 |
(gpointer) camel_pstring_strdup (e_m365_mail_message_get_id (response))); |
| 3120 |
} else { |
| 3121 |
success = FALSE; |
| 3122 |
} |
| 3123 |
} else { |
| 3124 |
success = FALSE; |
| 3125 |
} |
| 3126 |
|
| 3127 |
if (node) |
| 3128 |
json_node_unref (node); |
| 3129 |
} |
| 3130 |
} |
| 3131 |
} |
| 3132 |
|
| 3133 |
g_ptr_array_remove_range (requests, 0, requests->len); |
| 3134 |
|
| 3135 |
done += requests->len; |
| 3136 |
|
| 3137 |
camel_operation_progress (cancellable, done * 100.0 / total); |
| 3138 |
} |
| 3139 |
} |
| 3140 |
|
| 3141 |
g_ptr_array_free (requests, TRUE); |
| 3142 |
} else { |
| 3143 |
SoupMessage *message; |
| 3144 |
|
| 3145 |
message = e_m365_connection_prepare_copy_move_mail_message (cnc, user_override, message_ids->data, des_folder_id, do_copy, error); |
| 3146 |
|
| 3147 |
if (message) { |
| 3148 |
JsonObject *response = NULL; |
| 3149 |
|
| 3150 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, &response, cancellable, error); |
| 3151 |
|
| 3152 |
if (response) { |
| 3153 |
*out_des_message_ids = g_slist_prepend (*out_des_message_ids, |
| 3154 |
(gpointer) camel_pstring_strdup (e_m365_mail_message_get_id (response))); |
| 3155 |
json_object_unref (response); |
| 3156 |
} else { |
| 3157 |
success = FALSE; |
| 3158 |
} |
| 3159 |
|
| 3160 |
g_clear_object (&message); |
| 3161 |
} else { |
| 3162 |
success = FALSE; |
| 3163 |
} |
| 3164 |
} |
| 3165 |
|
| 3166 |
*out_des_message_ids = g_slist_reverse (*out_des_message_ids); |
| 3167 |
|
| 3168 |
return success; |
| 3169 |
} |
| 3170 |
|
| 3171 |
/* https://docs.microsoft.com/en-us/graph/api/message-delete?view=graph-rest-1.0&tabs=http */ |
| 3172 |
|
| 3173 |
static SoupMessage * |
| 3174 |
e_m365_connection_prepare_delete_mail_message (EM365Connection *cnc, |
| 3175 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 3176 |
const gchar *message_id, |
| 3177 |
GError **error) |
| 3178 |
{ |
| 3179 |
SoupMessage *message; |
| 3180 |
gchar *uri; |
| 3181 |
|
| 3182 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), NULL); |
| 3183 |
g_return_val_if_fail (message_id != NULL, NULL); |
| 3184 |
|
| 3185 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 3186 |
"messages", |
| 3187 |
message_id, |
| 3188 |
NULL, |
| 3189 |
NULL); |
| 3190 |
|
| 3191 |
message = m365_connection_new_soup_message (SOUP_METHOD_DELETE, uri, CSM_DEFAULT, error); |
| 3192 |
|
| 3193 |
if (!message) { |
| 3194 |
g_free (uri); |
| 3195 |
|
| 3196 |
return NULL; |
| 3197 |
} |
| 3198 |
|
| 3199 |
g_free (uri); |
| 3200 |
|
| 3201 |
return message; |
| 3202 |
} |
| 3203 |
|
| 3204 |
gboolean |
| 3205 |
e_m365_connection_delete_mail_messages_sync (EM365Connection *cnc, |
| 3206 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 3207 |
const GSList *message_ids, /* const gchar * */ |
| 3208 |
GSList **out_deleted_ids, /* (transfer container): const gchar *, borrowed from message_ids */ |
| 3209 |
GCancellable *cancellable, |
| 3210 |
GError **error) |
| 3211 |
{ |
| 3212 |
gboolean success = TRUE; |
| 3213 |
|
| 3214 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 3215 |
g_return_val_if_fail (message_ids != NULL, FALSE); |
| 3216 |
|
| 3217 |
if (g_slist_next (message_ids)) { |
| 3218 |
GPtrArray *requests; |
| 3219 |
GSList *link, *from_link = (GSList *) message_ids; |
| 3220 |
guint total, done = 0; |
| 3221 |
|
| 3222 |
total = g_slist_length ((GSList *) message_ids); |
| 3223 |
requests = g_ptr_array_new_full (MIN (E_M365_BATCH_MAX_REQUESTS, MIN (total, 50)), g_object_unref); |
| 3224 |
|
| 3225 |
for (link = (GSList *) message_ids; link && success; link = g_slist_next (link)) { |
| 3226 |
const gchar *id = link->data; |
| 3227 |
SoupMessage *message; |
| 3228 |
|
| 3229 |
message = e_m365_connection_prepare_delete_mail_message (cnc, user_override, id, error); |
| 3230 |
|
| 3231 |
if (!message) { |
| 3232 |
success = FALSE; |
| 3233 |
break; |
| 3234 |
} |
| 3235 |
|
| 3236 |
g_ptr_array_add (requests, message); |
| 3237 |
|
| 3238 |
if (requests->len == E_M365_BATCH_MAX_REQUESTS || !link->next) { |
| 3239 |
if (requests->len == 1) { |
| 3240 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_no_response_cb, NULL, cancellable, error); |
| 3241 |
} else { |
| 3242 |
success = e_m365_connection_batch_request_sync (cnc, E_M365_API_V1_0, requests, cancellable, error); |
| 3243 |
} |
| 3244 |
|
| 3245 |
if (success && out_deleted_ids) { |
| 3246 |
while (from_link) { |
| 3247 |
*out_deleted_ids = g_slist_prepend (*out_deleted_ids, from_link->data); |
| 3248 |
|
| 3249 |
if (from_link == link) |
| 3250 |
break; |
| 3251 |
|
| 3252 |
from_link = g_slist_next (from_link); |
| 3253 |
} |
| 3254 |
} |
| 3255 |
|
| 3256 |
g_ptr_array_remove_range (requests, 0, requests->len); |
| 3257 |
from_link = g_slist_next (link); |
| 3258 |
|
| 3259 |
done += requests->len; |
| 3260 |
|
| 3261 |
camel_operation_progress (cancellable, done * 100.0 / total); |
| 3262 |
} |
| 3263 |
} |
| 3264 |
|
| 3265 |
g_ptr_array_free (requests, TRUE); |
| 3266 |
} else { |
| 3267 |
SoupMessage *message; |
| 3268 |
|
| 3269 |
message = e_m365_connection_prepare_delete_mail_message (cnc, user_override, message_ids->data, error); |
| 3270 |
|
| 3271 |
if (message) { |
| 3272 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_no_response_cb, NULL, cancellable, error); |
| 3273 |
|
| 3274 |
if (success && out_deleted_ids) |
| 3275 |
*out_deleted_ids = g_slist_prepend (*out_deleted_ids, message_ids->data); |
| 3276 |
|
| 3277 |
g_clear_object (&message); |
| 3278 |
} else { |
| 3279 |
success = FALSE; |
| 3280 |
} |
| 3281 |
} |
| 3282 |
|
| 3283 |
if (out_deleted_ids && *out_deleted_ids && g_slist_next (*out_deleted_ids)) |
| 3284 |
*out_deleted_ids = g_slist_reverse (*out_deleted_ids); |
| 3285 |
|
| 3286 |
return success; |
| 3287 |
} |
| 3288 |
|
| 3289 |
/* https://docs.microsoft.com/en-us/graph/api/message-send?view=graph-rest-1.0&tabs=http */ |
| 3290 |
|
| 3291 |
gboolean |
| 3292 |
e_m365_connection_send_mail_message_sync (EM365Connection *cnc, |
| 3293 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 3294 |
const gchar *message_id, |
| 3295 |
GCancellable *cancellable, |
| 3296 |
GError **error) |
| 3297 |
{ |
| 3298 |
SoupMessage *message; |
| 3299 |
gboolean success; |
| 3300 |
gchar *uri; |
| 3301 |
|
| 3302 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 3303 |
g_return_val_if_fail (message_id != NULL, FALSE); |
| 3304 |
|
| 3305 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 3306 |
"messages", |
| 3307 |
message_id, |
| 3308 |
"send", |
| 3309 |
NULL); |
| 3310 |
|
| 3311 |
message = m365_connection_new_soup_message (SOUP_METHOD_POST, uri, CSM_DEFAULT, error); |
| 3312 |
|
| 3313 |
if (!message) { |
| 3314 |
g_free (uri); |
| 3315 |
|
| 3316 |
return FALSE; |
| 3317 |
} |
| 3318 |
|
| 3319 |
g_free (uri); |
| 3320 |
|
| 3321 |
soup_message_headers_append (soup_message_get_request_headers (message), "Content-Length", "0"); |
| 3322 |
|
| 3323 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_no_response_cb, NULL, cancellable, error); |
| 3324 |
|
| 3325 |
g_clear_object (&message); |
| 3326 |
|
| 3327 |
return success; |
| 3328 |
} |
| 3329 |
|
| 3330 |
/* https://docs.microsoft.com/en-us/graph/api/user-sendmail?view=graph-rest-1.0&tabs=http */ |
| 3331 |
|
| 3332 |
gboolean |
| 3333 |
e_m365_connection_send_mail_sync (EM365Connection *cnc, |
| 3334 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 3335 |
JsonBuilder *request, /* filled sendMail object */ |
| 3336 |
GCancellable *cancellable, |
| 3337 |
GError **error) |
| 3338 |
{ |
| 3339 |
SoupMessage *message; |
| 3340 |
gboolean success; |
| 3341 |
gchar *uri; |
| 3342 |
|
| 3343 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 3344 |
g_return_val_if_fail (request != NULL, FALSE); |
| 3345 |
|
| 3346 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 3347 |
"sendMail", NULL, NULL, NULL); |
| 3348 |
|
| 3349 |
message = m365_connection_new_soup_message (SOUP_METHOD_POST, uri, CSM_DEFAULT, error); |
| 3350 |
|
| 3351 |
if (!message) { |
| 3352 |
g_free (uri); |
| 3353 |
|
| 3354 |
return FALSE; |
| 3355 |
} |
| 3356 |
|
| 3357 |
g_free (uri); |
| 3358 |
|
| 3359 |
e_m365_connection_set_json_body (message, request); |
| 3360 |
|
| 3361 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_no_response_cb, NULL, cancellable, error); |
| 3362 |
|
| 3363 |
g_clear_object (&message); |
| 3364 |
|
| 3365 |
return success; |
| 3366 |
} |
| 3367 |
|
| 3368 |
gboolean |
| 3369 |
e_m365_connection_send_mail_mime_sync (EM365Connection *cnc, |
| 3370 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 3371 |
const gchar *base64_mime, |
| 3372 |
gssize base64_mime_length, |
| 3373 |
GCancellable *cancellable, |
| 3374 |
GError **error) |
| 3375 |
{ |
| 3376 |
SoupMessage *message; |
| 3377 |
gboolean success; |
| 3378 |
gchar *uri; |
| 3379 |
|
| 3380 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 3381 |
g_return_val_if_fail (base64_mime != NULL, FALSE); |
| 3382 |
|
| 3383 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 3384 |
"sendMail", NULL, NULL, NULL); |
| 3385 |
|
| 3386 |
message = m365_connection_new_soup_message (SOUP_METHOD_POST, uri, CSM_DEFAULT, error); |
| 3387 |
|
| 3388 |
if (!message) { |
| 3389 |
g_free (uri); |
| 3390 |
|
| 3391 |
return FALSE; |
| 3392 |
} |
| 3393 |
|
| 3394 |
g_free (uri); |
| 3395 |
|
| 3396 |
if (base64_mime_length < 0) |
| 3397 |
base64_mime_length = strlen (base64_mime); |
| 3398 |
|
| 3399 |
soup_message_headers_set_content_type (soup_message_get_request_headers (message), "text/plain", NULL); |
| 3400 |
e_soup_session_util_set_message_request_body_from_data (message, FALSE, "text/plain", base64_mime, base64_mime_length, NULL); |
| 3401 |
|
| 3402 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_no_response_cb, NULL, cancellable, error); |
| 3403 |
|
| 3404 |
g_clear_object (&message); |
| 3405 |
|
| 3406 |
return success; |
| 3407 |
} |
| 3408 |
|
| 3409 |
/* https://docs.microsoft.com/en-us/graph/api/contactfolder-get?view=graph-rest-1.0&tabs=http */ |
| 3410 |
|
| 3411 |
gboolean |
| 3412 |
e_m365_connection_get_contacts_folder_sync (EM365Connection *cnc, |
| 3413 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 3414 |
const gchar *folder_id, /* nullable - then the default 'contacts' folder is returned */ |
| 3415 |
const gchar *select, /* nullable - properties to select */ |
| 3416 |
EM365Folder **out_folder, |
| 3417 |
GCancellable *cancellable, |
| 3418 |
GError **error) |
| 3419 |
{ |
| 3420 |
SoupMessage *message; |
| 3421 |
gchar *uri; |
| 3422 |
gboolean success; |
| 3423 |
|
| 3424 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 3425 |
g_return_val_if_fail (out_folder != NULL, FALSE); |
| 3426 |
|
| 3427 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 3428 |
"contactFolders", |
| 3429 |
folder_id ? folder_id : "contacts", |
| 3430 |
NULL, |
| 3431 |
NULL); |
| 3432 |
|
| 3433 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 3434 |
|
| 3435 |
if (!message) { |
| 3436 |
g_free (uri); |
| 3437 |
|
| 3438 |
return FALSE; |
| 3439 |
} |
| 3440 |
|
| 3441 |
g_free (uri); |
| 3442 |
|
| 3443 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, out_folder, cancellable, error); |
| 3444 |
|
| 3445 |
g_clear_object (&message); |
| 3446 |
|
| 3447 |
return success; |
| 3448 |
} |
| 3449 |
|
| 3450 |
/* https://docs.microsoft.com/en-us/graph/api/profilephoto-get?view=graph-rest-1.0 */ |
| 3451 |
|
| 3452 |
gboolean |
| 3453 |
e_m365_connection_get_contact_photo_sync (EM365Connection *cnc, |
| 3454 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 3455 |
const gchar *folder_id, |
| 3456 |
const gchar *contact_id, |
| 3457 |
GByteArray **out_photo, |
| 3458 |
GCancellable *cancellable, |
| 3459 |
GError **error) |
| 3460 |
{ |
| 3461 |
SoupMessage *message; |
| 3462 |
gboolean success; |
| 3463 |
gchar *uri; |
| 3464 |
|
| 3465 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 3466 |
g_return_val_if_fail (folder_id != NULL, FALSE); |
| 3467 |
g_return_val_if_fail (contact_id != NULL, FALSE); |
| 3468 |
g_return_val_if_fail (out_photo != NULL, FALSE); |
| 3469 |
|
| 3470 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 3471 |
"contactFolders", |
| 3472 |
folder_id, |
| 3473 |
"contacts", |
| 3474 |
"", contact_id, |
| 3475 |
"", "photo", |
| 3476 |
"", "$value", |
| 3477 |
NULL); |
| 3478 |
|
| 3479 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 3480 |
|
| 3481 |
if (!message) { |
| 3482 |
g_free (uri); |
| 3483 |
|
| 3484 |
return FALSE; |
| 3485 |
} |
| 3486 |
|
| 3487 |
g_free (uri); |
| 3488 |
|
| 3489 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_to_byte_array_cb, out_photo, cancellable, error); |
| 3490 |
|
| 3491 |
g_clear_object (&message); |
| 3492 |
|
| 3493 |
return success; |
| 3494 |
} |
| 3495 |
|
| 3496 |
/* https://docs.microsoft.com/en-us/graph/api/profilephoto-update?view=graph-rest-1.0&tabs=http */ |
| 3497 |
|
| 3498 |
gboolean |
| 3499 |
e_m365_connection_update_contact_photo_sync (EM365Connection *cnc, |
| 3500 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 3501 |
const gchar *folder_id, |
| 3502 |
const gchar *contact_id, |
| 3503 |
const GByteArray *jpeg_photo, /* nullable, to remove the photo */ |
| 3504 |
GCancellable *cancellable, |
| 3505 |
GError **error) |
| 3506 |
{ |
| 3507 |
SoupMessage *message; |
| 3508 |
gboolean success; |
| 3509 |
gchar *uri; |
| 3510 |
|
| 3511 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 3512 |
|
| 3513 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 3514 |
"contactFolders", |
| 3515 |
folder_id, |
| 3516 |
"contacts", |
| 3517 |
"", contact_id, |
| 3518 |
"", "photo", |
| 3519 |
"", "$value", |
| 3520 |
NULL); |
| 3521 |
|
| 3522 |
message = m365_connection_new_soup_message (SOUP_METHOD_PUT, uri, CSM_DEFAULT, error); |
| 3523 |
|
| 3524 |
if (!message) { |
| 3525 |
g_free (uri); |
| 3526 |
|
| 3527 |
return FALSE; |
| 3528 |
} |
| 3529 |
|
| 3530 |
g_free (uri); |
| 3531 |
|
| 3532 |
soup_message_headers_set_content_type (soup_message_get_request_headers (message), "image/jpeg", NULL); |
| 3533 |
soup_message_headers_set_content_length (soup_message_get_request_headers (message), jpeg_photo ? jpeg_photo->len : 0); |
| 3534 |
|
| 3535 |
if (jpeg_photo) |
| 3536 |
e_soup_session_util_set_message_request_body_from_data (message, FALSE, "image/jpeg", jpeg_photo->data, jpeg_photo->len, NULL); |
| 3537 |
|
| 3538 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_no_response_cb, NULL, cancellable, error); |
| 3539 |
|
| 3540 |
g_clear_object (&message); |
| 3541 |
|
| 3542 |
return success; |
| 3543 |
} |
| 3544 |
|
| 3545 |
/* https://docs.microsoft.com/en-us/graph/api/contact-get?view=graph-rest-1.0&tabs=http */ |
| 3546 |
|
| 3547 |
static SoupMessage * |
| 3548 |
e_m365_connection_prepare_get_contact (EM365Connection *cnc, |
| 3549 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 3550 |
const gchar *folder_id, |
| 3551 |
const gchar *contact_id, |
| 3552 |
GError **error) |
| 3553 |
{ |
| 3554 |
SoupMessage *message; |
| 3555 |
gchar *uri; |
| 3556 |
|
| 3557 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), NULL); |
| 3558 |
g_return_val_if_fail (folder_id != NULL, NULL); |
| 3559 |
g_return_val_if_fail (contact_id != NULL, NULL); |
| 3560 |
|
| 3561 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 3562 |
"contactFolders", |
| 3563 |
folder_id, |
| 3564 |
"contacts", |
| 3565 |
"", contact_id, |
| 3566 |
NULL); |
| 3567 |
|
| 3568 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 3569 |
|
| 3570 |
g_free (uri); |
| 3571 |
|
| 3572 |
return message; |
| 3573 |
} |
| 3574 |
|
| 3575 |
gboolean |
| 3576 |
e_m365_connection_get_contact_sync (EM365Connection *cnc, |
| 3577 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 3578 |
const gchar *folder_id, |
| 3579 |
const gchar *contact_id, |
| 3580 |
EM365Contact **out_contact, |
| 3581 |
GCancellable *cancellable, |
| 3582 |
GError **error) |
| 3583 |
{ |
| 3584 |
SoupMessage *message; |
| 3585 |
gboolean success; |
| 3586 |
|
| 3587 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 3588 |
g_return_val_if_fail (folder_id != NULL, FALSE); |
| 3589 |
g_return_val_if_fail (contact_id != NULL, FALSE); |
| 3590 |
g_return_val_if_fail (out_contact != NULL, FALSE); |
| 3591 |
|
| 3592 |
message = e_m365_connection_prepare_get_contact (cnc, user_override, folder_id, contact_id, error); |
| 3593 |
|
| 3594 |
if (!message) |
| 3595 |
return FALSE; |
| 3596 |
|
| 3597 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, out_contact, cancellable, error); |
| 3598 |
|
| 3599 |
g_clear_object (&message); |
| 3600 |
|
| 3601 |
return success; |
| 3602 |
} |
| 3603 |
|
| 3604 |
/* https://learn.microsoft.com/en-us/graph/api/orgcontact-get?view=graph-rest-1.0&tabs=http */ |
| 3605 |
|
| 3606 |
static SoupMessage * |
| 3607 |
e_m365_connection_prepare_get_org_contact (EM365Connection *cnc, |
| 3608 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 3609 |
const gchar *contact_id, |
| 3610 |
GError **error) |
| 3611 |
{ |
| 3612 |
SoupMessage *message; |
| 3613 |
gchar *uri; |
| 3614 |
|
| 3615 |
uri = e_m365_connection_construct_uri (cnc, FALSE, user_override, E_M365_API_V1_0, |
| 3616 |
"contacts", |
| 3617 |
NULL, |
| 3618 |
NULL, |
| 3619 |
contact_id, |
| 3620 |
"$select", ORG_CONTACTS_PROPS, |
| 3621 |
NULL); |
| 3622 |
|
| 3623 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 3624 |
|
| 3625 |
g_free (uri); |
| 3626 |
|
| 3627 |
return message; |
| 3628 |
} |
| 3629 |
|
| 3630 |
/* https://learn.microsoft.com/en-us/graph/api/user-get?view=graph-rest-1.0&tabs=http */ |
| 3631 |
|
| 3632 |
static SoupMessage * |
| 3633 |
e_m365_connection_prepare_get_user (EM365Connection *cnc, |
| 3634 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 3635 |
const gchar *user_id, |
| 3636 |
GError **error) |
| 3637 |
{ |
| 3638 |
SoupMessage *message; |
| 3639 |
gchar *uri; |
| 3640 |
|
| 3641 |
uri = e_m365_connection_construct_uri (cnc, FALSE, user_override, E_M365_API_V1_0, |
| 3642 |
"users", |
| 3643 |
NULL, |
| 3644 |
NULL, |
| 3645 |
user_id, |
| 3646 |
"$select", USERS_PROPS, |
| 3647 |
NULL); |
| 3648 |
|
| 3649 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 3650 |
|
| 3651 |
g_free (uri); |
| 3652 |
|
| 3653 |
return message; |
| 3654 |
} |
| 3655 |
|
| 3656 |
static gboolean |
| 3657 |
e_m365_connection_get_contacts_internal_sync (EM365Connection *cnc, |
| 3658 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 3659 |
EM365FolderKind folder_kind, |
| 3660 |
const gchar *folder_id, |
| 3661 |
GPtrArray *ids, /* const gchar * */ |
| 3662 |
GPtrArray **out_contacts, /* EM365Contact * */ |
| 3663 |
GCancellable *cancellable, |
| 3664 |
GError **error) |
| 3665 |
{ |
| 3666 |
GPtrArray *requests; |
| 3667 |
guint total, done = 0, ii; |
| 3668 |
gboolean success = TRUE; |
| 3669 |
|
| 3670 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 3671 |
g_return_val_if_fail (folder_kind == E_M365_FOLDER_KIND_CONTACTS || |
| 3672 |
folder_kind == E_M365_FOLDER_KIND_ORG_CONTACTS || |
| 3673 |
folder_kind == E_M365_FOLDER_KIND_USERS, FALSE); |
| 3674 |
if (folder_kind == E_M365_FOLDER_KIND_CONTACTS) |
| 3675 |
g_return_val_if_fail (folder_id != NULL, FALSE); |
| 3676 |
g_return_val_if_fail (ids != NULL, FALSE); |
| 3677 |
g_return_val_if_fail (out_contacts != NULL, FALSE); |
| 3678 |
|
| 3679 |
*out_contacts = NULL; |
| 3680 |
total = ids->len; |
| 3681 |
requests = g_ptr_array_new_full (MIN (E_M365_BATCH_MAX_REQUESTS, MIN (total, 50)), g_object_unref); |
| 3682 |
|
| 3683 |
for (ii = 0; ii < ids->len && success; ii++) { |
| 3684 |
const gchar *id = g_ptr_array_index (ids, ii); |
| 3685 |
SoupMessage *message = NULL; |
| 3686 |
|
| 3687 |
switch (folder_kind) { |
| 3688 |
case E_M365_FOLDER_KIND_CONTACTS: |
| 3689 |
message = e_m365_connection_prepare_get_contact (cnc, user_override, folder_id, id, error); |
| 3690 |
break; |
| 3691 |
case E_M365_FOLDER_KIND_ORG_CONTACTS: |
| 3692 |
message = e_m365_connection_prepare_get_org_contact (cnc, user_override, id, error); |
| 3693 |
break; |
| 3694 |
case E_M365_FOLDER_KIND_USERS: |
| 3695 |
message = e_m365_connection_prepare_get_user (cnc, user_override, id, error); |
| 3696 |
break; |
| 3697 |
default: |
| 3698 |
break; |
| 3699 |
} |
| 3700 |
|
| 3701 |
if (!message) { |
| 3702 |
success = FALSE; |
| 3703 |
break; |
| 3704 |
} |
| 3705 |
|
| 3706 |
g_ptr_array_add (requests, message); |
| 3707 |
|
| 3708 |
if (requests->len == E_M365_BATCH_MAX_REQUESTS || ii + 1 >= ids->len) { |
| 3709 |
if (!*out_contacts) |
| 3710 |
*out_contacts = g_ptr_array_new_full (ids->len, (GDestroyNotify) json_object_unref); |
| 3711 |
|
| 3712 |
if (requests->len == 1) { |
| 3713 |
EM365Contact *contact = NULL; |
| 3714 |
|
| 3715 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, &contact, cancellable, error); |
| 3716 |
|
| 3717 |
if (success) |
| 3718 |
g_ptr_array_add (*out_contacts, contact); |
| 3719 |
} else { |
| 3720 |
success = e_m365_connection_batch_request_sync (cnc, E_M365_API_V1_0, requests, cancellable, error); |
| 3721 |
|
| 3722 |
if (success) { |
| 3723 |
guint jj; |
| 3724 |
|
| 3725 |
for (jj = 0; jj < requests->len && success; jj++) { |
| 3726 |
JsonNode *node = NULL; |
| 3727 |
|
| 3728 |
message = g_ptr_array_index (requests, jj); |
| 3729 |
success = e_m365_connection_json_node_from_message (message, NULL, &node, cancellable, error); |
| 3730 |
|
| 3731 |
if (success && node && JSON_NODE_HOLDS_OBJECT (node)) { |
| 3732 |
JsonObject *response; |
| 3733 |
|
| 3734 |
response = json_node_get_object (node); |
| 3735 |
|
| 3736 |
if (response) |
| 3737 |
g_ptr_array_add (*out_contacts, json_object_ref (response)); |
| 3738 |
else |
| 3739 |
success = FALSE; |
| 3740 |
} else { |
| 3741 |
success = FALSE; |
| 3742 |
} |
| 3743 |
|
| 3744 |
if (node) |
| 3745 |
json_node_unref (node); |
| 3746 |
} |
| 3747 |
} |
| 3748 |
} |
| 3749 |
|
| 3750 |
g_ptr_array_remove_range (requests, 0, requests->len); |
| 3751 |
|
| 3752 |
done += requests->len; |
| 3753 |
|
| 3754 |
camel_operation_progress (cancellable, done * 100.0 / total); |
| 3755 |
} |
| 3756 |
} |
| 3757 |
|
| 3758 |
g_ptr_array_free (requests, TRUE); |
| 3759 |
|
| 3760 |
if (!success && *out_contacts && !(*out_contacts)->len) |
| 3761 |
g_clear_pointer (out_contacts, g_ptr_array_unref); |
| 3762 |
|
| 3763 |
return success; |
| 3764 |
} |
| 3765 |
|
| 3766 |
/* https://learn.microsoft.com/en-us/graph/aad-advanced-queries?tabs=http |
| 3767 |
https://learn.microsoft.com/en-us/graph/search-query-parameter?tabs=http */ |
| 3768 |
|
| 3769 |
static gboolean |
| 3770 |
e_m365_connection_search_contacts_internal_sync (EM365Connection *cnc, |
| 3771 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 3772 |
EM365FolderKind folder_kind, |
| 3773 |
const gchar *folder_id, |
| 3774 |
const gchar *search_text, |
| 3775 |
GSList **out_contacts, /* transfer full, EM365Contact * */ |
| 3776 |
GCancellable *cancellable, |
| 3777 |
GError **error) |
| 3778 |
{ |
| 3779 |
EM365ResponseData rd; |
| 3780 |
SoupMessage *message = NULL; |
| 3781 |
const gchar *api_part = NULL; |
| 3782 |
const gchar *kind_str = NULL; |
| 3783 |
const gchar *kind_path_str = NULL; |
| 3784 |
GString *search_str = NULL; |
| 3785 |
gchar *uri, *filter = NULL, *mail_filter = NULL; |
| 3786 |
gboolean success = TRUE; |
| 3787 |
|
| 3788 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 3789 |
g_return_val_if_fail (folder_kind == E_M365_FOLDER_KIND_CONTACTS || |
| 3790 |
folder_kind == E_M365_FOLDER_KIND_ORG_CONTACTS || |
| 3791 |
folder_kind == E_M365_FOLDER_KIND_USERS, FALSE); |
| 3792 |
if (folder_kind == E_M365_FOLDER_KIND_CONTACTS) |
| 3793 |
g_return_val_if_fail (folder_id != NULL, FALSE); |
| 3794 |
g_return_val_if_fail (search_text != NULL, FALSE); |
| 3795 |
g_return_val_if_fail (out_contacts != NULL, FALSE); |
| 3796 |
|
| 3797 |
*out_contacts = NULL; |
| 3798 |
|
| 3799 |
if (strchr (search_text, '\'')) { |
| 3800 |
search_str = e_ews_common_utils_str_replace_string (search_text, "'", "''"); |
| 3801 |
search_text = search_str->str; |
| 3802 |
} |
| 3803 |
|
| 3804 |
if (strchr (search_text, '\"')) { |
| 3805 |
GString *tmp; |
| 3806 |
|
| 3807 |
/* remove double quotes */ |
| 3808 |
tmp = e_ews_common_utils_str_replace_string (search_text, "\"", ""); |
| 3809 |
if (search_str) |
| 3810 |
g_string_free (search_str, TRUE); |
| 3811 |
|
| 3812 |
search_str = tmp; |
| 3813 |
search_text = search_str->str; |
| 3814 |
} |
| 3815 |
|
| 3816 |
switch (folder_kind) { |
| 3817 |
case E_M365_FOLDER_KIND_CONTACTS: |
| 3818 |
g_return_val_if_fail (folder_id != NULL, FALSE); |
| 3819 |
kind_str = "contactFolders"; |
| 3820 |
kind_path_str = "contacts"; |
| 3821 |
mail_filter = g_strconcat ("\"emailAddresses:", search_text, "\"", NULL); |
| 3822 |
break; |
| 3823 |
case E_M365_FOLDER_KIND_ORG_CONTACTS: |
| 3824 |
api_part = "contacts"; |
| 3825 |
mail_filter = g_strconcat ("\"mail:", search_text, "\"", NULL); |
| 3826 |
break; |
| 3827 |
case E_M365_FOLDER_KIND_USERS: |
| 3828 |
api_part = "users"; |
| 3829 |
mail_filter = g_strconcat ("\"mail:", search_text, "\"", NULL); |
| 3830 |
break; |
| 3831 |
default: |
| 3832 |
g_warn_if_reached (); |
| 3833 |
break; |
| 3834 |
} |
| 3835 |
|
| 3836 |
filter = g_strconcat ( |
| 3837 |
"\"displayName:", search_text, "\"" |
| 3838 |
" OR " |
| 3839 |
"\"givenName:", search_text, "\"" |
| 3840 |
" OR " |
| 3841 |
"\"surname:", search_text, "\"", |
| 3842 |
mail_filter ? " OR " : NULL, |
| 3843 |
mail_filter, |
| 3844 |
NULL); |
| 3845 |
|
| 3846 |
uri = e_m365_connection_construct_uri (cnc, api_part == NULL, user_override, E_M365_API_V1_0, api_part, |
| 3847 |
kind_str, |
| 3848 |
folder_id, |
| 3849 |
kind_path_str, |
| 3850 |
"$top", "50", /* no need for too many; server default is currently 250 */ |
| 3851 |
"$count", "true", /* required */ |
| 3852 |
"$search", filter, |
| 3853 |
NULL); |
| 3854 |
|
| 3855 |
if (search_str) |
| 3856 |
g_string_free (search_str, TRUE); |
| 3857 |
g_free (mail_filter); |
| 3858 |
g_free (filter); |
| 3859 |
|
| 3860 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 3861 |
|
| 3862 |
if (!message) { |
| 3863 |
g_free (uri); |
| 3864 |
|
| 3865 |
return FALSE; |
| 3866 |
} |
| 3867 |
|
| 3868 |
g_free (uri); |
| 3869 |
|
| 3870 |
/* required */ |
| 3871 |
soup_message_headers_append (soup_message_get_request_headers (message), "ConsistencyLevel", "eventual"); |
| 3872 |
|
| 3873 |
memset (&rd, 0, sizeof (EM365ResponseData)); |
| 3874 |
|
| 3875 |
rd.out_items = out_contacts; |
| 3876 |
|
| 3877 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_valued_response_cb, NULL, &rd, cancellable, error); |
| 3878 |
|
| 3879 |
g_clear_object (&message); |
| 3880 |
|
| 3881 |
if (!success && *out_contacts) { |
| 3882 |
g_slist_free_full (*out_contacts, (GDestroyNotify) json_object_unref); |
| 3883 |
*out_contacts = NULL; |
| 3884 |
} |
| 3885 |
|
| 3886 |
return success; |
| 3887 |
} |
| 3888 |
|
| 3889 |
gboolean |
| 3890 |
e_m365_connection_get_contacts_sync (EM365Connection *cnc, |
| 3891 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 3892 |
const gchar *folder_id, |
| 3893 |
GPtrArray *ids, /* const gchar * */ |
| 3894 |
GPtrArray **out_contacts, /* EM365Contact * */ |
| 3895 |
GCancellable *cancellable, |
| 3896 |
GError **error) |
| 3897 |
{ |
| 3898 |
return e_m365_connection_get_contacts_internal_sync (cnc, user_override, E_M365_FOLDER_KIND_CONTACTS, |
| 3899 |
folder_id, ids, out_contacts, cancellable, error); |
| 3900 |
} |
| 3901 |
|
| 3902 |
/* https://docs.microsoft.com/en-us/graph/api/user-post-contacts?view=graph-rest-1.0&tabs=http */ |
| 3903 |
|
| 3904 |
gboolean |
| 3905 |
e_m365_connection_create_contact_sync (EM365Connection *cnc, |
| 3906 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 3907 |
const gchar *folder_id, /* if NULL, then goes to the Drafts folder */ |
| 3908 |
JsonBuilder *contact, /* filled contact object */ |
| 3909 |
EM365Contact **out_created_contact, /* free with json_object_unref() */ |
| 3910 |
GCancellable *cancellable, |
| 3911 |
GError **error) |
| 3912 |
{ |
| 3913 |
SoupMessage *message; |
| 3914 |
gboolean success; |
| 3915 |
gchar *uri; |
| 3916 |
|
| 3917 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 3918 |
g_return_val_if_fail (contact != NULL, FALSE); |
| 3919 |
g_return_val_if_fail (out_created_contact != NULL, FALSE); |
| 3920 |
|
| 3921 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 3922 |
folder_id ? "contactFolders" : "contacts", |
| 3923 |
folder_id, |
| 3924 |
folder_id ? "contacts" : NULL, |
| 3925 |
NULL); |
| 3926 |
|
| 3927 |
message = m365_connection_new_soup_message (SOUP_METHOD_POST, uri, CSM_DEFAULT, error); |
| 3928 |
|
| 3929 |
if (!message) { |
| 3930 |
g_free (uri); |
| 3931 |
|
| 3932 |
return FALSE; |
| 3933 |
} |
| 3934 |
|
| 3935 |
g_free (uri); |
| 3936 |
|
| 3937 |
e_m365_connection_set_json_body (message, contact); |
| 3938 |
|
| 3939 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, out_created_contact, cancellable, error); |
| 3940 |
|
| 3941 |
g_clear_object (&message); |
| 3942 |
|
| 3943 |
return success; |
| 3944 |
} |
| 3945 |
|
| 3946 |
/* https://docs.microsoft.com/en-us/graph/api/contact-update?view=graph-rest-1.0&tabs=http */ |
| 3947 |
|
| 3948 |
gboolean |
| 3949 |
e_m365_connection_update_contact_sync (EM365Connection *cnc, |
| 3950 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 3951 |
const gchar *folder_id, |
| 3952 |
const gchar *contact_id, |
| 3953 |
JsonBuilder *contact, /* values to update, as a contact object */ |
| 3954 |
GCancellable *cancellable, |
| 3955 |
GError **error) |
| 3956 |
{ |
| 3957 |
SoupMessage *message; |
| 3958 |
gchar *uri; |
| 3959 |
gboolean success; |
| 3960 |
|
| 3961 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 3962 |
g_return_val_if_fail (contact_id != NULL, FALSE); |
| 3963 |
g_return_val_if_fail (contact != NULL, FALSE); |
| 3964 |
|
| 3965 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 3966 |
folder_id ? "contactFolders" : "contacts", |
| 3967 |
folder_id, |
| 3968 |
folder_id ? "contacts" : contact_id, |
| 3969 |
"", folder_id ? contact_id : NULL, |
| 3970 |
NULL); |
| 3971 |
|
| 3972 |
/* The server returns the contact object back, but it can be ignored here */ |
| 3973 |
message = m365_connection_new_soup_message ("PATCH", uri, CSM_DISABLE_RESPONSE, error); |
| 3974 |
|
| 3975 |
if (!message) { |
| 3976 |
g_free (uri); |
| 3977 |
|
| 3978 |
return FALSE; |
| 3979 |
} |
| 3980 |
|
| 3981 |
g_free (uri); |
| 3982 |
|
| 3983 |
e_m365_connection_set_json_body (message, contact); |
| 3984 |
|
| 3985 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_no_response_cb, NULL, cancellable, error); |
| 3986 |
|
| 3987 |
g_clear_object (&message); |
| 3988 |
|
| 3989 |
return success; |
| 3990 |
} |
| 3991 |
|
| 3992 |
/* https://docs.microsoft.com/en-us/graph/api/contact-delete?view=graph-rest-1.0&tabs=http */ |
| 3993 |
|
| 3994 |
gboolean |
| 3995 |
e_m365_connection_delete_contact_sync (EM365Connection *cnc, |
| 3996 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 3997 |
const gchar *folder_id, |
| 3998 |
const gchar *contact_id, |
| 3999 |
GCancellable *cancellable, |
| 4000 |
GError **error) |
| 4001 |
{ |
| 4002 |
SoupMessage *message; |
| 4003 |
gchar *uri; |
| 4004 |
gboolean success; |
| 4005 |
|
| 4006 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 4007 |
g_return_val_if_fail (contact_id != NULL, FALSE); |
| 4008 |
|
| 4009 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 4010 |
folder_id ? "contactFolders" : "contacts", |
| 4011 |
folder_id, |
| 4012 |
folder_id ? "contacts" : contact_id, |
| 4013 |
"", folder_id ? contact_id : NULL, |
| 4014 |
NULL); |
| 4015 |
|
| 4016 |
message = m365_connection_new_soup_message (SOUP_METHOD_DELETE, uri, CSM_DEFAULT, error); |
| 4017 |
|
| 4018 |
if (!message) { |
| 4019 |
g_free (uri); |
| 4020 |
|
| 4021 |
return FALSE; |
| 4022 |
} |
| 4023 |
|
| 4024 |
g_free (uri); |
| 4025 |
|
| 4026 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_no_response_cb, NULL, cancellable, error); |
| 4027 |
|
| 4028 |
g_clear_object (&message); |
| 4029 |
|
| 4030 |
return success; |
| 4031 |
} |
| 4032 |
|
| 4033 |
/* https://learn.microsoft.com/en-us/graph/api/orgcontact-list?view=graph-rest-1.0&tabs=http */ |
| 4034 |
|
| 4035 |
gboolean |
| 4036 |
e_m365_connection_get_org_contacts_accessible_sync (EM365Connection *cnc, |
| 4037 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 4038 |
GCancellable *cancellable, |
| 4039 |
GError **error) |
| 4040 |
{ |
| 4041 |
EM365ResponseData rd; |
| 4042 |
GSList *contacts = NULL; |
| 4043 |
SoupMessage *message; |
| 4044 |
gchar *uri; |
| 4045 |
gboolean success; |
| 4046 |
|
| 4047 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 4048 |
|
| 4049 |
uri = e_m365_connection_construct_uri (cnc, FALSE, user_override, E_M365_API_V1_0, |
| 4050 |
"contacts", |
| 4051 |
NULL, NULL, NULL, |
| 4052 |
"$top", "1", |
| 4053 |
"$select", "id", |
| 4054 |
NULL); |
| 4055 |
|
| 4056 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 4057 |
|
| 4058 |
if (!message) { |
| 4059 |
g_free (uri); |
| 4060 |
|
| 4061 |
return FALSE; |
| 4062 |
} |
| 4063 |
|
| 4064 |
g_free (uri); |
| 4065 |
|
| 4066 |
memset (&rd, 0, sizeof (EM365ResponseData)); |
| 4067 |
|
| 4068 |
rd.read_only_once = TRUE; |
| 4069 |
rd.out_items = &contacts; |
| 4070 |
|
| 4071 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_valued_response_cb, NULL, &rd, cancellable, error); |
| 4072 |
|
| 4073 |
/* ignore returned contact, the call is used only to check whether the end point can be accessed */ |
| 4074 |
g_slist_free_full (contacts, (GDestroyNotify) json_object_unref); |
| 4075 |
g_clear_object (&message); |
| 4076 |
|
| 4077 |
return success; |
| 4078 |
} |
| 4079 |
|
| 4080 |
/* https://learn.microsoft.com/en-us/graph/api/orgcontact-get?view=graph-rest-1.0&tabs=http */ |
| 4081 |
|
| 4082 |
gboolean |
| 4083 |
e_m365_connection_get_org_contact_sync (EM365Connection *cnc, |
| 4084 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 4085 |
const gchar *contact_id, |
| 4086 |
EM365Contact **out_contact, |
| 4087 |
GCancellable *cancellable, |
| 4088 |
GError **error) |
| 4089 |
{ |
| 4090 |
SoupMessage *message; |
| 4091 |
gboolean success; |
| 4092 |
|
| 4093 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 4094 |
g_return_val_if_fail (contact_id != NULL, FALSE); |
| 4095 |
g_return_val_if_fail (out_contact != NULL, FALSE); |
| 4096 |
|
| 4097 |
message = e_m365_connection_prepare_get_org_contact (cnc, user_override, contact_id, error); |
| 4098 |
|
| 4099 |
if (!message) |
| 4100 |
return FALSE; |
| 4101 |
|
| 4102 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, out_contact, cancellable, error); |
| 4103 |
|
| 4104 |
g_clear_object (&message); |
| 4105 |
|
| 4106 |
return success; |
| 4107 |
} |
| 4108 |
|
| 4109 |
gboolean |
| 4110 |
e_m365_connection_get_org_contacts_sync (EM365Connection *cnc, |
| 4111 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 4112 |
GPtrArray *ids, /* const gchar * */ |
| 4113 |
GPtrArray **out_contacts, /* EM365Contact * */ |
| 4114 |
GCancellable *cancellable, |
| 4115 |
GError **error) |
| 4116 |
{ |
| 4117 |
return e_m365_connection_get_contacts_internal_sync (cnc, user_override, E_M365_FOLDER_KIND_ORG_CONTACTS, |
| 4118 |
NULL, ids, out_contacts, cancellable, error); |
| 4119 |
} |
| 4120 |
|
| 4121 |
/* https://learn.microsoft.com/en-us/graph/api/user-list?view=graph-rest-1.0&tabs=http */ |
| 4122 |
|
| 4123 |
gboolean |
| 4124 |
e_m365_connection_get_users_accessible_sync (EM365Connection *cnc, |
| 4125 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 4126 |
GCancellable *cancellable, |
| 4127 |
GError **error) |
| 4128 |
{ |
| 4129 |
EM365ResponseData rd; |
| 4130 |
GSList *contacts = NULL; |
| 4131 |
SoupMessage *message; |
| 4132 |
gchar *uri; |
| 4133 |
gboolean success; |
| 4134 |
|
| 4135 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 4136 |
|
| 4137 |
uri = e_m365_connection_construct_uri (cnc, FALSE, user_override, E_M365_API_V1_0, |
| 4138 |
"users", |
| 4139 |
NULL, NULL, NULL, |
| 4140 |
"$top", "1", |
| 4141 |
"$select", "id", |
| 4142 |
NULL); |
| 4143 |
|
| 4144 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 4145 |
|
| 4146 |
if (!message) { |
| 4147 |
g_free (uri); |
| 4148 |
|
| 4149 |
return FALSE; |
| 4150 |
} |
| 4151 |
|
| 4152 |
g_free (uri); |
| 4153 |
|
| 4154 |
memset (&rd, 0, sizeof (EM365ResponseData)); |
| 4155 |
|
| 4156 |
rd.read_only_once = TRUE; |
| 4157 |
rd.out_items = &contacts; |
| 4158 |
|
| 4159 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_valued_response_cb, NULL, &rd, cancellable, error); |
| 4160 |
|
| 4161 |
/* ignore returned contact, the call is used only to check whether the end point can be accessed */ |
| 4162 |
g_slist_free_full (contacts, (GDestroyNotify) json_object_unref); |
| 4163 |
g_clear_object (&message); |
| 4164 |
|
| 4165 |
return success; |
| 4166 |
} |
| 4167 |
|
| 4168 |
/* https://learn.microsoft.com/en-us/graph/api/user-get?view=graph-rest-1.0&tabs=http */ |
| 4169 |
|
| 4170 |
gboolean |
| 4171 |
e_m365_connection_get_user_sync (EM365Connection *cnc, |
| 4172 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 4173 |
const gchar *user_id, |
| 4174 |
EM365Contact **out_contact, |
| 4175 |
GCancellable *cancellable, |
| 4176 |
GError **error) |
| 4177 |
{ |
| 4178 |
SoupMessage *message; |
| 4179 |
gboolean success; |
| 4180 |
|
| 4181 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 4182 |
g_return_val_if_fail (user_id != NULL, FALSE); |
| 4183 |
g_return_val_if_fail (out_contact != NULL, FALSE); |
| 4184 |
|
| 4185 |
message = e_m365_connection_prepare_get_user (cnc, user_override, user_id, error); |
| 4186 |
|
| 4187 |
if (!message) |
| 4188 |
return FALSE; |
| 4189 |
|
| 4190 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, out_contact, cancellable, error); |
| 4191 |
|
| 4192 |
g_clear_object (&message); |
| 4193 |
|
| 4194 |
return success; |
| 4195 |
} |
| 4196 |
|
| 4197 |
gboolean |
| 4198 |
e_m365_connection_get_users_sync (EM365Connection *cnc, |
| 4199 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 4200 |
GPtrArray *ids, /* const gchar * */ |
| 4201 |
GPtrArray **out_contacts, /* EM365Contact * */ |
| 4202 |
GCancellable *cancellable, |
| 4203 |
GError **error) |
| 4204 |
{ |
| 4205 |
return e_m365_connection_get_contacts_internal_sync (cnc, user_override, E_M365_FOLDER_KIND_USERS, |
| 4206 |
NULL, ids, out_contacts, cancellable, error); |
| 4207 |
} |
| 4208 |
|
| 4209 |
gboolean |
| 4210 |
e_m365_connection_search_contacts_sync (EM365Connection *cnc, |
| 4211 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 4212 |
EM365FolderKind kind, |
| 4213 |
const gchar *folder_id, |
| 4214 |
const gchar *search_text, |
| 4215 |
GSList **out_contacts, /* transfer full, EM365Contact * */ |
| 4216 |
GCancellable *cancellable, |
| 4217 |
GError **error) |
| 4218 |
{ |
| 4219 |
return e_m365_connection_search_contacts_internal_sync (cnc, user_override, kind, |
| 4220 |
folder_id, search_text, out_contacts, cancellable, error); |
| 4221 |
} |
| 4222 |
|
| 4223 |
/* https://learn.microsoft.com/en-us/graph/api/user-list-people?view=graph-rest-1.0&tabs=http */ |
| 4224 |
|
| 4225 |
static gboolean |
| 4226 |
e_m365_connection_get_people_internal_sync (EM365Connection *cnc, |
| 4227 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 4228 |
gboolean id_only, |
| 4229 |
guint max_entries, |
| 4230 |
GPtrArray **out_contacts, |
| 4231 |
GCancellable *cancellable, |
| 4232 |
GError **error) |
| 4233 |
{ |
| 4234 |
EM365ResponseData rd; |
| 4235 |
SoupMessage *message; |
| 4236 |
gchar *uri, *max_entries_str = NULL; |
| 4237 |
const gchar *param_name1 = NULL, *param_value1 = NULL; |
| 4238 |
const gchar *param_name2 = NULL, *param_value2 = NULL; |
| 4239 |
gboolean success; |
| 4240 |
|
| 4241 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 4242 |
g_return_val_if_fail (out_contacts != NULL, FALSE); |
| 4243 |
|
| 4244 |
if (max_entries > 0) { |
| 4245 |
max_entries_str = g_strdup_printf ("%u", max_entries); |
| 4246 |
|
| 4247 |
param_name1 = "$top"; |
| 4248 |
param_value1 = max_entries_str; |
| 4249 |
} |
| 4250 |
|
| 4251 |
if (id_only) { |
| 4252 |
param_name2 = "$select"; |
| 4253 |
param_value2 = "id"; |
| 4254 |
} |
| 4255 |
|
| 4256 |
if (!param_name1) { |
| 4257 |
param_name1 = param_name2; |
| 4258 |
param_value1 = param_value2; |
| 4259 |
param_name2 = NULL; |
| 4260 |
param_value2 = NULL; |
| 4261 |
} |
| 4262 |
|
| 4263 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 4264 |
"people", NULL, NULL, |
| 4265 |
param_name1, param_value1, |
| 4266 |
param_name2, param_value2, |
| 4267 |
NULL); |
| 4268 |
|
| 4269 |
g_free (max_entries_str); |
| 4270 |
|
| 4271 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 4272 |
|
| 4273 |
if (!message) { |
| 4274 |
g_free (uri); |
| 4275 |
|
| 4276 |
return FALSE; |
| 4277 |
} |
| 4278 |
|
| 4279 |
g_free (uri); |
| 4280 |
|
| 4281 |
memset (&rd, 0, sizeof (EM365ResponseData)); |
| 4282 |
|
| 4283 |
rd.read_only_once = id_only; |
| 4284 |
rd.out_array_items = g_ptr_array_new_with_free_func ((GDestroyNotify) json_object_unref); |
| 4285 |
|
| 4286 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_valued_response_cb, NULL, &rd, cancellable, error); |
| 4287 |
|
| 4288 |
g_clear_object (&message); |
| 4289 |
|
| 4290 |
if (success) |
| 4291 |
*out_contacts = rd.out_array_items; |
| 4292 |
else |
| 4293 |
g_ptr_array_unref (rd.out_array_items); |
| 4294 |
|
| 4295 |
return success; |
| 4296 |
} |
| 4297 |
|
| 4298 |
|
| 4299 |
gboolean |
| 4300 |
e_m365_connection_get_people_accessible_sync (EM365Connection *cnc, |
| 4301 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 4302 |
GCancellable *cancellable, |
| 4303 |
GError **error) |
| 4304 |
{ |
| 4305 |
GPtrArray *contacts = NULL; |
| 4306 |
|
| 4307 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 4308 |
|
| 4309 |
if (!e_m365_connection_get_people_internal_sync (cnc, user_override, TRUE, 1, &contacts, cancellable, error)) |
| 4310 |
return FALSE; |
| 4311 |
|
| 4312 |
g_clear_pointer (&contacts, g_ptr_array_unref); |
| 4313 |
|
| 4314 |
return TRUE; |
| 4315 |
} |
| 4316 |
|
| 4317 |
gboolean |
| 4318 |
e_m365_connection_get_people_sync (EM365Connection *cnc, |
| 4319 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 4320 |
guint max_entries, |
| 4321 |
GPtrArray **out_contacts, /* EM365Contact * */ |
| 4322 |
GCancellable *cancellable, |
| 4323 |
GError **error) |
| 4324 |
{ |
| 4325 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 4326 |
g_return_val_if_fail (out_contacts != NULL, FALSE); |
| 4327 |
|
| 4328 |
return e_m365_connection_get_people_internal_sync (cnc, user_override, FALSE, max_entries, out_contacts, cancellable, error); |
| 4329 |
} |
| 4330 |
|
| 4331 |
/* https://docs.microsoft.com/en-us/graph/api/user-list-calendargroups?view=graph-rest-1.0&tabs=http */ |
| 4332 |
|
| 4333 |
gboolean |
| 4334 |
e_m365_connection_list_calendar_groups_sync (EM365Connection *cnc, |
| 4335 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 4336 |
GSList **out_groups, /* EM365CalendarGroup * - the returned calendarGroup objects */ |
| 4337 |
GCancellable *cancellable, |
| 4338 |
GError **error) |
| 4339 |
{ |
| 4340 |
EM365ResponseData rd; |
| 4341 |
SoupMessage *message; |
| 4342 |
gchar *uri; |
| 4343 |
gboolean success; |
| 4344 |
|
| 4345 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 4346 |
g_return_val_if_fail (out_groups != NULL, FALSE); |
| 4347 |
|
| 4348 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 4349 |
"calendarGroups", NULL, NULL, NULL); |
| 4350 |
|
| 4351 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 4352 |
|
| 4353 |
if (!message) { |
| 4354 |
g_free (uri); |
| 4355 |
|
| 4356 |
return FALSE; |
| 4357 |
} |
| 4358 |
|
| 4359 |
g_free (uri); |
| 4360 |
|
| 4361 |
memset (&rd, 0, sizeof (EM365ResponseData)); |
| 4362 |
|
| 4363 |
rd.out_items = out_groups; |
| 4364 |
|
| 4365 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_valued_response_cb, NULL, &rd, cancellable, error); |
| 4366 |
|
| 4367 |
g_clear_object (&message); |
| 4368 |
|
| 4369 |
return success; |
| 4370 |
} |
| 4371 |
|
| 4372 |
/* https://docs.microsoft.com/en-us/graph/api/user-post-calendargroups?view=graph-rest-1.0&tabs=http */ |
| 4373 |
|
| 4374 |
gboolean |
| 4375 |
e_m365_connection_create_calendar_group_sync (EM365Connection *cnc, |
| 4376 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 4377 |
const gchar *name, |
| 4378 |
EM365CalendarGroup **out_created_group, |
| 4379 |
GCancellable *cancellable, |
| 4380 |
GError **error) |
| 4381 |
{ |
| 4382 |
SoupMessage *message; |
| 4383 |
JsonBuilder *builder; |
| 4384 |
gboolean success; |
| 4385 |
gchar *uri; |
| 4386 |
|
| 4387 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 4388 |
g_return_val_if_fail (name != NULL, FALSE); |
| 4389 |
g_return_val_if_fail (out_created_group != NULL, FALSE); |
| 4390 |
|
| 4391 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 4392 |
"calendarGroups", NULL, NULL, NULL); |
| 4393 |
|
| 4394 |
message = m365_connection_new_soup_message (SOUP_METHOD_POST, uri, CSM_DEFAULT, error); |
| 4395 |
|
| 4396 |
if (!message) { |
| 4397 |
g_free (uri); |
| 4398 |
|
| 4399 |
return FALSE; |
| 4400 |
} |
| 4401 |
|
| 4402 |
g_free (uri); |
| 4403 |
|
| 4404 |
builder = json_builder_new_immutable (); |
| 4405 |
|
| 4406 |
e_m365_json_begin_object_member (builder, NULL); |
| 4407 |
e_m365_json_add_string_member (builder, "name", name); |
| 4408 |
e_m365_json_end_object_member (builder); |
| 4409 |
|
| 4410 |
e_m365_connection_set_json_body (message, builder); |
| 4411 |
|
| 4412 |
g_object_unref (builder); |
| 4413 |
|
| 4414 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, out_created_group, cancellable, error); |
| 4415 |
|
| 4416 |
g_clear_object (&message); |
| 4417 |
|
| 4418 |
return success; |
| 4419 |
} |
| 4420 |
|
| 4421 |
/* https://docs.microsoft.com/en-us/graph/api/calendargroup-get?view=graph-rest-1.0&tabs=http */ |
| 4422 |
|
| 4423 |
gboolean |
| 4424 |
e_m365_connection_get_calendar_group_sync (EM365Connection *cnc, |
| 4425 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 4426 |
const gchar *group_id, |
| 4427 |
EM365CalendarGroup **out_group, |
| 4428 |
GCancellable *cancellable, |
| 4429 |
GError **error) |
| 4430 |
{ |
| 4431 |
SoupMessage *message; |
| 4432 |
gboolean success; |
| 4433 |
gchar *uri; |
| 4434 |
|
| 4435 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 4436 |
g_return_val_if_fail (group_id != NULL, FALSE); |
| 4437 |
g_return_val_if_fail (out_group != NULL, FALSE); |
| 4438 |
|
| 4439 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 4440 |
"calendarGroups", |
| 4441 |
group_id, |
| 4442 |
NULL, |
| 4443 |
NULL); |
| 4444 |
|
| 4445 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 4446 |
|
| 4447 |
if (!message) { |
| 4448 |
g_free (uri); |
| 4449 |
|
| 4450 |
return FALSE; |
| 4451 |
} |
| 4452 |
|
| 4453 |
g_free (uri); |
| 4454 |
|
| 4455 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, out_group, cancellable, error); |
| 4456 |
|
| 4457 |
g_clear_object (&message); |
| 4458 |
|
| 4459 |
return success; |
| 4460 |
} |
| 4461 |
|
| 4462 |
/* https://docs.microsoft.com/en-us/graph/api/calendargroup-update?view=graph-rest-1.0&tabs=http */ |
| 4463 |
|
| 4464 |
gboolean |
| 4465 |
e_m365_connection_update_calendar_group_sync (EM365Connection *cnc, |
| 4466 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 4467 |
const gchar *group_id, |
| 4468 |
const gchar *name, |
| 4469 |
GCancellable *cancellable, |
| 4470 |
GError **error) |
| 4471 |
{ |
| 4472 |
SoupMessage *message; |
| 4473 |
JsonBuilder *builder; |
| 4474 |
gboolean success; |
| 4475 |
gchar *uri; |
| 4476 |
|
| 4477 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 4478 |
g_return_val_if_fail (group_id != NULL, FALSE); |
| 4479 |
g_return_val_if_fail (name != NULL, FALSE); |
| 4480 |
|
| 4481 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 4482 |
"calendarGroups", |
| 4483 |
group_id, |
| 4484 |
NULL, |
| 4485 |
NULL); |
| 4486 |
|
| 4487 |
message = m365_connection_new_soup_message ("PATCH", uri, CSM_DISABLE_RESPONSE, error); |
| 4488 |
|
| 4489 |
if (!message) { |
| 4490 |
g_free (uri); |
| 4491 |
|
| 4492 |
return FALSE; |
| 4493 |
} |
| 4494 |
|
| 4495 |
g_free (uri); |
| 4496 |
|
| 4497 |
builder = json_builder_new_immutable (); |
| 4498 |
|
| 4499 |
e_m365_json_begin_object_member (builder, NULL); |
| 4500 |
e_m365_json_add_string_member (builder, "name", name); |
| 4501 |
e_m365_json_end_object_member (builder); |
| 4502 |
|
| 4503 |
e_m365_connection_set_json_body (message, builder); |
| 4504 |
|
| 4505 |
g_object_unref (builder); |
| 4506 |
|
| 4507 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_no_response_cb, NULL, cancellable, error); |
| 4508 |
|
| 4509 |
g_clear_object (&message); |
| 4510 |
|
| 4511 |
return success; |
| 4512 |
} |
| 4513 |
|
| 4514 |
/* https://docs.microsoft.com/en-us/graph/api/calendargroup-delete?view=graph-rest-1.0&tabs=http */ |
| 4515 |
|
| 4516 |
gboolean |
| 4517 |
e_m365_connection_delete_calendar_group_sync (EM365Connection *cnc, |
| 4518 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 4519 |
const gchar *group_id, |
| 4520 |
GCancellable *cancellable, |
| 4521 |
GError **error) |
| 4522 |
{ |
| 4523 |
SoupMessage *message; |
| 4524 |
gboolean success; |
| 4525 |
gchar *uri; |
| 4526 |
|
| 4527 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 4528 |
g_return_val_if_fail (group_id != NULL, FALSE); |
| 4529 |
|
| 4530 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 4531 |
"calendarGroups", group_id, NULL, NULL); |
| 4532 |
|
| 4533 |
message = m365_connection_new_soup_message (SOUP_METHOD_DELETE, uri, CSM_DEFAULT, error); |
| 4534 |
|
| 4535 |
if (!message) { |
| 4536 |
g_free (uri); |
| 4537 |
|
| 4538 |
return FALSE; |
| 4539 |
} |
| 4540 |
|
| 4541 |
g_free (uri); |
| 4542 |
|
| 4543 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_no_response_cb, NULL, cancellable, error); |
| 4544 |
|
| 4545 |
g_clear_object (&message); |
| 4546 |
|
| 4547 |
return success; |
| 4548 |
} |
| 4549 |
|
| 4550 |
/* https://docs.microsoft.com/en-us/graph/api/resources/calendar?view=graph-rest-1.0 */ |
| 4551 |
|
| 4552 |
gboolean |
| 4553 |
e_m365_connection_list_calendars_sync (EM365Connection *cnc, |
| 4554 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 4555 |
const gchar *group_id, /* nullable, calendar group id for group calendars */ |
| 4556 |
const gchar *select, /* properties to select, nullable */ |
| 4557 |
GSList **out_calendars, /* EM365Calendar * - the returned calendar objects */ |
| 4558 |
GCancellable *cancellable, |
| 4559 |
GError **error) |
| 4560 |
{ |
| 4561 |
EM365ResponseData rd; |
| 4562 |
SoupMessage *message; |
| 4563 |
gchar *uri; |
| 4564 |
gboolean success; |
| 4565 |
|
| 4566 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 4567 |
g_return_val_if_fail (out_calendars != NULL, FALSE); |
| 4568 |
|
| 4569 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 4570 |
group_id ? "calendarGroups" : "calendars", |
| 4571 |
group_id, |
| 4572 |
group_id ? "calendars" : NULL, |
| 4573 |
"$select", select, |
| 4574 |
NULL); |
| 4575 |
|
| 4576 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 4577 |
|
| 4578 |
if (!message) { |
| 4579 |
g_free (uri); |
| 4580 |
|
| 4581 |
return FALSE; |
| 4582 |
} |
| 4583 |
|
| 4584 |
g_free (uri); |
| 4585 |
|
| 4586 |
memset (&rd, 0, sizeof (EM365ResponseData)); |
| 4587 |
|
| 4588 |
rd.out_items = out_calendars; |
| 4589 |
|
| 4590 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_valued_response_cb, NULL, &rd, cancellable, error); |
| 4591 |
|
| 4592 |
g_clear_object (&message); |
| 4593 |
|
| 4594 |
return success; |
| 4595 |
} |
| 4596 |
|
| 4597 |
/* https://docs.microsoft.com/en-us/graph/api/calendargroup-post-calendars?view=graph-rest-1.0&tabs=http */ |
| 4598 |
|
| 4599 |
gboolean |
| 4600 |
e_m365_connection_create_calendar_sync (EM365Connection *cnc, |
| 4601 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 4602 |
const gchar *group_id, /* nullable, then the default group is used */ |
| 4603 |
JsonBuilder *calendar, |
| 4604 |
EM365Calendar **out_created_calendar, |
| 4605 |
GCancellable *cancellable, |
| 4606 |
GError **error) |
| 4607 |
{ |
| 4608 |
SoupMessage *message; |
| 4609 |
gboolean success; |
| 4610 |
gchar *uri; |
| 4611 |
|
| 4612 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 4613 |
g_return_val_if_fail (calendar != NULL, FALSE); |
| 4614 |
g_return_val_if_fail (out_created_calendar != NULL, FALSE); |
| 4615 |
|
| 4616 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 4617 |
group_id ? "calendarGroups" : "calendars", |
| 4618 |
group_id, |
| 4619 |
"calendars", |
| 4620 |
NULL); |
| 4621 |
|
| 4622 |
message = m365_connection_new_soup_message (SOUP_METHOD_POST, uri, CSM_DEFAULT, error); |
| 4623 |
|
| 4624 |
if (!message) { |
| 4625 |
g_free (uri); |
| 4626 |
|
| 4627 |
return FALSE; |
| 4628 |
} |
| 4629 |
|
| 4630 |
g_free (uri); |
| 4631 |
|
| 4632 |
e_m365_connection_set_json_body (message, calendar); |
| 4633 |
|
| 4634 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, out_created_calendar, cancellable, error); |
| 4635 |
|
| 4636 |
g_clear_object (&message); |
| 4637 |
|
| 4638 |
return success; |
| 4639 |
} |
| 4640 |
|
| 4641 |
/* https://docs.microsoft.com/en-us/graph/api/calendar-get?view=graph-rest-1.0&tabs=http */ |
| 4642 |
|
| 4643 |
gboolean |
| 4644 |
e_m365_connection_get_calendar_folder_sync (EM365Connection *cnc, |
| 4645 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 4646 |
const gchar *group_id, /* nullable - then the default group is used */ |
| 4647 |
const gchar *calendar_id, /* nullable - then the default calendar is used */ |
| 4648 |
const gchar *select, /* nullable - properties to select */ |
| 4649 |
EM365Calendar **out_calendar, |
| 4650 |
GCancellable *cancellable, |
| 4651 |
GError **error) |
| 4652 |
{ |
| 4653 |
SoupMessage *message; |
| 4654 |
gchar *uri; |
| 4655 |
gboolean success; |
| 4656 |
|
| 4657 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 4658 |
g_return_val_if_fail (out_calendar != NULL, FALSE); |
| 4659 |
|
| 4660 |
if (group_id && calendar_id) { |
| 4661 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 4662 |
"calendarGroups", |
| 4663 |
group_id, |
| 4664 |
"calendars", |
| 4665 |
"", calendar_id, |
| 4666 |
"$select", select, |
| 4667 |
NULL); |
| 4668 |
} else if (group_id) { |
| 4669 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, "groups", |
| 4670 |
group_id, |
| 4671 |
"calendar", |
| 4672 |
NULL, |
| 4673 |
"$select", select, |
| 4674 |
NULL); |
| 4675 |
} else if (calendar_id) { |
| 4676 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 4677 |
"calendars", |
| 4678 |
calendar_id, |
| 4679 |
NULL, |
| 4680 |
"$select", select, |
| 4681 |
NULL); |
| 4682 |
} else { |
| 4683 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 4684 |
"calendar", |
| 4685 |
NULL, |
| 4686 |
NULL, |
| 4687 |
"$select", select, |
| 4688 |
NULL); |
| 4689 |
} |
| 4690 |
|
| 4691 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 4692 |
|
| 4693 |
if (!message) { |
| 4694 |
g_free (uri); |
| 4695 |
|
| 4696 |
return FALSE; |
| 4697 |
} |
| 4698 |
|
| 4699 |
g_free (uri); |
| 4700 |
|
| 4701 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, out_calendar, cancellable, error); |
| 4702 |
|
| 4703 |
g_clear_object (&message); |
| 4704 |
|
| 4705 |
return success; |
| 4706 |
} |
| 4707 |
|
| 4708 |
/* https://docs.microsoft.com/en-us/graph/api/calendar-update?view=graph-rest-1.0&tabs=http */ |
| 4709 |
|
| 4710 |
gboolean |
| 4711 |
e_m365_connection_update_calendar_sync (EM365Connection *cnc, |
| 4712 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 4713 |
const gchar *group_id, /* nullable - then the default group is used */ |
| 4714 |
const gchar *calendar_id, |
| 4715 |
const gchar *name, /* nullable - to keep the existing name */ |
| 4716 |
EM365CalendarColorType color, |
| 4717 |
GCancellable *cancellable, |
| 4718 |
GError **error) |
| 4719 |
{ |
| 4720 |
SoupMessage *message; |
| 4721 |
JsonBuilder *builder; |
| 4722 |
gboolean success; |
| 4723 |
gchar *uri; |
| 4724 |
|
| 4725 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 4726 |
g_return_val_if_fail (calendar_id != NULL, FALSE); |
| 4727 |
|
| 4728 |
/* Nothing to change */ |
| 4729 |
if (!name && (color == E_M365_CALENDAR_COLOR_NOT_SET || color == E_M365_CALENDAR_COLOR_UNKNOWN)) |
| 4730 |
return TRUE; |
| 4731 |
|
| 4732 |
if (group_id) { |
| 4733 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 4734 |
"calendarGroups", |
| 4735 |
group_id, |
| 4736 |
"calendars", |
| 4737 |
"", calendar_id, |
| 4738 |
NULL); |
| 4739 |
} else { |
| 4740 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 4741 |
"calendars", |
| 4742 |
calendar_id, |
| 4743 |
NULL, |
| 4744 |
NULL); |
| 4745 |
} |
| 4746 |
|
| 4747 |
message = m365_connection_new_soup_message ("PATCH", uri, CSM_DISABLE_RESPONSE, error); |
| 4748 |
|
| 4749 |
if (!message) { |
| 4750 |
g_free (uri); |
| 4751 |
|
| 4752 |
return FALSE; |
| 4753 |
} |
| 4754 |
|
| 4755 |
g_free (uri); |
| 4756 |
|
| 4757 |
builder = json_builder_new_immutable (); |
| 4758 |
|
| 4759 |
e_m365_json_begin_object_member (builder, NULL); |
| 4760 |
e_m365_calendar_add_name (builder, name); |
| 4761 |
e_m365_calendar_add_color (builder, color); |
| 4762 |
e_m365_json_end_object_member (builder); |
| 4763 |
|
| 4764 |
e_m365_connection_set_json_body (message, builder); |
| 4765 |
|
| 4766 |
g_object_unref (builder); |
| 4767 |
|
| 4768 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_no_response_cb, NULL, cancellable, error); |
| 4769 |
|
| 4770 |
g_clear_object (&message); |
| 4771 |
|
| 4772 |
return success; |
| 4773 |
} |
| 4774 |
|
| 4775 |
/* https://docs.microsoft.com/en-us/graph/api/calendar-delete?view=graph-rest-1.0&tabs=http */ |
| 4776 |
|
| 4777 |
gboolean |
| 4778 |
e_m365_connection_delete_calendar_sync (EM365Connection *cnc, |
| 4779 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 4780 |
const gchar *group_id, /* nullable - then the default group is used */ |
| 4781 |
const gchar *calendar_id, |
| 4782 |
GCancellable *cancellable, |
| 4783 |
GError **error) |
| 4784 |
{ |
| 4785 |
SoupMessage *message; |
| 4786 |
gboolean success; |
| 4787 |
gchar *uri; |
| 4788 |
|
| 4789 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 4790 |
g_return_val_if_fail (calendar_id != NULL, FALSE); |
| 4791 |
|
| 4792 |
if (group_id) { |
| 4793 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 4794 |
"calendarGroups", |
| 4795 |
group_id, |
| 4796 |
"calendars", |
| 4797 |
"", calendar_id, |
| 4798 |
NULL); |
| 4799 |
} else { |
| 4800 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 4801 |
"calendars", |
| 4802 |
calendar_id, |
| 4803 |
NULL, |
| 4804 |
NULL); |
| 4805 |
} |
| 4806 |
|
| 4807 |
message = m365_connection_new_soup_message (SOUP_METHOD_DELETE, uri, CSM_DEFAULT, error); |
| 4808 |
|
| 4809 |
if (!message) { |
| 4810 |
g_free (uri); |
| 4811 |
|
| 4812 |
return FALSE; |
| 4813 |
} |
| 4814 |
|
| 4815 |
g_free (uri); |
| 4816 |
|
| 4817 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_no_response_cb, NULL, cancellable, error); |
| 4818 |
|
| 4819 |
g_clear_object (&message); |
| 4820 |
|
| 4821 |
return success; |
| 4822 |
} |
| 4823 |
|
| 4824 |
/* https://learn.microsoft.com/en-us/graph/api/calendar-list-calendarpermissions?view=graph-rest-1.0&tabs=http */ |
| 4825 |
|
| 4826 |
gboolean |
| 4827 |
e_m365_connection_list_calendar_permissions_sync (EM365Connection *cnc, |
| 4828 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 4829 |
const gchar *group_id, /* nullable - calendar group for group calendars */ |
| 4830 |
const gchar *calendar_id, |
| 4831 |
GSList **out_permissions, /* EM365CalendarPermission * */ |
| 4832 |
GCancellable *cancellable, |
| 4833 |
GError **error) |
| 4834 |
{ |
| 4835 |
EM365ResponseData rd; |
| 4836 |
SoupMessage *message; |
| 4837 |
gchar *uri; |
| 4838 |
gboolean success; |
| 4839 |
|
| 4840 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 4841 |
g_return_val_if_fail (calendar_id != NULL, FALSE); |
| 4842 |
g_return_val_if_fail (out_permissions != NULL, FALSE); |
| 4843 |
|
| 4844 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 4845 |
group_id ? "calendarGroups" : "calendars", |
| 4846 |
group_id, |
| 4847 |
group_id ? "calendars" : NULL, |
| 4848 |
"", calendar_id, |
| 4849 |
"", "calendarPermissions", |
| 4850 |
NULL); |
| 4851 |
|
| 4852 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 4853 |
|
| 4854 |
if (!message) { |
| 4855 |
g_free (uri); |
| 4856 |
|
| 4857 |
return FALSE; |
| 4858 |
} |
| 4859 |
|
| 4860 |
g_free (uri); |
| 4861 |
|
| 4862 |
memset (&rd, 0, sizeof (EM365ResponseData)); |
| 4863 |
|
| 4864 |
rd.out_items = out_permissions; |
| 4865 |
|
| 4866 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_valued_response_cb, NULL, &rd, cancellable, error); |
| 4867 |
|
| 4868 |
g_clear_object (&message); |
| 4869 |
|
| 4870 |
return success; |
| 4871 |
} |
| 4872 |
|
| 4873 |
/* https://learn.microsoft.com/en-us/graph/api/calendar-post-calendarpermissions?view=graph-rest-1.0&tabs=http */ |
| 4874 |
|
| 4875 |
gboolean |
| 4876 |
e_m365_connection_create_calendar_permission_sync (EM365Connection *cnc, |
| 4877 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 4878 |
const gchar *group_id, /* nullable, then the default group is used */ |
| 4879 |
const gchar *calendar_id, |
| 4880 |
JsonBuilder *permission, |
| 4881 |
EM365CalendarPermission **out_created_permission, |
| 4882 |
GCancellable *cancellable, |
| 4883 |
GError **error) |
| 4884 |
{ |
| 4885 |
SoupMessage *message; |
| 4886 |
gboolean success; |
| 4887 |
gchar *uri; |
| 4888 |
|
| 4889 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 4890 |
g_return_val_if_fail (calendar_id != NULL, FALSE); |
| 4891 |
g_return_val_if_fail (permission != NULL, FALSE); |
| 4892 |
g_return_val_if_fail (out_created_permission != NULL, FALSE); |
| 4893 |
|
| 4894 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 4895 |
group_id ? "calendarGroups" : "calendars", |
| 4896 |
group_id, |
| 4897 |
group_id ? "calendars" : NULL, |
| 4898 |
"", calendar_id, |
| 4899 |
"", "calendarPermissions", |
| 4900 |
NULL); |
| 4901 |
|
| 4902 |
message = m365_connection_new_soup_message (SOUP_METHOD_POST, uri, CSM_DEFAULT, error); |
| 4903 |
|
| 4904 |
if (!message) { |
| 4905 |
g_free (uri); |
| 4906 |
|
| 4907 |
return FALSE; |
| 4908 |
} |
| 4909 |
|
| 4910 |
g_free (uri); |
| 4911 |
|
| 4912 |
e_m365_connection_set_json_body (message, permission); |
| 4913 |
|
| 4914 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, out_created_permission, cancellable, error); |
| 4915 |
|
| 4916 |
g_clear_object (&message); |
| 4917 |
|
| 4918 |
return success; |
| 4919 |
} |
| 4920 |
|
| 4921 |
/* https://learn.microsoft.com/en-us/graph/api/calendarpermission-get?view=graph-rest-1.0&tabs=http */ |
| 4922 |
|
| 4923 |
gboolean |
| 4924 |
e_m365_connection_get_calendar_permission_sync (EM365Connection *cnc, |
| 4925 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 4926 |
const gchar *group_id, /* nullable, then the default group is used */ |
| 4927 |
const gchar *calendar_id, |
| 4928 |
const gchar *permission_id, |
| 4929 |
EM365CalendarPermission **out_permission, |
| 4930 |
GCancellable *cancellable, |
| 4931 |
GError **error) |
| 4932 |
{ |
| 4933 |
SoupMessage *message; |
| 4934 |
gchar *uri; |
| 4935 |
gboolean success; |
| 4936 |
|
| 4937 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 4938 |
g_return_val_if_fail (calendar_id != NULL, FALSE); |
| 4939 |
g_return_val_if_fail (permission_id != NULL, FALSE); |
| 4940 |
g_return_val_if_fail (out_permission != NULL, FALSE); |
| 4941 |
|
| 4942 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 4943 |
group_id ? "calendarGroups" : "calendars", |
| 4944 |
group_id, |
| 4945 |
group_id ? "calendars" : NULL, |
| 4946 |
"", calendar_id, |
| 4947 |
"", "calendarPermissions", |
| 4948 |
"", permission_id, |
| 4949 |
NULL); |
| 4950 |
|
| 4951 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 4952 |
|
| 4953 |
if (!message) { |
| 4954 |
g_free (uri); |
| 4955 |
|
| 4956 |
return FALSE; |
| 4957 |
} |
| 4958 |
|
| 4959 |
g_free (uri); |
| 4960 |
|
| 4961 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, out_permission, cancellable, error); |
| 4962 |
|
| 4963 |
g_clear_object (&message); |
| 4964 |
|
| 4965 |
return success; |
| 4966 |
} |
| 4967 |
|
| 4968 |
/* https://learn.microsoft.com/en-us/graph/api/calendarpermission-update?view=graph-rest-1.0&tabs=http */ |
| 4969 |
|
| 4970 |
gboolean |
| 4971 |
e_m365_connection_update_calendar_permission_sync (EM365Connection *cnc, |
| 4972 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 4973 |
const gchar *group_id, /* nullable - then the default group is used */ |
| 4974 |
const gchar *calendar_id, |
| 4975 |
const gchar *permission_id, |
| 4976 |
JsonBuilder *permission, |
| 4977 |
GCancellable *cancellable, |
| 4978 |
GError **error) |
| 4979 |
{ |
| 4980 |
SoupMessage *message; |
| 4981 |
gboolean success; |
| 4982 |
gchar *uri; |
| 4983 |
|
| 4984 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 4985 |
g_return_val_if_fail (calendar_id != NULL, FALSE); |
| 4986 |
g_return_val_if_fail (permission_id != NULL, FALSE); |
| 4987 |
g_return_val_if_fail (permission != NULL, FALSE); |
| 4988 |
|
| 4989 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 4990 |
group_id ? "calendarGroups" : "calendars", |
| 4991 |
group_id, |
| 4992 |
group_id ? "calendars" : NULL, |
| 4993 |
"", calendar_id, |
| 4994 |
"", "calendarPermissions", |
| 4995 |
"", permission_id, |
| 4996 |
NULL); |
| 4997 |
|
| 4998 |
message = m365_connection_new_soup_message ("PATCH", uri, CSM_DISABLE_RESPONSE, error); |
| 4999 |
|
| 5000 |
if (!message) { |
| 5001 |
g_free (uri); |
| 5002 |
|
| 5003 |
return FALSE; |
| 5004 |
} |
| 5005 |
|
| 5006 |
g_free (uri); |
| 5007 |
|
| 5008 |
e_m365_connection_set_json_body (message, permission); |
| 5009 |
|
| 5010 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_no_response_cb, NULL, cancellable, error); |
| 5011 |
|
| 5012 |
g_clear_object (&message); |
| 5013 |
|
| 5014 |
return success; |
| 5015 |
} |
| 5016 |
|
| 5017 |
/* https://learn.microsoft.com/en-us/graph/api/calendarpermission-delete?view=graph-rest-1.0&tabs=http */ |
| 5018 |
|
| 5019 |
gboolean |
| 5020 |
e_m365_connection_delete_calendar_permission_sync (EM365Connection *cnc, |
| 5021 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 5022 |
const gchar *group_id, /* nullable - then the default group is used */ |
| 5023 |
const gchar *calendar_id, |
| 5024 |
const gchar *permission_id, |
| 5025 |
GCancellable *cancellable, |
| 5026 |
GError **error) |
| 5027 |
{ |
| 5028 |
SoupMessage *message; |
| 5029 |
gboolean success; |
| 5030 |
gchar *uri; |
| 5031 |
|
| 5032 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 5033 |
g_return_val_if_fail (calendar_id != NULL, FALSE); |
| 5034 |
g_return_val_if_fail (permission_id != NULL, FALSE); |
| 5035 |
|
| 5036 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 5037 |
group_id ? "calendarGroups" : "calendars", |
| 5038 |
group_id, |
| 5039 |
group_id ? "calendars" : NULL, |
| 5040 |
"", calendar_id, |
| 5041 |
"", "calendarPermissions", |
| 5042 |
"", permission_id, |
| 5043 |
NULL); |
| 5044 |
|
| 5045 |
message = m365_connection_new_soup_message (SOUP_METHOD_DELETE, uri, CSM_DEFAULT, error); |
| 5046 |
|
| 5047 |
if (!message) { |
| 5048 |
g_free (uri); |
| 5049 |
|
| 5050 |
return FALSE; |
| 5051 |
} |
| 5052 |
|
| 5053 |
g_free (uri); |
| 5054 |
|
| 5055 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_no_response_cb, NULL, cancellable, error); |
| 5056 |
|
| 5057 |
g_clear_object (&message); |
| 5058 |
|
| 5059 |
return success; |
| 5060 |
} |
| 5061 |
|
| 5062 |
static void |
| 5063 |
m365_connection_prefer_outlook_timezone (SoupMessage *message, |
| 5064 |
const gchar *prefer_outlook_timezone) |
| 5065 |
{ |
| 5066 |
g_return_if_fail (SOUP_IS_MESSAGE (message)); |
| 5067 |
|
| 5068 |
if (prefer_outlook_timezone && *prefer_outlook_timezone) { |
| 5069 |
gchar *prefer_value; |
| 5070 |
|
| 5071 |
prefer_value = g_strdup_printf ("outlook.timezone=\"%s\"", prefer_outlook_timezone); |
| 5072 |
|
| 5073 |
soup_message_headers_append (soup_message_get_request_headers (message), "Prefer", prefer_value); |
| 5074 |
|
| 5075 |
g_free (prefer_value); |
| 5076 |
} |
| 5077 |
} |
| 5078 |
|
| 5079 |
/* https://docs.microsoft.com/en-us/graph/api/user-list-events?view=graph-rest-1.0&tabs=http */ |
| 5080 |
|
| 5081 |
gboolean |
| 5082 |
e_m365_connection_list_events_sync (EM365Connection *cnc, |
| 5083 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 5084 |
const gchar *group_id, /* nullable, calendar group id for group calendars */ |
| 5085 |
const gchar *calendar_id, |
| 5086 |
const gchar *prefer_outlook_timezone, /* nullable - then UTC, otherwise that zone for the returned times */ |
| 5087 |
const gchar *select, /* nullable - properties to select */ |
| 5088 |
const gchar *filter, /* nullable - filter which events to list */ |
| 5089 |
GSList **out_events, /* EM365Event * - the returned event objects */ |
| 5090 |
GCancellable *cancellable, |
| 5091 |
GError **error) |
| 5092 |
{ |
| 5093 |
EM365ResponseData rd; |
| 5094 |
SoupMessage *message; |
| 5095 |
gchar *uri; |
| 5096 |
gboolean success; |
| 5097 |
|
| 5098 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 5099 |
g_return_val_if_fail (calendar_id != NULL, FALSE); |
| 5100 |
g_return_val_if_fail (out_events != NULL, FALSE); |
| 5101 |
|
| 5102 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 5103 |
group_id ? "calendarGroups" : "calendars", |
| 5104 |
group_id, |
| 5105 |
group_id ? "calendars" : NULL, |
| 5106 |
"", calendar_id, |
| 5107 |
"", "events", |
| 5108 |
"$select", select, |
| 5109 |
"$filter", filter, |
| 5110 |
select ? NULL : "$expand", |
| 5111 |
select ? NULL : "singleValueExtendedProperties($filter=id eq '" E_M365_RECURRENCE_BLOB_NAME "')", |
| 5112 |
NULL); |
| 5113 |
|
| 5114 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 5115 |
|
| 5116 |
if (!message) { |
| 5117 |
g_free (uri); |
| 5118 |
|
| 5119 |
return FALSE; |
| 5120 |
} |
| 5121 |
|
| 5122 |
g_free (uri); |
| 5123 |
|
| 5124 |
m365_connection_prefer_outlook_timezone (message, prefer_outlook_timezone); |
| 5125 |
|
| 5126 |
soup_message_headers_append (soup_message_get_request_headers (message), "Prefer", "outlook.body-content-type=\"text\""); |
| 5127 |
|
| 5128 |
memset (&rd, 0, sizeof (EM365ResponseData)); |
| 5129 |
|
| 5130 |
rd.out_items = out_events; |
| 5131 |
|
| 5132 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_valued_response_cb, NULL, &rd, cancellable, error); |
| 5133 |
|
| 5134 |
g_clear_object (&message); |
| 5135 |
|
| 5136 |
return success; |
| 5137 |
} |
| 5138 |
|
| 5139 |
/* https://docs.microsoft.com/en-us/graph/api/user-post-events?view=graph-rest-1.0&tabs=http |
| 5140 |
https://docs.microsoft.com/en-us/graph/api/group-post-events?view=graph-rest-1.0&tabs=http */ |
| 5141 |
|
| 5142 |
gboolean |
| 5143 |
e_m365_connection_create_event_sync (EM365Connection *cnc, |
| 5144 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 5145 |
const gchar *group_id, /* nullable, then the default group is used */ |
| 5146 |
const gchar *calendar_id, /* nullable, then the default user calendar is used */ |
| 5147 |
JsonBuilder *event, |
| 5148 |
EM365Event **out_created_event, |
| 5149 |
GCancellable *cancellable, |
| 5150 |
GError **error) |
| 5151 |
{ |
| 5152 |
SoupMessage *message; |
| 5153 |
gboolean success; |
| 5154 |
gchar *uri; |
| 5155 |
|
| 5156 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 5157 |
g_return_val_if_fail (event != NULL, FALSE); |
| 5158 |
g_return_val_if_fail (out_created_event != NULL, FALSE); |
| 5159 |
|
| 5160 |
if (calendar_id) { |
| 5161 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 5162 |
group_id ? "calendarGroups" : "calendars", |
| 5163 |
group_id, |
| 5164 |
group_id ? "calendars" : NULL, |
| 5165 |
"", calendar_id, |
| 5166 |
"", "events", |
| 5167 |
NULL); |
| 5168 |
} else { |
| 5169 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, "users", |
| 5170 |
"events", NULL, NULL, NULL); |
| 5171 |
} |
| 5172 |
|
| 5173 |
message = m365_connection_new_soup_message (SOUP_METHOD_POST, uri, CSM_DEFAULT, error); |
| 5174 |
|
| 5175 |
if (!message) { |
| 5176 |
g_free (uri); |
| 5177 |
|
| 5178 |
return FALSE; |
| 5179 |
} |
| 5180 |
|
| 5181 |
g_free (uri); |
| 5182 |
|
| 5183 |
e_m365_connection_set_json_body (message, event); |
| 5184 |
|
| 5185 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, out_created_event, cancellable, error); |
| 5186 |
|
| 5187 |
g_clear_object (&message); |
| 5188 |
|
| 5189 |
return success; |
| 5190 |
} |
| 5191 |
|
| 5192 |
/* https://docs.microsoft.com/en-us/graph/api/event-get?view=graph-rest-1.0&tabs=http */ |
| 5193 |
|
| 5194 |
SoupMessage * |
| 5195 |
e_m365_connection_prepare_get_event (EM365Connection *cnc, |
| 5196 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 5197 |
const gchar *group_id, /* nullable, then the default group is used */ |
| 5198 |
const gchar *calendar_id, |
| 5199 |
const gchar *event_id, |
| 5200 |
const gchar *prefer_outlook_timezone, /* nullable - then UTC, otherwise that zone for the returned times */ |
| 5201 |
const gchar *select, /* nullable - properties to select */ |
| 5202 |
GError **error) |
| 5203 |
{ |
| 5204 |
SoupMessage *message; |
| 5205 |
gchar *uri; |
| 5206 |
|
| 5207 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), NULL); |
| 5208 |
g_return_val_if_fail (calendar_id != NULL, NULL); |
| 5209 |
g_return_val_if_fail (event_id != NULL, NULL); |
| 5210 |
|
| 5211 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 5212 |
group_id ? "calendarGroups" : "calendars", |
| 5213 |
group_id, |
| 5214 |
group_id ? "calendars" : NULL, |
| 5215 |
"", calendar_id, |
| 5216 |
"", "events", |
| 5217 |
"", event_id, |
| 5218 |
"$select", select, |
| 5219 |
select ? NULL : "$expand", |
| 5220 |
select ? NULL : "singleValueExtendedProperties($filter=id eq '" E_M365_RECURRENCE_BLOB_NAME "')", |
| 5221 |
NULL); |
| 5222 |
|
| 5223 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 5224 |
|
| 5225 |
if (!message) { |
| 5226 |
g_free (uri); |
| 5227 |
|
| 5228 |
return NULL; |
| 5229 |
} |
| 5230 |
|
| 5231 |
g_free (uri); |
| 5232 |
|
| 5233 |
m365_connection_prefer_outlook_timezone (message, prefer_outlook_timezone); |
| 5234 |
soup_message_headers_append (soup_message_get_request_headers (message), "Prefer", "outlook.body-content-type=\"text\""); |
| 5235 |
|
| 5236 |
return message; |
| 5237 |
} |
| 5238 |
|
| 5239 |
gboolean |
| 5240 |
e_m365_connection_get_event_sync (EM365Connection *cnc, |
| 5241 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 5242 |
const gchar *group_id, /* nullable, then the default group is used */ |
| 5243 |
const gchar *calendar_id, |
| 5244 |
const gchar *event_id, |
| 5245 |
const gchar *prefer_outlook_timezone, /* nullable - then UTC, otherwise that zone for the returned times */ |
| 5246 |
const gchar *select, /* nullable - properties to select */ |
| 5247 |
EM365Event **out_event, |
| 5248 |
GCancellable *cancellable, |
| 5249 |
GError **error) |
| 5250 |
{ |
| 5251 |
SoupMessage *message; |
| 5252 |
gboolean success; |
| 5253 |
|
| 5254 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 5255 |
g_return_val_if_fail (calendar_id != NULL, FALSE); |
| 5256 |
g_return_val_if_fail (event_id != NULL, FALSE); |
| 5257 |
g_return_val_if_fail (out_event != NULL, FALSE); |
| 5258 |
|
| 5259 |
message = e_m365_connection_prepare_get_event (cnc, user_override, group_id, calendar_id, event_id, prefer_outlook_timezone, select, error); |
| 5260 |
|
| 5261 |
if (!message) |
| 5262 |
return FALSE; |
| 5263 |
|
| 5264 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, out_event, cancellable, error); |
| 5265 |
|
| 5266 |
g_clear_object (&message); |
| 5267 |
|
| 5268 |
return success; |
| 5269 |
} |
| 5270 |
|
| 5271 |
gboolean |
| 5272 |
e_m365_connection_get_events_sync (EM365Connection *cnc, |
| 5273 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 5274 |
const gchar *group_id, /* nullable, then the default group is used */ |
| 5275 |
const gchar *calendar_id, |
| 5276 |
const GSList *event_ids, /* const gchar * */ |
| 5277 |
const gchar *prefer_outlook_timezone, /* nullable - then UTC, otherwise that zone for the returned times */ |
| 5278 |
const gchar *select, /* nullable - properties to select */ |
| 5279 |
GSList **out_events, /* EM365Event *, in the same order as event_ids; can return partial list */ |
| 5280 |
GCancellable *cancellable, |
| 5281 |
GError **error) |
| 5282 |
{ |
| 5283 |
gboolean success = TRUE; |
| 5284 |
|
| 5285 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 5286 |
g_return_val_if_fail (calendar_id != NULL, FALSE); |
| 5287 |
g_return_val_if_fail (event_ids != NULL, FALSE); |
| 5288 |
g_return_val_if_fail (out_events != NULL, FALSE); |
| 5289 |
|
| 5290 |
if (g_slist_next (event_ids)) { |
| 5291 |
GPtrArray *requests; |
| 5292 |
GSList *link; |
| 5293 |
guint total, done = 0; |
| 5294 |
|
| 5295 |
total = g_slist_length ((GSList *) event_ids); |
| 5296 |
requests = g_ptr_array_new_full (MIN (E_M365_BATCH_MAX_REQUESTS, MIN (total, 50)), g_object_unref); |
| 5297 |
|
| 5298 |
for (link = (GSList *) event_ids; link && success; link = g_slist_next (link)) { |
| 5299 |
const gchar *id = link->data; |
| 5300 |
SoupMessage *message; |
| 5301 |
|
| 5302 |
message = e_m365_connection_prepare_get_event (cnc, user_override, group_id, calendar_id, id, prefer_outlook_timezone, select, error); |
| 5303 |
|
| 5304 |
if (!message) { |
| 5305 |
success = FALSE; |
| 5306 |
break; |
| 5307 |
} |
| 5308 |
|
| 5309 |
g_ptr_array_add (requests, message); |
| 5310 |
|
| 5311 |
if (requests->len == E_M365_BATCH_MAX_REQUESTS || !link->next) { |
| 5312 |
if (requests->len == 1) { |
| 5313 |
EM365Event *event = NULL; |
| 5314 |
|
| 5315 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, &event, cancellable, error); |
| 5316 |
|
| 5317 |
if (success) |
| 5318 |
*out_events = g_slist_prepend (*out_events, event); |
| 5319 |
} else { |
| 5320 |
success = e_m365_connection_batch_request_sync (cnc, E_M365_API_V1_0, requests, cancellable, error); |
| 5321 |
|
| 5322 |
if (success) { |
| 5323 |
guint ii; |
| 5324 |
|
| 5325 |
for (ii = 0; ii < requests->len && success; ii++) { |
| 5326 |
JsonNode *node = NULL; |
| 5327 |
|
| 5328 |
message = requests->pdata[ii]; |
| 5329 |
success = e_m365_connection_json_node_from_message (message, NULL, &node, cancellable, error); |
| 5330 |
|
| 5331 |
if (success && node && JSON_NODE_HOLDS_OBJECT (node)) { |
| 5332 |
JsonObject *response; |
| 5333 |
|
| 5334 |
response = json_node_get_object (node); |
| 5335 |
|
| 5336 |
if (response) { |
| 5337 |
*out_events = g_slist_prepend (*out_events, json_object_ref (response)); |
| 5338 |
} else { |
| 5339 |
success = FALSE; |
| 5340 |
} |
| 5341 |
} else { |
| 5342 |
success = FALSE; |
| 5343 |
} |
| 5344 |
|
| 5345 |
if (node) |
| 5346 |
json_node_unref (node); |
| 5347 |
} |
| 5348 |
} |
| 5349 |
} |
| 5350 |
|
| 5351 |
g_ptr_array_remove_range (requests, 0, requests->len); |
| 5352 |
|
| 5353 |
done += requests->len; |
| 5354 |
|
| 5355 |
camel_operation_progress (cancellable, done * 100.0 / total); |
| 5356 |
} |
| 5357 |
} |
| 5358 |
|
| 5359 |
g_ptr_array_free (requests, TRUE); |
| 5360 |
} else { |
| 5361 |
SoupMessage *message; |
| 5362 |
|
| 5363 |
message = e_m365_connection_prepare_get_event (cnc, user_override, group_id, calendar_id, event_ids->data, prefer_outlook_timezone, select, error); |
| 5364 |
|
| 5365 |
if (message) { |
| 5366 |
EM365Event *event = NULL; |
| 5367 |
|
| 5368 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, &event, cancellable, error); |
| 5369 |
|
| 5370 |
if (success) |
| 5371 |
*out_events = g_slist_prepend (*out_events, event); |
| 5372 |
|
| 5373 |
g_clear_object (&message); |
| 5374 |
} else { |
| 5375 |
success = FALSE; |
| 5376 |
} |
| 5377 |
} |
| 5378 |
|
| 5379 |
*out_events = g_slist_reverse (*out_events); |
| 5380 |
|
| 5381 |
return success; |
| 5382 |
} |
| 5383 |
|
| 5384 |
/* https://learn.microsoft.com/en-us/graph/api/event-list-instances?view=graph-rest-1.0&tabs=http */ |
| 5385 |
|
| 5386 |
gboolean |
| 5387 |
e_m365_connection_get_event_instance_id_sync (EM365Connection *cnc, |
| 5388 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 5389 |
const gchar *group_id, /* nullable, then the default group is used */ |
| 5390 |
const gchar *calendar_id, |
| 5391 |
const gchar *event_id, |
| 5392 |
ICalTime *instance_time, |
| 5393 |
gchar **out_instance_id, |
| 5394 |
GCancellable *cancellable, |
| 5395 |
GError **error) |
| 5396 |
{ |
| 5397 |
EM365ResponseData rd; |
| 5398 |
GSList *items = NULL; |
| 5399 |
SoupMessage *message; |
| 5400 |
gchar *uri, *start_date_time, *end_date_time; |
| 5401 |
gboolean success; |
| 5402 |
|
| 5403 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 5404 |
g_return_val_if_fail (calendar_id != NULL, FALSE); |
| 5405 |
g_return_val_if_fail (event_id != NULL, FALSE); |
| 5406 |
g_return_val_if_fail (instance_time != NULL, FALSE); |
| 5407 |
g_return_val_if_fail (out_instance_id != NULL, FALSE); |
| 5408 |
|
| 5409 |
start_date_time = g_strdup_printf ("%04d-%02d-%02dT00:00:00", |
| 5410 |
i_cal_time_get_year (instance_time), |
| 5411 |
i_cal_time_get_month (instance_time), |
| 5412 |
i_cal_time_get_day (instance_time)); |
| 5413 |
end_date_time = g_strdup_printf ("%04d-%02d-%02dT23:59:59.999", |
| 5414 |
i_cal_time_get_year (instance_time), |
| 5415 |
i_cal_time_get_month (instance_time), |
| 5416 |
i_cal_time_get_day (instance_time)); |
| 5417 |
|
| 5418 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 5419 |
group_id ? "calendarGroups" : "calendars", |
| 5420 |
group_id, |
| 5421 |
group_id ? "calendars" : NULL, |
| 5422 |
"", calendar_id, |
| 5423 |
"", "events", |
| 5424 |
"", event_id, |
| 5425 |
"", "instances", |
| 5426 |
"$select", "id,start", |
| 5427 |
"startDateTime", start_date_time, |
| 5428 |
"endDateTime", end_date_time, |
| 5429 |
NULL); |
| 5430 |
|
| 5431 |
g_free (start_date_time); |
| 5432 |
g_free (end_date_time); |
| 5433 |
|
| 5434 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 5435 |
|
| 5436 |
if (!message) { |
| 5437 |
g_free (uri); |
| 5438 |
|
| 5439 |
return FALSE; |
| 5440 |
} |
| 5441 |
|
| 5442 |
g_free (uri); |
| 5443 |
|
| 5444 |
*out_instance_id = NULL; |
| 5445 |
|
| 5446 |
memset (&rd, 0, sizeof (EM365ResponseData)); |
| 5447 |
|
| 5448 |
rd.out_items = &items; |
| 5449 |
|
| 5450 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_valued_response_cb, NULL, &rd, cancellable, error); |
| 5451 |
|
| 5452 |
if (success && items) { |
| 5453 |
EM365Event *event; |
| 5454 |
|
| 5455 |
/* more than one event returned; unlikely, but just in case */ |
| 5456 |
if (items->next) { |
| 5457 |
time_t instance_tt = i_cal_time_as_timet (instance_time); |
| 5458 |
GSList *link; |
| 5459 |
|
| 5460 |
for (link = items; link; link = g_slist_next (link)) { |
| 5461 |
EM365DateTimeWithZone *start_datetime; |
| 5462 |
|
| 5463 |
event = link->data; |
| 5464 |
if (event) |
| 5465 |
continue; |
| 5466 |
|
| 5467 |
start_datetime = e_m365_event_get_start (event); |
| 5468 |
if (!start_datetime) |
| 5469 |
continue; |
| 5470 |
|
| 5471 |
/* this is going to fail with timezones */ |
| 5472 |
if (instance_tt == e_m365_date_time_get_date_time (start_datetime)) { |
| 5473 |
*out_instance_id = g_strdup (e_m365_event_get_id (event)); |
| 5474 |
break; |
| 5475 |
} |
| 5476 |
} |
| 5477 |
} else if (items->data) { |
| 5478 |
/* easy case, only one hit found */ |
| 5479 |
event = items->data; |
| 5480 |
*out_instance_id = g_strdup (e_m365_event_get_id (event)); |
| 5481 |
} |
| 5482 |
} |
| 5483 |
|
| 5484 |
if (success && !*out_instance_id) { |
| 5485 |
gchar *str = i_cal_time_as_ical_string (instance_time); |
| 5486 |
|
| 5487 |
/* Translators: the '%s' is replaced with the date/time, which the instance was looked up for */ |
| 5488 |
g_set_error (error, E_SOUP_SESSION_ERROR, SOUP_STATUS_NOT_FOUND, _("Cannot find instance at '%s'"), str); |
| 5489 |
success = FALSE; |
| 5490 |
|
| 5491 |
g_free (str); |
| 5492 |
} |
| 5493 |
|
| 5494 |
g_clear_object (&message); |
| 5495 |
g_slist_free_full (items, (GDestroyNotify) json_object_unref); |
| 5496 |
|
| 5497 |
return success; |
| 5498 |
} |
| 5499 |
|
| 5500 |
/* https://docs.microsoft.com/en-us/graph/api/event-update?view=graph-rest-1.0&tabs=http */ |
| 5501 |
|
| 5502 |
gboolean |
| 5503 |
e_m365_connection_update_event_sync (EM365Connection *cnc, |
| 5504 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 5505 |
const gchar *group_id, /* nullable - then the default group is used */ |
| 5506 |
const gchar *calendar_id, |
| 5507 |
const gchar *event_id, |
| 5508 |
JsonBuilder *event, |
| 5509 |
GCancellable *cancellable, |
| 5510 |
GError **error) |
| 5511 |
{ |
| 5512 |
SoupMessage *message; |
| 5513 |
gboolean success; |
| 5514 |
gchar *uri; |
| 5515 |
|
| 5516 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 5517 |
g_return_val_if_fail (calendar_id != NULL, FALSE); |
| 5518 |
g_return_val_if_fail (event_id != NULL, FALSE); |
| 5519 |
g_return_val_if_fail (event != NULL, FALSE); |
| 5520 |
|
| 5521 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 5522 |
group_id ? "calendarGroups" : "calendars", |
| 5523 |
group_id, |
| 5524 |
group_id ? "calendars" : NULL, |
| 5525 |
"", calendar_id, |
| 5526 |
"", "events", |
| 5527 |
"", event_id, |
| 5528 |
NULL); |
| 5529 |
|
| 5530 |
message = m365_connection_new_soup_message ("PATCH", uri, CSM_DISABLE_RESPONSE, error); |
| 5531 |
|
| 5532 |
if (!message) { |
| 5533 |
g_free (uri); |
| 5534 |
|
| 5535 |
return FALSE; |
| 5536 |
} |
| 5537 |
|
| 5538 |
g_free (uri); |
| 5539 |
|
| 5540 |
e_m365_connection_set_json_body (message, event); |
| 5541 |
|
| 5542 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_no_response_cb, NULL, cancellable, error); |
| 5543 |
|
| 5544 |
g_clear_object (&message); |
| 5545 |
|
| 5546 |
return success; |
| 5547 |
} |
| 5548 |
|
| 5549 |
/* https://docs.microsoft.com/en-us/graph/api/event-delete?view=graph-rest-1.0&tabs=http */ |
| 5550 |
|
| 5551 |
gboolean |
| 5552 |
e_m365_connection_delete_event_sync (EM365Connection *cnc, |
| 5553 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 5554 |
const gchar *group_id, /* nullable - then the default group is used */ |
| 5555 |
const gchar *calendar_id, |
| 5556 |
const gchar *event_id, |
| 5557 |
GCancellable *cancellable, |
| 5558 |
GError **error) |
| 5559 |
{ |
| 5560 |
SoupMessage *message; |
| 5561 |
gboolean success; |
| 5562 |
gchar *uri; |
| 5563 |
|
| 5564 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 5565 |
g_return_val_if_fail (calendar_id != NULL, FALSE); |
| 5566 |
g_return_val_if_fail (event_id != NULL, FALSE); |
| 5567 |
|
| 5568 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 5569 |
group_id ? "calendarGroups" : "calendars", |
| 5570 |
group_id, |
| 5571 |
group_id ? "calendars" : NULL, |
| 5572 |
"", calendar_id, |
| 5573 |
"", "events", |
| 5574 |
"", event_id, |
| 5575 |
NULL); |
| 5576 |
|
| 5577 |
message = m365_connection_new_soup_message (SOUP_METHOD_DELETE, uri, CSM_DEFAULT, error); |
| 5578 |
|
| 5579 |
if (!message) { |
| 5580 |
g_free (uri); |
| 5581 |
|
| 5582 |
return FALSE; |
| 5583 |
} |
| 5584 |
|
| 5585 |
g_free (uri); |
| 5586 |
|
| 5587 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_no_response_cb, NULL, cancellable, error); |
| 5588 |
|
| 5589 |
g_clear_object (&message); |
| 5590 |
|
| 5591 |
return success; |
| 5592 |
} |
| 5593 |
|
| 5594 |
/* https://docs.microsoft.com/en-us/graph/api/event-accept?view=graph-rest-1.0&tabs=http |
| 5595 |
https://docs.microsoft.com/en-us/graph/api/event-tentativelyaccept?view=graph-rest-1.0&tabs=http |
| 5596 |
https://docs.microsoft.com/en-us/graph/api/event-decline?view=graph-rest-1.0&tabs=http */ |
| 5597 |
|
| 5598 |
gboolean |
| 5599 |
e_m365_connection_response_event_sync (EM365Connection *cnc, |
| 5600 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 5601 |
const gchar *group_id, /* nullable - then the default group is used */ |
| 5602 |
const gchar *calendar_id, |
| 5603 |
const gchar *event_id, |
| 5604 |
EM365ResponseType response, /* uses only accepted/tentatively accepted/declined values */ |
| 5605 |
const gchar *comment, /* nullable */ |
| 5606 |
gboolean send_response, |
| 5607 |
GCancellable *cancellable, |
| 5608 |
GError **error) |
| 5609 |
{ |
| 5610 |
JsonBuilder *builder; |
| 5611 |
SoupMessage *message; |
| 5612 |
gboolean success; |
| 5613 |
gchar *uri; |
| 5614 |
|
| 5615 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 5616 |
g_return_val_if_fail (calendar_id != NULL, FALSE); |
| 5617 |
g_return_val_if_fail (event_id != NULL, FALSE); |
| 5618 |
g_return_val_if_fail (response == E_M365_RESPONSE_ACCEPTED || response == E_M365_RESPONSE_TENTATIVELY_ACCEPTED || response == E_M365_RESPONSE_DECLINED, FALSE); |
| 5619 |
|
| 5620 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 5621 |
group_id ? "calendarGroups" : "calendars", |
| 5622 |
group_id, |
| 5623 |
group_id ? "calendars" : NULL, |
| 5624 |
"", calendar_id, |
| 5625 |
"", "events", |
| 5626 |
"", event_id, |
| 5627 |
"", response == E_M365_RESPONSE_TENTATIVELY_ACCEPTED ? "tentativelyAccept" : |
| 5628 |
response == E_M365_RESPONSE_DECLINED ? "decline" : "accept", |
| 5629 |
NULL); |
| 5630 |
|
| 5631 |
message = m365_connection_new_soup_message (SOUP_METHOD_POST, uri, CSM_DISABLE_RESPONSE, error); |
| 5632 |
|
| 5633 |
if (!message) { |
| 5634 |
g_free (uri); |
| 5635 |
|
| 5636 |
return FALSE; |
| 5637 |
} |
| 5638 |
|
| 5639 |
g_free (uri); |
| 5640 |
|
| 5641 |
builder = json_builder_new_immutable (); |
| 5642 |
|
| 5643 |
e_m365_json_begin_object_member (builder, NULL); |
| 5644 |
e_m365_json_add_nonempty_string_member (builder, "comment", comment); |
| 5645 |
e_m365_json_add_boolean_member (builder, "sendResponse", send_response); |
| 5646 |
e_m365_json_end_object_member (builder); |
| 5647 |
|
| 5648 |
e_m365_connection_set_json_body (message, builder); |
| 5649 |
|
| 5650 |
g_object_unref (builder); |
| 5651 |
|
| 5652 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_no_response_cb, NULL, cancellable, error); |
| 5653 |
|
| 5654 |
g_clear_object (&message); |
| 5655 |
|
| 5656 |
return success; |
| 5657 |
} |
| 5658 |
|
| 5659 |
/* https://learn.microsoft.com/en-us/graph/api/event-cancel?view=graph-rest-1.0&tabs=http */ |
| 5660 |
|
| 5661 |
gboolean |
| 5662 |
e_m365_connection_cancel_event_sync (EM365Connection *cnc, |
| 5663 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 5664 |
const gchar *group_id, /* nullable - then the default group is used */ |
| 5665 |
const gchar *calendar_id, |
| 5666 |
const gchar *event_id, |
| 5667 |
const gchar *comment, /* nullable */ |
| 5668 |
GCancellable *cancellable, |
| 5669 |
GError **error) |
| 5670 |
{ |
| 5671 |
JsonBuilder *builder; |
| 5672 |
SoupMessage *message; |
| 5673 |
gboolean success; |
| 5674 |
gchar *uri; |
| 5675 |
|
| 5676 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 5677 |
g_return_val_if_fail (calendar_id != NULL, FALSE); |
| 5678 |
g_return_val_if_fail (event_id != NULL, FALSE); |
| 5679 |
|
| 5680 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 5681 |
group_id ? "calendarGroups" : "calendars", |
| 5682 |
group_id, |
| 5683 |
group_id ? "calendars" : NULL, |
| 5684 |
"", calendar_id, |
| 5685 |
"", "events", |
| 5686 |
"", event_id, |
| 5687 |
"", "cancel", |
| 5688 |
NULL); |
| 5689 |
|
| 5690 |
message = m365_connection_new_soup_message (SOUP_METHOD_POST, uri, CSM_DISABLE_RESPONSE, error); |
| 5691 |
|
| 5692 |
if (!message) { |
| 5693 |
g_free (uri); |
| 5694 |
|
| 5695 |
return FALSE; |
| 5696 |
} |
| 5697 |
|
| 5698 |
g_free (uri); |
| 5699 |
|
| 5700 |
builder = json_builder_new_immutable (); |
| 5701 |
|
| 5702 |
e_m365_json_begin_object_member (builder, NULL); |
| 5703 |
e_m365_json_add_nonempty_string_member (builder, "comment", comment); |
| 5704 |
e_m365_json_end_object_member (builder); |
| 5705 |
|
| 5706 |
e_m365_connection_set_json_body (message, builder); |
| 5707 |
|
| 5708 |
g_object_unref (builder); |
| 5709 |
|
| 5710 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_no_response_cb, NULL, cancellable, error); |
| 5711 |
|
| 5712 |
g_clear_object (&message); |
| 5713 |
|
| 5714 |
return success; |
| 5715 |
} |
| 5716 |
|
| 5717 |
/* https://docs.microsoft.com/en-us/graph/api/event-dismissreminder?view=graph-rest-1.0&tabs=http */ |
| 5718 |
|
| 5719 |
gboolean |
| 5720 |
e_m365_connection_dismiss_reminder_sync (EM365Connection *cnc, |
| 5721 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 5722 |
const gchar *group_id, /* nullable - then the default group is used */ |
| 5723 |
const gchar *calendar_id, |
| 5724 |
const gchar *event_id, |
| 5725 |
GCancellable *cancellable, |
| 5726 |
GError **error) |
| 5727 |
{ |
| 5728 |
SoupMessage *message; |
| 5729 |
gboolean success; |
| 5730 |
gchar *uri; |
| 5731 |
|
| 5732 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 5733 |
g_return_val_if_fail (calendar_id != NULL, FALSE); |
| 5734 |
g_return_val_if_fail (event_id != NULL, FALSE); |
| 5735 |
|
| 5736 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 5737 |
group_id ? "calendarGroups" : "calendars", |
| 5738 |
group_id, |
| 5739 |
group_id ? "calendars" : NULL, |
| 5740 |
"", calendar_id, |
| 5741 |
"", "events", |
| 5742 |
"", event_id, |
| 5743 |
"", "dismissReminder", |
| 5744 |
NULL); |
| 5745 |
|
| 5746 |
message = m365_connection_new_soup_message (SOUP_METHOD_POST, uri, CSM_DEFAULT, error); |
| 5747 |
|
| 5748 |
if (!message) { |
| 5749 |
g_free (uri); |
| 5750 |
|
| 5751 |
return FALSE; |
| 5752 |
} |
| 5753 |
|
| 5754 |
g_free (uri); |
| 5755 |
|
| 5756 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_no_response_cb, NULL, cancellable, error); |
| 5757 |
|
| 5758 |
g_clear_object (&message); |
| 5759 |
|
| 5760 |
return success; |
| 5761 |
} |
| 5762 |
|
| 5763 |
/* https://docs.microsoft.com/en-us/graph/api/event-list-attachments?view=graph-rest-1.0&tabs=http */ |
| 5764 |
|
| 5765 |
gboolean |
| 5766 |
e_m365_connection_list_event_attachments_sync (EM365Connection *cnc, |
| 5767 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 5768 |
const gchar *group_id, /* nullable, then the default group is used */ |
| 5769 |
const gchar *calendar_id, |
| 5770 |
const gchar *event_id, |
| 5771 |
const gchar *select, /* nullable - properties to select */ |
| 5772 |
GSList **out_attachments, /* EM365Attachment * */ |
| 5773 |
GCancellable *cancellable, |
| 5774 |
GError **error) |
| 5775 |
{ |
| 5776 |
EM365ResponseData rd; |
| 5777 |
SoupMessage *message; |
| 5778 |
gchar *uri; |
| 5779 |
gboolean success; |
| 5780 |
|
| 5781 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 5782 |
g_return_val_if_fail (calendar_id != NULL, FALSE); |
| 5783 |
g_return_val_if_fail (event_id != NULL, FALSE); |
| 5784 |
g_return_val_if_fail (out_attachments != NULL, FALSE); |
| 5785 |
|
| 5786 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 5787 |
group_id ? "calendarGroups" : "calendars", |
| 5788 |
group_id, |
| 5789 |
group_id ? "calendars" : NULL, |
| 5790 |
"", calendar_id, |
| 5791 |
"", "events", |
| 5792 |
"", event_id, |
| 5793 |
"", "attachments", |
| 5794 |
"$select", select, |
| 5795 |
NULL); |
| 5796 |
|
| 5797 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 5798 |
|
| 5799 |
if (!message) { |
| 5800 |
g_free (uri); |
| 5801 |
|
| 5802 |
return FALSE; |
| 5803 |
} |
| 5804 |
|
| 5805 |
g_free (uri); |
| 5806 |
|
| 5807 |
memset (&rd, 0, sizeof (EM365ResponseData)); |
| 5808 |
|
| 5809 |
rd.out_items = out_attachments; |
| 5810 |
|
| 5811 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_valued_response_cb, NULL, &rd, cancellable, error); |
| 5812 |
|
| 5813 |
g_clear_object (&message); |
| 5814 |
|
| 5815 |
return success; |
| 5816 |
} |
| 5817 |
|
| 5818 |
/* https://docs.microsoft.com/en-us/graph/api/attachment-get?view=graph-rest-1.0&tabs=http */ |
| 5819 |
|
| 5820 |
gboolean |
| 5821 |
e_m365_connection_get_event_attachment_sync (EM365Connection *cnc, |
| 5822 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 5823 |
const gchar *group_id, /* nullable, then the default group is used */ |
| 5824 |
const gchar *calendar_id, |
| 5825 |
const gchar *event_id, |
| 5826 |
const gchar *attachment_id, |
| 5827 |
EM365ConnectionRawDataFunc func, |
| 5828 |
gpointer func_user_data, |
| 5829 |
GCancellable *cancellable, |
| 5830 |
GError **error) |
| 5831 |
{ |
| 5832 |
SoupMessage *message; |
| 5833 |
gboolean success; |
| 5834 |
gchar *uri; |
| 5835 |
|
| 5836 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 5837 |
g_return_val_if_fail (calendar_id != NULL, FALSE); |
| 5838 |
g_return_val_if_fail (event_id != NULL, FALSE); |
| 5839 |
g_return_val_if_fail (attachment_id != NULL, FALSE); |
| 5840 |
g_return_val_if_fail (func != NULL, FALSE); |
| 5841 |
|
| 5842 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 5843 |
group_id ? "calendarGroups" : "calendars", |
| 5844 |
group_id, |
| 5845 |
group_id ? "calendars" : NULL, |
| 5846 |
"", calendar_id, |
| 5847 |
"", "events", |
| 5848 |
"", event_id, |
| 5849 |
"", "attachments", |
| 5850 |
"", attachment_id, |
| 5851 |
"", "$value", |
| 5852 |
NULL); |
| 5853 |
|
| 5854 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 5855 |
|
| 5856 |
if (!message) { |
| 5857 |
g_free (uri); |
| 5858 |
|
| 5859 |
return FALSE; |
| 5860 |
} |
| 5861 |
|
| 5862 |
g_free (uri); |
| 5863 |
|
| 5864 |
success = m365_connection_send_request_sync (cnc, message, NULL, func, func_user_data, cancellable, error); |
| 5865 |
|
| 5866 |
g_clear_object (&message); |
| 5867 |
|
| 5868 |
return success; |
| 5869 |
} |
| 5870 |
|
| 5871 |
/* https://docs.microsoft.com/en-us/graph/api/event-post-attachments?view=graph-rest-1.0&tabs=http */ |
| 5872 |
|
| 5873 |
gboolean |
| 5874 |
e_m365_connection_add_event_attachment_sync (EM365Connection *cnc, |
| 5875 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 5876 |
const gchar *group_id, /* nullable, then the default group is used */ |
| 5877 |
const gchar *calendar_id, /* nullable, then the default user calendar is used */ |
| 5878 |
const gchar *event_id, |
| 5879 |
JsonBuilder *in_attachment, |
| 5880 |
EM365Attachment **out_attachment, /* nullable */ |
| 5881 |
GCancellable *cancellable, |
| 5882 |
GError **error) |
| 5883 |
{ |
| 5884 |
SoupMessage *message; |
| 5885 |
gboolean success; |
| 5886 |
gchar *uri; |
| 5887 |
|
| 5888 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 5889 |
g_return_val_if_fail (event_id != NULL, FALSE); |
| 5890 |
g_return_val_if_fail (in_attachment != NULL, FALSE); |
| 5891 |
|
| 5892 |
if (calendar_id) { |
| 5893 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 5894 |
group_id ? "calendarGroups" : "calendars", |
| 5895 |
group_id, |
| 5896 |
group_id ? "calendars" : NULL, |
| 5897 |
"", calendar_id, |
| 5898 |
"", "events", |
| 5899 |
"", event_id, |
| 5900 |
"", "attachments", |
| 5901 |
NULL); |
| 5902 |
} else { |
| 5903 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, "users", |
| 5904 |
"events", NULL, NULL, |
| 5905 |
"", event_id, |
| 5906 |
"", "attachments", |
| 5907 |
NULL); |
| 5908 |
} |
| 5909 |
|
| 5910 |
message = m365_connection_new_soup_message (SOUP_METHOD_POST, uri, out_attachment ? CSM_DEFAULT : CSM_DISABLE_RESPONSE, error); |
| 5911 |
|
| 5912 |
if (!message) { |
| 5913 |
g_free (uri); |
| 5914 |
|
| 5915 |
return FALSE; |
| 5916 |
} |
| 5917 |
|
| 5918 |
g_free (uri); |
| 5919 |
|
| 5920 |
e_m365_connection_set_json_body (message, in_attachment); |
| 5921 |
|
| 5922 |
success = m365_connection_send_request_sync (cnc, message, out_attachment ? e_m365_read_json_object_response_cb : NULL, |
| 5923 |
out_attachment ? NULL : e_m365_read_no_response_cb, out_attachment, cancellable, error); |
| 5924 |
|
| 5925 |
g_clear_object (&message); |
| 5926 |
|
| 5927 |
return success; |
| 5928 |
} |
| 5929 |
|
| 5930 |
/* https://docs.microsoft.com/en-us/graph/api/attachment-delete?view=graph-rest-1.0&tabs=http */ |
| 5931 |
|
| 5932 |
gboolean |
| 5933 |
e_m365_connection_delete_event_attachment_sync (EM365Connection *cnc, |
| 5934 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 5935 |
const gchar *group_id, /* nullable, then the default group is used */ |
| 5936 |
const gchar *calendar_id, |
| 5937 |
const gchar *event_id, |
| 5938 |
const gchar *attachment_id, |
| 5939 |
GCancellable *cancellable, |
| 5940 |
GError **error) |
| 5941 |
{ |
| 5942 |
SoupMessage *message; |
| 5943 |
gboolean success; |
| 5944 |
gchar *uri; |
| 5945 |
|
| 5946 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 5947 |
g_return_val_if_fail (calendar_id != NULL, FALSE); |
| 5948 |
g_return_val_if_fail (event_id != NULL, FALSE); |
| 5949 |
g_return_val_if_fail (attachment_id != NULL, FALSE); |
| 5950 |
|
| 5951 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 5952 |
group_id ? "calendarGroups" : "calendars", |
| 5953 |
group_id, |
| 5954 |
group_id ? "calendars" : NULL, |
| 5955 |
"", calendar_id, |
| 5956 |
"", "events", |
| 5957 |
"", event_id, |
| 5958 |
"", "attachments", |
| 5959 |
"", attachment_id, |
| 5960 |
NULL); |
| 5961 |
|
| 5962 |
message = m365_connection_new_soup_message (SOUP_METHOD_DELETE, uri, CSM_DEFAULT, error); |
| 5963 |
|
| 5964 |
if (!message) { |
| 5965 |
g_free (uri); |
| 5966 |
|
| 5967 |
return FALSE; |
| 5968 |
} |
| 5969 |
|
| 5970 |
g_free (uri); |
| 5971 |
|
| 5972 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_no_response_cb, NULL, cancellable, error); |
| 5973 |
|
| 5974 |
g_clear_object (&message); |
| 5975 |
|
| 5976 |
return success; |
| 5977 |
} |
| 5978 |
|
| 5979 |
/* https://docs.microsoft.com/en-us/graph/api/calendar-getschedule?view=graph-rest-1.0&tabs=http */ |
| 5980 |
|
| 5981 |
gboolean |
| 5982 |
e_m365_connection_get_schedule_sync (EM365Connection *cnc, |
| 5983 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 5984 |
gint interval_minutes, /* between 5 and 1440, -1 to use the default (30) */ |
| 5985 |
time_t start_time, |
| 5986 |
time_t end_time, |
| 5987 |
const GSList *email_addresses, /* const gchar * - SMTP addresses to query */ |
| 5988 |
GSList **out_infos, /* EM365ScheduleInformation * */ |
| 5989 |
GCancellable *cancellable, |
| 5990 |
GError **error) |
| 5991 |
{ |
| 5992 |
EM365ResponseData rd; |
| 5993 |
JsonBuilder *builder; |
| 5994 |
SoupMessage *message; |
| 5995 |
GSList *link; |
| 5996 |
gboolean success; |
| 5997 |
gchar *uri; |
| 5998 |
|
| 5999 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 6000 |
g_return_val_if_fail (email_addresses != NULL, FALSE); |
| 6001 |
g_return_val_if_fail (out_infos != NULL, FALSE); |
| 6002 |
|
| 6003 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 6004 |
"calendar", |
| 6005 |
"getSchedule", |
| 6006 |
NULL, |
| 6007 |
NULL); |
| 6008 |
|
| 6009 |
message = m365_connection_new_soup_message (SOUP_METHOD_POST, uri, CSM_DEFAULT, error); |
| 6010 |
|
| 6011 |
if (!message) { |
| 6012 |
g_free (uri); |
| 6013 |
|
| 6014 |
return FALSE; |
| 6015 |
} |
| 6016 |
|
| 6017 |
g_free (uri); |
| 6018 |
|
| 6019 |
builder = json_builder_new_immutable (); |
| 6020 |
|
| 6021 |
e_m365_json_begin_object_member (builder, NULL); |
| 6022 |
|
| 6023 |
if (interval_minutes > 0) |
| 6024 |
e_m365_json_add_int_member (builder, "interval", interval_minutes); |
| 6025 |
|
| 6026 |
e_m365_add_date_time (builder, "startTime", start_time, "UTC"); |
| 6027 |
e_m365_add_date_time (builder, "endTime", end_time, "UTC"); |
| 6028 |
e_m365_json_begin_array_member (builder, "schedules"); |
| 6029 |
|
| 6030 |
for (link = (GSList *) email_addresses; link; link = g_slist_next (link)) { |
| 6031 |
const gchar *addr = link->data; |
| 6032 |
|
| 6033 |
if (addr && *addr) |
| 6034 |
json_builder_add_string_value (builder, addr); |
| 6035 |
} |
| 6036 |
|
| 6037 |
e_m365_json_end_array_member (builder); /* "schedules" */ |
| 6038 |
e_m365_json_end_object_member (builder); |
| 6039 |
|
| 6040 |
e_m365_connection_set_json_body (message, builder); |
| 6041 |
|
| 6042 |
g_object_unref (builder); |
| 6043 |
|
| 6044 |
memset (&rd, 0, sizeof (EM365ResponseData)); |
| 6045 |
|
| 6046 |
rd.out_items = out_infos; |
| 6047 |
|
| 6048 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_valued_response_cb, NULL, &rd, cancellable, error); |
| 6049 |
|
| 6050 |
g_clear_object (&message); |
| 6051 |
|
| 6052 |
return success; |
| 6053 |
} |
| 6054 |
|
| 6055 |
/* https://learn.microsoft.com/en-us/graph/api/todo-list-lists?view=graph-rest-1.0&tabs=http */ |
| 6056 |
|
| 6057 |
gboolean |
| 6058 |
e_m365_connection_list_task_lists_sync (EM365Connection *cnc, |
| 6059 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 6060 |
GSList **out_task_lists, /* EM365TaskList * - the returned todoTaskList objects */ |
| 6061 |
GCancellable *cancellable, |
| 6062 |
GError **error) |
| 6063 |
{ |
| 6064 |
EM365ResponseData rd; |
| 6065 |
SoupMessage *message; |
| 6066 |
gchar *uri; |
| 6067 |
gboolean success; |
| 6068 |
|
| 6069 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 6070 |
g_return_val_if_fail (out_task_lists != NULL, FALSE); |
| 6071 |
|
| 6072 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 6073 |
"todo", |
| 6074 |
"lists", |
| 6075 |
NULL, |
| 6076 |
NULL); |
| 6077 |
|
| 6078 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 6079 |
|
| 6080 |
if (!message) { |
| 6081 |
g_free (uri); |
| 6082 |
|
| 6083 |
return FALSE; |
| 6084 |
} |
| 6085 |
|
| 6086 |
g_free (uri); |
| 6087 |
|
| 6088 |
memset (&rd, 0, sizeof (EM365ResponseData)); |
| 6089 |
|
| 6090 |
rd.out_items = out_task_lists; |
| 6091 |
|
| 6092 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_valued_response_cb, NULL, &rd, cancellable, error); |
| 6093 |
|
| 6094 |
g_clear_object (&message); |
| 6095 |
|
| 6096 |
return success; |
| 6097 |
} |
| 6098 |
|
| 6099 |
/* https://learn.microsoft.com/en-us/graph/api/todo-post-lists?view=graph-rest-1.0&tabs=http */ |
| 6100 |
|
| 6101 |
gboolean |
| 6102 |
e_m365_connection_create_task_list_sync (EM365Connection *cnc, |
| 6103 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 6104 |
JsonBuilder *task_list, |
| 6105 |
EM365TaskList **out_created_task_list, |
| 6106 |
GCancellable *cancellable, |
| 6107 |
GError **error) |
| 6108 |
{ |
| 6109 |
SoupMessage *message; |
| 6110 |
gboolean success; |
| 6111 |
gchar *uri; |
| 6112 |
|
| 6113 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 6114 |
g_return_val_if_fail (task_list != NULL, FALSE); |
| 6115 |
g_return_val_if_fail (out_created_task_list != NULL, FALSE); |
| 6116 |
|
| 6117 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 6118 |
"todo", |
| 6119 |
"lists", |
| 6120 |
NULL, |
| 6121 |
NULL); |
| 6122 |
|
| 6123 |
message = m365_connection_new_soup_message (SOUP_METHOD_POST, uri, CSM_DEFAULT, error); |
| 6124 |
|
| 6125 |
if (!message) { |
| 6126 |
g_free (uri); |
| 6127 |
|
| 6128 |
return FALSE; |
| 6129 |
} |
| 6130 |
|
| 6131 |
g_free (uri); |
| 6132 |
|
| 6133 |
e_m365_connection_set_json_body (message, task_list); |
| 6134 |
|
| 6135 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, out_created_task_list, cancellable, error); |
| 6136 |
|
| 6137 |
g_clear_object (&message); |
| 6138 |
|
| 6139 |
return success; |
| 6140 |
} |
| 6141 |
|
| 6142 |
/* https://learn.microsoft.com/en-us/graph/api/todotasklist-get?view=graph-rest-1.0&tabs=http */ |
| 6143 |
|
| 6144 |
gboolean |
| 6145 |
e_m365_connection_get_task_list_sync (EM365Connection *cnc, |
| 6146 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 6147 |
const gchar *task_list_id, |
| 6148 |
EM365TaskList **out_task_list, |
| 6149 |
GCancellable *cancellable, |
| 6150 |
GError **error) |
| 6151 |
{ |
| 6152 |
SoupMessage *message; |
| 6153 |
gchar *uri; |
| 6154 |
gboolean success; |
| 6155 |
|
| 6156 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 6157 |
g_return_val_if_fail (task_list_id != NULL, FALSE); |
| 6158 |
g_return_val_if_fail (out_task_list != NULL, FALSE); |
| 6159 |
|
| 6160 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 6161 |
"todo", |
| 6162 |
"lists", |
| 6163 |
task_list_id, |
| 6164 |
NULL); |
| 6165 |
|
| 6166 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 6167 |
|
| 6168 |
if (!message) { |
| 6169 |
g_free (uri); |
| 6170 |
|
| 6171 |
return FALSE; |
| 6172 |
} |
| 6173 |
|
| 6174 |
g_free (uri); |
| 6175 |
|
| 6176 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, out_task_list, cancellable, error); |
| 6177 |
|
| 6178 |
g_clear_object (&message); |
| 6179 |
|
| 6180 |
return success; |
| 6181 |
} |
| 6182 |
|
| 6183 |
/* https://learn.microsoft.com/en-us/graph/api/todotasklist-update?view=graph-rest-1.0&tabs=http */ |
| 6184 |
|
| 6185 |
gboolean |
| 6186 |
e_m365_connection_update_task_list_sync (EM365Connection *cnc, |
| 6187 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 6188 |
const gchar *task_list_id, |
| 6189 |
const gchar *display_name, |
| 6190 |
GCancellable *cancellable, |
| 6191 |
GError **error) |
| 6192 |
{ |
| 6193 |
SoupMessage *message; |
| 6194 |
JsonBuilder *builder; |
| 6195 |
gboolean success; |
| 6196 |
gchar *uri; |
| 6197 |
|
| 6198 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 6199 |
g_return_val_if_fail (task_list_id != NULL, FALSE); |
| 6200 |
g_return_val_if_fail (display_name != NULL, FALSE); |
| 6201 |
|
| 6202 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 6203 |
"todo", |
| 6204 |
"lists", |
| 6205 |
task_list_id, |
| 6206 |
NULL); |
| 6207 |
|
| 6208 |
message = m365_connection_new_soup_message ("PATCH", uri, CSM_DISABLE_RESPONSE, error); |
| 6209 |
|
| 6210 |
if (!message) { |
| 6211 |
g_free (uri); |
| 6212 |
|
| 6213 |
return FALSE; |
| 6214 |
} |
| 6215 |
|
| 6216 |
g_free (uri); |
| 6217 |
|
| 6218 |
builder = json_builder_new_immutable (); |
| 6219 |
|
| 6220 |
e_m365_json_begin_object_member (builder, NULL); |
| 6221 |
e_m365_json_add_string_member (builder, "displayName", display_name); |
| 6222 |
e_m365_json_end_object_member (builder); |
| 6223 |
|
| 6224 |
e_m365_connection_set_json_body (message, builder); |
| 6225 |
|
| 6226 |
g_object_unref (builder); |
| 6227 |
|
| 6228 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_no_response_cb, NULL, cancellable, error); |
| 6229 |
|
| 6230 |
g_clear_object (&message); |
| 6231 |
|
| 6232 |
return success; |
| 6233 |
} |
| 6234 |
|
| 6235 |
/* https://learn.microsoft.com/en-us/graph/api/todotasklist-delete?view=graph-rest-1.0&tabs=http */ |
| 6236 |
|
| 6237 |
gboolean |
| 6238 |
e_m365_connection_delete_task_list_sync (EM365Connection *cnc, |
| 6239 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 6240 |
const gchar *task_list_id, |
| 6241 |
GCancellable *cancellable, |
| 6242 |
GError **error) |
| 6243 |
{ |
| 6244 |
SoupMessage *message; |
| 6245 |
gboolean success; |
| 6246 |
gchar *uri; |
| 6247 |
|
| 6248 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 6249 |
g_return_val_if_fail (task_list_id != NULL, FALSE); |
| 6250 |
|
| 6251 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 6252 |
"todo", |
| 6253 |
"lists", |
| 6254 |
task_list_id, |
| 6255 |
NULL); |
| 6256 |
|
| 6257 |
message = m365_connection_new_soup_message (SOUP_METHOD_DELETE, uri, CSM_DEFAULT, error); |
| 6258 |
|
| 6259 |
if (!message) { |
| 6260 |
g_free (uri); |
| 6261 |
|
| 6262 |
return FALSE; |
| 6263 |
} |
| 6264 |
|
| 6265 |
g_free (uri); |
| 6266 |
|
| 6267 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_no_response_cb, NULL, cancellable, error); |
| 6268 |
|
| 6269 |
g_clear_object (&message); |
| 6270 |
|
| 6271 |
return success; |
| 6272 |
} |
| 6273 |
|
| 6274 |
/* https://learn.microsoft.com/en-us/graph/api/todotasklist-delta?view=graph-rest-1.0 */ |
| 6275 |
|
| 6276 |
gboolean |
| 6277 |
e_m365_connection_get_task_lists_delta_sync (EM365Connection *cnc, |
| 6278 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 6279 |
const gchar *delta_link, /* previous delta link */ |
| 6280 |
guint max_page_size, /* 0 for default by the server */ |
| 6281 |
EM365ConnectionJsonFunc func, /* function to call with each result set */ |
| 6282 |
gpointer func_user_data, /* user data passed into the 'func' */ |
| 6283 |
gchar **out_delta_link, |
| 6284 |
GCancellable *cancellable, |
| 6285 |
GError **error) |
| 6286 |
{ |
| 6287 |
EM365ResponseData rd; |
| 6288 |
SoupMessage *message = NULL; |
| 6289 |
gboolean success; |
| 6290 |
|
| 6291 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 6292 |
g_return_val_if_fail (out_delta_link != NULL, FALSE); |
| 6293 |
g_return_val_if_fail (func != NULL, FALSE); |
| 6294 |
|
| 6295 |
if (delta_link) { |
| 6296 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, delta_link, CSM_DEFAULT, NULL); |
| 6297 |
} else { |
| 6298 |
gchar *uri; |
| 6299 |
|
| 6300 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 6301 |
"todo", |
| 6302 |
"lists", |
| 6303 |
"delta", |
| 6304 |
NULL); |
| 6305 |
|
| 6306 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 6307 |
|
| 6308 |
if (!message) { |
| 6309 |
g_free (uri); |
| 6310 |
|
| 6311 |
return FALSE; |
| 6312 |
} |
| 6313 |
|
| 6314 |
g_free (uri); |
| 6315 |
} |
| 6316 |
|
| 6317 |
if (max_page_size > 0) { |
| 6318 |
gchar *prefer_value; |
| 6319 |
|
| 6320 |
prefer_value = g_strdup_printf ("odata.maxpagesize=%u", max_page_size); |
| 6321 |
|
| 6322 |
soup_message_headers_append (soup_message_get_request_headers (message), "Prefer", prefer_value); |
| 6323 |
|
| 6324 |
g_free (prefer_value); |
| 6325 |
} |
| 6326 |
|
| 6327 |
memset (&rd, 0, sizeof (EM365ResponseData)); |
| 6328 |
|
| 6329 |
rd.json_func = func; |
| 6330 |
rd.func_user_data = func_user_data; |
| 6331 |
rd.out_delta_link = out_delta_link; |
| 6332 |
|
| 6333 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_valued_response_cb, NULL, &rd, cancellable, error); |
| 6334 |
|
| 6335 |
g_clear_object (&message); |
| 6336 |
|
| 6337 |
return success; |
| 6338 |
} |
| 6339 |
|
| 6340 |
/* https://learn.microsoft.com/en-us/graph/api/todotasklist-list-tasks?view=graph-rest-1.0&tabs=http */ |
| 6341 |
|
| 6342 |
gboolean |
| 6343 |
e_m365_connection_list_tasks_sync (EM365Connection *cnc, |
| 6344 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 6345 |
const gchar *group_id, /* unused, always NULL */ |
| 6346 |
const gchar *task_list_id, |
| 6347 |
const gchar *prefer_outlook_timezone, /* nullable - then UTC, otherwise that zone for the returned times */ |
| 6348 |
const gchar *select, /* nullable - properties to select */ |
| 6349 |
const gchar *filter, /* nullable - filter which tasks to list */ |
| 6350 |
GSList **out_tasks, /* EM365Task * - the returned task objects */ |
| 6351 |
GCancellable *cancellable, |
| 6352 |
GError **error) |
| 6353 |
{ |
| 6354 |
EM365ResponseData rd; |
| 6355 |
SoupMessage *message; |
| 6356 |
gchar *uri; |
| 6357 |
gboolean success; |
| 6358 |
|
| 6359 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 6360 |
g_return_val_if_fail (task_list_id != NULL, FALSE); |
| 6361 |
g_return_val_if_fail (out_tasks != NULL, FALSE); |
| 6362 |
|
| 6363 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 6364 |
"todo", |
| 6365 |
"lists", |
| 6366 |
task_list_id, |
| 6367 |
"", "tasks", |
| 6368 |
"$select", select, |
| 6369 |
"$filter", filter, |
| 6370 |
NULL); |
| 6371 |
|
| 6372 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 6373 |
|
| 6374 |
if (!message) { |
| 6375 |
g_free (uri); |
| 6376 |
|
| 6377 |
return FALSE; |
| 6378 |
} |
| 6379 |
|
| 6380 |
g_free (uri); |
| 6381 |
|
| 6382 |
m365_connection_prefer_outlook_timezone (message, prefer_outlook_timezone); |
| 6383 |
soup_message_headers_append (soup_message_get_request_headers (message), "Prefer", "outlook.body-content-type=\"text\""); |
| 6384 |
|
| 6385 |
memset (&rd, 0, sizeof (EM365ResponseData)); |
| 6386 |
|
| 6387 |
rd.out_items = out_tasks; |
| 6388 |
|
| 6389 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_valued_response_cb, NULL, &rd, cancellable, error); |
| 6390 |
|
| 6391 |
g_clear_object (&message); |
| 6392 |
|
| 6393 |
return success; |
| 6394 |
} |
| 6395 |
|
| 6396 |
/* https://learn.microsoft.com/en-us/graph/api/todotasklist-post-tasks?view=graph-rest-1.0&tabs=http */ |
| 6397 |
|
| 6398 |
gboolean |
| 6399 |
e_m365_connection_create_task_sync (EM365Connection *cnc, |
| 6400 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 6401 |
const gchar *group_id, /* unused, always NULL */ |
| 6402 |
const gchar *task_list_id, |
| 6403 |
JsonBuilder *task, |
| 6404 |
EM365Task **out_created_task, |
| 6405 |
GCancellable *cancellable, |
| 6406 |
GError **error) |
| 6407 |
{ |
| 6408 |
SoupMessage *message; |
| 6409 |
gboolean success; |
| 6410 |
gchar *uri; |
| 6411 |
|
| 6412 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 6413 |
g_return_val_if_fail (task_list_id != NULL, FALSE); |
| 6414 |
g_return_val_if_fail (task != NULL, FALSE); |
| 6415 |
g_return_val_if_fail (out_created_task != NULL, FALSE); |
| 6416 |
|
| 6417 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 6418 |
"todo", |
| 6419 |
"lists", |
| 6420 |
task_list_id, |
| 6421 |
"", "tasks", |
| 6422 |
NULL); |
| 6423 |
|
| 6424 |
message = m365_connection_new_soup_message (SOUP_METHOD_POST, uri, CSM_DEFAULT, error); |
| 6425 |
|
| 6426 |
if (!message) { |
| 6427 |
g_free (uri); |
| 6428 |
|
| 6429 |
return FALSE; |
| 6430 |
} |
| 6431 |
|
| 6432 |
g_free (uri); |
| 6433 |
|
| 6434 |
e_m365_connection_set_json_body (message, task); |
| 6435 |
|
| 6436 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, out_created_task, cancellable, error); |
| 6437 |
|
| 6438 |
g_clear_object (&message); |
| 6439 |
|
| 6440 |
return success; |
| 6441 |
} |
| 6442 |
|
| 6443 |
/* https://learn.microsoft.com/en-us/graph/api/todotask-get?view=graph-rest-1.0&tabs=http */ |
| 6444 |
|
| 6445 |
SoupMessage * |
| 6446 |
e_m365_connection_prepare_get_task (EM365Connection *cnc, |
| 6447 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 6448 |
const gchar *group_id, /* unused, always NULL */ |
| 6449 |
const gchar *task_list_id, |
| 6450 |
const gchar *task_id, |
| 6451 |
const gchar *prefer_outlook_timezone, /* nullable - then UTC, otherwise that zone for the returned times */ |
| 6452 |
const gchar *select, /* nullable - properties to select */ |
| 6453 |
GError **error) |
| 6454 |
{ |
| 6455 |
SoupMessage *message; |
| 6456 |
gchar *uri; |
| 6457 |
|
| 6458 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), NULL); |
| 6459 |
g_return_val_if_fail (task_list_id != NULL, NULL); |
| 6460 |
g_return_val_if_fail (task_id != NULL, NULL); |
| 6461 |
|
| 6462 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 6463 |
"todo", |
| 6464 |
"lists", |
| 6465 |
task_list_id, |
| 6466 |
"", "tasks", |
| 6467 |
"", task_id, |
| 6468 |
"$select", select, |
| 6469 |
NULL); |
| 6470 |
|
| 6471 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 6472 |
|
| 6473 |
if (!message) { |
| 6474 |
g_free (uri); |
| 6475 |
|
| 6476 |
return NULL; |
| 6477 |
} |
| 6478 |
|
| 6479 |
g_free (uri); |
| 6480 |
|
| 6481 |
m365_connection_prefer_outlook_timezone (message, prefer_outlook_timezone); |
| 6482 |
soup_message_headers_append (soup_message_get_request_headers (message), "Prefer", "outlook.body-content-type=\"text\""); |
| 6483 |
|
| 6484 |
return message; |
| 6485 |
} |
| 6486 |
|
| 6487 |
gboolean |
| 6488 |
e_m365_connection_get_task_sync (EM365Connection *cnc, |
| 6489 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 6490 |
const gchar *group_id, /* unused, always NULL */ |
| 6491 |
const gchar *task_list_id, |
| 6492 |
const gchar *task_id, |
| 6493 |
const gchar *prefer_outlook_timezone, /* nullable - then UTC, otherwise that zone for the returned times */ |
| 6494 |
const gchar *select, /* nullable - properties to select */ |
| 6495 |
EM365Task **out_task, |
| 6496 |
GCancellable *cancellable, |
| 6497 |
GError **error) |
| 6498 |
{ |
| 6499 |
SoupMessage *message; |
| 6500 |
gboolean success; |
| 6501 |
|
| 6502 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 6503 |
g_return_val_if_fail (task_list_id != NULL, FALSE); |
| 6504 |
g_return_val_if_fail (task_id != NULL, FALSE); |
| 6505 |
g_return_val_if_fail (out_task != NULL, FALSE); |
| 6506 |
|
| 6507 |
message = e_m365_connection_prepare_get_task (cnc, user_override, group_id, task_list_id, task_id, prefer_outlook_timezone, select, error); |
| 6508 |
|
| 6509 |
if (!message) |
| 6510 |
return FALSE; |
| 6511 |
|
| 6512 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, out_task, cancellable, error); |
| 6513 |
|
| 6514 |
g_clear_object (&message); |
| 6515 |
|
| 6516 |
return success; |
| 6517 |
} |
| 6518 |
|
| 6519 |
gboolean |
| 6520 |
e_m365_connection_get_tasks_sync (EM365Connection *cnc, |
| 6521 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 6522 |
const gchar *group_id, /* unused, always NULL */ |
| 6523 |
const gchar *task_list_id, |
| 6524 |
const GSList *task_ids, /* const gchar * */ |
| 6525 |
const gchar *prefer_outlook_timezone, /* nullable - then UTC, otherwise that zone for the returned times */ |
| 6526 |
const gchar *select, /* nullable - properties to select */ |
| 6527 |
GSList **out_tasks, /* EM365Task *, in the same order as task_ids; can return partial list */ |
| 6528 |
GCancellable *cancellable, |
| 6529 |
GError **error) |
| 6530 |
{ |
| 6531 |
gboolean success = TRUE; |
| 6532 |
|
| 6533 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 6534 |
g_return_val_if_fail (task_list_id != NULL, FALSE); |
| 6535 |
g_return_val_if_fail (task_ids != NULL, FALSE); |
| 6536 |
g_return_val_if_fail (out_tasks != NULL, FALSE); |
| 6537 |
|
| 6538 |
if (g_slist_next (task_ids)) { |
| 6539 |
GPtrArray *requests; |
| 6540 |
GSList *link; |
| 6541 |
guint total, done = 0; |
| 6542 |
|
| 6543 |
total = g_slist_length ((GSList *) task_ids); |
| 6544 |
requests = g_ptr_array_new_full (MIN (E_M365_BATCH_MAX_REQUESTS, MIN (total, 50)), g_object_unref); |
| 6545 |
|
| 6546 |
for (link = (GSList *) task_ids; link && success; link = g_slist_next (link)) { |
| 6547 |
const gchar *id = link->data; |
| 6548 |
SoupMessage *message; |
| 6549 |
|
| 6550 |
message = e_m365_connection_prepare_get_task (cnc, user_override, group_id, task_list_id, id, prefer_outlook_timezone, select, error); |
| 6551 |
|
| 6552 |
if (!message) { |
| 6553 |
success = FALSE; |
| 6554 |
break; |
| 6555 |
} |
| 6556 |
|
| 6557 |
g_ptr_array_add (requests, message); |
| 6558 |
|
| 6559 |
if (requests->len == E_M365_BATCH_MAX_REQUESTS || !link->next) { |
| 6560 |
if (requests->len == 1) { |
| 6561 |
EM365Task *task = NULL; |
| 6562 |
|
| 6563 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, &task, cancellable, error); |
| 6564 |
|
| 6565 |
if (success) |
| 6566 |
*out_tasks = g_slist_prepend (*out_tasks, task); |
| 6567 |
} else { |
| 6568 |
success = e_m365_connection_batch_request_sync (cnc, E_M365_API_V1_0, requests, cancellable, error); |
| 6569 |
|
| 6570 |
if (success) { |
| 6571 |
guint ii; |
| 6572 |
|
| 6573 |
for (ii = 0; ii < requests->len && success; ii++) { |
| 6574 |
JsonNode *node = NULL; |
| 6575 |
|
| 6576 |
message = requests->pdata[ii]; |
| 6577 |
success = e_m365_connection_json_node_from_message (message, NULL, &node, cancellable, error); |
| 6578 |
|
| 6579 |
if (success && node && JSON_NODE_HOLDS_OBJECT (node)) { |
| 6580 |
JsonObject *response; |
| 6581 |
|
| 6582 |
response = json_node_get_object (node); |
| 6583 |
|
| 6584 |
if (response) { |
| 6585 |
*out_tasks = g_slist_prepend (*out_tasks, json_object_ref (response)); |
| 6586 |
} else { |
| 6587 |
success = FALSE; |
| 6588 |
} |
| 6589 |
} else { |
| 6590 |
success = FALSE; |
| 6591 |
} |
| 6592 |
|
| 6593 |
if (node) |
| 6594 |
json_node_unref (node); |
| 6595 |
} |
| 6596 |
} |
| 6597 |
} |
| 6598 |
|
| 6599 |
g_ptr_array_remove_range (requests, 0, requests->len); |
| 6600 |
|
| 6601 |
done += requests->len; |
| 6602 |
|
| 6603 |
camel_operation_progress (cancellable, done * 100.0 / total); |
| 6604 |
} |
| 6605 |
} |
| 6606 |
|
| 6607 |
g_ptr_array_free (requests, TRUE); |
| 6608 |
} else { |
| 6609 |
SoupMessage *message; |
| 6610 |
|
| 6611 |
message = e_m365_connection_prepare_get_task (cnc, user_override, group_id, task_list_id, task_ids->data, prefer_outlook_timezone, select, error); |
| 6612 |
|
| 6613 |
if (message) { |
| 6614 |
EM365Task *task = NULL; |
| 6615 |
|
| 6616 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, &task, cancellable, error); |
| 6617 |
|
| 6618 |
if (success) |
| 6619 |
*out_tasks = g_slist_prepend (*out_tasks, task); |
| 6620 |
|
| 6621 |
g_clear_object (&message); |
| 6622 |
} else { |
| 6623 |
success = FALSE; |
| 6624 |
} |
| 6625 |
} |
| 6626 |
|
| 6627 |
*out_tasks = g_slist_reverse (*out_tasks); |
| 6628 |
|
| 6629 |
return success; |
| 6630 |
} |
| 6631 |
|
| 6632 |
/* https://learn.microsoft.com/en-us/graph/api/todotask-update?view=graph-rest-1.0&tabs=http */ |
| 6633 |
|
| 6634 |
gboolean |
| 6635 |
e_m365_connection_update_task_sync (EM365Connection *cnc, |
| 6636 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 6637 |
const gchar *group_id, /* unused, always NULL */ |
| 6638 |
const gchar *task_list_id, |
| 6639 |
const gchar *task_id, |
| 6640 |
JsonBuilder *task, |
| 6641 |
GCancellable *cancellable, |
| 6642 |
GError **error) |
| 6643 |
{ |
| 6644 |
SoupMessage *message; |
| 6645 |
gboolean success; |
| 6646 |
gchar *uri; |
| 6647 |
|
| 6648 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 6649 |
g_return_val_if_fail (task_list_id != NULL, FALSE); |
| 6650 |
g_return_val_if_fail (task_id != NULL, FALSE); |
| 6651 |
g_return_val_if_fail (task != NULL, FALSE); |
| 6652 |
|
| 6653 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 6654 |
"todo", |
| 6655 |
"lists", |
| 6656 |
task_list_id, |
| 6657 |
"", "tasks", |
| 6658 |
"", task_id, |
| 6659 |
NULL); |
| 6660 |
|
| 6661 |
message = m365_connection_new_soup_message ("PATCH", uri, CSM_DISABLE_RESPONSE, error); |
| 6662 |
|
| 6663 |
if (!message) { |
| 6664 |
g_free (uri); |
| 6665 |
|
| 6666 |
return FALSE; |
| 6667 |
} |
| 6668 |
|
| 6669 |
g_free (uri); |
| 6670 |
|
| 6671 |
e_m365_connection_set_json_body (message, task); |
| 6672 |
|
| 6673 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_no_response_cb, NULL, cancellable, error); |
| 6674 |
|
| 6675 |
g_clear_object (&message); |
| 6676 |
|
| 6677 |
return success; |
| 6678 |
} |
| 6679 |
|
| 6680 |
/* https://learn.microsoft.com/en-us/graph/api/todotask-delete?view=graph-rest-1.0&tabs=http */ |
| 6681 |
|
| 6682 |
gboolean |
| 6683 |
e_m365_connection_delete_task_sync (EM365Connection *cnc, |
| 6684 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 6685 |
const gchar *group_id, /* unused, always NULL */ |
| 6686 |
const gchar *task_list_id, |
| 6687 |
const gchar *task_id, |
| 6688 |
GCancellable *cancellable, |
| 6689 |
GError **error) |
| 6690 |
{ |
| 6691 |
SoupMessage *message; |
| 6692 |
gboolean success; |
| 6693 |
gchar *uri; |
| 6694 |
|
| 6695 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 6696 |
g_return_val_if_fail (task_list_id != NULL, FALSE); |
| 6697 |
g_return_val_if_fail (task_id != NULL, FALSE); |
| 6698 |
|
| 6699 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 6700 |
"todo", |
| 6701 |
"lists", |
| 6702 |
task_list_id, |
| 6703 |
"", "tasks", |
| 6704 |
"", task_id, |
| 6705 |
NULL); |
| 6706 |
|
| 6707 |
message = m365_connection_new_soup_message (SOUP_METHOD_DELETE, uri, CSM_DEFAULT, error); |
| 6708 |
|
| 6709 |
if (!message) { |
| 6710 |
g_free (uri); |
| 6711 |
|
| 6712 |
return FALSE; |
| 6713 |
} |
| 6714 |
|
| 6715 |
g_free (uri); |
| 6716 |
|
| 6717 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_no_response_cb, NULL, cancellable, error); |
| 6718 |
|
| 6719 |
g_clear_object (&message); |
| 6720 |
|
| 6721 |
return success; |
| 6722 |
} |
| 6723 |
|
| 6724 |
/* https://learn.microsoft.com/en-us/graph/api/todotask-delta?view=graph-rest-1.0 */ |
| 6725 |
|
| 6726 |
gboolean |
| 6727 |
e_m365_connection_get_tasks_delta_sync (EM365Connection *cnc, |
| 6728 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 6729 |
const gchar *task_list_id, |
| 6730 |
const gchar *delta_link, /* previous delta link */ |
| 6731 |
guint max_page_size, /* 0 for default by the server */ |
| 6732 |
EM365ConnectionJsonFunc func, /* function to call with each result set */ |
| 6733 |
gpointer func_user_data, /* user data passed into the 'func' */ |
| 6734 |
gchar **out_delta_link, |
| 6735 |
GCancellable *cancellable, |
| 6736 |
GError **error) |
| 6737 |
{ |
| 6738 |
EM365ResponseData rd; |
| 6739 |
SoupMessage *message = NULL; |
| 6740 |
gboolean success; |
| 6741 |
|
| 6742 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 6743 |
g_return_val_if_fail (task_list_id != NULL, FALSE); |
| 6744 |
g_return_val_if_fail (out_delta_link != NULL, FALSE); |
| 6745 |
g_return_val_if_fail (func != NULL, FALSE); |
| 6746 |
|
| 6747 |
if (delta_link) { |
| 6748 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, delta_link, CSM_DEFAULT, NULL); |
| 6749 |
} else { |
| 6750 |
gchar *uri; |
| 6751 |
|
| 6752 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 6753 |
"todo", |
| 6754 |
"lists", |
| 6755 |
"task_list_id", |
| 6756 |
"", "tasks", |
| 6757 |
"", "delta", |
| 6758 |
NULL); |
| 6759 |
|
| 6760 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 6761 |
|
| 6762 |
if (!message) { |
| 6763 |
g_free (uri); |
| 6764 |
|
| 6765 |
return FALSE; |
| 6766 |
} |
| 6767 |
|
| 6768 |
g_free (uri); |
| 6769 |
} |
| 6770 |
|
| 6771 |
if (max_page_size > 0) { |
| 6772 |
gchar *prefer_value; |
| 6773 |
|
| 6774 |
prefer_value = g_strdup_printf ("odata.maxpagesize=%u", max_page_size); |
| 6775 |
|
| 6776 |
soup_message_headers_append (soup_message_get_request_headers (message), "Prefer", prefer_value); |
| 6777 |
|
| 6778 |
g_free (prefer_value); |
| 6779 |
} |
| 6780 |
|
| 6781 |
memset (&rd, 0, sizeof (EM365ResponseData)); |
| 6782 |
|
| 6783 |
rd.json_func = func; |
| 6784 |
rd.func_user_data = func_user_data; |
| 6785 |
rd.out_delta_link = out_delta_link; |
| 6786 |
|
| 6787 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_valued_response_cb, NULL, &rd, cancellable, error); |
| 6788 |
|
| 6789 |
g_clear_object (&message); |
| 6790 |
|
| 6791 |
return success; |
| 6792 |
} |
| 6793 |
|
| 6794 |
/* https://learn.microsoft.com/en-us/graph/api/todotask-list-checklistitems?view=graph-rest-1.0&tabs=http */ |
| 6795 |
|
| 6796 |
gboolean |
| 6797 |
e_m365_connection_list_checklist_items_sync (EM365Connection *cnc, |
| 6798 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 6799 |
const gchar *task_list_id, |
| 6800 |
const gchar *task_id, |
| 6801 |
const gchar *select, /* nullable - properties to select */ |
| 6802 |
GSList **out_items, /* EM365ChecklistItem * */ |
| 6803 |
GCancellable *cancellable, |
| 6804 |
GError **error) |
| 6805 |
{ |
| 6806 |
EM365ResponseData rd; |
| 6807 |
SoupMessage *message; |
| 6808 |
gchar *uri; |
| 6809 |
gboolean success; |
| 6810 |
|
| 6811 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 6812 |
g_return_val_if_fail (task_list_id != NULL, FALSE); |
| 6813 |
g_return_val_if_fail (task_id != NULL, FALSE); |
| 6814 |
g_return_val_if_fail (out_items != NULL, FALSE); |
| 6815 |
|
| 6816 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 6817 |
"todo", |
| 6818 |
"lists", |
| 6819 |
task_list_id, |
| 6820 |
"", "tasks", |
| 6821 |
"", task_id, |
| 6822 |
"", "checklistItems", |
| 6823 |
"$select", select, |
| 6824 |
NULL); |
| 6825 |
|
| 6826 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 6827 |
|
| 6828 |
if (!message) { |
| 6829 |
g_free (uri); |
| 6830 |
|
| 6831 |
return FALSE; |
| 6832 |
} |
| 6833 |
|
| 6834 |
g_free (uri); |
| 6835 |
|
| 6836 |
memset (&rd, 0, sizeof (EM365ResponseData)); |
| 6837 |
|
| 6838 |
rd.out_items = out_items; |
| 6839 |
|
| 6840 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_valued_response_cb, NULL, &rd, cancellable, error); |
| 6841 |
|
| 6842 |
g_clear_object (&message); |
| 6843 |
|
| 6844 |
return success; |
| 6845 |
} |
| 6846 |
|
| 6847 |
/* https://learn.microsoft.com/en-us/graph/api/checklistitem-get?view=graph-rest-1.0&tabs=http */ |
| 6848 |
|
| 6849 |
gboolean |
| 6850 |
e_m365_connection_get_checklist_item_sync (EM365Connection *cnc, |
| 6851 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 6852 |
const gchar *task_list_id, |
| 6853 |
const gchar *task_id, |
| 6854 |
const gchar *item_id, |
| 6855 |
EM365ChecklistItem **out_item, /* nullable */ |
| 6856 |
GCancellable *cancellable, |
| 6857 |
GError **error) |
| 6858 |
{ |
| 6859 |
SoupMessage *message; |
| 6860 |
gchar *uri; |
| 6861 |
gboolean success; |
| 6862 |
|
| 6863 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 6864 |
g_return_val_if_fail (task_list_id != NULL, FALSE); |
| 6865 |
g_return_val_if_fail (task_id != NULL, FALSE); |
| 6866 |
g_return_val_if_fail (item_id != NULL, FALSE); |
| 6867 |
g_return_val_if_fail (out_item != NULL, FALSE); |
| 6868 |
|
| 6869 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 6870 |
"todo", |
| 6871 |
"lists", |
| 6872 |
task_list_id, |
| 6873 |
"", "tasks", |
| 6874 |
"", task_id, |
| 6875 |
"", "checklistItems", |
| 6876 |
"", item_id, |
| 6877 |
NULL); |
| 6878 |
|
| 6879 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 6880 |
|
| 6881 |
if (!message) { |
| 6882 |
g_free (uri); |
| 6883 |
|
| 6884 |
return FALSE; |
| 6885 |
} |
| 6886 |
|
| 6887 |
g_free (uri); |
| 6888 |
|
| 6889 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, out_item, cancellable, error); |
| 6890 |
|
| 6891 |
g_clear_object (&message); |
| 6892 |
|
| 6893 |
return success; |
| 6894 |
} |
| 6895 |
|
| 6896 |
/* https://learn.microsoft.com/en-us/graph/api/todotask-post-checklistitems?view=graph-rest-1.0&tabs=http */ |
| 6897 |
|
| 6898 |
gboolean |
| 6899 |
e_m365_connection_create_checklist_item_sync (EM365Connection *cnc, |
| 6900 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 6901 |
const gchar *task_list_id, |
| 6902 |
const gchar *task_id, |
| 6903 |
JsonBuilder *in_item, |
| 6904 |
EM365ChecklistItem **out_item, /* nullable */ |
| 6905 |
GCancellable *cancellable, |
| 6906 |
GError **error) |
| 6907 |
{ |
| 6908 |
SoupMessage *message; |
| 6909 |
gboolean success; |
| 6910 |
gchar *uri; |
| 6911 |
|
| 6912 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 6913 |
g_return_val_if_fail (task_list_id != NULL, FALSE); |
| 6914 |
g_return_val_if_fail (task_id != NULL, FALSE); |
| 6915 |
g_return_val_if_fail (in_item != NULL, FALSE); |
| 6916 |
|
| 6917 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 6918 |
"todo", |
| 6919 |
"lists", |
| 6920 |
task_list_id, |
| 6921 |
"", "tasks", |
| 6922 |
"", task_id, |
| 6923 |
"", "checklistItems", |
| 6924 |
NULL); |
| 6925 |
|
| 6926 |
message = m365_connection_new_soup_message (SOUP_METHOD_POST, uri, out_item ? CSM_DEFAULT : CSM_DISABLE_RESPONSE, error); |
| 6927 |
|
| 6928 |
if (!message) { |
| 6929 |
g_free (uri); |
| 6930 |
|
| 6931 |
return FALSE; |
| 6932 |
} |
| 6933 |
|
| 6934 |
g_free (uri); |
| 6935 |
|
| 6936 |
e_m365_connection_set_json_body (message, in_item); |
| 6937 |
|
| 6938 |
success = m365_connection_send_request_sync (cnc, message, out_item ? e_m365_read_json_object_response_cb : NULL, |
| 6939 |
out_item ? NULL : e_m365_read_no_response_cb, out_item, cancellable, error); |
| 6940 |
|
| 6941 |
g_clear_object (&message); |
| 6942 |
|
| 6943 |
return success; |
| 6944 |
} |
| 6945 |
|
| 6946 |
/* https://learn.microsoft.com/en-us/graph/api/checklistitem-update?view=graph-rest-1.0&tabs=http */ |
| 6947 |
|
| 6948 |
gboolean |
| 6949 |
e_m365_connection_update_checklist_item_sync (EM365Connection *cnc, |
| 6950 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 6951 |
const gchar *task_list_id, |
| 6952 |
const gchar *task_id, |
| 6953 |
const gchar *item_id, |
| 6954 |
JsonBuilder *in_item, |
| 6955 |
GCancellable *cancellable, |
| 6956 |
GError **error) |
| 6957 |
{ |
| 6958 |
SoupMessage *message; |
| 6959 |
gboolean success; |
| 6960 |
gchar *uri; |
| 6961 |
|
| 6962 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 6963 |
g_return_val_if_fail (task_list_id != NULL, FALSE); |
| 6964 |
g_return_val_if_fail (task_id != NULL, FALSE); |
| 6965 |
g_return_val_if_fail (item_id != NULL, FALSE); |
| 6966 |
g_return_val_if_fail (in_item != NULL, FALSE); |
| 6967 |
|
| 6968 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 6969 |
"todo", |
| 6970 |
"lists", |
| 6971 |
task_list_id, |
| 6972 |
"", "tasks", |
| 6973 |
"", task_id, |
| 6974 |
"", "checklistItems", |
| 6975 |
"", item_id, |
| 6976 |
NULL); |
| 6977 |
|
| 6978 |
message = m365_connection_new_soup_message ("PATCH", uri, CSM_DISABLE_RESPONSE, error); |
| 6979 |
|
| 6980 |
if (!message) { |
| 6981 |
g_free (uri); |
| 6982 |
|
| 6983 |
return FALSE; |
| 6984 |
} |
| 6985 |
|
| 6986 |
g_free (uri); |
| 6987 |
|
| 6988 |
e_m365_connection_set_json_body (message, in_item); |
| 6989 |
|
| 6990 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_no_response_cb, NULL, cancellable, error); |
| 6991 |
|
| 6992 |
g_clear_object (&message); |
| 6993 |
|
| 6994 |
return success; |
| 6995 |
} |
| 6996 |
|
| 6997 |
/* https://learn.microsoft.com/en-us/graph/api/checklistitem-delete?view=graph-rest-1.0&tabs=http */ |
| 6998 |
|
| 6999 |
gboolean |
| 7000 |
e_m365_connection_delete_checklist_item_sync (EM365Connection *cnc, |
| 7001 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 7002 |
const gchar *task_list_id, |
| 7003 |
const gchar *task_id, |
| 7004 |
const gchar *item_id, |
| 7005 |
GCancellable *cancellable, |
| 7006 |
GError **error) |
| 7007 |
{ |
| 7008 |
SoupMessage *message; |
| 7009 |
gboolean success; |
| 7010 |
gchar *uri; |
| 7011 |
|
| 7012 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 7013 |
g_return_val_if_fail (task_list_id != NULL, FALSE); |
| 7014 |
g_return_val_if_fail (task_id != NULL, FALSE); |
| 7015 |
g_return_val_if_fail (item_id != NULL, FALSE); |
| 7016 |
|
| 7017 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 7018 |
"todo", |
| 7019 |
"lists", |
| 7020 |
task_list_id, |
| 7021 |
"", "tasks", |
| 7022 |
"", task_id, |
| 7023 |
"", "checklistItems", |
| 7024 |
"", item_id, |
| 7025 |
NULL); |
| 7026 |
|
| 7027 |
message = m365_connection_new_soup_message (SOUP_METHOD_DELETE, uri, CSM_DEFAULT, error); |
| 7028 |
|
| 7029 |
if (!message) { |
| 7030 |
g_free (uri); |
| 7031 |
|
| 7032 |
return FALSE; |
| 7033 |
} |
| 7034 |
|
| 7035 |
g_free (uri); |
| 7036 |
|
| 7037 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_no_response_cb, NULL, cancellable, error); |
| 7038 |
|
| 7039 |
g_clear_object (&message); |
| 7040 |
|
| 7041 |
return success; |
| 7042 |
} |
| 7043 |
|
| 7044 |
/* https://learn.microsoft.com/en-us/graph/api/todotask-list-linkedresources?view=graph-rest-1.0&tabs=http */ |
| 7045 |
|
| 7046 |
gboolean |
| 7047 |
e_m365_connection_list_linked_resources_sync (EM365Connection *cnc, |
| 7048 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 7049 |
const gchar *task_list_id, |
| 7050 |
const gchar *task_id, |
| 7051 |
const gchar *select, /* nullable - properties to select */ |
| 7052 |
GSList **out_resources, /* EM365LinkedResource * */ |
| 7053 |
GCancellable *cancellable, |
| 7054 |
GError **error) |
| 7055 |
{ |
| 7056 |
EM365ResponseData rd; |
| 7057 |
SoupMessage *message; |
| 7058 |
gchar *uri; |
| 7059 |
gboolean success; |
| 7060 |
|
| 7061 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 7062 |
g_return_val_if_fail (task_list_id != NULL, FALSE); |
| 7063 |
g_return_val_if_fail (task_id != NULL, FALSE); |
| 7064 |
g_return_val_if_fail (out_resources != NULL, FALSE); |
| 7065 |
|
| 7066 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 7067 |
"todo", |
| 7068 |
"lists", |
| 7069 |
task_list_id, |
| 7070 |
"", "tasks", |
| 7071 |
"", task_id, |
| 7072 |
"", "linkedResources", |
| 7073 |
"$select", select, |
| 7074 |
NULL); |
| 7075 |
|
| 7076 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 7077 |
|
| 7078 |
if (!message) { |
| 7079 |
g_free (uri); |
| 7080 |
|
| 7081 |
return FALSE; |
| 7082 |
} |
| 7083 |
|
| 7084 |
g_free (uri); |
| 7085 |
|
| 7086 |
memset (&rd, 0, sizeof (EM365ResponseData)); |
| 7087 |
|
| 7088 |
rd.out_items = out_resources; |
| 7089 |
|
| 7090 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_valued_response_cb, NULL, &rd, cancellable, error); |
| 7091 |
|
| 7092 |
g_clear_object (&message); |
| 7093 |
|
| 7094 |
return success; |
| 7095 |
} |
| 7096 |
|
| 7097 |
/* https://learn.microsoft.com/en-us/graph/api/linkedresource-get?view=graph-rest-1.0&tabs=http */ |
| 7098 |
|
| 7099 |
gboolean |
| 7100 |
e_m365_connection_get_linked_resource_sync (EM365Connection *cnc, |
| 7101 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 7102 |
const gchar *task_list_id, |
| 7103 |
const gchar *task_id, |
| 7104 |
const gchar *resource_id, |
| 7105 |
EM365LinkedResource **out_resource, /* nullable */ |
| 7106 |
GCancellable *cancellable, |
| 7107 |
GError **error) |
| 7108 |
{ |
| 7109 |
SoupMessage *message; |
| 7110 |
gchar *uri; |
| 7111 |
gboolean success; |
| 7112 |
|
| 7113 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 7114 |
g_return_val_if_fail (task_list_id != NULL, FALSE); |
| 7115 |
g_return_val_if_fail (task_id != NULL, FALSE); |
| 7116 |
g_return_val_if_fail (resource_id != NULL, FALSE); |
| 7117 |
g_return_val_if_fail (out_resource != NULL, FALSE); |
| 7118 |
|
| 7119 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 7120 |
"todo", |
| 7121 |
"lists", |
| 7122 |
task_list_id, |
| 7123 |
"", "tasks", |
| 7124 |
"", task_id, |
| 7125 |
"", "linkedResources", |
| 7126 |
"", resource_id, |
| 7127 |
NULL); |
| 7128 |
|
| 7129 |
message = m365_connection_new_soup_message (SOUP_METHOD_GET, uri, CSM_DEFAULT, error); |
| 7130 |
|
| 7131 |
if (!message) { |
| 7132 |
g_free (uri); |
| 7133 |
|
| 7134 |
return FALSE; |
| 7135 |
} |
| 7136 |
|
| 7137 |
g_free (uri); |
| 7138 |
|
| 7139 |
success = m365_connection_send_request_sync (cnc, message, e_m365_read_json_object_response_cb, NULL, out_resource, cancellable, error); |
| 7140 |
|
| 7141 |
g_clear_object (&message); |
| 7142 |
|
| 7143 |
return success; |
| 7144 |
} |
| 7145 |
|
| 7146 |
/* https://learn.microsoft.com/en-us/graph/api/todotask-post-linkedresources?view=graph-rest-1.0&tabs=http */ |
| 7147 |
|
| 7148 |
gboolean |
| 7149 |
e_m365_connection_create_linked_resource_sync (EM365Connection *cnc, |
| 7150 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 7151 |
const gchar *task_list_id, |
| 7152 |
const gchar *task_id, |
| 7153 |
JsonBuilder *in_resource, |
| 7154 |
EM365LinkedResource **out_resource, /* nullable */ |
| 7155 |
GCancellable *cancellable, |
| 7156 |
GError **error) |
| 7157 |
{ |
| 7158 |
SoupMessage *message; |
| 7159 |
gboolean success; |
| 7160 |
gchar *uri; |
| 7161 |
|
| 7162 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 7163 |
g_return_val_if_fail (task_list_id != NULL, FALSE); |
| 7164 |
g_return_val_if_fail (task_id != NULL, FALSE); |
| 7165 |
g_return_val_if_fail (in_resource != NULL, FALSE); |
| 7166 |
|
| 7167 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 7168 |
"todo", |
| 7169 |
"lists", |
| 7170 |
task_list_id, |
| 7171 |
"", "tasks", |
| 7172 |
"", task_id, |
| 7173 |
"", "linkedResources", |
| 7174 |
NULL); |
| 7175 |
|
| 7176 |
message = m365_connection_new_soup_message (SOUP_METHOD_POST, uri, out_resource ? CSM_DEFAULT : CSM_DISABLE_RESPONSE, error); |
| 7177 |
|
| 7178 |
if (!message) { |
| 7179 |
g_free (uri); |
| 7180 |
|
| 7181 |
return FALSE; |
| 7182 |
} |
| 7183 |
|
| 7184 |
g_free (uri); |
| 7185 |
|
| 7186 |
e_m365_connection_set_json_body (message, in_resource); |
| 7187 |
|
| 7188 |
success = m365_connection_send_request_sync (cnc, message, out_resource ? e_m365_read_json_object_response_cb : NULL, |
| 7189 |
out_resource ? NULL : e_m365_read_no_response_cb, out_resource, cancellable, error); |
| 7190 |
|
| 7191 |
g_clear_object (&message); |
| 7192 |
|
| 7193 |
return success; |
| 7194 |
} |
| 7195 |
|
| 7196 |
/* https://learn.microsoft.com/en-us/graph/api/linkedresource-update?view=graph-rest-1.0&tabs=http */ |
| 7197 |
|
| 7198 |
gboolean |
| 7199 |
e_m365_connection_update_linked_resource_sync (EM365Connection *cnc, |
| 7200 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 7201 |
const gchar *task_list_id, |
| 7202 |
const gchar *task_id, |
| 7203 |
const gchar *resource_id, |
| 7204 |
JsonBuilder *in_resource, |
| 7205 |
GCancellable *cancellable, |
| 7206 |
GError **error) |
| 7207 |
{ |
| 7208 |
SoupMessage *message; |
| 7209 |
gboolean success; |
| 7210 |
gchar *uri; |
| 7211 |
|
| 7212 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 7213 |
g_return_val_if_fail (task_list_id != NULL, FALSE); |
| 7214 |
g_return_val_if_fail (task_id != NULL, FALSE); |
| 7215 |
g_return_val_if_fail (resource_id != NULL, FALSE); |
| 7216 |
g_return_val_if_fail (in_resource != NULL, FALSE); |
| 7217 |
|
| 7218 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 7219 |
"todo", |
| 7220 |
"lists", |
| 7221 |
task_list_id, |
| 7222 |
"", "tasks", |
| 7223 |
"", task_id, |
| 7224 |
"", "linkedResources", |
| 7225 |
"", resource_id, |
| 7226 |
NULL); |
| 7227 |
|
| 7228 |
message = m365_connection_new_soup_message ("PATCH", uri, CSM_DISABLE_RESPONSE, error); |
| 7229 |
|
| 7230 |
if (!message) { |
| 7231 |
g_free (uri); |
| 7232 |
|
| 7233 |
return FALSE; |
| 7234 |
} |
| 7235 |
|
| 7236 |
g_free (uri); |
| 7237 |
|
| 7238 |
e_m365_connection_set_json_body (message, in_resource); |
| 7239 |
|
| 7240 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_no_response_cb, NULL, cancellable, error); |
| 7241 |
|
| 7242 |
g_clear_object (&message); |
| 7243 |
|
| 7244 |
return success; |
| 7245 |
} |
| 7246 |
|
| 7247 |
/* https://learn.microsoft.com/en-us/graph/api/linkedresource-delete?view=graph-rest-1.0&tabs=http */ |
| 7248 |
|
| 7249 |
gboolean |
| 7250 |
e_m365_connection_delete_linked_resource_sync (EM365Connection *cnc, |
| 7251 |
const gchar *user_override, /* for which user, NULL to use the account user */ |
| 7252 |
const gchar *task_list_id, |
| 7253 |
const gchar *task_id, |
| 7254 |
const gchar *resource_id, |
| 7255 |
GCancellable *cancellable, |
| 7256 |
GError **error) |
| 7257 |
{ |
| 7258 |
SoupMessage *message; |
| 7259 |
gboolean success; |
| 7260 |
gchar *uri; |
| 7261 |
|
| 7262 |
g_return_val_if_fail (E_IS_M365_CONNECTION (cnc), FALSE); |
| 7263 |
g_return_val_if_fail (task_list_id != NULL, FALSE); |
| 7264 |
g_return_val_if_fail (task_id != NULL, FALSE); |
| 7265 |
g_return_val_if_fail (resource_id != NULL, FALSE); |
| 7266 |
|
| 7267 |
uri = e_m365_connection_construct_uri (cnc, TRUE, user_override, E_M365_API_V1_0, NULL, |
| 7268 |
"todo", |
| 7269 |
"lists", |
| 7270 |
task_list_id, |
| 7271 |
"", "tasks", |
| 7272 |
"", task_id, |
| 7273 |
"", "linkedResources", |
| 7274 |
"", resource_id, |
| 7275 |
NULL); |
| 7276 |
|
| 7277 |
message = m365_connection_new_soup_message (SOUP_METHOD_DELETE, uri, CSM_DEFAULT, error); |
| 7278 |
|
| 7279 |
if (!message) { |
| 7280 |
g_free (uri); |
| 7281 |
|
| 7282 |
return FALSE; |
| 7283 |
} |
| 7284 |
|
| 7285 |
g_free (uri); |
| 7286 |
|
| 7287 |
success = m365_connection_send_request_sync (cnc, message, NULL, e_m365_read_no_response_cb, NULL, cancellable, error); |
| 7288 |
|
| 7289 |
g_clear_object (&message); |
| 7290 |
|
| 7291 |
return success; |
| 7292 |
} |