[Xfce4-commits] <mousepad:master> * mousepad/mousepad-file.c: Don't set an error and return false on externally modified when the file does not exist. This fixes an error when saving a new file. * mousepad/mousepad-file.c: Emit signal when the readonly status changes. * mousepad/mousepad-document.c: Use the readonly signal to update the label color. This fixes a readonly-colored label when saving a new file.

Nick Schermer noreply at xfce.org
Sat May 5 21:31:16 CEST 2012


Updating branch refs/heads/master
         to e6deae974c002619ee11c3d68d92ec768b235ac5 (commit)
       from 790cd28e036aec098aaee8cc03b101f0c436580d (commit)

commit e6deae974c002619ee11c3d68d92ec768b235ac5
Author: Nick Schermer <nick at xfce.org>
Date:   Sun Mar 2 13:51:40 2008 +0000

    	* mousepad/mousepad-file.c: Don't set an error and return false on
    	  externally modified when the file does not exist. This fixes
    	  an error when saving a new file.
    	* mousepad/mousepad-file.c: Emit signal when the readonly status
    	  changes.
    	* mousepad/mousepad-document.c: Use the readonly signal to update
    	  the label color. This fixes a readonly-colored label when saving
    	  a new file.
    
    (Old svn revision: 26656)

 ChangeLog                    |   12 +++++++
 mousepad/mousepad-document.c |   23 ++++++++-----
 mousepad/mousepad-file.c     |   72 +++++++++++++++++++++++++++++++----------
 3 files changed, 80 insertions(+), 27 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 11f34d4..fcdbe88 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2008-03-02	Nick Schermer <nick at xfce.org>
+
+	* mousepad/mousepad-file.c: Don't set an error and return false on
+	  externally modified when the file does not exist. This fixes
+	  an error when saving a new file.
+	* mousepad/mousepad-file.c: Emit signal when the readonly status 
+	  changes.
+	* mousepad/mousepad-document.c: Use the readonly signal to update
+	  the label color. This fixes a readonly-colored label when saving
+	  a new file.
+
+
 2008-02-17	Nick Schermer <nick at xfce.org>
 
 	* mousepad/mousepad-search-bar.c: Select the text in the search
diff --git a/mousepad/mousepad-document.c b/mousepad/mousepad-document.c
index d637297..fefab92 100644
--- a/mousepad/mousepad-document.c
+++ b/mousepad/mousepad-document.c
@@ -242,6 +242,7 @@ mousepad_document_init (MousepadDocument *document)
   g_signal_connect (G_OBJECT (document->buffer), "notify::cursor-position", G_CALLBACK (mousepad_document_notify_cursor_position), document);
   g_signal_connect (G_OBJECT (document->buffer), "notify::has-selection", G_CALLBACK (mousepad_document_notify_has_selection), document);
   g_signal_connect_swapped (G_OBJECT (document->buffer), "modified-changed", G_CALLBACK (mousepad_document_label_color), document);
+  g_signal_connect_swapped (G_OBJECT (document->file), "readonly-changed", G_CALLBACK (mousepad_document_label_color), document);
   g_signal_connect (G_OBJECT (document->textview), "notify::overwrite", G_CALLBACK (mousepad_document_notify_overwrite), document);
   g_signal_connect (G_OBJECT (document->textview), "drag-data-received", G_CALLBACK (mousepad_document_drag_data_received), document);
 }
@@ -415,9 +416,9 @@ mousepad_document_filename_changed (MousepadDocument *document,
 static void
 mousepad_document_label_color (MousepadDocument *document)
 {
-  const GdkColor green = {0, 0x0000, 0x9999, 0x0000};
-  const GdkColor red   = {0, 0xffff, 0x0000, 0x0000};
-  gboolean       readonly, modified;
+  GdkColor  green = {0, 0x0000, 0x9999, 0x0000};
+  GdkColor  red   = {0, 0xffff, 0x0000, 0x0000};
+  GdkColor *color;
 
   _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
   _mousepad_return_if_fail (GTK_IS_TEXT_BUFFER (document->buffer));
@@ -425,13 +426,17 @@ mousepad_document_label_color (MousepadDocument *document)
 
   if (document->priv->label)
     {
-      /* get states */
-      readonly = mousepad_file_get_read_only (document->file);
-      modified = gtk_text_buffer_get_modified (document->buffer);
-
+      /* label color */
+      if (gtk_text_buffer_get_modified (document->buffer))
+        color = &green;
+      else if (mousepad_file_get_read_only (document->file))
+        color = &red;
+      else
+        color = NULL;
+       
       /* update colors */
-      gtk_widget_modify_fg (document->priv->label, GTK_STATE_NORMAL, modified ? &red : (readonly ? &green : NULL));
-      gtk_widget_modify_fg (document->priv->label, GTK_STATE_ACTIVE, modified ? &red : (readonly ? &green : NULL));
+      gtk_widget_modify_fg (document->priv->label, GTK_STATE_NORMAL, color);
+      gtk_widget_modify_fg (document->priv->label, GTK_STATE_ACTIVE, color);
     }
 }
 
diff --git a/mousepad/mousepad-file.c b/mousepad/mousepad-file.c
index 6ae1d6f..81fec42 100644
--- a/mousepad/mousepad-file.c
+++ b/mousepad/mousepad-file.c
@@ -25,6 +25,9 @@
 #ifdef HAVE_STRING_H
 #include <string.h>
 #endif
+#ifdef HAVE_ERRNO_H
+#include <errno.h>
+#endif
 
 #include <glib.h>
 #include <glib/gstdio.h>
@@ -41,6 +44,7 @@ enum
 {
   /* EXTERNALLY_MODIFIED, */
   FILENAME_CHANGED,
+  READONLY_CHANGED,
   LAST_SIGNAL
 };
 
@@ -77,6 +81,8 @@ struct _MousepadFile
 static void  mousepad_file_class_init       (MousepadFileClass  *klass);
 static void  mousepad_file_init             (MousepadFile       *file);
 static void  mousepad_file_finalize         (GObject            *object);
+static void  mousepad_file_set_readonly     (MousepadFile       *file,
+                                             gboolean            readonly);
 
 
 
@@ -107,6 +113,14 @@ mousepad_file_class_init (MousepadFileClass *klass)
                   G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
 #endif
 
+  file_signals[READONLY_CHANGED] =
+    g_signal_new (I_("readonly-changed"),
+                  G_TYPE_FROM_CLASS (gobject_class),
+                  G_SIGNAL_RUN_LAST,
+                  0, NULL, NULL,
+                  g_cclosure_marshal_VOID__BOOLEAN,
+                  G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
+  
   file_signals[FILENAME_CHANGED] =
     g_signal_new (I_("filename-changed"),
                   G_TYPE_FROM_CLASS (gobject_class),
@@ -148,6 +162,24 @@ mousepad_file_finalize (GObject *object)
 
 
 
+static void
+mousepad_file_set_readonly (MousepadFile *file,
+                            gboolean      readonly)
+{
+  _mousepad_return_if_fail (MOUSEPAD_IS_FILE (file));
+  
+  if (G_LIKELY (file->readonly != readonly))
+    {
+      /* store new value */
+      file->readonly = readonly;
+      
+      /* emit signal */
+      g_signal_emit (G_OBJECT (file), file_signals[READONLY_CHANGED], 0, readonly);
+    }  
+}
+
+
+
 MousepadFile *
 mousepad_file_new (GtkTextBuffer *buffer)
 {
@@ -280,7 +312,8 @@ mousepad_file_open (MousepadFile  *file,
   /* check if the file exists, if not, it's a filename from the command line */
   if (g_file_test (file->filename, G_FILE_TEST_EXISTS) == FALSE)
     {
-      file->readonly = FALSE;
+      /* update readonly status */
+      mousepad_file_set_readonly (file, FALSE);
 
       return TRUE;
     }
@@ -373,7 +406,7 @@ mousepad_file_open (MousepadFile  *file,
       if (G_LIKELY (g_lstat (file->filename, &statb) == 0));
         {
           /* store the readonly mode */
-          file->readonly = !((statb.st_mode & S_IWUSR) != 0);
+          mousepad_file_set_readonly (file, !((statb.st_mode & S_IWUSR) != 0));
 
           /* store the file modification time */
           file->mtime = statb.st_mtime;
@@ -494,7 +527,7 @@ mousepad_file_save (MousepadFile  *file,
       gtk_text_buffer_set_modified (file->buffer, FALSE);
 
       /* we saved succesfully */
-      file->readonly = FALSE;
+      mousepad_file_set_readonly (file, FALSE);
 
       failed:
 
@@ -549,25 +582,28 @@ mousepad_file_get_externally_modified (MousepadFile  *file,
                                        GError       **error)
 {
   struct stat statb;
-  gboolean    modified = TRUE;
+  GFileError  error_code;
 
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_FILE (file), FALSE);
-  _mousepad_return_val_if_fail (file->filename != NULL, FALSE);
+  _mousepad_return_val_if_fail (MOUSEPAD_IS_FILE (file), TRUE);
+  _mousepad_return_val_if_fail (file->filename != NULL, TRUE);
   _mousepad_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
+  /* check if our modification time differs from the current one */
   if (G_LIKELY (g_lstat (file->filename, &statb) == 0))
-    {
-      /* check if our modification time differs from the current one */
-      modified = (file->mtime > 0 && statb.st_mtime != file->mtime);
-    }
-  else if (error != NULL)
-    {
-      /* failed to stat the file */
-      g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
-                   _("Failed to read the status of \"%s\""), file->filename);
-    }
-
-  return modified;
+    return (file->mtime > 0 && statb.st_mtime != file->mtime);
+    
+  /* get the error code */
+  error_code = g_file_error_from_errno (errno);
+  
+  /* file does not exists, nothing wrong with that */
+  if (G_LIKELY (error_code == G_FILE_ERROR_NOENT))
+    return FALSE;
+
+  /* set an error */
+  if (error != NULL)
+    g_set_error (error, G_FILE_ERROR, error_code, _("Failed to read the status of \"%s\""), file->filename);
+
+  return TRUE;
 }
 
 


More information about the Xfce4-commits mailing list