diff --git a/NEWS b/NEWS index 5c2a56fc..b76a0789 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,12 @@ +Major Changes in 52.alpha: +* Add a Jitsi video conference to an event on a Federated Computer Core's + calendar with one click. The room is on the Core's own Jitsi server, and + its link goes in the event's notes and in the iCalendar CONFERENCE + property, so that the people invited get it. +* Show a Join button for Jitsi links and CONFERENCE properties in events +* Keep showing an event's location in its popover when the event also has + an online meeting + Major Changes in 51.0: * Preparations for further keyboard navigation improvements * Show a support URL in the About dialog diff --git a/PLAN.md b/PLAN.md index 04d9b76c..70dae1ed 100644 --- a/PLAN.md +++ b/PLAN.md @@ -26,31 +26,38 @@ event get the link in their invitation, and the event popover already shows a ### Work -- [ ] Room names: generate an unguessable name (e.g. a random word triple +- [x] Room names: generate an unguessable name (e.g. a random word triple plus a short random suffix) with a function in `src/federated/` and a unit test in `tests/`. -- [ ] Server lookup: a function in `src/federated/gcal-federated-core.c` +- [x] Server lookup: a function in `src/federated/gcal-federated-core.c` that returns `https://jitsi.` for a Core calendar, using the `CoreConf` that `load_core_conf()` already reads, with a unit test. -- [ ] Event editor UI: an "Add Video Conference" button in the event +- [x] Event editor UI: an "Add Video Conference" button in the event editor (probably beside the location in `src/gui/event-editor/gcal-summary-section.blp`). Once added, show the link with Copy and Remove buttons instead. -- [ ] Saving: when the event is saved, add the link to the description +- [x] Saving: when the event is saved, add the link to the description and a `CONFERENCE` property to the event's iCalendar component, so that it reaches attendees in the invitation. Keep it stable across later edits and updates, and take both out again on Remove. - [ ] Check that Nextcloud keeps the `CONFERENCE` property and puts it in - the invitations it sends. -- [ ] Reading: detect Jitsi links in existing events (`CONFERENCE`, + the invitations it sends. Its source says it stores events as they + are and attaches the whole event as `event.ics`, but the email body + shows only the description (with our link), location and `URL`, not + `CONFERENCE`. Still to see on a Core: whether sabre/vobject's iTip + broker keeps `CONFERENCE` in that attachment. +- [x] Reading: detect Jitsi links in existing events (`CONFERENCE`, description and location) so that the editor shows "conference added" rather than offering to add a second one, and so that the popover's Join button finds them (`gcal_util_extract_meeting_url()` in `src/utils/gcal-utils.c` only knows Google Meet and Teams). - [ ] Recurring events: check that the link applies to the whole series - and to "this and future" edits. -- [ ] Accessibility and translation: labels and tooltips are translatable + and to "this and future" edits. "All events" now copies the + conference to the series, and the other choices save the edited + occurrence with it. Still to try on a Core, including occurrences + that were edited on their own before. +- [x] Accessibility and translation: labels and tooltips are translatable and the new widgets are reachable by keyboard and screen reader. -- [ ] Docs: describe the feature in `README.md`, and add a `NEWS` entry. +- [x] Docs: describe the feature in `README.md`, and add a `NEWS` entry. - [ ] Test by hand on a Core calendar: add a conference, invite someone, and check the invitation email and the Join button both open the room. diff --git a/README.md b/README.md index b88964f8..1237627a 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,22 @@ People can be invited to events on the calendars of a Core that CoreDesktop's `configure` has set up on this computer. You become the event's organizer, and the Core's Nextcloud emails the invitations when you save the event. +## Video conferences + +Events on a Core's calendars can have a video conference in a room on the +Core's own Jitsi server, at `jitsi.`. Click **Add Video +Conference** under the event's location, and Federated Calendar makes up a +room name that nobody will guess, like `BraveOttersSing-k3f9x2m7`. + +The room's link goes at the end of the event's notes, so that everyone you +invite gets it in their invitation email, whatever mail app they use. It's +also saved as the event's iCalendar `CONFERENCE` property, for the calendar +apps that read it. The location stays free for a place to meet in person. + +The event editor shows the link with buttons to copy or remove it, and the +event's popover has a **Join** button that opens the room. These also work +for Jitsi links that other people put in their events' notes or location. + ## Building On Arch, install the build tools first: diff --git a/po/POTFILES.in b/po/POTFILES.in index 3de7cea8..eb3a76d8 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -4,6 +4,7 @@ data/org.gnome.Calendar.desktop.in.in data/org.gnome.calendar.gschema.xml.in src/appdata/org.gnome.Calendar.metainfo.xml.in.in src/core/gcal-event.c +src/federated/gcal-jitsi.c src/gui/calendar-management/calendar-row.blp src/gui/calendar-management/gcal-calendar-management-dialog.blp src/gui/calendar-management/gcal-calendars-page.blp diff --git a/src/core/gcal-event.c b/src/core/gcal-event.c index f21da6bf..6df6178f 100644 --- a/src/core/gcal-event.c +++ b/src/core/gcal-event.c @@ -2270,3 +2270,187 @@ gcal_event_attendees_equal (GcalEvent *event1, return TRUE; } + +static GPtrArray * +get_conference_properties (GcalEvent *self) +{ + ICalComponent *icalcomp; + ICalProperty *prop; + GPtrArray *props; + + props = g_ptr_array_new_with_free_func (g_object_unref); + icalcomp = e_cal_component_get_icalcomponent (self->component); + + for (prop = i_cal_component_get_first_property (icalcomp, I_CAL_CONFERENCE_PROPERTY); + prop; + prop = i_cal_component_get_next_property (icalcomp, I_CAL_CONFERENCE_PROPERTY)) + { + g_ptr_array_add (props, prop); + } + + return props; +} + +/** + * gcal_event_dup_conference_url: + * @self: a #GcalEvent + * + * Gets the URL of the first of the event's conferences, from its iCalendar + * CONFERENCE properties (RFC 7986). + * + * Returns: (transfer full) (nullable): the conference's URL, or %NULL + */ +gchar * +gcal_event_dup_conference_url (GcalEvent *self) +{ + g_autoptr (GPtrArray) props = NULL; + + g_return_val_if_fail (GCAL_IS_EVENT (self), NULL); + + props = get_conference_properties (self); + + for (guint i = 0; i < props->len; i++) + { + const gchar *url = i_cal_property_get_conference (g_ptr_array_index (props, i)); + + if (url && *url) + return g_strdup (url); + } + + return NULL; +} + +/** + * gcal_event_add_conference: + * @self: a #GcalEvent + * @url: the URL of a video conference + * @label: (nullable): a name for the conference to show people + * + * Adds a CONFERENCE property (RFC 7986) for the video conference at @url + * to @self, unless it has one already. + */ +void +gcal_event_add_conference (GcalEvent *self, + const gchar *url, + const gchar *label) +{ + g_autoptr (GPtrArray) props = NULL; + ICalComponent *icalcomp; + ICalProperty *prop; + + g_return_if_fail (GCAL_IS_EVENT (self)); + g_return_if_fail (url != NULL && *url); + + props = get_conference_properties (self); + + for (guint i = 0; i < props->len; i++) + { + if (g_strcmp0 (i_cal_property_get_conference (g_ptr_array_index (props, i)), url) == 0) + return; + } + + prop = i_cal_property_new_conference (url); + i_cal_property_take_parameter (prop, i_cal_parameter_new_value (I_CAL_VALUE_URI)); + /* Parsed, because libical 3 can't make a FEATURE with several values */ + i_cal_property_take_parameter (prop, i_cal_parameter_new_from_string ("FEATURE=AUDIO,VIDEO")); + + if (label && *label) + i_cal_property_take_parameter (prop, i_cal_parameter_new_label (label)); + + icalcomp = e_cal_component_get_icalcomponent (self->component); + i_cal_component_take_property (icalcomp, prop); +} + +/** + * gcal_event_remove_conference: + * @self: a #GcalEvent + * @url: the URL of a video conference + * + * Removes the CONFERENCE properties for the conference at @url from @self. + */ +void +gcal_event_remove_conference (GcalEvent *self, + const gchar *url) +{ + g_autoptr (GPtrArray) props = NULL; + ICalComponent *icalcomp; + + g_return_if_fail (GCAL_IS_EVENT (self)); + g_return_if_fail (url != NULL); + + props = get_conference_properties (self); + icalcomp = e_cal_component_get_icalcomponent (self->component); + + for (guint i = 0; i < props->len; i++) + { + ICalProperty *prop = g_ptr_array_index (props, i); + + if (g_strcmp0 (i_cal_property_get_conference (prop), url) == 0) + i_cal_component_remove_property (icalcomp, prop); + } +} + +/** + * gcal_event_copy_conferences: + * @self: a #GcalEvent + * @event: the #GcalEvent to copy from + * + * Replaces the CONFERENCE properties of @self with those of @event. + */ +void +gcal_event_copy_conferences (GcalEvent *self, + GcalEvent *event) +{ + g_autoptr (GPtrArray) old_props = NULL; + g_autoptr (GPtrArray) new_props = NULL; + ICalComponent *icalcomp; + + g_return_if_fail (GCAL_IS_EVENT (self)); + g_return_if_fail (GCAL_IS_EVENT (event)); + + old_props = get_conference_properties (self); + new_props = get_conference_properties (event); + icalcomp = e_cal_component_get_icalcomponent (self->component); + + for (guint i = 0; i < old_props->len; i++) + i_cal_component_remove_property (icalcomp, g_ptr_array_index (old_props, i)); + + for (guint i = 0; i < new_props->len; i++) + i_cal_component_take_property (icalcomp, i_cal_property_clone (g_ptr_array_index (new_props, i))); +} + +/** + * gcal_event_conferences_equal: + * @event1: a #GcalEvent + * @event2: a #GcalEvent + * + * Checks if @event1 and @event2 have the same CONFERENCE properties. + * + * Returns: %TRUE if the events' conferences are identical + */ +gboolean +gcal_event_conferences_equal (GcalEvent *event1, + GcalEvent *event2) +{ + g_autoptr (GPtrArray) props1 = NULL; + g_autoptr (GPtrArray) props2 = NULL; + + g_assert (GCAL_IS_EVENT (event1) && GCAL_IS_EVENT (event2)); + + props1 = get_conference_properties (event1); + props2 = get_conference_properties (event2); + + if (props1->len != props2->len) + return FALSE; + + for (guint i = 0; i < props1->len; i++) + { + g_autofree gchar *string1 = i_cal_property_as_ical_string (g_ptr_array_index (props1, i)); + g_autofree gchar *string2 = i_cal_property_as_ical_string (g_ptr_array_index (props2, i)); + + if (g_strcmp0 (string1, string2) != 0) + return FALSE; + } + + return TRUE; +} diff --git a/src/core/gcal-event.h b/src/core/gcal-event.h index b4f034f3..ced69910 100644 --- a/src/core/gcal-event.h +++ b/src/core/gcal-event.h @@ -171,6 +171,21 @@ gboolean gcal_event_schedule_equal (GcalEvent gboolean gcal_event_strings_equal (GcalEvent *event1, GcalEvent *event2); +gchar* gcal_event_dup_conference_url (GcalEvent *self); + +void gcal_event_add_conference (GcalEvent *self, + const gchar *url, + const gchar *label); + +void gcal_event_remove_conference (GcalEvent *self, + const gchar *url); + +void gcal_event_copy_conferences (GcalEvent *self, + GcalEvent *event); + +gboolean gcal_event_conferences_equal (GcalEvent *event1, + GcalEvent *event2); + G_END_DECLS #endif /* GCAL_EVENT_H */ diff --git a/src/federated/gcal-federated-core.c b/src/federated/gcal-federated-core.c index 6263bed7..a950bd9f 100644 --- a/src/federated/gcal-federated-core.c +++ b/src/federated/gcal-federated-core.c @@ -147,6 +147,64 @@ load_cores (const gchar *config_dir) return g_steal_pointer (&cores); } +/* + * Looks for the Core whose Nextcloud is at @host + */ +static CoreConf * +find_core_conf (const gchar *config_dir, + const gchar *host) +{ + g_autoptr (GPtrArray) cores = NULL; + + if (!host) + return NULL; + + cores = load_cores (config_dir); + + for (guint i = 0; i < cores->len; i++) + { + CoreConf *conf = g_ptr_array_index (cores, i); + g_autofree gchar *nextcloud_host = g_strconcat ("nextcloud.", conf->domain, NULL); + + if (g_ascii_strcasecmp (host, nextcloud_host) == 0) + return g_ptr_array_steal_index (cores, i); + } + + return NULL; +} + +#ifdef ENABLE_FEDERATED_CORE +/* + * Looks for the Core whose Nextcloud @calendar is on + */ +static CoreConf * +find_calendar_core_conf (GcalCalendar *calendar, + ESourceWebdav **out_webdav) +{ + g_autofree gchar *config_dir = NULL; + g_autoptr (GUri) uri = NULL; + ESourceWebdav *webdav; + ESource *source; + + source = gcal_calendar_get_source (calendar); + if (!source || !e_source_has_extension (source, E_SOURCE_EXTENSION_WEBDAV_BACKEND)) + return NULL; + + webdav = e_source_get_extension (source, E_SOURCE_EXTENSION_WEBDAV_BACKEND); + uri = e_source_webdav_dup_uri (webdav); + if (!uri) + return NULL; + + if (out_webdav) + *out_webdav = webdav; + + /* CoreDesktop writes to ~/.config, even where XDG_CONFIG_HOME is elsewhere (e.g. Flatpak) */ + config_dir = g_build_filename (g_get_home_dir (), ".config", "coredesktop", NULL); + + return find_core_conf (config_dir, g_uri_get_host (uri)); +} +#endif + /** * gcal_federated_core_find_core: * @config_dir: CoreDesktop's configuration directory @@ -164,32 +222,75 @@ gcal_federated_core_find_core (const gchar *config_dir, gchar **out_email, gchar **out_name) { - g_autoptr (GPtrArray) cores = NULL; + g_autoptr (CoreConf) conf = NULL; g_return_val_if_fail (config_dir != NULL, FALSE); - if (!host) + conf = find_core_conf (config_dir, host); + if (!conf) return FALSE; - cores = load_cores (config_dir); + if (out_email) + *out_email = g_strdup (conf->email); + if (out_name) + *out_name = g_strdup (conf->name); - for (guint i = 0; i < cores->len; i++) - { - CoreConf *conf = g_ptr_array_index (cores, i); - g_autofree gchar *nextcloud_host = g_strconcat ("nextcloud.", conf->domain, NULL); + return TRUE; +} - if (g_ascii_strcasecmp (host, nextcloud_host) != 0) - continue; +/** + * gcal_federated_core_find_jitsi_server: + * @config_dir: CoreDesktop's configuration directory + * @host: the host name of a CalDAV server + * + * Looks for the Jitsi server of the Core whose Nextcloud is at @host. Every + * Core runs Jitsi at jitsi.. + * + * Returns: (transfer full) (nullable): the address of the Jitsi server, or + * %NULL if @host isn't the Nextcloud of a Core set up on this computer + */ +gchar * +gcal_federated_core_find_jitsi_server (const gchar *config_dir, + const gchar *host) +{ + g_autoptr (CoreConf) conf = NULL; - if (out_email) - *out_email = g_strdup (conf->email); - if (out_name) - *out_name = g_strdup (conf->name); + g_return_val_if_fail (config_dir != NULL, NULL); - return TRUE; - } + conf = find_core_conf (config_dir, host); + if (!conf) + return NULL; - return FALSE; + return g_strconcat ("https://jitsi.", conf->domain, NULL); +} + +/** + * gcal_federated_core_get_jitsi_server: + * @calendar: a #GcalCalendar + * + * Checks whether @calendar is on the Nextcloud of a Federated Computer Core + * set up on this computer, whose events can have a video conference on the + * Core's Jitsi server. + * + * Returns: (transfer full) (nullable): the address of the Core's Jitsi + * server, or %NULL if @calendar isn't on a Core + */ +gchar * +gcal_federated_core_get_jitsi_server (GcalCalendar *calendar) +{ +#ifdef ENABLE_FEDERATED_CORE + g_autoptr (CoreConf) conf = NULL; + + g_return_val_if_fail (GCAL_IS_CALENDAR (calendar), NULL); + + conf = find_calendar_core_conf (calendar, NULL); + if (!conf) + return NULL; + + return g_strconcat ("https://jitsi.", conf->domain, NULL); +#else + return NULL; +#endif } /** @@ -210,30 +311,17 @@ gcal_federated_core_get_organizer (GcalCalendar *calendar, gchar **out_name) { #ifdef ENABLE_FEDERATED_CORE - g_autofree gchar *config_dir = NULL; g_autofree gchar *server_email = NULL; - g_autofree gchar *core_email = NULL; - g_autofree gchar *core_name = NULL; - g_autoptr (GUri) uri = NULL; - ESourceWebdav *webdav; - ESource *source; + g_autoptr (CoreConf) conf = NULL; + ESourceWebdav *webdav = NULL; g_return_val_if_fail (GCAL_IS_CALENDAR (calendar), FALSE); if (gcal_calendar_is_read_only (calendar)) return FALSE; - source = gcal_calendar_get_source (calendar); - if (!source || !e_source_has_extension (source, E_SOURCE_EXTENSION_WEBDAV_BACKEND)) - return FALSE; - - webdav = e_source_get_extension (source, E_SOURCE_EXTENSION_WEBDAV_BACKEND); - uri = e_source_webdav_dup_uri (webdav); - - /* CoreDesktop writes to ~/.config, even where XDG_CONFIG_HOME is elsewhere (e.g. Flatpak) */ - config_dir = g_build_filename (g_get_home_dir (), ".config", "coredesktop", NULL); - - if (!uri || !gcal_federated_core_find_core (config_dir, g_uri_get_host (uri), &core_email, &core_name)) + conf = find_calendar_core_conf (calendar, &webdav); + if (!conf) return FALSE; /* @@ -244,9 +332,9 @@ gcal_federated_core_get_organizer (GcalCalendar *calendar, server_email = e_source_webdav_dup_email_address (webdav); if (out_email) - *out_email = (server_email && *server_email) ? g_steal_pointer (&server_email) : g_steal_pointer (&core_email); + *out_email = (server_email && *server_email) ? g_steal_pointer (&server_email) : g_strdup (conf->email); if (out_name) - *out_name = g_steal_pointer (&core_name); + *out_name = g_strdup (conf->name); return TRUE; #else diff --git a/src/federated/gcal-federated-core.h b/src/federated/gcal-federated-core.h index a00bc5c7..b1b14e64 100644 --- a/src/federated/gcal-federated-core.h +++ b/src/federated/gcal-federated-core.h @@ -31,4 +31,9 @@ gboolean gcal_federated_core_find_core (const gchar gchar **out_email, gchar **out_name); +gchar* gcal_federated_core_get_jitsi_server (GcalCalendar *calendar); + +gchar* gcal_federated_core_find_jitsi_server (const gchar *config_dir, + const gchar *host); + G_END_DECLS diff --git a/src/federated/gcal-jitsi.c b/src/federated/gcal-jitsi.c new file mode 100644 index 00000000..c61bc77b --- /dev/null +++ b/src/federated/gcal-jitsi.c @@ -0,0 +1,452 @@ +/* gcal-jitsi.c + * + * Copyright (C) 2026 David Young + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#define G_LOG_DOMAIN "GcalJitsi" + +#include "config.h" + +#include "gcal-jitsi.h" + +#include +#include +#include +#include + +/** + * SECTION:gcal-jitsi + * @short_description: Jitsi video conferences + * @title:GcalJitsi + * + * Every Federated Computer Core runs Jitsi, so an event on a Core calendar + * can have a video conference in a Jitsi room on the Core. Anyone who knows + * a room's name can join it, so the names are made unguessable. + * + * The room's link goes in the event's description, which every mail client + * shows in the invitation, and in its iCalendar CONFERENCE property + * (RFC 7986) for the calendar apps that read it. + */ + +/* + * Each list has 64 words, so that a random byte picks one without bias. + * Together they make names like BraveOttersSing. + */ +static const gchar * const adjectives[] = { + "Amber", "Ancient", "Autumn", "Bold", "Brave", "Bright", "Calm", "Clever", + "Cosmic", "Crimson", "Curious", "Daring", "Dusty", "Eager", "Early", "Electric", + "Emerald", "Fancy", "Gentle", "Gilded", "Golden", "Grand", "Happy", "Hidden", + "Honest", "Humble", "Icy", "Jolly", "Keen", "Kind", "Lively", "Lucky", + "Lunar", "Mellow", "Merry", "Misty", "Modest", "Noble", "Nimble", "Olive", + "Patient", "Polar", "Proud", "Quiet", "Rapid", "Rosy", "Royal", "Rustic", + "Silver", "Sleepy", "Smooth", "Solar", "Sunny", "Swift", "Tidy", "Tiny", + "Velvet", "Vivid", "Warm", "Wild", "Wise", "Witty", "Young", "Zesty", +}; + +static const gchar * const nouns[] = { + "Apples", "Badgers", "Beacons", "Bears", "Bees", "Birches", "Bridges", "Brooks", + "Cabins", "Canyons", "Cedars", "Clouds", "Comets", "Coves", "Daisies", "Dolphins", + "Eagles", "Ferns", "Fjords", "Foxes", "Gardens", "Geysers", "Glaciers", "Harbors", + "Hawks", "Herons", "Hills", "Islands", "Kites", "Lagoons", "Lanterns", "Lemons", + "Lynxes", "Maples", "Meadows", "Moons", "Oaks", "Orchards", "Otters", "Owls", + "Pebbles", "Pines", "Planets", "Ponds", "Puffins", "Ravens", "Rivers", "Robins", + "Sails", "Seals", "Sparrows", "Stars", "Streams", "Swans", "Tigers", "Trails", + "Tulips", "Valleys", "Violins", "Waves", "Whales", "Willows", "Wolves", "Wrens", +}; + +static const gchar * const verbs[] = { + "Bloom", "Bounce", "Breathe", "Chatter", "Climb", "Dance", "Dream", "Drift", + "Explore", "Float", "Flourish", "Fly", "Gather", "Glide", "Glow", "Grow", + "Hop", "Hum", "Jump", "Laugh", "Leap", "Linger", "Listen", "March", + "Meander", "Mingle", "Nap", "Paddle", "Ponder", "Prance", "Race", "Ramble", + "Rest", "Ripple", "Rise", "Roam", "Roll", "Run", "Sail", "Shimmer", + "Shine", "Sing", "Skate", "Sketch", "Smile", "Soar", "Sparkle", "Spin", + "Splash", "Sprint", "Stroll", "Swim", "Swing", "Swirl", "Thrive", "Travel", + "Tumble", "Twinkle", "Waltz", "Wander", "Whistle", "Wink", "Yodel", "Zoom", +}; + +G_STATIC_ASSERT (G_N_ELEMENTS (adjectives) == 64); +G_STATIC_ASSERT (G_N_ELEMENTS (nouns) == 64); +G_STATIC_ASSERT (G_N_ELEMENTS (verbs) == 64); + +/* 32 letters and digits, leaving out the easily confused l, o, 0 and 1 */ +static const gchar suffix_chars[] = "abcdefghijkmnpqrstuvwxyz23456789"; + +G_STATIC_ASSERT (sizeof (suffix_chars) - 1 == 32); + +#define SUFFIX_LENGTH 8 + +/* Hosts of Jitsi servers other than the Cores' jitsi. */ +static const gchar * const jitsi_hosts[] = { + "meet.jit.si", + "8x8.vc", +}; + +static gboolean +fill_random (guint8 *buffer, + gsize length) +{ + gsize filled = 0; + + while (filled < length) + { + gssize n = getrandom (buffer + filled, length - filled, 0); + + if (n < 0) + { + if (errno == EINTR) + continue; + + g_warning ("Could not get random bytes: %s", g_strerror (errno)); + return FALSE; + } + + filled += n; + } + + return TRUE; +} + +static gboolean +is_url_end (gchar c) +{ + return c == '\0' || g_ascii_isspace (c) || strchr ("<>\"'", c) != NULL; +} + +/** + * gcal_jitsi_generate_room_name: + * + * Makes up a name for a new Jitsi room that's easy to read out, like + * BraveOttersSing-k3f9x2m7. Three words and a suffix give 58 random bits, + * which nobody will guess. + * + * Returns: (transfer full) (nullable): a room name, or %NULL if no random + * numbers were available + */ +gchar * +gcal_jitsi_generate_room_name (void) +{ + guint8 random[3 + SUFFIX_LENGTH]; + GString *name; + + if (!fill_random (random, sizeof (random))) + return NULL; + + name = g_string_new (NULL); + g_string_append (name, adjectives[random[0] & 63]); + g_string_append (name, nouns[random[1] & 63]); + g_string_append (name, verbs[random[2] & 63]); + g_string_append_c (name, '-'); + + for (gsize i = 0; i < SUFFIX_LENGTH; i++) + g_string_append_c (name, suffix_chars[random[3 + i] & 31]); + + memset (random, 0, sizeof (random)); + + return g_string_free (name, FALSE); +} + +/** + * gcal_jitsi_build_room_url: + * @server: the Jitsi server, e.g. https://jitsi.example.com + * @room_name: the name of a room + * + * Returns: (transfer full): the address of the room @room_name on @server + */ +gchar * +gcal_jitsi_build_room_url (const gchar *server, + const gchar *room_name) +{ + g_autofree gchar *escaped = NULL; + gsize len; + + g_return_val_if_fail (server != NULL, NULL); + g_return_val_if_fail (room_name != NULL && *room_name, NULL); + + escaped = g_uri_escape_string (room_name, NULL, FALSE); + len = strlen (server); + + if (len > 0 && server[len - 1] == '/') + return g_strconcat (server, escaped, NULL); + + return g_strconcat (server, "/", escaped, NULL); +} + +/** + * gcal_jitsi_is_room_url: + * @url: (nullable): a URL + * + * Checks whether @url is a room on a Jitsi server: a Core's + * jitsi., or one of the public Jitsi servers. + * + * Returns: %TRUE if @url is a Jitsi room + */ +gboolean +gcal_jitsi_is_room_url (const gchar *url) +{ + g_autoptr (GUri) uri = NULL; + const gchar *scheme; + const gchar *host; + const gchar *path; + + if (!url) + return FALSE; + + uri = g_uri_parse (url, G_URI_FLAGS_NONE, NULL); + if (!uri) + return FALSE; + + scheme = g_uri_get_scheme (uri); + host = g_uri_get_host (uri); + path = g_uri_get_path (uri); + + if (g_ascii_strcasecmp (scheme, "https") != 0 && g_ascii_strcasecmp (scheme, "http") != 0) + return FALSE; + + /* The server's front page isn't a room */ + if (!host || !path || !path[0] || g_str_equal (path, "/")) + return FALSE; + + if (g_ascii_strncasecmp (host, "jitsi.", strlen ("jitsi.")) == 0 && strchr (host + strlen ("jitsi."), '.')) + return TRUE; + + for (gsize i = 0; i < G_N_ELEMENTS (jitsi_hosts); i++) + { + if (g_ascii_strcasecmp (host, jitsi_hosts[i]) == 0) + return TRUE; + } + + return FALSE; +} + +/** + * gcal_jitsi_find_room_url: + * @text: (nullable): some text, like the description of an event + * + * Looks for the address of a Jitsi room in @text. + * + * Returns: (transfer full) (nullable): the first Jitsi room in @text, or %NULL + */ +gchar * +gcal_jitsi_find_room_url (const gchar *text) +{ + const gchar *start; + + if (!text) + return NULL; + + for (start = text; (start = strstr (start, "http")) != NULL; start++) + { + g_autofree gchar *url = NULL; + const gchar *end; + + if (g_ascii_strncasecmp (start, "https://", strlen ("https://")) != 0 && + g_ascii_strncasecmp (start, "http://", strlen ("http://")) != 0) + continue; + + for (end = start; !is_url_end (*end); end++) + ; + + /* Punctuation after a link ends the sentence, not the link */ + while (end > start && strchr (".,;:!?)]", end[-1])) + end--; + + url = g_strndup (start, end - start); + + if (gcal_jitsi_is_room_url (url)) + return g_steal_pointer (&url); + } + + return NULL; +} + +/** + * gcal_jitsi_find_event_conference: + * @event: a #GcalEvent + * + * Finds the video conference of @event, so that it isn't offered a second + * one. That's the event's CONFERENCE property, if it has one, or else a + * Jitsi room in its description or location. + * + * Returns: (transfer full) (nullable): the conference's URL, or %NULL + */ +gchar * +gcal_jitsi_find_event_conference (GcalEvent *event) +{ + gchar *url; + + g_return_val_if_fail (GCAL_IS_EVENT (event), NULL); + + url = gcal_event_dup_conference_url (event); + + if (!url) + url = gcal_jitsi_find_room_url (gcal_event_get_description (event)); + + if (!url) + url = gcal_jitsi_find_room_url (gcal_event_get_location (event)); + + return url; +} + +/** + * gcal_jitsi_add_to_description: + * @description: (nullable): the description of an event + * @url: the URL of a video conference + * + * Adds a line inviting people to join the conference at @url to the end of + * @description, unless @url is in it already. + * + * Returns: (transfer full): the new description + */ +gchar * +gcal_jitsi_add_to_description (const gchar *description, + const gchar *url) +{ + g_autofree gchar *text = NULL; + g_autofree gchar *line = NULL; + + g_return_val_if_fail (url != NULL, NULL); + + if (description && strstr (description, url)) + return g_strdup (description); + + /* Translators: %s is the address of a video conference room */ + line = g_strdup_printf (_("Join the video conference: %s"), url); + + text = g_strchomp (g_strdup (description ? description : "")); + + if (!*text) + return g_steal_pointer (&line); + + return g_strconcat (text, "\n\n", line, NULL); +} + +/** + * gcal_jitsi_remove_from_description: + * @description: (nullable): the description of an event + * @url: the URL of a video conference + * + * Takes the lines that mention @url out of @description. + * + * Returns: (transfer full): the new description + */ +gchar * +gcal_jitsi_remove_from_description (const gchar *description, + const gchar *url) +{ + g_auto (GStrv) lines = NULL; + GString *text; + + g_return_val_if_fail (url != NULL, NULL); + + if (!description) + return g_strdup (""); + + if (!strstr (description, url)) + return g_strdup (description); + + lines = g_strsplit (description, "\n", -1); + text = g_string_new (NULL); + + for (gsize i = 0; lines[i]; i++) + { + if (strstr (lines[i], url)) + { + /* Don't leave two blank lines where the link was */ + if (text->len >= 2 && g_str_has_suffix (text->str, "\n\n") && (!lines[i + 1] || !*lines[i + 1])) + g_string_truncate (text, text->len - 1); + + continue; + } + + g_string_append (text, lines[i]); + g_string_append_c (text, '\n'); + } + + return g_strchomp (g_string_free (text, FALSE)); +} + +/** + * gcal_jitsi_add_conference: + * @event: a #GcalEvent + * @url: the URL of a Jitsi room + * + * Adds the video conference at @url to @event: a CONFERENCE property, and + * a link in the description for the people invited. + */ +void +gcal_jitsi_add_conference (GcalEvent *event, + const gchar *url) +{ + g_autofree gchar *description = NULL; + + g_return_if_fail (GCAL_IS_EVENT (event)); + g_return_if_fail (url != NULL); + + gcal_event_add_conference (event, url, _("Jitsi Video Conference")); + + description = gcal_jitsi_add_to_description (gcal_event_get_description (event), url); + gcal_event_set_description (event, description); +} + +/** + * gcal_jitsi_remove_conference: + * @event: a #GcalEvent + * @url: the URL of a video conference + * + * Takes the video conference at @url out of @event's CONFERENCE properties + * and description. + */ +void +gcal_jitsi_remove_conference (GcalEvent *event, + const gchar *url) +{ + g_autofree gchar *description = NULL; + + g_return_if_fail (GCAL_IS_EVENT (event)); + g_return_if_fail (url != NULL); + + gcal_event_remove_conference (event, url); + + description = gcal_jitsi_remove_from_description (gcal_event_get_description (event), url); + gcal_event_set_description (event, description); +} + +/** + * gcal_jitsi_ensure_description: + * @event: a #GcalEvent + * + * Puts the link to @event's Jitsi conference back in its description, if it + * was taken out, so that the people invited get it. Mail clients show the + * description, but few show the CONFERENCE property. + */ +void +gcal_jitsi_ensure_description (GcalEvent *event) +{ + g_autofree gchar *description = NULL; + g_autofree gchar *url = NULL; + + g_return_if_fail (GCAL_IS_EVENT (event)); + + url = gcal_event_dup_conference_url (event); + + /* Other apps' conferences are their business */ + if (!gcal_jitsi_is_room_url (url)) + return; + + description = gcal_jitsi_add_to_description (gcal_event_get_description (event), url); + gcal_event_set_description (event, description); +} diff --git a/src/federated/gcal-jitsi.h b/src/federated/gcal-jitsi.h new file mode 100644 index 00000000..f7fb8a8a --- /dev/null +++ b/src/federated/gcal-jitsi.h @@ -0,0 +1,50 @@ +/* gcal-jitsi.h + * + * Copyright (C) 2026 David Young + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include "gcal-event.h" + +G_BEGIN_DECLS + +gchar* gcal_jitsi_generate_room_name (void); + +gchar* gcal_jitsi_build_room_url (const gchar *server, + const gchar *room_name); + +gboolean gcal_jitsi_is_room_url (const gchar *url); + +gchar* gcal_jitsi_find_room_url (const gchar *text); + +gchar* gcal_jitsi_find_event_conference (GcalEvent *event); + +gchar* gcal_jitsi_add_to_description (const gchar *description, + const gchar *url); + +gchar* gcal_jitsi_remove_from_description (const gchar *description, + const gchar *url); + +void gcal_jitsi_add_conference (GcalEvent *event, + const gchar *url); + +void gcal_jitsi_remove_conference (GcalEvent *event, + const gchar *url); + +void gcal_jitsi_ensure_description (GcalEvent *event); + +G_END_DECLS diff --git a/src/federated/meson.build b/src/federated/meson.build index 7168f1ed..fe4f7a0d 100644 --- a/src/federated/meson.build +++ b/src/federated/meson.build @@ -2,4 +2,5 @@ calendar_incs += include_directories('.') sources += files( 'gcal-federated-core.c', + 'gcal-jitsi.c', ) diff --git a/src/gui/event-editor/gcal-event-editor-dialog.c b/src/gui/event-editor/gcal-event-editor-dialog.c index 0811f487..266f3fe3 100644 --- a/src/gui/event-editor/gcal-event-editor-dialog.c +++ b/src/gui/event-editor/gcal-event-editor-dialog.c @@ -29,6 +29,7 @@ #include "gcal-event-editor-section.h" #include "gcal-event.h" #include "gcal-federated-core.h" +#include "gcal-jitsi.h" #include "gcal-notes-section.h" #include "gcal-reminders-section.h" #include "gcal-schedule-section.h" @@ -171,6 +172,29 @@ update_invite_state (GcalEventEditorDialog *self) gtk_widget_set_visible (GTK_WIDGET (self->attendees_section), organizer != NULL || can_invite); } +/* + * Events on the calendars of a Federated Computer Core can have a video + * conference on the Core's Jitsi server. + */ +static void +update_conference_state (GcalEventEditorDialog *self) +{ + g_autofree gchar *server = NULL; + GcalCalendar *calendar; + + if (!self->edited_event) + return; + + calendar = gcal_calendar_combo_row_get_calendar (self->calendar_combo_row); + if (!calendar) + calendar = gcal_event_get_calendar (self->edited_event); + + if (self->writable && calendar) + server = gcal_federated_core_get_jitsi_server (calendar); + + gcal_summary_section_set_conference_server (GCAL_SUMMARY_SECTION (self->summary_section), server); +} + static void set_event (GcalEventEditorDialog *self, GcalEvent *event, @@ -209,6 +233,7 @@ set_event (GcalEventEditorDialog *self, set_writable (self, !gcal_calendar_is_read_only (calendar)); update_invite_state (self); + update_conference_state (self); gtk_widget_set_visible (self->cancel_button, self->writable); @@ -280,6 +305,10 @@ apply_event_properties_to_template_event (GcalEvent *template_event, if (!gcal_event_attendees_equal (template_event, event)) gcal_event_copy_attendees (template_event, event); + + /* A conference added to one occurrence is for the whole series */ + if (!gcal_event_conferences_equal (template_event, event)) + gcal_event_copy_conferences (template_event, event); } static void @@ -320,7 +349,7 @@ save_event (GcalEventEditorDialog *self, GcalContext *context; GcalManager *manager; - gboolean calendar_equal, alarms_equal, schedule_equal, strings_equal, attendees_equal, event_equal; + gboolean calendar_equal, alarms_equal, schedule_equal, strings_equal, attendees_equal, conferences_equal, event_equal; GCAL_ENTRY; @@ -337,12 +366,16 @@ save_event (GcalEventEditorDialog *self, gcal_event_set_organizer (self->edited_event, NULL, NULL); } + /* The people invited get the conference's link in the description */ + gcal_jitsi_ensure_description (self->edited_event); + calendar_equal = selected_calendar && calendar == selected_calendar; alarms_equal = gcal_event_alarms_equal (self->event, self->edited_event); schedule_equal = gcal_event_schedule_equal (self->event, self->edited_event); strings_equal = gcal_event_strings_equal (self->event, self->edited_event); attendees_equal = gcal_event_attendees_equal (self->event, self->edited_event); - event_equal = calendar_equal && alarms_equal && schedule_equal && strings_equal && attendees_equal; + conferences_equal = gcal_event_conferences_equal (self->event, self->edited_event); + event_equal = calendar_equal && alarms_equal && schedule_equal && strings_equal && attendees_equal && conferences_equal; if (self->event_is_new) { @@ -428,6 +461,7 @@ static void on_calendar_selected_cb (GcalEventEditorDialog *self) { update_invite_state (self); + update_conference_state (self); } static void diff --git a/src/gui/event-editor/gcal-notes-section.c b/src/gui/event-editor/gcal-notes-section.c index f050be6c..a7eebc1e 100644 --- a/src/gui/event-editor/gcal-notes-section.c +++ b/src/gui/event-editor/gcal-notes-section.c @@ -29,14 +29,64 @@ struct _GcalNotesSection GtkTextView *notes_text; GtkLabel *placeholder; + + GcalEvent *event; + gulong description_changed_id; }; G_DEFINE_FINAL_TYPE (GcalNotesSection, gcal_notes_section, GCAL_TYPE_EVENT_EDITOR_SECTION) +/* + * Auxiliary methods + */ + +static void on_notes_text_changed_cb (GcalNotesSection *self); + +static void +set_text (GcalNotesSection *self, + const gchar *text) +{ + GtkTextBuffer *buffer = gtk_text_view_get_buffer (self->notes_text); + + g_signal_handlers_block_by_func (buffer, on_notes_text_changed_cb, self); + gtk_text_buffer_set_text (buffer, text, -1); + g_signal_handlers_unblock_by_func (buffer, on_notes_text_changed_cb, self); +} + +static void +disconnect_event (GcalNotesSection *self) +{ + if (self->event) + g_clear_signal_handler (&self->description_changed_id, self->event); + + self->description_changed_id = 0; + g_clear_weak_pointer (&self->event); +} + + /* * Callbacks */ +/* + * The description changes as people type here, but also elsewhere, e.g. + * when a video conference's link is added to it. + */ +static void +on_event_description_changed_cb (GcalNotesSection *self) +{ + g_autofree char *text = NULL; + GtkTextIter start, end; + GtkTextBuffer *buffer; + + buffer = gtk_text_view_get_buffer (self->notes_text); + gtk_text_buffer_get_bounds (buffer, &start, &end); + text = gtk_text_buffer_get_text (buffer, &start, &end, TRUE); + + if (g_strcmp0 (text, gcal_event_get_description (self->event)) != 0) + set_text (self, gcal_event_get_description (self->event)); +} + static void on_notes_text_changed_cb (GcalNotesSection *self) { @@ -102,20 +152,20 @@ gcal_notes_section_event_set_cb (GcalEventEditorSection *section, GcalEvent *event) { GcalNotesSection *self = GCAL_NOTES_SECTION (section); - GtkTextIter start, end; - GtkTextBuffer *buffer; - buffer = gtk_text_view_get_buffer (self->notes_text); - gtk_text_buffer_get_bounds (buffer, &start, &end); + disconnect_event (self); - g_signal_handlers_block_by_func (buffer, on_notes_text_changed_cb, self); + set_text (self, event ? gcal_event_get_description (event) : ""); if (event) - gtk_text_buffer_set_text (buffer, gcal_event_get_description (event), -1); - else - gtk_text_buffer_delete (buffer, &start, &end); - - g_signal_handlers_unblock_by_func (buffer, on_notes_text_changed_cb, self); + { + g_set_weak_pointer (&self->event, event); + self->description_changed_id = g_signal_connect_object (event, + "notify::description", + G_CALLBACK (on_event_description_changed_cb), + self, + G_CONNECT_SWAPPED); + } } @@ -123,12 +173,23 @@ gcal_notes_section_event_set_cb (GcalEventEditorSection *section, * GObject overrides */ +static void +gcal_notes_section_dispose (GObject *object) +{ + disconnect_event (GCAL_NOTES_SECTION (object)); + + G_OBJECT_CLASS (gcal_notes_section_parent_class)->dispose (object); +} + static void gcal_notes_section_class_init (GcalNotesSectionClass *klass) { + GObjectClass *object_class = G_OBJECT_CLASS (klass); GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); GcalEventEditorSectionClass *section_class = GCAL_EVENT_EDITOR_SECTION_CLASS (klass); + object_class->dispose = gcal_notes_section_dispose; + section_class->event_set_cb = gcal_notes_section_event_set_cb; gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/calendar/ui/event-editor/gcal-notes-section.ui"); diff --git a/src/gui/event-editor/gcal-summary-section.blp b/src/gui/event-editor/gcal-summary-section.blp index 6ff56101..a9848c7e 100644 --- a/src/gui/event-editor/gcal-summary-section.blp +++ b/src/gui/event-editor/gcal-summary-section.blp @@ -13,4 +13,59 @@ template $GcalSummarySection: $GcalEventEditorSection { notify::text => $on_location_entry_changed_cb(template); editable: bind template.dialog as <$GcalEventEditorDialog>.writable; } + + Adw.ButtonRow add_conference_row { + visible: false; + title: _("Add _Video Conference"); + use-underline: true; + start-icon-name: "camera-web-symbolic"; + tooltip-text: _("Make a room on your Core’s Jitsi server, and send its link to the people invited"); + activated => $on_add_conference_row_activated_cb(template); + } + + Adw.ActionRow conference_row { + visible: false; + title: _("Video Conference"); + use-markup: false; + subtitle-selectable: true; + + [prefix] + Image { + icon-name: "camera-web-symbolic"; + accessible-role: presentation; + } + + [suffix] + Button copy_conference_button { + valign: center; + icon-name: "edit-copy-symbolic"; + tooltip-text: _("Copy Link"); + clicked => $on_copy_conference_button_clicked_cb(template); + + accessibility { + label: _("Copy Link"); + } + + styles [ + "flat", + ] + } + + [suffix] + Button remove_conference_button { + valign: center; + icon-name: "edit-delete-symbolic"; + tooltip-text: _("Remove Video Conference"); + visible: bind template.dialog as <$GcalEventEditorDialog>.writable; + clicked => $on_remove_conference_button_clicked_cb(template); + + accessibility { + label: _("Remove Video Conference"); + } + + styles [ + "flat", + ] + } + } } diff --git a/src/gui/event-editor/gcal-summary-section.c b/src/gui/event-editor/gcal-summary-section.c index 58a541a1..5d624079 100644 --- a/src/gui/event-editor/gcal-summary-section.c +++ b/src/gui/event-editor/gcal-summary-section.c @@ -21,6 +21,7 @@ #define G_LOG_DOMAIN "GcalSummarySection" #include "gcal-debug.h" +#include "gcal-jitsi.h" #include "gcal-summary-section.h" #include "gcal-utils.h" @@ -32,15 +33,155 @@ struct _GcalSummarySection AdwEntryRow *summary_entry; AdwEntryRow *location_entry; + AdwButtonRow *add_conference_row; + AdwActionRow *conference_row; + GtkButton *copy_conference_button; + GtkButton *remove_conference_button; + + /* The Jitsi server of the event's Core, if a conference can be added */ + gchar *conference_server; + + /* The event's conference */ + gchar *conference_url; + + guint copied_timeout_id; }; G_DEFINE_FINAL_TYPE (GcalSummarySection, gcal_summary_section, GCAL_TYPE_EVENT_EDITOR_SECTION) +/* + * Auxiliary methods + */ + +static void +update_conference_rows (GcalSummarySection *self) +{ + gboolean has_conference = self->conference_url != NULL; + + adw_action_row_set_subtitle (self->conference_row, has_conference ? self->conference_url : ""); + + gtk_widget_set_visible (GTK_WIDGET (self->conference_row), has_conference); + gtk_widget_set_visible (GTK_WIDGET (self->add_conference_row), !has_conference && self->conference_server); +} + +static void +announce (GcalSummarySection *self, + const gchar *message) +{ + gtk_accessible_announce (GTK_ACCESSIBLE (self), message, GTK_ACCESSIBLE_ANNOUNCEMENT_PRIORITY_MEDIUM); +} + +static void +reset_copy_button (GcalSummarySection *self) +{ + g_clear_handle_id (&self->copied_timeout_id, g_source_remove); + + gtk_button_set_icon_name (self->copy_conference_button, "edit-copy-symbolic"); + gtk_widget_set_tooltip_text (GTK_WIDGET (self->copy_conference_button), _("Copy Link")); +} + + /* * Callbacks */ +static gboolean +on_copied_timeout_cb (gpointer user_data) +{ + GcalSummarySection *self = GCAL_SUMMARY_SECTION (user_data); + + self->copied_timeout_id = 0; + reset_copy_button (self); + + return G_SOURCE_REMOVE; +} + +static void +on_add_conference_row_activated_cb (GcalSummarySection *self) +{ + g_autofree gchar *room_name = NULL; + GcalEvent *event; + + event = gcal_event_editor_section_get_event (GCAL_EVENT_EDITOR_SECTION (self)); + + if (!event || !self->conference_server || self->conference_url) + return; + + room_name = gcal_jitsi_generate_room_name (); + if (!room_name) + return; + + self->conference_url = gcal_jitsi_build_room_url (self->conference_server, room_name); + + gcal_jitsi_add_conference (event, self->conference_url); + + update_conference_rows (self); + + /* The button that had the focus is gone */ + gtk_widget_grab_focus (GTK_WIDGET (self->copy_conference_button)); + announce (self, _("Video conference added")); +} + +static void +on_copy_conference_button_clicked_cb (GcalSummarySection *self) +{ + if (!self->conference_url) + return; + + gdk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (self)), self->conference_url); + + gtk_button_set_icon_name (self->copy_conference_button, "object-select-symbolic"); + gtk_widget_set_tooltip_text (GTK_WIDGET (self->copy_conference_button), _("Copied")); + + g_clear_handle_id (&self->copied_timeout_id, g_source_remove); + self->copied_timeout_id = g_timeout_add_seconds (2, on_copied_timeout_cb, self); + + announce (self, _("Link copied")); +} + +static void +on_remove_conference_button_clicked_cb (GcalSummarySection *self) +{ + g_autofree gchar *other_url = NULL; + g_autofree gchar *location = NULL; + GcalEvent *event; + + event = gcal_event_editor_section_get_event (GCAL_EVENT_EDITOR_SECTION (self)); + + if (!event || !self->conference_url) + return; + + gcal_jitsi_remove_conference (event, self->conference_url); + + /* The conference could have been in the location */ + location = g_strdup (gcal_event_get_location (event)); + if (location && g_str_equal (g_strstrip (location), self->conference_url)) + gtk_editable_set_text (GTK_EDITABLE (self->location_entry), ""); + + /* There could be another one, e.g. a different room in the location */ + other_url = gcal_jitsi_find_event_conference (event); + + /* A link amid other words in the location is the person's to edit */ + if (g_strcmp0 (other_url, self->conference_url) == 0) + g_clear_pointer (&other_url, g_free); + + g_free (self->conference_url); + self->conference_url = g_steal_pointer (&other_url); + + reset_copy_button (self); + update_conference_rows (self); + + if (gtk_widget_get_visible (GTK_WIDGET (self->add_conference_row))) + gtk_widget_grab_focus (GTK_WIDGET (self->add_conference_row)); + else if (self->conference_url) + gtk_widget_grab_focus (GTK_WIDGET (self->copy_conference_button)); + else + gtk_widget_grab_focus (GTK_WIDGET (self->location_entry)); + + announce (self, _("Video conference removed")); +} + static void on_summary_entry_text_changed_cb (GcalSummarySection *self) { @@ -107,6 +248,13 @@ gcal_summary_section_event_set_cb (GcalEventEditorSection *section, g_signal_handlers_unblock_by_func (self->location_entry, on_location_entry_changed_cb, self); gtk_widget_remove_css_class (GTK_WIDGET (self->summary_entry), "error"); + + g_clear_pointer (&self->conference_url, g_free); + if (event) + self->conference_url = gcal_jitsi_find_event_conference (event); + + reset_copy_button (self); + update_conference_rows (self); } @@ -137,12 +285,37 @@ gcal_summary_section_unmap (GtkWidget *widget) * GObject overrides */ +static void +gcal_summary_section_dispose (GObject *object) +{ + GcalSummarySection *self = GCAL_SUMMARY_SECTION (object); + + g_clear_handle_id (&self->copied_timeout_id, g_source_remove); + + G_OBJECT_CLASS (gcal_summary_section_parent_class)->dispose (object); +} + +static void +gcal_summary_section_finalize (GObject *object) +{ + GcalSummarySection *self = GCAL_SUMMARY_SECTION (object); + + g_clear_pointer (&self->conference_server, g_free); + g_clear_pointer (&self->conference_url, g_free); + + G_OBJECT_CLASS (gcal_summary_section_parent_class)->finalize (object); +} + static void gcal_summary_section_class_init (GcalSummarySectionClass *klass) { + GObjectClass *object_class = G_OBJECT_CLASS (klass); GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); GcalEventEditorSectionClass *section_class = GCAL_EVENT_EDITOR_SECTION_CLASS (klass); + object_class->dispose = gcal_summary_section_dispose; + object_class->finalize = gcal_summary_section_finalize; + widget_class->grab_focus = gcal_summary_section_grab_focus; widget_class->unmap = gcal_summary_section_unmap; @@ -150,11 +323,18 @@ gcal_summary_section_class_init (GcalSummarySectionClass *klass) gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/calendar/ui/event-editor/gcal-summary-section.ui"); + gtk_widget_class_bind_template_child (widget_class, GcalSummarySection, add_conference_row); + gtk_widget_class_bind_template_child (widget_class, GcalSummarySection, conference_row); + gtk_widget_class_bind_template_child (widget_class, GcalSummarySection, copy_conference_button); gtk_widget_class_bind_template_child (widget_class, GcalSummarySection, location_entry); + gtk_widget_class_bind_template_child (widget_class, GcalSummarySection, remove_conference_button); gtk_widget_class_bind_template_child (widget_class, GcalSummarySection, summary_entry); - gtk_widget_class_bind_template_callback (widget_class, on_summary_entry_text_changed_cb); + gtk_widget_class_bind_template_callback (widget_class, on_add_conference_row_activated_cb); + gtk_widget_class_bind_template_callback (widget_class, on_copy_conference_button_clicked_cb); gtk_widget_class_bind_template_callback (widget_class, on_location_entry_changed_cb); + gtk_widget_class_bind_template_callback (widget_class, on_remove_conference_button_clicked_cb); + gtk_widget_class_bind_template_callback (widget_class, on_summary_entry_text_changed_cb); } static void @@ -162,3 +342,23 @@ gcal_summary_section_init (GcalSummarySection *self) { gtk_widget_init_template (GTK_WIDGET (self)); } + +/** + * gcal_summary_section_set_conference_server: + * @self: a #GcalSummarySection + * @server: (nullable): the address of a Jitsi server + * + * Offers to add a video conference on @server to the event, unless it has + * one already. %NULL hides the offer, e.g. for calendars that aren't on a + * Federated Computer Core. + */ +void +gcal_summary_section_set_conference_server (GcalSummarySection *self, + const gchar *server) +{ + g_return_if_fail (GCAL_IS_SUMMARY_SECTION (self)); + + g_set_str (&self->conference_server, server); + + update_conference_rows (self); +} diff --git a/src/gui/event-editor/gcal-summary-section.h b/src/gui/event-editor/gcal-summary-section.h index 263ca401..4f569089 100644 --- a/src/gui/event-editor/gcal-summary-section.h +++ b/src/gui/event-editor/gcal-summary-section.h @@ -29,4 +29,7 @@ G_BEGIN_DECLS #define GCAL_TYPE_SUMMARY_SECTION (gcal_summary_section_get_type()) G_DECLARE_FINAL_TYPE (GcalSummarySection, gcal_summary_section, GCAL, SUMMARY_SECTION, GcalEventEditorSection) +void gcal_summary_section_set_conference_server (GcalSummarySection *self, + const gchar *server); + G_END_DECLS diff --git a/src/gui/gcal-event-popover.c b/src/gui/gcal-event-popover.c index 86f0b34d..3ce1e4e5 100644 --- a/src/gui/gcal-event-popover.c +++ b/src/gui/gcal-event-popover.c @@ -442,8 +442,25 @@ static void add_meeting (GcalEventPopover *self, const gchar *url) { + GtkWidget *child; GtkWidget *row; + /* The same conference can be in the CONFERENCE property, description and location */ + for (child = gtk_widget_get_first_child (GTK_WIDGET (self->meetings_listbox)); + child; + child = gtk_widget_get_next_sibling (child)) + { + g_autofree gchar *row_url = NULL; + + if (!GCAL_IS_MEETING_ROW (child)) + continue; + + g_object_get (child, "url", &row_url, NULL); + + if (g_strcmp0 (row_url, url) == 0) + return; + } + row = gcal_meeting_row_new (url); g_signal_connect (row, "join-meeting", G_CALLBACK (on_join_meeting_cb), self); gtk_list_box_append (self->meetings_listbox, row); @@ -478,12 +495,27 @@ set_up_location_label (GcalEventPopover *self) location = g_string_free (string, FALSE); } + /* + * A link in the location is shown as a meeting instead. A place stays, + * even when the meeting is in the description. + */ gtk_widget_set_visible (self->location_box, - location && g_utf8_strlen (location, -1) > 0 && - !gtk_widget_get_visible (GTK_WIDGET (self->meetings_box))); + !guri && location && g_utf8_strlen (location, -1) > 0); gtk_label_set_markup (self->location_label, location); } +static void +set_up_conference (GcalEventPopover *self) +{ + g_autofree gchar *url = NULL; + + url = gcal_event_dup_conference_url (self->event); + + /* Only links can be joined, not e.g. tel: dial-in numbers */ + if (url && (g_str_has_prefix (url, "https://") || g_str_has_prefix (url, "http://"))) + add_meeting (self, url); +} + static void set_up_description_label (GcalEventPopover *self) { @@ -514,6 +546,7 @@ set_event_internal (GcalEventPopover *self, gtk_label_set_label (self->summary_label, gcal_event_get_summary (event)); + set_up_conference (self); set_up_description_label (self); set_up_location_label (self); update_placeholder_label (self); diff --git a/src/utils/gcal-utils.c b/src/utils/gcal-utils.c index b3d45c51..3a94ef9f 100644 --- a/src/utils/gcal-utils.c +++ b/src/utils/gcal-utils.c @@ -26,6 +26,7 @@ #include "gcal-application.h" #include "gcal-context.h" #include "gcal-enums.h" +#include "gcal-jitsi.h" #include "gcal-utils.h" #include "gcal-event-widget.h" #include "gcal-view.h" @@ -805,7 +806,8 @@ gcal_util_extract_teams_section (const gchar *description, * @out_description: (nullable)(transfer full): return location for a parsed description * @out_meeting_url: (nullable)(transfer full): return location for the parsed meeting url * - * Parses @description for meeting URLs. + * Parses @description for meeting URLs: the sections that Google Meet and + * Microsoft Teams add to descriptions, and links to Jitsi rooms. */ void gcal_util_extract_meeting_url (const char *description, @@ -818,8 +820,9 @@ gcal_util_extract_meeting_url (const char *description, if (out_description) *out_description = g_strdup (description); + /* Jitsi rooms are a link in the description, not a section of their own */ if (out_meeting_url) - *out_meeting_url = NULL; + *out_meeting_url = gcal_jitsi_find_room_url (description); } } diff --git a/tests/meson.build b/tests/meson.build index b213dcc2..abdbed45 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -44,6 +44,7 @@ tests = [ 'utils', 'event-attendee', 'federated-core', + 'jitsi', ] foreach test : tests diff --git a/tests/test-event.c b/tests/test-event.c index c8cbe42c..836b8666 100644 --- a/tests/test-event.c +++ b/tests/test-event.c @@ -86,6 +86,16 @@ "RECURRENCE-ID:20180714T000000Z\n" \ "END:VEVENT\n" +#define STUB_EVENT_WITH_CONFERENCE "BEGIN:VEVENT\n" \ + "SUMMARY:Stub event\n" \ + "UID:example@uid\n" \ + "DTSTAMP:19970114T170000Z\n" \ + "DTSTART:20180714T170000Z\n" \ + "DTEND:20180715T035959Z\n" \ + "CONFERENCE;VALUE=URI;FEATURE=PHONE;LABEL=Dial in:tel:+1-555-0100\n" \ + "CONFERENCE;VALUE=URI;FEATURE=VIDEO:https://example.com/room\n" \ + "END:VEVENT\n" + /* * Auxiliary methods */ @@ -670,6 +680,124 @@ event_schedule_equal (void) /*********************************************************************************************************************/ +#define ROOM_URL "https://jitsi.example.com/BraveOttersSing-k3f9x2m7" + +static void +event_add_conference (void) +{ + g_autoptr (GcalEvent) reloaded = NULL; + g_autoptr (GcalEvent) event = NULL; + g_autoptr (GString) unfolded = NULL; + g_autoptr (GError) error = NULL; + g_autofree gchar *string = NULL; + g_autofree gchar *url = NULL; + + event = create_event_for_string (STUB_EVENT, &error); + g_assert_no_error (error); + + g_assert_null (gcal_event_dup_conference_url (event)); + + gcal_event_add_conference (event, ROOM_URL, "Jitsi"); + + /* Adding the same conference again doesn't duplicate it */ + gcal_event_add_conference (event, ROOM_URL, "Jitsi"); + + url = gcal_event_dup_conference_url (event); + g_assert_cmpstr (url, ==, ROOM_URL); + + string = e_cal_component_get_as_string (gcal_event_get_component (event)); + g_test_message ("%s", string); + + /* Unfold the long lines */ + unfolded = g_string_new (string); + g_string_replace (unfolded, "\r\n ", "", 0); + g_string_replace (unfolded, "\n ", "", 0); + g_free (string); + string = g_string_free (g_steal_pointer (&unfolded), FALSE); + + g_assert_nonnull (strstr (string, "CONFERENCE;")); + g_assert_nonnull (strstr (string, "VALUE=URI")); + g_assert_nonnull (strstr (string, "FEATURE=AUDIO,VIDEO")); + g_assert_nonnull (strstr (string, "LABEL=Jitsi")); + g_assert_nonnull (strstr (string, ":" ROOM_URL)); + g_assert_null (strstr (strstr (string, "CONFERENCE;") + 1, "CONFERENCE;")); + + /* It survives a round trip through the iCalendar text, as on a server */ + reloaded = create_event_for_string (string, &error); + g_assert_no_error (error); + g_assert_true (gcal_event_conferences_equal (event, reloaded)); + + g_clear_pointer (&url, g_free); + url = gcal_event_dup_conference_url (reloaded); + g_assert_cmpstr (url, ==, ROOM_URL); + + gcal_event_remove_conference (event, ROOM_URL); + g_assert_null (gcal_event_dup_conference_url (event)); + g_assert_false (gcal_event_conferences_equal (event, reloaded)); +} + +static void +event_remove_conference (void) +{ + g_autoptr (GcalEvent) event = NULL; + g_autoptr (GError) error = NULL; + g_autofree gchar *url = NULL; + + event = create_event_for_string (STUB_EVENT_WITH_CONFERENCE, &error); + g_assert_no_error (error); + + /* The first conference that has a URL */ + url = gcal_event_dup_conference_url (event); + g_assert_cmpstr (url, ==, "tel:+1-555-0100"); + g_clear_pointer (&url, g_free); + + /* Only the conference that's removed goes */ + gcal_event_remove_conference (event, "tel:+1-555-0100"); + url = gcal_event_dup_conference_url (event); + g_assert_cmpstr (url, ==, "https://example.com/room"); + g_clear_pointer (&url, g_free); + + gcal_event_remove_conference (event, "https://example.com/other-room"); + url = gcal_event_dup_conference_url (event); + g_assert_cmpstr (url, ==, "https://example.com/room"); + g_clear_pointer (&url, g_free); + + gcal_event_remove_conference (event, "https://example.com/room"); + g_assert_null (gcal_event_dup_conference_url (event)); +} + +static void +event_copy_conferences (void) +{ + g_autoptr (GcalEvent) with_conference = NULL; + g_autoptr (GcalEvent) clone = NULL; + g_autoptr (GcalEvent) event = NULL; + g_autoptr (GError) error = NULL; + g_autofree gchar *url = NULL; + + with_conference = create_event_for_string (STUB_EVENT_WITH_CONFERENCE, &error); + g_assert_no_error (error); + + event = create_event_for_string (STUB_EVENT, &error); + g_assert_no_error (error); + + gcal_event_add_conference (event, ROOM_URL, "Jitsi"); + g_assert_false (gcal_event_conferences_equal (event, with_conference)); + + /* Copying replaces the conferences, rather than adding to them */ + gcal_event_copy_conferences (event, with_conference); + g_assert_true (gcal_event_conferences_equal (event, with_conference)); + + url = gcal_event_dup_conference_url (event); + g_assert_cmpstr (url, ==, "tel:+1-555-0100"); + + /* The event editor works on a clone */ + clone = gcal_event_new_from_event (with_conference); + g_assert_true (gcal_event_conferences_equal (clone, with_conference)); +} + +/*********************************************************************************************************************/ + gint main (gint argc, gchar *argv[]) @@ -694,6 +822,9 @@ main (gint argc, g_test_add_func ("/event/attendees/invite", event_invite_attendees); g_test_add_func ("/event/attendees/copy", event_copy_attendees); g_test_add_func ("/event/equal/schedule", event_schedule_equal); + g_test_add_func ("/event/conference/add", event_add_conference); + g_test_add_func ("/event/conference/remove", event_remove_conference); + g_test_add_func ("/event/conference/copy", event_copy_conferences); return g_test_run (); } diff --git a/tests/test-federated-core.c b/tests/test-federated-core.c index 656b250e..9904b180 100644 --- a/tests/test-federated-core.c +++ b/tests/test-federated-core.c @@ -149,6 +149,50 @@ federated_core_incomplete_conf (void) remove_config_dir (config_dir); } +static void +federated_core_find_jitsi_server (void) +{ + g_autofree gchar *config_dir = create_config_dir (); + g_autofree gchar *server = NULL; + + /* Nothing set up */ + g_assert_null (gcal_federated_core_find_jitsi_server (config_dir, "nextcloud.example.com")); + + write_file (config_dir, "cores/example.com.conf", CORE_CONF ("example.com", "ana@example.com", "Ana\\ Lima")); + write_file (config_dir, "cores/example.org.conf", CORE_CONF ("example.org", "bo@example.org", "Bo")); + + server = gcal_federated_core_find_jitsi_server (config_dir, "nextcloud.example.com"); + g_assert_cmpstr (server, ==, "https://jitsi.example.com"); + g_clear_pointer (&server, g_free); + + server = gcal_federated_core_find_jitsi_server (config_dir, "NextCloud.Example.Org"); + g_assert_cmpstr (server, ==, "https://jitsi.example.org"); + g_clear_pointer (&server, g_free); + + /* Only for calendars on the Core's own Nextcloud */ + g_assert_null (gcal_federated_core_find_jitsi_server (config_dir, "example.com")); + g_assert_null (gcal_federated_core_find_jitsi_server (config_dir, "jitsi.example.com")); + g_assert_null (gcal_federated_core_find_jitsi_server (config_dir, "nextcloud.example.com.evil.net")); + g_assert_null (gcal_federated_core_find_jitsi_server (config_dir, "nextcloud.example.net")); + g_assert_null (gcal_federated_core_find_jitsi_server (config_dir, NULL)); + + remove_config_dir (config_dir); +} + +static void +federated_core_jitsi_server_single_core_conf (void) +{ + g_autofree gchar *config_dir = create_config_dir (); + g_autofree gchar *server = NULL; + + write_file (config_dir, "core.conf", CORE_CONF ("example.com", "ana@example.com", "Ana")); + + server = gcal_federated_core_find_jitsi_server (config_dir, "nextcloud.example.com"); + g_assert_cmpstr (server, ==, "https://jitsi.example.com"); + + remove_config_dir (config_dir); +} + /*********************************************************************************************************************/ gint @@ -160,6 +204,8 @@ main (gint argc, g_test_add_func ("/federated-core/find-core", federated_core_find_core); g_test_add_func ("/federated-core/single-core-conf", federated_core_single_core_conf); g_test_add_func ("/federated-core/incomplete-conf", federated_core_incomplete_conf); + g_test_add_func ("/federated-core/find-jitsi-server", federated_core_find_jitsi_server); + g_test_add_func ("/federated-core/jitsi-server-single-core-conf", federated_core_jitsi_server_single_core_conf); return g_test_run (); } diff --git a/tests/test-jitsi.c b/tests/test-jitsi.c new file mode 100644 index 00000000..fca12b0b --- /dev/null +++ b/tests/test-jitsi.c @@ -0,0 +1,327 @@ +/* test-jitsi.c + * + * Copyright (C) 2026 David Young + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include + +#include "gcal-event.h" +#include "gcal-jitsi.h" +#include "gcal-stub-calendar.h" +#include "gcal-utils.h" + +#define ROOM_URL "https://jitsi.example.com/BraveOttersSing-k3f9x2m7" + +#define STUB_EVENT(extra) \ + "BEGIN:VEVENT\n" \ + "SUMMARY:Stub event\n" \ + "UID:example@uid\n" \ + "DTSTAMP:19970114T170000Z\n" \ + "DTSTART:20180714T170000Z\n" \ + "DTEND:20180715T035959Z\n" \ + extra \ + "END:VEVENT\n" + +static GcalEvent * +create_event_for_string (const gchar *string) +{ + g_autoptr (ECalComponent) component = NULL; + g_autoptr (GcalCalendar) calendar = NULL; + g_autoptr (GError) error = NULL; + GcalEvent *event; + + component = e_cal_component_new_from_string (string); + g_assert_nonnull (component); + + calendar = gcal_stub_calendar_new (NULL, &error); + g_assert_no_error (error); + + event = gcal_event_new (calendar, component, &error); + g_assert_no_error (error); + + return event; +} + +/*********************************************************************************************************************/ + +static void +jitsi_room_name (void) +{ + g_autoptr (GHashTable) names = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); + + for (gint i = 0; i < 1000; i++) + { + g_autofree gchar *name = gcal_jitsi_generate_room_name (); + g_autofree gchar *url = NULL; + const gchar *dash; + + g_assert_nonnull (name); + + /* Three capitalized words, a dash and a suffix of 8 */ + g_assert_true (g_regex_match_simple ("^([A-Z][a-z]+){3}-[a-km-np-z2-9]{8}$", name, 0, 0)); + + dash = strchr (name, '-'); + g_assert_cmpuint (strlen (dash + 1), ==, 8); + + /* Room names are safe in a URL as they are */ + url = gcal_jitsi_build_room_url ("https://jitsi.example.com", name); + g_assert_true (g_str_has_suffix (url, name)); + g_assert_true (gcal_jitsi_is_room_url (url)); + + g_assert_true (g_hash_table_add (names, g_steal_pointer (&name))); + } +} + +static void +jitsi_build_room_url (void) +{ + g_autofree gchar *url = NULL; + g_autofree gchar *slash_url = NULL; + g_autofree gchar *escaped_url = NULL; + + url = gcal_jitsi_build_room_url ("https://jitsi.example.com", "BraveOttersSing-k3f9x2m7"); + g_assert_cmpstr (url, ==, ROOM_URL); + + slash_url = gcal_jitsi_build_room_url ("https://jitsi.example.com/", "BraveOttersSing-k3f9x2m7"); + g_assert_cmpstr (slash_url, ==, ROOM_URL); + + escaped_url = gcal_jitsi_build_room_url ("https://jitsi.example.com", "Team meeting/1"); + g_assert_cmpstr (escaped_url, ==, "https://jitsi.example.com/Team%20meeting%2F1"); +} + +static void +jitsi_is_room_url (void) +{ + g_assert_true (gcal_jitsi_is_room_url (ROOM_URL)); + g_assert_true (gcal_jitsi_is_room_url ("https://JITSI.Example.Org/Room")); + g_assert_true (gcal_jitsi_is_room_url ("https://meet.jit.si/SomeRoom")); + g_assert_true (gcal_jitsi_is_room_url ("https://8x8.vc/vpaas-magic-cookie-123/Room")); + + /* A server, but not a room on it */ + g_assert_false (gcal_jitsi_is_room_url ("https://jitsi.example.com")); + g_assert_false (gcal_jitsi_is_room_url ("https://jitsi.example.com/")); + + /* Not Jitsi */ + g_assert_false (gcal_jitsi_is_room_url ("https://meet.google.com/abc-defg-hij")); + g_assert_false (gcal_jitsi_is_room_url ("https://example.com/jitsi/Room")); + g_assert_false (gcal_jitsi_is_room_url ("https://notjitsi.example.com/Room")); + g_assert_false (gcal_jitsi_is_room_url ("https://jitsi/Room")); + g_assert_false (gcal_jitsi_is_room_url ("ftp://jitsi.example.com/Room")); + g_assert_false (gcal_jitsi_is_room_url ("jitsi.example.com/Room")); + g_assert_false (gcal_jitsi_is_room_url ("Room 101")); + g_assert_false (gcal_jitsi_is_room_url ("")); + g_assert_false (gcal_jitsi_is_room_url (NULL)); +} + +static void +jitsi_find_room_url (void) +{ + struct { + const gchar *text; + const gchar *url; + } texts[] = { + { ROOM_URL, ROOM_URL }, + { "Join the video conference: " ROOM_URL, ROOM_URL }, + { "Agenda\n\nJoin the video conference: " ROOM_URL "\n", ROOM_URL }, + { "Meet at " ROOM_URL ". Bring notes.", ROOM_URL }, + { "(" ROOM_URL ")", ROOM_URL }, + { "Join", ROOM_URL }, + { "<" ROOM_URL ">", ROOM_URL }, + { "See https://example.com/agenda, then " ROOM_URL, ROOM_URL }, + { "Rejoignez la visioconférence : https://meet.jit.si/Équipe", "https://meet.jit.si/Équipe" }, + { "https://meet.google.com/abc-defg-hij", NULL }, + { "https://jitsi.example.com/", NULL }, + { "http", NULL }, + { "", NULL }, + { NULL, NULL }, + }; + + for (gsize i = 0; i < G_N_ELEMENTS (texts); i++) + { + g_autofree gchar *url = gcal_jitsi_find_room_url (texts[i].text); + + g_assert_cmpstr (url, ==, texts[i].url); + } +} + +static void +jitsi_find_event_conference (void) +{ + struct { + const gchar *event; + const gchar *url; + } events[] = { + { STUB_EVENT (""), NULL }, + { STUB_EVENT ("LOCATION:Room 101\n"), NULL }, + { STUB_EVENT ("LOCATION:https://meet.google.com/abc-defg-hij\n"), NULL }, + { STUB_EVENT ("LOCATION:" ROOM_URL "\n"), ROOM_URL }, + { STUB_EVENT ("DESCRIPTION:Agenda\\n\\nJoin the video conference: " ROOM_URL "\n"), ROOM_URL }, + { STUB_EVENT ("CONFERENCE;VALUE=URI;FEATURE=VIDEO:" ROOM_URL "\n"), ROOM_URL }, + + /* Any conference in the CONFERENCE property comes first */ + { STUB_EVENT ("CONFERENCE;VALUE=URI;FEATURE=VIDEO:https://zoom.us/j/123\n" + "DESCRIPTION:" ROOM_URL "\n"), "https://zoom.us/j/123" }, + { STUB_EVENT ("DESCRIPTION:https://meet.jit.si/Other\n" + "LOCATION:" ROOM_URL "\n"), "https://meet.jit.si/Other" }, + }; + + for (gsize i = 0; i < G_N_ELEMENTS (events); i++) + { + g_autoptr (GcalEvent) event = create_event_for_string (events[i].event); + g_autofree gchar *url = gcal_jitsi_find_event_conference (event); + + g_assert_cmpstr (url, ==, events[i].url); + } +} + +static void +jitsi_add_to_description (void) +{ + g_autofree gchar *empty = NULL; + g_autofree gchar *null = NULL; + g_autofree gchar *notes = NULL; + g_autofree gchar *trailing = NULL; + g_autofree gchar *again = NULL; + + empty = gcal_jitsi_add_to_description ("", ROOM_URL); + g_assert_cmpstr (empty, ==, "Join the video conference: " ROOM_URL); + + null = gcal_jitsi_add_to_description (NULL, ROOM_URL); + g_assert_cmpstr (null, ==, "Join the video conference: " ROOM_URL); + + notes = gcal_jitsi_add_to_description ("Agenda", ROOM_URL); + g_assert_cmpstr (notes, ==, "Agenda\n\nJoin the video conference: " ROOM_URL); + + trailing = gcal_jitsi_add_to_description ("Agenda\n\n \n", ROOM_URL); + g_assert_cmpstr (trailing, ==, "Agenda\n\nJoin the video conference: " ROOM_URL); + + /* Only once, however often the event is saved */ + again = gcal_jitsi_add_to_description (notes, ROOM_URL); + g_assert_cmpstr (again, ==, notes); +} + +static void +jitsi_remove_from_description (void) +{ + struct { + const gchar *description; + const gchar *result; + } descriptions[] = { + { "Join the video conference: " ROOM_URL, "" }, + { "Agenda\n\nJoin the video conference: " ROOM_URL, "Agenda" }, + { "Agenda\n\nJoin the video conference: " ROOM_URL "\n", "Agenda" }, + { "Agenda\n\nJoin the video conference: " ROOM_URL "\n\nBring notes", "Agenda\n\nBring notes" }, + { "Agenda\nJoin the video conference: " ROOM_URL "\nBring notes", "Agenda\nBring notes" }, + { "Agenda\n\nhttps://meet.jit.si/Other", "Agenda\n\nhttps://meet.jit.si/Other" }, + { "", "" }, + { NULL, "" }, + }; + + for (gsize i = 0; i < G_N_ELEMENTS (descriptions); i++) + { + g_autofree gchar *result = gcal_jitsi_remove_from_description (descriptions[i].description, ROOM_URL); + + g_assert_cmpstr (result, ==, descriptions[i].result); + } +} + +static void +jitsi_add_and_remove_conference (void) +{ + g_autoptr (GcalEvent) event = NULL; + g_autoptr (GcalEvent) saved = NULL; + g_autofree gchar *string = NULL; + g_autofree gchar *url = NULL; + + event = create_event_for_string (STUB_EVENT ("DESCRIPTION:Agenda\n" + "LOCATION:Room 101\n")); + + gcal_jitsi_add_conference (event, ROOM_URL); + + url = gcal_event_dup_conference_url (event); + g_assert_cmpstr (url, ==, ROOM_URL); + g_assert_cmpstr (gcal_event_get_description (event), ==, "Agenda\n\nJoin the video conference: " ROOM_URL); + g_assert_cmpstr (gcal_event_get_location (event), ==, "Room 101"); + + /* Saved and loaded again, the editor finds the same conference */ + string = e_cal_component_get_as_string (gcal_event_get_component (event)); + saved = create_event_for_string (string); + + g_clear_pointer (&url, g_free); + url = gcal_jitsi_find_event_conference (saved); + g_assert_cmpstr (url, ==, ROOM_URL); + g_assert_cmpstr (gcal_event_get_description (saved), ==, gcal_event_get_description (event)); + + /* Saving it again doesn't add the link twice */ + gcal_jitsi_ensure_description (saved); + g_assert_cmpstr (gcal_event_get_description (saved), ==, gcal_event_get_description (event)); + + gcal_jitsi_remove_conference (saved, ROOM_URL); + + g_assert_null (gcal_event_dup_conference_url (saved)); + g_assert_null (gcal_jitsi_find_event_conference (saved)); + g_assert_cmpstr (gcal_event_get_description (saved), ==, "Agenda"); + g_assert_cmpstr (gcal_event_get_location (saved), ==, "Room 101"); +} + +static void +jitsi_ensure_description (void) +{ + g_autoptr (GcalEvent) edited = NULL; + g_autoptr (GcalEvent) other = NULL; + g_autoptr (GcalEvent) plain = NULL; + + /* Someone took the link out of the description, but not the conference */ + edited = create_event_for_string (STUB_EVENT ("DESCRIPTION:Agenda\n" + "CONFERENCE;VALUE=URI;FEATURE=AUDIO,VIDEO:" ROOM_URL "\n")); + gcal_jitsi_ensure_description (edited); + g_assert_cmpstr (gcal_event_get_description (edited), ==, "Agenda\n\nJoin the video conference: " ROOM_URL); + + /* Other apps' conferences are left alone */ + other = create_event_for_string (STUB_EVENT ("DESCRIPTION:Agenda\n" + "CONFERENCE;VALUE=URI;FEATURE=VIDEO:https://zoom.us/j/123\n")); + gcal_jitsi_ensure_description (other); + g_assert_cmpstr (gcal_event_get_description (other), ==, "Agenda"); + + plain = create_event_for_string (STUB_EVENT ("DESCRIPTION:Agenda\n")); + gcal_jitsi_ensure_description (plain); + g_assert_cmpstr (gcal_event_get_description (plain), ==, "Agenda"); +} + +/*********************************************************************************************************************/ + +gint +main (gint argc, + gchar *argv[]) +{ + /* The descriptions are checked in English */ + g_setenv ("LANGUAGE", "C", TRUE); + + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/jitsi/room-name", jitsi_room_name); + g_test_add_func ("/jitsi/build-room-url", jitsi_build_room_url); + g_test_add_func ("/jitsi/is-room-url", jitsi_is_room_url); + g_test_add_func ("/jitsi/find-room-url", jitsi_find_room_url); + g_test_add_func ("/jitsi/find-event-conference", jitsi_find_event_conference); + g_test_add_func ("/jitsi/add-to-description", jitsi_add_to_description); + g_test_add_func ("/jitsi/remove-from-description", jitsi_remove_from_description); + g_test_add_func ("/jitsi/add-and-remove-conference", jitsi_add_and_remove_conference); + g_test_add_func ("/jitsi/ensure-description", jitsi_ensure_description); + + return g_test_run (); +} diff --git a/tests/test-utils.c b/tests/test-utils.c index aa128b64..e00dabac 100644 --- a/tests/test-utils.c +++ b/tests/test-utils.c @@ -164,6 +164,10 @@ date_time_from_icaltime (void) "Pour les organisateurs : Options de réunion\n" \ "________________________________________________________________________________" +#define JITSI_SAMPLE_DESCRIPTION \ +"Agenda for the week\n" \ +"\n" \ +"Join the video conference: https://jitsi.example.com/BraveOttersSing-k3f9x2m7" static void extract_meeting_url (void) @@ -188,6 +192,28 @@ extract_meeting_url (void) gcal_util_extract_meeting_url (TEAMS_SAMPLE_DESCRIPTION, &description, &meeting_url); g_assert_cmpstr (description, ==, ""); g_assert_cmpstr (meeting_url, ==, "https://teams.microsoft.com/meet/12345678912345?p=0AbCdEfGjKLm1nOjTj"); + + g_clear_pointer (&description, g_free); + g_clear_pointer (&meeting_url, g_free); + + /* Jitsi links stay in the description, which has no section to take out */ + gcal_util_extract_meeting_url (JITSI_SAMPLE_DESCRIPTION, &description, &meeting_url); + g_assert_cmpstr (description, ==, JITSI_SAMPLE_DESCRIPTION); + g_assert_cmpstr (meeting_url, ==, "https://jitsi.example.com/BraveOttersSing-k3f9x2m7"); + + g_clear_pointer (&description, g_free); + g_clear_pointer (&meeting_url, g_free); + + gcal_util_extract_meeting_url ("Agenda: https://example.com/agenda", &description, &meeting_url); + g_assert_cmpstr (description, ==, "Agenda: https://example.com/agenda"); + g_assert_null (meeting_url); + + g_clear_pointer (&description, g_free); + g_clear_pointer (&meeting_url, g_free); + + gcal_util_extract_meeting_url (NULL, &description, &meeting_url); + g_assert_null (description); + g_assert_null (meeting_url); } /*********************************************************************************************************************/