Debian Codegraph @

src/EWS/evolution/e-ews-photo-source.c

raw
1 /*
2 * SPDX-FileCopyrightText: (C) 2018 Red Hat, Inc. (www.redhat.com)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "evolution-ews-config.h"
7
8 #include <e-util/e-util.h>
9
10 #include "common/camel-ews-settings.h"
11 #include "common/e-ews-connection.h"
12
13 #include "e-ews-photo-source.h"
14
15 /* Standard GObject macros */
16 #define E_TYPE_EWS_PHOTO_SOURCE \
17 (e_ews_photo_source_get_type ())
18 #define E_EWS_PHOTO_SOURCE(obj) \
19 (G_TYPE_CHECK_INSTANCE_CAST \
20 ((obj), E_TYPE_EWS_PHOTO_SOURCE, EEwsPhotoSource))
21 #define E_EWS_PHOTO_SOURCE_CLASS(cls) \
22 (G_TYPE_CHECK_CLASS_CAST \
23 ((cls), E_TYPE_EWS_PHOTO_SOURCE, EEwsPhotoSourceClass))
24 #define E_IS_EWS_PHOTO_SOURCE(obj) \
25 (G_TYPE_CHECK_INSTANCE_TYPE \
26 ((obj), E_TYPE_EWS_PHOTO_SOURCE))
27 #define E_IS_EWS_PHOTO_SOURCE_CLASS(cls) \
28 (G_TYPE_CHECK_CLASS_TYPE \
29 ((cls), E_TYPE_EWS_PHOTO_SOURCE))
30 #define E_EWS_PHOTO_SOURCE_GET_CLASS(obj) \
31 (G_TYPE_INSTANCE_GET_CLASS \
32 ((obj), E_TYPE_EWS_PHOTO_SOURCE, EEwsPhotoSourceClass))
33
34 typedef struct _EEwsPhotoSource EEwsPhotoSource;
35 typedef struct _EEwsPhotoSourceClass EEwsPhotoSourceClass;
36
37 struct _EEwsPhotoSource {
38 EExtension parent;
39 GThreadPool *pool;
40 };
41
42 struct _EEwsPhotoSourceClass {
43 EExtensionClass parent_class;
44 };
45
46 GType e_ews_photo_source_get_type (void) G_GNUC_CONST;
47
48 static void ews_photo_source_iface_init (EPhotoSourceInterface *iface);
49
50 G_DEFINE_DYNAMIC_TYPE_EXTENDED (EEwsPhotoSource, e_ews_photo_source, E_TYPE_EXTENSION, 0,
51 G_IMPLEMENT_INTERFACE_DYNAMIC (E_TYPE_PHOTO_SOURCE, ews_photo_source_iface_init))
52
53 static void
54 e_ews_photo_source_pool_thread_func_cb (gpointer data,
55 gpointer user_data)
56 {
57 GTask *task = data;
58 GCancellable *cancellable = g_task_get_cancellable (task);
59 const gchar *email_address = g_task_get_task_data (task);
60 GSList *connections, *link;
61 GHashTable *covered_uris;
62 GError *local_error = NULL;
63
64 /* Most users connect to a single server anyway, thus no big deal doing
65 this in serial, instead of in parallel. */
66 covered_uris = g_hash_table_new_full (camel_strcase_hash, camel_strcase_equal, g_free, NULL);
67 connections = e_ews_connection_list_existing ();
68
69 for (link = connections; link && !g_cancellable_is_cancelled (cancellable); link = g_slist_next (link)) {
70 EEwsConnection *cnc = link->data;
71 gchar *picture_data = NULL;
72 const gchar *uri;
73
74 if (!E_IS_EWS_CONNECTION (cnc) ||
75 !e_ews_connection_satisfies_server_version (cnc, E_EWS_EXCHANGE_2013))
76 continue;
77
78 uri = e_ews_connection_get_uri (cnc);
79 if (!uri || !*uri || g_hash_table_contains (covered_uris, uri))
80 continue;
81
82 g_hash_table_insert (covered_uris, g_strdup (uri), NULL);
83
84 if (e_ews_connection_get_user_photo_sync (cnc, G_PRIORITY_LOW, email_address, E_EWS_SIZE_REQUESTED_48X48,
85 &picture_data, cancellable, local_error ? NULL : &local_error) && picture_data) {
86 gsize len = 0;
87 guchar *decoded;
88
89 decoded = g_base64_decode (picture_data, &len);
90 if (len && decoded) {
91 GInputStream *stream;
92
93 stream = g_memory_input_stream_new_from_data (decoded, len, g_free);
94 decoded = NULL;
95
96 g_task_return_pointer (task, stream, g_object_unref);
97 g_clear_object (&task);
98 g_free (decoded);
99 break;
100 }
101
102 g_free (decoded);
103 }
104 }
105
106 g_slist_free_full (connections, g_object_unref);
107 g_hash_table_destroy (covered_uris);
108
109 if (task) {
110 if (!local_error) {
111 /* Do not localize the string, it won't go into the UI/be visible to users */
112 g_set_error_literal (&local_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND, "Not Found");
113 }
114
115 g_task_return_error (task, local_error);
116 g_clear_object (&task);
117 } else {
118 g_clear_error (&local_error);
119 }
120 }
121
122 static void
123 ews_photo_source_get_photo (EPhotoSource *photo_source,
124 const gchar *email_address,
125 GCancellable *cancellable,
126 GAsyncReadyCallback callback,
127 gpointer user_data)
128 {
129 EEwsPhotoSource *ews_photo_source;
130 GTask *task;
131
132 g_return_if_fail (E_IS_EWS_PHOTO_SOURCE (photo_source));
133 g_return_if_fail (email_address != NULL);
134
135 ews_photo_source = E_EWS_PHOTO_SOURCE (photo_source);
136
137 task = g_task_new (photo_source, cancellable, callback, user_data);
138 g_task_set_source_tag (task, ews_photo_source_get_photo);
139 g_task_set_task_data (task, g_strdup (email_address), g_free);
140 /* process only one request at a time, without using GTask threads, because
141 those are important to not be used for a long time */
142 g_thread_pool_push (ews_photo_source->pool, task, NULL);
143 }
144
145 static gboolean
146 ews_photo_source_get_photo_finish (EPhotoSource *photo_source,
147 GAsyncResult *result,
148 GInputStream **out_stream,
149 gint *out_priority,
150 GError **error)
151 {
152 GInputStream *input_stream;
153 gboolean success;
154
155 g_return_val_if_fail (E_IS_EWS_PHOTO_SOURCE (photo_source), FALSE);
156 g_return_val_if_fail (g_task_is_valid (result, photo_source), FALSE);
157
158 if (out_priority)
159 *out_priority = G_PRIORITY_DEFAULT;
160
161 input_stream = g_task_propagate_pointer (G_TASK (result), error);
162
163 success = input_stream != NULL;
164
165 if (out_stream)
166 *out_stream = input_stream;
167 else
168 g_clear_object (&input_stream);
169
170 return success;
171 }
172
173 static void
174 ews_photo_source_constructed (GObject *object)
175 {
176 EPhotoCache *photo_cache;
177
178 /* Chain up to parent's method. */
179 G_OBJECT_CLASS (e_ews_photo_source_parent_class)->constructed (object);
180
181 photo_cache = E_PHOTO_CACHE (e_extension_get_extensible (E_EXTENSION (object)));
182
183 e_photo_cache_add_photo_source (photo_cache, E_PHOTO_SOURCE (object));
184 }
185
186 static void
187 ews_photo_source_finalize (GObject *object)
188 {
189 EEwsPhotoSource *ews_photo_source = E_EWS_PHOTO_SOURCE (object);
190
191 g_thread_pool_free (ews_photo_source->pool, FALSE, TRUE);
192
193 /* Chain up to parent's method. */
194 G_OBJECT_CLASS (e_ews_photo_source_parent_class)->finalize (object);
195 }
196
197 static void
198 e_ews_photo_source_class_init (EEwsPhotoSourceClass *class)
199 {
200 GObjectClass *object_class;
201 EExtensionClass *extension_class;
202
203 object_class = G_OBJECT_CLASS (class);
204 object_class->constructed = ews_photo_source_constructed;
205 object_class->finalize = ews_photo_source_finalize;
206
207 extension_class = E_EXTENSION_CLASS (class);
208 extension_class->extensible_type = E_TYPE_PHOTO_CACHE;
209 }
210
211 static void
212 e_ews_photo_source_class_finalize (EEwsPhotoSourceClass *class)
213 {
214 }
215
216 static void
217 ews_photo_source_iface_init (EPhotoSourceInterface *iface)
218 {
219 iface->get_photo = ews_photo_source_get_photo;
220 iface->get_photo_finish = ews_photo_source_get_photo_finish;
221 }
222
223 static void
224 e_ews_photo_source_init (EEwsPhotoSource *extension)
225 {
226 extension->pool = g_thread_pool_new (e_ews_photo_source_pool_thread_func_cb, NULL, 1, FALSE, NULL);
227 }
228
229 void
230 e_ews_photo_source_type_register (GTypeModule *type_module)
231 {
232 /* XXX G_DEFINE_DYNAMIC_TYPE declares a static type registration
233 * function, so we have to wrap it with a public function in
234 * order to register types from a separate compilation unit. */
235 e_ews_photo_source_register_type (type_module);
236 }