[tepl] Metadata: create class
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [tepl] Metadata: create class
- Date: Sat, 18 Apr 2020 17:20:38 +0000 (UTC)
commit 0aaf7ec8b79b9b16a9cd0bcd94bda1c6444d598b
Author: Sébastien Wilmet <swilmet gnome org>
Date: Sat Apr 18 02:48:26 2020 +0200
Metadata: create class
docs/reference/tepl-docs.xml | 2 +-
docs/reference/tepl-sections.txt | 27 +++-----
po/POTFILES.in | 1 +
tepl/meson.build | 2 +
tepl/tepl-metadata.c | 146 +++++++++++++++++++++++++++++++++++++++
tepl/tepl-metadata.h | 72 +++++++++++++++++++
tepl/tepl.h | 1 +
7 files changed, 232 insertions(+), 19 deletions(-)
---
diff --git a/docs/reference/tepl-docs.xml b/docs/reference/tepl-docs.xml
index dea48e0..53181ca 100644
--- a/docs/reference/tepl-docs.xml
+++ b/docs/reference/tepl-docs.xml
@@ -51,7 +51,7 @@
<chapter id="file-metadata">
<title>File Metadata</title>
- <xi:include href="xml/file-metadata.xml"/>
+ <xi:include href="xml/metadata.xml"/>
<xi:include href="xml/metadata-store.xml"/>
</chapter>
diff --git a/docs/reference/tepl-sections.txt b/docs/reference/tepl-sections.txt
index bc2acf9..6f20bda 100644
--- a/docs/reference/tepl-sections.txt
+++ b/docs/reference/tepl-sections.txt
@@ -189,24 +189,6 @@ tepl_file_loader_error_get_type
tepl_file_loader_error_quark
</SECTION>
-<SECTION>
-<FILE>file-metadata</FILE>
-TeplFileMetadata
-tepl_file_metadata_new
-tepl_file_metadata_get
-tepl_file_metadata_set
-<SUBSECTION Standard>
-TEPL_FILE_METADATA
-TEPL_FILE_METADATA_CLASS
-TEPL_FILE_METADATA_GET_CLASS
-TEPL_IS_FILE_METADATA
-TEPL_IS_FILE_METADATA_CLASS
-TEPL_TYPE_FILE_METADATA
-TeplFileMetadataClass
-TeplFileMetadataPrivate
-tepl_file_metadata_get_type
-</SECTION>
-
<SECTION>
<FILE>file-saver</FILE>
TeplFileSaver
@@ -308,6 +290,15 @@ tepl_iter_get_line_indentation
tepl_menu_shell_append_edit_actions
</SECTION>
+<SECTION>
+<FILE>metadata</FILE>
+TeplMetadata
+tepl_metadata_new
+tepl_metadata_get
+tepl_metadata_set
+<SUBSECTION Standard>
+</SECTION>
+
<SECTION>
<FILE>metadata-store</FILE>
TeplMetadataStore
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 995464e..0accf5b 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -19,6 +19,7 @@ tepl/tepl-init.c
tepl/tepl-io-error-info-bars.c
tepl/tepl-iter.c
tepl/tepl-menu-shell.c
+tepl/tepl-metadata.c
tepl/tepl-metadata-parser.c
tepl/tepl-metadata-store.c
tepl/tepl-notebook.c
diff --git a/tepl/meson.build b/tepl/meson.build
index 8f9991c..ae71131 100644
--- a/tepl/meson.build
+++ b/tepl/meson.build
@@ -17,6 +17,7 @@ tepl_public_headers = [
'tepl-io-error-info-bars.h',
'tepl-iter.h',
'tepl-menu-shell.h',
+ 'tepl-metadata.h',
'tepl-metadata-store.h',
'tepl-notebook.h',
'tepl-tab.h',
@@ -44,6 +45,7 @@ tepl_public_c_files = [
'tepl-io-error-info-bars.c',
'tepl-iter.c',
'tepl-menu-shell.c',
+ 'tepl-metadata.c',
'tepl-metadata-store.c',
'tepl-notebook.c',
'tepl-tab.c',
diff --git a/tepl/tepl-metadata.c b/tepl/tepl-metadata.c
new file mode 100644
index 0000000..775ce88
--- /dev/null
+++ b/tepl/tepl-metadata.c
@@ -0,0 +1,146 @@
+/*
+ * This file is part of Tepl, a text editor library.
+ *
+ * Copyright 2020 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * Tepl 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; either version 2.1 of the License, or (at your
+ * option) any later version.
+ *
+ * Tepl 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/>.
+ */
+
+#include "tepl-metadata.h"
+
+struct _TeplMetadataPrivate
+{
+ /* Keys: gchar *
+ * Values: nullable gchar *
+ * hash_table never NULL.
+ */
+ GHashTable *hash_table;
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE (TeplMetadata, tepl_metadata, G_TYPE_OBJECT)
+
+static void
+tepl_metadata_finalize (GObject *object)
+{
+ TeplMetadata *metadata = TEPL_METADATA (object);
+
+ g_hash_table_unref (metadata->priv->hash_table);
+
+ G_OBJECT_CLASS (tepl_metadata_parent_class)->finalize (object);
+}
+
+static void
+tepl_metadata_class_init (TeplMetadataClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->finalize = tepl_metadata_finalize;
+}
+
+static void
+tepl_metadata_init (TeplMetadata *metadata)
+{
+ metadata->priv = tepl_metadata_get_instance_private (metadata);
+
+ metadata->priv->hash_table = g_hash_table_new_full (g_str_hash,
+ g_str_equal,
+ g_free,
+ g_free);
+}
+
+TeplMetadata *
+tepl_metadata_new (void)
+{
+ return g_object_new (TEPL_TYPE_METADATA, NULL);
+}
+
+gchar *
+tepl_metadata_get (TeplMetadata *metadata,
+ const gchar *key)
+{
+ g_return_val_if_fail (TEPL_IS_METADATA (metadata), NULL);
+ g_return_val_if_fail (_tepl_metadata_key_is_valid (key), NULL);
+
+ return g_strdup (g_hash_table_lookup (metadata->priv->hash_table, key));
+}
+
+void
+tepl_metadata_set (TeplMetadata *metadata,
+ const gchar *key,
+ const gchar *value)
+{
+ g_return_if_fail (TEPL_IS_METADATA (metadata));
+ g_return_if_fail (_tepl_metadata_key_is_valid (key));
+ g_return_if_fail (value == NULL || g_utf8_validate (value, -1, NULL));
+
+ g_hash_table_replace (metadata->priv->hash_table,
+ g_strdup (key),
+ g_strdup (value));
+}
+
+static gboolean
+key_char_is_valid (gchar ch)
+{
+ /* TODO update comment.
+ * At the time of writing this, the GIO API doesn't document the
+ * requirements for valid attribute names. See the docs of
+ * g_file_query_info() for example. Clearly '*' and ',' must not be used
+ * because they serve to query several attributes. ':' is used in "::"
+ * to separate the namespace from the attribute name, I'm not sure that
+ * there can be several nested namespaces like in
+ * "metadata::gCSVedit::delimiter"; in case of doubt it's better not to
+ * support it by not allowing ':'.
+ */
+ return (g_ascii_isalnum (ch) || ch == '-' || ch == '_');
+}
+
+/*
+ * _tepl_metadata_key_is_valid:
+ * @metadata_key: (nullable): a string, or %NULL.
+ *
+ * Returns whether @metadata_key is a valid string that can be used as a
+ * metadata key when using the Tepl metadata API.
+ *
+ * It returns %TRUE only if @metadata_key is a non-empty string containing only
+ * ASCII alphanumeric characters (see g_ascii_isalnum()), `"-"` (dash) or `"_"`
+ * (underscore).
+ *
+ * Examples of valid metadata keys:
+ * - `"gedit-spell-checking-language"`
+ * - `"gCSVedit_column_delimiter"`
+ *
+ * Returns: whether @metadata_key is valid.
+ */
+gboolean
+_tepl_metadata_key_is_valid (const gchar *key)
+{
+ const gchar *p;
+
+ if (key == NULL || key[0] == '\0')
+ {
+ return FALSE;
+ }
+
+ for (p = key; *p != '\0'; p++)
+ {
+ gchar cur_char = *p;
+
+ if (!key_char_is_valid (cur_char))
+ {
+ return FALSE;
+ }
+ }
+
+ return TRUE;
+}
diff --git a/tepl/tepl-metadata.h b/tepl/tepl-metadata.h
new file mode 100644
index 0000000..30404be
--- /dev/null
+++ b/tepl/tepl-metadata.h
@@ -0,0 +1,72 @@
+/*
+ * This file is part of Tepl, a text editor library.
+ *
+ * Copyright 2020 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * Tepl 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; either version 2.1 of the License, or (at your
+ * option) any later version.
+ *
+ * Tepl 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/>.
+ */
+
+#ifndef TEPL_METADATA_H
+#define TEPL_METADATA_H
+
+#if !defined (TEPL_H_INSIDE) && !defined (TEPL_COMPILATION)
+#error "Only <tepl/tepl.h> can be included directly."
+#endif
+
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+#define TEPL_TYPE_METADATA (tepl_metadata_get_type ())
+#define TEPL_METADATA(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TEPL_TYPE_METADATA, TeplMetadata))
+#define TEPL_METADATA_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TEPL_TYPE_METADATA,
TeplMetadataClass))
+#define TEPL_IS_METADATA(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TEPL_TYPE_METADATA))
+#define TEPL_IS_METADATA_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TEPL_TYPE_METADATA))
+#define TEPL_METADATA_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TEPL_TYPE_METADATA,
TeplMetadataClass))
+
+typedef struct _TeplMetadata TeplMetadata;
+typedef struct _TeplMetadataClass TeplMetadataClass;
+typedef struct _TeplMetadataPrivate TeplMetadataPrivate;
+
+struct _TeplMetadata
+{
+ GObject parent;
+
+ TeplMetadataPrivate *priv;
+};
+
+struct _TeplMetadataClass
+{
+ GObjectClass parent_class;
+
+ gpointer padding[12];
+};
+
+GType tepl_metadata_get_type (void);
+
+TeplMetadata * tepl_metadata_new (void);
+
+gchar * tepl_metadata_get (TeplMetadata *metadata,
+ const gchar *key);
+
+void tepl_metadata_set (TeplMetadata *metadata,
+ const gchar *key,
+ const gchar *value);
+
+G_GNUC_INTERNAL
+gboolean _tepl_metadata_key_is_valid (const gchar *key);
+
+G_END_DECLS
+
+#endif /* TEPL_METADATA_H */
diff --git a/tepl/tepl.h b/tepl/tepl.h
index d9e59d0..8d98c51 100644
--- a/tepl/tepl.h
+++ b/tepl/tepl.h
@@ -43,6 +43,7 @@
#include <tepl/tepl-io-error-info-bars.h>
#include <tepl/tepl-iter.h>
#include <tepl/tepl-menu-shell.h>
+#include <tepl/tepl-metadata.h>
#include <tepl/tepl-metadata-store.h>
#include <tepl/tepl-notebook.h>
#include <tepl/tepl-tab.h>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]