From 70ab8a8a462b2bf99215ddfcea131ae8e0d6cd98 Mon Sep 17 00:00:00 2001 From: Geert Janssens Date: Thu, 18 Mar 2021 19:12:23 +0100 Subject: [PATCH] GncQuotes - drop parameterized constructor The book parameter is only needed while fetching quotes. In case the user passes one or more commodities to process the book can be readily derived from the commodity/commodities. In the other case (fetch all quotes) the user now is required to pass a book to the call. --- gnucash/gnome-utils/dialog-transfer.cpp | 4 +-- gnucash/gnome/dialog-price-edit-db.cpp | 4 +-- gnucash/gnucash-commands.cpp | 4 +-- libgnucash/app-utils/gnc-quotes.cpp | 40 ++++++++++--------------- libgnucash/app-utils/gnc-quotes.hpp | 3 +- 5 files changed, 23 insertions(+), 32 deletions(-) diff --git a/gnucash/gnome-utils/dialog-transfer.cpp b/gnucash/gnome-utils/dialog-transfer.cpp index 9c5d2602bd..9a818d5587 100644 --- a/gnucash/gnome-utils/dialog-transfer.cpp +++ b/gnucash/gnome-utils/dialog-transfer.cpp @@ -1785,7 +1785,7 @@ gnc_xfer_dialog_fetch (GtkButton *button, XferDialog *xferData) ENTER(" "); - GncQuotes quotes (xferData->book); + GncQuotes quotes; if (quotes.cmd_result() != 0) { if (!quotes.error_msg().empty()) @@ -1795,7 +1795,7 @@ gnc_xfer_dialog_fetch (GtkButton *button, XferDialog *xferData) } gnc_set_busy_cursor (nullptr, TRUE); - quotes.fetch(); + quotes.fetch (xferData->book); gnc_unset_busy_cursor (nullptr); /*the results should be in the price db now, but don't crash if not. */ diff --git a/gnucash/gnome/dialog-price-edit-db.cpp b/gnucash/gnome/dialog-price-edit-db.cpp index d6b9614c33..bec4fdb31e 100644 --- a/gnucash/gnome/dialog-price-edit-db.cpp +++ b/gnucash/gnome/dialog-price-edit-db.cpp @@ -559,7 +559,7 @@ gnc_prices_dialog_get_quotes_clicked (GtkWidget *widget, gpointer data) auto pdb_dialog = static_cast (data); ENTER(" "); - GncQuotes quotes (pdb_dialog->book); + GncQuotes quotes; if (quotes.cmd_result() != 0) { if (!quotes.error_msg().empty()) @@ -569,7 +569,7 @@ gnc_prices_dialog_get_quotes_clicked (GtkWidget *widget, gpointer data) } gnc_set_busy_cursor (NULL, TRUE); - quotes.fetch(); + quotes.fetch (pdb_dialog->book); gnc_unset_busy_cursor (NULL); /* Without this, the summary bar on the accounts tab diff --git a/gnucash/gnucash-commands.cpp b/gnucash/gnucash-commands.cpp index afab139c75..145cd009c8 100644 --- a/gnucash/gnucash-commands.cpp +++ b/gnucash/gnucash-commands.cpp @@ -341,7 +341,7 @@ Gnucash::add_quotes (const bo_str& uri) if (qof_session_get_error(session) != ERR_BACKEND_NO_ERR) cleanup_and_exit_with_failure (session); - GncQuotes quotes (qof_session_get_book(session)); + GncQuotes quotes; if (quotes.cmd_result() == 0) { std::cout << bl::format (bl::translate ("Found Finance::Quote version {1}.")) % quotes.version() << std::endl; @@ -356,7 +356,7 @@ Gnucash::add_quotes (const bo_str& uri) std::cerr << bl::translate ("Error message:") << std::endl; std::cerr << quotes.error_msg() << std::endl; } - quotes.fetch (); + quotes.fetch (qof_session_get_book(session)); qof_session_save(session, NULL); if (qof_session_get_error(session) != ERR_BACKEND_NO_ERR) diff --git a/libgnucash/app-utils/gnc-quotes.cpp b/libgnucash/app-utils/gnc-quotes.cpp index 27d5aeb56c..05a53dd0d8 100644 --- a/libgnucash/app-utils/gnc-quotes.cpp +++ b/libgnucash/app-utils/gnc-quotes.cpp @@ -66,7 +66,7 @@ public: GncQuotesImpl (); GncQuotesImpl (QofBook *book); - void fetch (); + void fetch (QofBook *book); void fetch (CommVec& commodities); void fetch (gnc_commodity *comm); @@ -100,23 +100,12 @@ private: /* GncQuotes implementation */ GncQuotesImpl::GncQuotesImpl () -{ - check (nullptr); -} - -GncQuotesImpl::GncQuotesImpl (QofBook *book) -{ - check (book); -} - -void -GncQuotesImpl::check (QofBook *book) { m_version.clear(); m_sources.clear(); m_error_msg.clear(); m_cmd_result = 0; - m_book = book; + m_book = nullptr; m_dflt_curr = gnc_default_currency(); auto perl_executable = bp::search_path("perl"); @@ -151,7 +140,11 @@ GncQuotesImpl::sources_as_glist() void GncQuotesImpl::fetch (CommVec& commodities) { + if (commodities.empty()) + return; + m_comm_vec = std::move (commodities); // Store for later use + m_book = qof_instance_get_book (m_comm_vec[0]); bpt::ptree pt, pt_child; pt.put ("defaultcurrency", gnc_commodity_get_mnemonic (m_dflt_curr)); @@ -206,11 +199,16 @@ GncQuotesImpl::fetch (CommVec& commodities) void -GncQuotesImpl::fetch () +GncQuotesImpl::fetch (QofBook *book) { + if (!book) + { + m_cmd_result = 1; + m_error_msg = "No book set\n"; + return; + } auto commodities = gnc_quotes_get_quotable_commodities ( - gnc_commodity_table_get_table (m_book)); - + gnc_commodity_table_get_table (book)); fetch (commodities); } @@ -219,7 +217,6 @@ void GncQuotesImpl::fetch (gnc_commodity *comm) { auto commodities = CommVec {comm}; - fetch (commodities); } @@ -533,15 +530,10 @@ GncQuotes::GncQuotes () m_impl = std::make_unique (); } -GncQuotes::GncQuotes (QofBook *book) -{ - m_impl = std::make_unique (book); -} - void -GncQuotes::fetch (void) +GncQuotes::fetch (QofBook *book) { - m_impl->fetch (); + m_impl->fetch (book); } void GncQuotes::fetch (CommVec& commodities) diff --git a/libgnucash/app-utils/gnc-quotes.hpp b/libgnucash/app-utils/gnc-quotes.hpp index 7ab51bffb5..3c2ec0f5ce 100644 --- a/libgnucash/app-utils/gnc-quotes.hpp +++ b/libgnucash/app-utils/gnc-quotes.hpp @@ -45,11 +45,10 @@ class GncQuotes public: // Constructor - checks for presence of Finance::Quote and import version and quote sources GncQuotes (); - GncQuotes (QofBook *book); ~GncQuotes (); // Fetch quotes for all commodities in our db that have a quote source set - void fetch (void); + void fetch (QofBook *book); // Only fetch quotes for the commodities passed that have a quote source set void fetch (CommVec& commodities); // Fetch quote for the commodity if it has a quote source set