alkimia  8.0.2
alkonlinequotesource.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright 2018 Ralf Habacker <ralf.habacker@freenet.de> *
3  * *
4  * This file is part of libalkimia. *
5  * *
6  * libalkimia is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public License *
8  * as published by the Free Software Foundation; either version 2.1 of *
9  * the License or (at your option) version 3 or any later version. *
10  * *
11  * libalkimia is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with this program. If not, see <http://www.gnu.org/licenses/> *
18  ***************************************************************************/
19 
20 #include "alkonlinequotesource.h"
21 
22 #include "alkonlinequotesprofile.h"
23 
24 #include <QFile>
25 #include <QFileInfo>
26 #include <QtDebug>
27 
28 #include <KConfig>
29 #include <KConfigGroup>
30 
32 {
33 public:
34  Private()
35  : m_skipStripping(false)
36  , m_profile(nullptr)
37  , m_isGHNSSource(false)
38  , m_storageChanged(false)
39  , m_readOnly(true)
40  {
41  }
42 
43  Private(const Private *other)
44  : m_name(other->m_name)
45  , m_url(other->m_url)
46  , m_sym(other->m_sym)
47  , m_price(other->m_price)
48  , m_date(other->m_date)
49  , m_dateformat(other->m_dateformat)
51  , m_profile(other->m_profile)
52  , m_isGHNSSource(other->m_isGHNSSource)
53  , m_readOnly(other->m_readOnly)
54  {
55  }
56 
57  bool read()
58  {
59  KConfig *kconfig = m_profile->kConfig();
60  if (!kconfig)
61  return false;
62  const QString &group = QString("Online-Quote-Source-%1").arg(m_name);
63  if (!kconfig->hasGroup(group)) {
64  return false;
65  }
66  KConfigGroup grp = kconfig->group(group);
67  m_sym = grp.readEntry("SymbolRegex");
68  m_date = grp.readEntry("DateRegex");
69  m_dateformat = grp.readEntry("DateFormatRegex", "%m %d %y");
70  m_price = grp.readEntry("PriceRegex");
71  m_url = grp.readEntry("URL");
72  m_skipStripping = grp.readEntry("SkipStripping", false);
73  m_isGHNSSource = false;
74  m_readOnly = false;
75  return true;
76  }
77 
78  bool write()
79  {
80  KConfig *kconfig = m_profile->kConfig();
81  if (!kconfig)
82  return false;
83  KConfigGroup grp = kconfig->group(QString("Online-Quote-Source-%1").arg(m_name));
84  grp.writeEntry("URL", m_url);
85  grp.writeEntry("PriceRegex", m_price);
86  grp.writeEntry("DateRegex", m_date);
87  grp.writeEntry("DateFormatRegex", m_dateformat);
88  grp.writeEntry("SymbolRegex", m_sym);
89  if (m_skipStripping) {
90  grp.writeEntry("SkipStripping", m_skipStripping);
91  } else {
92  grp.deleteEntry("SkipStripping");
93  }
94  kconfig->sync();
95  return true;
96  }
97 
98  bool remove()
99  {
100  KConfig *kconfig = m_profile->kConfig();
101  if (!kconfig)
102  return false;
103  kconfig->deleteGroup(QString("Online-Quote-Source-%1").arg(m_name));
104  kconfig->sync();
105  return true;
106  }
107 
108  QString ghnsReadFilePath()
109  {
110  return m_profile->hotNewStuffReadFilePath(m_name + QLatin1String(".txt"));
111  }
112 
113  QString ghnsWriteFilePath()
114  {
115  return m_profile->hotNewStuffWriteFilePath(m_name + QLatin1String(".txt"));
116  }
117 
118  // This is currently in skrooge format
119  bool readFromGHNSFile()
120  {
121  QFileInfo f(ghnsReadFilePath());
122  if (!f.exists())
123  f.setFile(ghnsWriteFilePath());
124  m_readOnly = !f.isWritable();
125 
126  QFile file(f.absoluteFilePath());
127  if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
128  return false;
129 
130  QTextStream in(&file);
131  while (!in.atEnd()) {
132  QString line = in.readLine();
133  int index = line.indexOf("=");
134  if (index == -1)
135  return false;
136  QString key = line.left(index);
137  QString value = line.mid(index+1);
138  if (key == "url")
139  m_url = value;
140  else if (key == "price") {
141  m_price = value;
142  m_price.replace("\\\\", "\\");
143  } else if (key == "date") {
144  m_date = value;
145  m_date.replace("\\\\", "\\");
146  } else if (key == "dateformat")
147  m_dateformat = value;
148  }
149 
150  m_skipStripping = true;
151  m_isGHNSSource = true;
152  return true;
153  }
154 
155  // This is currently in skrooge format
156  bool writeToGHNSFile()
157  {
158  QFile file(ghnsWriteFilePath());
159  if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
160  return false;
161 
162  QTextStream out(&file);
163  out << "date=" << m_date << "\n";
164  out << "dateformat=" << m_dateformat << "\n";
165  out << "mode=HTML\n";
166  out << "price=" << m_price << "\n";
167  out << "url=" << m_url << "\n";
168  return true;
169  }
170 
171  bool removeGHNSFile()
172  {
173  qDebug() << "delete" << ghnsWriteFilePath();
174  return true;
175  }
176 
177  QString m_name;
178  QString m_url;
179  QString m_sym;
180  QString m_price;
181  QString m_date;
182  QString m_dateformat;
183  bool m_skipStripping;
185  bool m_isGHNSSource;
186  bool m_storageChanged;
187  bool m_readOnly;
188 };
189 
191  : d(new Private)
192 {
193 }
194 
196  : d(new Private(other.d))
197 {
198 }
199 
201 {
202  swap(*this, other);
203  return *this;
204 }
205 
206 AlkOnlineQuoteSource::AlkOnlineQuoteSource(const QString &name, const QString &url,
207  const QString &sym, const QString &price,
208  const QString &date, const QString &dateformat,
209  bool skipStripping)
210  : d(new Private)
211 {
212  d->m_name = name;
213  d->m_url = url;
214  d->m_sym = sym;
215  d->m_price = price;
216  d->m_date = date;
219  d->m_isGHNSSource = false;
220 }
221 
223  : d(new Private)
224 {
226  *this = profile->defaultQuoteSources()[name];
227  } else {
228  d->m_profile = profile;
229  d->m_name = name;
230  read();
231  }
232 }
233 
235 {
236  delete d;
237 }
238 
240 {
241  return !isValid() && !d->m_url.isEmpty();
242 }
243 
245 {
246  return !d->m_name.isEmpty();
247 }
248 
250 {
251  return d->m_name;
252 }
253 
255 {
256  return d->m_url;
257 }
258 
260 {
261  return d->m_sym;
262 }
263 
265 {
266  return d->m_price;
267 }
268 
270 {
271  return d->m_date;
272 }
273 
275 {
276  return d->m_dateformat;
277 }
278 
280 {
281  return d->m_skipStripping;
282 }
283 
284 void AlkOnlineQuoteSource::setName(const QString &name)
285 {
286  d->m_name = name;
287 }
288 
289 void AlkOnlineQuoteSource::setUrl(const QString &url)
290 {
291  d->m_url = url;
292 }
293 
294 void AlkOnlineQuoteSource::setSym(const QString &symbol)
295 {
296  d->m_sym = symbol;
297 }
298 
299 void AlkOnlineQuoteSource::setPrice(const QString &price)
300 {
301  d->m_price = price;
302 }
303 
304 void AlkOnlineQuoteSource::setDate(const QString &date)
305 {
306  d->m_date = date;
307 }
308 
309 void AlkOnlineQuoteSource::setDateformat(const QString &dateformat)
310 {
312 }
313 
315 {
316  d->m_skipStripping = state;
317 }
318 
320 {
321  d->m_storageChanged = d->m_isGHNSSource != state;
322  d->m_isGHNSSource = state;
323 }
324 
326 {
327  return d->m_isGHNSSource;
328 }
329 
331 {
332  return d->m_readOnly;
333 }
334 
336 {
337  return d->ghnsWriteFilePath();
338 }
339 
341 {
342  d->m_profile = profile;
343  qDebug() << "using profile" << profile->name();
344 }
345 
347 {
348  return d->m_profile;
349 }
350 
352 {
353  bool result = false;
354  if (d->m_profile->hasGHNSSupport()) {
355  result = d->readFromGHNSFile();
356  if (result)
357  return true;
358  }
359  return d->read();
360 }
361 
363 {
364  bool result = false;
365  // check if type has been changedd->isGHNS
366  if (d->m_profile->hasGHNSSupport() && d->m_isGHNSSource) {
367  result = d->writeToGHNSFile();
368  if (d->m_storageChanged)
369  d->remove();
370  return result;
371  } else {
372  result = d->write();
374  d->removeGHNSFile();
375  }
376  }
377  d->m_storageChanged = false;
378  return result;
379 }
380 
381 void AlkOnlineQuoteSource::rename(const QString &name)
382 {
384  remove();
385  d->m_name = name;
386  write();
387  } else
388  d->m_name = name;
389 }
390 
392 {
393  if (d->m_profile->hasGHNSSupport() && d->m_isGHNSSource) {
394  d->removeGHNSFile();
396  d->remove();
397  }
398 }
AlkOnlineQuoteSource::profile
AlkOnlineQuotesProfile * profile()
Definition: alkonlinequotesource.cpp:346
AlkOnlineQuoteSource::Private
Definition: alkonlinequotesource.cpp:31
AlkOnlineQuotesProfile::kConfig
KConfig * kConfig() const
Definition: alkonlinequotesprofile.cpp:362
AlkOnlineQuoteSource
Definition: alkonlinequotesource.h:35
AlkOnlineQuoteSource::swap
friend void swap(AlkOnlineQuoteSource &first, AlkOnlineQuoteSource &second)
Definition: alkonlinequotesource.h:87
AlkOnlineQuoteSource::Private::m_dateformat
QString m_dateformat
Definition: alkonlinequotesource.cpp:199
AlkOnlineQuoteSource::Private::m_storageChanged
bool m_storageChanged
Definition: alkonlinequotesource.cpp:203
AlkOnlineQuoteSource::setName
void setName(const QString &name)
Definition: alkonlinequotesource.cpp:284
AlkOnlineQuoteSource::setSym
void setSym(const QString &symbol)
Definition: alkonlinequotesource.cpp:294
AlkOnlineQuoteSource::Private::m_price
QString m_price
Definition: alkonlinequotesource.cpp:197
AlkOnlineQuoteSource::operator=
AlkOnlineQuoteSource & operator=(AlkOnlineQuoteSource other)
Definition: alkonlinequotesource.cpp:200
AlkOnlineQuoteSource::setPrice
void setPrice(const QString &price)
Definition: alkonlinequotesource.cpp:299
AlkOnlineQuoteSource::rename
void rename(const QString &name)
Definition: alkonlinequotesource.cpp:381
AlkOnlineQuoteSource::setDate
void setDate(const QString &date)
Definition: alkonlinequotesource.cpp:304
AlkOnlineQuoteSource::isValid
bool isValid()
Definition: alkonlinequotesource.cpp:244
AlkOnlineQuoteSource::Private::m_name
QString m_name
Definition: alkonlinequotesource.cpp:194
AlkOnlineQuoteSource::Private::m_profile
AlkOnlineQuotesProfile * m_profile
Definition: alkonlinequotesource.cpp:201
AlkOnlineQuoteSource::Private::ghnsWriteFilePath
QString ghnsWriteFilePath()
Definition: alkonlinequotesource.cpp:130
AlkOnlineQuoteSource::Private::readFromGHNSFile
bool readFromGHNSFile()
Definition: alkonlinequotesource.cpp:136
AlkOnlineQuotesProfile
Definition: alkonlinequotesprofile.h:34
AlkOnlineQuoteSource::Private::m_isGHNSSource
bool m_isGHNSSource
Definition: alkonlinequotesource.cpp:202
AlkOnlineQuoteSource::dateformat
QString dateformat() const
Definition: alkonlinequotesource.cpp:274
AlkOnlineQuotesProfile::Type::None
@ None
AlkOnlineQuoteSource::setGHNS
void setGHNS(bool state)
Definition: alkonlinequotesource.cpp:319
AlkOnlineQuoteSource::Private::read
bool read()
Definition: alkonlinequotesource.cpp:74
AlkOnlineQuoteSource::ghnsWriteFileName
QString ghnsWriteFileName()
Definition: alkonlinequotesource.cpp:335
AlkOnlineQuoteSource::Private::write
bool write()
Definition: alkonlinequotesource.cpp:95
AlkOnlineQuoteSource::skipStripping
bool skipStripping() const
Definition: alkonlinequotesource.cpp:279
AlkOnlineQuoteSource::Private::m_skipStripping
bool m_skipStripping
Definition: alkonlinequotesource.cpp:200
AlkOnlineQuoteSource::date
QString date() const
Definition: alkonlinequotesource.cpp:269
AlkOnlineQuoteSource::isEmpty
bool isEmpty()
Definition: alkonlinequotesource.cpp:239
AlkOnlineQuotesProfile::defaultQuoteSources
const Map defaultQuoteSources()
Definition: alkonlinequotesprofile.cpp:377
AlkOnlineQuoteSource::remove
void remove()
Definition: alkonlinequotesource.cpp:391
AlkOnlineQuoteSource::write
bool write()
Definition: alkonlinequotesource.cpp:362
AlkOnlineQuoteSource::d
Private * d
Definition: alkonlinequotesource.h:83
alkonlinequotesprofile.h
AlkOnlineQuotesProfile::hasGHNSSupport
bool hasGHNSSupport()
Definition: alkonlinequotesprofile.cpp:372
AlkOnlineQuotesProfile::name
QString name() const
Definition: alkonlinequotesprofile.cpp:310
AlkOnlineQuoteSource::Private::Private
Private()
Definition: alkonlinequotesource.cpp:51
AlkOnlineQuoteSource::Private::m_sym
QString m_sym
Definition: alkonlinequotesource.cpp:196
AlkOnlineQuoteSource::Private::ghnsReadFilePath
QString ghnsReadFilePath()
Definition: alkonlinequotesource.cpp:125
AlkOnlineQuoteSource::isReadOnly
bool isReadOnly()
Definition: alkonlinequotesource.cpp:330
AlkOnlineQuoteSource::read
bool read()
Definition: alkonlinequotesource.cpp:351
AlkOnlineQuotesProfile::hotNewStuffWriteFilePath
QString hotNewStuffWriteFilePath(const QString &fileName) const
Definition: alkonlinequotesprofile.cpp:335
AlkOnlineQuoteSource::setDateformat
void setDateformat(const QString &dateformat)
Definition: alkonlinequotesource.cpp:309
AlkOnlineQuoteSource::Private::m_url
QString m_url
Definition: alkonlinequotesource.cpp:195
AlkOnlineQuoteSource::Private::m_date
QString m_date
Definition: alkonlinequotesource.cpp:198
AlkOnlineQuoteSource::name
QString name() const
Definition: alkonlinequotesource.cpp:249
AlkOnlineQuoteSource::setProfile
void setProfile(AlkOnlineQuotesProfile *profile)
Definition: alkonlinequotesource.cpp:340
AlkOnlineQuoteSource::~AlkOnlineQuoteSource
~AlkOnlineQuoteSource()
Definition: alkonlinequotesource.cpp:234
AlkOnlineQuotesProfile::type
Type type()
Definition: alkonlinequotesprofile.cpp:367
AlkOnlineQuoteSource::Private::remove
bool remove()
Definition: alkonlinequotesource.cpp:115
AlkOnlineQuoteSource::Private::m_readOnly
bool m_readOnly
Definition: alkonlinequotesource.cpp:204
AlkOnlineQuoteSource::sym
QString sym() const
Definition: alkonlinequotesource.cpp:259
AlkOnlineQuoteSource::url
QString url() const
Definition: alkonlinequotesource.cpp:254
AlkOnlineQuoteSource::AlkOnlineQuoteSource
AlkOnlineQuoteSource()
Definition: alkonlinequotesource.cpp:190
AlkOnlineQuoteSource::price
QString price() const
Definition: alkonlinequotesource.cpp:264
alkonlinequotesource.h
AlkOnlineQuotesProfile::hotNewStuffReadFilePath
QString hotNewStuffReadFilePath(const QString &fileName) const
Definition: alkonlinequotesprofile.cpp:325
AlkOnlineQuoteSource::Private::writeToGHNSFile
bool writeToGHNSFile()
Definition: alkonlinequotesource.cpp:173
AlkOnlineQuoteSource::isGHNS
bool isGHNS()
Definition: alkonlinequotesource.cpp:325
AlkOnlineQuoteSource::setUrl
void setUrl(const QString &url)
Definition: alkonlinequotesource.cpp:289
AlkOnlineQuoteSource::setSkipStripping
void setSkipStripping(bool state)
Definition: alkonlinequotesource.cpp:314
AlkOnlineQuoteSource::Private::removeGHNSFile
bool removeGHNSFile()
Definition: alkonlinequotesource.cpp:188