GnuCash  5.6-150-g038405b370+
gnc-features.cpp
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 <unordered_map>
23 #include <string>
24 #include <numeric>
25 #include <algorithm>
26 
27 #include <config.h>
28 
29 #include <glib.h>
30 #include <glib/gi18n.h>
31 #include "qofbook.h"
32 #include "qofbook.hpp"
33 
34 #include "gnc-features.h"
35 
36 static const FeaturesTable features_table
37 {
38  { GNC_FEATURE_CREDIT_NOTES, "Customer and vendor credit notes (requires at least GnuCash 2.5.0)" },
39  { GNC_FEATURE_NUM_FIELD_SOURCE, "User specifies source of 'num' field'; either transaction number or split action (requires at least GnuCash 2.5.0)" },
40  { GNC_FEATURE_KVP_EXTRA_DATA, "Extra data for addresses, jobs or invoice entries (requires at least GnuCash 2.6.4)" },
41  { GNC_FEATURE_GUID_BAYESIAN, "Use account GUID as key for Bayesian data (requires at least GnuCash 2.6.12)" },
42  { GNC_FEATURE_GUID_FLAT_BAYESIAN, "Use account GUID as key for bayesian data and store KVP flat (requires at least Gnucash 2.6.19)" },
43  { GNC_FEATURE_SQLITE3_ISO_DATES, "Use ISO formatted date-time strings in SQLite3 databases (requires at least GnuCash 2.6.20)"},
44  { GNC_FEATURE_REG_SORT_FILTER, "Store the register sort and filter settings in .gcm metadata file (requires at least GnuCash 3.3)"},
45  { GNC_FEATURE_BUDGET_UNREVERSED, "Store budget amounts unreversed (i.e. natural) signs (requires at least Gnucash 3.8)"},
46  { GNC_FEATURE_BUDGET_SHOW_EXTRA_ACCOUNT_COLS, "Show extra account columns in the Budget View (requires at least Gnucash 3.8)"},
47  { GNC_FEATURE_EQUITY_TYPE_OPENING_BALANCE, GNC_FEATURE_EQUITY_TYPE_OPENING_BALANCE " (requires at least Gnucash 4.3)" },
48 };
49 
50 /* To obsolete a feature leave the #define in gnc-features.h and move the
51  * feature from features_table to obsolete_features after removing all of the
52  * code that depends on the feature. The feature will be removed from the book's
53  * KVP, allowing the book to be opened with GnuCash versions that don't support
54  * the feature.
55  *
56  * Do this only if the book's data is restored to compatibility with older
57  * GnuCash versions lacking the feature. In general this can be used only in
58  * cases where a feature was created but never implemented in a way that affects
59  * the book's data.
60  */
61 static const FeaturesTable obsolete_features{
62  {GNC_FEATURE_BOOK_CURRENCY, "User-specified book currency stored in KVP. Never implemented but some user managed to get it set anyway. (requires at least GnuCash 2.7.0)"},
63 };
64 
65 /* This static indicates the debugging module that this .o belongs to. */
66 static QofLogModule log_module = G_LOG_DOMAIN;
67 
68 /********************************************************************\
69 \********************************************************************/
70 
71 static const char*
72 header = N_("This Dataset contains features not supported "
73  "by this version of GnuCash. You must use a "
74  "newer version of GnuCash in order to support "
75  "the following features:");
76 
77 /* Check if the session requires features unknown to this version of GnuCash.
78  *
79  * Returns a message to display if we found unknown features, NULL if
80  * we're okay.
81  */
82 gchar *gnc_features_test_unknown (QofBook *book)
83 {
84  auto unknowns {qof_book_get_unknown_features (book, features_table)};
85  if (unknowns.empty())
86  return nullptr;
87 
88  auto obsolete = std::remove_if(unknowns.begin(), unknowns.end(),
89  [](auto& unknown){
90  return obsolete_features.find(unknown.first) != obsolete_features.end();
91  });
92  while (obsolete != unknowns.end())
93  {
94  qof_book_unset_feature(book, obsolete->first.data());
95  obsolete = unknowns.erase(obsolete);
96  }
97 
98  if (unknowns.empty())
99  return nullptr;
100 
101  auto accum = [](const auto& a, const auto& b){ return a + "\n* " + b.second.data(); };
102  auto msg {std::accumulate (unknowns.begin(), unknowns.end(),
103  std::string (_(header)), accum)};
104  return g_strdup (msg.c_str());
105 }
106 
107 void gnc_features_set_used (QofBook *book, const gchar *feature)
108 {
109  g_return_if_fail (book);
110  g_return_if_fail (feature);
111 
112  /* Can't set an unknown feature */
113  auto iter = features_table.find (feature);
114  if (iter == features_table.end ())
115  {
116  PWARN("Tried to set unknown feature as used.");
117  return;
118  }
119 
120  qof_book_set_feature (book, feature, iter->second.data());
121 }
122 
123 
124 void gnc_features_set_unused (QofBook *book, const gchar *feature)
125 {
126  g_return_if_fail (book);
127  g_return_if_fail (feature);
128 
129  /* Can't set an unknown feature */
130  auto iter = features_table.find (feature);
131  if (iter == features_table.end ())
132  {
133  PWARN("Tried to set unknown feature as unused.");
134  return;
135  }
136 
137  qof_book_unset_feature (book, feature);
138 }
139 
140 gboolean gnc_features_check_used (QofBook *book, const gchar * feature)
141 {
142  return qof_book_test_feature (book, feature);
143 }
144 
#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.
#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.
gchar * gnc_features_test_unknown(QofBook *book)
Test if the current book relies on features only introduced in a more recent version of GnuCash...
Encapsulate all the information about a dataset.
Utility functions for file access.