GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-features.c
1 /********************************************************************\
2  * gnc-features.c -- manage GnuCash features table *
3  * Copyright (C) 2011 Derek Atkins <[email protected]> *
4  * Copyright (C) 2012 Geert Janssens <[email protected]> *
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 "libqof/qof/qof.h"
33 #include "gnc-features.h"
34 
35 typedef struct
36 {
37  const gchar *key;
38  const gchar *desc;
39 } gncFeature;
40 
41 static GHashTable *features_table = NULL;
42 static gncFeature known_features[] =
43 {
44  { GNC_FEATURE_CREDIT_NOTES, "Customer and vendor credit notes (requires at least GnuCash 2.5.0)" },
45  { GNC_FEATURE_NUM_FIELD_SOURCE, "User specifies source of 'num' field'; either transaction number or split action (requires at least GnuCash 2.5.0)" },
46  { GNC_FEATURE_KVP_EXTRA_DATA, "Extra data for addresses, jobs or invoice entries (requires at least GnuCash 2.6.4)" },
47  { NULL },
48 };
49 
50 /* This static indicates the debugging module that this .o belongs to. */
51 static QofLogModule log_module = G_LOG_DOMAIN;
52 
53 /********************************************************************\
54 \********************************************************************/
55 
56 static void gnc_features_init ()
57 {
58  gint i;
59 
60  if (features_table)
61  return;
62 
63  features_table = g_hash_table_new (g_str_hash, g_str_equal);
64  for (i = 0; known_features[i].key; i++)
65  g_hash_table_insert (features_table,
66  g_strdup (known_features[i].key),
67  g_strdup (known_features[i].desc));
68 }
69 
70 static void gnc_features_test_one(gpointer pkey, gpointer value,
71  gpointer data)
72 {
73  const gchar *key = (const gchar*)pkey;
74  const gchar *feature_desc = (const gchar*)value;
75  GList **unknown_features;
76 
77  g_assert(data);
78  unknown_features = (GList**) data;
79 
80  /* Check if this feature is in the known features list. */
81  if (g_hash_table_lookup_extended (features_table, key, NULL, NULL))
82  return;
83 
84  /* It is unknown, so add the description to the unknown features list: */
85  g_assert(feature_desc);
86 
87  *unknown_features = g_list_prepend(*unknown_features,
88  (gpointer)feature_desc);
89 }
90 
91 /* Check if the session requires features unknown to this version of GnuCash.
92  *
93  * Returns a message to display if we found unknown features, NULL if
94  * we're okay.
95  */
97 {
98 
99  GList* features_list = NULL;
100  GHashTable *features_used = qof_book_get_features (book);
101 
102  /* Setup the known_features hash table */
103  gnc_features_init();
104 
105  /* Iterate over the members of this frame for unknown features */
106  g_hash_table_foreach (features_used, &gnc_features_test_one,
107  &features_list);
108  if (features_list)
109  {
110  GList *i;
111  char* msg = g_strdup(_("This Dataset contains features not supported "
112  "by this version of GnuCash. You must use a "
113  "newer version of GnuCash in order to support "
114  "the following features:"
115  ));
116 
117  for (i = features_list; i; i = i->next)
118  {
119  char *tmp = g_strconcat(msg, "\n* ", i->data, NULL);
120  g_free (msg);
121  msg = tmp;
122  }
123 
124  g_list_free(features_list);
125  return msg;
126  }
127 
128  return NULL;
129 }
130 
131 void gnc_features_set_used (QofBook *book, const gchar *feature)
132 {
133  const gchar *description;
134 
135  g_return_if_fail (book);
136  g_return_if_fail (feature);
137 
138  gnc_features_init();
139 
140  /* Can't set an unknown feature */
141  description = g_hash_table_lookup (features_table, feature);
142  if (!description)
143  {
144  PWARN("Tried to set unknown feature as used.");
145  return;
146  }
147 
148  qof_book_set_feature (book, feature, description);
149 
150 
151 }
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
void gnc_features_set_used(QofBook *book, const gchar *feature)
Definition: gnc-features.c:131
GHashTable * qof_book_get_features(QofBook *book)
#define PWARN(format, args...)
Definition: qoflog.h:243
gchar * gnc_features_test_unknown(QofBook *book)
Definition: gnc-features.c:96
const gchar * QofLogModule
Definition: qofid.h:89
Utility functions for file access.