[evolution-data-server] WebDAVNotes: Let the user choose which extension is used for new notes



commit 7636922d7ac5990b8e86cfa11d16c622a10265d2
Author: Milan Crha <mcrha redhat com>
Date:   Wed Dec 1 18:00:13 2021 +0100

    WebDAVNotes: Let the user choose which extension is used for new notes
    
    Rather than forcing .txt or .md extension in the code, let the user choose
    which one is preferred. Still default to the .md extension.
    
    Related to https://gitlab.gnome.org/GNOME/evolution-data-server/-/issues/360

 .../evolution-data-server-docs.sgml.in             |   1 +
 .../webdav-notes/e-cal-backend-webdav-notes.c      |  18 +-
 src/libedataserver/CMakeLists.txt                  |   2 +
 src/libedataserver/e-source-webdav-notes.c         | 205 +++++++++++++++++++++
 src/libedataserver/e-source-webdav-notes.h         |  89 +++++++++
 src/libedataserver/e-source.c                      |   2 +
 src/libedataserver/libedataserver.h                |   1 +
 7 files changed, 316 insertions(+), 2 deletions(-)
---
diff --git a/docs/reference/evolution-data-server/evolution-data-server-docs.sgml.in 
b/docs/reference/evolution-data-server/evolution-data-server-docs.sgml.in
index c5ce8efa6..30a95ea97 100644
--- a/docs/reference/evolution-data-server/evolution-data-server-docs.sgml.in
+++ b/docs/reference/evolution-data-server/evolution-data-server-docs.sgml.in
@@ -119,6 +119,7 @@
       <xi:include href="xml/e-source-task-list.xml"/>
       <xi:include href="xml/e-source-uoa.xml"/>
       <xi:include href="xml/e-source-webdav.xml"/>
+      <xi:include href="xml/e-source-webdav-notes.xml"/>
       <xi:include href="xml/e-source-weather.xml"/>
       <xi:include href="xml/e-source-backend-summary-setup.xml"/>
     </chapter>
diff --git a/src/calendar/backends/webdav-notes/e-cal-backend-webdav-notes.c 
b/src/calendar/backends/webdav-notes/e-cal-backend-webdav-notes.c
index 6821544ff..3eed5fc19 100644
--- a/src/calendar/backends/webdav-notes/e-cal-backend-webdav-notes.c
+++ b/src/calendar/backends/webdav-notes/e-cal-backend-webdav-notes.c
@@ -1094,8 +1094,22 @@ ecb_webdav_notes_save_component_sync (ECalMetaBackend *meta_backend,
 
                base_filename = ecb_webdav_notes_construct_base_filename (description);
                if (!ecb_webdav_notes_has_supported_extension (uid, &ext_len, &use_ext, &use_ext_num_suffix, 
&use_content_type)) {
-                       /* Default to the markdown format */
-                       g_warn_if_fail (ecb_webdav_notes_has_supported_extension (".md", &ext_len, &use_ext, 
&use_ext_num_suffix, &use_content_type));
+                       ESource *source;
+                       ESourceWebDAVNotes *notes_extension;
+                       const gchar *default_ext;
+
+                       source = e_backend_get_source (E_BACKEND (meta_backend));
+                       notes_extension = e_source_get_extension (source, E_SOURCE_EXTENSION_WEBDAV_NOTES);
+
+                       e_source_extension_property_lock (E_SOURCE_EXTENSION (notes_extension));
+                       default_ext = e_source_webdav_notes_get_default_ext (notes_extension);
+
+                       if (!ecb_webdav_notes_has_supported_extension (default_ext, &ext_len, &use_ext, 
&use_ext_num_suffix, &use_content_type)) {
+                               /* Fallback to the markdown format in case of broken setup */
+                               g_warn_if_fail (ecb_webdav_notes_has_supported_extension (".md", &ext_len, 
&use_ext, &use_ext_num_suffix, &use_content_type));
+                       }
+
+                       e_source_extension_property_unlock (E_SOURCE_EXTENSION (notes_extension));
                }
 
                expected_filename = g_strconcat (base_filename, use_ext, NULL);
diff --git a/src/libedataserver/CMakeLists.txt b/src/libedataserver/CMakeLists.txt
index b196ae26a..1412cb1d9 100644
--- a/src/libedataserver/CMakeLists.txt
+++ b/src/libedataserver/CMakeLists.txt
@@ -120,6 +120,7 @@ set(SOURCES
        e-source-uoa.c
        e-source-weather.c
        e-source-webdav.c
+       e-source-webdav-notes.c
        e-debug-log.c
        e-time-utils.c
        e-transliterator-private.h
@@ -212,6 +213,7 @@ set(HEADERS
        e-source-uoa.h
        e-source-weather.h
        e-source-webdav.h
+       e-source-webdav-notes.h
        e-debug-log.h
        e-time-utils.h
        e-uid.h
diff --git a/src/libedataserver/e-source-webdav-notes.c b/src/libedataserver/e-source-webdav-notes.c
new file mode 100644
index 000000000..c96139255
--- /dev/null
+++ b/src/libedataserver/e-source-webdav-notes.c
@@ -0,0 +1,205 @@
+/*
+ * This library is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation.
+ *
+ * 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 Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+/**
+ * SECTION: e-source-webdav-notes
+ * @include: libedataserver/libedataserver.h
+ * @short_description: WebDAV Notes specific settings
+ *
+ * #ESourceWebDAVNotes is an extension holding specific settings
+ * for the WebDAV Notes backend.
+ **/
+
+#include "e-source-webdav-notes.h"
+
+#include <libedataserver/e-data-server-util.h>
+
+struct _ESourceWebDAVNotesPrivate {
+       gchar *default_ext;
+};
+
+enum {
+       PROP_0,
+       PROP_DEFAULT_EXT
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE (ESourceWebDAVNotes, e_source_webdav_notes, E_TYPE_SOURCE_EXTENSION)
+
+static void
+source_webdav_notes_set_property (GObject *object,
+                                 guint property_id,
+                                 const GValue *value,
+                                 GParamSpec *pspec)
+{
+       switch (property_id) {
+               case PROP_DEFAULT_EXT:
+                       e_source_webdav_notes_set_default_ext (
+                               E_SOURCE_WEBDAV_NOTES (object),
+                               g_value_get_string (value));
+                       return;
+       }
+
+       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+}
+
+static void
+source_webdav_notes_get_property (GObject *object,
+                                 guint property_id,
+                                 GValue *value,
+                                 GParamSpec *pspec)
+{
+       switch (property_id) {
+               case PROP_DEFAULT_EXT:
+                       g_value_take_string (
+                               value,
+                               e_source_webdav_notes_dup_default_ext (
+                               E_SOURCE_WEBDAV_NOTES (object)));
+                       return;
+       }
+
+       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+}
+
+static void
+source_webdav_notes_finalize (GObject *object)
+{
+       ESourceWebDAVNotesPrivate *priv;
+
+       priv = E_SOURCE_WEBDAV_NOTES (object)->priv;
+
+       g_free (priv->default_ext);
+
+       /* Chain up to parent's method. */
+       G_OBJECT_CLASS (e_source_webdav_notes_parent_class)->finalize (object);
+}
+
+static void
+e_source_webdav_notes_class_init (ESourceWebDAVNotesClass *class)
+{
+       GObjectClass *object_class;
+       ESourceExtensionClass *extension_class;
+
+       object_class = G_OBJECT_CLASS (class);
+       object_class->set_property = source_webdav_notes_set_property;
+       object_class->get_property = source_webdav_notes_get_property;
+       object_class->finalize = source_webdav_notes_finalize;
+
+       extension_class = E_SOURCE_EXTENSION_CLASS (class);
+       extension_class->name = E_SOURCE_EXTENSION_WEBDAV_NOTES;
+
+       g_object_class_install_property (
+               object_class,
+               PROP_DEFAULT_EXT,
+               g_param_spec_string (
+                       "default-ext",
+                       "Default Ext",
+                       "Default file extension for new notes",
+                       ".md",
+                       G_PARAM_READWRITE |
+                       G_PARAM_CONSTRUCT |
+                       G_PARAM_EXPLICIT_NOTIFY |
+                       G_PARAM_STATIC_STRINGS |
+                       E_SOURCE_PARAM_SETTING));
+}
+
+static void
+e_source_webdav_notes_init (ESourceWebDAVNotes *extension)
+{
+       extension->priv = e_source_webdav_notes_get_instance_private (extension);
+}
+
+/**
+ * e_source_webdav_notes_get_default_ext:
+ * @extension: an #ESourceWebDAVNotes
+ *
+ * Returns the default file extension for new notes.
+ *
+ * Returns: (nullable): the default file extension, or %NULL, when none is set
+ *
+ * Since: 3.44
+ **/
+const gchar *
+e_source_webdav_notes_get_default_ext (ESourceWebDAVNotes *extension)
+{
+       g_return_val_if_fail (E_IS_SOURCE_WEBDAV_NOTES (extension), NULL);
+
+       return extension->priv->default_ext;
+}
+
+/**
+ * e_source_webdav_notes_dup_default_ext:
+ * @extension: an #ESourceWebDAVNotes
+ *
+ * Thread-safe variation of e_source_webdav_notes_get_default_ext().
+ * Use this function when accessing @extension from multiple threads.
+ *
+ * The returned string should be freed with g_free() when no longer needed.
+ *
+ * Returns: (nullable) (transfer full): a newly-allocated copy of #ESourceWebDAVNotes:default-ext,
+ *    or %NULL, when none is set
+ *
+ * Since: 3.44
+ **/
+gchar *
+e_source_webdav_notes_dup_default_ext (ESourceWebDAVNotes *extension)
+{
+       const gchar *protected;
+       gchar *duplicate;
+
+       g_return_val_if_fail (E_IS_SOURCE_WEBDAV_NOTES (extension), NULL);
+
+       e_source_extension_property_lock (E_SOURCE_EXTENSION (extension));
+
+       protected = e_source_webdav_notes_get_default_ext (extension);
+       duplicate = g_strdup (protected);
+
+       e_source_extension_property_unlock (E_SOURCE_EXTENSION (extension));
+
+       return duplicate;
+}
+
+/**
+ * e_source_webdav_notes_set_default_ext:
+ * @extension: an #ESourceWebDAVNotes
+ * @default_ext: (nullable): a default file extension, or %NULL
+ *
+ * Sets the default file extension for new notes.
+ *
+ * The internal copy of @default_ext is automatically stripped of leading and
+ * trailing whitespace.  If the resulting string is empty, %NULL is set
+ * instead.
+ *
+ * Since: 3.44
+ **/
+void
+e_source_webdav_notes_set_default_ext (ESourceWebDAVNotes *extension,
+                                      const gchar *default_ext)
+{
+       g_return_if_fail (E_IS_SOURCE_WEBDAV_NOTES (extension));
+
+       e_source_extension_property_lock (E_SOURCE_EXTENSION (extension));
+
+       if (e_util_strcmp0 (extension->priv->default_ext, default_ext) == 0) {
+               e_source_extension_property_unlock (E_SOURCE_EXTENSION (extension));
+               return;
+       }
+
+       g_free (extension->priv->default_ext);
+       extension->priv->default_ext = e_util_strdup_strip (default_ext);
+
+       e_source_extension_property_unlock (E_SOURCE_EXTENSION (extension));
+
+       g_object_notify (G_OBJECT (extension), "default-ext");
+}
diff --git a/src/libedataserver/e-source-webdav-notes.h b/src/libedataserver/e-source-webdav-notes.h
new file mode 100644
index 000000000..54473557c
--- /dev/null
+++ b/src/libedataserver/e-source-webdav-notes.h
@@ -0,0 +1,89 @@
+/*
+ * This library is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation.
+ *
+ * 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 Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#if !defined (__LIBEDATASERVER_H_INSIDE__) && !defined (LIBEDATASERVER_COMPILATION)
+#error "Only <libedataserver/libedataserver.h> should be included directly."
+#endif
+
+#ifndef E_SOURCE_WEBDAV_NOTES_H
+#define E_SOURCE_WEBDAV_NOTES_H
+
+#include <libedataserver/e-source-extension.h>
+
+/* Standard GObject macros */
+#define E_TYPE_SOURCE_WEBDAV_NOTES \
+       (e_source_webdav_notes_get_type ())
+#define E_SOURCE_WEBDAV_NOTES(obj) \
+       (G_TYPE_CHECK_INSTANCE_CAST \
+       ((obj), E_TYPE_SOURCE_WEBDAV_NOTES, ESourceWebDAVNotes))
+#define E_SOURCE_WEBDAV_NOTES_CLASS(cls) \
+       (G_TYPE_CHECK_CLASS_CAST \
+       ((cls), E_TYPE_SOURCE_WEBDAV_NOTES, ESourceWebDAVNotesClass))
+#define E_IS_SOURCE_WEBDAV_NOTES(obj) \
+       (G_TYPE_CHECK_INSTANCE_TYPE \
+       ((obj), E_TYPE_SOURCE_WEBDAV_NOTES))
+#define E_IS_SOURCE_WEBDAV_NOTES_CLASS(cls) \
+       (G_TYPE_CHECK_CLASS_TYPE \
+       ((cls), E_TYPE_SOURCE_WEBDAV_NOTES))
+#define E_SOURCE_WEBDAV_NOTES_GET_CLASS(obj) \
+       (G_TYPE_INSTANCE_GET_CLASS \
+       ((obj), E_TYPE_SOURCE_WEBDAV_NOTES, ESourceWebDAVNotesClass))
+
+/**
+ * E_SOURCE_EXTENSION_WEBDAV_NOTES:
+ *
+ * Pass this extension name to e_source_get_extension() to access
+ * #ESourceWebDAVNotes.  This is also used as a group name in key files.
+ *
+ * Since: 3.44
+ **/
+#define E_SOURCE_EXTENSION_WEBDAV_NOTES "WebDAV Notes"
+
+G_BEGIN_DECLS
+
+typedef struct _ESourceWebDAVNotes ESourceWebDAVNotes;
+typedef struct _ESourceWebDAVNotesClass ESourceWebDAVNotesClass;
+typedef struct _ESourceWebDAVNotesPrivate ESourceWebDAVNotesPrivate;
+
+/**
+ * ESourceWebDAVNotes:
+ *
+ * Contains only private data that should be read and manipulated using the
+ * functions below.
+ *
+ * Since: 3.44
+ **/
+struct _ESourceWebDAVNotes {
+       /*< private >*/
+       ESourceExtension parent;
+       ESourceWebDAVNotesPrivate *priv;
+};
+
+struct _ESourceWebDAVNotesClass {
+       ESourceExtensionClass parent_class;
+};
+
+GType          e_source_webdav_notes_get_type  (void) G_GNUC_CONST;
+const gchar *  e_source_webdav_notes_get_default_ext
+                                               (ESourceWebDAVNotes *extension);
+gchar *                e_source_webdav_notes_dup_default_ext
+                                               (ESourceWebDAVNotes *extension);
+void           e_source_webdav_notes_set_default_ext
+                                               (ESourceWebDAVNotes *extension,
+                                                const gchar *default_ext);
+
+G_END_DECLS
+
+#endif /* E_SOURCE_WEBDAV_NOTES_H */
diff --git a/src/libedataserver/e-source.c b/src/libedataserver/e-source.c
index 6c76cd22f..b2fb6102d 100644
--- a/src/libedataserver/e-source.c
+++ b/src/libedataserver/e-source.c
@@ -115,6 +115,7 @@
 #include "e-source-uoa.h"
 #include "e-source-weather.h"
 #include "e-source-webdav.h"
+#include "e-source-webdav-notes.h"
 
 #include "e-source.h"
 
@@ -2432,6 +2433,7 @@ e_source_class_init (ESourceClass *class)
        g_type_ensure (E_TYPE_SOURCE_UOA);
        g_type_ensure (E_TYPE_SOURCE_WEATHER);
        g_type_ensure (E_TYPE_SOURCE_WEBDAV);
+       g_type_ensure (E_TYPE_SOURCE_WEBDAV_NOTES);
 }
 
 static void
diff --git a/src/libedataserver/libedataserver.h b/src/libedataserver/libedataserver.h
index 34e71cbf3..311beed84 100644
--- a/src/libedataserver/libedataserver.h
+++ b/src/libedataserver/libedataserver.h
@@ -94,6 +94,7 @@
 #include <libedataserver/e-source-task-list.h>
 #include <libedataserver/e-source-uoa.h>
 #include <libedataserver/e-source-webdav.h>
+#include <libedataserver/e-source-webdav-notes.h>
 #include <libedataserver/e-source-weather.h>
 #include <libedataserver/e-source.h>
 #include <libedataserver/e-time-utils.h>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]