diff --git a/libgnucash/app-utils/CMakeLists.txt b/libgnucash/app-utils/CMakeLists.txt index 6fcba6f778..3192c5d582 100644 --- a/libgnucash/app-utils/CMakeLists.txt +++ b/libgnucash/app-utils/CMakeLists.txt @@ -49,6 +49,7 @@ set(app_utils_ALL_SOURCES ${app_utils_SOURCES} ${app_utils_HEADERS}) set(app_utils_ALL_LIBRARIES gnc-engine ${Boost_FILESYSTEM_LIBRARY} + ${Boost_PROPERTY_TREE_LIBRARY} ${GIO_LDFLAGS} ${LIBXML2_LDFLAGS} ${LIBXSLT_LDFLAGS} diff --git a/libgnucash/app-utils/gnc-quotes.cpp b/libgnucash/app-utils/gnc-quotes.cpp index 63c1601fbf..ee341973ec 100644 --- a/libgnucash/app-utils/gnc-quotes.cpp +++ b/libgnucash/app-utils/gnc-quotes.cpp @@ -20,22 +20,32 @@ * Boston, MA 02110-1301, USA gnu@gnu.org * \ *******************************************************************/ +#include + #include #include #include #include +#include #include #include +#include +#include #include +#include "gnc-commodity.hpp" #include "gnc-quotes.hpp" extern "C" { - #include "gnc-path.h" +#include "gnc-commodity.h" +#include "gnc-path.h" +#include "gnc-ui-util.h" #include #include +#include } namespace bp = boost::process; +namespace bpt = boost::property_tree; static GncQuotes quotes_cached; static bool quotes_initialized = false; @@ -99,6 +109,56 @@ GncQuotes::sources_as_glist() } +void +GncQuotes::fetch (const CommVec& commodities) +{ + auto dflt_curr = gnc_default_currency(); + bpt::ptree pt, pt_child; + pt.put ("defaultcurrency", gnc_commodity_get_mnemonic (dflt_curr)); + + std::for_each (commodities.cbegin(), commodities.cend(), + [&pt, &dflt_curr] (auto comm) + { + auto comm_mnemonic = gnc_commodity_get_mnemonic (comm); + auto comm_ns = std::string("currency"); + if (gnc_commodity_is_currency (comm)) + { + if (gnc_commodity_equiv(comm, dflt_curr) || + (!comm_mnemonic || (strcmp (comm_mnemonic, "XXX") == 0))) + return; + } + else + comm_ns = gnc_quote_source_get_internal_name (gnc_commodity_get_quote_source (comm)); + + auto key = comm_ns + "." + comm_mnemonic; + pt.put (key, ""); + } + + ); + + std::ostringstream result; + bpt::write_json(result, pt); + std::cerr << "GncQuotes fetch_all - resulting json object\n" << result.str() << std::endl; + +} + + +void +GncQuotes::fetch_all (QofBook *book) +{ + auto commodities = gnc_quotes_get_quotable_commodities ( + gnc_commodity_table_get_table (book)); + + fetch (commodities); +} + +static const std::vector +format_quotes (const std::vector) +{ + return std::vector (); +} + + /******************************************************************** * gnc_quotes_get_quotable_commodities * list commodities in a given namespace that get price quotes diff --git a/libgnucash/app-utils/gnc-quotes.hpp b/libgnucash/app-utils/gnc-quotes.hpp index 648d335a5c..8058813a98 100644 --- a/libgnucash/app-utils/gnc-quotes.hpp +++ b/libgnucash/app-utils/gnc-quotes.hpp @@ -24,9 +24,11 @@ #include #include +#include // For CommVec alias extern "C" { #include +#include } using QuoteSources = std::vector; @@ -36,10 +38,12 @@ const std::string not_found = std::string ("Not Found"); class GncQuotes { public: - // Constructor - check for presence of Finance::Quote and import version and quote sources + // Constructor - checks for presence of Finance::Quote and import version and quote sources GncQuotes(); - // Function to check if Finance::Quote is properly installed + void fetch_all (QofBook *book); + void fetch (const CommVec& commodities); + const int cmd_result() noexcept { return m_cmd_result; } const std::string& error_msg() noexcept { return m_error_msg; } const std::string& version() noexcept { return m_version.empty() ? not_found : m_version; } @@ -47,6 +51,7 @@ public: GList* sources_as_glist (); private: + // Function to check if Finance::Quote is properly installed void check (void); std::string m_version; diff --git a/libgnucash/engine/CMakeLists.txt b/libgnucash/engine/CMakeLists.txt index 0b494dabbe..470fbd34bc 100644 --- a/libgnucash/engine/CMakeLists.txt +++ b/libgnucash/engine/CMakeLists.txt @@ -53,6 +53,7 @@ set (engine_HEADERS gnc-aqbanking-templates.h gnc-budget.h gnc-commodity.h + gnc-commodity.hpp gnc-date.h gnc-datetime.hpp gnc-engine.h diff --git a/libgnucash/engine/gnc-commodity.hpp b/libgnucash/engine/gnc-commodity.hpp new file mode 100644 index 0000000000..d4590c62c2 --- /dev/null +++ b/libgnucash/engine/gnc-commodity.hpp @@ -0,0 +1,46 @@ +/********************************************************************** + * gnc-commodity.hpp -- API for tradable commodities (incl. currency) * + * * + * 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 * + * * + *********************************************************************/ + +/** @addtogroup Engine + @{ */ +/** @addtogroup Commodity Commodities + + @{ */ +/** @file gnc-commodity.hpp + * @brief Commodity handling public routines (C++ api) + * @author Copyright (C) 2021 Geert Janssens + */ + +#ifndef GNC_COMMODITY_HPP +#define GNC_COMMODITY_HPP + +#include + +extern "C" { +#include +} + +using CommVec = std::vector; + +#endif /* GNC_COMMODITY_HPP */ +/** @} */ +/** @} */