KDELibs4Support

kcurrencycode.cpp
1 /*
2  Copyright (c) 2009 John Layt <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "kcurrencycode.h"
21 
22 #include <KConfigCore/kconfig.h>
23 #include <KConfigCore/kconfiggroup.h>
24 #include <KI18n/klocalizedstring.h>
25 
26 #include <QDate>
27 #include <QDir>
28 #include <QFileInfo>
29 #include <QSharedData>
30 #include <QStandardPaths>
31 #include <QStringList>
32 
33 class KCurrencyCodePrivate : public QSharedData
34 {
35 public:
36  KCurrencyCodePrivate(const QString &isoCurrencyCode, const QString &language = QString());
37  KCurrencyCodePrivate(const QFileInfo &currencyCodeFile, const QString &language = QString());
38  KCurrencyCodePrivate(const KCurrencyCodePrivate &other);
39  virtual ~KCurrencyCodePrivate();
40 
41  void loadCurrency(const QFileInfo &currencyCodeFile, const QString &language);
42 
43  QString m_currencyCodeIsoAlpha3;
44  QString m_currencyCodeIsoNumeric3;
45  QString m_currencyNameIso;
46  QString m_currencyNameDisplay;
47  QStringList m_currencyUnitSymbols;
48  QString m_currencyUnitSymbolDefault;
49  QString m_currencyUnitSymbolUnambiguous;
50  QString m_currencyUnitSingular;
51  QString m_currencyUnitPlural;
52  QString m_currencySubunitSymbol;
53  QString m_currencySubunitSingular;
54  QString m_currencySubunitPlural;
55  QDate m_currencyIntroducedDate;
56  QDate m_currencySuspendedDate;
57  QDate m_currencyWithdrawnDate;
58  int m_currencySubunits;
59  int m_currencySubunitsPerUnit;
60  bool m_currencySubunitsInCirculation;
61  int m_currencyDecimalPlacesDisplay;
62  QStringList m_currencyCountriesInUse;
63 };
64 
65 KCurrencyCodePrivate::KCurrencyCodePrivate(const QString &isoCurrencyCode, const QString &language)
66 {
67  QFileInfo file(QStandardPaths::locate(QStandardPaths::GenericDataLocation, QLatin1String("kf5/locale/currency/") + QString::fromLatin1("%1.desktop").arg(isoCurrencyCode.toLower())));
68 
69  loadCurrency(file, language);
70 }
71 
72 KCurrencyCodePrivate::KCurrencyCodePrivate(const QFileInfo &currencyCodeFile, const QString &language)
73 {
74  loadCurrency(currencyCodeFile, language);
75 }
76 
77 KCurrencyCodePrivate::KCurrencyCodePrivate(const KCurrencyCodePrivate &other)
78  : QSharedData(other),
79  m_currencyCodeIsoAlpha3(other.m_currencyCodeIsoAlpha3),
80  m_currencyCodeIsoNumeric3(other.m_currencyCodeIsoNumeric3),
81  m_currencyNameIso(other.m_currencyNameIso),
82  m_currencyNameDisplay(other.m_currencyNameDisplay),
83  m_currencyUnitSymbols(other.m_currencyUnitSymbols),
84  m_currencyUnitSymbolDefault(other.m_currencyUnitSymbolDefault),
85  m_currencyUnitSymbolUnambiguous(other.m_currencyUnitSymbolUnambiguous),
86  m_currencyUnitSingular(other.m_currencyUnitSingular),
87  m_currencyUnitPlural(other.m_currencyUnitPlural),
88  m_currencySubunitSymbol(other.m_currencySubunitSymbol),
89  m_currencySubunitSingular(other.m_currencySubunitSingular),
90  m_currencySubunitPlural(other.m_currencySubunitPlural),
91  m_currencyIntroducedDate(other.m_currencyIntroducedDate),
92  m_currencySuspendedDate(other.m_currencySuspendedDate),
93  m_currencyWithdrawnDate(other.m_currencyWithdrawnDate),
94  m_currencySubunits(other.m_currencySubunits),
95  m_currencySubunitsPerUnit(other.m_currencySubunitsPerUnit),
96  m_currencySubunitsInCirculation(other.m_currencySubunitsInCirculation),
97  m_currencyDecimalPlacesDisplay(other.m_currencyDecimalPlacesDisplay),
98  m_currencyCountriesInUse(other.m_currencyCountriesInUse)
99 {
100 }
101 
102 KCurrencyCodePrivate::~KCurrencyCodePrivate()
103 {
104 }
105 
106 void KCurrencyCodePrivate::loadCurrency(const QFileInfo &currencyCodeFile, const QString &language)
107 {
108  KConfig cgFile(currencyCodeFile.absoluteFilePath());
109 
110  // If language is empty, means to stick with the global default, which is the default for any new KConfig
111  if (!language.isEmpty()) {
112  cgFile.setLocale(language);
113  }
114 
115  KConfigGroup cg(&cgFile, "Currency Code");
116 
117  m_currencyCodeIsoAlpha3 = cg.readEntry("CurrencyCodeIsoAlpha3", QString());
118  m_currencyCodeIsoNumeric3 = cg.readEntry("CurrencyCodeIsoNumeric3", QString());
119  m_currencyNameIso = cg.readEntry("CurrencyNameIso", QString());
120  m_currencyNameDisplay = cg.readEntry("Name", QString());
121  m_currencyUnitSymbols = cg.readEntry("CurrencyUnitSymbols", QStringList());
122  m_currencyUnitSymbolDefault = cg.readEntry("CurrencyUnitSymbolDefault", QString());
123  m_currencyUnitSymbolUnambiguous = cg.readEntry("CurrencyUnitSymbolUnambiguous", QString());
124  m_currencyUnitSingular = cg.readEntry("CurrencyUnitSingular", QString());
125  m_currencyUnitPlural = cg.readEntry("CurrencyUnitPlural", QString());
126  m_currencySubunitSymbol = cg.readEntry("CurrencySubunitSymbol", QString());
127  m_currencySubunitSingular = cg.readEntry("CurrencySubunitSingular", QString());
128  m_currencySubunitPlural = cg.readEntry("CurrencySubunitPlural", QString());
129  m_currencyIntroducedDate = cg.readEntry("CurrencyIntroducedDate", QDate());
130  m_currencySuspendedDate = cg.readEntry("CurrencySuspendedDate", QDate());
131  m_currencyWithdrawnDate = cg.readEntry("CurrencyWithdrawnDate", QDate());
132  m_currencySubunits = cg.readEntry("CurrencySubunits", 1);
133  m_currencySubunitsInCirculation = cg.readEntry("CurrencySubunitsInCirculation", true);
134  m_currencySubunitsPerUnit = cg.readEntry("CurrencySubunitsPerUnit", 100);
135  m_currencyDecimalPlacesDisplay = cg.readEntry("CurrencyDecimalPlacesDisplay", 2);
136  m_currencyCountriesInUse = cg.readEntry("CurrencyCountriesInUse", QStringList());
137 }
138 
139 KCurrencyCode::KCurrencyCode(const QString &isoCurrencyCode, const QString &language)
140  : d(new KCurrencyCodePrivate(isoCurrencyCode, language))
141 {
142 }
143 
144 KCurrencyCode::KCurrencyCode(const QFileInfo &currencyCodeFile, const QString &language)
145  : d(new KCurrencyCodePrivate(currencyCodeFile, language))
146 {
147 }
148 
150  : d(rhs.d)
151 {
152 }
153 
155 {
156  if (&rhs != this) {
157  d = rhs.d;
158  }
159  return *this;
160 }
161 
163 {
164 }
165 
167 {
168  return d->m_currencyCodeIsoAlpha3;
169 }
170 
172 {
173  return d->m_currencyCodeIsoNumeric3;
174 }
175 
177 {
178  return d->m_currencyNameDisplay;
179 }
180 
182 {
183  return d->m_currencyNameIso;
184 }
185 
187 {
188  if (dateWithdrawn() != QDate()) {
189  return ObsoleteCurrency;
190  } else if (dateSuspended() != QDate()) {
191  return SuspendedCurrency;
192  } else {
193  return ActiveCurrency;
194  }
195 }
196 
198 {
199  return d->m_currencyIntroducedDate;
200 }
201 
203 {
204  return d->m_currencySuspendedDate;
205 }
206 
208 {
209  return d->m_currencyWithdrawnDate;
210 }
211 
213 {
214  return d->m_currencyUnitSymbols;
215 }
216 
218 {
219  return d->m_currencyUnitSymbolDefault;
220 }
221 
223 {
224  if (d->m_currencyUnitSymbolUnambiguous.isEmpty()) {
225  return d->m_currencyUnitSymbolDefault;
226  } else {
227  return d->m_currencyUnitSymbolUnambiguous;
228  }
229 }
230 
232 {
233  if (d->m_currencySubunits > 0) {
234  return true;
235  } else {
236  return false;
237  }
238 }
239 
241 {
242  return (hasSubunits() && d->m_currencySubunitsInCirculation);
243 }
244 
246 {
247  return d->m_currencySubunitSymbol;
248 }
249 
251 {
252  return d->m_currencySubunitsPerUnit;
253 }
254 
256 {
257  return d->m_currencyDecimalPlacesDisplay;
258 }
259 
261 {
262  return d->m_currencyCountriesInUse;
263 }
264 
266 {
267  return !d->m_currencyCodeIsoAlpha3.isEmpty();
268 }
269 
270 bool KCurrencyCode::isValid(const QString &isoCurrencyCode, CurrencyStatusFlags currencyStatusFlags)
271 {
272  KCurrencyCode test = KCurrencyCode(isoCurrencyCode);
273  return test.isValid() && (currencyStatusFlags & test.status());
274 }
275 
277 {
278  QStringList currencyCodes;
279 
281  Q_FOREACH (const QString &dir, dirs) {
282  Q_FOREACH (const QString &path, QDir(dir).entryList(QStringList() << QLatin1String("*.desktop"))) {
283  const QString code = path.mid(path.length() - 11, 3).toUpper();
284  if (KCurrencyCode::isValid(code, currencyStatus)) {
285  currencyCodes.append(code);
286  }
287  }
288  }
289 
290  return currencyCodes;
291 }
292 
293 QString KCurrencyCode::currencyCodeToName(const QString &isoCurrencyCode, const QString &language)
294 {
295  KCurrencyCode temp = KCurrencyCode(isoCurrencyCode, language);
296  if (temp.isValid()) {
297  return temp.name();
298  } else {
299  return QString();
300  }
301 }
virtual ~KCurrencyCode()
Destructor.
QString subunitSymbol() const
Return the Currency subunit symbol if it has one e.g.
CurrencyStatus status() const
Return Currency Status for the currency, if Active, Suspended or Obsolete.
QStringList locateAll(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options)
QDate dateIntroduced() const
Return the date the currency was introduced.
int subunitsPerUnit() const
Return the number of subunits in every unit, e.g.
static QString currencyCodeToName(const QString &currencyCode, const QString &language=QString())
Convert a known ISO Currency Code to a human readable, localized form.
bool hasSubunits() const
Return if the Currency has subunits or not, e.g.
QString unambiguousSymbol() const
Return the unambiguous Symbol for the Currency, e.g.
Currency is currently in use.
Definition: kcurrencycode.h:58
QDate dateWithdrawn() const
Return the date the currency was withdrawn from circulation.
QString isoName() const
Return untranslated official ISO Currency Code Name.
Currency is not currently in use but has not been replaced.
Definition: kcurrencycode.h:59
Currency is no longer in use and has been replaced.
Definition: kcurrencycode.h:60
void append(const T &value)
QDate dateSuspended() const
Return the date the currency was suspended.
bool setLocale(const QString &aLocale)
QString absoluteFilePath() const const
bool isEmpty() const const
bool hasSubunitsInCirculation() const
Return if the Currency has subunits in circulation, e.g.
KCurrencyCode & operator=(const KCurrencyCode &rhs)
Assignment operator.
QString toLower() const const
int decimalPlaces() const
Return the number of decimal places required to display the currency subunits.
QString isoCurrencyCodeNumeric() const
Return the ISO 4217 Currency Code in Numeric 3 format, e.g.
QString isoCurrencyCode() const
Return the ISO 4217 Currency Code in Alpha 3 format, e.g.
QString mid(int position, int n) const const
bool isValid() const
Return if the currency object loaded/initialised correctly.
CurrencyStatus
The Status of the Currency.
Definition: kcurrencycode.h:57
int length() const const
QStringList symbolList() const
Return a list of valid Symbols for the Currency in order of preference.
QString fromLatin1(const char *str, int size)
QString name() const
Return translated Currency Code Name in a standard display format e.g.
QStringList countriesUsingCurrency() const
Return a list of countries known to be using the currency.
QString defaultSymbol() const
Return the default Symbol for the Currency, e.g.
QString locate(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options)
static QStringList allCurrencyCodesList(CurrencyStatusFlags currencyStatus=CurrencyStatusFlags(ActiveCurrency| SuspendedCurrency| ObsoleteCurrency))
Provides list of all known ISO Currency Codes.
KCurrencyCode(const QString &isoCurrencyCode, const QString &language=QString())
Constructs a KCurrencyCode for a given ISO Currency Code.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Nov 30 2020 22:59:24 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.