[gtksourceview/wip/gedit-document-stuff] file: add the read-only property



commit 9da23808cfb3c2c1d7a18fb58483f3ac2384d686
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Thu Jun 11 17:34:29 2015 +0200

    file: add the read-only property
    
    The code comes from GeditDocument.

 docs/reference/gtksourceview-3.0-sections.txt |    1 +
 gtksourceview/gtksourcefile.c                 |   80 +++++++++++++++++++++++--
 gtksourceview/gtksourcefile.h                 |    6 ++
 gtksourceview/gtksourcefileloader.c           |   14 ++++
 gtksourceview/gtksourcefilesaver.c            |    1 +
 5 files changed, 97 insertions(+), 5 deletions(-)
---
diff --git a/docs/reference/gtksourceview-3.0-sections.txt b/docs/reference/gtksourceview-3.0-sections.txt
index a7e8de0..41970d5 100644
--- a/docs/reference/gtksourceview-3.0-sections.txt
+++ b/docs/reference/gtksourceview-3.0-sections.txt
@@ -252,6 +252,7 @@ gtk_source_file_get_newline_type
 gtk_source_file_get_compression_type
 gtk_source_file_is_externally_modified
 gtk_source_file_is_deleted
+gtk_source_file_is_readonly
 gtk_source_file_set_mount_operation_factory
 <SUBSECTION Standard>
 GTK_SOURCE_FILE
diff --git a/gtksourceview/gtksourcefile.c b/gtksourceview/gtksourcefile.c
index 35f813d..5335a45 100644
--- a/gtksourceview/gtksourcefile.c
+++ b/gtksourceview/gtksourcefile.c
@@ -45,7 +45,8 @@ enum
        PROP_LOCATION,
        PROP_ENCODING,
        PROP_NEWLINE_TYPE,
-       PROP_COMPRESSION_TYPE
+       PROP_COMPRESSION_TYPE,
+       PROP_READ_ONLY
 };
 
 struct _GtkSourceFilePrivate
@@ -68,6 +69,7 @@ struct _GtkSourceFilePrivate
 
        guint externally_modified : 1;
        guint deleted : 1;
+       guint readonly : 1;
 };
 
 G_DEFINE_TYPE_WITH_PRIVATE (GtkSourceFile, gtk_source_file, G_TYPE_OBJECT)
@@ -102,6 +104,10 @@ gtk_source_file_get_property (GObject    *object,
                        g_value_set_enum (value, file->priv->compression_type);
                        break;
 
+               case PROP_READ_ONLY:
+                       g_value_set_boolean (value, file->priv->readonly);
+                       break;
+
                default:
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                        break;
@@ -224,6 +230,23 @@ gtk_source_file_class_init (GtkSourceFileClass *klass)
                                                            GTK_SOURCE_COMPRESSION_TYPE_NONE,
                                                            G_PARAM_READABLE |
                                                            G_PARAM_STATIC_STRINGS));
+
+       /**
+        * GtkSourceFile:read-only:
+        *
+        * Whether the file is read-only or not. The value of this property is
+        * not updated automatically (there is no file monitors).
+        *
+        * Since: 3.18
+        */
+       g_object_class_install_property (object_class,
+                                        PROP_READ_ONLY,
+                                        g_param_spec_boolean ("read-only",
+                                                              "Read Only",
+                                                              "",
+                                                              FALSE,
+                                                              G_PARAM_READABLE |
+                                                              G_PARAM_STATIC_STRINGS));
 }
 
 static void
@@ -467,7 +490,8 @@ check_file_on_disk (GtkSourceFile *file)
        }
 
        info = g_file_query_info (file->priv->location,
-                                 G_FILE_ATTRIBUTE_TIME_MODIFIED,
+                                 G_FILE_ATTRIBUTE_TIME_MODIFIED ","
+                                 G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE,
                                  G_FILE_QUERY_INFO_NONE,
                                  NULL,
                                  NULL);
@@ -475,9 +499,11 @@ check_file_on_disk (GtkSourceFile *file)
        if (info == NULL)
        {
                file->priv->deleted = TRUE;
+               return;
        }
-       else if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_TIME_MODIFIED) &&
-                file->priv->modification_time_set)
+
+       if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_TIME_MODIFIED) &&
+           file->priv->modification_time_set)
        {
                GTimeVal timeval;
 
@@ -493,7 +519,16 @@ check_file_on_disk (GtkSourceFile *file)
                }
        }
 
-       g_clear_object (&info);
+       if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE))
+       {
+               gboolean readonly;
+
+               readonly = !g_file_info_get_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE);
+
+               _gtk_source_file_set_readonly (file, readonly);
+       }
+
+       g_object_unref (info);
 }
 
 void
@@ -559,3 +594,38 @@ gtk_source_file_is_deleted (GtkSourceFile *file)
 
        return file->priv->deleted;
 }
+
+void
+_gtk_source_file_set_readonly (GtkSourceFile *file,
+                              gboolean       readonly)
+{
+       g_return_if_fail (GTK_SOURCE_IS_FILE (file));
+
+       readonly = readonly != FALSE;
+
+       if (file->priv->readonly != readonly)
+       {
+               file->priv->readonly = readonly;
+               g_object_notify (G_OBJECT (file), "read-only");
+       }
+}
+
+/**
+ * gtk_source_file_is_readonly:
+ * @file: a #GtkSourceFile.
+ *
+ * Checks synchronously whether the file is read-only. If the
+ * #GtkSourceFile:location is %NULL, returns FALSE.
+ *
+ * Returns: whether the file is read-only.
+ * Since: 3.18
+ */
+gboolean
+gtk_source_file_is_readonly (GtkSourceFile *file)
+{
+       g_return_val_if_fail (GTK_SOURCE_IS_FILE (file), FALSE);
+
+       check_file_on_disk (file);
+
+       return file->priv->readonly;
+}
diff --git a/gtksourceview/gtksourcefile.h b/gtksourceview/gtksourcefile.h
index ddb7987..6bc956c 100644
--- a/gtksourceview/gtksourcefile.h
+++ b/gtksourceview/gtksourcefile.h
@@ -91,6 +91,8 @@ gboolean       gtk_source_file_is_externally_modified         (GtkSourceFile *file);
 
 gboolean        gtk_source_file_is_deleted                     (GtkSourceFile *file);
 
+gboolean        gtk_source_file_is_readonly                    (GtkSourceFile *file);
+
 G_GNUC_INTERNAL
 void            _gtk_source_file_set_encoding                  (GtkSourceFile           *file,
                                                                 const GtkSourceEncoding *encoding);
@@ -122,6 +124,10 @@ G_GNUC_INTERNAL
 void            _gtk_source_file_set_deleted                   (GtkSourceFile *file,
                                                                 gboolean       deleted);
 
+G_GNUC_INTERNAL
+void            _gtk_source_file_set_readonly                  (GtkSourceFile *file,
+                                                                gboolean       readonly);
+
 G_END_DECLS
 
 #endif /* __GTK_SOURCE_FILE_H__ */
diff --git a/gtksourceview/gtksourcefileloader.c b/gtksourceview/gtksourcefileloader.c
index 244b579..d8b8b8e 100644
--- a/gtksourceview/gtksourcefileloader.c
+++ b/gtksourceview/gtksourcefileloader.c
@@ -1140,6 +1140,20 @@ gtk_source_file_loader_load_finish (GtkSourceFileLoader  *loader,
                        g_file_info_get_modification_time (loader->priv->info, &modification_time);
                        _gtk_source_file_set_modification_time (loader->priv->file, modification_time);
                }
+
+               if (g_file_info_has_attribute (loader->priv->info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE))
+               {
+                       gboolean readonly;
+
+                       readonly = !g_file_info_get_attribute_boolean (loader->priv->info,
+                                                                      G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE);
+
+                       _gtk_source_file_set_readonly (loader->priv->file, readonly);
+               }
+               else
+               {
+                       _gtk_source_file_set_readonly (loader->priv->file, FALSE);
+               }
        }
 
        reset (loader);
diff --git a/gtksourceview/gtksourcefilesaver.c b/gtksourceview/gtksourcefilesaver.c
index d88d910..bd49e03 100644
--- a/gtksourceview/gtksourcefilesaver.c
+++ b/gtksourceview/gtksourcefilesaver.c
@@ -1406,6 +1406,7 @@ gtk_source_file_saver_save_finish (GtkSourceFileSaver  *saver,
 
                _gtk_source_file_set_externally_modified (saver->priv->file, FALSE);
                _gtk_source_file_set_deleted (saver->priv->file, FALSE);
+               _gtk_source_file_set_readonly (saver->priv->file, FALSE);
 
                if (g_file_info_has_attribute (saver->priv->info, G_FILE_ATTRIBUTE_TIME_MODIFIED))
                {


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