r21565 - gnucash/trunk/src - Check the read-only attribute in the XML backend as well and refuse to save if we are read-only.

Christian Stimming cstim at code.gnucash.org
Sun Nov 13 16:26:21 EST 2011


Author: cstim
Date: 2011-11-13 16:26:21 -0500 (Sun, 13 Nov 2011)
New Revision: 21565
Trac: http://svn.gnucash.org/trac/changeset/21565

Modified:
   gnucash/trunk/src/backend/xml/gnc-backend-xml.c
   gnucash/trunk/src/gnome-utils/gnc-main-window.c
Log:
Check the read-only attribute in the XML backend as well and refuse to save if we are read-only.

Also, add a "(read-only)" suffix in the window title if we are read-only.

Modified: gnucash/trunk/src/backend/xml/gnc-backend-xml.c
===================================================================
--- gnucash/trunk/src/backend/xml/gnc-backend-xml.c	2011-11-13 21:01:31 UTC (rev 21564)
+++ gnucash/trunk/src/backend/xml/gnc-backend-xml.c	2011-11-13 21:26:21 UTC (rev 21565)
@@ -340,8 +340,28 @@
 
     if (!ignore_lock && !gnc_xml_be_get_file_lock (be))
     {
+        // We should not ignore the lock, but couldn't get it. The
+        // be_get_file_lock() already set the appropriate backend_error in this
+        // case, so we just return here.
         g_free (be->lockfile);
         be->lockfile = NULL;
+
+        if (force)
+        {
+            QofBackendError berror = qof_backend_get_error(be_start);
+            if (berror == ERR_BACKEND_LOCKED || berror == ERR_BACKEND_READONLY)
+            {
+                // Even though we couldn't get the lock, we were told to force
+                // the opening. This is ok because the FORCE argument is
+                // changed only if the caller wants a read-only book.
+            }
+            else
+            {
+                // Unknown error. Push it again on the error stack.
+                qof_backend_set_error(be_start, berror);
+            }
+        }
+
         LEAVE("");
         return;
     }
@@ -358,6 +378,12 @@
     FileBackend *be = (FileBackend*)be_start;
     ENTER (" ");
 
+    if ( be->primary_book && qof_book_is_readonly( be->primary_book ) )
+    {
+        qof_backend_set_error( (QofBackend*)be, ERR_BACKEND_READONLY );
+        return;
+    }
+
     if (be->linkfile)
         g_unlink (be->linkfile);
 
@@ -656,6 +682,14 @@
 
     ENTER (" book=%p file=%s", book, datafile);
 
+    if (book && qof_book_is_readonly(book))
+    {
+        /* Are we read-only? Don't continue in this case. */
+        qof_backend_set_error( (QofBackend*)be, ERR_BACKEND_READONLY );
+        LEAVE("");
+        return FALSE;
+    }
+
     /* If the book is 'clean', recently saved, then don't save again. */
     /* XXX this is currently broken due to faulty 'Save As' logic. */
     /* if (FALSE == qof_book_not_saved (book)) return FALSE; */
@@ -938,6 +972,13 @@
     if (NULL == fbe->primary_book) fbe->primary_book = book;
     if (book != fbe->primary_book) return;
 
+    if (qof_book_is_readonly( fbe->primary_book ) )
+    {
+        /* Are we read-only? Don't continue in this case. */
+        qof_backend_set_error( (QofBackend*)be, ERR_BACKEND_READONLY );
+        return;
+    }
+
     gnc_xml_be_write_to_file (fbe, book, fbe->fullpath, TRUE);
     gnc_xml_be_remove_old_files (fbe);
     LEAVE ("book=%p", book);

Modified: gnucash/trunk/src/gnome-utils/gnc-main-window.c
===================================================================
--- gnucash/trunk/src/gnome-utils/gnc-main-window.c	2011-11-13 21:01:31 UTC (rev 21564)
+++ gnucash/trunk/src/gnome-utils/gnc-main-window.c	2011-11-13 21:26:21 UTC (rev 21565)
@@ -1359,6 +1359,8 @@
     gchar *filename = NULL;
     const gchar *book_id = NULL;
     const gchar *dirty = "";
+    const gchar *readonly_text = NULL;
+    gchar *readonly;
     gchar *title;
     GtkAction* action;
 
@@ -1380,7 +1382,16 @@
                 gtk_action_set_sensitive(action, TRUE);
             }
         }
+        if (qof_book_is_readonly(book))
+        {
+            /* Translators: This string is shown in the window title if this
+            document is, well, read-only. */
+            readonly_text = _("(read-only)");
+        }
     }
+    readonly = (readonly_text != NULL)
+               ? g_strdup_printf(" %s", readonly_text)
+               : g_strdup("");
 
     if (!book_id)
         filename = g_strdup(_("Unsaved Book"));
@@ -1408,14 +1419,15 @@
     {
         /* The Gnome HIG 2.0 recommends the application name not be used. (p16)
          * but several developers prefer to use it anyway. */
-        title = g_strdup_printf("%s%s - %s - GnuCash", dirty, filename,
+        title = g_strdup_printf("%s%s%s - %s - GnuCash", dirty, filename, readonly,
                                 gnc_plugin_page_get_page_name(page));
     }
     else
     {
-        title = g_strdup_printf("%s%s - GnuCash", dirty, filename);
+        title = g_strdup_printf("%s%s%s - GnuCash", dirty, filename, readonly);
     }
     g_free( filename );
+    g_free(readonly);
 
     return title;
 }



More information about the gnucash-changes mailing list