Author: nick
Date: 2008-03-02 13:51:40 +0000 (Sun, 02 Mar 2008)
New Revision: 26656

Modified:
   mousepad/branches/nick_0_3/ChangeLog
   mousepad/branches/nick_0_3/mousepad/mousepad-document.c
   mousepad/branches/nick_0_3/mousepad/mousepad-file.c
Log:
        * 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.

Modified: mousepad/branches/nick_0_3/ChangeLog
===================================================================
--- mousepad/branches/nick_0_3/ChangeLog        2008-03-02 10:37:59 UTC (rev 
26655)
+++ mousepad/branches/nick_0_3/ChangeLog        2008-03-02 13:51:40 UTC (rev 
26656)
@@ -1,3 +1,15 @@
+2008-03-02     Nick Schermer <[EMAIL PROTECTED]>
+
+       * 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 <[EMAIL PROTECTED]>
 
        * mousepad/mousepad-search-bar.c: Select the text in the search

Modified: mousepad/branches/nick_0_3/mousepad/mousepad-document.c
===================================================================
--- mousepad/branches/nick_0_3/mousepad/mousepad-document.c     2008-03-02 
10:37:59 UTC (rev 26655)
+++ mousepad/branches/nick_0_3/mousepad/mousepad-document.c     2008-03-02 
13:51:40 UTC (rev 26656)
@@ -242,6 +242,7 @@
   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 @@
 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 @@
 
   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);
     }
 }
 

Modified: mousepad/branches/nick_0_3/mousepad/mousepad-file.c
===================================================================
--- mousepad/branches/nick_0_3/mousepad/mousepad-file.c 2008-03-02 10:37:59 UTC 
(rev 26655)
+++ mousepad/branches/nick_0_3/mousepad/mousepad-file.c 2008-03-02 13:51:40 UTC 
(rev 26656)
@@ -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 @@
 {
   /* EXTERNALLY_MODIFIED, */
   FILENAME_CHANGED,
+  READONLY_CHANGED,
   LAST_SIGNAL
 };
 
@@ -77,6 +81,8 @@
 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 @@
                   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 @@
 
 
 
+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 @@
   /* 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 @@
       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 @@
       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 @@
                                        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 (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;
 
-  return modified;
+  /* 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;
 }
 
 

_______________________________________________
Xfce4-commits mailing list
Xfce4-commits@xfce.org
http://foo-projects.org/mailman/listinfo/xfce4-commits

Reply via email to