GnuCash  4.12-404-geb24099a91
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_GUID_BAYESIAN, "Use account GUID as key for Bayesian data (requires at least GnuCash 2.6.12)" },
49  { GNC_FEATURE_GUID_FLAT_BAYESIAN, "Use account GUID as key for bayesian data and store KVP flat (requires at least Gnucash 2.6.19)" },
50  { GNC_FEATURE_SQLITE3_ISO_DATES, "Use ISO formatted date-time strings in SQLite3 databases (requires at least GnuCash 2.6.20)"},
51  { GNC_FEATURE_REG_SORT_FILTER, "Store the register sort and filter settings in .gcm metadata file (requires at least GnuCash 3.3)"},
52  { GNC_FEATURE_BUDGET_UNREVERSED, "Store budget amounts unreversed (i.e. natural) signs (requires at least Gnucash 3.8)"},
53  { GNC_FEATURE_BUDGET_SHOW_EXTRA_ACCOUNT_COLS, "Show extra account columns in the Budget View (requires at least Gnucash 3.8)"},
54  { GNC_FEATURE_EQUITY_TYPE_OPENING_BALANCE, GNC_FEATURE_EQUITY_TYPE_OPENING_BALANCE " (requires at least Gnucash 4.3)" },
55  { NULL },
56 };
57 
58 /* This static indicates the debugging module that this .o belongs to. */
59 static QofLogModule log_module = G_LOG_DOMAIN;
60 
61 /********************************************************************\
62 \********************************************************************/
63 
64 static void gnc_features_init ()
65 {
66  gint i;
67 
68  if (features_table)
69  return;
70 
71  features_table = g_hash_table_new (g_str_hash, g_str_equal);
72  for (i = 0; known_features[i].key; i++)
73  g_hash_table_insert (features_table,
74  g_strdup (known_features[i].key),
75  g_strdup (known_features[i].desc));
76 }
77 
78 static void gnc_features_test_one(gpointer pkey, gpointer value,
79  gpointer data)
80 {
81  const gchar *key = (const gchar*)pkey;
82  const gchar *feature_desc = (const gchar*)value;
83  GList **unknown_features;
84 
85  g_assert(data);
86  unknown_features = (GList**) data;
87 
88  /* Check if this feature is in the known features list. */
89  if (g_hash_table_lookup_extended (features_table, key, NULL, NULL))
90  return;
91 
92  /* It is unknown, so add the description to the unknown features list: */
93  g_assert(feature_desc);
94 
95  *unknown_features = g_list_prepend(*unknown_features,
96  (gpointer)feature_desc);
97 }
98 
99 /* Check if the session requires features unknown to this version of GnuCash.
100  *
101  * Returns a message to display if we found unknown features, NULL if
102  * we're okay.
103  */
104 gchar *gnc_features_test_unknown (QofBook *book)
105 {
106 
107  GList* features_list = NULL;
108  GHashTable *features_used = qof_book_get_features (book);
109 
110  /* Setup the known_features hash table */
111  gnc_features_init();
112 
113  /* Iterate over the members of this frame for unknown features */
114  g_hash_table_foreach (features_used, &gnc_features_test_one,
115  &features_list);
116  if (features_list)
117  {
118  const char* sep = "\n* ";
119  const char* header = _("This Dataset contains features not supported "
120  "by this version of GnuCash. You must use a "
121  "newer version of GnuCash in order to support "
122  "the following features:");
123 
124  char *features_str = gnc_g_list_stringjoin (features_list, sep);
125  char *msg = g_strconcat (header, sep, features_str, NULL);
126  g_free (features_str);
127  g_list_free(features_list);
128  return msg;
129  }
130  g_hash_table_unref (features_used);
131  return NULL;
132 }
133 
134 void gnc_features_set_used (QofBook *book, const gchar *feature)
135 {
136  const gchar *description;
137 
138  g_return_if_fail (book);
139  g_return_if_fail (feature);
140 
141  gnc_features_init();
142 
143  /* Can't set an unknown feature */
144  description = g_hash_table_lookup (features_table, feature);
145  if (!description)
146  {
147  PWARN("Tried to set unknown feature as used.");
148  return;
149  }
150 
151  qof_book_set_feature (book, feature, description);
152 }
153 
154 
155 void gnc_features_set_unused (QofBook *book, const gchar *feature)
156 {
157  const gchar *description;
158 
159  g_return_if_fail (book);
160  g_return_if_fail (feature);
161 
162  gnc_features_init();
163 
164  /* Can't unset an unknown feature */
165  description = g_hash_table_lookup (features_table, feature);
166  if (!description)
167  {
168  PWARN("Tried to set unknown feature as unused.");
169  return;
170  }
171 
172  qof_book_unset_feature (book, feature, description);
173 }
174 
176 {
177  gchar const * checked_feature;
178  gboolean found;
179 };
180 
181 static void gnc_features_check_feature_cb (gpointer pkey, gpointer value,
182  gpointer data)
183 {
184  const gchar *key = (const gchar*)pkey;
185  struct CheckFeature * check_data = data;
186  g_assert(data);
187  if (!g_strcmp0 (key, check_data->checked_feature))
188  check_data->found = TRUE;
189 }
190 
191 gboolean gnc_features_check_used (QofBook *book, const gchar * feature)
192 {
193  GHashTable *features_used = qof_book_get_features (book);
194  struct CheckFeature check_data = {feature, FALSE};
195  /* Setup the known_features hash table */
196  gnc_features_init();
197  g_hash_table_foreach (features_used, &gnc_features_check_feature_cb, &check_data);
198  g_hash_table_unref (features_used);
199  return check_data.found;
200 }
201 
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:134
GHashTable * qof_book_get_features(QofBook *book)
Access functions for reading and setting the used-features on this book.
Definition: qofbook.cpp:1103
#define PWARN(format, args...)
Log a warning.
Definition: qoflog.h:250
void gnc_features_set_unused(QofBook *book, const gchar *feature)
Indicate that the current book does not use the given feature.
Definition: gnc-features.c:155
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:104
GLib helper routines.
Utility functions for file access.