GnuCash  4.8a-132-gcdaeb421d+
gnc-features.c
1 /********************************************************************\
2  * gnc-features.c -- manage GnuCash features table *
3  * Copyright (C) 2011 Derek Atkins <derek@ihtfp.com> *
4  * Copyright (C) 2012 Geert Janssens <geert@kobaltwit.be> *
5  * *
6  * This program is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU General Public License as *
8  * published by the Free Software Foundation; either version 2 of *
9  * the License, or (at your option) any later version. *
10  * *
11  * This program is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License*
17  * along with this program; if not, write to the Free Software *
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
19  * *
20 \********************************************************************/
21 
22 #include <config.h>
23 
24 #include <glib.h>
25 #include <glib/gi18n.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <string.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 
32 #include "qof.h"
33 #include "gnc-features.h"
34 #include "gnc-glib-utils.h"
35 
36 typedef struct
37 {
38  const gchar *key;
39  const gchar *desc;
40 } gncFeature;
41 
42 static GHashTable *features_table = NULL;
43 static gncFeature known_features[] =
44 {
45  { GNC_FEATURE_CREDIT_NOTES, "Customer and vendor credit notes (requires at least GnuCash 2.5.0)" },
46  { GNC_FEATURE_NUM_FIELD_SOURCE, "User specifies source of 'num' field'; either transaction number or split action (requires at least GnuCash 2.5.0)" },
47  { GNC_FEATURE_KVP_EXTRA_DATA, "Extra data for addresses, jobs or invoice entries (requires at least GnuCash 2.6.4)" },
48  { GNC_FEATURE_BOOK_CURRENCY, "User specifies a 'book-currency'; costs of other currencies/commodities tracked in terms of book-currency (requires at least GnuCash 2.7.0)" },
49  { GNC_FEATURE_GUID_BAYESIAN, "Use account GUID as key for Bayesian data (requires at least GnuCash 2.6.12)" },
50  { GNC_FEATURE_GUID_FLAT_BAYESIAN, "Use account GUID as key for bayesian data and store KVP flat (requires at least Gnucash 2.6.19)" },
51  { GNC_FEATURE_SQLITE3_ISO_DATES, "Use ISO formatted date-time strings in SQLite3 databases (requires at least GnuCash 2.6.20)"},
52  { GNC_FEATURE_REG_SORT_FILTER, "Store the register sort and filter settings in .gcm metadata file (requires at least GnuCash 3.3)"},
53  { GNC_FEATURE_BUDGET_UNREVERSED, "Store budget amounts unreversed (i.e. natural) signs (requires at least Gnucash 3.8)"},
54  { GNC_FEATURE_BUDGET_SHOW_EXTRA_ACCOUNT_COLS, "Show extra account columns in the Budget View (requires at least Gnucash 3.8)"},
55  { GNC_FEATURE_EQUITY_TYPE_OPENING_BALANCE, GNC_FEATURE_EQUITY_TYPE_OPENING_BALANCE " (requires at least Gnucash 4.3)" },
56  { NULL },
57 };
58 
59 /* This static indicates the debugging module that this .o belongs to. */
60 static QofLogModule log_module = G_LOG_DOMAIN;
61 
62 /********************************************************************\
63 \********************************************************************/
64 
65 static void gnc_features_init ()
66 {
67  gint i;
68 
69  if (features_table)
70  return;
71 
72  features_table = g_hash_table_new (g_str_hash, g_str_equal);
73  for (i = 0; known_features[i].key; i++)
74  g_hash_table_insert (features_table,
75  g_strdup (known_features[i].key),
76  g_strdup (known_features[i].desc));
77 }
78 
79 static void gnc_features_test_one(gpointer pkey, gpointer value,
80  gpointer data)
81 {
82  const gchar *key = (const gchar*)pkey;
83  const gchar *feature_desc = (const gchar*)value;
84  GList **unknown_features;
85 
86  g_assert(data);
87  unknown_features = (GList**) data;
88 
89  /* Check if this feature is in the known features list. */
90  if (g_hash_table_lookup_extended (features_table, key, NULL, NULL))
91  return;
92 
93  /* It is unknown, so add the description to the unknown features list: */
94  g_assert(feature_desc);
95 
96  *unknown_features = g_list_prepend(*unknown_features,
97  (gpointer)feature_desc);
98 }
99 
100 /* Check if the session requires features unknown to this version of GnuCash.
101  *
102  * Returns a message to display if we found unknown features, NULL if
103  * we're okay.
104  */
105 gchar *gnc_features_test_unknown (QofBook *book)
106 {
107 
108  GList* features_list = NULL;
109  GHashTable *features_used = qof_book_get_features (book);
110 
111  /* Setup the known_features hash table */
112  gnc_features_init();
113 
114  /* Iterate over the members of this frame for unknown features */
115  g_hash_table_foreach (features_used, &gnc_features_test_one,
116  &features_list);
117  if (features_list)
118  {
119  const char* sep = "\n* ";
120  const char* header = _("This Dataset contains features not supported "
121  "by this version of GnuCash. You must use a "
122  "newer version of GnuCash in order to support "
123  "the following features:");
124 
125  char *features_str = gnc_g_list_stringjoin (features_list, sep);
126  char *msg = g_strconcat (header, sep, features_str, NULL);
127  g_free (features_str);
128  g_list_free(features_list);
129  return msg;
130  }
131  g_hash_table_unref (features_used);
132  return NULL;
133 }
134 
135 void gnc_features_set_used (QofBook *book, const gchar *feature)
136 {
137  const gchar *description;
138 
139  g_return_if_fail (book);
140  g_return_if_fail (feature);
141 
142  gnc_features_init();
143 
144  /* Can't set an unknown feature */
145  description = g_hash_table_lookup (features_table, feature);
146  if (!description)
147  {
148  PWARN("Tried to set unknown feature as used.");
149  return;
150  }
151 
152  qof_book_set_feature (book, feature, description);
153 }
154 
156 {
157  gchar const * checked_feature;
158  gboolean found;
159 };
160 
161 static void gnc_features_check_feature_cb (gpointer pkey, gpointer value,
162  gpointer data)
163 {
164  const gchar *key = (const gchar*)pkey;
165  struct CheckFeature * check_data = data;
166  g_assert(data);
167  if (!g_strcmp0 (key, check_data->checked_feature))
168  check_data->found = TRUE;
169 }
170 
171 gboolean gnc_features_check_used (QofBook *book, const gchar * feature)
172 {
173  GHashTable *features_used = qof_book_get_features (book);
174  struct CheckFeature check_data = {feature, FALSE};
175  /* Setup the known_features hash table */
176  gnc_features_init();
177  g_hash_table_foreach (features_used, &gnc_features_check_feature_cb, &check_data);
178  g_hash_table_unref (features_used);
179  return check_data.found;
180 }
181 
gchar * gnc_g_list_stringjoin(GList *list_of_strings, const gchar *sep)
Return a string joining a GList whose elements are gchar* strings.
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
void gnc_features_set_used(QofBook *book, const gchar *feature)
Indicate that the current book uses the given feature.
Definition: gnc-features.c:135
GHashTable * qof_book_get_features(QofBook *book)
Access functions for reading and setting the used-features on this book.
Definition: qofbook.cpp:1223
#define PWARN(format, args...)
Log a warning.
Definition: qoflog.h:250
gchar * gnc_features_test_unknown(QofBook *book)
Test if the current book relies on features only introduced in a more recent version of GnuCash...
Definition: gnc-features.c:105
GLib helper routines.
Utility functions for file access.