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

0001 /*
0002  * SPDX-FileCopyrightText: 2018,2023 Ralf Habacker ralf.habacker@freenet.de
0003  * SPDX-FileCopyrightText: 2023 Thomas Baumgart <tbaumgart@kde.org>
0004  * SPDX-License-Identifier: LGPL-2.1-or-later
0005  *
0006  * This file is part of libalkimia.
0007  */
0008 
0009 #ifndef ALKONLINEQUOTESOURCE_P_H
0010 #define ALKONLINEQUOTESOURCE_P_H
0011 
0012 #include "alkonlinequotesource.h"
0013 
0014 #include <QFile>
0015 #include <QFileInfo>
0016 #include <QtDebug>
0017 
0018 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
0019 #include <KSharedConfig>
0020 #else
0021 #include <KConfig>
0022 #endif
0023 #include <KConfigGroup>
0024 
0025 class ALK_NO_EXPORT AlkOnlineQuoteSource::Private
0026 {
0027 public:
0028     Private()
0029         : m_dataFormat(HTML)
0030         , m_idSelector(Symbol)
0031         , m_profile(nullptr)
0032         , m_isGHNSSource(false)
0033         , m_storageChanged(false)
0034         , m_readOnly(true)
0035     {
0036     }
0037 
0038     explicit Private(const Private* other)
0039         : m_name(other->m_name)
0040         , m_url(other->m_url)
0041         , m_priceRegex(other->m_priceRegex)
0042         , m_dataFormat(other->m_dataFormat)
0043         , m_dateRegex(other->m_dateRegex)
0044         , m_dateFormat(other->m_dateFormat)
0045         , m_idRegex(other->m_idRegex)
0046         , m_idSelector(other->m_idSelector)
0047         , m_profile(other->m_profile)
0048         , m_isGHNSSource(other->m_isGHNSSource)
0049         , m_storageChanged(other->m_storageChanged)
0050         , m_readOnly(other->m_readOnly)
0051     {
0052     }
0053 
0054     bool read()
0055     {
0056         auto kconfig = m_profile->kConfig();
0057         if (!kconfig)
0058             return false;
0059         const QString &group = QString("Online-Quote-Source-%1").arg(m_name);
0060         if (!kconfig->hasGroup(group)) {
0061             return false;
0062         }
0063         KConfigGroup grp = kconfig->group(group);
0064         // in newer profiles DataFormat takes precedence over SkipStripping
0065         // if DataFormat is not present, search for SkipStripping
0066         if (grp.hasKey(QLatin1String("DataFormat"))) {
0067             m_dataFormat = static_cast<DataFormat>(grp.readEntry(QLatin1String("DataFormat"), 0));
0068         } else {
0069             m_dataFormat = grp.readEntry<bool>(QLatin1String("SkipStripping"), false) == true ? HTML : StrippedHTML;
0070         }
0071 
0072         m_dateRegex = grp.readEntry("DateRegex");
0073         m_dateFormat = grp.readEntry("DateFormatRegex", "%m %d %y");
0074         if (grp.hasKey("DefaultId"))
0075             m_defaultId = grp.readEntry("DefaultId");
0076         else if (grp.hasKey("DebugId")) // For compatibility with 8.1.72
0077             m_defaultId = grp.readEntry("DebugId");
0078         m_priceRegex = grp.readEntry("PriceRegex");
0079         if (grp.hasKey("SymbolRegex"))
0080             m_idRegex = grp.readEntry("SymbolRegex");
0081         else
0082             m_idRegex = grp.readEntry("IDRegex");
0083         m_idSelector = static_cast<IdSelector>(grp.readEntry("IDBy", "0").toInt());
0084         m_url = grp.readEntry("URL");
0085 
0086         m_isGHNSSource = false;
0087         m_readOnly = false;
0088         return true;
0089     }
0090 
0091     bool write()
0092     {
0093         auto kconfig = m_profile->kConfig();
0094         if (!kconfig)
0095             return false;
0096         KConfigGroup grp = kconfig->group(QString("Online-Quote-Source-%1").arg(m_name));
0097         grp.writeEntry("URL", m_url);
0098         grp.writeEntry("PriceRegex", m_priceRegex);
0099         grp.writeEntry("DataFormat", static_cast<int>(m_dataFormat));
0100         grp.writeEntry("DateRegex", m_dateRegex);
0101         grp.writeEntry("DateFormatRegex", m_dateFormat);
0102         grp.deleteEntry("DebugId");
0103         grp.writeEntry("DefaultId", m_defaultId);
0104         grp.writeEntry("IDRegex", m_idRegex);
0105         grp.writeEntry("IDBy", static_cast<int>(m_idSelector));
0106         /// @todo remove the following code block if backward
0107         /// compatibility is not needed anymore. for some time,
0108         /// we maintain it though
0109         if (m_dataFormat == HTML) {
0110             grp.writeEntry(QLatin1String("SkipStripping"), true);
0111         } else {
0112             grp.deleteEntry("SkipStripping");
0113         }
0114         grp.deleteEntry("SymbolRegex");
0115 
0116 
0117         kconfig->sync();
0118         return true;
0119     }
0120 
0121     bool remove()
0122     {
0123         auto kconfig = m_profile->kConfig();
0124         if (!kconfig)
0125             return false;
0126         kconfig->deleteGroup(QString("Online-Quote-Source-%1").arg(m_name));
0127         kconfig->sync();
0128         return true;
0129     }
0130 
0131     QString ghnsReadFilePath()
0132     {
0133         QString file = m_profile->GHNSFilePath(m_name);
0134         if (!file.isEmpty())
0135             return file;
0136         return m_profile->hotNewStuffReadFilePath(m_name + QLatin1String(".txt"));
0137     }
0138 
0139     QString ghnsWriteFilePath()
0140     {
0141         QString file = m_profile->GHNSFilePath(m_name);
0142         if (!file.isEmpty())
0143             return file;
0144         return m_profile->hotNewStuffWriteFilePath(m_name + QLatin1String(".txt"));
0145     }
0146 
0147     // This is currently in skrooge format
0148     bool readFromGHNSFile()
0149     {
0150         QFileInfo f(ghnsReadFilePath());
0151         if (!f.exists())
0152             f.setFile(ghnsWriteFilePath());
0153         m_readOnly = !f.isWritable();
0154 
0155         QFile file(f.absoluteFilePath());
0156         if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
0157             return false;
0158 
0159         QTextStream in(&file);
0160         while (!in.atEnd()) {
0161             QString line = in.readLine();
0162             int index = line.indexOf("=");
0163             if (index == -1)
0164                 return false;
0165             QString key = line.left(index);
0166             QString value = line.mid(index+1);
0167             if (key == "url")
0168                 m_url = value;
0169             else if (key == "price") {
0170                 m_priceRegex = value;
0171                 m_priceRegex.replace("\\\\", "\\");
0172             } else if (key == "date") {
0173                 m_dateRegex = value;
0174                 m_dateRegex.replace("\\\\", "\\");
0175             } else if (key == "dateformat")
0176                 m_dateFormat = value;
0177             else if (key == "defaultid")
0178                 m_defaultId = value;
0179             else if (key == "debugid") // for compatibility with 8.1.72
0180                 m_defaultId = value;
0181             else if (key == "mode") {
0182                 if (value == "StrippedHTML")
0183                     m_dataFormat = AlkOnlineQuoteSource::DataFormat::StrippedHTML;
0184                 else if (value == "HTML")
0185                     m_dataFormat = AlkOnlineQuoteSource::DataFormat::HTML;
0186                 else if (value == "CSV")
0187                     m_dataFormat = AlkOnlineQuoteSource::DataFormat::CSV;
0188             }
0189         }
0190 
0191         m_isGHNSSource = true;
0192         return true;
0193     }
0194 
0195     // This is currently in skrooge format
0196     bool writeToGHNSFile()
0197     {
0198         QFile file(ghnsWriteFilePath());
0199         if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
0200             return false;
0201 
0202         QTextStream out(&file);
0203         out << "date=" << m_dateRegex << "\n";
0204         out << "dateformat=" << m_dateFormat << "\n";
0205         out << "defaultid=" << m_defaultId << "\n";
0206         if (m_dataFormat == AlkOnlineQuoteSource::DataFormat::StrippedHTML)
0207             out << "mode=StrippedHTML\n";
0208         else if (m_dataFormat == AlkOnlineQuoteSource::DataFormat::HTML)
0209             out << "mode=HTML\n";
0210         else if (m_dataFormat == AlkOnlineQuoteSource::DataFormat::CSV)
0211             out << "mode=CSV\n";
0212         out << "price=" << m_priceRegex << "\n";
0213         out << "url=" << m_url << "\n";
0214         return true;
0215     }
0216 
0217     bool removeGHNSFile()
0218     {
0219         qDebug() << "delete" << ghnsWriteFilePath();
0220         return true;
0221     }
0222 
0223     QString m_name;
0224     QString m_url;
0225     QString m_priceRegex;
0226     DataFormat m_dataFormat;
0227     QString m_dateRegex;
0228     QString m_dateFormat;
0229     QString m_defaultId;
0230     QString m_idRegex;
0231     IdSelector m_idSelector;
0232     AlkOnlineQuotesProfile *m_profile;
0233     bool m_isGHNSSource;
0234     bool m_storageChanged;
0235     bool m_readOnly;
0236 };
0237 
0238 #endif // ALKONLINEQUOTESOURCE_P_H