From 7424086a61e68300446b8e060263de6602a0d549 Mon Sep 17 00:00:00 2001 From: minhdanh Date: Sun, 28 Jan 2024 07:24:15 +0700 Subject: [PATCH] Support online web calendar --- core/Utils.vala | 3 + plugins/Web/WebBackend.vala | 176 ++++++++++++++++++++++++++++++++++++ plugins/Web/meson.build | 7 ++ plugins/meson.build | 1 + 4 files changed, 187 insertions(+) create mode 100644 plugins/Web/WebBackend.vala create mode 100644 plugins/Web/meson.build diff --git a/core/Utils.vala b/core/Utils.vala index b3e527397..2e07920a3 100644 --- a/core/Utils.vala +++ b/core/Utils.vala @@ -53,6 +53,9 @@ namespace Maya.Util { parent_source = registry.ref_source (parent_uid); } + if (source.parent == "webcal-stub") { + return _("On the web"); + } return _("On this computer"); } diff --git a/plugins/Web/WebBackend.vala b/plugins/Web/WebBackend.vala new file mode 100644 index 000000000..8de01910e --- /dev/null +++ b/plugins/Web/WebBackend.vala @@ -0,0 +1,176 @@ +// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*- +/*- + * Copyright (c) 2013 Maya Developers (https://launchpad.net/maya) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA. + * + * Authored by: Corentin Noël + */ + +public Maya.Backend get_backend (Module module) { + debug ("Activating Web Backend"); + var b = new Maya.WebBackend (); + b.ref (); + return b; +} + +public static Maya.Backend backend; + +public class Maya.WebBackend : GLib.Object, Maya.Backend { + + public WebBackend () { + backend = this; + } + + public string get_name () { + return _("On the web"); + } + + public string get_uid () { + return "webcal-stub"; + } + + public Gee.Collection get_new_calendar_widget (E.Source? to_edit = null) { + var collection = new Gee.LinkedList (); + + bool keep_copy = false; + if (to_edit != null) { + E.SourceOffline source_offline = (E.SourceOffline)to_edit.get_extension (E.SOURCE_EXTENSION_OFFLINE); + keep_copy = source_offline.stay_synchronized; + } + + collection.add (Maya.DefaultPlacementWidgets.get_keep_copy (0, keep_copy)); + + var url_label = new PlacementWidget (); + url_label.widget = new Gtk.Label (_("URL:")); + ((Gtk.Label) url_label.widget).expand = true; + ((Gtk.Misc) url_label.widget).xalign = 1.0f; + url_label.row = 1; + url_label.column = 0; + url_label.ref_name = "url_label"; + collection.add (url_label); + + var url_entry = new PlacementWidget (); + url_entry.widget = new Gtk.Entry (); + ((Gtk.Entry)url_entry.widget).text = "http://"; + url_entry.row = 1; + url_entry.column = 1; + url_entry.ref_name = "url_entry"; + url_entry.needed = true; + collection.add (url_entry); + if (to_edit != null) { + E.SourceWebdav webdav = (E.SourceWebdav)to_edit.get_extension (E.SOURCE_EXTENSION_WEBDAV_BACKEND); +#if HAS_EDS_3_46 + var uri = webdav.dup_uri (); +#else + var uri = webdav.dup_soup_uri (); +#endif + if (uri.get_port () != 80) { + ((Gtk.Entry)url_entry.widget).text = "%s://%s:%u%s".printf (uri.get_scheme (), uri.get_host (), uri.get_port (), uri.get_path ()); + } else { + ((Gtk.Entry)url_entry.widget).text = "%s://%s%s".printf (uri.get_scheme (), uri.get_host (), uri.get_path ()); + } + } + + var secure_checkbutton = new PlacementWidget (); + secure_checkbutton.widget = new Gtk.CheckButton.with_label (_("Use a secure connection")); + secure_checkbutton.row = 3; + secure_checkbutton.column = 1; + secure_checkbutton.ref_name = "secure_checkbutton"; + collection.add (secure_checkbutton); + if (to_edit != null) { + E.SourceSecurity security = (E.SourceSecurity)to_edit.get_extension (E.SOURCE_EXTENSION_SECURITY); + ((Gtk.CheckButton)secure_checkbutton.widget).active = security.secure; + } + + return collection; + } + + public void add_new_calendar (string name, string color, bool set_default, Gee.Collection widgets) { + try { + var new_source = new E.Source (null, null); + new_source.display_name = name; + new_source.parent = get_uid (); + E.SourceCalendar cal = (E.SourceCalendar)new_source.get_extension (E.SOURCE_EXTENSION_CALENDAR); + cal.color = color; + cal.backend_name = "webcal"; + E.SourceWebdav webdav = (E.SourceWebdav)new_source.get_extension (E.SOURCE_EXTENSION_WEBDAV_BACKEND); + E.SourceAuthentication auth = (E.SourceAuthentication)new_source.get_extension (E.SOURCE_EXTENSION_AUTHENTICATION); + E.SourceOffline offline = (E.SourceOffline)new_source.get_extension (E.SOURCE_EXTENSION_OFFLINE); + foreach (var widget in widgets) { + switch (widget.ref_name) { + case "url_entry": +#if HAS_EDS_3_46 + webdav.uri = GLib.Uri.parse (((Gtk.Entry)widget.widget).text, GLib.UriFlags.NONE); +#else + webdav.soup_uri = new Soup.URI (((Gtk.Entry)widget.widget).text); +#endif + break; + case "keep_copy": + offline.set_stay_synchronized (((Gtk.CheckButton)widget.widget).active); + break; + } + } + + var calmodel = Calendar.EventStore.get_default (); + var registry = calmodel.registry; + var list = new List (); + list.append (new_source); + registry.create_sources_sync (list); + calmodel.add_source (new_source); + if (set_default) { + registry.default_calendar = new_source; + } + + } catch (GLib.Error error) { + critical (error.message); + } + } + + public void modify_calendar (string name, string color, bool set_default, Gee.Collection widgets, E.Source source) { + try { + source.display_name = name; + E.SourceCalendar cal = (E.SourceCalendar)source.get_extension (E.SOURCE_EXTENSION_CALENDAR); + cal.color = color; + E.SourceWebdav webdav = (E.SourceWebdav)source.get_extension (E.SOURCE_EXTENSION_WEBDAV_BACKEND); + E.SourceAuthentication auth = (E.SourceAuthentication)source.get_extension (E.SOURCE_EXTENSION_AUTHENTICATION); + E.SourceOffline offline = (E.SourceOffline)source.get_extension (E.SOURCE_EXTENSION_OFFLINE); + foreach (var widget in widgets) { + switch (widget.ref_name) { + case "url_entry": +#if HAS_EDS_3_46 + webdav.uri = GLib.Uri.parse (((Gtk.Entry)widget.widget).text, GLib.UriFlags.NONE); +#else + webdav.soup_uri = new Soup.URI (((Gtk.Entry)widget.widget).text); +#endif + break; + case "keep_copy": + offline.set_stay_synchronized (((Gtk.CheckButton)widget.widget).active); + break; + } + } + + source.write.begin (null); + if (set_default) { + var registry = new E.SourceRegistry.sync (null); + registry.default_calendar = source; + } + + } catch (GLib.Error error) { + critical (error.message); + } + } +} diff --git a/plugins/Web/meson.build b/plugins/Web/meson.build new file mode 100644 index 000000000..c6cfde337 --- /dev/null +++ b/plugins/Web/meson.build @@ -0,0 +1,7 @@ +shared_module( + 'web', + 'WebBackend.vala', + dependencies: core_dep, + install: true, + install_dir: join_paths(pluginsdir, 'Web') +) diff --git a/plugins/meson.build b/plugins/meson.build index 44fe45a03..9147086ca 100644 --- a/plugins/meson.build +++ b/plugins/meson.build @@ -1,2 +1,3 @@ subdir('CalDAV') subdir('Google') +subdir('Web')