Line data Source code
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 :
51 : /** QuoteFailure elements are namespace, mnemonic, error code, and
52 : * F::Q errormsg if there is one.
53 : */
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 0 : GncQuoteException(const std::string& msg) : std::runtime_error(msg) {}
61 : };
62 :
63 : class GncQuotesImpl;
64 :
65 : class GncQuotes
66 : {
67 : public:
68 : /** Create a GncQuotes object.
69 : *
70 : * Throws a GncQuoteException if Finance::Quote is not installed or fails to initialize.
71 : */
72 : GncQuotes ();
73 : ~GncQuotes ();
74 :
75 : /** Fetch quotes for all commodities in our db that have a quote source set
76 : *
77 : * @param book The current book.
78 : */
79 : void fetch (QofBook *book);
80 : /** Fetch quotes for a vector of commodities
81 : *
82 : * @param commodities std::vector of the gnc_commodity* to get quotes for.
83 : * @note Commodities without a quote source will be silently ignored.
84 : */
85 : void fetch (CommVec& commodities);
86 : /** Fetch quote for a single commodity
87 : *
88 : * @param comm Commodity for which to retrieve a quote
89 : * @note Commodity must have a quote source set or the call will silently fail.
90 : */
91 : void fetch (gnc_commodity *comm);
92 : /** Report quote results from Finance::Quote to std::cout.
93 : *
94 : * @param source A valid quote source
95 : * @param commodities A std::vector of symbols to request quotes for.
96 : * @note If requesting currency rates the first symbol is the to-currency and the rest are from-currencies. For example, {"USD", "EUR", "CAD"} will print the price of 1 Euro and 1 Canadian Dollar in US Dollars.
97 : * @param verbose Ignored for currency queries. If false it will print the six fields GnuCash uses regardless of whether a value was returned; if true it will print all of the fields for which Finanace::Quote returned values.
98 : */
99 : void report (const char* source, const StrVec& commodities, bool verbose = false);
100 : /** Get the installed Finance::Quote version
101 : *
102 : * @return the Finance::Quote version string
103 : */
104 : const std::string& version() noexcept;
105 :
106 : /** Get the available Finance::Quote sources as a std::vector
107 : *
108 : * @return The quote sources configured in Finance::Quote
109 : */
110 : const QuoteSources& sources() noexcept;
111 :
112 : /** Report if there were quotes requested but not retrieved.
113 : *
114 : * @returns True if there were quote failures.
115 : */
116 : bool had_failures() noexcept;
117 :
118 : /** Report the commodities for which quotes were requested but not successfully retrieved.
119 : *
120 : * This does not include requested commodities that didn't have a quote source.
121 : *
122 : * @return a reference to a vector of QuoteFailure tuples.
123 : * @note The vector and its contents belong to the GncQuotes object and will be destroyed with it.
124 : */
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 */
|