Alkimia API

alkonlinequotesprofile.cpp
1/*
2 SPDX-FileCopyrightText: 2018 Ralf Habacker ralf.habacker @freenet.de
3 SPDX-FileCopyrightText: 2019 Thomas Baumgart tbaumgart @kde.org
4
5 This file is part of libalkimia.
6
7 SPDX-License-Identifier: LGPL-2.1-or-later
8*/
9
10#include "alkonlinequotesprofile.h"
11
12#include "alkdebug.h"
13
14#include "alkonlinequotesource.h"
15#include "alkonlinequotesprofilemanager.h"
16
17#include "alkonlinequotesprofile_p.h"
18
19#include <KSharedConfig>
20
21#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
22#include <QRegularExpression>
23#include <QStandardPaths>
24#else
25#include <KConfig>
26#include <KGlobal>
27#include <KStandardDirs>
28#include <QRegExp>
29#endif
30
31AlkOnlineQuotesProfile::AlkOnlineQuotesProfile(const QString &name, Type type,
32 const QString &ghnsConfigFile)
33 : d(new Private(this))
34{
35 d->m_name = name;
36 d->m_GHNSFile = ghnsConfigFile;
37 d->m_type = type;
38 if (type == Type::KMyMoney5)
39 d->m_kconfigFile = QString("%1/kmymoney/kmymoneyrc").arg(d->configPath());
40 else if (type == Type::KMyMoney4)
41 d->m_kconfigFile = QString("%1/kmymoneyrc").arg(d->configPath());
42 else if (type == Type::Alkimia5 || type == Type::Alkimia4)
43 d->m_kconfigFile = QString("%1/alkimiarc").arg(d->configPath());
44 else if (type == Type::Test)
45 d->m_kconfigFile = QString("%1/alkimiatestrc").arg(d->configPath());
46 else
47 d->m_kconfigFile.clear();
48
49 if (!d->m_kconfigFile.isEmpty()) {
50 d->m_config = KSharedConfig::openConfig(d->m_kconfigFile, KConfig::SimpleConfig);
51 alkDebug() << d->m_config;
52 }
53
54 if (!d->m_GHNSFile.isEmpty()) {
55 QString ghnsFile = hotNewStuffConfigFile();
56 KConfig ghnsConfig(ghnsFile);
57 KConfigGroup group = ghnsConfig.group("KNewStuff3");
58 d->m_GHNSFilePath = group.readEntry("TargetDir");
59 d->m_engine->init(ghnsFile);
60 d->m_engine->checkForUpdates();
61 }
62}
63
64AlkOnlineQuotesProfile::~AlkOnlineQuotesProfile()
65{
66 delete d;
67}
68
69QString AlkOnlineQuotesProfile::name() const
70{
71 return d->m_name;
72}
73
74QString AlkOnlineQuotesProfile::hotNewStuffConfigFile() const
75{
76#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
78 if (configFile.isEmpty()) {
79 configFile = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "knsrcfiles/" + d->m_GHNSFile);
80 }
81#else
82 QString configFile = KStandardDirs::locate("config", d->m_GHNSFile);
83 if (configFile.isEmpty()) {
84 configFile = KStandardDirs::locate("data", "knsrcfiles/" + d->m_GHNSFile);
85 }
86#endif
87 if (configFile.isEmpty()) {
88 configFile = QString("%1/%2").arg(KNSRC_DIR, d->m_GHNSFile);
89 }
90
91 return configFile;
92}
93
94QString AlkOnlineQuotesProfile::hotNewStuffReadFilePath(const QString &fileName) const
95{
96 for (const QString &path : hotNewStuffReadPath()) {
97 QFileInfo f(path + fileName);
98 if (f.exists())
99 return f.absoluteFilePath();
100 }
101 return QString();
102}
103
104QString AlkOnlineQuotesProfile::hotNewStuffWriteFilePath(const QString &fileName) const
105{
106 return QString("%1%2").arg(hotNewStuffWriteDir(), fileName);
107}
108
109QStringList AlkOnlineQuotesProfile::hotNewStuffReadPath() const
110{
111 return QStringList()
112 << QString("%1/%2/").arg(d->dataReadPath(), d->m_GHNSFilePath)
113 << hotNewStuffWriteDir();
114}
115
116QString AlkOnlineQuotesProfile::hotNewStuffWriteDir() const
117{
118 return QString("%1/%2/").arg(d->dataWritePath(), d->m_GHNSFilePath);
119}
120
121QString AlkOnlineQuotesProfile::hotNewStuffRelPath() const
122{
123 return d->m_GHNSFilePath;
124}
125
126QString AlkOnlineQuotesProfile::kConfigFile() const
127{
128 return d->m_kconfigFile;
129}
130
131KSharedConfigPtr AlkOnlineQuotesProfile::kConfig() const
132{
133 return d->m_config;
134}
135
136#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
137void AlkOnlineQuotesProfile::setKConfig(KSharedConfigPtr kconfig)
138{
139 d->m_config = kconfig;
140}
141#endif
142
143void AlkOnlineQuotesProfile::reload()
144{
145 d->reload();
146 Q_EMIT sourcesChanged();
147}
148
149AlkOnlineQuotesProfile::Type AlkOnlineQuotesProfile::type()
150{
151 return d->m_type;
152}
153
154bool AlkOnlineQuotesProfile::typeIsSupported() const
155{
156#ifndef ENABLE_FINANCEQUOTE
157 if (d->m_type == Type::Script) {
158 return false;
159 }
160#endif
161 return true;
162}
163
164bool AlkOnlineQuotesProfile::typeIsOperational() const
165{
166 if (d->m_type == Type::Script) {
167#ifdef ENABLE_FINANCEQUOTE
168 if (d->quoteSourcesFinanceQuote().isEmpty()) {
169 return false;
170 }
171#else
172 return false;
173#endif
174 }
175 return true;
176}
177
178bool AlkOnlineQuotesProfile::hasGHNSSupport()
179{
180 return !d->m_GHNSFile.isEmpty();
181}
182
183const AlkOnlineQuotesProfile::Map AlkOnlineQuotesProfile::defaultQuoteSources()
184{
185 switch(d->m_type) {
186 case AlkOnlineQuotesProfile::Type::Test:
187 return d->quoteSourcesTesting();
188 default:
189 return d->defaultQuoteSources();
190 }
191}
192
193const QStringList AlkOnlineQuotesProfile::quoteSources()
194{
195 QStringList result;
196 switch(d->m_type) {
197 case AlkOnlineQuotesProfile::Type::Alkimia4:
198 case AlkOnlineQuotesProfile::Type::Alkimia5:
199 case AlkOnlineQuotesProfile::Type::KMyMoney4:
200 case AlkOnlineQuotesProfile::Type::KMyMoney5:
201 result << d->quoteSourcesNative();
202 break;
203#ifdef ENABLE_FINANCEQUOTE
204 case AlkOnlineQuotesProfile::Type::Script:
205 result << d->quoteSourcesFinanceQuote();
206 break;
207#endif
208 case AlkOnlineQuotesProfile::Type::None:
209 result << d->defaultQuoteSources().keys();
210 break;
211 case AlkOnlineQuotesProfile::Type::Test:
212 result << d->quoteSourcesNative();
213 break;
214 default:
215 break;
216 }
217 if (hasGHNSSupport())
218 result << d->quoteSourcesGHNS();
219 return result;
220}
221
222const AlkOnlineQuotesProfile::Map AlkOnlineQuotesProfile::quoteSourcesTesting()
223{
224 return d->quoteSourcesTesting();
225}
226
227void AlkOnlineQuotesProfile::setManager(AlkOnlineQuotesProfileManager *manager)
228{
229 d->m_profileManager = manager;
230}
231
232AlkOnlineQuotesProfileManager *AlkOnlineQuotesProfile::manager()
233{
234 return d->m_profileManager;
235}
236
237QString AlkOnlineQuotesProfile::scriptPath()
238{
239 return d->m_financeQuoteScriptPath;
240}
241
242QString AlkOnlineQuotesProfile::GHNSId(const QString &name) const
243{
244 return d->GHNSId(name);
245}
246
247QString AlkOnlineQuotesProfile::GHNSFilePath(const QString &name) const
248{
249 return d->GHNSFilePath(name);
250}
251
252QString AlkOnlineQuotesProfile::GHNSName(const QString &id) const
253{
254 return d->GHNSName(id);
255}
Wrapper for debug output.
Type type(const QSqlDatabase &db)
QString name(StandardAction id)
Q_EMITQ_EMIT
QString locate(StandardLocation type, const QString &fileName, LocateOptions options)
QString arg(Args &&... args) const const
void clear()
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.