Skip to content

Commit

Permalink
GncQuotes - add parameterized construction
Browse files Browse the repository at this point in the history
For all but the basic check a book is required. Might
as well be able to pass it directly and store a reference
to it. That will simplify member function declarations.
  • Loading branch information
gjanssens authored and jralls committed Oct 2, 2022
1 parent 3685e5d commit 65ae464
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 26 deletions.
32 changes: 16 additions & 16 deletions gnucash/gnucash-commands.cpp
Expand Up @@ -82,22 +82,6 @@ scm_add_quotes(void *data, [[maybe_unused]] int argc, [[maybe_unused]] char **ar
gnc_prefs_init ();
qof_event_suspend();

GncQuotes quotes;
if (quotes.cmd_result() == 0)
{
std::cout << bl::format (bl::translate ("Found Finance::Quote version {1}.")) % quotes.version() << std::endl;
auto quote_sources = quotes.sources_as_glist();
gnc_quote_source_set_fq_installed (quotes.version().c_str(), quote_sources);
g_list_free_full (quote_sources, g_free);
}
else
{
std::cerr << bl::translate ("No quotes retrieved. Finance::Quote isn't "
"installed properly.") << "\n";
std::cerr << bl::translate ("Error message:") << std::endl;
std::cerr << quotes.error_msg() << std::endl;
}

scm_c_eval_string("(debug-set! stack 200000)");

auto mod = scm_c_resolve_module("gnucash price-quotes");
Expand All @@ -116,6 +100,22 @@ scm_add_quotes(void *data, [[maybe_unused]] int argc, [[maybe_unused]] char **ar
if (qof_session_get_error(session) != ERR_BACKEND_NO_ERR)
scm_cleanup_and_exit_with_failure (session);

GncQuotes quotes (qof_session_get_book(session));
if (quotes.cmd_result() == 0)
{
std::cout << bl::format (bl::translate ("Found Finance::Quote version {1}.")) % quotes.version() << std::endl;
auto quote_sources = quotes.sources_as_glist();
gnc_quote_source_set_fq_installed (quotes.version().c_str(), quote_sources);
g_list_free_full (quote_sources, g_free);
}
else
{
std::cerr << bl::translate ("No quotes retrieved. Finance::Quote isn't "
"installed properly.") << "\n";
std::cerr << bl::translate ("Error message:") << std::endl;
std::cerr << quotes.error_msg() << std::endl;
}

auto scm_book = gnc_book_to_scm(qof_session_get_book(session));
auto scm_result = scm_call_2(add_quotes, SCM_BOOL_F, scm_book);

Expand Down
17 changes: 11 additions & 6 deletions libgnucash/app-utils/gnc-quotes.cpp
Expand Up @@ -55,18 +55,24 @@ namespace bio = boost::iostreams;
CommVec
gnc_quotes_get_quotable_commodities(const gnc_commodity_table * table);

GncQuotes::GncQuotes()
GncQuotes::GncQuotes ()
{
check();
check (nullptr);
}

GncQuotes::GncQuotes (QofBook *book)
{
check (book);
}

void
GncQuotes::check (void)
GncQuotes::check (QofBook *book)
{
m_version.clear();
m_sources.clear();
m_error_msg.clear();
m_cmd_result = 0;
m_book = book;

auto perl_executable = bp::search_path("perl");
auto fq_check = std::string(gnc_path_get_bindir()) + "/gnc-fq-check";
Expand Down Expand Up @@ -121,7 +127,6 @@ GncQuotes::fetch (const CommVec& commodities)
auto key = comm_ns + "." + comm_mnemonic;
pt.put (key, "");
}

);

std::ostringstream result;
Expand All @@ -132,10 +137,10 @@ GncQuotes::fetch (const CommVec& commodities)


void
GncQuotes::fetch_all (QofBook *book)
GncQuotes::fetch_all ()
{
auto commodities = gnc_quotes_get_quotable_commodities (
gnc_commodity_table_get_table (book));
gnc_commodity_table_get_table (m_book));

fetch (commodities);
}
Expand Down
14 changes: 10 additions & 4 deletions libgnucash/app-utils/gnc-quotes.hpp
Expand Up @@ -42,9 +42,11 @@ class GncQuotes
{
public:
// Constructor - checks for presence of Finance::Quote and import version and quote sources
GncQuotes();
GncQuotes ();
GncQuotes (QofBook *book);
~GncQuotes () = default;

void fetch_all (QofBook *book);
void fetch_all ();
void fetch (const CommVec& commodities);

const int cmd_result() noexcept { return m_cmd_result; }
Expand All @@ -54,19 +56,23 @@ class GncQuotes
GList* sources_as_glist ();

private:
GncQuotes () = delete;
// Check if Finance::Quote is properly installed
void check (void);
void check (QofBook *book);
// Run the command specified. Returns two vectors for further processing by the caller
// - one with the contents of stdout
// - one with the contents of stderr
// Will also set m_cmd_result
CmdOutput run_cmd (std::string cmd_name, StrVec args, StrVec input_vec);
template <typename BufferT> CmdOutput run_cmd (std::string cmd_name, StrVec args, BufferT input_vec);

void parse_quotes (std::string);


std::string m_version;
QuoteSources m_sources;
int m_cmd_result;
std::string m_error_msg;
QofBook *m_book;
};

#endif /* GNC_QUOTES_HPP */

0 comments on commit 65ae464

Please sign in to comment.