[gtranslator] Display an error at save time if file is readonly



commit 96f166e145a43bc64833363aadeaf25cf903f8d5
Author: Francisco Puga <fran puga gmail com>
Date:   Thu Aug 20 20:50:00 2009 +0200

    Display an error at save time if file is readonly

 src/po.c |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/po.h |    1 +
 2 files changed, 53 insertions(+), 0 deletions(-)
---
diff --git a/src/po.c b/src/po.c
index c648d80..dcf384d 100644
--- a/src/po.c
+++ b/src/po.c
@@ -283,6 +283,45 @@ po_file_is_empty (po_file_t file)
   return TRUE;
 }
 
+
+/**
+ * is_read_only:
+ * @location: a GFile Object that represents the file to check
+ *
+ * This method is copied from gedit, file gedit-commands-file.c
+ *
+ * Returns: False if file is writeable. True if file doesn't exists, is read-only or read-only attribute can't be check
+ */
+static gboolean
+is_read_only (const gchar *filename)
+{
+  gboolean ret = TRUE; /* default to read only */
+  GFileInfo *info;
+  GFile *location;
+
+  location = g_file_new_for_path (filename);
+  info = g_file_query_info (location,
+			    G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE,
+			    G_FILE_QUERY_INFO_NONE,
+			    NULL,
+			    NULL);
+  g_object_unref (location);
+
+  if (info != NULL)
+    {
+      if (g_file_info_has_attribute (info,
+				     G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE))
+	{
+	  ret = !g_file_info_get_attribute_boolean (info,
+						    G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE);
+	}
+
+      g_object_unref (info);
+    }
+
+  return ret;
+}
+
 /***************************** Public funcs ***********************************/
 
 /**
@@ -842,6 +881,19 @@ gtranslator_po_save_file (GtranslatorPo * po, GError ** error)
       return;
     }
 
+
+  if (is_read_only (filename))
+    {
+      g_set_error (error,
+		   GTR_PO_ERROR,
+		   GTR_PO_ERROR_READONLY,
+		   _("The file %s is read-only, and can not be overwritten"),
+		   filename);
+      g_free (filename);
+      return;
+    }
+
+
   /*
    * Check if the file is right
    */
diff --git a/src/po.h b/src/po.h
index ebad674..40c0e9e 100644
--- a/src/po.h
+++ b/src/po.h
@@ -76,6 +76,7 @@ enum
   GTR_PO_ERROR_FILENAME,
   GTR_PO_ERROR_RECOVERY,
   GTR_PO_ERROR_FILE_EMPTY,
+  GTR_PO_ERROR_READONLY,
   GTR_PO_ERROR_OTHER,
 };
 



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