[Gnucash-changes] check_data_type, backend configuration and gettext

Neil Williams codehelp at cvs.gnucash.org
Sun Sep 4 12:43:50 EDT 2005


Log Message:
-----------
check_data_type, backend configuration and gettext

Tags:
----
gnucash-gnome2-dev

Modified Files:
--------------
    gnucash/src/backend/file:
        gnc-backend-file.c
        gnc-backend-file.h
        io-gncbin-r.c
        io-gncbin.h
        io-gncxml-v2.h
        io-gncxml.h
        sixtp-utils.c

Revision Data
-------------
Index: gnc-backend-file.h
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/backend/file/gnc-backend-file.h,v
retrieving revision 1.1.2.3
retrieving revision 1.1.2.4
diff -Lsrc/backend/file/gnc-backend-file.h -Lsrc/backend/file/gnc-backend-file.h -u -r1.1.2.3 -r1.1.2.4
--- src/backend/file/gnc-backend-file.h
+++ src/backend/file/gnc-backend-file.h
@@ -19,7 +19,7 @@
  * Boston, MA  02111-1307,  USA       gnu at gnu.org                   *
 \********************************************************************/
 /** @file gnc-backend-file.h
- *  @breif load and save data to files 
+ *  @brief load and save data to files 
  *  @author Copyright (c) 2000 Gnumatic Inc.
  *  @author Copyright (c) 2002 Derek Atkins <warlord at MIT.EDU>
  *  @author Copyright (c) 2003 Linas Vepstas <linas at linas.org>
@@ -77,4 +77,6 @@
 
 QofBackend * libgncmod_backend_file_LTX_gnc_backend_new(void);
 
+void gnc_provider_init(void);
+
 #endif /* GNC_BACKEND_FILE_H_ */
Index: io-gncxml.h
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/backend/file/io-gncxml.h,v
retrieving revision 1.2.6.2
retrieving revision 1.2.6.3
diff -Lsrc/backend/file/io-gncxml.h -Lsrc/backend/file/io-gncxml.h -u -r1.2.6.2 -r1.2.6.3
--- src/backend/file/io-gncxml.h
+++ src/backend/file/io-gncxml.h
@@ -22,9 +22,9 @@
  *                                                                  *
 \********************************************************************/
 
-/*
+/**
  * @file io-gncxml.h
- * @breif api for Version 1 XML-based file format
+ * @brief api for Version 1 XML-based file format
  *
  * Initial code by Rob L. Browning 4Q 2000
  * Tuneups by James LewisMoss Dec 2000
Index: io-gncbin.h
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/backend/file/io-gncbin.h,v
retrieving revision 1.2.6.3
retrieving revision 1.2.6.4
diff -Lsrc/backend/file/io-gncbin.h -Lsrc/backend/file/io-gncbin.h -u -r1.2.6.3 -r1.2.6.4
--- src/backend/file/io-gncbin.h
+++ src/backend/file/io-gncbin.h
@@ -30,24 +30,36 @@
 #include "qofbook.h"
 
 /* @file io-gncbin.h
- * @breif read (old X-Accountant format) binary datafile
+ * @brief read (old X-Accountant format) binary datafile
  * @author Copyright (C) 1997 Robin D. Clark
  * @author Copyright (C) 1998, 1999 Linas Vepstas <linas at linas.org>
- *
- * NOTE: These routines should not be used directly for file IO.  They
- *    are not inherently safe against file-locking errors.  For direct
- *    file IO, the QofBook's higher level functions should be used.
- *
- * qof_session_load_from_binfile() will load the financial data
- *   represented by the book's file_path into the indicated book.
- *
- * gnc_get_binfile_io_error() will return an error code for any error
- * detected that occured during reading or writing.  It will reset the
- * error code after being called.  The current implementation can be
- * thought of as a "stack of depth one", and this routine as a "pop".
- * Future implementations may have a deeper stack.
+
+  @note These routines should not be used directly for file IO.  They
+    are not inherently safe against file-locking errors.  For direct
+    file IO, the QofBook's higher level functions should be used.
 */
+/** Load the financial data represented by the book's file_path
+into the indicated book.*/
 void            qof_session_load_from_binfile(QofBook *, const char * filepath);
+
+/** Error handler.
+
+\return an error code for any error
+  detected that occured during reading or writing.
+ 
+The error code will be reset after the call.
+  
+ The current implementation can be
+ thought of as a "stack of depth one", and this routine as a "pop".
+*/
 QofBackendError gnc_get_binfile_io_error(void);
 
+/** \brief Attempt to identify the binary format.
+
+Instead of assuming that anything not v1 or v2 XML is the gnc
+binary format, this function tries to load the version token
+from the file.
+*/
+gboolean gnc_is_bin_file (const gchar *name);
+
 #endif /* IO_GNCBIN_H */
Index: sixtp-utils.c
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/backend/file/sixtp-utils.c,v
retrieving revision 1.7.4.5
retrieving revision 1.7.4.6
diff -Lsrc/backend/file/sixtp-utils.c -Lsrc/backend/file/sixtp-utils.c -u -r1.7.4.5 -r1.7.4.6
--- src/backend/file/sixtp-utils.c
+++ src/backend/file/sixtp-utils.c
@@ -388,34 +388,26 @@
 gnc_timegm (struct tm *tm)
 {
   time_t result;
-#ifdef GNUCASH_MAJOR_VERSION
-  char *put_str;
-#endif
   char *old_tz;
 
   old_tz = getenv ("TZ");
-#ifdef GNUCASH_MAJOR_VERSION
-/** \todo Implement a separate method of configuration handling. */
   /* FIXME: there's no way to report this error to the caller. */
-  if(gnc_setenv("TZ", "UTC", 1) != 0)
+  if(setenv("TZ", "UTC", 1) != 0)
     PERR ("couldn't switch the TZ.");
-#endif
   result = mktime (tm);
-#ifdef GNUCASH_MAJOR_VERSION
   if(old_tz)
   {
     /* FIXME: there's no way to report this error to the caller. */
-    if(gnc_setenv("TZ", old_tz, 1) != 0)
+    if(setenv("TZ", old_tz, 1) != 0)
       PERR ("couldn't switch the TZ back.");
   }
   else
   {
     /* FIXME: there's no way to report this error to the caller. */
-    gnc_unsetenv("TZ");
+    unsetenv("TZ");
     if(errno != 0)
       PERR ("couldn't restore the TZ to undefined.");
   }
-#endif
   return result;
 }
 #endif
Index: gnc-backend-file.c
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/backend/file/gnc-backend-file.c,v
retrieving revision 1.23.4.11
retrieving revision 1.23.4.12
diff -Lsrc/backend/file/gnc-backend-file.c -Lsrc/backend/file/gnc-backend-file.c -u -r1.23.4.11 -r1.23.4.12
--- src/backend/file/gnc-backend-file.c
+++ src/backend/file/gnc-backend-file.c
@@ -19,7 +19,7 @@
  * Boston, MA  02111-1307,  USA       gnu at gnu.org                   *
 \********************************************************************/
 /** @file gnc-backend-file.c
- *  @breif load and save data to files 
+ *  @brief load and save data to files 
  *  @author Copyright (c) 2000 Gnumatic Inc.
  *  @author Copyright (c) 2002 Derek Atkins <warlord at MIT.EDU>
  *  @author Copyright (c) 2003 Linas Vepstas <linas at linas.org>
@@ -51,10 +51,24 @@
 #include "io-gncxml.h"
 #include "io-gncbin.h"
 #include "io-gncxml-v2.h"
-
+#include "messages.h"
 #include "gnc-backend-api.h"
 #include "gnc-backend-file.h"
 
+#ifndef GNUCASH_MAJOR_VERSION
+#include "gnc-address-xml-v2.h"
+#include "gnc-bill-term-xml-v2.h"
+#include "gnc-customer-xml-v2.h"
+#include "gnc-employee-xml-v2.h"
+#include "gnc-entry-xml-v2.h"
+#include "gnc-invoice-xml-v2.h"
+#include "gnc-job-xml-v2.h"
+#include "gnc-order-xml-v2.h"
+#include "gnc-owner-xml-v2.h"
+#include "gnc-tax-table-xml-v2.h"
+#include "gnc-vendor-xml-v2.h"
+#endif
+
 #include "qofbackend-p.h"
 #include "qofbook.h"
 #include "qofsession.h"
@@ -71,37 +85,49 @@
 static gint file_retention_days = 0;
 static gboolean file_compression = FALSE;
 
+static void option_cb (QofBackendOption *option, gpointer data)
+{
+	if(0 == safe_strcmp(GNC_BE_DAYS, option->option_name)) {
+		file_retention_days = *(gint*)option->value;
+	}
+	if(0 == safe_strcmp(GNC_BE_ZIP, option->option_name)) {
+		file_compression = (gboolean)qof_util_bool_to_int(option->value);
+	}
+}
+
+/* lookup the options and modify the frame */
 static void
 gnc_file_be_set_config(QofBackend *be, KvpFrame *config)
 {
-	gchar* temp;
-
-	if(!be | !config) { return; }
-	file_retention_days = (gint)kvp_frame_get_gint64(config, GNC_BE_DAYS);
-	temp = kvp_frame_get_string(config, GNC_BE_ZIP);
-	file_compression = (gboolean)qof_util_bool_to_int(temp);
-	
+	qof_backend_option_foreach(config, option_cb, NULL);
 }
 
 static KvpFrame*
 gnc_file_be_get_config(QofBackend *be)
 {
-	if(!be) { return NULL; }
-	if(!kvp_frame_is_empty(be->backend_configuration)) {
-		kvp_frame_delete(be->backend_configuration);
-		be->backend_configuration = kvp_frame_new();
-	}
-	kvp_frame_add_gint64(be->backend_configuration, 
-		GNC_BE_DAYS, (gint64)file_retention_days);
-	if(file_compression) {
-		kvp_frame_add_string(be->backend_configuration, 
-			GNC_BE_ZIP, "TRUE");
-	}
-	else {
-		kvp_frame_add_string(be->backend_configuration, 
-			GNC_BE_ZIP, "FALSE");
-	}
-	return be->backend_configuration;
+	QofBackendOption *option;
+
+	qof_backend_prepare_frame(be);
+	option = g_new0(QofBackendOption, 1);
+	option->option_name = GNC_BE_DAYS;
+	option->description = _("Number of days to retain old files");
+	option->tooltip = _("GnuCash keeps backups of old files, "
+		"this setting dictates how long each is kept");
+	option->type = KVP_TYPE_GINT64;
+	option->value = (gpointer)&file_retention_days;
+	qof_backend_prepare_option(be, option);
+	g_free(option);
+	option = g_new0(QofBackendOption, 1);
+	option->option_name = GNC_BE_ZIP;
+	option->description = _("Compress output files?");
+	option->tooltip = _("GnuCash can save data files with compression."
+		" Enable this option to compress your data file. ");
+	option->type = KVP_TYPE_STRING;
+	if(file_compression) { option->value = (gpointer)"TRUE"; }
+	else { option->value = (gpointer)"FALSE"; }
+	qof_backend_prepare_option(be, option);
+	g_free(option);
+	return qof_backend_complete_frame(be);
 }
 
 /* ================================================================= */
@@ -224,9 +250,7 @@
         return;
     }
     be->fullpath = g_strdup (be->dirname);
-#ifdef GNUCASH_MAJOR_VERSION
     be->be.fullpath = be->fullpath;
-#endif
     p = strrchr (be->dirname, '/');
     if (p && p != be->dirname)
     {
@@ -426,25 +450,40 @@
         return GNC_BOOK_XML1_FILE;
     } else if(is_gzipped_file(path)) {
         return GNC_BOOK_XML2_FILE;
+	}
+	else if(gnc_is_bin_file(path)) { return GNC_BOOK_BIN_FILE; }
 #ifdef GNUCASH_MAJOR_VERSION
-    } else if(is_our_qsf_object(path)) {
+    else if(is_our_qsf_object(path)) {
         return QSF_GNC_OBJECT;  /**< QSF object file using only GnuCash QOF objects */
     } else if(is_qsf_object(path)) {
 		return QSF_OBJECT;   /**< QSF object file that needs a QSF map */
     } else if(is_qsf_map(path)) {
 		return QSF_MAP;      /**< QSF map file */
-#endif
-    } else {
-        return GNC_BOOK_BIN_FILE;
     }
+#endif
+	return GNC_BOOK_NOT_OURS;
 }
 
 static gboolean
-gnc_determine_file_type (QofBackend *be, const char *path)
+gnc_determine_file_type (const char *path)
 {
+	struct stat sbuf;
+	int rc;
+	FILE *t;
+
+	if (!path) { return FALSE; }
+	if (0 == safe_strcmp(path, QOF_STDOUT)) { return FALSE; }
+	t = fopen(path, "r");
+	if(!t) { PINFO (" new file"); return TRUE; }
+	fclose(t);
+	rc = stat(path, &sbuf);
+	if(rc < 0) { return FALSE; }
+	if (sbuf.st_size == 0)    { PINFO (" empty file"); return TRUE; }
 	if(gnc_is_xml_data_file_v2(path))   { return TRUE; } 
 	else if(gnc_is_xml_data_file(path)) { return TRUE; } 
 	else if(is_gzipped_file(path))      { return TRUE; }
+	else if(gnc_is_bin_file(path))      { return TRUE; }
+	PINFO (" %s is not a gnc file", path);
 	return FALSE;
 }	
 
@@ -844,7 +883,7 @@
 /* ---------------------------------------------------------------------- */
 
 
-/* Load financial data from a file into the book, automtically
+/* Load financial data from a file into the book, automatically
    detecting the format of the file, if possible.  Return FALSE on
    error, and set the error parameter to indicate what went wrong if
    it's not NULL.  This function does not manage file locks in any
@@ -889,7 +928,6 @@
 		break;
 #endif
     case GNC_BOOK_BIN_FILE:
-        /* presume it's an old-style binary file */
         qof_session_load_from_binfile(book, be->fullpath);
         error = gnc_get_binfile_io_error();
         break;
@@ -913,14 +951,12 @@
 static gboolean
 gnc_file_be_save_may_clobber_data (QofBackend *bend)
 {
-#ifdef GNUCASH_MAJOR_VERSION
   struct stat statbuf;
   if (!bend->fullpath) return FALSE;
 
   /* FIXME: Make sure this doesn't need more sophisticated semantics
    * in the face of special file, devices, pipes, symlinks, etc. */
   if (stat(bend->fullpath, &statbuf) == 0) return TRUE;
-#endif                                                                                
   return FALSE;
 }
 
@@ -1021,7 +1057,6 @@
 	be->sync = file_sync_all;
 	be->load_config = gnc_file_be_set_config;
 	be->get_config = gnc_file_be_get_config;
-	be->check_data_type = gnc_determine_file_type;
 
 	gnc_be->export = gnc_file_be_write_accounts_to_file;
 	gnc_be->price_lookup = NULL;
@@ -1032,11 +1067,23 @@
 	gnc_be->lockfd = -1;
 
 	gnc_be->primary_book = NULL;
+#ifndef GNUCASH_MAJOR_VERSION
+	gnc_address_xml_initialize ();
+	gnc_billterm_xml_initialize ();
+	gnc_customer_xml_initialize ();
+	gnc_employee_xml_initialize ();
+	gnc_entry_xml_initialize ();
+	gnc_invoice_xml_initialize ();
+	gnc_job_xml_initialize ();
+	gnc_order_xml_initialize ();
+	gnc_owner_xml_initialize ();
+	gnc_taxtable_xml_initialize ();
+	gnc_vendor_xml_initialize ();
+#endif
 	return be;
 }
 
-
-#if 0
+#ifndef GNUCASH_MAJOR_VERSION
 static void
 gnc_provider_free (QofBackendProvider *prov)
 {
@@ -1048,6 +1095,12 @@
 void
 gnc_provider_init(void)
 {
+#ifdef ENABLE_NLS
+	setlocale (LC_ALL, "");
+	bindtextdomain (GETTEXT_PACKAGE, LOCALE_DIR);
+	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
+	textdomain (GETTEXT_PACKAGE);
+#endif
         QofBackendProvider *prov;
         prov = g_new0 (QofBackendProvider, 1);
         prov->provider_name = "GnuCash File Backend Version 2";
@@ -1055,10 +1108,10 @@
         prov->partial_book_supported = FALSE;
         prov->backend_new = gnc_backend_new;
         prov->provider_free = gnc_provider_free;
+	prov->check_data_type = gnc_determine_file_type;
 	    prov->provider_config = "gnucash-file-backend-v2.xml";
         qof_backend_register_provider (prov);
 }
 #endif
 
-
 /* ========================== END OF FILE ===================== */
Index: io-gncbin-r.c
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/backend/file/io-gncbin-r.c,v
retrieving revision 1.15.2.7
retrieving revision 1.15.2.8
diff -Lsrc/backend/file/io-gncbin-r.c -Lsrc/backend/file/io-gncbin-r.c -u -r1.15.2.7 -r1.15.2.8
--- src/backend/file/io-gncbin-r.c
+++ src/backend/file/io-gncbin-r.c
@@ -20,7 +20,7 @@
  *                                                                  *
  ********************************************************************
  * @file io-gncbin-r.c
- * @breif read (old X-Accountant format) binary datafile 
+ * @brief read (old X-Accountant format) binary datafile 
  * @author Copyright (C) 1997 Robin D. Clark
  * @author Copyright (C) 1997-2001 Linas Vepstas <linas at linas.org>
  * @author Copyright (C) 1999-2000 Rob Browning
@@ -408,6 +408,26 @@
  ********************** LOAD DATA ***********************************
 \********************************************************************/
 
+gboolean gnc_is_bin_file (const gchar *name)
+{
+  int  fd, err, token;
+
+  err = 0;
+  token = 0;
+  fd = open( name, RFLAGS, 0 );
+
+  /* check for valid file descriptor */
+  if( 0 > fd ) { return FALSE; }
+
+  /* Read in the file format token */
+  err = read( fd, &token, sizeof(int) );
+  if( sizeof(int) != err ) { return FALSE; }
+  XACC_FLIP_INT (token);
+  close(fd);
+  if(token == VERSION ) { return TRUE; }
+  return FALSE;
+}
+
 /********************************************************************\
  *   reads in the data from file descriptor                         *
  *                                                                  * 
Index: io-gncxml-v2.h
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/backend/file/io-gncxml-v2.h,v
retrieving revision 1.11.4.3
retrieving revision 1.11.4.4
diff -Lsrc/backend/file/io-gncxml-v2.h -Lsrc/backend/file/io-gncxml-v2.h -u -r1.11.4.3 -r1.11.4.4
--- src/backend/file/io-gncxml-v2.h
+++ src/backend/file/io-gncxml-v2.h
@@ -22,10 +22,9 @@
  *                                                                  *
 \********************************************************************/
 
-/*
+/**
  * @file io-gncxml.h
- * @breif api for GnuCash version 2 XML-based file format
- *
+ * @brief api for GnuCash version 2 XML-based file format
  * @author Initial code by James LewisMoss, 2001
  */
 
@@ -123,27 +122,27 @@
 	QofBook *book;
 } gnc_template_xaction_data;
 
-/* Call after loading each record */
+/** Call after loading each record */
 void run_callback(sixtp_gdv2 *data, const char *type);
 
-/* read in an account group from a file */
+/** read in an account group from a file */
 gboolean qof_session_load_from_xml_file_v2(FileBackend *, QofBook *);
 
 /* write all book info to a file */
 gboolean gnc_book_write_to_xml_filehandle_v2(QofBook *book, FILE *fh);
 gboolean gnc_book_write_to_xml_file_v2(QofBook *book, const char *filename, gboolean compress);
 
-/* write just the commodities and accounts to a file */
+/** write just the commodities and accounts to a file */
 gboolean gnc_book_write_accounts_to_xml_filehandle_v2(QofBackend *be, QofBook *book, FILE *fh);
 gboolean gnc_book_write_accounts_to_xml_file_v2(QofBackend * be, QofBook *book,
 						const char *filename);
 
-/* The is_gncxml_file() routine checks to see if the first few 
+/** The is_gncxml_file() routine checks to see if the first few 
  * chars of the file look like gnc-xml data.
  */
 gboolean gnc_is_xml_data_file_v2(const gchar *name);
 
-/* Write a name-space declaration for the provided namespace data type
+/** Write a name-space declaration for the provided namespace data type
  * within the GNC XML namespace at http://www.gnucash.org/XML.
  */
 void gnc_xml2_write_namespace_decl (FILE *out, const char *namespace);


More information about the gnucash-changes mailing list