mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-25 18:55:30 -06:00
Add GncOptionDB class.
This commit is contained in:
159
libgnucash/app-utils/gnc-optiondb.cpp
Normal file
159
libgnucash/app-utils/gnc-optiondb.cpp
Normal file
@@ -0,0 +1,159 @@
|
||||
/********************************************************************\
|
||||
* gnc-optiondb.cpp -- 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 *
|
||||
* *
|
||||
\********************************************************************/
|
||||
|
||||
#include "gnc-optiondb.hpp"
|
||||
|
||||
GncOptionDB::GncOptionDB() : m_default_section{nullptr} {}
|
||||
|
||||
GncOptionDB::GncOptionDB(QofBook* book) : GncOptionDB() {}
|
||||
|
||||
void
|
||||
GncOptionDB::save_to_book(QofBook* book, bool do_clear) const
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
GncOptionDB::register_option(const char* section, GncOption&& option)
|
||||
{
|
||||
auto db_section = std::find_if(
|
||||
m_sections.begin(), m_sections.end(),
|
||||
[section](GncOptionSection sect) -> bool
|
||||
{
|
||||
return sect.first == std::string{section};
|
||||
});
|
||||
|
||||
if (db_section == m_sections.end())
|
||||
{
|
||||
m_sections.emplace_back(std::make_pair(std::string{section},
|
||||
GncOptionVec{}));
|
||||
db_section = std::prev(m_sections.end());
|
||||
}
|
||||
auto wrapper = std::make_shared<GncOptionWrapper>(option, nullptr);
|
||||
db_section->second.emplace_back(wrapper);
|
||||
}
|
||||
|
||||
void
|
||||
GncOptionDB::unregister_option(const char* section, const char* name)
|
||||
{
|
||||
auto db_section = std::find_if(
|
||||
m_sections.begin(), m_sections.end(),
|
||||
[section](GncOptionSection sect) -> bool
|
||||
{
|
||||
return sect.first == std::string{section};
|
||||
});
|
||||
if (db_section != m_sections.end())
|
||||
{
|
||||
db_section->second.erase(
|
||||
std::remove_if(
|
||||
db_section->second.begin(), db_section->second.end(),
|
||||
[name](GncOptionWrapperPtr option) -> bool
|
||||
{
|
||||
return option->m_option.get_name() == std::string{name};
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
GncOptionDB::set_default_section(const char* section)
|
||||
{
|
||||
}
|
||||
|
||||
SCM
|
||||
GncOptionDB::lookup_option(const char* section, const char* name) const
|
||||
{
|
||||
auto db_section = std::find_if(
|
||||
m_sections.begin(), m_sections.end(),
|
||||
[section](GncOptionSection sect) -> bool
|
||||
{
|
||||
return sect.first == std::string{section};
|
||||
});
|
||||
if (db_section == m_sections.end())
|
||||
return SCM_BOOL_F;
|
||||
auto db_opt = std::find_if(
|
||||
db_section->second.begin(), db_section->second.end(),
|
||||
[name](GncOptionWrapperPtr option) -> bool
|
||||
{
|
||||
return option->m_option.get_name() == std::string{name};
|
||||
});
|
||||
if (db_opt == db_section->second.end() || !*db_opt)
|
||||
return SCM_BOOL_F;
|
||||
return (*db_opt)->m_option.get_scm_value();
|
||||
}
|
||||
|
||||
static const std::string empty_string;
|
||||
std::string
|
||||
GncOptionDB::lookup_string_option(const char* section, const char* name) const
|
||||
{
|
||||
auto db_section = std::find_if(
|
||||
m_sections.begin(), m_sections.end(),
|
||||
[section](GncOptionSection sect) -> bool
|
||||
{
|
||||
return sect.first == std::string{section};
|
||||
});
|
||||
if (db_section == m_sections.end())
|
||||
return empty_string;
|
||||
auto db_opt = std::find_if(
|
||||
db_section->second.begin(), db_section->second.end(),
|
||||
[name](GncOptionWrapperPtr option) -> bool
|
||||
{
|
||||
return option->m_option.get_name() == std::string{name};
|
||||
});
|
||||
if (db_opt == db_section->second.end() || !*db_opt)
|
||||
return empty_string;
|
||||
return (*db_opt)->m_option.get_value<std::string>();
|
||||
}
|
||||
|
||||
bool
|
||||
GncOptionDB::set_option(const char* section, const char* name, SCM value)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void
|
||||
GncOptionDB::set_selectable(const char* section, const char* name)
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
GncOptionDB::commit()
|
||||
{
|
||||
std::for_each(
|
||||
m_sections.begin(), m_sections.end(),
|
||||
[](GncOptionSection section)
|
||||
{
|
||||
std::for_each(
|
||||
section.second.begin(), section.second.end(),
|
||||
[](GncOptionWrapperPtr option)
|
||||
{
|
||||
/* FIXME, not implemented.
|
||||
if (option->m_option.is_dirty())
|
||||
{
|
||||
option->m_option.commit();
|
||||
|
||||
* FIXME, no Gtk in gtk_widget_set_value(option->m_widget,
|
||||
* libgnucash! option->m_option.get_value());
|
||||
}
|
||||
*/
|
||||
});
|
||||
});
|
||||
}
|
||||
71
libgnucash/app-utils/gnc-optiondb.hpp
Normal file
71
libgnucash/app-utils/gnc-optiondb.hpp
Normal file
@@ -0,0 +1,71 @@
|
||||
/********************************************************************\
|
||||
* 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_HPP_
|
||||
#define GNC_OPTIONDB_HPP_
|
||||
|
||||
#include "gnc-option.hpp"
|
||||
|
||||
class GncOptionDB;
|
||||
struct GncOptionWrapper
|
||||
{
|
||||
GncOptionWrapper(const GncOption& opt, void* ptr) : m_option{opt}, m_widget{ptr} {}
|
||||
GncOptionWrapper(GncOption&& opt, void* ptr) : m_option{opt}, m_widget{ptr} {}
|
||||
GncOption m_option;
|
||||
void* m_widget; /* Don't want widget code in libgnucash! GObject closure?*/
|
||||
};
|
||||
|
||||
using GncOptionWrapperPtr = std::shared_ptr<GncOptionWrapper>;
|
||||
using GncOptionVec = std::vector<GncOptionWrapperPtr>;
|
||||
using GncOptionSection = std::pair<std::string, GncOptionVec>;
|
||||
using GncOptionSectionPtr = std::shared_ptr<const GncOptionSection>;
|
||||
class GncOptionDB
|
||||
{
|
||||
public:
|
||||
GncOptionDB();
|
||||
GncOptionDB(QofBook* book);
|
||||
~GncOptionDB() = default;
|
||||
|
||||
void save_to_book(QofBook* book, bool do_clear) const;
|
||||
int num_sections() const noexcept { return m_sections.size(); }
|
||||
bool get_changed() const noexcept { return m_dirty; }
|
||||
void register_option(const char* section, GncOption&& option);
|
||||
void unregister_option(const char* section, const char* name);
|
||||
void set_default_section(const char* section);
|
||||
const GncOptionSectionPtr get_default_section() const noexcept
|
||||
{
|
||||
return m_default_section;
|
||||
}
|
||||
SCM lookup_option(const char* section, const char* name) const;
|
||||
std::string lookup_string_option(const char* section,
|
||||
const char* name) const;
|
||||
bool set_option(const char* section, const char* name, SCM value);
|
||||
void set_selectable(const char* section, const char* name);
|
||||
void commit();
|
||||
private:
|
||||
GncOptionSectionPtr m_default_section;
|
||||
std::vector<GncOptionSection> m_sections;
|
||||
bool m_dirty = false;
|
||||
};
|
||||
|
||||
#endif //GNC_OPTIONDB_HPP_
|
||||
@@ -31,7 +31,9 @@ add_app_utils_test(test-sx test-sx.cpp)
|
||||
|
||||
set(gtest_gnc_option_SOURCES
|
||||
${MODULEPATH}/gnc-option.cpp
|
||||
gtest-gnc-option.cpp)
|
||||
${MODULEPATH}/gnc-optiondb.cpp
|
||||
gtest-gnc-option.cpp
|
||||
gtest-gnc-optiondb.cpp)
|
||||
|
||||
set(gtest_gnc_option_INCLUDES
|
||||
${MODULEPATH}
|
||||
|
||||
58
libgnucash/app-utils/test/gtest-gnc-optiondb.cpp
Normal file
58
libgnucash/app-utils/test/gtest-gnc-optiondb.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
/********************************************************************
|
||||
* gtest-gnc-optiondb.cpp -- unit tests for GncOption class. *
|
||||
* 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 *
|
||||
* *
|
||||
*******************************************************************/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <gnc-optiondb.hpp>
|
||||
|
||||
TEST(GncOptionDB, test_ctor)
|
||||
{
|
||||
EXPECT_NO_THROW ({ GncOptionDB optiondb; });
|
||||
}
|
||||
|
||||
TEST(GncOptionDB, test_register_option)
|
||||
{
|
||||
GncOptionDB optiondb;
|
||||
GncOption option1{"foo", "bar", "baz", "Phony Option",
|
||||
std::string{"waldo"}};
|
||||
optiondb.register_option("foo", std::move(option1));
|
||||
EXPECT_EQ(optiondb.num_sections(), 1);
|
||||
}
|
||||
|
||||
TEST(GncOptionDB, test_lookup_string_option)
|
||||
{
|
||||
GncOptionDB optiondb;
|
||||
GncOption option1{"foo", "bar", "baz", "Phony Option",
|
||||
std::string{"waldo"}};
|
||||
optiondb.register_option("foo", std::move(option1));
|
||||
EXPECT_STREQ("waldo", optiondb.lookup_string_option("foo", "bar").c_str());
|
||||
}
|
||||
|
||||
TEST(GncOptionDB, test_unregister_option)
|
||||
{
|
||||
GncOptionDB optiondb;
|
||||
GncOption option1{"foo", "bar", "baz", "Phony Option",
|
||||
std::string{"waldo"}};
|
||||
optiondb.register_option("foo", std::move(option1));
|
||||
optiondb.unregister_option("foo", "bar");
|
||||
EXPECT_TRUE(optiondb.lookup_string_option("foo", "bar").empty());
|
||||
}
|
||||
@@ -530,6 +530,7 @@ libgnucash/app-utils/gnc-helpers.c
|
||||
libgnucash/app-utils/gnc-help-utils.c
|
||||
libgnucash/app-utils/gncmod-app-utils.c
|
||||
libgnucash/app-utils/gnc-option.cpp
|
||||
libgnucash/app-utils/gnc-optiondb.cpp
|
||||
libgnucash/app-utils/gnc-prefs-utils.c
|
||||
libgnucash/app-utils/gnc-state.c
|
||||
libgnucash/app-utils/gnc-sx-instance-model.c
|
||||
|
||||
Reference in New Issue
Block a user