[gtksourceview/wip/gedit-document-stuff] file: add is_externally_modified() and is_deleted() functions



commit 7df0588baee805a7ad3507ff2ddaae9023467e77
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Thu Jun 11 14:09:40 2015 +0200

    file: add is_externally_modified() and is_deleted() functions
    
    The code comes from GeditDocument.

 docs/reference/gtksourceview-3.0-sections.txt |    2 +
 gtksourceview/gtksourcefile.c                 |  112 ++++++++++++++++++++++++-
 gtksourceview/gtksourcefile.h                 |   14 +++-
 gtksourceview/gtksourcefileloader.c           |    3 +
 gtksourceview/gtksourcefilesaver.c            |    3 +
 5 files changed, 132 insertions(+), 2 deletions(-)
---
diff --git a/docs/reference/gtksourceview-3.0-sections.txt b/docs/reference/gtksourceview-3.0-sections.txt
index f93bb9f..a7e8de0 100644
--- a/docs/reference/gtksourceview-3.0-sections.txt
+++ b/docs/reference/gtksourceview-3.0-sections.txt
@@ -250,6 +250,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_is_externally_modified
+gtk_source_file_is_deleted
 gtk_source_file_set_mount_operation_factory
 <SUBSECTION Standard>
 GTK_SOURCE_FILE
diff --git a/gtksourceview/gtksourcefile.c b/gtksourceview/gtksourcefile.c
index 8926660..15875ee 100644
--- a/gtksourceview/gtksourcefile.c
+++ b/gtksourceview/gtksourcefile.c
@@ -2,7 +2,7 @@
 /* gtksourcefile.c
  * This file is part of GtkSourceView
  *
- * Copyright (C) 2014 - Sébastien Wilmet <swilmet gnome org>
+ * Copyright (C) 2014, 2015 - 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
@@ -65,6 +65,9 @@ struct _GtkSourceFilePrivate
        GTimeVal modification_time;
 
        guint modification_time_set : 1;
+
+       guint externally_modified : 1;
+       guint deleted : 1;
 };
 
 G_DEFINE_TYPE_WITH_PRIVATE (GtkSourceFile, gtk_source_file, G_TYPE_OBJECT)
@@ -275,6 +278,9 @@ gtk_source_file_set_location (GtkSourceFile *file,
 
                /* The modification_time is for the old location. */
                file->priv->modification_time_set = FALSE;
+
+               file->priv->externally_modified = FALSE;
+               file->priv->deleted = FALSE;
        }
 }
 
@@ -457,3 +463,107 @@ _gtk_source_file_set_modification_time (GtkSourceFile *file,
                file->priv->modification_time_set = TRUE;
        }
 }
+
+static void
+check_file_on_disk (GtkSourceFile *file)
+{
+       GFileInfo *info;
+
+       if (file->priv->location == NULL)
+       {
+               return;
+       }
+
+       info = g_file_query_info (file->priv->location,
+                                 G_FILE_ATTRIBUTE_TIME_MODIFIED,
+                                 G_FILE_QUERY_INFO_NONE,
+                                 NULL,
+                                 NULL);
+
+       if (info == NULL)
+       {
+               file->priv->deleted = TRUE;
+       }
+       else if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_TIME_MODIFIED) &&
+                file->priv->modification_time_set)
+       {
+               GTimeVal timeval;
+
+               g_file_info_get_modification_time (info, &timeval);
+
+               /* Note that the modification time can even go backwards if the
+                * user is copying over an old file.
+                */
+               if (timeval.tv_sec != file->priv->modification_time.tv_sec ||
+                   timeval.tv_usec != file->priv->modification_time.tv_usec)
+               {
+                       file->priv->externally_modified = TRUE;
+               }
+       }
+
+       g_clear_object (&info);
+}
+
+void
+_gtk_source_file_set_externally_modified (GtkSourceFile *file,
+                                         gboolean       externally_modified)
+{
+       g_return_if_fail (GTK_SOURCE_IS_FILE (file));
+
+       file->priv->externally_modified = externally_modified != FALSE;
+}
+
+/**
+ * gtk_source_file_is_externally_modified:
+ * @file: a #GtkSourceFile.
+ *
+ * Checks synchronously whether the file is externally modified. If the
+ * #GtkSourceFile:location is %NULL, returns FALSE.
+ *
+ * Returns: whether the file is externally modified.
+ * Since: 3.18
+ */
+gboolean
+gtk_source_file_is_externally_modified (GtkSourceFile *file)
+{
+       g_return_val_if_fail (GTK_SOURCE_IS_FILE (file), FALSE);
+
+       if (!file->priv->externally_modified)
+       {
+               check_file_on_disk (file);
+       }
+
+       return file->priv->externally_modified;
+}
+
+void
+_gtk_source_file_set_deleted (GtkSourceFile *file,
+                             gboolean       deleted)
+{
+       g_return_if_fail (GTK_SOURCE_IS_FILE (file));
+
+       file->priv->deleted = deleted != FALSE;
+}
+
+/**
+ * gtk_source_file_is_deleted:
+ * @file: a #GtkSourceFile.
+ *
+ * Checks synchronously whether the file has been deleted. If the
+ * #GtkSourceFile:location is %NULL, returns FALSE.
+ *
+ * Returns: whether the file has been deleted.
+ * Since: 3.18
+ */
+gboolean
+gtk_source_file_is_deleted (GtkSourceFile *file)
+{
+       g_return_val_if_fail (GTK_SOURCE_IS_FILE (file), FALSE);
+
+       if (!file->priv->deleted)
+       {
+               check_file_on_disk (file);
+       }
+
+       return file->priv->deleted;
+}
diff --git a/gtksourceview/gtksourcefile.h b/gtksourceview/gtksourcefile.h
index e4859f1..ddb7987 100644
--- a/gtksourceview/gtksourcefile.h
+++ b/gtksourceview/gtksourcefile.h
@@ -2,7 +2,7 @@
 /* gtksourcefile.h
  * This file is part of GtkSourceView
  *
- * Copyright (C) 2014 - Sébastien Wilmet <swilmet gnome org>
+ * Copyright (C) 2014, 2015 - 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
@@ -87,6 +87,10 @@ void          gtk_source_file_set_mount_operation_factory    (GtkSourceFile
                                                                 gpointer                        user_data,
                                                                 GDestroyNotify                  notify);
 
+gboolean        gtk_source_file_is_externally_modified         (GtkSourceFile *file);
+
+gboolean        gtk_source_file_is_deleted                     (GtkSourceFile *file);
+
 G_GNUC_INTERNAL
 void            _gtk_source_file_set_encoding                  (GtkSourceFile           *file,
                                                                 const GtkSourceEncoding *encoding);
@@ -110,6 +114,14 @@ G_GNUC_INTERNAL
 void            _gtk_source_file_set_modification_time         (GtkSourceFile *file,
                                                                 GTimeVal       modification_time);
 
+G_GNUC_INTERNAL
+void            _gtk_source_file_set_externally_modified       (GtkSourceFile *file,
+                                                                gboolean       externally_modified);
+
+G_GNUC_INTERNAL
+void            _gtk_source_file_set_deleted                   (GtkSourceFile *file,
+                                                                gboolean       deleted);
+
 G_END_DECLS
 
 #endif /* __GTK_SOURCE_FILE_H__ */
diff --git a/gtksourceview/gtksourcefileloader.c b/gtksourceview/gtksourcefileloader.c
index cfec545..244b579 100644
--- a/gtksourceview/gtksourcefileloader.c
+++ b/gtksourceview/gtksourcefileloader.c
@@ -1131,6 +1131,9 @@ gtk_source_file_loader_load_finish (GtkSourceFileLoader  *loader,
                _gtk_source_file_set_compression_type (loader->priv->file,
                                                       loader->priv->auto_detected_compression_type);
 
+               _gtk_source_file_set_externally_modified (loader->priv->file, FALSE);
+               _gtk_source_file_set_deleted (loader->priv->file, FALSE);
+
                if (g_file_info_has_attribute (loader->priv->info, G_FILE_ATTRIBUTE_TIME_MODIFIED))
                {
                        GTimeVal modification_time;
diff --git a/gtksourceview/gtksourcefilesaver.c b/gtksourceview/gtksourcefilesaver.c
index 0c6e3c9..d88d910 100644
--- a/gtksourceview/gtksourcefilesaver.c
+++ b/gtksourceview/gtksourcefilesaver.c
@@ -1404,6 +1404,9 @@ gtk_source_file_saver_save_finish (GtkSourceFileSaver  *saver,
                _gtk_source_file_set_compression_type (saver->priv->file,
                                                       saver->priv->compression_type);
 
+               _gtk_source_file_set_externally_modified (saver->priv->file, FALSE);
+               _gtk_source_file_set_deleted (saver->priv->file, FALSE);
+
                if (g_file_info_has_attribute (saver->priv->info, G_FILE_ATTRIBUTE_TIME_MODIFIED))
                {
                        GTimeVal modification_time;


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