[gtksourceview/wip/metadata: 2/8] File: add get/set_metadata()



commit f054bbf11edd62c2632a8ba050155c13d48182a5
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Apr 29 12:45:49 2016 +0200

    File: add get/set_metadata()

 docs/reference/gtksourceview-3.0-sections.txt |    2 +
 gtksourceview/gtksourcefile.c                 |   79 ++++++++++++++++++++++++-
 gtksourceview/gtksourcefile.h                 |   11 +++-
 3 files changed, 90 insertions(+), 2 deletions(-)
---
diff --git a/docs/reference/gtksourceview-3.0-sections.txt b/docs/reference/gtksourceview-3.0-sections.txt
index f411703..be54a3c 100644
--- a/docs/reference/gtksourceview-3.0-sections.txt
+++ b/docs/reference/gtksourceview-3.0-sections.txt
@@ -255,6 +255,8 @@ gtk_source_file_set_location
 gtk_source_file_get_encoding
 gtk_source_file_get_newline_type
 gtk_source_file_get_compression_type
+gtk_source_file_get_metadata
+gtk_source_file_set_metadata
 gtk_source_file_check_file_on_disk
 gtk_source_file_is_local
 gtk_source_file_is_externally_modified
diff --git a/gtksourceview/gtksourcefile.c b/gtksourceview/gtksourcefile.c
index 7d1b111..391b749 100644
--- a/gtksourceview/gtksourcefile.c
+++ b/gtksourceview/gtksourcefile.c
@@ -2,7 +2,7 @@
 /* gtksourcefile.c
  * This file is part of GtkSourceView
  *
- * Copyright (C) 2014, 2015 - Sébastien Wilmet <swilmet gnome org>
+ * Copyright (C) 2014-2016 - Sébastien Wilmet <swilmet gnome org>
  *
  * GtkSourceView is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -64,6 +64,8 @@ struct _GtkSourceFilePrivate
        gpointer mount_operation_userdata;
        GDestroyNotify mount_operation_notify;
 
+       GFileInfo *metadata;
+
        /* Last known modification time of 'location'. The value is updated on a
         * file loading or file saving.
         */
@@ -148,6 +150,7 @@ gtk_source_file_dispose (GObject *object)
        GtkSourceFile *file = GTK_SOURCE_FILE (object);
 
        g_clear_object (&file->priv->location);
+       g_clear_object (&file->priv->metadata);
 
        if (file->priv->mount_operation_notify != NULL)
        {
@@ -261,6 +264,7 @@ gtk_source_file_init (GtkSourceFile *file)
        file->priv->encoding = NULL;
        file->priv->newline_type = GTK_SOURCE_NEWLINE_TYPE_LF;
        file->priv->compression_type = GTK_SOURCE_COMPRESSION_TYPE_NONE;
+       file->priv->metadata = g_file_info_new ();
 }
 
 /**
@@ -669,3 +673,76 @@ gtk_source_file_is_readonly (GtkSourceFile *file)
 
        return file->priv->readonly;
 }
+
+/**
+ * gtk_source_file_get_metadata:
+ * @file: a #GtkSourceFile.
+ * @key: the name of the metadata.
+ *
+ * Gets a metadata.
+ *
+ * Returns: (nullable): the value of the metadata, or %NULL. Free with g_free().
+ * Since: 3.22
+ */
+gchar *
+gtk_source_file_get_metadata (GtkSourceFile *file,
+                             const gchar   *key)
+{
+       gchar *attribute_key;
+       gchar *value = NULL;
+
+       g_return_val_if_fail (GTK_SOURCE_IS_FILE (file), NULL);
+       g_return_val_if_fail (key != NULL && key[0] != '\0', NULL);
+
+       attribute_key = g_strconcat ("metadata::", key, NULL);
+
+       if (g_file_info_has_attribute (file->priv->metadata, attribute_key) &&
+           g_file_info_get_attribute_type (file->priv->metadata, attribute_key) == 
G_FILE_ATTRIBUTE_TYPE_STRING)
+       {
+               value = g_strdup (g_file_info_get_attribute_string (file->priv->metadata, attribute_key));
+       }
+
+       g_free (attribute_key);
+       return value;
+}
+
+/**
+ * gtk_source_file_set_metadata:
+ * @file: a #GtkSourceFile.
+ * @key: the name of the metadata.
+ * @value: (nullable): the value of the metadata, or %NULL to unset.
+ *
+ * Sets a metadata. It's preferable that @key starts with a namespace, to not
+ * get metadata conflicts between applications.
+ *
+ * Since: 3.22
+ */
+void
+gtk_source_file_set_metadata (GtkSourceFile *file,
+                             const gchar   *key,
+                             const gchar   *value)
+{
+       gchar *attribute_key;
+
+       g_return_if_fail (GTK_SOURCE_IS_FILE (file));
+       g_return_if_fail (key != NULL && key[0] != '\0');
+
+       attribute_key = g_strconcat ("metadata::", key, NULL);
+
+       if (value != NULL)
+       {
+               g_file_info_set_attribute_string (file->priv->metadata,
+                                                 attribute_key,
+                                                 value);
+       }
+       else
+       {
+               /* Unset the key */
+               g_file_info_set_attribute (file->priv->metadata,
+                                          attribute_key,
+                                          G_FILE_ATTRIBUTE_TYPE_INVALID,
+                                          NULL);
+       }
+
+       g_free (attribute_key);
+}
diff --git a/gtksourceview/gtksourcefile.h b/gtksourceview/gtksourcefile.h
index 9be712f..1685abe 100644
--- a/gtksourceview/gtksourcefile.h
+++ b/gtksourceview/gtksourcefile.h
@@ -2,7 +2,7 @@
 /* gtksourcefile.h
  * This file is part of GtkSourceView
  *
- * Copyright (C) 2014, 2015 - Sébastien Wilmet <swilmet gnome org>
+ * Copyright (C) 2014-2016 - Sébastien Wilmet <swilmet gnome org>
  *
  * GtkSourceView is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -110,6 +110,15 @@ gboolean    gtk_source_file_is_deleted                     (GtkSourceFile *file);
 GTK_SOURCE_AVAILABLE_IN_3_18
 gboolean        gtk_source_file_is_readonly                    (GtkSourceFile *file);
 
+GTK_SOURCE_AVAILABLE_IN_3_22
+gchar *                 gtk_source_file_get_metadata                   (GtkSourceFile *file,
+                                                                const gchar   *key);
+
+GTK_SOURCE_AVAILABLE_IN_3_22
+void            gtk_source_file_set_metadata                   (GtkSourceFile *file,
+                                                                const gchar   *key,
+                                                                const gchar   *value);
+
 G_GNUC_INTERNAL
 void            _gtk_source_file_set_encoding                  (GtkSourceFile           *file,
                                                                 const GtkSourceEncoding *encoding);


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