File indexing completed on 2024-05-19 05:06:56

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