r18984 - gnucash/trunk/src - Bug #525821 - new or edited account names should be checked for reserved chars like ":"

Geert Janssens gjanssens at code.gnucash.org
Tue Mar 30 18:19:39 EDT 2010


Author: gjanssens
Date: 2010-03-30 18:19:39 -0400 (Tue, 30 Mar 2010)
New Revision: 18984
Trac: http://svn.gnucash.org/trac/changeset/18984

Modified:
   gnucash/trunk/src/engine/Account.c
   gnucash/trunk/src/engine/Account.h
   gnucash/trunk/src/gnome-utils/dialog-preferences.c
   gnucash/trunk/src/gnome-utils/glade/preferences.glade
   gnucash/trunk/src/gnome-utils/gnc-file.c
Log:
Bug #525821 - new or edited account names should be checked for reserved chars like ":"
This patch checks if the separator character is used in account names when loading
a data file, or when changing the separator character in the preferences.
If the separator character is incompatible with some account names, a warning
dialog box is popped up explaining the situation. This dialog also lists the
violating account names.
Additionally, the preferences dialog will show a warning sign as long as the separator
clashes with some account names. The tooltip for this sign will also display the
violating account names.

Modified: gnucash/trunk/src/engine/Account.c
===================================================================
--- gnucash/trunk/src/engine/Account.c	2010-03-30 20:00:12 UTC (rev 18983)
+++ gnucash/trunk/src/engine/Account.c	2010-03-30 22:19:39 UTC (rev 18984)
@@ -207,6 +207,69 @@
     account_separator[count] = '\0';
 }
 
+gchar *gnc_account_name_violations_errmsg (const gchar *separator, GList* invalid_account_names)
+{
+    GList *node;
+    gchar *message = NULL;
+    gchar *account_list = NULL;
+
+    if ( !invalid_account_names )
+        return NULL;
+
+    for ( node = invalid_account_names;  node; node = g_list_next(node))
+    {
+        if ( !account_list )
+            account_list = node->data;
+        else
+        {
+            gchar *tmp_list = NULL;
+
+            tmp_list = g_strconcat (account_list, "\n", node->data, NULL );
+            g_free ( account_list );
+            account_list = tmp_list;
+        }
+    }
+
+    /* Translators: The first %s will be the account separator character,
+       the second %s is a list of account names.
+       The resulting string will be displayed to the user if there are
+       account names containing the separator character. */
+    message = g_strdup_printf(
+                _("The separator character \"%s\" is used in one or more account names.\n\n"
+                  "This will result in unexpected behaviour. "
+                  "Either change the account names or choose another separator character.\n\n"
+                  "Below you will find the list of invalid account names:\n"
+                  "%s"), separator, account_list );
+    g_free ( account_list );
+    return message;
+}
+
+GList *gnc_account_list_name_violations (QofBook *book, const gchar *separator)
+{
+    Account *root_account = gnc_book_get_root_account(book);
+    GList   *accounts, *node;
+    GList   *invalid_list = NULL;
+
+    g_return_val_if_fail (separator != NULL, NULL);
+
+    if (root_account == NULL)
+        return NULL;
+
+    accounts = gnc_account_get_descendants (root_account);
+    for (node = accounts; node; node = g_list_next(node))
+    {
+        Account *acct      = (Account*)node->data;
+        gchar   *acct_name = g_strdup ( xaccAccountGetName ( acct ) );
+
+        if ( g_strstr_len ( acct_name, -1, separator ) )
+            invalid_list = g_list_prepend ( invalid_list, (gpointer) acct_name );
+        else
+            g_free ( acct_name );
+    }
+
+    return invalid_list;
+}
+
 /********************************************************************\
 \********************************************************************/
 

Modified: gnucash/trunk/src/engine/Account.h
===================================================================
--- gnucash/trunk/src/engine/Account.h	2010-03-30 20:00:12 UTC (rev 18983)
+++ gnucash/trunk/src/engine/Account.h	2010-03-30 22:19:39 UTC (rev 18984)
@@ -251,6 +251,32 @@
 
 /** @} */
 
+/** Composes a translatable error message showing which account
+ *  names clash with the current account separator. Can be called
+ *  after gnc_account_list_name_violations to have a consistent
+ *  error message in different parts of GnuCash
+ *
+ *  @param separator The separator character that was verified against
+ *  @param invalid_account_names A GList of invalid account names.
+ *
+ *  @return An error message that can be displayed to the user or logged.
+ *          This message string should be freed with g_free when no longer
+ *          needed.
+ */
+gchar *gnc_account_name_violations_errmsg (const gchar *separator, GList* invalid_account_names);
+
+/** Runs through all the accounts and returns a list of account names
+ *  that contain the provided separator character. This can be used to
+ *  check if certain account names are invalid.
+ *
+ *  @param book Pointer to the book with accounts to verify
+ *  @param separator The separator character to verify against
+ *
+ *  @return A GList of invalid account names. Should be freed with g_list_free
+ *          if no longer needed.
+ */
+GList *gnc_account_list_name_violations (QofBook *book, const gchar *separator);
+
 /* ------------------ */
 
 /** @name Account general setters/getters

Modified: gnucash/trunk/src/gnome-utils/dialog-preferences.c
===================================================================
--- gnucash/trunk/src/gnome-utils/dialog-preferences.c	2010-03-30 20:00:12 UTC (rev 18983)
+++ gnucash/trunk/src/gnome-utils/dialog-preferences.c	2010-03-30 22:19:39 UTC (rev 18984)
@@ -69,6 +69,7 @@
 #include "gnc-gobject-utils.h"
 #include "gnc-period-select.h"
 #include "gnc-engine.h"
+#include "Account.h"
 #include "gnc-ui.h"
 #include "gnc-ui-util.h"
 #include "gnc-component-manager.h"
@@ -128,8 +129,10 @@
 static void
 gnc_account_separator_prefs_cb (GConfEntry *unused, GtkWidget *dialog)
 {
-    GtkWidget *label;
+    GtkWidget *label, *image;
     gchar *sample;
+    GList *invalid_account_names;
+    QofBook *book;
 
     label = gnc_glade_lookup_widget(dialog, "sample_account");
     /* Translators: Both %s will be the account separator character; the
@@ -144,6 +147,27 @@
     DEBUG(" Label set to '%s'", sample);
     gtk_label_set_text(GTK_LABEL(label), sample);
     g_free(sample);
+
+    /* Check if the new separator clashes with existing account names */
+    image = gnc_glade_lookup_widget(dialog, "separator_error");
+    book = gnc_get_current_book();
+    invalid_account_names = gnc_account_list_name_violations ( book,
+                                     gnc_get_account_separator_string() );
+    if ( invalid_account_names )
+    {
+        GtkTooltipsData *tipsdata = gtk_tooltips_data_get (image);
+        gchar *message = gnc_account_name_violations_errmsg ( gnc_get_account_separator_string(),
+                                                              invalid_account_names );
+        gnc_warning_dialog(dialog, message);
+
+        gtk_tooltips_set_tip ( tipsdata->tooltips, image, message, NULL);
+        gtk_widget_set_visible (image, TRUE);
+        g_free ( message );
+    }
+    else
+        gtk_widget_set_visible (image, FALSE);
+
+    g_list_free ( invalid_account_names );
 }
 
 

Modified: gnucash/trunk/src/gnome-utils/glade/preferences.glade
===================================================================
--- gnucash/trunk/src/gnome-utils/glade/preferences.glade	2010-03-30 20:00:12 UTC (rev 18983)
+++ gnucash/trunk/src/gnome-utils/glade/preferences.glade	2010-03-30 22:19:39 UTC (rev 18984)
@@ -1056,6 +1056,22 @@
 	      </child>
 
 	      <child>
+		<widget class="GtkImage" id="separator_error">
+		  <property name="visible">False</property>
+		  <property name="stock">gtk-dialog-warning</property>
+		  <property name="tooltip" translatable="no">Placeholder tooltip.</property>
+		</widget>
+		  <packing>
+		    <property name="left_attach">2</property>
+		    <property name="right_attach">3</property>
+		    <property name="top_attach">1</property>
+		    <property name="bottom_attach">2</property>
+		  <property name="x_options">fill</property>
+		  <property name="y_options"></property>
+		  </packing>
+		</child>
+
+	      <child>
 		<widget class="GtkLabel" id="sample_account">
 		  <property name="visible">True</property>
 		  <property name="label" translatable="yes"></property>

Modified: gnucash/trunk/src/gnome-utils/gnc-file.c
===================================================================
--- gnucash/trunk/src/gnome-utils/gnc-file.c	2010-03-30 20:00:12 UTC (rev 18983)
+++ gnucash/trunk/src/gnome-utils/gnc-file.c	2010-03-30 22:19:39 UTC (rev 18984)
@@ -32,6 +32,7 @@
 #include "gnc-commodity.h"
 #include "gnc-component-manager.h"
 #include "gnc-engine.h"
+#include "Account.h"
 #include "gnc-file.h"
 #include "gnc-gui-query.h"
 #include "gnc-hooks.h"
@@ -638,6 +639,8 @@
 gnc_post_file_open (const char * filename)
 {
     QofSession *current_session, *new_session;
+    QofBook *new_book;
+    GList *invalid_account_names;
     gboolean uh_oh = FALSE;
     char * newfile;
     QofBackendError io_err = ERR_BACKEND_NO_ERR;
@@ -870,6 +873,19 @@
     /* Call this after re-enabling events. */
     gnc_book_opened ();
 
+    /* Check for account names that may contain the current separator character
+     * and inform the user if there are any */
+    new_book = gnc_get_current_book();
+    invalid_account_names = gnc_account_list_name_violations ( new_book,
+                                     gnc_get_account_separator_string() );
+    if ( invalid_account_names )
+    {
+        gchar *message = gnc_account_name_violations_errmsg ( gnc_get_account_separator_string(),
+                                                              invalid_account_names );
+        gnc_warning_dialog(NULL, message);
+        g_free ( message );
+    }
+
     return TRUE;
 }
 



More information about the gnucash-changes mailing list