File indexing completed on 2024-05-12 16:41:59

0001 /*
0002     SPDX-FileCopyrightText: 2004 Ace Jones <acejones@users.sourceforge.net>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #ifndef WEBPRICEQUOTE_H
0007 #define WEBPRICEQUOTE_H
0008 
0009 // ----------------------------------------------------------------------------
0010 // QT Headers
0011 
0012 #include <QProcess>
0013 #include <QDate>
0014 
0015 // ----------------------------------------------------------------------------
0016 // KDE Headers
0017 
0018 // ----------------------------------------------------------------------------
0019 // Project Headers
0020 
0021 #include "csv/import/core/csvimportercore.h"
0022 
0023 class KJob;
0024 class QDate;
0025 class QTextCodec;
0026 /**
0027 Helper class to attend the process which is running the script, in the case
0028 of a local script being used to fetch the quote.
0029 
0030 @author Thomas Baumgart <thb@net-bembel.de> & Ace Jones <acejones@users.sourceforge.net>
0031 */
0032 class WebPriceQuoteProcess: public QProcess
0033 {
0034     Q_OBJECT
0035 public:
0036     WebPriceQuoteProcess();
0037     inline void setWebID(const QString& _webID) {
0038         m_webID = _webID;
0039         m_string.truncate(0);
0040     }
0041 
0042 public Q_SLOTS:
0043     void slotReceivedDataFromFilter();
0044     void slotProcessExited(int exitCode, QProcess::ExitStatus exitStatus);
0045 
0046 Q_SIGNALS:
0047     void processExited(const QString&);
0048 
0049 private:
0050     QString m_webID;
0051     QString m_string;
0052 };
0053 
0054 /**
0055 Helper class to run the Finance::Quote process. This is used only for the purpose of obtaining
0056 a list of valid sources. The actual price quotes are obtained thru WebPriceQuoteProcess.
0057 The class also contains functions to convert between the rather cryptic source names used
0058 by the Finance::Quote package, and more user-friendly names.
0059 
0060 @author Thomas Baumgart <thb@net-bembel.de> & Ace Jones <acejones@users.sourceforge.net>, Tony B<tonybloom@users.sourceforge.net>
0061  */
0062 class FinanceQuoteProcess: public QProcess
0063 {
0064     Q_OBJECT
0065 public:
0066     FinanceQuoteProcess();
0067     void launch(const QString& scriptPath);
0068     bool isFinished() const {
0069         return(m_isDone);
0070     };
0071     const QStringList getSourceList() const;
0072     const QString crypticName(const QString& niceName) const;
0073     const QString niceName(const QString& crypticName) const;
0074 
0075 public Q_SLOTS:
0076     void slotReceivedDataFromFilter();
0077     void slotProcessExited();
0078 
0079 private:
0080     bool m_isDone;
0081     QString m_string;
0082     typedef QMap<QString, QString> fqNameMap;
0083     fqNameMap m_fqNames;
0084 };
0085 
0086 /**
0087   * @author Thomas Baumgart & Ace Jones
0088   *
0089   * This is a helper class to store information about an online source
0090   * for stock prices or currency exchange rates.
0091   */
0092 struct WebPriceQuoteSource {
0093     enum identifyBy {Symbol, IdentificationNumber, Name};
0094     WebPriceQuoteSource() : m_webIDBy(Symbol), m_skipStripping(false) {}
0095     explicit WebPriceQuoteSource(const QString& name);
0096     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);
0097     ~WebPriceQuoteSource() {}
0098 
0099     void write() const;
0100     void rename(const QString& name);
0101     void remove() const;
0102 
0103     QString    m_name;
0104     QString    m_url;
0105     QString    m_csvUrl;
0106     QString    m_webID;
0107     identifyBy m_webIDBy;
0108     QString    m_price;
0109     QString    m_date;
0110     QString    m_dateformat;
0111     bool       m_skipStripping;
0112 };
0113 
0114 /**
0115 Retrieves a price quote from a web-based quote source
0116 
0117 @author Ace Jones <acejones@users.sourceforge.net>
0118 */
0119 class WebPriceQuote: public QObject
0120 {
0121     Q_OBJECT
0122 public:
0123     explicit WebPriceQuote(QObject* = 0);
0124     ~WebPriceQuote();
0125 
0126     typedef enum _quoteSystemE {
0127         Native = 0,
0128         FinanceQuote
0129     } quoteSystemE;
0130 
0131     void setDate(const QDate& _from, const QDate& _to);
0132     /**
0133       * This launches a web-based quote update for the given @p _webID.
0134       * When the quote is received back from the web source, it will be
0135       * emitted on the 'quote' signal.
0136       *
0137       * @param _webID the identification of the stock to fetch a price for
0138       * @param _kmmID an arbitrary identifier, which will be emitted in the quote
0139       *                signal when a price is sent back.
0140       * @param _source the source of the quote (must be a valid value returned
0141       *                by quoteSources().  Send QString() to use the default
0142       *                source.
0143       * @return bool Whether the quote fetch process was launched successfully
0144       */
0145 
0146     bool launch(const QString& _webID, const QString& _kmmID, const QString& _source = QString());
0147 
0148     /**
0149       * This returns a list of the names of the quote sources
0150       * currently defined.
0151       *
0152      * @param _system whether to return Native or Finance::Quote source list
0153      * @return QStringList of quote source names
0154       */
0155     static const QStringList quoteSources(const _quoteSystemE _system = Native);
0156     static const QMap<QString, PricesProfile> defaultCSVQuoteSources();
0157 
0158 Q_SIGNALS:
0159     void csvquote(const QString&, const QString&, MyMoneyStatement&);
0160     void quote(const QString&, const QString&, const QDate&, const double&);
0161     void failed(const QString&, const QString&);
0162     void status(const QString&);
0163     void error(const QString&);
0164 
0165 protected Q_SLOTS:
0166     void slotParseCSVQuote(const QString& filename);
0167     void slotParseQuote(const QString&);
0168     void downloadCSV(KJob* job);
0169     void downloadResult(KJob* job);
0170 
0171 protected:
0172     static const QMap<QString, WebPriceQuoteSource> defaultQuoteSources();
0173 
0174 private:
0175     bool launchCSV(const QString& _webID, const QString& _kmmID, const QString& _source = QString());
0176     bool launchNative(const QString& _webID, const QString& _kmmID, const QString& _source = QString());
0177     bool launchFinanceQuote(const QString& _webID, const QString& _kmmID, const QString& _source = QString());
0178     void enter_loop();
0179 
0180     static const QStringList quoteSourcesNative();
0181     static const QStringList quoteSourcesFinanceQuote();
0182 
0183 private:
0184     /// \internal d-pointer class.
0185     class Private;
0186     /// \internal d-pointer instance.
0187     Private* const d;
0188 
0189     static QString m_financeQuoteScriptPath;
0190     static QStringList m_financeQuoteSources;
0191 
0192 };
0193 
0194 class MyMoneyDateFormat
0195 {
0196 public:
0197     explicit MyMoneyDateFormat(const QString& _format): m_format(_format) {}
0198     const QString convertDate(const QDate& _in) const;
0199     const QDate convertString(const QString& _in, bool _strict = true, unsigned _centurymidpoint = QDate::currentDate().year()) const;
0200     const QString& format() const {
0201         return m_format;
0202     }
0203 private:
0204     QString m_format;
0205 };
0206 
0207 namespace convertertest
0208 {
0209 
0210 /**
0211 Simple class to handle signals/slots for unit tests
0212 
0213 @author Ace Jones <acejones@users.sourceforge.net>
0214 */
0215 class QuoteReceiver : public QObject
0216 {
0217     Q_OBJECT
0218 public:
0219     explicit QuoteReceiver(WebPriceQuote* q, QObject *parent = 0);
0220     ~QuoteReceiver();
0221 public Q_SLOTS:
0222     void slotGetQuote(const QString&, const QString&, const QDate&, const double&);
0223     void slotStatus(const QString&);
0224     void slotError(const QString&);
0225 public:
0226     QStringList m_statuses;
0227     QStringList m_errors;
0228     MyMoneyMoney m_price;
0229     QDate m_date;
0230 };
0231 
0232 } // end namespace convertertest
0233 
0234 
0235 #endif // WEBPRICEQUOTE_H