File indexing completed on 2024-07-14 04:55:01

0001 /*
0002     SPDX-FileCopyrightText: 2018 Ralf Habacker ralf.habacker @freenet.de
0003     SPDX-FileCopyrightText: 2019 Thomas Baumgart tbaumgart @kde.org
0004 
0005     This file is part of libalkimia.
0006 
0007     SPDX-License-Identifier: LGPL-2.1-or-later
0008 */
0009 
0010 #include "alkonlinequotesprofile.h"
0011 
0012 #include "alkonlinequotesprofilemanager.h"
0013 #include "alkonlinequotesource.h"
0014 
0015 #include "alkonlinequotesprofile_p.h"
0016 
0017 #include <KSharedConfig>
0018 #include <QtDebug>
0019 
0020 #if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
0021     #include <QRegularExpression>
0022     #include <QStandardPaths>
0023 #else
0024     #include <QRegExp>
0025     #include <KConfig>
0026     #include <KGlobal>
0027     #include <KStandardDirs>
0028 #endif
0029 
0030 AlkOnlineQuotesProfile::AlkOnlineQuotesProfile(const QString &name, Type type,
0031                                                const QString &ghnsConfigFile)
0032     : d(new Private(this))
0033 {
0034     d->m_name = name;
0035     d->m_GHNSFile = ghnsConfigFile;
0036     d->m_type = type;
0037     if (type == Type::KMyMoney5)
0038         d->m_kconfigFile = QString("%1/kmymoney/kmymoneyrc").arg(d->configPath());
0039     else if (type == Type::KMyMoney4)
0040         d->m_kconfigFile = QString("%1/kmymoneyrc").arg(d->configPath());
0041     else if (type == Type::Alkimia5 || type == Type::Alkimia4)
0042         d->m_kconfigFile = QString("%1/alkimiarc").arg(d->configPath());
0043     else
0044         d->m_kconfigFile.clear();
0045 
0046     if (!d->m_kconfigFile.isEmpty()) {
0047         d->m_config = KSharedConfig::openConfig(d->m_kconfigFile, KConfig::SimpleConfig);
0048         qDebug() << d->m_config;
0049     }
0050 
0051     if (!d->m_GHNSFile.isEmpty()) {
0052         QString ghnsFile = hotNewStuffConfigFile();
0053         KConfig ghnsConfig(ghnsFile);
0054         KConfigGroup group = ghnsConfig.group("KNewStuff3");
0055         d->m_GHNSFilePath = group.readEntry("TargetDir");
0056         d->m_engine->init(ghnsFile);
0057         d->m_engine->checkForUpdates();
0058     }
0059 }
0060 
0061 AlkOnlineQuotesProfile::~AlkOnlineQuotesProfile()
0062 {
0063     delete d;
0064 }
0065 
0066 QString AlkOnlineQuotesProfile::name() const
0067 {
0068     return d->m_name;
0069 }
0070 
0071 QString AlkOnlineQuotesProfile::hotNewStuffConfigFile() const
0072 {
0073 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
0074     QString configFile = QStandardPaths::locate(QStandardPaths::ConfigLocation, d->m_GHNSFile);
0075     if (configFile.isEmpty()) {
0076         configFile = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "knsrcfiles/" + d->m_GHNSFile);
0077     }
0078 #else
0079     QString configFile = KStandardDirs::locate("config", d->m_GHNSFile);
0080     if (configFile.isEmpty()) {
0081          configFile = KStandardDirs::locate("data", "knsrcfiles/" + d->m_GHNSFile);
0082     }
0083 #endif
0084     if (configFile.isEmpty()) {
0085         configFile = QString("%1/%2").arg(KNSRC_DIR, d->m_GHNSFile);
0086     }
0087 
0088     return configFile;
0089 }
0090 
0091 QString AlkOnlineQuotesProfile::hotNewStuffReadFilePath(const QString &fileName) const
0092 {
0093     for (const QString &path : hotNewStuffReadPath()) {
0094         QFileInfo f(path + fileName);
0095         if (f.exists())
0096             return f.absoluteFilePath();
0097     }
0098     return QString();
0099 }
0100 
0101 QString AlkOnlineQuotesProfile::hotNewStuffWriteFilePath(const QString &fileName) const
0102 {
0103     return QString("%1%2").arg(hotNewStuffWriteDir(), fileName);
0104 }
0105 
0106 QStringList AlkOnlineQuotesProfile::hotNewStuffReadPath() const
0107 {
0108     return QStringList()
0109         << QString("%1/%2/").arg(d->dataReadPath(), d->m_GHNSFilePath)
0110         << hotNewStuffWriteDir();
0111 }
0112 
0113 QString AlkOnlineQuotesProfile::hotNewStuffWriteDir() const
0114 {
0115     return QString("%1/%2/").arg(d->dataWritePath(), d->m_GHNSFilePath);
0116 }
0117 
0118 QString AlkOnlineQuotesProfile::hotNewStuffRelPath() const
0119 {
0120     return d->m_GHNSFilePath;
0121 }
0122 
0123 QString AlkOnlineQuotesProfile::kConfigFile() const
0124 {
0125     return d->m_kconfigFile;
0126 }
0127 
0128 KSharedConfigPtr AlkOnlineQuotesProfile::kConfig() const
0129 {
0130     return d->m_config;
0131 }
0132 
0133 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
0134 void AlkOnlineQuotesProfile::setKConfig(KSharedConfigPtr kconfig)
0135 {
0136     d->m_config = kconfig;
0137 }
0138 #endif
0139 
0140 AlkOnlineQuotesProfile::Type AlkOnlineQuotesProfile::type()
0141 {
0142     return d->m_type;
0143 }
0144 
0145 bool AlkOnlineQuotesProfile::typeIsSupported() const
0146 {
0147 #ifndef ENABLE_FINANCEQUOTE
0148     if (d->m_type == Type::Script) {
0149         return false;
0150     }
0151 #endif
0152     return true;
0153 }
0154 
0155 bool AlkOnlineQuotesProfile::typeIsOperational() const
0156 {
0157     if (d->m_type == Type::Script) {
0158 #ifdef ENABLE_FINANCEQUOTE
0159         if (d->quoteSourcesFinanceQuote().isEmpty()) {
0160             return false;
0161         }
0162 #else
0163         return false;
0164 #endif
0165     }
0166     return true;
0167 }
0168 
0169 bool AlkOnlineQuotesProfile::hasGHNSSupport()
0170 {
0171     return !d->m_GHNSFile.isEmpty();
0172 }
0173 
0174 const AlkOnlineQuotesProfile::Map AlkOnlineQuotesProfile::defaultQuoteSources()
0175 {
0176     return d->defaultQuoteSources();
0177 }
0178 
0179 const QStringList AlkOnlineQuotesProfile::quoteSources()
0180 {
0181     QStringList result;
0182     switch(d->m_type) {
0183     case AlkOnlineQuotesProfile::Type::Alkimia4:
0184     case AlkOnlineQuotesProfile::Type::Alkimia5:
0185     case AlkOnlineQuotesProfile::Type::KMyMoney4:
0186     case AlkOnlineQuotesProfile::Type::KMyMoney5:
0187         result << d->quoteSourcesNative();
0188         break;
0189 #ifdef ENABLE_FINANCEQUOTE
0190     case AlkOnlineQuotesProfile::Type::Script:
0191         result << d->quoteSourcesFinanceQuote();
0192         break;
0193 #endif
0194     case AlkOnlineQuotesProfile::Type::None:
0195         result << d->defaultQuoteSources().keys();
0196         break;
0197     default:
0198         break;
0199     }
0200     if (hasGHNSSupport())
0201         result << d->quoteSourcesGHNS();
0202     return result;
0203 }
0204 
0205 void AlkOnlineQuotesProfile::setManager(AlkOnlineQuotesProfileManager *manager)
0206 {
0207     d->m_profileManager = manager;
0208 }
0209 
0210 AlkOnlineQuotesProfileManager *AlkOnlineQuotesProfile::manager()
0211 {
0212     return d->m_profileManager;
0213 }
0214 
0215 QString AlkOnlineQuotesProfile::scriptPath()
0216 {
0217     return d->m_financeQuoteScriptPath;
0218 }
0219 
0220 QString AlkOnlineQuotesProfile::GHNSId(const QString &name) const
0221 {
0222     return d->GHNSId(name);
0223 }
0224 
0225 QString AlkOnlineQuotesProfile::GHNSFilePath(const QString &name) const
0226 {
0227     return d->GHNSFilePath(name);
0228 }