gnucash maint: remove unnecesary 'home' level in path for US Income Tax book tax information

J.Alex Aycinena alex.aycinena at code.gnucash.org
Thu Jan 24 21:54:22 EST 2019


Updated	 via  https://github.com/Gnucash/gnucash/commit/fc153643 (commit)
	from  https://github.com/Gnucash/gnucash/commit/3a486727 (commit)



commit fc1536432619888dc28cd5c0aa58658094a93c45
Author: Alex Aycinena <alex.aycinena at gmail.com>
Date:   Thu Jan 24 18:36:13 2019 -0800

    remove unnecesary 'home' level in path for US Income Tax book tax information

diff --git a/libgnucash/app-utils/gnc-ui-util.c b/libgnucash/app-utils/gnc-ui-util.c
index 017befc..9137a43 100644
--- a/libgnucash/app-utils/gnc-ui-util.c
+++ b/libgnucash/app-utils/gnc-ui-util.c
@@ -227,8 +227,10 @@ gnc_is_new_book (void)
                             ? TRUE : FALSE);
 }
 
-#define OPTION_TAXUS_NAME "book/tax_US/name"
-#define OPTION_TAXUS_TYPE "book/tax_US/type"
+#define OPTION_TAXUS_NAME "tax_US/name"
+#define OPTION_TAXUS_TYPE "tax_US/type"
+#define OLD_OPTION_TAXUS_NAME "book/tax_US/name"
+#define OLD_OPTION_TAXUS_TYPE "book/tax_US/type"
 
 void
 gnc_set_current_book_tax_name (const gchar *tax_name)
@@ -239,7 +241,45 @@ gnc_set_current_book_tax_name (const gchar *tax_name)
 const gchar *
 gnc_get_current_book_tax_name (void)
 {
-    return qof_book_get_string_option(gnc_get_current_book(), OPTION_TAXUS_NAME);
+    QofBook* book = gnc_get_current_book();
+    const char* tax_name =
+        qof_book_get_string_option(book, OPTION_TAXUS_NAME);
+    if (tax_name)
+    {
+        return tax_name;
+    }
+    else
+    {
+        const char* old_option_taxus_name =
+            qof_book_get_string_option(book, OLD_OPTION_TAXUS_NAME);
+        if (old_option_taxus_name)
+        {
+            char* taxus_name = g_strdup(old_option_taxus_name);
+            const char* old_option_taxus_type =
+                qof_book_get_string_option(book, OLD_OPTION_TAXUS_TYPE);
+            if (old_option_taxus_type)
+            { /* switch both name and type and remove unused frames */
+                char* taxus_type = g_strdup(old_option_taxus_type);
+                qof_book_set_string_option(book, OPTION_TAXUS_NAME, taxus_name);
+                qof_book_set_string_option(book, OLD_OPTION_TAXUS_NAME, '\0');
+                qof_book_set_string_option(book, OPTION_TAXUS_TYPE, taxus_type);
+                qof_book_set_string_option(book, OLD_OPTION_TAXUS_TYPE, '\0');
+                qof_book_option_frame_delete(book, "book/tax_US");
+                qof_book_option_frame_delete(book, "book");
+                g_free (taxus_type);
+            }
+            else
+            { /* switch just name and remove unused frames */
+                qof_book_set_string_option(book, OPTION_TAXUS_NAME, taxus_name);
+                qof_book_set_string_option(book, OLD_OPTION_TAXUS_NAME, '\0');
+                qof_book_option_frame_delete(book, "book/tax_US");
+                qof_book_option_frame_delete(book, "book");
+            }
+            g_free (taxus_name);
+            return qof_book_get_string_option(book, OPTION_TAXUS_NAME);
+        }
+        return NULL;
+    }
 }
 
 void
@@ -251,7 +291,45 @@ gnc_set_current_book_tax_type (const gchar *tax_type)
 const gchar *
 gnc_get_current_book_tax_type (void)
 {
-    return qof_book_get_string_option(gnc_get_current_book(), OPTION_TAXUS_TYPE);
+    QofBook* book = gnc_get_current_book();
+    const char* tax_type =
+        qof_book_get_string_option(book, OPTION_TAXUS_TYPE);
+    if (tax_type)
+    {
+        return tax_type;
+    }
+    else
+    {
+        const char* old_option_taxus_type =
+            qof_book_get_string_option(book, OLD_OPTION_TAXUS_TYPE);
+        if (old_option_taxus_type)
+        {
+            char* taxus_type = g_strdup(old_option_taxus_type);
+            const char* old_option_taxus_name =
+                qof_book_get_string_option(book, OLD_OPTION_TAXUS_NAME);
+            if (old_option_taxus_name)
+            { /* switch both name and type and remove unused frames */
+                char* taxus_name = g_strdup(old_option_taxus_name);
+                qof_book_set_string_option(book, OPTION_TAXUS_NAME, taxus_name);
+                qof_book_set_string_option(book, OLD_OPTION_TAXUS_NAME, '\0');
+                qof_book_set_string_option(book, OPTION_TAXUS_TYPE, taxus_type);
+                qof_book_set_string_option(book, OLD_OPTION_TAXUS_TYPE, '\0');
+                qof_book_option_frame_delete(book, "book/tax_US");
+                qof_book_option_frame_delete(book, "book");
+                g_free (taxus_name);
+            }
+            else
+            { /* switch just type and remove unused frames */
+                qof_book_set_string_option(book, OPTION_TAXUS_TYPE, taxus_type);
+                qof_book_set_string_option(book, OLD_OPTION_TAXUS_TYPE, '\0');
+                qof_book_option_frame_delete(book, "book/tax_US");
+                qof_book_option_frame_delete(book, "book");
+            }
+            g_free (taxus_type);
+            return qof_book_get_string_option(book, OPTION_TAXUS_TYPE);
+        }
+        return NULL;
+    }
 }
 
 /** Calls gnc_book_option_num_field_source_change to initiate registered
diff --git a/libgnucash/engine/qofbook.cpp b/libgnucash/engine/qofbook.cpp
index fa67f5b..2df7bf6 100644
--- a/libgnucash/engine/qofbook.cpp
+++ b/libgnucash/engine/qofbook.cpp
@@ -1172,6 +1172,20 @@ qof_book_set_string_option(QofBook* book, const char* opt_name, const char* opt_
 }
 
 void
+qof_book_option_frame_delete (QofBook *book, const char* opt_name)
+{
+    if (opt_name && (*opt_name != '\0'))
+    {
+        qof_book_begin_edit(book);
+        auto frame = qof_instance_get_slots(QOF_INSTANCE(book));
+        auto opt_path = opt_name_to_path(opt_name);
+        delete frame->set_path(opt_path, nullptr);
+        qof_instance_set_dirty (QOF_INSTANCE (book));
+        qof_book_commit_edit(book);
+    }
+}
+
+void
 qof_book_begin_edit (QofBook *book)
 {
     qof_begin_edit(&book->inst);
diff --git a/libgnucash/engine/qofbook.h b/libgnucash/engine/qofbook.h
index 67f41c2..39c21f8 100644
--- a/libgnucash/engine/qofbook.h
+++ b/libgnucash/engine/qofbook.h
@@ -375,6 +375,7 @@ char *qof_book_get_counter_format (const QofBook *book,
 
 const char* qof_book_get_string_option(const QofBook* book, const char* opt_name);
 void qof_book_set_string_option(QofBook* book, const char* opt_name, const char* opt_val);
+void qof_book_option_frame_delete (QofBook *book, const char* opt_name);
 
 /** Access functions for reading and setting the used-features on this book.
  */



Summary of changes:
 libgnucash/app-utils/gnc-ui-util.c | 86 ++++++++++++++++++++++++++++++++++++--
 libgnucash/engine/qofbook.cpp      | 14 +++++++
 libgnucash/engine/qofbook.h        |  1 +
 3 files changed, 97 insertions(+), 4 deletions(-)



More information about the gnucash-changes mailing list