GnuCash  5.6-150-g038405b370+
gnc-quotes.hpp
1 /********************************************************************\
2  * gnc-quotes.hpp -- proxy for Finance::Quote *
3  * Copyright (C) 2021 Geert Janssens <geert@kobaltwit.be> *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License*
16  * along with this program; if not, contact: *
17  * *
18  * Free Software Foundation Voice: +1-617-542-5942 *
19  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
20  * Boston, MA 02110-1301, USA gnu@gnu.org *
21 \********************************************************************/
22 #ifndef GNC_QUOTES_HPP
23 #define GNC_QUOTES_HPP
24 
25 #include <memory>
26 #include <string>
27 #include <vector>
28 #include <gnc-commodity.hpp> // For CommVec alias
29 #include <glib.h>
30 #include <stdexcept>
31 
32 extern "C" {
33 #include <qofbook.h>
34 }
35 
36 using StrVec = std::vector<std::string>;
37 using QuoteSources = StrVec;
38 
39 enum class GncQuoteError
40 {
41  SUCCESS,
42  NO_RESULT,
43  QUOTE_FAILED,
44  NO_CURRENCY,
45  UNKNOWN_CURRENCY,
46  NO_PRICE,
47  UNKNOWN_PRICE_TYPE,
48  PRICE_PARSE_FAILURE,
49 };
50 
54 using QuoteFailure = std::tuple<std::string, std::string,
55  GncQuoteError, std::string>;
56 using QFVec = std::vector<QuoteFailure>;
57 
58 struct GncQuoteException : public std::runtime_error
59 {
60  GncQuoteException(const std::string& msg) : std::runtime_error(msg) {}
61 };
62 
63 class GncQuotesImpl;
64 
65 class GncQuotes
66 {
67 public:
72  GncQuotes ();
73  ~GncQuotes ();
74 
79  void fetch (QofBook *book);
85  void fetch (CommVec& commodities);
91  void fetch (gnc_commodity *comm);
99  void report (const char* source, const StrVec& commodities, bool verbose = false);
104  const std::string& version() noexcept;
105 
110  const QuoteSources& sources() noexcept;
111 
116  bool had_failures() noexcept;
117 
125  const QFVec& failures() noexcept;
126 
127  /* Report the commodities for which quotes were requested but not successfully retrieved.
128  *
129  * This does not include requested commodities that didn't have a quote source.
130  *
131  * @return A localized std::string with an intro and a list of the quote failures with a cause. The string is owned by the caller.
132  */
133  const std::string report_failures() noexcept;
134 
135 private:
136  std::unique_ptr<GncQuotesImpl> m_impl;
137 };
138 
139 #endif /* GNC_QUOTES_HPP */
Commodity handling public routines (C++ api)
bool had_failures() noexcept
Report if there were quotes requested but not retrieved.
const QFVec & failures() noexcept
Report the commodities for which quotes were requested but not successfully retrieved.
GncQuotes()
Create a GncQuotes object.
Encapsulate all the information about a dataset.
void report(const char *source, const StrVec &commodities, bool verbose=false)
Report quote results from Finance::Quote to std::cout.
void fetch(QofBook *book)
Fetch quotes for all commodities in our db that have a quote source set.
const std::string & version() noexcept
Get the installed Finance::Quote version.
const QuoteSources & sources() noexcept
Get the available Finance::Quote sources as a std::vector.