9#include "alkonlinequotesource.h"
11#include "alkonlinequotesource_p.h"
12#include "alkonlinequotesprofile.h"
13#include "alktestdefs.h"
20static const char *fqName =
"Finance::Quote";
22AlkOnlineQuoteSource::AlkOnlineQuoteSource()
28 : d(new Private(other.d))
38AlkOnlineQuoteSource::AlkOnlineQuoteSource(
const QString& name,
41 const IdSelector idBy,
45 DataFormat dataFormat,
46 DecimalSeparator priceDecimalSeparator,
47 DownloadType downloadType)
52 d->m_idRegex = idRegex;
53 d->m_idSelector = idBy;
54 d->m_priceDecimalSeparator = priceDecimalSeparator;
55 d->m_priceRegex = priceRegex;
56 d->m_dataFormat = dataFormat;
57 d->m_dateRegex = dateRegex;
58 d->m_dateFormat = dateFormat;
59 d->m_downloadType = downloadType;
60 d->m_isGHNSSource =
false;
63AlkOnlineQuoteSource::AlkOnlineQuoteSource(
const QString &name, AlkOnlineQuotesProfile *profile)
66 if ((profile !=
nullptr) && (profile->type() == AlkOnlineQuotesProfile::Type::None) && (profile->defaultQuoteSources().
contains(name))) {
67 *this = profile->defaultQuoteSources()[name];
69 d->m_profile = profile;
78 "https://fx-rate.net/%1/%2",
80 AlkOnlineQuoteSource::Symbol,
81 "Today\\s+=\\s+([^<]+)",
82 ",\\s*(\\d+\\s*[a-zA-Z]{3}\\s*\\d{4})",
87AlkOnlineQuoteSource AlkOnlineQuoteSource::testQuoteSource(
const QString& name,
bool twoSymbols, DownloadType downloadType, DataFormat format)
89 QString urlString = TEST_LAUNCH_URL;
100 if (format == HTML) {
102 priceRegexString =
QLatin1String(
"</span><br\\s*/>\\s+([\\d.]+)\\s+\\w");
108 }
else if (format == CSV) {
112 }
else if (format == JSON) {
114 priceRegexString =
QLatin1String(
"chart:result:indicators:quote:open");
122 if (downloadType == Javascript) {
124 priceRegexString =
QLatin1String(
"<span>\\s*([\\d.]+)\\s+\\w");
131 AlkOnlineQuoteSource::Symbol,
136 AlkOnlineQuoteSource::Period,
141AlkOnlineQuoteSource::~AlkOnlineQuoteSource()
153 return !d->m_referenceId.isEmpty();
156bool AlkOnlineQuoteSource::isEmpty()
158 return !isValid() && !d->m_url.isEmpty();
161bool AlkOnlineQuoteSource::isValid()
163 return !d->m_name.isEmpty();
168 return d->m_profile->GHNSName(d->m_referenceId);
171QString AlkOnlineQuoteSource::name()
const
176QString AlkOnlineQuoteSource::url()
const
181QString AlkOnlineQuoteSource::idRegex()
const
186AlkOnlineQuoteSource::IdSelector AlkOnlineQuoteSource::idSelector()
const
188 return d->m_idSelector;
191QString AlkOnlineQuoteSource::priceRegex()
const
193 return d->m_priceRegex;
198 return d->m_priceDecimalSeparator;
201QString AlkOnlineQuoteSource::dateRegex()
const
203 return d->m_dateRegex;
211 return d->m_dataFormat;
214QString AlkOnlineQuoteSource::dateFormat()
const
216 return d->m_dateFormat;
219AlkOnlineQuoteSource::DownloadType AlkOnlineQuoteSource::downloadType()
const
221 return d->m_downloadType;
233 return d->m_name.section(
' ', 1);
238 d->m_referenceId = d->m_profile->GHNSId(name);
241void AlkOnlineQuoteSource::setName(
const QString &name)
246void AlkOnlineQuoteSource::setUrl(
const QString &url)
251void AlkOnlineQuoteSource::setPriceRegex(
const QString &priceRegex)
253 d->m_priceRegex = priceRegex;
256void AlkOnlineQuoteSource::setPriceDecimalSeparator(DecimalSeparator separator)
258 d->m_priceDecimalSeparator = separator;
261void AlkOnlineQuoteSource::setIdRegex(
const QString &idRegex)
263 d->m_idRegex = idRegex;
266void AlkOnlineQuoteSource::setIdSelector(AlkOnlineQuoteSource::IdSelector idSelector)
268 d->m_idSelector = idSelector;
282 d->m_dateRegex = dateRegex;
290void AlkOnlineQuoteSource::setDateFormat(
const QString &dateFormat)
292 d->m_dateFormat = dateFormat;
295void AlkOnlineQuoteSource::setDownloadType(DownloadType downloadType)
297 d->m_downloadType = downloadType;
300void AlkOnlineQuoteSource::setGHNS(
bool state)
302 d->m_storageChanged = d->m_isGHNSSource != state;
303 d->m_isGHNSSource = state;
306bool AlkOnlineQuoteSource::isGHNS()
const
308 return d->m_isGHNSSource;
311bool AlkOnlineQuoteSource::isReadOnly()
const
313 return d->m_readOnly;
323 return d->m_name.contains(fqName);
336QString AlkOnlineQuoteSource::ghnsWriteFileName()
const
338 return d->ghnsWriteFilePath();
341void AlkOnlineQuoteSource::setProfile(AlkOnlineQuotesProfile *profile)
343 d->m_profile = profile;
344 alkDebug() <<
"using profile" << profile->name();
347AlkOnlineQuotesProfile *AlkOnlineQuoteSource::profile()
352AlkOnlineQuotesProfile *AlkOnlineQuoteSource::profile()
const
357bool AlkOnlineQuoteSource::read()
359 if (d->m_profile->hasGHNSSupport()) {
360 if (d->readFromGHNSFile()) {
367bool AlkOnlineQuoteSource::write()
372 if (d->m_profile->hasGHNSSupport() && d->m_isGHNSSource) {
373 result = d->writeToGHNSFile();
374 if (d->m_storageChanged)
378 if (d->m_storageChanged && d->m_profile->quoteSources().contains(d->m_name))
379 d->m_name.append(
".local");
381 if (d->m_profile->hasGHNSSupport() && d->m_storageChanged) {
385 d->m_storageChanged =
false;
390void AlkOnlineQuoteSource::rename(
const QString &name)
392 if (d->m_profile->type() != AlkOnlineQuotesProfile::Type::None) {
400void AlkOnlineQuoteSource::remove()
402 if (d->m_profile->hasGHNSSupport() && d->m_isGHNSSource) {
404 }
else if (d->m_profile->type() != AlkOnlineQuotesProfile::Type::None) {
411 return d->m_defaultId;
429 return d->m_dataFormat == CSV || d->m_dataFormat == JSON;
432AlkOnlineQuoteSource::Private::Private(
const Private *other)
433 : m_referenceId(other->m_referenceId)
434 , m_name(other->m_name)
435 , m_url(other->m_url)
436 , m_priceDecimalSeparator(other->m_priceDecimalSeparator)
437 , m_priceRegex(other->m_priceRegex)
438 , m_dataFormat(other->m_dataFormat)
439 , m_dateRegex(other->m_dateRegex)
440 , m_dateFormat(other->m_dateFormat)
441 , m_defaultId(other->m_defaultId)
442 , m_downloadType(other->m_downloadType)
443 , m_idRegex(other->m_idRegex)
444 , m_idSelector(other->m_idSelector)
445 , m_profile(other->m_profile)
446 , m_isGHNSSource(other->m_isGHNSSource)
447 , m_storageChanged(other->m_storageChanged)
448 , m_readOnly(other->m_readOnly)
455 case AlkOnlineQuoteSource::StrippedHTML:
456 return i18nc(
"@item:inlistbox Stock",
"Stripped HTML");
457 case AlkOnlineQuoteSource::DataFormat::HTML:
458 return i18nc(
"@item:inlistbox Stock",
"HTML");
459 case AlkOnlineQuoteSource::DataFormat::CSV:
460 return i18nc(
"@item:inlistbox Stock",
"CSV");
461 case AlkOnlineQuoteSource::DataFormat::CSS:
462 return i18nc(
"@item:inlistbox Stock",
"CSS");
463 case AlkOnlineQuoteSource::DataFormat::JSON:
464 return i18nc(
"@item:inlistbox Stock",
"JSON");
void setReferenceName(const QString &name)
Make this source a reference.
const QString & defaultId() const
Return the default identifier known to work.
void setDefaultId(const QString &defaultId)
Set the default identifier, which is known to work.
bool requiresTwoIdentifier() const
Return state if this source requires two identifier.
DataFormat
Supported formats of downloaded data.
bool isReference() const
Return state if this source is a reference.
DecimalSeparator
Type of decimal separator.
AlkOnlineQuoteSource asReference() const
Return referenced quote source.
QString financeQuoteName() const
Returns the name of the "Finance::Quote" source.
DataFormat dataFormat() const
Return the format of the downloaded data.
bool isFinanceQuote() const
Checks whether the current source is of type "Finance::Quote".
void setDataFormat(DataFormat dataFormat)
Set the format of the downloaded data.
QString referenceName() const
Return name of the referenced quote source.
void setDateRegex(const QString &dateRegex)
Set regular expression for parsing dates.
bool supportsDateRange() const
Return state if this source supports a date range.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
Type type(const QSqlDatabase &db)
char * toString(const EngineQuery &query)
QString name(StandardAction id)
bool contains(const Key &key) const const
QString & append(QChar ch)
bool contains(QChar ch, Qt::CaseSensitivity cs) const const