File indexing completed on 2024-04-14 14:19:57

0001 /*
0002     Copyright (c) 2009 John Layt <john@layt.net>
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Library General Public
0006     License as published by the Free Software Foundation; either
0007     version 2 of the License, or (at your option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012     Library General Public License for more details.
0013 
0014     You should have received a copy of the GNU Library General Public License
0015     along with this library; see the file COPYING.LIB.  If not, write to
0016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017     Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "kcurrencycode.h"
0021 
0022 #include <kconfig.h>
0023 #include <kconfiggroup.h>
0024 #include <klocalizedstring.h>
0025 
0026 #include <QDate>
0027 #include <QDir>
0028 #include <QFileInfo>
0029 #include <QSharedData>
0030 #include <QStandardPaths>
0031 #include <QStringList>
0032 
0033 class KCurrencyCodePrivate : public QSharedData
0034 {
0035 public:
0036     KCurrencyCodePrivate(const QString &isoCurrencyCode, const QString &language = QString());
0037     KCurrencyCodePrivate(const QFileInfo &currencyCodeFile, const QString &language = QString());
0038     KCurrencyCodePrivate(const KCurrencyCodePrivate &other);
0039     virtual ~KCurrencyCodePrivate();
0040 
0041     void loadCurrency(const QFileInfo &currencyCodeFile, const QString &language);
0042 
0043     QString     m_currencyCodeIsoAlpha3;
0044     QString     m_currencyCodeIsoNumeric3;
0045     QString     m_currencyNameIso;
0046     QString     m_currencyNameDisplay;
0047     QStringList m_currencyUnitSymbols;
0048     QString     m_currencyUnitSymbolDefault;
0049     QString     m_currencyUnitSymbolUnambiguous;
0050     QString     m_currencyUnitSingular;
0051     QString     m_currencyUnitPlural;
0052     QString     m_currencySubunitSymbol;
0053     QString     m_currencySubunitSingular;
0054     QString     m_currencySubunitPlural;
0055     QDate       m_currencyIntroducedDate;
0056     QDate       m_currencySuspendedDate;
0057     QDate       m_currencyWithdrawnDate;
0058     int         m_currencySubunits;
0059     int         m_currencySubunitsPerUnit;
0060     bool        m_currencySubunitsInCirculation;
0061     int         m_currencyDecimalPlacesDisplay;
0062     QStringList m_currencyCountriesInUse;
0063 };
0064 
0065 KCurrencyCodePrivate::KCurrencyCodePrivate(const QString &isoCurrencyCode, const QString &language)
0066 {
0067     QFileInfo file(QStandardPaths::locate(QStandardPaths::GenericDataLocation, QLatin1String("kf5/locale/currency/") + QString::fromLatin1("%1.desktop").arg(isoCurrencyCode.toLower())));
0068 
0069     loadCurrency(file, language);
0070 }
0071 
0072 KCurrencyCodePrivate::KCurrencyCodePrivate(const QFileInfo &currencyCodeFile, const QString &language)
0073 {
0074     loadCurrency(currencyCodeFile, language);
0075 }
0076 
0077 KCurrencyCodePrivate::KCurrencyCodePrivate(const KCurrencyCodePrivate &other)
0078     : QSharedData(other),
0079       m_currencyCodeIsoAlpha3(other.m_currencyCodeIsoAlpha3),
0080       m_currencyCodeIsoNumeric3(other.m_currencyCodeIsoNumeric3),
0081       m_currencyNameIso(other.m_currencyNameIso),
0082       m_currencyNameDisplay(other.m_currencyNameDisplay),
0083       m_currencyUnitSymbols(other.m_currencyUnitSymbols),
0084       m_currencyUnitSymbolDefault(other.m_currencyUnitSymbolDefault),
0085       m_currencyUnitSymbolUnambiguous(other.m_currencyUnitSymbolUnambiguous),
0086       m_currencyUnitSingular(other.m_currencyUnitSingular),
0087       m_currencyUnitPlural(other.m_currencyUnitPlural),
0088       m_currencySubunitSymbol(other.m_currencySubunitSymbol),
0089       m_currencySubunitSingular(other.m_currencySubunitSingular),
0090       m_currencySubunitPlural(other.m_currencySubunitPlural),
0091       m_currencyIntroducedDate(other.m_currencyIntroducedDate),
0092       m_currencySuspendedDate(other.m_currencySuspendedDate),
0093       m_currencyWithdrawnDate(other.m_currencyWithdrawnDate),
0094       m_currencySubunits(other.m_currencySubunits),
0095       m_currencySubunitsPerUnit(other.m_currencySubunitsPerUnit),
0096       m_currencySubunitsInCirculation(other.m_currencySubunitsInCirculation),
0097       m_currencyDecimalPlacesDisplay(other.m_currencyDecimalPlacesDisplay),
0098       m_currencyCountriesInUse(other.m_currencyCountriesInUse)
0099 {
0100 }
0101 
0102 KCurrencyCodePrivate::~KCurrencyCodePrivate()
0103 {
0104 }
0105 
0106 void KCurrencyCodePrivate::loadCurrency(const QFileInfo &currencyCodeFile, const QString &language)
0107 {
0108     KConfig cgFile(currencyCodeFile.absoluteFilePath());
0109 
0110     // If language is empty, means to stick with the global default, which is the default for any new KConfig
0111     if (!language.isEmpty()) {
0112         cgFile.setLocale(language);
0113     }
0114 
0115     KConfigGroup cg(&cgFile, "Currency Code");
0116 
0117     m_currencyCodeIsoAlpha3         = cg.readEntry("CurrencyCodeIsoAlpha3",         QString());
0118     m_currencyCodeIsoNumeric3       = cg.readEntry("CurrencyCodeIsoNumeric3",       QString());
0119     m_currencyNameIso               = cg.readEntry("CurrencyNameIso",               QString());
0120     m_currencyNameDisplay           = cg.readEntry("Name",                          QString());
0121     m_currencyUnitSymbols           = cg.readEntry("CurrencyUnitSymbols",           QStringList());
0122     m_currencyUnitSymbolDefault     = cg.readEntry("CurrencyUnitSymbolDefault",     QString());
0123     m_currencyUnitSymbolUnambiguous = cg.readEntry("CurrencyUnitSymbolUnambiguous", QString());
0124     m_currencyUnitSingular          = cg.readEntry("CurrencyUnitSingular",          QString());
0125     m_currencyUnitPlural            = cg.readEntry("CurrencyUnitPlural",            QString());
0126     m_currencySubunitSymbol         = cg.readEntry("CurrencySubunitSymbol",         QString());
0127     m_currencySubunitSingular       = cg.readEntry("CurrencySubunitSingular",       QString());
0128     m_currencySubunitPlural         = cg.readEntry("CurrencySubunitPlural",         QString());
0129     m_currencyIntroducedDate        = cg.readEntry("CurrencyIntroducedDate",        QDate());
0130     m_currencySuspendedDate         = cg.readEntry("CurrencySuspendedDate",         QDate());
0131     m_currencyWithdrawnDate         = cg.readEntry("CurrencyWithdrawnDate",         QDate());
0132     m_currencySubunits              = cg.readEntry("CurrencySubunits",              1);
0133     m_currencySubunitsInCirculation = cg.readEntry("CurrencySubunitsInCirculation", true);
0134     m_currencySubunitsPerUnit       = cg.readEntry("CurrencySubunitsPerUnit",       100);
0135     m_currencyDecimalPlacesDisplay  = cg.readEntry("CurrencyDecimalPlacesDisplay",  2);
0136     m_currencyCountriesInUse        = cg.readEntry("CurrencyCountriesInUse",        QStringList());
0137 }
0138 
0139 KCurrencyCode::KCurrencyCode(const QString &isoCurrencyCode, const QString &language)
0140     : d(new KCurrencyCodePrivate(isoCurrencyCode, language))
0141 {
0142 }
0143 
0144 KCurrencyCode::KCurrencyCode(const QFileInfo &currencyCodeFile, const QString &language)
0145     : d(new KCurrencyCodePrivate(currencyCodeFile, language))
0146 {
0147 }
0148 
0149 KCurrencyCode::KCurrencyCode(const KCurrencyCode &rhs)
0150     : d(rhs.d)
0151 {
0152 }
0153 
0154 KCurrencyCode &KCurrencyCode::operator=(const KCurrencyCode &rhs)
0155 {
0156     if (&rhs != this) {
0157         d = rhs.d;
0158     }
0159     return *this;
0160 }
0161 
0162 KCurrencyCode::~KCurrencyCode()
0163 {
0164 }
0165 
0166 QString KCurrencyCode::isoCurrencyCode() const
0167 {
0168     return d->m_currencyCodeIsoAlpha3;
0169 }
0170 
0171 QString KCurrencyCode::isoCurrencyCodeNumeric() const
0172 {
0173     return d->m_currencyCodeIsoNumeric3;
0174 }
0175 
0176 QString KCurrencyCode::name() const
0177 {
0178     return d->m_currencyNameDisplay;
0179 }
0180 
0181 QString KCurrencyCode::isoName() const
0182 {
0183     return d->m_currencyNameIso;
0184 }
0185 
0186 KCurrencyCode::CurrencyStatus KCurrencyCode::status() const
0187 {
0188     if (dateWithdrawn() != QDate()) {
0189         return ObsoleteCurrency;
0190     } else if (dateSuspended() != QDate()) {
0191         return SuspendedCurrency;
0192     } else {
0193         return ActiveCurrency;
0194     }
0195 }
0196 
0197 QDate KCurrencyCode::dateIntroduced() const
0198 {
0199     return d->m_currencyIntroducedDate;
0200 }
0201 
0202 QDate KCurrencyCode::dateSuspended() const
0203 {
0204     return d->m_currencySuspendedDate;
0205 }
0206 
0207 QDate KCurrencyCode::dateWithdrawn() const
0208 {
0209     return d->m_currencyWithdrawnDate;
0210 }
0211 
0212 QStringList KCurrencyCode::symbolList() const
0213 {
0214     return d->m_currencyUnitSymbols;
0215 }
0216 
0217 QString KCurrencyCode::defaultSymbol() const
0218 {
0219     return d->m_currencyUnitSymbolDefault;
0220 }
0221 
0222 QString KCurrencyCode::unambiguousSymbol() const
0223 {
0224     if (d->m_currencyUnitSymbolUnambiguous.isEmpty()) {
0225         return d->m_currencyUnitSymbolDefault;
0226     } else {
0227         return d->m_currencyUnitSymbolUnambiguous;
0228     }
0229 }
0230 
0231 bool KCurrencyCode::hasSubunits() const
0232 {
0233     if (d->m_currencySubunits > 0) {
0234         return true;
0235     } else {
0236         return false;
0237     }
0238 }
0239 
0240 bool KCurrencyCode::hasSubunitsInCirculation() const
0241 {
0242     return (hasSubunits() && d->m_currencySubunitsInCirculation);
0243 }
0244 
0245 QString KCurrencyCode::subunitSymbol() const
0246 {
0247     return d->m_currencySubunitSymbol;
0248 }
0249 
0250 int KCurrencyCode::subunitsPerUnit() const
0251 {
0252     return d->m_currencySubunitsPerUnit;
0253 }
0254 
0255 int KCurrencyCode::decimalPlaces() const
0256 {
0257     return d->m_currencyDecimalPlacesDisplay;
0258 }
0259 
0260 QStringList KCurrencyCode::countriesUsingCurrency() const
0261 {
0262     return d->m_currencyCountriesInUse;
0263 }
0264 
0265 bool KCurrencyCode::isValid() const
0266 {
0267     return !d->m_currencyCodeIsoAlpha3.isEmpty();
0268 }
0269 
0270 bool KCurrencyCode::isValid(const QString &isoCurrencyCode, CurrencyStatusFlags currencyStatusFlags)
0271 {
0272     KCurrencyCode test = KCurrencyCode(isoCurrencyCode);
0273     return test.isValid() && (currencyStatusFlags & test.status());
0274 }
0275 
0276 QStringList KCurrencyCode::allCurrencyCodesList(CurrencyStatusFlags currencyStatus)
0277 {
0278     QStringList currencyCodes;
0279 
0280     const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QLatin1String("kf5/locale/currency/"), QStandardPaths::LocateDirectory);
0281     Q_FOREACH (const QString &dir, dirs) {
0282         Q_FOREACH (const QString &path, QDir(dir).entryList(QStringList() << QLatin1String("*.desktop"))) {
0283             const QString code = path.mid(path.length() - 11, 3).toUpper();
0284             if (KCurrencyCode::isValid(code, currencyStatus)) {
0285                 currencyCodes.append(code);
0286             }
0287         }
0288     }
0289 
0290     return currencyCodes;
0291 }
0292 
0293 QString KCurrencyCode::currencyCodeToName(const QString &isoCurrencyCode, const QString &language)
0294 {
0295     KCurrencyCode temp = KCurrencyCode(isoCurrencyCode, language);
0296     if (temp.isValid()) {
0297         return temp.name();
0298     } else {
0299         return QString();
0300     }
0301 }