Debian Codegraph @

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

raw
1 /*
2 * SPDX-FileCopyrightText: (C) 1999-2008 Novell, Inc. (www.novell.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 <camel/camel.h>
10 #include <camel/camel-search-private.h>
11
12 #include "e-ews-common-utils.h"
13 #include "common/e-ews-query-to-restriction.h"
14
15 #include "camel-ews-folder.h"
16 #include "camel-ews-search.h"
17
18 struct _CamelEwsSearchPrivate {
19 GWeakRef ews_store;
20 gint *local_data_search; /* not NULL, if testing whether all used headers are all locally available */
21
22 GHashTable *cached_results; /* gchar * (search description) ~> GHashTable { gchar * (uid {from string pool}) ~> NULL } */
23 GCancellable *cancellable; /* not referenced */
24 GError **error; /* not referenced */
25 };
26
27 enum {
28 PROP_0,
29 PROP_STORE
30 };
31
32 G_DEFINE_TYPE_WITH_PRIVATE (CamelEwsSearch, camel_ews_search, CAMEL_TYPE_FOLDER_SEARCH)
33
34 static void
35 ews_search_set_property (GObject *object,
36 guint property_id,
37 const GValue *value,
38 GParamSpec *pspec)
39 {
40 switch (property_id) {
41 case PROP_STORE:
42 camel_ews_search_set_store (
43 CAMEL_EWS_SEARCH (object),
44 g_value_get_object (value));
45 return;
46 }
47
48 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
49 }
50
51 static void
52 ews_search_get_property (GObject *object,
53 guint property_id,
54 GValue *value,
55 GParamSpec *pspec)
56 {
57 switch (property_id) {
58 case PROP_STORE:
59 g_value_take_object (
60 value,
61 camel_ews_search_ref_store (
62 CAMEL_EWS_SEARCH (object)));
63 return;
64 }
65
66 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
67 }
68
69 static void
70 ews_search_dispose (GObject *object)
71 {
72 CamelEwsSearch *ews_search = CAMEL_EWS_SEARCH (object);
73
74 g_weak_ref_set (&ews_search->priv->ews_store, NULL);
75
76 /* Chain up to parent's dispose() method. */
77 G_OBJECT_CLASS (camel_ews_search_parent_class)->dispose (object);
78 }
79
80 static void
81 ews_search_finalize (GObject *object)
82 {
83 CamelEwsSearch *ews_search = CAMEL_EWS_SEARCH (object);
84
85 g_weak_ref_clear (&ews_search->priv->ews_store);
86 g_hash_table_destroy (ews_search->priv->cached_results);
87
88 /* Chain up to parent's finalize() method. */
89 G_OBJECT_CLASS (camel_ews_search_parent_class)->finalize (object);
90 }
91
92 static CamelSExpResult *
93 ews_search_result_match_all (CamelSExp *sexp,
94 CamelFolderSearch *search)
95 {
96 CamelSExpResult *result;
97
98 g_return_val_if_fail (search != NULL, NULL);
99
100 if (camel_folder_search_get_current_message_info (search)) {
101 result = camel_sexp_result_new (sexp, CAMEL_SEXP_RES_BOOL);
102 result->value.boolean = TRUE;
103 } else {
104 GPtrArray *summary;
105 gint ii;
106
107 summary = camel_folder_search_get_summary (search);
108
109 result = camel_sexp_result_new (sexp, CAMEL_SEXP_RES_ARRAY_PTR);
110 result->value.ptrarray = g_ptr_array_new ();
111
112 for (ii = 0; summary && ii < summary->len; ii++) {
113 g_ptr_array_add (
114 result->value.ptrarray,
115 (gpointer) summary->pdata[ii]);
116 }
117 }
118
119 return result;
120 }
121
122 static CamelSExpResult *
123 ews_search_result_match_none (CamelSExp *sexp,
124 CamelFolderSearch *search)
125 {
126 CamelSExpResult *result;
127
128 g_return_val_if_fail (search != NULL, NULL);
129
130 if (camel_folder_search_get_current_message_info (search)) {
131 result = camel_sexp_result_new (sexp, CAMEL_SEXP_RES_BOOL);
132 result->value.boolean = FALSE;
133 } else {
134 result = camel_sexp_result_new (sexp, CAMEL_SEXP_RES_ARRAY_PTR);
135 result->value.ptrarray = g_ptr_array_new ();
136 }
137
138 return result;
139 }
140
141 static GPtrArray *
142 ews_search_items_to_ptr_array (const GSList *items)
143 {
144 GPtrArray *matches = NULL;
145 const GSList *link;
146
147 for (link = items; link; link = g_slist_next (link)) {
148 EEwsItem *item = link->data;
149 const EwsId *id;
150
151 if (!item || e_ews_item_get_item_type (item) == E_EWS_ITEM_TYPE_ERROR)
152 continue;
153
154 id = e_ews_item_get_id (item);
155 if (!id || !id->id)
156 continue;
157
158 if (!matches)
159 matches = g_ptr_array_new ();
160
161 g_ptr_array_add (matches, (gpointer) camel_pstring_strdup (id->id));
162 }
163
164 return matches;
165 }
166
167 static gchar *
168 ews_search_describe_criteria (const GPtrArray *words)
169 {
170 GString *desc;
171
172 desc = g_string_sized_new (64);
173
174 if (words && words->len) {
175 guint ii;
176
177 for (ii = 0; ii < words->len; ii++) {
178 const gchar *word = words->pdata[ii];
179
180 if (word) {
181 g_string_append (desc, word);
182 g_string_append_c (desc, '\n');
183 }
184 }
185 }
186
187 g_string_append_c (desc, '\n');
188
189 return g_string_free (desc, FALSE);
190 }
191
192 static CamelSExpResult *
193 ews_search_process_criteria (CamelSExp *sexp,
194 CamelFolderSearch *search,
195 CamelEwsStore *ews_store,
196 const GPtrArray *words)
197 {
198 CamelSExpResult *result;
199 CamelEwsSearch *ews_search = CAMEL_EWS_SEARCH (search);
200 CamelMessageInfo *info;
201 GHashTable *cached_results;
202 gchar *criteria_desc;
203 GPtrArray *uids = NULL;
204 GError *local_error = NULL;
205
206 criteria_desc = ews_search_describe_criteria (words);
207 cached_results = g_hash_table_lookup (ews_search->priv->cached_results, criteria_desc);
208
209 if (!cached_results) {
210 CamelEwsFolder *ews_folder;
211
212 ews_folder = CAMEL_EWS_FOLDER (camel_folder_search_get_folder (search));
213
214 /* Sanity check. */
215 g_return_val_if_fail (ews_folder != NULL, NULL);
216
217 if (ews_folder != NULL) {
218 EEwsConnection *connection = NULL;
219 gchar *folder_id = NULL;
220 gboolean can_search;
221
222 /* there should always be one, held by one of the callers of this function */
223 g_warn_if_fail (ews_store != NULL);
224
225 can_search = ews_store != NULL && words != NULL;
226
227 if (can_search) {
228 folder_id = camel_ews_store_summary_get_folder_id_from_name (ews_store->summary,
229 camel_folder_get_full_name (CAMEL_FOLDER (ews_folder)));
230 if (!folder_id)
231 can_search = FALSE;
232 }
233
234 if (can_search) {
235 connection = camel_ews_store_ref_connection (ews_store);
236 if (!connection)
237 can_search = FALSE;
238 }
239
240 if (can_search) {
241 EwsFolderId *fid;
242 GSList *found_items = NULL;
243 gboolean includes_last_item = FALSE;
244 GString *expression;
245 guint ii;
246
247 fid = e_ews_folder_id_new (folder_id, NULL, FALSE);
248 expression = g_string_new ("");
249
250 if (words->len >= 2)
251 g_string_append (expression, "(and ");
252
253 for (ii = 0; ii < words->len; ii++) {
254 GString *word;
255
256 word = e_ews_common_utils_str_replace_string (g_ptr_array_index (words, ii), "\"", "\\\"");
257
258 g_string_append (expression, "(body-contains \"");
259 g_string_append (expression, word->str);
260 g_string_append (expression, "\")");
261
262 g_string_free (word, TRUE);
263 }
264
265 /* Close the 'and' */
266 if (words->len >= 2)
267 g_string_append_c (expression, ')');
268
269 if (e_ews_connection_find_folder_items_sync (
270 connection, EWS_PRIORITY_MEDIUM,
271 fid, "IdOnly", NULL, NULL, expression->str, NULL,
272 E_EWS_FOLDER_TYPE_MAILBOX, &includes_last_item, &found_items,
273 e_ews_query_to_restriction,
274 ews_search->priv->cancellable, &local_error)) {
275
276 uids = ews_search_items_to_ptr_array (found_items);
277 }
278
279 g_slist_free_full (found_items, g_object_unref);
280 g_string_free (expression, TRUE);
281 e_ews_folder_id_free (fid);
282 }
283
284 g_clear_object (&connection);
285 g_free (folder_id);
286 }
287
288 if (local_error != NULL)
289 g_propagate_error (ews_search->priv->error, local_error);
290
291 if (!uids) {
292 /* Make like we've got an empty result */
293 uids = g_ptr_array_new ();
294 }
295 }
296
297 if (!cached_results) {
298 cached_results = g_hash_table_new_full (g_str_hash, g_str_equal, (GDestroyNotify) camel_pstring_free, NULL);
299
300 if (uids) {
301 guint ii;
302
303 for (ii = 0; ii < uids->len; ii++) {
304 g_hash_table_insert (cached_results, (gpointer) camel_pstring_strdup (uids->pdata[ii]), NULL);
305 }
306 }
307
308 g_hash_table_insert (ews_search->priv->cached_results, criteria_desc, cached_results);
309 } else {
310 g_free (criteria_desc);
311 }
312
313 info = camel_folder_search_get_current_message_info (search);
314
315 if (info) {
316 result = camel_sexp_result_new (sexp, CAMEL_SEXP_RES_BOOL);
317 result->value.boolean = g_hash_table_contains (cached_results, camel_message_info_get_uid (info));
318 } else {
319 if (!uids) {
320 GHashTableIter iter;
321 gpointer key;
322
323 uids = g_ptr_array_sized_new (g_hash_table_size (cached_results));
324
325 g_hash_table_iter_init (&iter, cached_results);
326
327 while (g_hash_table_iter_next (&iter, &key, NULL)) {
328 g_ptr_array_add (uids, (gpointer) camel_pstring_strdup (key));
329 }
330 }
331
332 result = camel_sexp_result_new (sexp, CAMEL_SEXP_RES_ARRAY_PTR);
333 result->value.ptrarray = g_ptr_array_ref (uids);
334 }
335
336 if (uids)
337 g_ptr_array_unref (uids);
338
339 return result;
340 }
341
342 static GPtrArray *
343 ews_search_gather_words (CamelSExpResult **argv,
344 gint from_index,
345 gint argc)
346 {
347 GPtrArray *ptrs;
348 GHashTable *words_hash;
349 GHashTableIter iter;
350 gpointer key, value;
351 gint ii, jj;
352
353 g_return_val_if_fail (argv != 0, NULL);
354
355 words_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
356
357 for (ii = from_index; ii < argc; ii++) {
358 struct _camel_search_words *words;
359
360 if (argv[ii]->type != CAMEL_SEXP_RES_STRING)
361 continue;
362
363 /* Handle multiple search words within a single term. */
364 words = camel_search_words_split ((const guchar *) argv[ii]->value.string);
365
366 for (jj = 0; jj < words->len; jj++) {
367 const gchar *word = words->words[jj]->word;
368
369 g_hash_table_insert (words_hash, g_strdup (word), NULL);
370 }
371
372 camel_search_words_free (words);
373 }
374
375 ptrs = g_ptr_array_new_full (g_hash_table_size (words_hash), g_free);
376
377 g_hash_table_iter_init (&iter, words_hash);
378 while (g_hash_table_iter_next (&iter, &key, &value)) {
379 g_ptr_array_add (ptrs, g_strdup (key));
380 }
381
382 if (ptrs->len == 0) {
383 g_ptr_array_free (ptrs, TRUE);
384 ptrs = NULL;
385 }
386
387 g_hash_table_destroy (words_hash);
388
389 return ptrs;
390 }
391
392 static CamelSExpResult *
393 ews_search_body_contains (CamelSExp *sexp,
394 gint argc,
395 CamelSExpResult **argv,
396 CamelFolderSearch *search)
397 {
398 CamelEwsSearch *ews_search = CAMEL_EWS_SEARCH (search);
399 CamelEwsStore *ews_store;
400 CamelSExpResult *result;
401 GPtrArray *words;
402
403 /* Always do body-search server-side */
404 if (ews_search->priv->local_data_search) {
405 *ews_search->priv->local_data_search = -1;
406 return ews_search_result_match_none (sexp, search);
407 }
408
409 /* Match everything if argv = [""] */
410 if (argc == 1 && argv[0]->value.string[0] == '\0')
411 return ews_search_result_match_all (sexp, search);
412
413 /* Match nothing if empty argv or empty summary. */
414 if (argc == 0 || camel_folder_search_get_summary_empty (search))
415 return ews_search_result_match_none (sexp, search);
416
417 ews_store = camel_ews_search_ref_store (CAMEL_EWS_SEARCH (search));
418
419 /* This will be NULL if we're offline. Search from cache. */
420 if (!ews_store) {
421 /* Chain up to parent's method. */
422 return CAMEL_FOLDER_SEARCH_CLASS (camel_ews_search_parent_class)->
423 body_contains (sexp, argc, argv, search);
424 }
425
426 words = ews_search_gather_words (argv, 0, argc);
427
428 result = ews_search_process_criteria (sexp, search, ews_store, words);
429
430 g_ptr_array_free (words, TRUE);
431 g_object_unref (ews_store);
432
433 return result;
434 }
435
436 static void
437 camel_ews_search_class_init (CamelEwsSearchClass *class)
438 {
439 GObjectClass *object_class;
440 CamelFolderSearchClass *search_class;
441
442 object_class = G_OBJECT_CLASS (class);
443 object_class->set_property = ews_search_set_property;
444 object_class->get_property = ews_search_get_property;
445 object_class->dispose = ews_search_dispose;
446 object_class->finalize = ews_search_finalize;
447
448 search_class = CAMEL_FOLDER_SEARCH_CLASS (class);
449 search_class->body_contains = ews_search_body_contains;
450
451 g_object_class_install_property (
452 object_class,
453 PROP_STORE,
454 g_param_spec_object (
455 "store",
456 "EWS Store",
457 "EWS Store for server-side searches",
458 CAMEL_TYPE_EWS_STORE,
459 G_PARAM_READWRITE |
460 G_PARAM_STATIC_STRINGS));
461 }
462
463 static void
464 camel_ews_search_init (CamelEwsSearch *search)
465 {
466 search->priv = camel_ews_search_get_instance_private (search);
467 search->priv->local_data_search = NULL;
468 search->priv->cached_results = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_hash_table_destroy);
469
470 g_weak_ref_init (&search->priv->ews_store, NULL);
471 }
472
473 /**
474 * camel_ews_search_new:
475 * @ews_store: a #CamelEwsStore to which the search belongs
476 *
477 * Returns a new #CamelEwsSearch instance.
478 *
479 * Returns: a new #CamelEwsSearch
480 *
481 * Since: 3.24
482 **/
483 CamelFolderSearch *
484 camel_ews_search_new (CamelEwsStore *ews_store)
485 {
486 g_return_val_if_fail (CAMEL_IS_EWS_STORE (ews_store), NULL);
487
488 return g_object_new (
489 CAMEL_TYPE_EWS_SEARCH,
490 "store", ews_store,
491 NULL);
492 }
493
494 /**
495 * camel_ews_search_ref_store:
496 * @search: a #CamelEwsSearch
497 *
498 * Returns a #CamelEwsStore to use for server-side searches,
499 * or %NULL when the store is offline.
500 *
501 * The returned #CamelEwsStore is referenced for thread-safety and
502 * must be unreferenced with g_object_unref() when finished with it.
503 *
504 * Returns: a #CamelEwsStore, or %NULL
505 *
506 * Since: 3.24
507 **/
508 CamelEwsStore *
509 camel_ews_search_ref_store (CamelEwsSearch *search)
510 {
511 CamelEwsStore *ews_store;
512
513 g_return_val_if_fail (CAMEL_IS_EWS_SEARCH (search), NULL);
514
515 ews_store = g_weak_ref_get (&search->priv->ews_store);
516
517 if (ews_store && !camel_offline_store_get_online (CAMEL_OFFLINE_STORE (ews_store)))
518 g_clear_object (&ews_store);
519
520 return ews_store;
521 }
522
523 /**
524 * camel_ews_search_set_store:
525 * @search: a #CamelEwsSearch
526 * @ews_store: a #CamelEwsStore, or %NULL
527 *
528 * Sets a #CamelEwsStore to use for server-side searches. Generally
529 * this is set for the duration of a single search when online, and then
530 * reset to %NULL.
531 *
532 * Since: 3.24
533 **/
534 void
535 camel_ews_search_set_store (CamelEwsSearch *search,
536 CamelEwsStore *ews_store)
537 {
538 g_return_if_fail (CAMEL_IS_EWS_SEARCH (search));
539
540 if (ews_store != NULL)
541 g_return_if_fail (CAMEL_IS_EWS_STORE (ews_store));
542
543 g_weak_ref_set (&search->priv->ews_store, ews_store);
544
545 g_object_notify (G_OBJECT (search), "store");
546 }
547
548 void
549 camel_ews_search_clear_cached_results (CamelEwsSearch *search)
550 {
551 g_return_if_fail (CAMEL_IS_EWS_SEARCH (search));
552
553 g_hash_table_remove_all (search->priv->cached_results);
554 }
555
556 /**
557 * camel_ews_search_set_cancellable_and_error:
558 * @search: a #CamelEwsSearch
559 * @cancellable: a #GCancellable, or %NULL
560 * @error: a #GError, or %NULL
561 *
562 * Sets @cancellable and @error to use for server-side searches. This way
563 * the search can return accurate errors and be eventually cancelled by
564 * a user.
565 *
566 * Note: The caller is responsible to keep alive both @cancellable and @error
567 * for the whole run of the search and reset them both to NULL after
568 * the search is finished.
569 *
570 * Since: 3.24
571 **/
572 void
573 camel_ews_search_set_cancellable_and_error (CamelEwsSearch *search,
574 GCancellable *cancellable,
575 GError **error)
576 {
577 g_return_if_fail (CAMEL_IS_EWS_SEARCH (search));
578
579 if (cancellable)
580 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
581
582 search->priv->cancellable = cancellable;
583 search->priv->error = error;
584 }