summaryrefslogtreecommitdiffstats
path: root/kmymoney2/converter/webpricequote.h
blob: e25dfb89933a22c190667696852a834a138e18ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/***************************************************************************
                          webpricequote.h
                             -------------------
    begin                : Thu Dec 30 2004
    copyright            : (C) 2004 by Ace Jones
    email                : Ace Jones <acejones@users.sourceforge.net>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef WEBPRICEQUOTE_H
#define WEBPRICEQUOTE_H

// ----------------------------------------------------------------------------
// QT Headers

#include <qobject.h>
#include <qdatetime.h>
#include <qstringlist.h>
#include <qmap.h>

// ----------------------------------------------------------------------------
// KDE Headers

#include <kprocess.h>
namespace KIO {
  class Job;
};

// ----------------------------------------------------------------------------
// Project Headers

#include "../mymoney/mymoneymoney.h"

/**
Helper class to attend the process which is running the script, in the case
of a local script being used to fetch the quote.

@author Thomas Baumgart <thb@net-bembel.de> & Ace Jones <acejones@users.sourceforge.net>
*/
class WebPriceQuoteProcess: public KProcess
{
  Q_OBJECT
public:
  WebPriceQuoteProcess(void);
  void setSymbol(const QString& _symbol) { m_symbol = _symbol; m_string.truncate(0); }

public slots:
  void slotReceivedDataFromFilter(KProcess*, char*, int);
  void slotProcessExited(KProcess*);

signals:
  void processExited(const QString&);

private:
  QString m_symbol;
  QString m_string;
};

/**
Helper class to run the Finance::Quote process. This is used only for the purpose of obtaining
a list of valid sources. The actual price quotes are obtained thru WebPriceQuoteProcess.
The class also contains functions to convert between the rather cryptic source names used
by the Finance::Quote package, and more user-friendly names.

@author Thomas Baumgart <thb@net-bembel.de> & Ace Jones <acejones@users.sourceforge.net>, Tony B<tonybloom@users.sourceforge.net>
 */
class FinanceQuoteProcess: public KProcess
{
  Q_OBJECT
  public:
    FinanceQuoteProcess(void);
    void launch (const QString& scriptPath);
    bool isFinished() { return(m_isDone);};
    QStringList getSourceList();
    const QString crypticName(const QString& niceName);
    const QString niceName(const QString& crypticName);

  public slots:
    void slotReceivedDataFromFilter(KProcess*, char*, int);
    void slotProcessExited(KProcess*);

  private:
    bool m_isDone;
    QString m_string;
    typedef QMap<QString, QString> fqNameMap;
    fqNameMap m_fqNames;
};

/**
  * @author Thomas Baumgart & Ace Jones
  *
  * This is a helper class to store information about an online source
  * for stock prices or currency exchange rates.
  */
struct WebPriceQuoteSource
{
  WebPriceQuoteSource() {}
  WebPriceQuoteSource(const QString& name);
  WebPriceQuoteSource(const QString& name, const QString& url, const QString& sym, const QString& price, const QString& date, const QString& dateformat);
  ~WebPriceQuoteSource() {}

  void write(void) const;
  void rename(const QString& name);
  void remove(void) const;

  QString    m_name;
  QString    m_url;
  QString    m_sym;
  QString    m_price;
  QString    m_date;
  QString    m_dateformat;
  bool       m_skipStripping;
};

/**
Retrieves a price quote from a web-based quote source

@author Ace Jones <acejones@users.sourceforge.net>
*/
class WebPriceQuote: public QObject
{
  Q_OBJECT
public:
  WebPriceQuote( QObject* = 0, const char* = 0 );
  ~WebPriceQuote();

  typedef enum _quoteSystemE {
    Native=0,
    FinanceQuote
  } quoteSystemE;

  /**
    * This launches a web-based quote update for the given @p _symbol.
    * When the quote is received back from the web source, it will be
    * emitted on the 'quote' signal.
    *
    * @param _symbol the trading symbol of the stock to fetch a price for
    * @param _id an arbitrary identifier, which will be emitted in the quote
    *                signal when a price is sent back.
    * @param _source the source of the quote (must be a valid value returned
    *                by quoteSources().  Send QString() to use the default
    *                source.
    * @return bool Whether the quote fetch process was launched successfully
    */

  bool launch(const QString& _symbol, const QString& _id, const QString& _source=QString());

  /**
    * This returns a list of the names of the quote sources
    * currently defined.
    *
   * @param _system whether to return Native or Finance::Quote source list
   * @return QStringList of quote source names
    */
  static QStringList quoteSources(const _quoteSystemE _system=Native);

signals:
  void quote(const QString&, const QString&, const QDate&, const double&);
  void failed(const QString&, const QString&);
  void status(const QString&);
  void error(const QString&);

protected slots:
  void slotParseQuote(const QString&);

protected:
  static QMap<QString,WebPriceQuoteSource> defaultQuoteSources(void);

private:
  bool download(const KURL& u, QString & target, QWidget* window);
  void removeTempFile(const QString& tmpFile);

private slots:
  void slotResult( KIO::Job * job );


private:
  bool launchNative(const QString& _symbol, const QString& _id, const QString& _source=QString());
  bool launchFinanceQuote(const QString& _symbol, const QString& _id, const QString& _source=QString());
  void enter_loop(void);

  static QStringList quoteSourcesNative();
  static QStringList quoteSourcesFinanceQuote();

  WebPriceQuoteProcess m_filter;
  QString m_symbol;
  QString m_id;
  QDate m_date;
  double m_price;
  WebPriceQuoteSource m_source;
  static QString m_financeQuoteScriptPath;
  static QStringList m_financeQuoteSources;


  /**
   * Whether the download succeeded or not. Taken from KIO::NetAccess
   */
  bool bJobOK;
  static QString* lastErrorMsg;
  static int lastErrorCode;
  QString m_tmpFile;
};

class MyMoneyDateFormat
{
public:
  MyMoneyDateFormat(const QString& _format): m_format(_format) {}
  QString convertDate(const QDate& _in) const;
  QDate convertString(const QString& _in, bool _strict=true, unsigned _centurymidpoint = QDate::currentDate().year() ) const;
  const QString& format(void) const { return m_format; }
private:
  QString m_format;
};

namespace convertertest {

/**
Simple class to handle signals/slots for unit tests

@author Ace Jones <acejones@users.sourceforge.net>
*/
class QuoteReceiver : public QObject
{
Q_OBJECT
public:
    QuoteReceiver(WebPriceQuote* q, QObject *parent = 0, const char *name = 0);
    ~QuoteReceiver();
public slots:
  void slotGetQuote(const QString&,const QDate&, const double&);
  void slotStatus(const QString&);
  void slotError(const QString&);
public:
  QStringList m_statuses;
  QStringList m_errors;
  MyMoneyMoney m_price;
  QDate m_date;
};

} // end namespace convertertest


#endif // WEBPRICEQUOTE_H

// vim:cin:si:ai:et:ts=2:sw=2: