alkimia  8.0.3
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 
34 static const char *fqName = "Finance::Quote";
35 
37 {
38 public:
40  : m_skipStripping(false)
41  , m_profile(nullptr)
42  , m_isGHNSSource(false)
43  , m_storageChanged(false)
44  , m_readOnly(true)
45  {
46  }
47 
48  Private(const Private *other)
49  : m_name(other->m_name)
50  , m_url(other->m_url)
51  , m_sym(other->m_sym)
52  , m_price(other->m_price)
53  , m_date(other->m_date)
54  , m_dateformat(other->m_dateformat)
56  , m_profile(other->m_profile)
58  , m_readOnly(other->m_readOnly)
59  {
60  }
61 
62  bool read()
63  {
64  KConfig *kconfig = m_profile->kConfig();
65  if (!kconfig)
66  return false;
67  const QString &group = QString("Online-Quote-Source-%1").arg(m_name);
68  if (!kconfig->hasGroup(group)) {
69  return false;
70  }
71  KConfigGroup grp = kconfig->group(group);
72  m_sym = grp.readEntry("SymbolRegex");
73  m_date = grp.readEntry("DateRegex");
74  m_dateformat = grp.readEntry("DateFormatRegex", "%m %d %y");
75  m_price = grp.readEntry("PriceRegex");
76  m_url = grp.readEntry("URL");
77  m_skipStripping = grp.readEntry("SkipStripping", false);
78  m_isGHNSSource = false;
79  m_readOnly = false;
80  return true;
81  }
82 
83  bool write()
84  {
85  KConfig *kconfig = m_profile->kConfig();
86  if (!kconfig)
87  return false;
88  KConfigGroup grp = kconfig->group(QString("Online-Quote-Source-%1").arg(m_name));
89  grp.writeEntry("URL", m_url);
90  grp.writeEntry("PriceRegex", m_price);
91  grp.writeEntry("DateRegex", m_date);
92  grp.writeEntry("DateFormatRegex", m_dateformat);
93  grp.writeEntry("SymbolRegex", m_sym);
94  if (m_skipStripping) {
95  grp.writeEntry("SkipStripping", m_skipStripping);
96  } else {
97  grp.deleteEntry("SkipStripping");
98  }
99  kconfig->sync();
100  return true;
101  }
102 
103  bool remove()
104  {
105  KConfig *kconfig = m_profile->kConfig();
106  if (!kconfig)
107  return false;
108  kconfig->deleteGroup(QString("Online-Quote-Source-%1").arg(m_name));
109  kconfig->sync();
110  return true;
111  }
112 
114  {
115  return m_profile->hotNewStuffReadFilePath(m_name + QLatin1String(".txt"));
116  }
117 
119  {
120  return m_profile->hotNewStuffWriteFilePath(m_name + QLatin1String(".txt"));
121  }
122 
123  // This is currently in skrooge format
125  {
126  QFileInfo f(ghnsReadFilePath());
127  if (!f.exists())
128  f.setFile(ghnsWriteFilePath());
129  m_readOnly = !f.isWritable();
130 
131  QFile file(f.absoluteFilePath());
132  if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
133  return false;
134 
135  QTextStream in(&file);
136  while (!in.atEnd()) {
137  QString line = in.readLine();
138  int index = line.indexOf("=");
139  if (index == -1)
140  return false;
141  QString key = line.left(index);
142  QString value = line.mid(index+1);
143  if (key == "url")
144  m_url = value;
145  else if (key == "price") {
146  m_price = value;
147  m_price.replace("\\\\", "\\");
148  } else if (key == "date") {
149  m_date = value;
150  m_date.replace("\\\\", "\\");
151  } else if (key == "dateformat")
152  m_dateformat = value;
153  }
154 
155  m_skipStripping = true;
156  m_isGHNSSource = true;
157  return true;
158  }
159 
160  // This is currently in skrooge format
162  {
163  QFile file(ghnsWriteFilePath());
164  if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
165  return false;
166 
167  QTextStream out(&file);
168  out << "date=" << m_date << "\n";
169  out << "dateformat=" << m_dateformat << "\n";
170  out << "mode=HTML\n";
171  out << "price=" << m_price << "\n";
172  out << "url=" << m_url << "\n";
173  return true;
174  }
175 
177  {
178  qDebug() << "delete" << ghnsWriteFilePath();
179  return true;
180  }
181 
182  QString m_name;
183  QString m_url;
184  QString m_sym;
185  QString m_price;
186  QString m_date;
187  QString m_dateformat;
193 };
194 
196  : d(new Private)
197 {
198 }
199 
201  : d(new Private(other.d))
202 {
203 }
204 
206 {
207  swap(*this, other);
208  return *this;
209 }
210 
211 AlkOnlineQuoteSource::AlkOnlineQuoteSource(const QString &name, const QString &url,
212  const QString &sym, const QString &price,
213  const QString &date, const QString &dateformat,
214  bool skipStripping)
215  : d(new Private)
216 {
217  d->m_name = name;
218  d->m_url = url;
219  d->m_sym = sym;
220  d->m_price = price;
221  d->m_date = date;
224  d->m_isGHNSSource = false;
225 }
226 
228  : d(new Private)
229 {
231  *this = profile->defaultQuoteSources()[name];
232  } else {
233  d->m_profile = profile;
234  d->m_name = name;
235  read();
236  }
237 }
238 
240 {
241  delete d;
242 }
243 
245 {
246  return !isValid() && !d->m_url.isEmpty();
247 }
248 
250 {
251  return !d->m_name.isEmpty();
252 }
253 
255 {
256  return d->m_name;
257 }
258 
260 {
261  return d->m_url;
262 }
263 
265 {
266  return d->m_sym;
267 }
268 
270 {
271  return d->m_price;
272 }
273 
275 {
276  return d->m_date;
277 }
278 
280 {
281  return d->m_dateformat;
282 }
283 
292 {
293  return d->m_name.section(' ', 1);
294 }
295 
297 {
298  return d->m_skipStripping;
299 }
300 
301 void AlkOnlineQuoteSource::setName(const QString &name)
302 {
303  d->m_name = name;
304 }
305 
306 void AlkOnlineQuoteSource::setUrl(const QString &url)
307 {
308  d->m_url = url;
309 }
310 
311 void AlkOnlineQuoteSource::setSym(const QString &symbol)
312 {
313  d->m_sym = symbol;
314 }
315 
316 void AlkOnlineQuoteSource::setPrice(const QString &price)
317 {
318  d->m_price = price;
319 }
320 
321 void AlkOnlineQuoteSource::setDate(const QString &date)
322 {
323  d->m_date = date;
324 }
325 
326 void AlkOnlineQuoteSource::setDateformat(const QString &dateformat)
327 {
329 }
330 
332 {
333  d->m_skipStripping = state;
334 }
335 
337 {
338  d->m_storageChanged = d->m_isGHNSSource != state;
339  d->m_isGHNSSource = state;
340 }
341 
343 {
344  return d->m_isGHNSSource;
345 }
346 
348 {
349  return d->m_readOnly;
350 }
351 
358 {
359  return d->m_name.contains(fqName);
360 }
361 
367 bool AlkOnlineQuoteSource::isFinanceQuote(const QString &name)
368 {
369  return name.contains(fqName);
370 }
371 
373 {
374  return d->ghnsWriteFilePath();
375 }
376 
378 {
379  d->m_profile = profile;
380  qDebug() << "using profile" << profile->name();
381 }
382 
384 {
385  return d->m_profile;
386 }
387 
389 {
390  bool result = false;
391  if (d->m_profile->hasGHNSSupport()) {
392  result = d->readFromGHNSFile();
393  if (result)
394  return true;
395  }
396  return d->read();
397 }
398 
400 {
401  bool result = false;
402  // check if type has been changedd->isGHNS
403  if (d->m_profile->hasGHNSSupport() && d->m_isGHNSSource) {
404  result = d->writeToGHNSFile();
405  if (d->m_storageChanged)
406  d->remove();
407  return result;
408  } else {
409  result = d->write();
411  d->removeGHNSFile();
412  }
413  }
414  d->m_storageChanged = false;
415  return result;
416 }
417 
418 void AlkOnlineQuoteSource::rename(const QString &name)
419 {
421  remove();
422  d->m_name = name;
423  write();
424  } else
425  d->m_name = name;
426 }
427 
429 {
430  if (d->m_profile->hasGHNSSupport() && d->m_isGHNSSource) {
431  d->removeGHNSFile();
433  d->remove();
434  }
435 }
void setPrice(const QString &price)
QString hotNewStuffWriteFilePath(const QString &fileName) const
void setSkipStripping(bool state)
static const char * fqName
void setDate(const QString &date)
void setName(const QString &name)
void setSym(const QString &symbol)
QString hotNewStuffReadFilePath(const QString &fileName) const
AlkOnlineQuoteSource & operator=(AlkOnlineQuoteSource other)
void rename(const QString &name)
AlkOnlineQuotesProfile * m_profile
QString financeQuoteName() const
friend void swap(AlkOnlineQuoteSource &first, AlkOnlineQuoteSource &second)
void setDateformat(const QString &dateformat)
AlkOnlineQuotesProfile * profile()
void setUrl(const QString &url)
void setProfile(AlkOnlineQuotesProfile *profile)