GncQuotes - start implementation of fetch/fetch_all

This commit is contained in:
Geert Janssens 2021-02-04 15:10:55 +01:00 committed by John Ralls
parent f3fdc5de12
commit a6771754d5
5 changed files with 116 additions and 3 deletions

View File

@ -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}

View File

@ -20,22 +20,32 @@
* Boston, MA 02110-1301, USA gnu@gnu.org *
\ *******************************************************************/
#include <config.h>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <boost/filesystem.hpp>
#include <boost/process.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <glib.h>
#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 <gnc-prefs.h>
#include <regex.h>
#include <qofbook.h>
}
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 <std::string>
format_quotes (const std::vector<gnc_commodity*>)
{
return std::vector <std::string>();
}
/********************************************************************
* gnc_quotes_get_quotable_commodities
* list commodities in a given namespace that get price quotes

View File

@ -24,9 +24,11 @@
#include <string>
#include <vector>
#include <gnc-commodity.hpp> // For CommVec alias
extern "C" {
#include <glib.h>
#include <qofbook.h>
}
using QuoteSources = std::vector<std::string>;
@ -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;

View File

@ -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

View File

@ -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 <vector>
extern "C" {
#include <gnc-commodity.h>
}
using CommVec = std::vector<gnc_commodity*>;
#endif /* GNC_COMMODITY_HPP */
/** @} */
/** @} */