Debian Codegraph @

src/EWS/evolution/e-ews-ooo-notificator.c

raw
1 /*
2 * SPDX-FileCopyrightText: (C) 2012 Red Hat, Inc. (www.redhat.com)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "evolution-ews-config.h"
7
8 #include "e-ews-ooo-notificator.h"
9 #include "camel/camel-ews-store.h"
10 #include "common/e-ews-oof-settings.h"
11
12 #include <shell/e-shell.h>
13 #include <shell/e-shell-view.h>
14 #include <mail/e-mail-backend.h>
15 #include <mail/e-mail-ui-session.h>
16 #include <glib/gi18n-lib.h>
17
18 typedef gboolean (*EEwsOooNotificationDispatcherFunction) (gpointer data);
19
20 struct _EEwsOooNotificatorPrivate {
21 EShell *shell;
22 EMailAccountStore *account_store;
23 GList *stores;
24 GHashTable *alerts;
25 };
26
27 typedef struct _EEwsOooNotificatorDispatcherData {
28 EEwsOooNotificator *extension;
29 CamelEwsStore *ews_store;
30 guint timeout_id;
31 } EEwsOooNotificatorDispatcherData;
32
33 G_DEFINE_DYNAMIC_TYPE_EXTENDED (EEwsOooNotificator, e_ews_ooo_notificator, E_TYPE_EXTENSION, 0,
34 G_ADD_PRIVATE_DYNAMIC (EEwsOooNotificator))
35
36 /* Forward declarations */
37 static void e_ews_ooo_notificator_has_ooo_set_cb (EEwsOooNotificator *extension,
38 GParamSpec *pspec,
39 CamelEwsStore *ews_store);
40 static void e_ews_ooo_notificator_service_disabled_cb (EEwsOooNotificator *extension,
41 CamelService *service,
42 EMailAccountStore *account_store);
43 static void e_ews_ooo_notificator_service_removed_cb (EEwsOooNotificator *extension,
44 CamelService *service,
45 EMailAccountStore *session);
46 static void e_ews_ooo_notificator_service_added_cb (EEwsOooNotificator *extension,
47 CamelService *service,
48 EMailAccountStore *session);
49 static void e_ews_ooo_notificator_online_cb (EEwsOooNotificator* extension,
50 GParamSpec *pspec,
51 EShell *shell);
52
53 static EShellView *
54 e_ews_ooo_notificator_get_extensible (EEwsOooNotificator *extension)
55 {
56 EExtensible *extensible;
57
58 extensible = e_extension_get_extensible (E_EXTENSION (extension));
59
60 return E_SHELL_VIEW (extensible);
61 }
62
63 static void
64 e_ews_ooo_notificator_dispatcher_data_free (gpointer user_data)
65 {
66 EEwsOooNotificatorDispatcherData *data = user_data;
67
68 if (data->extension)
69 g_object_unref (data->extension);
70 if (data->ews_store)
71 g_object_unref (data->ews_store);
72 g_slice_free (EEwsOooNotificatorDispatcherData, data);
73 }
74
75 static void
76 e_ews_ooo_notificator_dismiss_cb (EEwsOooNotificatorDispatcherData *data,
77 gint response_id,
78 EAlert *alert)
79 {
80
81 if (response_id == GTK_RESPONSE_ACCEPT)
82 camel_ews_store_unset_oof_settings_state (data->ews_store);
83
84 camel_ews_store_set_ooo_alert_state (data->ews_store, CAMEL_EWS_STORE_OOO_ALERT_STATE_CLOSED);
85 g_hash_table_remove (data->extension->priv->alerts, data->ews_store);
86
87 if (data->timeout_id) {
88 guint id;
89
90 /* Unset the data structure before calling remove(),
91 because the removal can free the `data`. */
92 id = data->timeout_id;
93 data->timeout_id = 0;
94
95 g_source_remove (id);
96 }
97 }
98
99 static void
100 e_ews_ooo_notificator_hide_notification (EEwsOooNotificator *extension,
101 CamelEwsStore *ews_store)
102 {
103 EAlert *alert;
104
105 alert = g_hash_table_lookup (extension->priv->alerts, ews_store);
106 if (alert) {
107 e_alert_response (alert, GTK_RESPONSE_NONE);
108 g_hash_table_remove (extension->priv->alerts, ews_store);
109 }
110 }
111
112 static gboolean
113 e_ews_ooo_notificator_hide_notification_by_timeout_cb (gpointer user_data)
114 {
115 EEwsOooNotificatorDispatcherData *data = user_data;
116
117 e_ews_ooo_notificator_hide_notification (data->extension, data->ews_store);
118
119 return FALSE;
120 }
121
122 static void
123 e_ews_ooo_notificator_show_notification (EEwsOooNotificator *extension,
124 CamelEwsStore *ews_store)
125 {
126 EAlert *alert;
127 EShellView *view;
128 EShellContent *shell_content;
129 EEwsOooNotificatorDispatcherData *data;
130 EUIAction *action;
131 const gchar *account_name;
132
133 data = g_slice_new0 (EEwsOooNotificatorDispatcherData);
134 data->extension = g_object_ref (extension);
135 data->ews_store = g_object_ref (ews_store);
136
137 view = e_ews_ooo_notificator_get_extensible (extension);
138 shell_content = e_shell_view_get_shell_content (view);
139
140 account_name = camel_service_get_display_name (CAMEL_SERVICE (ews_store));
141 alert = e_alert_new ("ews:has-ooo-set", account_name, NULL);
142
143 g_signal_connect_swapped (
144 alert,
145 "response",
146 G_CALLBACK (e_ews_ooo_notificator_dismiss_cb), data);
147
148 action = e_ui_action_new ("ews-ooo-notificator", "ooo-unset-on-server", NULL);
149 e_ui_action_set_label (action, _("Unset on Server"));
150 e_ui_action_set_tooltip (action, _("Unset the “Out of Office” status"));
151 e_alert_add_action (alert, action, GTK_RESPONSE_ACCEPT, FALSE);
152 g_object_unref (action);
153
154 g_hash_table_insert (extension->priv->alerts, g_object_ref (ews_store), g_object_ref_sink (alert));
155 e_alert_sink_submit_alert (E_ALERT_SINK (shell_content), alert);
156
157 /* If the user doesn't cancel the notify, it will be hide automatically in 5 minutes */
158 data->timeout_id = e_named_timeout_add_seconds_full (
159 G_PRIORITY_DEFAULT, 300,
160 e_ews_ooo_notificator_hide_notification_by_timeout_cb,
161 data, e_ews_ooo_notificator_dispatcher_data_free);
162 }
163
164 static gboolean
165 e_ews_ooo_notificator_has_ooo_set (gpointer user_data)
166 {
167 EEwsOooNotificatorDispatcherData *data = user_data;
168 CamelEwsStoreOooAlertState alert_state;
169 gboolean has_ooo_set;
170
171 alert_state = camel_ews_store_get_ooo_alert_state (data->ews_store);
172 has_ooo_set = camel_ews_store_get_has_ooo_set (data->ews_store);
173
174 if (has_ooo_set && alert_state == CAMEL_EWS_STORE_OOO_ALERT_STATE_UNKNOWN) {
175 e_ews_ooo_notificator_show_notification (data->extension, data->ews_store);
176 camel_ews_store_set_ooo_alert_state (data->ews_store, CAMEL_EWS_STORE_OOO_ALERT_STATE_NOTIFIED);
177 }
178
179 return FALSE;
180 }
181
182 static gboolean
183 e_ews_ooo_notificator_service_disabled (gpointer user_data)
184 {
185 EEwsOooNotificatorDispatcherData *data = user_data;
186 CamelEwsStoreOooAlertState state;
187
188 e_ews_ooo_notificator_hide_notification (data->extension, data->ews_store);
189 state = camel_ews_store_get_ooo_alert_state (data->ews_store);
190 if (state != CAMEL_EWS_STORE_OOO_ALERT_STATE_CLOSED)
191 camel_ews_store_set_ooo_alert_state (data->ews_store, CAMEL_EWS_STORE_OOO_ALERT_STATE_UNKNOWN);
192 camel_ews_store_set_has_ooo_set (data->ews_store, FALSE);
193
194 return FALSE;
195 }
196
197 static gboolean
198 e_ews_ooo_notificator_service_removed (gpointer user_data)
199 {
200 EEwsOooNotificatorDispatcherData *data = user_data;
201
202 e_ews_ooo_notificator_hide_notification (data->extension, data->ews_store);
203 g_signal_handlers_disconnect_by_func (
204 data->ews_store,
205 e_ews_ooo_notificator_has_ooo_set_cb,
206 data->extension);
207 data->extension->priv->stores = g_list_remove (
208 data->extension->priv->stores, data->ews_store);
209
210 return FALSE;
211 }
212
213 /*
214 * GTK+ UI calls cannot be done in a dedicated thread.
215 * So, let's ensure that our functions (that do something in the UI) will run
216 * in the main thread, calling them through e_named_timeout_add_full().
217 */
218 static void
219 e_ews_ooo_notificator_dispatcher (EEwsOooNotificatorDispatcherData *data,
220 EEwsOooNotificationDispatcherFunction function,
221 GDestroyNotify destroy_data)
222 {
223 e_named_timeout_add_full (G_PRIORITY_DEFAULT, 1, function, data, destroy_data);
224 }
225
226 static void
227 e_ews_ooo_notificator_has_ooo_set_cb (EEwsOooNotificator *extension,
228 GParamSpec *pspec,
229 CamelEwsStore *ews_store)
230 {
231 EEwsOooNotificatorDispatcherData *data;
232
233 data = g_slice_new0 (EEwsOooNotificatorDispatcherData);
234 data->extension = g_object_ref (extension);
235 data->ews_store = g_object_ref (ews_store);
236
237 e_ews_ooo_notificator_dispatcher (
238 data,
239 e_ews_ooo_notificator_has_ooo_set,
240 e_ews_ooo_notificator_dispatcher_data_free);
241 }
242
243 static void
244 e_ews_ooo_notificator_service_disabled_cb (EEwsOooNotificator *extension,
245 CamelService *service,
246 EMailAccountStore *account_store)
247 {
248
249 EEwsOooNotificatorDispatcherData *data;
250
251 if (!CAMEL_IS_EWS_STORE (service))
252 return;
253
254 data = g_slice_new0 (EEwsOooNotificatorDispatcherData);
255 data->extension = g_object_ref (extension);
256 data->ews_store = g_object_ref (CAMEL_EWS_STORE (service));
257
258 e_ews_ooo_notificator_dispatcher (
259 data,
260 e_ews_ooo_notificator_service_disabled,
261 e_ews_ooo_notificator_dispatcher_data_free);
262 }
263
264 static void
265 e_ews_ooo_notificator_service_removed_cb (EEwsOooNotificator *extension,
266 CamelService *service,
267 EMailAccountStore *session)
268 {
269 EEwsOooNotificatorDispatcherData *data;
270
271 if (!CAMEL_IS_EWS_STORE (service))
272 return;
273
274 data = g_slice_new0 (EEwsOooNotificatorDispatcherData);
275 data->extension = g_object_ref (extension);
276 data->ews_store = g_object_ref (CAMEL_EWS_STORE (service));
277
278 e_ews_ooo_notificator_dispatcher (
279 data,
280 e_ews_ooo_notificator_service_removed,
281 e_ews_ooo_notificator_dispatcher_data_free);
282 }
283
284 static void
285 e_ews_ooo_notificator_service_added_cb (EEwsOooNotificator *extension,
286 CamelService *service,
287 EMailAccountStore *session)
288 {
289 CamelEwsStore *ews_store;
290
291 if (!CAMEL_IS_EWS_STORE (service))
292 return;
293
294 ews_store = CAMEL_EWS_STORE (service);
295 g_signal_connect_swapped (
296 ews_store, "notify::has-ooo-set",
297 G_CALLBACK (e_ews_ooo_notificator_has_ooo_set_cb), extension);
298 extension->priv->stores = g_list_append (extension->priv->stores, g_object_ref (ews_store));
299 }
300
301 static void
302 e_ews_ooo_notificator_online_cb (EEwsOooNotificator* extension,
303 GParamSpec *pspec,
304 EShell *shell)
305 {
306 GList *l;
307
308 if (e_shell_get_online (shell))
309 return;
310
311 for (l = extension->priv->stores; l; l = l->next) {
312 EEwsOooNotificatorDispatcherData *data;
313
314 data = g_slice_new0 (EEwsOooNotificatorDispatcherData);
315 data->extension = g_object_ref (extension);
316 data->ews_store = g_object_ref (CAMEL_EWS_STORE (l->data));
317
318 e_ews_ooo_notificator_dispatcher (
319 data,
320 e_ews_ooo_notificator_service_disabled,
321 e_ews_ooo_notificator_dispatcher_data_free);
322 }
323 }
324
325 static void
326 e_ews_ooo_notificator_init (EEwsOooNotificator *extension)
327 {
328 extension->priv = e_ews_ooo_notificator_get_instance_private (extension);
329
330 extension->priv->alerts = g_hash_table_new_full (g_direct_hash, g_direct_equal, g_object_unref, g_object_unref);
331 }
332
333 static void
334 e_ews_ooo_notificator_constructed (GObject *object)
335 {
336 EEwsOooNotificator *extension;
337 EShell *shell;
338 EShellView *view;
339 EShellBackend *backend;
340 EMailAccountStore *account_store;
341 EMailSession *session;
342 GList *stores, *l;
343 const gchar *view_name;
344
345 /* Chain up to parent's constructed() method. */
346 G_OBJECT_CLASS (e_ews_ooo_notificator_parent_class)->constructed (object);
347
348 extension = E_EWS_OOO_NOTIFICATOR (object);
349 view = e_ews_ooo_notificator_get_extensible (extension);
350 view_name = e_shell_view_get_name (view);
351
352 if (g_strcmp0 (view_name, "mail") != 0)
353 return;
354
355 backend = e_shell_view_get_shell_backend (view);
356 shell = e_shell_backend_get_shell (backend);
357 session = e_mail_backend_get_session (E_MAIL_BACKEND (backend));
358 account_store = e_mail_ui_session_get_account_store (E_MAIL_UI_SESSION (session));
359 stores = camel_session_list_services (CAMEL_SESSION (session));
360
361 extension->priv->shell = shell;
362 extension->priv->account_store = g_object_ref (account_store);
363
364 for (l = stores; l; l = l->next) {
365 CamelService *service = l->data;
366 CamelEwsStore *ews_store;
367 gboolean has_ooo_set;
368
369 if (!CAMEL_IS_EWS_STORE (service))
370 continue;
371
372 ews_store = CAMEL_EWS_STORE (service);
373 has_ooo_set = camel_ews_store_get_has_ooo_set (ews_store);
374
375 if (has_ooo_set) {
376 e_ews_ooo_notificator_show_notification (extension, ews_store);
377 camel_ews_store_set_ooo_alert_state (ews_store, CAMEL_EWS_STORE_OOO_ALERT_STATE_NOTIFIED);
378 }
379
380 g_signal_connect_swapped (
381 ews_store, "notify::has-ooo-set",
382 G_CALLBACK (e_ews_ooo_notificator_has_ooo_set_cb), extension);
383 extension->priv->stores = g_list_append (extension->priv->stores, g_object_ref (ews_store));
384 }
385
386 g_signal_connect_swapped (
387 account_store, "service-disabled",
388 G_CALLBACK (e_ews_ooo_notificator_service_disabled_cb), extension);
389
390 g_signal_connect_swapped (
391 account_store, "service-removed",
392 G_CALLBACK (e_ews_ooo_notificator_service_removed_cb), extension);
393
394 g_signal_connect_swapped (
395 account_store, "service-added",
396 G_CALLBACK (e_ews_ooo_notificator_service_added_cb), extension);
397
398 g_signal_connect_swapped (
399 shell, "notify::online",
400 G_CALLBACK (e_ews_ooo_notificator_online_cb), extension);
401
402 g_list_free_full (stores, g_object_unref);
403 }
404
405 static void
406 e_ews_ooo_notificator_dispose (GObject *object)
407 {
408 EEwsOooNotificator *extension;
409 GList *l;
410
411 extension = E_EWS_OOO_NOTIFICATOR (object);
412
413 if (extension->priv->shell) {
414 g_signal_handlers_disconnect_by_data (extension->priv->shell, extension);
415 extension->priv->shell = NULL;
416 }
417
418 if (extension->priv->account_store) {
419 g_signal_handlers_disconnect_by_data (extension->priv->account_store, extension);
420 g_object_unref (extension->priv->account_store);
421 extension->priv->account_store = NULL;
422 }
423
424 for (l = extension->priv->stores; l; l = l->next) {
425 CamelService *service = l->data;
426
427 if (service) {
428 g_signal_handlers_disconnect_by_data (service, extension);
429 g_object_unref (service);
430 }
431 }
432 g_list_free (extension->priv->stores);
433 extension->priv->stores = NULL;
434
435 /* Chain up to parent's dispose() method. */
436 G_OBJECT_CLASS (e_ews_ooo_notificator_parent_class)->dispose (object);
437 }
438
439 static void
440 e_ews_ooo_notificator_finalize (GObject *object)
441 {
442 EEwsOooNotificator *extension;
443
444 extension = E_EWS_OOO_NOTIFICATOR (object);
445
446 if (extension->priv->alerts) {
447 g_hash_table_destroy (extension->priv->alerts);
448 extension->priv->alerts = NULL;
449 }
450
451 /* Chain up to parent's finalize() method. */
452 G_OBJECT_CLASS (e_ews_ooo_notificator_parent_class)->finalize (object);
453 }
454
455 static void
456 e_ews_ooo_notificator_class_init (EEwsOooNotificatorClass *class)
457 {
458 GObjectClass *object_class;
459 EExtensionClass *extension_class;
460
461 object_class = G_OBJECT_CLASS (class);
462 object_class->constructed = e_ews_ooo_notificator_constructed;
463 object_class->dispose = e_ews_ooo_notificator_dispose;
464 object_class->finalize = e_ews_ooo_notificator_finalize;
465
466 extension_class = E_EXTENSION_CLASS (class);
467 extension_class->extensible_type = E_TYPE_SHELL_VIEW;
468 }
469
470 static void
471 e_ews_ooo_notificator_class_finalize (EEwsOooNotificatorClass *class)
472 {
473 }
474
475 void
476 e_ews_ooo_notificator_type_register (GTypeModule *type_module)
477 {
478 /* XXX G_DEFINE_DYNAMIC_TYPE declares a static type registration
479 * function, so we have to wrap it with a public function in
480 * order to register types from a separate compilation unit. */
481 e_ews_ooo_notificator_register_type (type_module);
482 }