mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-25 18:55:30 -06:00
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. git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@18984 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
@@ -207,6 +207,69 @@ gnc_set_account_separator (const gchar *separator)
|
||||
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;
|
||||
}
|
||||
|
||||
/********************************************************************\
|
||||
\********************************************************************/
|
||||
|
||||
|
||||
@@ -251,6 +251,32 @@ void gnc_book_set_root_account(QofBook *book, Account *root);
|
||||
|
||||
/** @} */
|
||||
|
||||
/** 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
|
||||
|
||||
@@ -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 @@ GSList *add_ins = NULL;
|
||||
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 @@ gnc_account_separator_prefs_cb (GConfEntry *unused, GtkWidget *dialog)
|
||||
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 );
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1055,6 +1055,22 @@
|
||||
</packing>
|
||||
</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>
|
||||
|
||||
@@ -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 @@ static gboolean
|
||||
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 @@ gnc_post_file_open (const char * filename)
|
||||
/* 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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user