File indexing completed on 2024-04-28 05:02:30

0001 /*
0002     SPDX-FileCopyrightText: 2004 Ace Jones acejones @users.sourceforge.net
0003     SPDX-FileCopyrightText: 2019 Thomas Baumgart tbaumgart @kde.org
0004 
0005     This file is part of libalkimia.
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include "alkonlinequote.h"
0011 
0012 #include "alkonlinequote_p.h"
0013 
0014 AlkOnlineQuote::Errors::Errors()
0015 {
0016 }
0017 
0018 AlkOnlineQuote::Errors::Errors(Type type)
0019 {
0020     if (type != None)
0021         m_type.append(type);
0022     else
0023         m_type.clear();
0024 }
0025 
0026 AlkOnlineQuote::Errors& AlkOnlineQuote::Errors::operator|=(Type t)
0027 {
0028     if (!m_type.contains(t)) {
0029         m_type.append(t);
0030     }
0031     return *this;
0032 }
0033 
0034 bool AlkOnlineQuote::Errors::operator &(Type t) const
0035 {
0036     return m_type.contains(t);
0037 }
0038 
0039 
0040 AlkOnlineQuote::AlkOnlineQuote(AlkOnlineQuotesProfile *profile, QObject *_parent)
0041     : QObject(_parent)
0042     , d(new Private(this))
0043 {
0044     if (profile)
0045         d->m_profile = profile;
0046     else {
0047         d->m_profile = new AlkOnlineQuotesProfile;
0048         d->m_ownProfile = true;
0049     }
0050 }
0051 
0052 AlkOnlineQuote::~AlkOnlineQuote()
0053 {
0054     delete d;
0055 }
0056 
0057 AlkOnlineQuotesProfile *AlkOnlineQuote::profile()
0058 {
0059     return d->m_profile;
0060 }
0061 
0062 void AlkOnlineQuote::setProfile(AlkOnlineQuotesProfile *profile)
0063 {
0064     if (profile && d->m_ownProfile) {
0065         // switching from own profile to external
0066         delete d->m_profile;
0067         d->m_ownProfile = false;
0068         d->m_profile = profile;
0069 
0070     } else if (!profile && !d->m_ownProfile) {
0071         // switching from external to own profile
0072         d->m_profile = new AlkOnlineQuotesProfile;
0073         d->m_ownProfile = true;
0074 
0075     } else if (profile) {
0076         // exchange external profile
0077         d->m_profile = profile;
0078     }
0079 }
0080 
0081 void AlkOnlineQuote::setAcceptLanguage(const QString &language)
0082 {
0083     d->m_acceptLanguage = language;
0084 }
0085 
0086 int AlkOnlineQuote::timeout() const
0087 {
0088     return d->m_timeout;
0089 }
0090 
0091 void AlkOnlineQuote::setTimeout(int newTimeout)
0092 {
0093     d->m_timeout = newTimeout;
0094 }
0095 
0096 void AlkOnlineQuote::setDateRange(const QDate &from, const QDate &to)
0097 {
0098     d->m_startDate = from;
0099     d->m_endDate = to;
0100 }
0101 
0102 void AlkOnlineQuote::setUseSingleQuoteSignal(bool state)
0103 {
0104     d->m_useSingleQuoteSignal = state;
0105 }
0106 
0107 bool AlkOnlineQuote::useSingleQuoteSignal()
0108 {
0109     return d->m_useSingleQuoteSignal;
0110 }
0111 
0112 const AlkOnlineQuoteSource &AlkOnlineQuote::source() const
0113 {
0114     return d->m_source;
0115 }
0116 
0117 bool AlkOnlineQuote::launch(const QString &_symbol, const QString &_id, const QString &_source)
0118 {
0119 #ifdef ENABLE_FINANCEQUOTE
0120     if (AlkOnlineQuoteSource::isFinanceQuote(_source) ||
0121             d->m_profile->type() == AlkOnlineQuotesProfile::Type::Script) {
0122         return d->launchFinanceQuote(_symbol, _id, _source);
0123     } else
0124 #endif
0125     if (_source.endsWith(QLatin1String(".css"))) {
0126         return d->launchWebKitCssSelector(_symbol, _id, _source);
0127     } else if (_source.endsWith(QLatin1String(".webkit"))) {
0128         return d->launchWebKitHtmlParser(_symbol, _id, _source);
0129     } else {
0130         return d->launchNative(_symbol, _id, _source);
0131     }
0132 }
0133 
0134 const AlkOnlineQuote::Errors &AlkOnlineQuote::errors()
0135 {
0136     return d->m_errors;
0137 }
0138 
0139 AlkOnlineQuote::Private &AlkOnlineQuote::d_ptr()
0140 {
0141     return *d;
0142 }