[tepl] Buffer: associate a TeplMetadata object



commit d4343fde48e46c4da7c026db560061e10ae8836e
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sun Apr 19 12:20:03 2020 +0200

    Buffer: associate a TeplMetadata object

 docs/reference/tepl-sections.txt |  3 ++
 tepl/tepl-buffer.c               | 91 +++++++++++++++++++++++++++++++++++++++-
 tepl/tepl-buffer.h               | 11 ++++-
 3 files changed, 102 insertions(+), 3 deletions(-)
---
diff --git a/docs/reference/tepl-sections.txt b/docs/reference/tepl-sections.txt
index 396641e..ff73435 100644
--- a/docs/reference/tepl-sections.txt
+++ b/docs/reference/tepl-sections.txt
@@ -98,6 +98,9 @@ TeplBuffer
 TeplSelectionType
 tepl_buffer_new
 tepl_buffer_get_file
+tepl_buffer_get_metadata
+tepl_buffer_load_metadata_from_metadata_manager
+tepl_buffer_save_metadata_into_metadata_manager
 tepl_buffer_is_untouched
 tepl_buffer_get_short_title
 tepl_buffer_get_full_title
diff --git a/tepl/tepl-buffer.c b/tepl/tepl-buffer.c
index d0aa367..2381a5f 100644
--- a/tepl/tepl-buffer.c
+++ b/tepl/tepl-buffer.c
@@ -1,7 +1,7 @@
 /*
  * This file is part of Tepl, a text editor library.
  *
- * Copyright 2016, 2017 - Sébastien Wilmet <swilmet gnome org>
+ * Copyright 2016-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
@@ -19,6 +19,7 @@
 
 #include "tepl-buffer.h"
 #include "tepl-abstract-factory.h"
+#include "tepl-metadata-manager.h"
 #include "tepl-utils.h"
 
 /**
@@ -30,7 +31,8 @@
  * for a text editor.
  *
  * It also adds an association to a #TeplFile that can be retrieved with
- * tepl_buffer_get_file(). The association cannot change.
+ * tepl_buffer_get_file(). The association cannot change. The same for
+ * #TeplMetadata with tepl_buffer_get_metadata().
  *
  * The properties and signals have the tepl namespace, to avoid potential
  * conflicts in the future if the property or signal is moved to
@@ -42,6 +44,7 @@ typedef struct _TeplBufferPrivate TeplBufferPrivate;
 struct _TeplBufferPrivate
 {
        TeplFile *file;
+       TeplMetadata *metadata;
 
        GtkTextTag *invalid_char_tag;
 
@@ -147,6 +150,7 @@ tepl_buffer_dispose (GObject *object)
        TeplBufferPrivate *priv = tepl_buffer_get_instance_private (TEPL_BUFFER (object));
 
        g_clear_object (&priv->file);
+       g_clear_object (&priv->metadata);
 
        if (priv->idle_cursor_moved_id != 0)
        {
@@ -386,6 +390,8 @@ tepl_buffer_init (TeplBuffer *buffer)
        factory = tepl_abstract_factory_get_singleton ();
        priv->file = tepl_abstract_factory_create_file (factory);
 
+       priv->metadata = tepl_metadata_new ();
+
        g_signal_connect_object (priv->file,
                                 "notify::short-name",
                                 G_CALLBACK (file_short_name_notify_cb),
@@ -433,6 +439,87 @@ tepl_buffer_get_file (TeplBuffer *buffer)
        return priv->file;
 }
 
+/**
+ * tepl_buffer_get_metadata:
+ * @buffer: a #TeplBuffer.
+ *
+ * Returns the #TeplMetadata of @buffer. The returned object is guaranteed to be
+ * the same for the lifetime of @buffer.
+ *
+ * Returns: (transfer none): the associated #TeplMetadata.
+ * Since: 5.0
+ */
+TeplMetadata *
+tepl_buffer_get_metadata (TeplBuffer *buffer)
+{
+       TeplBufferPrivate *priv;
+
+       g_return_val_if_fail (TEPL_IS_BUFFER (buffer), NULL);
+
+       priv = tepl_buffer_get_instance_private (buffer);
+       return priv->metadata;
+}
+
+/**
+ * tepl_buffer_load_metadata_from_metadata_manager:
+ * @buffer: a #TeplBuffer.
+ *
+ * Calls tepl_metadata_manager_copy_from() for #TeplFile:location (if not %NULL)
+ * to the associated #TeplMetadata of @buffer.
+ *
+ * Since: 5.0
+ */
+void
+tepl_buffer_load_metadata_from_metadata_manager (TeplBuffer *buffer)
+{
+       TeplBufferPrivate *priv;
+       GFile *location;
+
+       g_return_if_fail (TEPL_IS_BUFFER (buffer));
+
+       priv = tepl_buffer_get_instance_private (buffer);
+
+       location = tepl_file_get_location (priv->file);
+
+       if (location != NULL)
+       {
+               TeplMetadataManager *manager;
+
+               manager = tepl_metadata_manager_get_singleton ();
+               tepl_metadata_manager_copy_from (manager, location, priv->metadata);
+       }
+}
+
+/**
+ * tepl_buffer_save_metadata_into_metadata_manager:
+ * @buffer: a #TeplBuffer.
+ *
+ * Calls tepl_metadata_manager_merge_into() for #TeplFile:location (if not
+ * %NULL) from the associated #TeplMetadata of @buffer.
+ *
+ * Since: 5.0
+ */
+void
+tepl_buffer_save_metadata_into_metadata_manager (TeplBuffer *buffer)
+{
+       TeplBufferPrivate *priv;
+       GFile *location;
+
+       g_return_if_fail (TEPL_IS_BUFFER (buffer));
+
+       priv = tepl_buffer_get_instance_private (buffer);
+
+       location = tepl_file_get_location (priv->file);
+
+       if (location != NULL)
+       {
+               TeplMetadataManager *manager;
+
+               manager = tepl_metadata_manager_get_singleton ();
+               tepl_metadata_manager_merge_into (manager, location, priv->metadata);
+       }
+}
+
 /**
  * tepl_buffer_is_untouched:
  * @buffer: a #TeplBuffer.
diff --git a/tepl/tepl-buffer.h b/tepl/tepl-buffer.h
index a3b3530..e34574d 100644
--- a/tepl/tepl-buffer.h
+++ b/tepl/tepl-buffer.h
@@ -1,7 +1,7 @@
 /*
  * This file is part of Tepl, a text editor library.
  *
- * Copyright 2016, 2017 - Sébastien Wilmet <swilmet gnome org>
+ * Copyright 2016-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
@@ -26,6 +26,7 @@
 
 #include <gtksourceview/gtksource.h>
 #include <tepl/tepl-file.h>
+#include <tepl/tepl-metadata.h>
 
 G_BEGIN_DECLS
 
@@ -64,6 +65,14 @@ TeplBuffer *         tepl_buffer_new                         (void);
 
 TeplFile *             tepl_buffer_get_file                    (TeplBuffer *buffer);
 
+TeplMetadata *         tepl_buffer_get_metadata                (TeplBuffer *buffer);
+
+void                   tepl_buffer_load_metadata_from_metadata_manager
+                                                               (TeplBuffer *buffer);
+
+void                   tepl_buffer_save_metadata_into_metadata_manager
+                                                               (TeplBuffer *buffer);
+
 gboolean               tepl_buffer_is_untouched                (TeplBuffer *buffer);
 
 gchar *                        tepl_buffer_get_short_title             (TeplBuffer *buffer);


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