KUnitConversion

currency.cpp
1/*
2 * SPDX-FileCopyrightText: 2009 Petri Damstén <damu@iki.fi>
3 * SPDX-FileCopyrightText: 2014 John Layt <jlayt@kde.org>
4 *
5 * SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
8#include "currency_p.h"
9#include "kunitconversion_debug.h"
10#include "unit_p.h"
11#include "unitcategory.h"
12#include <KLocalizedString>
13#include <QDateTime>
14#include <QDir>
15#include <QFileInfo>
16#include <QLocale>
17#include <QNetworkAccessManager>
18#include <QNetworkInterface>
19#include <QNetworkReply>
20#include <QNetworkRequest>
21#include <QSaveFile>
22#include <QStandardPaths>
23#include <QXmlStreamReader>
24
25using namespace std::chrono_literals;
26
27namespace KUnitConversion
28{
29static const char URL[] = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml";
30
31static QString cacheLocation()
32{
33#ifndef Q_OS_ANDROID
34 return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/libkunitconversion/currency.xml");
35#else
36 return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/libkunitconversion/currency.xml");
37#endif
38}
39
40class CurrencyCategoryPrivate : public UnitCategoryPrivate
41{
42public:
43 CurrencyCategoryPrivate(CategoryId id, const QString &name, const QString &description)
44 : UnitCategoryPrivate(id, name, description)
45 {
46 }
47
48 Value convert(const Value &value, const Unit &toUnit) override;
49 bool hasOnlineConversionTable() const override;
50 UpdateJob* syncConversionTable(std::chrono::seconds updateSkipSeconds) override;
51
52 QNetworkReply *m_currentReply = nullptr;
53 bool readConversionTable(const QString &cacheLocation);
54 bool m_initialized{false}; //!< indicates if units are prepared from currency table
55};
56
57bool CurrencyCategoryPrivate::hasOnlineConversionTable() const
58{
59 return true;
60}
61
62UnitCategory Currency::makeCategory()
63{
64 auto c = UnitCategoryPrivate::makeCategory(new CurrencyCategoryPrivate(CurrencyCategory, i18n("Currency"), i18n("From ECB")));
65 auto d = UnitCategoryPrivate::get(c);
66 KLocalizedString symbolString = ki18nc("%1 value, %2 unit symbol (currency)", "%1 %2");
67
68 // Static rates
69 d->addDefaultUnit(UnitPrivate::makeUnit(CurrencyCategory,
70 Eur,
71 1.0,
72 QStringLiteral("EUR"),
73 i18nc("currency name", "Euro"),
74 i18nc("EUR Euro - unit synonyms for matching user input", "euro;euros") + QStringLiteral(";EUR;") + QStringLiteral("€")
75 + QLatin1Char(';') + i18nc("currency name", "Euro"),
76 symbolString,
77 ki18nc("amount in units (real)", "%1 euros"),
78 ki18ncp("amount in units (integer)", "%1 euro", "%1 euros")));
79
80 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
81 Ats,
82 0.0726728,
83 QStringLiteral("ATS"),
84 i18nc("currency name", "Austrian Schilling"),
85 i18nc("ATS Austrian Schilling - unit synonyms for matching user input", "schilling;schillings") + QStringLiteral(";ATS;")
86 + QLocale::territoryToString(QLocale::Austria) + QLatin1Char(';') + i18nc("currency name", "Austrian Schilling"),
87 symbolString,
88 ki18nc("amount in units (real)", "%1 schillings"),
89 ki18ncp("amount in units (integer)", "%1 schilling", "%1 schillings")));
90
91 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
92 Bef,
93 0.0247894,
94 QStringLiteral("BEF"),
95 i18nc("currency name", "Belgian Franc"),
96 i18nc("BEF Belgian Franc - unit synonyms for matching user input", "franc;francs") + QStringLiteral(";BEF;")
97 + QLocale::territoryToString(QLocale::Belgium) + QLatin1Char(';') + i18nc("currency name", "Belgian Franc"),
98 symbolString,
99 ki18nc("amount in units (real)", "%1 Belgian francs"),
100 ki18ncp("amount in units (integer)", "%1 Belgian franc", "%1 Belgian francs")));
101
102 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
103 Nlg,
104 0.45378,
105 QStringLiteral("NLG"),
106 i18nc("currency name", "Netherlands Guilder"),
107 i18nc("NLG Netherlands Guilder - unit synonyms for matching user input", "guilder;guilders") + QStringLiteral(";NLG;")
108 + QLocale::territoryToString(QLocale::Netherlands) + QLatin1Char(';') + i18nc("currency name", "Netherlands Guilder"),
109 symbolString,
110 ki18nc("amount in units (real)", "%1 guilders"),
111 ki18ncp("amount in units (integer)", "%1 guilder", "%1 guilders")));
112
113 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
114 Fim,
115 0.168188,
116 QStringLiteral("FIM"),
117 i18nc("currency name", "Finnish Markka"),
118 i18nc("FIM Finnish Markka - unit synonyms for matching user input", "markka;markkas;markkaa") + QStringLiteral(";FIM;")
119 + QLocale::territoryToString(QLocale::Finland) + QLatin1Char(';') + i18nc("currency name", "Finnish Markka"),
120 symbolString,
121 ki18nc("amount in units (real)", "%1 markkas"),
122 ki18ncp("amount in units (integer)", "%1 markka", "%1 markkas"))); // Alternative = markkaa
123
124 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
125 Frf,
126 0.152449,
127 QStringLiteral("FRF"),
128 i18nc("currency name", "French Franc"),
129 i18nc("FRF French Franc - unit synonyms for matching user input", "franc;francs") + QStringLiteral(";FRF;")
130 + QLocale::territoryToString(QLocale::France) + QLatin1Char(';') + i18nc("currency name", "French Franc"),
131 symbolString,
132 ki18nc("amount in units (real)", "%1 French francs"),
133 ki18ncp("amount in units (integer)", "%1 French franc", "%1 French francs")));
134
135 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
136 Dem,
137 0.511292,
138 QStringLiteral("DEM"),
139 i18nc("currency name", "German Mark"),
140 i18nc("DEM German Mark - unit synonyms for matching user input", "mark;marks") + QStringLiteral(";DEM;")
141 + QLocale::territoryToString(QLocale::Germany) + QLatin1Char(';') + i18nc("currency name", "German Mark"),
142 symbolString,
143 ki18nc("amount in units (real)", "%1 marks"),
144 ki18ncp("amount in units (integer)", "%1 mark", "%1 marks")));
145
146 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
147 Iep,
148 1.26974,
149 QStringLiteral("IEP"),
150 i18nc("currency name", "Irish Pound"),
151 i18nc("IEP Irish Pound - unit synonyms for matching user input", "Irish pound;Irish pounds") + QStringLiteral(";IEP;")
152 + QLocale::territoryToString(QLocale::Ireland) + QLatin1Char(';') + i18nc("currency name", "Irish Pound"),
153 symbolString,
154 ki18nc("amount in units (real)", "%1 Irish pounds"),
155 ki18ncp("amount in units (integer)", "%1 Irish pound", "%1 Irish pounds")));
156
157 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
158 Itl,
159 0.000516457,
160 QStringLiteral("ITL"),
161 i18nc("currency name", "Italian Lira"),
162 i18nc("ITL Italian Lira - unit synonyms for matching user input", "lira;liras") + QStringLiteral(";ITL;")
163 + QLocale::territoryToString(QLocale::Italy) + QLatin1Char(';') + i18nc("currency name", "Italian Lira"),
164 symbolString,
165 ki18nc("amount in units (real)", "%1 Italian lira"),
166 ki18ncp("amount in units (integer)", "%1 Italian lira", "%1 Italian lira")));
167
168 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
169 Luf,
170 0.0247894,
171 QStringLiteral("LUF"),
172 i18nc("currency name", "Luxembourgish Franc"),
173 i18nc("LUF Luxembourgish Franc - unit synonyms for matching user input", "franc;francs") + QStringLiteral(";LUF;")
174 + QLocale::territoryToString(QLocale::Luxembourg) + QLatin1Char(';') + i18nc("currency name", "Luxembourgish Franc"),
175 symbolString,
176 ki18nc("amount in units (real)", "%1 Luxembourgish francs"),
177 ki18ncp("amount in units (integer)", "%1 Luxembourgish franc", "%1 Luxembourgish francs")));
178
179 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
180 Pte,
181 0.00498798,
182 QStringLiteral("PTE"),
183 i18nc("currency name", "Portuguese Escudo"),
184 i18nc("PTE Portuguese Escudo - unit synonyms for matching user input", "escudo;escudos") + QStringLiteral(";PTE;")
185 + QLocale::territoryToString(QLocale::Portugal) + QLatin1Char(';') + i18nc("currency name", "Portuguese Escudo"),
186 symbolString,
187 ki18nc("amount in units (real)", "%1 escudos"),
188 ki18ncp("amount in units (integer)", "%1 escudo", "%1 escudos")));
189
190 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
191 Esp,
192 0.00601012,
193 QStringLiteral("ESP"),
194 i18nc("currency name", "Spanish Peseta"),
195 i18nc("ESP Spanish Peseta - unit synonyms for matching user input", "peseta;pesetas") + QStringLiteral(";ESP;")
196 + QLocale::territoryToString(QLocale::Spain) + QLatin1Char(';') + i18nc("currency name", "Spanish Peseta"),
197 symbolString,
198 ki18nc("amount in units (real)", "%1 pesetas"),
199 ki18ncp("amount in units (integer)", "%1 peseta", "%1 pesetas")));
200
201 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
202 Grd,
203 0.0029347,
204 QStringLiteral("GRD"),
205 i18nc("currency name", "Greek Drachma"),
206 i18nc("GRD Greek Drachma - unit synonyms for matching user input", "drachma;drachmas") + QStringLiteral(";GRD;")
207 + QLocale::territoryToString(QLocale::Greece) + QLatin1Char(';') + i18nc("currency name", "Greek Drachma"),
208 symbolString,
209 ki18nc("amount in units (real)", "%1 drachmas"),
210 ki18ncp("amount in units (integer)", "%1 drachma", "%1 drachmas")));
211
212 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
213 Sit,
214 0.00417293,
215 QStringLiteral("SIT"),
216 i18nc("currency name", "Slovenian Tolar"),
217 i18nc("SIT Slovenian Tolar - unit synonyms for matching user input", "tolar;tolars;tolarjev") + QStringLiteral(";SIT;")
218 + QLocale::territoryToString(QLocale::Slovenia) + QLatin1Char(';') + i18nc("currency name", "Slovenian Tolar"),
219 symbolString,
220 ki18nc("amount in units (real)", "%1 tolars"),
221 ki18ncp("amount in units (integer)", "%1 tolar", "%1 tolars"))); // Alt: tolarjev/tolarja/tolarji
222
223 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
224 Cyp,
225 1.7086,
226 QStringLiteral("CYP"),
227 i18nc("currency name", "Cypriot Pound"),
228 i18nc("CYP Cypriot Pound - unit synonyms for matching user input", "Cypriot pound;Cypriot pounds") + QStringLiteral(";CYP;")
229 + QLocale::territoryToString(QLocale::Cyprus) + QLatin1Char(';') + i18nc("currency name", "Cypriot Pound"),
230 symbolString,
231 ki18nc("amount in units (real)", "%1 Cypriot pounds"),
232 ki18ncp("amount in units (integer)", "%1 Cypriot pound", "%1 Cypriot pounds")));
233
234 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
235 Mtl,
236 2.32937,
237 QStringLiteral("MTL"),
238 i18nc("currency name", "Maltese Lira"),
239 i18nc("MTL Maltese Lira - unit synonyms for matching user input", "Maltese lira") + QStringLiteral(";MTL;")
240 + QLocale::territoryToString(QLocale::Malta) + QLatin1Char(';') + i18nc("currency name", "Maltese Lira"),
241 symbolString,
242 ki18nc("amount in units (real)", "%1 Maltese lira"),
243 ki18ncp("amount in units (integer)", "%1 Maltese lira", "%1 Maltese lira")));
244
245 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
246 Skk,
247 0.0331939,
248 QStringLiteral("SKK"),
249 i18nc("currency name", "Slovak Koruna"),
250 i18nc("SKK Slovak Koruna - unit synonyms for matching user input", "koruna;korunas;koruny;korun") + QStringLiteral(";SKK;")
251 + QLocale::territoryToString(QLocale::Slovakia) + QLatin1Char(';') + i18nc("currency name", "Slovak Koruna"),
252 symbolString,
253 ki18nc("amount in units (real)", "%1 Slovak korunas"),
254 ki18ncp("amount in units (integer)", "%1 Slovak koruna", "%1 Slovak korunas"))); // Alt: koruny, korun
255
256 d->addDefaultUnit(UnitPrivate::makeUnit(CurrencyCategory,
257 Xpf,
258 0.00838,
259 QStringLiteral("XPF"),
260 i18nc("currency name", "CFP franc"),
261 i18nc("XPF CFP franc - unit synonyms for matching user input", "CFP;CFP franc;CFP francs;Pacific franc;Pacific francs") + QStringLiteral(";XPF;")
262 + QLocale::territoryToString(QLocale::FrenchPolynesia) + QLatin1Char(';') + i18nc("currency name", "CFP franc"),
263 symbolString,
264 ki18nc("amount in units (real)", "%1 CFP francs"),
265 ki18ncp("amount in units (integer)", "%1 CFP franc", "%1 CFP francs")));
266
267 // From ECB
268 d->addCommonUnit(UnitPrivate::makeUnit(CurrencyCategory,
269 Usd,
270 qSNaN(),
271 QStringLiteral("USD"),
272 i18nc("currency name", "United States Dollar"),
273 i18nc("USD United States Dollar - unit synonyms for matching user input", "dollar;dollars") + QStringLiteral(";USD;$;")
274 + QLocale::territoryToString(QLocale::UnitedStates) + QLatin1Char(';') + i18nc("currency name", "United States Dollar"),
275 symbolString,
276 ki18nc("amount in units (real)", "%1 United States dollars"),
277 ki18ncp("amount in units (integer)", "%1 United States dollar", "%1 United States dollars")));
278
279 d->addCommonUnit(UnitPrivate::makeUnit(CurrencyCategory,
280 Jpy,
281 qSNaN(),
282 QStringLiteral("JPY"),
283 i18nc("currency name", "Japanese Yen"),
284 i18nc("JPY Japanese Yen - unit synonyms for matching user input", "yen") + QStringLiteral(";JPY;") + QStringLiteral("¥")
285 + QLatin1Char(';') + QLocale::territoryToString(QLocale::Japan) + QLatin1Char(';') + i18nc("currency name", "Japanese Yen"),
286 symbolString,
287 ki18nc("amount in units (real)", "%1 yen"),
288 ki18ncp("amount in units (integer)", "%1 yen", "%1 yen")));
289
290 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
291 Bgn,
292 qSNaN(),
293 QStringLiteral("BGN"),
294 i18nc("currency name", "Bulgarian Lev"),
295 i18nc("BGN Bulgarian Lev - unit synonyms for matching user input", "lev;leva") + QStringLiteral(";BGN;")
296 + QLocale::territoryToString(QLocale::Bulgaria) + QLatin1Char(';') + i18nc("currency name", "Bulgarian Lev"),
297 symbolString,
298 ki18nc("amount in units (real)", "%1 leva"),
299 ki18ncp("amount in units (integer)", "%1 lev", "%1 leva")));
300
301 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
302 Czk,
303 qSNaN(),
304 QStringLiteral("CZK"),
305 i18nc("currency name", "Czech Koruna"),
306 i18nc("CZK Czech Koruna - unit synonyms for matching user input", "koruna;korunas") + QStringLiteral(";CZK;")
307 + QLocale::territoryToString(QLocale::CzechRepublic) + QLatin1Char(';') + i18nc("currency name", "Czech Koruna"),
308 symbolString,
309 ki18nc("amount in units (real)", "%1 Czech korunas"),
310 // Alt: koruny, korun
311 ki18ncp("amount in units (integer)", "%1 Czech koruna", "%1 Czech korunas")));
312
313 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
314 Dkk,
315 qSNaN(),
316 QStringLiteral("DKK"),
317 i18nc("currency name", "Danish Krone"),
318 i18nc("DKK Danish Krone - unit synonyms for matching user input", "Danish krone;Danish kroner") + QStringLiteral(";DKK;")
319 + QLocale::territoryToString(QLocale::Denmark) + QLatin1Char(';') + i18nc("currency name", "Danish Krone"),
320 symbolString,
321 ki18nc("amount in units (real)", "%1 Danish kroner"),
322 ki18ncp("amount in units (integer)", "%1 Danish krone", "%1 Danish kroner")));
323
324 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
325 Eek,
326 qSNaN(),
327 QStringLiteral("EEK"),
328 i18nc("currency name", "Estonian Kroon"),
329 i18nc("EEK Estonian Kroon - unit synonyms for matching user input", "kroon;kroons;krooni") + QStringLiteral(";EEK;")
330 + QLocale::territoryToString(QLocale::Estonia) + QLatin1Char(';') + i18nc("currency name", "Estonian Kroon"),
331 symbolString,
332 ki18nc("amount in units (real)", "%1 kroons"),
333 ki18ncp("amount in units (integer)", "%1 kroon", "%1 kroons"))); // Alt: krooni
334
335 d->addCommonUnit(UnitPrivate::makeUnit(CurrencyCategory,
336 Gbp,
337 qSNaN(),
338 QStringLiteral("GBP"),
339 i18nc("currency name", "British Pound"),
340 i18nc("GBP British Pound - unit synonyms for matching user input", "pound;pounds;pound sterling;pounds sterling")
341 + QStringLiteral(";GBP;") + QStringLiteral("£") + QLatin1Char(';') + QLocale::territoryToString(QLocale::UnitedKingdom)
342 + QLatin1Char(';') + i18nc("currency name", "British Pound"),
343 symbolString,
344 ki18nc("amount in units (real)", "%1 pounds sterling"),
345 ki18ncp("amount in units (integer)", "%1 pound sterling", "%1 pounds sterling")));
346
347 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
348 Huf,
349 qSNaN(),
350 QStringLiteral("HUF"),
351 i18nc("currency name", "Hungarian Forint"),
352 i18nc("HUF hungarian Forint - unit synonyms for matching user input", "forint") + QStringLiteral(";HUF;")
353 + QLocale::territoryToString(QLocale::Hungary) + QLatin1Char(';') + i18nc("currency name", "Hungarian Forint"),
354 symbolString,
355 ki18nc("amount in units (real)", "%1 forint"),
356 ki18ncp("amount in units (integer)", "%1 forint", "%1 forint")));
357
358 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
359 Ils,
360 qSNaN(),
361 QStringLiteral("ILS"),
362 i18nc("currency name", "Israeli New Shekel"),
363 i18nc("ILS Israeli New Shekel - unit synonyms for matching user input", "shekel;shekels;sheqel;sheqels;sheqalim")
364 + QStringLiteral(";ILS;") + QLocale::territoryToString(QLocale::Israel) + QLatin1Char(';')
365 + i18nc("currency name", "Israeli New Shekel"),
366 symbolString,
367 ki18nc("amount in units (real)", "%1 shekels"),
368 ki18ncp("amount in units (integer)", "%1 shekel", "%1 shekels")));
369
370 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
371 Ltl,
372 qSNaN(),
373 QStringLiteral("LTL"),
374 i18nc("currency name", "Lithuanian Litas"),
375 i18nc("LTL Lithuanian Litas - unit synonyms for matching user input", "litas;litai;litu") + QStringLiteral(";LTL;")
376 + QLocale::territoryToString(QLocale::Lithuania) + QLatin1Char(';') + i18nc("currency name", "Lithuanian Litas"),
377 symbolString,
378 ki18nc("amount in units (real)", "%1 litas"),
379 ki18ncp("amount in units (integer)", "%1 litas", "%1 litai"))); // Alt: litu
380
381 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
382 Lvl,
383 qSNaN(),
384 QStringLiteral("LVL"),
385 i18nc("currency name", "Latvian Lats"),
386 i18nc("LVL Latvian Lats - unit synonyms for matching user input", "lats;lati") + QStringLiteral(";LVL;")
387 + QLocale::territoryToString(QLocale::Latvia) + QLatin1Char(';') + i18nc("currency name", "Latvian Lats"),
388 symbolString,
389 ki18nc("amount in units (real)", "%1 lati"),
390 ki18ncp("amount in units (integer)", "%1 lats", "%1 lati")));
391
392 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
393 Pln,
394 qSNaN(),
395 QStringLiteral("PLN"),
396 i18nc("currency name", "Polish Zloty"),
397 i18nc("PLN Polish Zloty - unit synonyms for matching user input", "zloty;zlotys;zloties") + QStringLiteral(";PLN;")
398 + QLocale::territoryToString(QLocale::Poland) + QLatin1Char(';') + i18nc("currency name", "Polish Zloty"),
399 symbolString,
400 ki18nc("amount in units (real)", "%1 zlotys"),
401 ki18ncp("amount in units (integer)", "%1 zloty", "%1 zlotys"))); // Alt: zloty, zlote, zlotych
402
403 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
404 Ron,
405 qSNaN(),
406 QStringLiteral("RON"),
407 i18nc("currency name", "Romanian Leu"),
408 i18nc("RON Romanian Leu - unit synonyms for matching user input", "leu;lei") + QStringLiteral(";RON;")
409 + QLocale::territoryToString(QLocale::Romania) + QLatin1Char(';') + i18nc("currency name", "Romanian Leu"),
410 symbolString,
411 ki18nc("amount in units (real)", "%1 lei"),
412 ki18ncp("amount in units (integer)", "%1 leu", "%1 lei")));
413
414 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
415 Sek,
416 qSNaN(),
417 QStringLiteral("SEK"),
418 i18nc("currency name", "Swedish Krona"),
419 i18nc("SEK Swedish Krona - unit synonyms for matching user input", "krona;kronor") + QStringLiteral(";SEK;")
420 + QLocale::territoryToString(QLocale::Sweden) + QLatin1Char(';') + i18nc("currency name", "Swedish Krona"),
421 symbolString,
422 ki18nc("amount in units (real)", "%1 kronor"),
423 ki18ncp("amount in units (integer)", "%1 krona", "%1 kronor")));
424
425 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
426 Chf,
427 qSNaN(),
428 QStringLiteral("CHF"),
429 i18nc("currency name", "Swiss Franc"),
430 i18nc("CHF Swiss Franc - unit synonyms for matching user input", "franc;francs") + QStringLiteral(";CHF;")
431 + QLocale::territoryToString(QLocale::Switzerland) + QLatin1Char(';') + i18nc("currency name", "Swiss Franc"),
432 symbolString,
433 ki18nc("amount in units (real)", "%1 Swiss francs"),
434 ki18ncp("amount in units (integer)", "%1 Swiss franc", "%1 Swiss francs")));
435
436 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
437 Nok,
438 qSNaN(),
439 QStringLiteral("NOK"),
440 i18nc("currency name", "Norwegian Krone"),
441 i18nc("Norwegian Krone - unit synonyms for matching user input", "Norwegian krone;Norwegian kroner") + QStringLiteral(";NOK;")
442 + QLocale::territoryToString(QLocale::Norway) + QLatin1Char(';') + i18nc("currency name", "Norwegian Krone"),
443 symbolString,
444 ki18nc("amount in units (real)", "%1 Norwegian kroner"),
445 ki18ncp("amount in units (integer)", "%1 Norwegian krone", "%1 Norwegian kroner")));
446
447 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
448 Hrk,
449 1.0/7.53450,
450 QStringLiteral("HRK"),
451 i18nc("currency name", "Croatian Kuna"),
452 i18nc("HRK Croatian Kuna - unit synonyms for matching user input", "kuna;kune") + QStringLiteral(";HRK;")
453 + QLocale::territoryToString(QLocale::Croatia) + QLatin1Char(';') + i18nc("currency name", "Croatian Kuna"),
454 symbolString,
455 ki18nc("amount in units (real)", "%1 kune"),
456 ki18ncp("amount in units (integer)", "%1 kuna", "%1 kune")));
457
458 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
459 Rub,
460 qSNaN(),
461 QStringLiteral("RUB"),
462 i18nc("currency name", "Russian Ruble"),
463 i18nc("RUB Russian Ruble - unit synonyms for matching user input", "ruble;rubles;rouble;roubles") + QStringLiteral(";RUB;")
464 + QLocale::territoryToString(QLocale::Russia) + QLatin1Char(';') + i18nc("currency name", "Russian Ruble"),
465 symbolString,
466 ki18nc("amount in units (real)", "%1 rubles"),
467 ki18ncp("amount in units (integer)", "%1 ruble", "%1 rubles"))); // Alt: rouble/roubles
468
469 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
470 Try,
471 qSNaN(),
472 QStringLiteral("TRY"),
473 i18nc("currency name", "Turkish Lira"),
474 i18nc("TRY Turkish Lira - unit synonyms for matching user input", "lira") + QStringLiteral(";TRY;")
475 + QLocale::territoryToString(QLocale::Turkey) + QLatin1Char(';') + i18nc("currency name", "Turkish Lira"),
476 symbolString,
477 ki18nc("amount in units (real)", "%1 Turkish lira"),
478 ki18ncp("amount in units (integer)", "%1 Turkish lira", "%1 Turkish lira")));
479
480 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
481 Aud,
482 qSNaN(),
483 QStringLiteral("AUD"),
484 i18nc("currency name", "Australian Dollar"),
485 i18nc("AUD Australian Dollar - unit synonyms for matching user input", "Australian dollar;Australian dollars") + QStringLiteral(";AUD;")
486 + QLocale::territoryToString(QLocale::Australia) + QLatin1Char(';') + i18nc("currency name", "Australian Dollar"),
487 symbolString,
488 ki18nc("amount in units (real)", "%1 Australian dollars"),
489 ki18ncp("amount in units (integer)", "%1 Australian dollar", "%1 Australian dollars")));
490
491 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
492 Brl,
493 qSNaN(),
494 QStringLiteral("BRL"),
495 i18nc("currency name", "Brazilian Real"),
496 i18nc("BRL Brazilian Real - unit synonyms for matching user input", "real;reais") + QStringLiteral(";BRL;")
497 + QLocale::territoryToString(QLocale::Brazil) + QLatin1Char(';') + i18nc("currency name", "Brazilian Real"),
498 symbolString,
499 ki18nc("amount in units (real)", "%1 reais"),
500 ki18ncp("amount in units (integer)", "%1 real", "%1 reais")));
501
502 d->addCommonUnit(UnitPrivate::makeUnit(CurrencyCategory,
503 Cad,
504 qSNaN(),
505 QStringLiteral("CAD"),
506 i18nc("currency name", "Canadian Dollar"),
507 i18nc("Canadian Dollar - unit synonyms for matching user input", "Canadian dollar;Canadian dollars") + QStringLiteral(";CAD;")
508 + QLocale::territoryToString(QLocale::Canada) + QLatin1Char(';') + i18nc("currency name", "Canadian Dollar"),
509 symbolString,
510 ki18nc("amount in units (real)", "%1 Canadian dollars"),
511 ki18ncp("amount in units (integer)", "%1 Canadian dollar", "%1 Canadian dollars")));
512
513 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
514 Cny,
515 qSNaN(),
516 QStringLiteral("CNY"),
517 i18nc("currency name", "Chinese Yuan"),
518 i18nc("Chinese Yuan - unit synonyms for matching user input", "yuan") + QStringLiteral(";CNY;")
519 + QLocale::territoryToString(QLocale::China) + QLatin1Char(';') + i18nc("currency name", "Chinese Yuan"),
520 symbolString,
521 ki18nc("amount in units (real)", "%1 yuan"),
522 ki18ncp("amount in units (integer)", "%1 yuan", "%1 yuan")));
523
524 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
525 Hkd,
526 qSNaN(),
527 QStringLiteral("HKD"),
528 i18nc("currency name", "Hong Kong Dollar"),
529 i18nc("Hong Kong Dollar - unit synonyms for matching user input", "Hong Kong dollar;Hong Kong dollars") + QStringLiteral(";HKD;")
530 + QLocale::territoryToString(QLocale::HongKong) + QLatin1Char(';') + i18nc("currency name", "Hong Kong Dollar"),
531 symbolString,
532 ki18nc("amount in units (real)", "%1 Hong Kong dollars"),
533 ki18ncp("amount in units (integer)", "%1 Hong Kong dollar", "%1 Hong Kong dollars")));
534
535 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
536 Idr,
537 qSNaN(),
538 QStringLiteral("IDR"),
539 i18nc("currency name", "Indonesian Rupiah"),
540 i18nc("IDR Indonesian Rupiah - unit synonyms for matching user input", "rupiah;rupiahs") + QStringLiteral(";IDR;")
541 + QLocale::territoryToString(QLocale::Indonesia) + QLatin1Char(';') + i18nc("currency name", "Indonesian Rupiah"),
542 symbolString,
543 ki18nc("amount in units (real)", "%1 rupiahs"),
544 ki18ncp("amount in units (integer)", "%1 rupiah", "%1 rupiahs"))); // Alt: rupiah
545
546 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
547 Inr,
548 qSNaN(),
549 QStringLiteral("INR"),
550 i18nc("currency name", "Indian Rupee"),
551 i18nc("INR Indian Rupee - unit synonyms for matching user input", "rupee;rupees") + QStringLiteral(";INR;")
552 + QLocale::territoryToString(QLocale::India) + QLatin1Char(';') + i18nc("currency name", "Indian Rupee"),
553 symbolString,
554 ki18nc("amount in units (real)", "%1 rupees"),
555 ki18ncp("amount in units (integer)", "%1 rupee", "%1 rupees"))); // Alt: rupee
556
557 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
558 Krw,
559 qSNaN(),
560 QStringLiteral("KRW"),
561 i18nc("currency name", "Korean Won"),
562 i18nc("KRW Korean Won - unit synonyms for matching user input", "won") + QStringLiteral(";KRW;")
563 + QLocale::territoryToString(QLocale::SouthKorea) + QLatin1Char(';') + i18nc("currency name", "Korean Won"),
564 symbolString,
565 ki18nc("amount in units (real)", "%1 won"),
566 ki18ncp("amount in units (integer)", "%1 won", "%1 won")));
567
568 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
569 Mxn,
570 qSNaN(),
571 QStringLiteral("MXN"),
572 i18nc("currency name", "Mexican Peso"),
573 i18nc("MXN Mexican Peso - unit synonyms for matching user input", "Mexican peso;Mexican pesos") + QStringLiteral(";MXN;")
574 + QLocale::territoryToString(QLocale::Mexico) + QLatin1Char(';') + i18nc("currency name", "Mexican Peso"),
575 symbolString,
576 ki18nc("amount in units (real)", "%1 Mexican pesos"),
577 ki18ncp("amount in units (integer)", "%1 Mexican peso", "%1 Mexican pesos")));
578
579 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
580 Myr,
581 qSNaN(),
582 QStringLiteral("MYR"),
583 i18nc("currency name", "Malaysian Ringgit"),
584 i18nc("MYR Malaysian Ringgit - unit synonyms for matching user input", "ringgit;ringgits") + QStringLiteral(";MYR;")
585 + QLocale::territoryToString(QLocale::Malaysia) + QLatin1Char(';') + i18nc("currency name", "Malaysian Ringgit"),
586 symbolString,
587 ki18nc("amount in units (real)", "%1 ringgit"),
588 ki18ncp("amount in units (integer)", "%1 ringgit", "%1 ringgit"))); // Alt: ringgits
589
590 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
591 Nzd,
592 qSNaN(),
593 QStringLiteral("NZD"),
594 i18nc("currency name", "New Zealand Dollar"),
595 i18nc("NZD New Zealand Dollar - unit synonyms for matching user input", "New Zealand dollar;New Zealand dollars")
596 + QStringLiteral(";NZD;") + QLocale::territoryToString(QLocale::NewZealand) + QLatin1Char(';')
597 + i18nc("currency name", "New Zealand Dollar"),
598 symbolString,
599 ki18nc("amount in units (real)", "%1 New Zealand dollars"),
600 ki18ncp("amount in units (integer)", "%1 New Zealand dollar", "%1 New Zealand dollars")));
601
602 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
603 Php,
604 qSNaN(),
605 QStringLiteral("PHP"),
606 i18nc("currency name", "Philippine Peso"),
607 i18nc("PHP Philippine Peso - unit synonyms for matching user input", "Philippine peso;Philippine pesos") + QStringLiteral(";PHP;")
608 + QLocale::territoryToString(QLocale::Philippines) + QLatin1Char(';') + i18nc("currency name", "Philippine Peso"),
609 symbolString,
610 ki18nc("amount in units (real)", "%1 Philippine pesos"),
611 ki18ncp("amount in units (integer)", "%1 Philippine peso", "%1 Philippine pesos")));
612
613 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
614 Sgd,
615 qSNaN(),
616 QStringLiteral("SGD"),
617 i18nc("currency name", "Singapore Dollar"),
618 i18nc("SGD Singapore Dollar - unit synonyms for matching user input", "Singapore dollar;Singapore dollars") + QStringLiteral(";SGD;")
619 + QLocale::territoryToString(QLocale::Singapore) + QLatin1Char(';') + i18nc("currency name", "Singapore Dollar"),
620 symbolString,
621 ki18nc("amount in units (real)", "%1 Singapore dollars"),
622 ki18ncp("amount in units (integer)", "%1 Singapore dollar", "%1 Singapore dollars")));
623
624 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
625 Thb,
626 qSNaN(),
627 QStringLiteral("THB"),
628 i18nc("currency name", "Thai Baht"),
629 i18nc("THB Thai Baht - unit synonyms for matching user input", "baht") + QStringLiteral(";THB;")
630 + QLocale::territoryToString(QLocale::Thailand) + QLatin1Char(';') + i18nc("currency name", "Thai Baht"),
631 symbolString,
632 ki18nc("amount in units (real)", "%1 baht"),
633 ki18ncp("amount in units (integer)", "%1 baht", "%1 baht")));
634
635 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
636 Zar,
637 qSNaN(),
638 QStringLiteral("ZAR"),
639 i18nc("currency name", "South African Rand"),
640 i18nc("South African Rand - unit synonyms for matching user input", "rand") + QStringLiteral(";ZAR;")
641 + QLocale::territoryToString(QLocale::SouthAfrica) + QLatin1Char(';') + i18nc("currency name", "South African Rand"),
642 symbolString,
643 ki18nc("amount in units (real)", "%1 rand"),
644 ki18ncp("amount in units (integer)", "%1 rand", "%1 rand")));
645 d->addUnit(UnitPrivate::makeUnit(CurrencyCategory,
646 Isk,
647 qSNaN(),
648 QStringLiteral("ISK"),
649 i18nc("currency name", "Icelandic Krona"),
650 i18nc("Icelandic Krona - unit synonyms for matching user input", "Icelandic krona;Icelandic crown") + QStringLiteral(";ISK;")
651 + QLocale::territoryToString(QLocale::Iceland) + QLatin1Char(';') + i18nc("currency name", "Icelandic Krona"),
652 symbolString,
653 ki18nc("amount in units (real)", "%1 Icelandic kronar"),
654 ki18ncp("amount in units (integer)", "%1 Icelandic krona", "%1 Icelandic kronar")));
655
656 return c;
657}
658
659static bool isConnected()
660{
661 // don't download currencies in autotests
662 if (qEnvironmentVariableIsSet("KF5UNITCONVERT_NO_DOWNLOAD")) {
663 return false;
664 }
665 bool ret = false;
666 const auto listInterface = QNetworkInterface::allInterfaces();
667 for (const QNetworkInterface &net : listInterface) {
668 if (net.flags().testFlag(QNetworkInterface::IsUp) && !net.flags().testFlag(QNetworkInterface::IsLoopBack)) {
669 ret = true;
670 break;
671 }
672 }
673 return ret;
674}
675
676QDateTime Currency::lastConversionTableUpdate()
677{
678 QFileInfo info(cacheLocation());
679 if (!info.exists()) {
680 qCDebug(LOG_KUNITCONVERSION) << "No cache file available:" << cacheLocation();
681 return QDateTime();
682 } else {
683 return info.lastModified();
684 }
685}
686
687UpdateJob* CurrencyCategoryPrivate::syncConversionTable(std::chrono::seconds updateSkipPeriod)
688{
689 QFileInfo info(cacheLocation());
690 if (info.exists() && info.lastModified().secsTo(QDateTime::currentDateTime()) <= updateSkipPeriod.count()) {
691 return nullptr; // already present and up to date
692 }
693 if (!isConnected()) {
694 qCInfo(LOG_KUNITCONVERSION) << "currency conversion table update has no network connection, abort update";
695 return nullptr;
696 }
697 if (m_currentReply) {
698 return makeUpdateJob(m_currentReply);
699 }
700
701 qCDebug(LOG_KUNITCONVERSION) << "currency conversion table sync started";
702 m_currentReply = nam()->get(QNetworkRequest(QUrl(QString::fromLatin1(URL))));
703 QObject::connect(m_currentReply, &QNetworkReply::finished, [this] {
704 auto reply = m_currentReply;
705 m_currentReply = nullptr;
706 reply->deleteLater();
707 if (reply->error()) {
708 qCWarning(LOG_KUNITCONVERSION) << "currency conversion table network error" << reply->errorString();
709 return;
710 }
711 const auto cachePath = cacheLocation();
712 QFileInfo info(cachePath);
713 const QString cacheDir = info.absolutePath();
714 if (!QFileInfo::exists(cacheDir)) {
715 QDir().mkpath(cacheDir);
716 }
717
718 QSaveFile cacheFile(cachePath);
719 if (!cacheFile.open(QFile::WriteOnly)) {
720 qCCritical(LOG_KUNITCONVERSION) << cacheFile.errorString();
721 return;
722 }
723 cacheFile.write(reply->readAll());
724 if (!cacheFile.commit()) {
725 qCCritical(LOG_KUNITCONVERSION) << cacheFile.errorString();
726 return;
727 }
728 qCInfo(LOG_KUNITCONVERSION) << "currency conversion table data obtained via network";
729 m_initialized = readConversionTable(cachePath);
730 });
731
732 return makeUpdateJob(m_currentReply);
733}
734
735bool CurrencyCategoryPrivate::readConversionTable(const QString &cachePath)
736{
737 QFile file(cachePath);
738 if (!file.open(QIODevice::ReadOnly)) {
739 return false;
740 }
741 QXmlStreamReader xml(&file);
742 while (!xml.atEnd()) {
743 xml.readNext();
744
745 if (xml.isStartElement() && xml.name() == QLatin1String("Cube")) {
746 const auto attributes = xml.attributes();
747 if (attributes.hasAttribute(QLatin1String("currency"))) {
748 Unit unit = m_unitMap.value(attributes.value(QLatin1String("currency")).toString());
749 if (unit.isValid()) {
750 const auto multiplier = attributes.value(QLatin1String("rate")).toDouble();
751 if (!qFuzzyIsNull(multiplier)) {
752 unit.setUnitMultiplier(1.0 / multiplier);
753 qCDebug(LOG_KUNITCONVERSION()) << "currency updated:" << unit.description() << multiplier;
754 }
755 }
756 }
757 }
758 }
759
760 if (xml.hasError()) {
761 qCCritical(LOG_KUNITCONVERSION) << "currency conversion fetch could not parse obtained XML, update aborted";
762 return false;
763 }
764 return true;
765}
766
767Value CurrencyCategoryPrivate::convert(const Value &value, const Unit &to)
768{
769 if (!m_initialized) {
770 m_initialized = readConversionTable(cacheLocation());
771 }
772
773 Value v = UnitCategoryPrivate::convert(value, to);
774 return v;
775}
776
777} // KUnitConversion namespace
QString i18nc(const char *context, const char *text, const TYPE &arg...)
KLocalizedString KI18N_EXPORT ki18ncp(const char *context, const char *singular, const char *plural)
KLocalizedString KI18N_EXPORT ki18nc(const char *context, const char *text)
QString i18n(const char *text, const TYPE &arg...)
char * toString(const EngineQuery &query)
QString name(StandardAction id)
QDateTime currentDateTime()
bool mkpath(const QString &dirPath) const const
bool exists() const const
QString territoryToString(Territory territory)
QList< QNetworkInterface > allInterfaces()
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
QString writableLocation(StandardLocation type)
QString fromLatin1(QByteArrayView str)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:06:12 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.