Line data Source code
1 : /********************************************************************\
2 : * gnc-optiondb.hpp -- Collection of GncOption objects *
3 : * Copyright (C) 2019 John Ralls <jralls@ceridwen.us> *
4 : * *
5 : * This program is free software; you can redistribute it and/or *
6 : * modify it under the terms of the GNU General Public License as *
7 : * published by the Free Software Foundation; either version 2 of *
8 : * the License, or (at your option) any later version. *
9 : * *
10 : * This program is distributed in the hope that it will be useful, *
11 : * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 : * GNU General Public License for more details. *
14 : * *
15 : * You should have received a copy of the GNU General Public License*
16 : * along with this program; if not, contact: *
17 : * *
18 : * Free Software Foundation Voice: +1-617-542-5942 *
19 : * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
20 : * Boston, MA 02110-1301, USA gnu@gnu.org *
21 : * *
22 : \********************************************************************/
23 : /** @addtogroup Engine
24 : @{ */
25 : /** @addtogroup Options
26 : @{ */
27 : /** @file gnc-optiondb-impl.hpp
28 : @brief Implementation details for GncOptionDB.
29 : @author Copyright 2019-2021 John Ralls <jralls@ceridwen.us>
30 : */
31 :
32 : #ifndef GNC_OPTIONDB_P_HPP_
33 : #define GNC_OPTIONDB_P_HPP_
34 :
35 : #include "gnc-option.hpp"
36 : #include "gnc-option-impl.hpp"
37 :
38 : #include <functional>
39 : #include <exception>
40 : #include <optional>
41 : #include <iostream>
42 :
43 : #include <config.h>
44 : #include "qof.h"
45 : #include "gncInvoice.h"
46 : #include "gncCustomer.h"
47 : #include "gncEmployee.h"
48 : #include "gncJob.h"
49 : #include "gncVendor.h"
50 : #include "gncTaxTable.h"
51 :
52 : using GncOptionVec = std::vector<GncOption>;
53 :
54 : /** class GncOptionSection
55 : * The upper-level classification implementation. Contains the options for a
56 : * section; sections are displayed as separate tabs on the option dialog.
57 : */
58 : class GncOptionSection
59 : {
60 : std::string m_name;
61 : GncOptionVec m_options;
62 : public:
63 72 : GncOptionSection(const char* name) : m_name{name}, m_options{} {}
64 24 : ~GncOptionSection() = default;
65 :
66 : void foreach_option(std::function<void(GncOption&)> func);
67 : void foreach_option(std::function<void(const GncOption&)> func) const;
68 172 : const std::string& get_name() const noexcept { return m_name; }
69 : size_t get_num_options() const noexcept { return m_options.size(); }
70 : void add_option(GncOption&& option);
71 : void remove_option(const char* name);
72 : const GncOption* find_option(const char* name) const;
73 : };
74 :
75 : using GncOptionSectionPtr = std::shared_ptr<GncOptionSection>;
76 :
77 : inline bool
78 3425 : operator<(const GncOptionSectionPtr& right, const GncOptionSectionPtr& left)
79 : {
80 3425 : return right->get_name() < left->get_name();
81 : }
82 :
83 : using GncOptionDBChangeCallback = void (*)(void* user_data);
84 :
85 : struct GncOptionDBCallback
86 : {
87 0 : GncOptionDBCallback(size_t id, GncOptionDBChangeCallback func,
88 0 : void* data) :
89 0 : m_id{id}, m_func{func}, m_data{data} {}
90 : ~GncOptionDBCallback() = default;
91 : GncOptionDBCallback(const GncOptionDBCallback&) = delete;
92 : GncOptionDBCallback(GncOptionDBCallback&&) = default;
93 : GncOptionDBCallback& operator=(const GncOptionDBCallback&) = default;
94 : GncOptionDBCallback& operator=(GncOptionDBCallback&&) = default;
95 :
96 : size_t m_id;
97 : GncOptionDBChangeCallback m_func;
98 : void* m_data;
99 : };
100 :
101 : using GncCallbackVec = std::vector<GncOptionDBCallback>;
102 :
103 : /** @class GncOptionDB
104 : * Holds all of the options for a book, report, or stylesheet, organized by
105 : * GncOptionSections.
106 : */
107 : class GncOptionDB
108 : {
109 : public:
110 : GncOptionDB();
111 : GncOptionDB(QofBook* book);
112 : ~GncOptionDB() = default;
113 :
114 : /* The non-const version can't be redirected to the const one because the
115 : * function parameters are incompatible.
116 : */
117 2 : void foreach_section(std::function<void(GncOptionSectionPtr&)> func)
118 : {
119 12 : for (auto& section : m_sections)
120 8 : func(section);
121 2 : }
122 0 : void foreach_section(std::function<void(const GncOptionSectionPtr&)> func) const
123 : {
124 0 : for (auto& section : m_sections)
125 0 : func(section);
126 0 : }
127 : size_t num_sections() const noexcept { return m_sections.size(); }
128 : bool get_changed() const noexcept { return m_dirty; }
129 : void register_option(const char* section, GncOption&& option);
130 : void register_option(const char* section, GncOption* option);
131 : void unregister_option(const char* section, const char* name);
132 : void set_default_section(const char* section);
133 : const GncOptionSection* const get_default_section() const noexcept;
134 : std::string lookup_string_option(const char* section, const char* name);
135 0 : bool set_string_option(const char* section, const char* name, const std::string& value)
136 : {
137 0 : return set_option<std::string>(section, name, value);
138 : }
139 : template <typename ValueType>
140 0 : bool set_option(const char* section, const char* name, ValueType value)
141 : {
142 : try
143 : {
144 0 : auto option{find_option(section, name)};
145 0 : if (!option)
146 0 : return false;
147 0 : option->set_value(value);
148 0 : return true;
149 : }
150 0 : catch(const std::invalid_argument& err)
151 : {
152 0 : printf("Set Failed: %s\n", err.what());
153 0 : return false;
154 : }
155 : }
156 : // void set_selectable(const char* section, const char* name);
157 : void make_internal(const char* section, const char* name);
158 : void commit() {};
159 68 : GncOptionSection* find_section(const std::string& sectname)
160 : {
161 68 : return const_cast<GncOptionSection*>(static_cast<const GncOptionDB&>(*this).find_section(sectname));
162 : }
163 : const GncOptionSection* find_section(const std::string& sectname) const;
164 0 : GncOption* find_option(const std::string& section, const char* name)
165 : {
166 0 : return const_cast<GncOption*>(static_cast<const GncOptionDB&>(*this).find_option(section, name));
167 : }
168 : const GncOption* find_option(const std::string& section, const char* name) const;
169 : std::ostream& save_to_key_value(std::ostream& oss) const noexcept;
170 : std::istream& load_from_key_value(std::istream& iss);
171 : void save_to_kvp(QofBook* book, bool clear_book) const noexcept;
172 : void load_from_kvp(QofBook* book) noexcept;
173 : std::ostream& save_option_key_value(std::ostream& oss,
174 : const std::string& section,
175 : const std::string& name) const noexcept;
176 : std::istream& load_option_key_value(std::istream& iss);
177 : size_t register_callback(GncOptionDBChangeCallback, void*);
178 : void unregister_callback(size_t);
179 : void run_callbacks();
180 : private:
181 : GncOptionSection* m_default_section;
182 : std::vector<GncOptionSectionPtr> m_sections;
183 : bool m_dirty = false;
184 : GncCallbackVec m_callbacks{};
185 :
186 : std::function<GncOptionUIItem*()> m_get_ui_value;
187 : std::function<void(GncOptionUIItem*)> m_set_ui_value;
188 : };
189 :
190 :
191 : #endif // GNC_OPTIONDB_P_HPP_
192 : /** @}
193 : @} */
|