2020-01-27 15:40:39 -06:00
|
|
|
/********************************************************************\
|
|
|
|
* gnc-optiondb.hpp -- Collection of GncOption objects *
|
|
|
|
* Copyright (C) 2019 John Ralls <jralls@ceridwen.us> *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License as *
|
|
|
|
* published by the Free Software Foundation; either version 2 of *
|
|
|
|
* the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License*
|
|
|
|
* along with this program; if not, contact: *
|
|
|
|
* *
|
|
|
|
* Free Software Foundation Voice: +1-617-542-5942 *
|
|
|
|
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
|
|
|
|
* Boston, MA 02110-1301, USA gnu@gnu.org *
|
|
|
|
* *
|
|
|
|
\********************************************************************/
|
|
|
|
|
|
|
|
#ifndef GNC_OPTIONDB_P_HPP_
|
|
|
|
#define GNC_OPTIONDB_P_HPP_
|
|
|
|
|
|
|
|
#include "gnc-option.hpp"
|
|
|
|
#include "gnc-option-impl.hpp"
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <exception>
|
|
|
|
#include <optional>
|
|
|
|
#include <iostream>
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
#include <config.h>
|
|
|
|
#include <qof.h>
|
|
|
|
#include <gncInvoice.h>
|
2021-03-11 19:56:53 -06:00
|
|
|
#include <gncCustomer.h>
|
|
|
|
#include <gncEmployee.h>
|
|
|
|
#include <gncJob.h>
|
|
|
|
#include <gncVendor.h>
|
2020-01-27 15:40:39 -06:00
|
|
|
#include <gncTaxTable.h>
|
|
|
|
}
|
|
|
|
|
|
|
|
using GncOptionVec = std::vector<GncOption>;
|
2020-03-17 14:24:20 -05:00
|
|
|
|
|
|
|
class GncOptionSection
|
|
|
|
{
|
|
|
|
std::string m_name;
|
|
|
|
GncOptionVec m_options;
|
|
|
|
public:
|
|
|
|
GncOptionSection(const char* name) : m_name{name}, m_options{} {}
|
|
|
|
~GncOptionSection() = default;
|
|
|
|
|
|
|
|
void foreach_option(std::function<void(GncOption&)> func);
|
2020-03-24 13:06:57 -05:00
|
|
|
void foreach_option(std::function<void(const GncOption&)> func) const;
|
2020-03-17 14:24:20 -05:00
|
|
|
const std::string& get_name() const noexcept { return m_name; }
|
|
|
|
size_t get_num_options() const noexcept { return m_options.size(); }
|
|
|
|
void add_option(GncOption&& option);
|
|
|
|
void remove_option(const char* name);
|
|
|
|
const GncOption* find_option(const char* name) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
using GncOptionSectionPtr = std::shared_ptr<GncOptionSection>;
|
2021-08-12 16:29:56 -05:00
|
|
|
using GncOptionDBChangeCallback = void (*)(void* user_data);
|
|
|
|
|
|
|
|
struct GncOptionDBCallback
|
|
|
|
{
|
|
|
|
GncOptionDBCallback(size_t id, GncOptionDBChangeCallback func,
|
|
|
|
void* data) :
|
|
|
|
m_id{id}, m_func{func}, m_data{data} {}
|
|
|
|
~GncOptionDBCallback() = default;
|
|
|
|
GncOptionDBCallback(const GncOptionDBCallback&) = delete;
|
|
|
|
GncOptionDBCallback(GncOptionDBCallback&&) = default;
|
|
|
|
GncOptionDBCallback& operator=(const GncOptionDBCallback&) = default;
|
|
|
|
GncOptionDBCallback& operator=(GncOptionDBCallback&&) = default;
|
|
|
|
|
|
|
|
size_t m_id;
|
|
|
|
GncOptionDBChangeCallback m_func;
|
|
|
|
void* m_data;
|
|
|
|
};
|
|
|
|
|
|
|
|
using GncCallbackVec = std::vector<GncOptionDBCallback>;
|
2020-03-17 14:24:20 -05:00
|
|
|
|
2020-01-27 15:40:39 -06:00
|
|
|
class GncOptionDB
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
GncOptionDB();
|
|
|
|
GncOptionDB(QofBook* book);
|
|
|
|
~GncOptionDB() = default;
|
|
|
|
|
2020-03-17 14:24:20 -05:00
|
|
|
/* The non-const version can't be redirected to the const one because the
|
|
|
|
* function parameters are incompatible.
|
|
|
|
*/
|
|
|
|
void foreach_section(std::function<void(GncOptionSectionPtr&)> func)
|
|
|
|
{
|
|
|
|
for (auto& section : m_sections)
|
|
|
|
func(section);
|
|
|
|
}
|
|
|
|
void foreach_section(std::function<void(const GncOptionSectionPtr&)> func) const
|
|
|
|
{
|
|
|
|
for (auto& section : m_sections)
|
|
|
|
func(section);
|
|
|
|
}
|
|
|
|
size_t num_sections() const noexcept { return m_sections.size(); }
|
2020-01-27 15:40:39 -06:00
|
|
|
bool get_changed() const noexcept { return m_dirty; }
|
|
|
|
void register_option(const char* section, GncOption&& option);
|
2021-01-04 19:08:03 -06:00
|
|
|
void register_option(const char* section, GncOption* option);
|
2020-01-27 15:40:39 -06:00
|
|
|
void unregister_option(const char* section, const char* name);
|
|
|
|
void set_default_section(const char* section);
|
|
|
|
const GncOptionSection* const get_default_section() const noexcept;
|
2020-03-17 14:24:20 -05:00
|
|
|
std::string lookup_string_option(const char* section, const char* name);
|
2020-01-27 15:40:39 -06:00
|
|
|
template <typename ValueType>
|
|
|
|
bool set_option(const char* section, const char* name, ValueType value)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto option{find_option(section, name)};
|
|
|
|
if (!option)
|
|
|
|
return false;
|
2020-03-17 14:24:20 -05:00
|
|
|
option->set_value(value);
|
2020-01-27 15:40:39 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const std::invalid_argument& err)
|
|
|
|
{
|
|
|
|
printf("Set Failed: %s\n", err.what());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// void set_selectable(const char* section, const char* name);
|
|
|
|
void make_internal(const char* section, const char* name);
|
|
|
|
void commit() {};
|
2020-03-17 14:24:20 -05:00
|
|
|
GncOptionSection* find_section(const std::string& sectname)
|
|
|
|
{
|
|
|
|
return const_cast<GncOptionSection*>(static_cast<const GncOptionDB&>(*this).find_section(sectname));
|
|
|
|
}
|
|
|
|
const GncOptionSection* find_section(const std::string& sectname) const;
|
|
|
|
GncOption* find_option(const std::string& section, const char* name)
|
|
|
|
{
|
|
|
|
return const_cast<GncOption*>(static_cast<const GncOptionDB&>(*this).find_option(section, name));
|
2020-01-27 15:40:39 -06:00
|
|
|
}
|
2020-03-17 14:24:20 -05:00
|
|
|
const GncOption* find_option(const std::string& section, const char* name) const;
|
2020-01-27 15:40:39 -06:00
|
|
|
std::ostream& save_to_scheme(std::ostream& oss,
|
|
|
|
const char* options_prolog) const noexcept;
|
|
|
|
std::istream& load_from_scheme(std::istream& iss) noexcept;
|
|
|
|
std::ostream& save_to_key_value(std::ostream& oss) const noexcept;
|
|
|
|
std::istream& load_from_key_value(std::istream& iss);
|
|
|
|
void save_to_kvp(QofBook* book, bool clear_book) const noexcept;
|
|
|
|
void load_from_kvp(QofBook* book) noexcept;
|
|
|
|
std::ostream& save_option_scheme(std::ostream& oss,
|
|
|
|
const char* option_prolog,
|
|
|
|
const std::string& section,
|
|
|
|
const std::string& name) const noexcept;
|
|
|
|
std::istream& load_option_scheme(std::istream& iss);
|
|
|
|
std::ostream& save_option_key_value(std::ostream& oss,
|
|
|
|
const std::string& section,
|
|
|
|
const std::string& name) const noexcept;
|
|
|
|
std::istream& load_option_key_value(std::istream& iss);
|
2021-08-12 16:29:56 -05:00
|
|
|
size_t register_callback(GncOptionDBChangeCallback, void*);
|
|
|
|
void unregister_callback(size_t);
|
|
|
|
void run_callbacks();
|
2020-01-27 15:40:39 -06:00
|
|
|
private:
|
2020-03-17 14:24:20 -05:00
|
|
|
GncOptionSection* m_default_section;
|
|
|
|
std::vector<GncOptionSectionPtr> m_sections;
|
2020-01-27 15:40:39 -06:00
|
|
|
bool m_dirty = false;
|
2021-08-12 16:29:56 -05:00
|
|
|
GncCallbackVec m_callbacks{};
|
2020-01-27 15:40:39 -06:00
|
|
|
|
|
|
|
std::function<GncOptionUIItem*()> m_get_ui_value;
|
|
|
|
std::function<void(GncOptionUIItem*)> m_set_ui_value;
|
|
|
|
static constexpr char const* const scheme_tags[]
|
|
|
|
{
|
|
|
|
"(let ((option (gnc:lookup-option ",
|
|
|
|
" ",
|
|
|
|
")))",
|
|
|
|
" ((lambda (o) (if o (gnc:option-set-value o ",
|
|
|
|
"))) option))"
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif // GNC_OPTIONDB_P_HPP_
|