1 /***************************************************************************
2                           webpricequote.h
3                              -------------------
4     begin                : Thu Dec 30 2004
5     copyright            : (C) 2004 by Ace Jones
6     email                : Ace Jones <acejones@users.sourceforge.net>
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #ifndef WEBPRICEQUOTE_H
19 #define WEBPRICEQUOTE_H
20 
21 // ----------------------------------------------------------------------------
22 // QT Headers
23 
24 #include <QProcess>
25 #include <QDate>
26 
27 // ----------------------------------------------------------------------------
28 // KDE Headers
29 
30 // ----------------------------------------------------------------------------
31 // Project Headers
32 
33 #include "csv/import/core/csvimportercore.h"
34 
35 class KJob;
36 class QDate;
37 class QTextCodec;
38 /**
39 Helper class to attend the process which is running the script, in the case
40 of a local script being used to fetch the quote.
41 
42 @author Thomas Baumgart <thb@net-bembel.de> & Ace Jones <acejones@users.sourceforge.net>
43 */
44 class WebPriceQuoteProcess: public QProcess
45 {
46   Q_OBJECT
47 public:
48   WebPriceQuoteProcess();
setWebID(const QString & _webID)49   inline void setWebID(const QString& _webID) {
50     m_webID = _webID; m_string.truncate(0);
51   }
52 
53 public Q_SLOTS:
54   void slotReceivedDataFromFilter();
55   void slotProcessExited(int exitCode, QProcess::ExitStatus exitStatus);
56 
57 Q_SIGNALS:
58   void processExited(const QString&);
59 
60 private:
61   QString m_webID;
62   QString m_string;
63 };
64 
65 /**
66 Helper class to run the Finance::Quote process. This is used only for the purpose of obtaining
67 a list of valid sources. The actual price quotes are obtained thru WebPriceQuoteProcess.
68 The class also contains functions to convert between the rather cryptic source names used
69 by the Finance::Quote package, and more user-friendly names.
70 
71 @author Thomas Baumgart <thb@net-bembel.de> & Ace Jones <acejones@users.sourceforge.net>, Tony B<tonybloom@users.sourceforge.net>
72  */
73 class FinanceQuoteProcess: public QProcess
74 {
75   Q_OBJECT
76 public:
77   FinanceQuoteProcess();
78   void launch(const QString& scriptPath);
isFinished()79   bool isFinished() const {
80     return(m_isDone);
81   };
82   const QStringList getSourceList() const;
83   const QString crypticName(const QString& niceName) const;
84   const QString niceName(const QString& crypticName) const;
85 
86 public Q_SLOTS:
87   void slotReceivedDataFromFilter();
88   void slotProcessExited();
89 
90 private:
91   bool m_isDone;
92   QString m_string;
93   typedef QMap<QString, QString> fqNameMap;
94   fqNameMap m_fqNames;
95 };
96 
97 /**
98   * @author Thomas Baumgart & Ace Jones
99   *
100   * This is a helper class to store information about an online source
101   * for stock prices or currency exchange rates.
102   */
103 struct WebPriceQuoteSource {
104   enum identifyBy {Symbol, IdentificationNumber, Name};
WebPriceQuoteSourceWebPriceQuoteSource105   WebPriceQuoteSource() : m_webIDBy(Symbol), m_skipStripping(false) {}
106   explicit WebPriceQuoteSource(const QString& name);
107   WebPriceQuoteSource(const QString& name, const QString& url, const QString& csvUrl, const QString& id, const identifyBy idBy, const QString& price, const QString& date, const QString& dateformat, bool skipStripping = false);
~WebPriceQuoteSourceWebPriceQuoteSource108   ~WebPriceQuoteSource() {}
109 
110   void write() const;
111   void rename(const QString& name);
112   void remove() const;
113 
114   QString    m_name;
115   QString    m_url;
116   QString    m_csvUrl;
117   QString    m_webID;
118   identifyBy m_webIDBy;
119   QString    m_price;
120   QString    m_date;
121   QString    m_dateformat;
122   bool       m_skipStripping;
123 };
124 
125 /**
126 Retrieves a price quote from a web-based quote source
127 
128 @author Ace Jones <acejones@users.sourceforge.net>
129 */
130 class WebPriceQuote: public QObject
131 {
132   Q_OBJECT
133 public:
134   explicit WebPriceQuote(QObject* = 0);
135   ~WebPriceQuote();
136 
137   typedef enum _quoteSystemE {
138     Native = 0,
139     FinanceQuote
140   } quoteSystemE;
141 
142   void setDate(const QDate& _from, const QDate& _to);
143   /**
144     * This launches a web-based quote update for the given @p _webID.
145     * When the quote is received back from the web source, it will be
146     * emitted on the 'quote' signal.
147     *
148     * @param _webID the identification of the stock to fetch a price for
149     * @param _kmmID an arbitrary identifier, which will be emitted in the quote
150     *                signal when a price is sent back.
151     * @param _source the source of the quote (must be a valid value returned
152     *                by quoteSources().  Send QString() to use the default
153     *                source.
154     * @return bool Whether the quote fetch process was launched successfully
155     */
156 
157   bool launch(const QString& _webID, const QString& _kmmID, const QString& _source = QString());
158 
159   /**
160     * This returns a list of the names of the quote sources
161     * currently defined.
162     *
163    * @param _system whether to return Native or Finance::Quote source list
164    * @return QStringList of quote source names
165     */
166   static const QStringList quoteSources(const _quoteSystemE _system = Native);
167   static const QMap<QString, PricesProfile> defaultCSVQuoteSources();
168 
169 Q_SIGNALS:
170   void csvquote(const QString&, const QString&, MyMoneyStatement&);
171   void quote(const QString&, const QString&, const QDate&, const double&);
172   void failed(const QString&, const QString&);
173   void status(const QString&);
174   void error(const QString&);
175 
176 protected Q_SLOTS:
177   void slotParseCSVQuote(const QString& filename);
178   void slotParseQuote(const QString&);
179   void downloadCSV(KJob* job);
180   void downloadResult(KJob* job);
181 
182 protected:
183   static const QMap<QString, WebPriceQuoteSource> defaultQuoteSources();
184 
185 private:
186   bool launchCSV(const QString& _webID, const QString& _kmmID, const QString& _source = QString());
187   bool launchNative(const QString& _webID, const QString& _kmmID, const QString& _source = QString());
188   bool launchFinanceQuote(const QString& _webID, const QString& _kmmID, const QString& _source = QString());
189   void enter_loop();
190 
191   static const QStringList quoteSourcesNative();
192   static const QStringList quoteSourcesFinanceQuote();
193 
194 private:
195   /// \internal d-pointer class.
196   class Private;
197   /// \internal d-pointer instance.
198   Private* const d;
199 
200   static QString m_financeQuoteScriptPath;
201   static QStringList m_financeQuoteSources;
202 
203 };
204 
205 class MyMoneyDateFormat
206 {
207 public:
MyMoneyDateFormat(const QString & _format)208   explicit MyMoneyDateFormat(const QString& _format): m_format(_format) {}
209   const QString convertDate(const QDate& _in) const;
210   const QDate convertString(const QString& _in, bool _strict = true, unsigned _centurymidpoint = QDate::currentDate().year()) const;
format()211   const QString& format() const {
212     return m_format;
213   }
214 private:
215   QString m_format;
216 };
217 
218 namespace convertertest
219 {
220 
221 /**
222 Simple class to handle signals/slots for unit tests
223 
224 @author Ace Jones <acejones@users.sourceforge.net>
225 */
226 class QuoteReceiver : public QObject
227 {
228   Q_OBJECT
229 public:
230   explicit QuoteReceiver(WebPriceQuote* q, QObject *parent = 0);
231   ~QuoteReceiver();
232 public Q_SLOTS:
233   void slotGetQuote(const QString&, const QString&, const QDate&, const double&);
234   void slotStatus(const QString&);
235   void slotError(const QString&);
236 public:
237   QStringList m_statuses;
238   QStringList m_errors;
239   MyMoneyMoney m_price;
240   QDate m_date;
241 };
242 
243 } // end namespace convertertest
244 
245 
246 #endif // WEBPRICEQUOTE_H
247