Debian Codegraph @

src/EWS/evolution/e-ews-comp-editor-extension.c

raw
1 /*
2 * SPDX-FileCopyrightText: (C) 2022 Red Hat (www.redhat.com)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "evolution-ews-config.h"
7
8 #include <glib/gi18n-lib.h>
9 #include <glib-object.h>
10
11 #include <calendar/gui/e-comp-editor.h>
12 #include <calendar/gui/e-comp-editor-event.h>
13 #include <calendar/gui/e-comp-editor-page-general.h>
14
15 #include "e-ews-comp-editor-extension.h"
16
17 #define E_TYPE_EWS_COMP_EDITOR_EXTENSION (e_ews_comp_editor_extension_get_type ())
18
19 GType e_ews_comp_editor_extension_get_type (void);
20
21 typedef struct _EEwsCompEditorExtension {
22 EExtension parent;
23 } EEwsCompEditorExtension;
24
25 typedef struct _EEwsCompEditorExtensionClass {
26 EExtensionClass parent_class;
27 } EEwsCompEditorExtensionClass;
28
29 G_DEFINE_DYNAMIC_TYPE (EEwsCompEditorExtension, e_ews_comp_editor_extension, E_TYPE_EXTENSION)
30
31 static void
32 e_ews_comp_editor_extension_target_client_changed_cb (ECompEditor *comp_editor)
33 {
34 ECalClient *target_client;
35 ECompEditorPropertyPart *part;
36 gboolean can_use;
37
38 target_client = e_comp_editor_get_target_client (comp_editor);
39
40 can_use = target_client != NULL;
41
42 if (can_use) {
43 ESource *source;
44
45 source = e_client_get_source (E_CLIENT (target_client));
46
47 #define check_is_ews_backend(ext) (e_source_has_extension (source, ext) && \
48 g_strcmp0 (e_source_backend_get_backend_name (e_source_get_extension (source, ext)), "ews") == 0)
49
50 can_use = source && (
51 check_is_ews_backend (E_SOURCE_EXTENSION_CALENDAR) ||
52 check_is_ews_backend (E_SOURCE_EXTENSION_MEMO_LIST) ||
53 check_is_ews_backend (E_SOURCE_EXTENSION_TASK_LIST));
54
55 #undef check_is_ews_backend
56 }
57
58 /* These two have limited length on the EWS server */
59 part = e_comp_editor_get_property_part (comp_editor, I_CAL_SUMMARY_PROPERTY);
60 if (part) {
61 GtkWidget *edit_widget;
62
63 edit_widget = e_comp_editor_property_part_get_edit_widget (part);
64 if (GTK_IS_ENTRY (edit_widget))
65 gtk_entry_set_max_length (GTK_ENTRY (edit_widget), can_use ? 255 : 0);
66 }
67
68 part = e_comp_editor_get_property_part (comp_editor, I_CAL_LOCATION_PROPERTY);
69 if (part) {
70 GtkWidget *edit_widget;
71
72 edit_widget = e_comp_editor_property_part_get_edit_widget (part);
73 if (GTK_IS_ENTRY (edit_widget))
74 gtk_entry_set_max_length (GTK_ENTRY (edit_widget), can_use ? 255 : 0);
75 }
76 }
77
78 static void
79 e_ews_comp_editor_extension_update_actions (ECompEditor *comp_editor)
80 {
81 EUIAction *action;
82 gboolean can_use = FALSE;
83
84 action = e_comp_editor_get_action (comp_editor, "ews-online-meeting");
85
86 g_return_if_fail (action != NULL);
87
88 /* Requirements to see the action:
89 * - it's a new component (the option cannot be changed, it can be only set on create)
90 * - it is a meeting
91 * - the target calendar is EWS
92 * - the authentication method is OAuth2
93 *
94 * It would check the host this connects to, but there are different
95 * end points, not only outlook.office365.com, thus check whether the auth
96 * method is OAuth2. It is also not accurate, because on-premise Exchange
97 * servers can have configured OAuth2, without using the Microsoft servers,
98 * but it might be good enough test.
99 */
100 can_use = (e_comp_editor_get_flags (comp_editor) & E_COMP_EDITOR_FLAG_IS_NEW) != 0;
101
102 if (can_use) {
103 ECompEditorPage *page_general;
104
105 page_general = e_comp_editor_get_page (comp_editor, E_TYPE_COMP_EDITOR_PAGE_GENERAL);
106
107 can_use = page_general && e_comp_editor_page_general_get_show_attendees (E_COMP_EDITOR_PAGE_GENERAL (page_general));
108 }
109
110 if (can_use) {
111 ECalClient *target_client;
112
113 target_client = e_comp_editor_get_target_client (comp_editor);
114
115 can_use = target_client != NULL;
116
117 if (can_use) {
118 ESource *source;
119
120 source = e_client_get_source (E_CLIENT (target_client));
121
122 can_use = source && e_source_has_extension (source, E_SOURCE_EXTENSION_CALENDAR) &&
123 g_strcmp0 (e_source_backend_get_backend_name (e_source_get_extension (source, E_SOURCE_EXTENSION_CALENDAR)), "ews") == 0;
124
125 if (can_use) {
126 ESourceRegistry *registry;
127 ESource *collection_source;
128
129 registry = e_shell_get_registry (e_comp_editor_get_shell (comp_editor));
130 collection_source = e_source_registry_find_extension (registry, source, E_SOURCE_EXTENSION_COLLECTION);
131 can_use = collection_source && e_source_has_extension (source, E_SOURCE_EXTENSION_AUTHENTICATION);
132
133 if (can_use) {
134 EOAuth2Services *oauth2_services;
135 const gchar *method;
136
137 oauth2_services = e_source_registry_get_oauth2_services (registry);
138 method = e_source_authentication_get_method (e_source_get_extension (source, E_SOURCE_EXTENSION_AUTHENTICATION));
139
140 can_use = method && e_oauth2_services_is_oauth2_alias (oauth2_services, method);
141 }
142
143 g_clear_object (&collection_source);
144 }
145 }
146 }
147
148 e_ui_action_set_visible (action, can_use);
149 }
150
151 static void
152 e_ews_comp_editor_extension_fill_widgets_cb (ECompEditor *comp_editor,
153 ICalComponent *component)
154 {
155 EUIAction *action;
156
157 action = e_comp_editor_get_action (comp_editor, "ews-online-meeting");
158
159 if (action)
160 e_ui_action_set_active (action, FALSE);
161
162 e_ews_comp_editor_extension_update_actions (comp_editor);
163 }
164
165 static gboolean
166 e_ews_comp_editor_extension_fill_component_cb (ECompEditor *comp_editor,
167 ICalComponent *component)
168 {
169 EUIAction *action;
170
171 action = e_comp_editor_get_action (comp_editor, "ews-online-meeting");
172
173 if (action && e_ui_action_get_visible (action) &&
174 e_ui_action_get_active (action)) {
175 e_cal_util_component_set_x_property (component, "X-M365-ONLINE-MEETING", "1");
176 } else {
177 e_cal_util_component_remove_x_property (component, "X-M365-ONLINE-MEETING");
178 }
179
180 return TRUE;
181 }
182
183 static void
184 e_ews_comp_editor_extension_handle_map_unmap (ECompEditor *comp_editor,
185 gboolean is_map)
186 {
187 ECompEditorPage *page_general;
188
189 /* Cannot do this in the 'constructed' method, because the extensions are loaded
190 before the Event editor fills the comp editor with the pages. */
191 page_general = e_comp_editor_get_page (comp_editor, E_TYPE_COMP_EDITOR_PAGE_GENERAL);
192
193 if (page_general) {
194 if (is_map) {
195 g_signal_connect_object (page_general, "notify::show-attendees",
196 G_CALLBACK (e_ews_comp_editor_extension_update_actions), comp_editor,
197 G_CONNECT_SWAPPED);
198 } else {
199 g_signal_handlers_disconnect_by_func (page_general,
200 G_CALLBACK (e_ews_comp_editor_extension_update_actions), comp_editor);
201 }
202 }
203 }
204
205 static void
206 e_ews_comp_editor_extension_map_cb (ECompEditor *comp_editor,
207 gpointer user_data)
208 {
209 e_ews_comp_editor_extension_handle_map_unmap (comp_editor, TRUE);
210 }
211
212 static void
213 e_ews_comp_editor_extension_unmap_cb (ECompEditor *comp_editor,
214 gpointer user_data)
215 {
216 e_ews_comp_editor_extension_handle_map_unmap (comp_editor, FALSE);
217 }
218
219 static void
220 e_ews_comp_editor_extension_constructed (GObject *object)
221 {
222 static const gchar *eui =
223 "<eui>"
224 "<menu id='main-menu'>"
225 "<submenu action='options-menu'>"
226 "<placeholder id='toggles'>"
227 "<item action='ews-online-meeting'/>"
228 "</placeholder>"
229 "</submenu>"
230 "</menu>"
231 "<toolbar id='toolbar-with-headerbar'>"
232 "<placeholder id='content'>"
233 "<item action='ews-online-meeting'/>"
234 "</placeholder>"
235 "</toolbar>"
236 "<toolbar id='toolbar-without-headerbar'>"
237 "<placeholder id='content'>"
238 "<item action='ews-online-meeting'/>"
239 "</placeholder>"
240 "</toolbar>"
241 "</eui>";
242
243 static const EUIActionEntry entries[] = {
244 { "ews-online-meeting",
245 "stock_people",
246 N_("Online Meeting"),
247 NULL,
248 N_("Create the meeting as an online meeting in the main user calendar"),
249 NULL, NULL, "false", (EUIActionFunc) e_ui_action_set_state }
250 };
251 EExtensible *extensible;
252
253 /* Chain up to parent's method */
254 G_OBJECT_CLASS (e_ews_comp_editor_extension_parent_class)->constructed (object);
255
256 extensible = e_extension_get_extensible (E_EXTENSION (object));
257
258 if (E_IS_COMP_EDITOR_EVENT (extensible)) {
259 ECompEditor *comp_editor = E_COMP_EDITOR (extensible);
260 EUIManager *ui_manager;
261 EUIParser *ui_parser;
262 GError *local_error = NULL;
263
264 ui_manager = e_comp_editor_get_ui_manager (comp_editor);
265 ui_parser = e_ui_manager_get_parser (ui_manager);
266
267 e_ui_manager_add_actions (ui_manager, "individual", GETTEXT_PACKAGE,
268 entries, G_N_ELEMENTS (entries), comp_editor);
269
270 if (!e_ui_parser_merge_data (ui_parser, eui, -1, &local_error))
271 g_critical ("%s: Failed to merge .eui data: %s", G_STRFUNC, local_error ? local_error->message : "Unknown error");
272
273 g_clear_error (&local_error);
274
275 g_signal_connect (comp_editor, "map",
276 G_CALLBACK (e_ews_comp_editor_extension_map_cb), NULL);
277
278 g_signal_connect (comp_editor, "unmap",
279 G_CALLBACK (e_ews_comp_editor_extension_unmap_cb), NULL);
280
281 g_signal_connect (comp_editor, "notify::target-client",
282 G_CALLBACK (e_ews_comp_editor_extension_update_actions), NULL);
283
284 g_signal_connect (comp_editor, "notify::flags",
285 G_CALLBACK (e_ews_comp_editor_extension_update_actions), NULL);
286
287 g_signal_connect (comp_editor, "fill-widgets",
288 G_CALLBACK (e_ews_comp_editor_extension_fill_widgets_cb), NULL);
289
290 g_signal_connect (comp_editor, "fill-component",
291 G_CALLBACK (e_ews_comp_editor_extension_fill_component_cb), NULL);
292 }
293
294 g_signal_connect (extensible, "notify::target-client",
295 G_CALLBACK (e_ews_comp_editor_extension_target_client_changed_cb), NULL);
296 }
297
298 static void
299 e_ews_comp_editor_extension_class_init (EEwsCompEditorExtensionClass *klass)
300 {
301 GObjectClass *object_class;
302 EExtensionClass *extension_class;
303
304 object_class = G_OBJECT_CLASS (klass);
305 object_class->constructed = e_ews_comp_editor_extension_constructed;
306
307 extension_class = E_EXTENSION_CLASS (klass);
308 extension_class->extensible_type = E_TYPE_COMP_EDITOR;
309 }
310
311 static void
312 e_ews_comp_editor_extension_class_finalize (EEwsCompEditorExtensionClass *klass)
313 {
314 }
315
316 static void
317 e_ews_comp_editor_extension_init (EEwsCompEditorExtension *extension)
318 {
319 }
320
321 void
322 e_ews_comp_editor_extension_type_register (GTypeModule *type_module)
323 {
324 e_ews_comp_editor_extension_register_type (type_module);
325 }