Move the features tests to its own source files.

This allows for
- other engine consumers to use the features test as well (think
CuteCash, python bindings,...)
- a central point for developers to check for feature definitions
- a central point to manage all feature related code

BP

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@21978 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Geert Janssens 2012-02-09 17:17:59 +00:00
parent 54d2786216
commit 4119c0f2da
5 changed files with 171 additions and 74 deletions

View File

@ -10,6 +10,7 @@ src/app-utils/gnc-component-manager.c
src/app-utils/gnc-entry-quickfill.c
src/app-utils/gnc-euro.c
src/app-utils/gnc-exp-parser.c
src/app-utils/gnc-features.c
src/app-utils/gnc-gettext-util.c
src/app-utils/gnc-helpers.c
src/app-utils/gnc-help-utils.c
@ -424,11 +425,12 @@ src/libqof/qof/qofsession.c
src/libqof/qof/qofutil.c
src/libqof/qof/qof-win32.c
src/plugins/bi_import/dialog-bi-import.c
src/plugins/bi_import/gtkbuilder/dialog-bi-import-gui.glade
src/plugins/bi_import/gncmod-bi-import.c
src/plugins/bi_import/gnc-plugin-bi-import.c
src/plugins/bi_import/dialog-bi-import-gui.c
src/plugins/bi_import/dialog-bi-import-helper.c
src/plugins/bi_import/glade/bi_import.glade
src/plugins/bi_import/gncmod-bi-import.c
src/plugins/bi_import/gnc-plugin-bi-import.c
src/plugins/bi_import/gtkbuilder/dialog-bi-import-gui.glade
src/python/gncmod-python.c
src/register/ledger-core/gnc-ledger-display.c
src/register/ledger-core/gncmod-ledger-core.c

View File

@ -47,6 +47,7 @@ libgncmod_app_utils_la_SOURCES = \
gnc-entry-quickfill.c \
gnc-euro.c \
gnc-exp-parser.c \
gnc-features.c \
gnc-gettext-util.c \
gnc-helpers.c \
gnc-sx-instance-model.c \
@ -70,6 +71,7 @@ gncinclude_HEADERS = \
gnc-entry-quickfill.h \
gnc-euro.h \
gnc-exp-parser.h \
gnc-features.h \
gnc-gettext-util.h \
gnc-help-utils.h \
gnc-helpers.h \

View File

@ -0,0 +1,103 @@
/********************************************************************\
* gnc-features.c -- manage GnuCash features table *
* Copyright (C) 2011 Derek Atkins <derek@ihtfp.com> *
* Copyright (C) 2012 Geert Janssens <geert@kobaltwit.be> *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License*
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
* *
\********************************************************************/
#include "config.h"
#include <glib.h>
#include <glib/gi18n.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "gnc-engine.h"
#include "gnc-features.h"
/* This static indicates the debugging module that this .o belongs to. */
static QofLogModule log_module = GNC_MOD_GUI;
/********************************************************************\
\********************************************************************/
static void features_test(const gchar *key, KvpValue *value, gpointer data)
{
GList** unknown_features = (GList**) data;
char* feature_desc;
g_assert(data);
/* XXX: test if 'key' is an unknown feature. */
/* Yes, it is unknown, so add the description to the list: */
feature_desc = kvp_value_get_string(value);
g_assert(feature_desc);
*unknown_features = g_list_prepend(*unknown_features, feature_desc);
}
/*
* Right now this is done by a KVP check for a features table.
* Currently we don't know about any features, so the mere
* existence of this KVP frame means we have a problem and
* need to tell the user.
*
* returns a message to display if we found unknown features, NULL if we're okay.
*/
gchar *test_unknown_features(QofSession* new_session)
{
KvpFrame *frame = qof_book_get_slots (qof_session_get_book (new_session));
KvpValue *value;
g_assert(frame);
value = kvp_frame_get_value(frame, "features");
if (value)
{
GList* features_list = NULL;
frame = kvp_value_get_frame(value);
g_assert(frame);
/* Iterate over the members of this frame for unknown features */
kvp_frame_for_each_slot(frame, &features_test, &features_list);
if (features_list)
{
GList *i;
char* msg = g_strdup(
_("This Dataset contains features not supported by this "
"version of GnuCash. You must use a newer version of "
"GnuCash in order to support the following features:"
));
for (i = features_list; i; i = i->next)
{
char *tmp = g_strconcat(msg, "\n* ", _(i->data), NULL);
g_free (msg);
msg = tmp;
}
g_free(msg);
g_list_free(features_list);
return msg;
}
}
return NULL;
}

View File

@ -0,0 +1,50 @@
/********************************************************************\
* gnc-features.h -- manage GnuCash features table *
* Copyright (C) 2011 Derek Atkins <derek@ihtfp.com> *
* Copyright (C) 2012 Geert Janssens <geert@kobaltwit.be> *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License*
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
* *
\********************************************************************/
/** @addtogroup Utils Utility functions
@{ */
/** @addtogroup UtilFeature Features
* @{ */
/** @file gnc-features.h
* @brief Utility functions for file access
* @author Copyright (C) 2011 Derek Atkins <derek@ihtfp.com>
* @author Copyright (C) 2012 Geert Janssens <geert@kobaltwit.be>
*
* These functions help you to manage features that GnuCash supports.
* This is mainly used to prevent older GnuCash versions from opening
* datasets with data they aren't capable of processing properly.
*/
#ifndef GNC_FEATURES_H
#define GNC_FEATURES_H
/**
* Test if the current session relies on features we don't know.
*
* Returns a message to display if we found unknown features, NULL if we're okay.
*/
gchar *test_unknown_features(QofSession* new_session);
#endif /* GNC_FEATURES_H */
/** @} */
/** @} */

View File

@ -34,6 +34,7 @@
#include "gnc-engine.h"
#include "Account.h"
#include "gnc-file.h"
#include "gnc-features.h"
#include "gnc-filepath-utils.h"
#include "gnc-gui-query.h"
#include "gnc-hooks.h"
@ -619,76 +620,6 @@ gnc_file_query_save (gboolean can_cancel)
}
static void features_test(const gchar *key, KvpValue *value, gpointer data)
{
GList** unknown_features = (GList**) data;
char* feature_desc;
g_assert(data);
/* XXX: test if 'key' is an unknown feature. */
/* Yes, it is unknown, so add the description to the list: */
feature_desc = kvp_value_get_string(value);
g_assert(feature_desc);
*unknown_features = g_list_prepend(*unknown_features, feature_desc);
}
/*
* Right now this is done by a KVP check for a features table.
* Currently we don't know about any features, so the mere
* existence of this KVP frame means we have a problem and
* need to tell the user.
*
* returns true if we found unknown features, false if we're okay.
*/
static gboolean test_unknown_features(QofSession* new_session)
{
KvpFrame *frame = qof_book_get_slots (qof_session_get_book (new_session));
KvpValue *value;
g_assert(frame);
value = kvp_frame_get_value(frame, "features");
if (value)
{
GList* features_list = NULL;
frame = kvp_value_get_frame(value);
g_assert(frame);
/* Iterate over the members of this frame for unknown features */
kvp_frame_for_each_slot(frame, &features_test, &features_list);
if (features_list)
{
GList *i;
char* msg = g_strdup(
_("This Dataset contains features not supported by this "
"version of GnuCash. You must use a newer version of "
"GnuCash in order to support the following features:"
));
for (i = features_list; i; i = i->next)
{
char *tmp = g_strconcat(msg, "\n* ", _(i->data), NULL);
g_free (msg);
msg = tmp;
}
// XXX: should pull out the file name here */
gnc_error_dialog(gnc_ui_get_toplevel(), msg, "");
g_free(msg);
g_list_free(features_list);
return TRUE;
}
}
return FALSE;
}
/* private utilities for file open; done in two stages */
#define RESPONSE_NEW 1
@ -988,7 +919,16 @@ RESTART:
/* test for unknown features. */
if (!uh_oh)
{
uh_oh = test_unknown_features(new_session);
gchar *msg = test_unknown_features(new_session);
if (msg)
{
uh_oh = TRUE;
// XXX: should pull out the file name here */
gnc_error_dialog(gnc_ui_get_toplevel(), msg, "");
g_free (msg);
}
}
}