| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: (C) 2024 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-m365-comp-editor-extension.h" |
| 16 |
|
| 17 |
#define E_TYPE_M365_COMP_EDITOR_EXTENSION (e_m365_comp_editor_extension_get_type ()) |
| 18 |
|
| 19 |
GType e_m365_comp_editor_extension_get_type (void); |
| 20 |
|
| 21 |
typedef struct _EM365CompEditorExtension { |
| 22 |
EExtension parent; |
| 23 |
} EM365CompEditorExtension; |
| 24 |
|
| 25 |
typedef struct _EM365CompEditorExtensionClass { |
| 26 |
EExtensionClass parent_class; |
| 27 |
} EM365CompEditorExtensionClass; |
| 28 |
|
| 29 |
G_DEFINE_DYNAMIC_TYPE (EM365CompEditorExtension, e_m365_comp_editor_extension, E_TYPE_EXTENSION) |
| 30 |
|
| 31 |
static void |
| 32 |
e_m365_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_m365_backend(ext) (e_source_has_extension (source, ext) && \ |
| 48 |
g_strcmp0 (e_source_backend_get_backend_name (e_source_get_extension (source, ext)), "microsoft365") == 0) |
| 49 |
|
| 50 |
can_use = source && ( |
| 51 |
check_is_m365_backend (E_SOURCE_EXTENSION_CALENDAR) || |
| 52 |
check_is_m365_backend (E_SOURCE_EXTENSION_MEMO_LIST) || |
| 53 |
check_is_m365_backend (E_SOURCE_EXTENSION_TASK_LIST)); |
| 54 |
|
| 55 |
#undef check_is_m365_backend |
| 56 |
} |
| 57 |
|
| 58 |
/* The Summary has limited length on the Exchange 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 |
|
| 69 |
static void |
| 70 |
e_m365_comp_editor_extension_update_actions (ECompEditor *comp_editor) |
| 71 |
{ |
| 72 |
EUIAction *action; |
| 73 |
gboolean can_use = FALSE; |
| 74 |
|
| 75 |
action = e_comp_editor_get_action (comp_editor, "m365-online-meeting"); |
| 76 |
|
| 77 |
g_return_if_fail (action != NULL); |
| 78 |
|
| 79 |
/* Requirements to see the action: |
| 80 |
* - it's a new component (the option cannot be changed, it can be only set on create) |
| 81 |
* - it is a meeting |
| 82 |
* - the target calendar is M365 |
| 83 |
*/ |
| 84 |
can_use = (e_comp_editor_get_flags (comp_editor) & E_COMP_EDITOR_FLAG_IS_NEW) != 0; |
| 85 |
|
| 86 |
if (can_use) { |
| 87 |
ECompEditorPage *page_general; |
| 88 |
|
| 89 |
page_general = e_comp_editor_get_page (comp_editor, E_TYPE_COMP_EDITOR_PAGE_GENERAL); |
| 90 |
|
| 91 |
can_use = page_general && e_comp_editor_page_general_get_show_attendees (E_COMP_EDITOR_PAGE_GENERAL (page_general)); |
| 92 |
} |
| 93 |
|
| 94 |
if (can_use) { |
| 95 |
ECalClient *target_client; |
| 96 |
|
| 97 |
target_client = e_comp_editor_get_target_client (comp_editor); |
| 98 |
|
| 99 |
can_use = target_client != NULL; |
| 100 |
|
| 101 |
if (can_use) { |
| 102 |
ESource *source; |
| 103 |
|
| 104 |
source = e_client_get_source (E_CLIENT (target_client)); |
| 105 |
|
| 106 |
can_use = source && e_source_has_extension (source, E_SOURCE_EXTENSION_CALENDAR) && |
| 107 |
g_strcmp0 (e_source_backend_get_backend_name (e_source_get_extension (source, E_SOURCE_EXTENSION_CALENDAR)), "microsoft365") == 0; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
e_ui_action_set_visible (action, can_use); |
| 112 |
} |
| 113 |
|
| 114 |
static void |
| 115 |
e_m365_comp_editor_extension_fill_widgets_cb (ECompEditor *comp_editor, |
| 116 |
ICalComponent *component) |
| 117 |
{ |
| 118 |
EUIAction *action; |
| 119 |
|
| 120 |
action = e_comp_editor_get_action (comp_editor, "m365-online-meeting"); |
| 121 |
|
| 122 |
if (action) |
| 123 |
e_ui_action_set_active (action, FALSE); |
| 124 |
|
| 125 |
e_m365_comp_editor_extension_update_actions (comp_editor); |
| 126 |
} |
| 127 |
|
| 128 |
static gboolean |
| 129 |
e_m365_comp_editor_extension_fill_component_cb (ECompEditor *comp_editor, |
| 130 |
ICalComponent *component) |
| 131 |
{ |
| 132 |
EUIAction *action; |
| 133 |
|
| 134 |
action = e_comp_editor_get_action (comp_editor, "m365-online-meeting"); |
| 135 |
|
| 136 |
if (action && e_ui_action_get_visible (action) && |
| 137 |
e_ui_action_get_active (action)) { |
| 138 |
e_cal_util_component_set_x_property (component, "X-M365-ONLINE-MEETING", "1"); |
| 139 |
} else { |
| 140 |
e_cal_util_component_remove_x_property (component, "X-M365-ONLINE-MEETING"); |
| 141 |
} |
| 142 |
|
| 143 |
return TRUE; |
| 144 |
} |
| 145 |
|
| 146 |
static void |
| 147 |
e_m365_comp_editor_extension_handle_map_unmap (ECompEditor *comp_editor, |
| 148 |
gboolean is_map) |
| 149 |
{ |
| 150 |
ECompEditorPage *page_general; |
| 151 |
|
| 152 |
/* Cannot do this in the 'constructed' method, because the extensions are loaded |
| 153 |
before the Event editor fills the comp editor with the pages. */ |
| 154 |
page_general = e_comp_editor_get_page (comp_editor, E_TYPE_COMP_EDITOR_PAGE_GENERAL); |
| 155 |
|
| 156 |
if (page_general) { |
| 157 |
if (is_map) { |
| 158 |
g_signal_connect_object (page_general, "notify::show-attendees", |
| 159 |
G_CALLBACK (e_m365_comp_editor_extension_update_actions), comp_editor, |
| 160 |
G_CONNECT_SWAPPED); |
| 161 |
} else { |
| 162 |
g_signal_handlers_disconnect_by_func (page_general, |
| 163 |
G_CALLBACK (e_m365_comp_editor_extension_update_actions), comp_editor); |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
static void |
| 169 |
e_m365_comp_editor_extension_map_cb (ECompEditor *comp_editor, |
| 170 |
gpointer user_data) |
| 171 |
{ |
| 172 |
e_m365_comp_editor_extension_handle_map_unmap (comp_editor, TRUE); |
| 173 |
} |
| 174 |
|
| 175 |
static void |
| 176 |
e_m365_comp_editor_extension_unmap_cb (ECompEditor *comp_editor, |
| 177 |
gpointer user_data) |
| 178 |
{ |
| 179 |
e_m365_comp_editor_extension_handle_map_unmap (comp_editor, FALSE); |
| 180 |
} |
| 181 |
|
| 182 |
static void |
| 183 |
e_m365_comp_editor_extension_constructed (GObject *object) |
| 184 |
{ |
| 185 |
static const gchar *eui = |
| 186 |
"<eui>" |
| 187 |
"<menu id='main-menu'>" |
| 188 |
"<submenu action='options-menu'>" |
| 189 |
"<placeholder id='toggles'>" |
| 190 |
"<item action='m365-online-meeting'/>" |
| 191 |
"</placeholder>" |
| 192 |
"</submenu>" |
| 193 |
"</menu>" |
| 194 |
"<toolbar id='toolbar-with-headerbar'>" |
| 195 |
"<placeholder id='content'>" |
| 196 |
"<item action='m365-online-meeting'/>" |
| 197 |
"</placeholder>" |
| 198 |
"</toolbar>" |
| 199 |
"<toolbar id='toolbar-without-headerbar'>" |
| 200 |
"<placeholder id='content'>" |
| 201 |
"<item action='m365-online-meeting'/>" |
| 202 |
"</placeholder>" |
| 203 |
"</toolbar>" |
| 204 |
"</eui>"; |
| 205 |
|
| 206 |
static const EUIActionEntry entries[] = { |
| 207 |
{ "m365-online-meeting", |
| 208 |
"stock_people", |
| 209 |
N_("Online Meeting"), |
| 210 |
NULL, |
| 211 |
N_("Create the meeting as an online meeting in the main user calendar"), |
| 212 |
NULL, NULL, "false", (EUIActionFunc) e_ui_action_set_state } |
| 213 |
}; |
| 214 |
EExtensible *extensible; |
| 215 |
|
| 216 |
/* Chain up to parent's method */ |
| 217 |
G_OBJECT_CLASS (e_m365_comp_editor_extension_parent_class)->constructed (object); |
| 218 |
|
| 219 |
extensible = e_extension_get_extensible (E_EXTENSION (object)); |
| 220 |
|
| 221 |
if (E_IS_COMP_EDITOR_EVENT (extensible)) { |
| 222 |
ECompEditor *comp_editor = E_COMP_EDITOR (extensible); |
| 223 |
EUIManager *ui_manager; |
| 224 |
EUIParser *ui_parser; |
| 225 |
GError *local_error = NULL; |
| 226 |
|
| 227 |
ui_manager = e_comp_editor_get_ui_manager (comp_editor); |
| 228 |
ui_parser = e_ui_manager_get_parser (ui_manager); |
| 229 |
|
| 230 |
e_ui_manager_add_actions (ui_manager, "individual", GETTEXT_PACKAGE, |
| 231 |
entries, G_N_ELEMENTS (entries), comp_editor); |
| 232 |
|
| 233 |
if (!e_ui_parser_merge_data (ui_parser, eui, -1, &local_error)) |
| 234 |
g_critical ("%s: Failed to merge .eui data: %s", G_STRFUNC, local_error ? local_error->message : "Unknown error"); |
| 235 |
|
| 236 |
g_clear_error (&local_error); |
| 237 |
|
| 238 |
g_signal_connect (comp_editor, "map", |
| 239 |
G_CALLBACK (e_m365_comp_editor_extension_map_cb), NULL); |
| 240 |
|
| 241 |
g_signal_connect (comp_editor, "unmap", |
| 242 |
G_CALLBACK (e_m365_comp_editor_extension_unmap_cb), NULL); |
| 243 |
|
| 244 |
g_signal_connect (comp_editor, "notify::target-client", |
| 245 |
G_CALLBACK (e_m365_comp_editor_extension_update_actions), NULL); |
| 246 |
|
| 247 |
g_signal_connect (comp_editor, "notify::flags", |
| 248 |
G_CALLBACK (e_m365_comp_editor_extension_update_actions), NULL); |
| 249 |
|
| 250 |
g_signal_connect (comp_editor, "fill-widgets", |
| 251 |
G_CALLBACK (e_m365_comp_editor_extension_fill_widgets_cb), NULL); |
| 252 |
|
| 253 |
g_signal_connect (comp_editor, "fill-component", |
| 254 |
G_CALLBACK (e_m365_comp_editor_extension_fill_component_cb), NULL); |
| 255 |
} |
| 256 |
|
| 257 |
g_signal_connect (extensible, "notify::target-client", |
| 258 |
G_CALLBACK (e_m365_comp_editor_extension_target_client_changed_cb), NULL); |
| 259 |
} |
| 260 |
|
| 261 |
static void |
| 262 |
e_m365_comp_editor_extension_class_init (EM365CompEditorExtensionClass *klass) |
| 263 |
{ |
| 264 |
GObjectClass *object_class; |
| 265 |
EExtensionClass *extension_class; |
| 266 |
|
| 267 |
object_class = G_OBJECT_CLASS (klass); |
| 268 |
object_class->constructed = e_m365_comp_editor_extension_constructed; |
| 269 |
|
| 270 |
extension_class = E_EXTENSION_CLASS (klass); |
| 271 |
extension_class->extensible_type = E_TYPE_COMP_EDITOR; |
| 272 |
} |
| 273 |
|
| 274 |
static void |
| 275 |
e_m365_comp_editor_extension_class_finalize (EM365CompEditorExtensionClass *klass) |
| 276 |
{ |
| 277 |
} |
| 278 |
|
| 279 |
static void |
| 280 |
e_m365_comp_editor_extension_init (EM365CompEditorExtension *extension) |
| 281 |
{ |
| 282 |
} |
| 283 |
|
| 284 |
void |
| 285 |
e_m365_comp_editor_extension_type_register (GTypeModule *type_module) |
| 286 |
{ |
| 287 |
e_m365_comp_editor_extension_register_type (type_module); |
| 288 |
} |