KDECore
kcurrencycode.cpp
Go to the documentation of this file.
00001 /* 00002 Copyright (c) 2009 John Layt <john@layt.net> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "kcurrencycode.h" 00021 00022 #include <QtCore/QSharedData> 00023 #include <QtCore/QDate> 00024 #include <QtCore/QString> 00025 #include <QtCore/QStringList> 00026 #include <QtCore/QFileInfo> 00027 00028 #include "kconfig.h" 00029 #include "kconfiggroup.h" 00030 #include "kglobal.h" 00031 #include "klocale.h" 00032 #include "kstandarddirs.h" 00033 #include "kdebug.h" 00034 00035 class KCurrencyCodePrivate : public QSharedData 00036 { 00037 public: 00038 KCurrencyCodePrivate( const QString &isoCurrencyCode, const QString &language = QString() ); 00039 KCurrencyCodePrivate( const QFileInfo ¤cyCodeFile, const QString &language = QString() ); 00040 KCurrencyCodePrivate( const KCurrencyCodePrivate& other ); 00041 virtual ~KCurrencyCodePrivate(); 00042 00043 void loadCurrency( const QFileInfo ¤cyCodeFile, const QString &language ); 00044 00045 QString m_currencyCodeIsoAlpha3; 00046 QString m_currencyCodeIsoNumeric3; 00047 QString m_currencyNameIso; 00048 QString m_currencyNameDisplay; 00049 QStringList m_currencyUnitSymbols; 00050 QString m_currencyUnitSymbolDefault; 00051 QString m_currencyUnitSymbolUnambiguous; 00052 QString m_currencyUnitSingular; 00053 QString m_currencyUnitPlural; 00054 QString m_currencySubunitSymbol; 00055 QString m_currencySubunitSingular; 00056 QString m_currencySubunitPlural; 00057 QDate m_currencyIntroducedDate; 00058 QDate m_currencySuspendedDate; 00059 QDate m_currencyWithdrawnDate; 00060 int m_currencySubunits; 00061 int m_currencySubunitsPerUnit; 00062 bool m_currencySubunitsInCirculation; 00063 int m_currencyDecimalPlacesDisplay; 00064 QStringList m_currencyCountriesInUse; 00065 }; 00066 00067 KCurrencyCodePrivate::KCurrencyCodePrivate( const QString &isoCurrencyCode, const QString &language ) 00068 { 00069 QFileInfo file( KStandardDirs::locate( "locale", QString::fromLatin1( "currency/%1.desktop" ).arg( isoCurrencyCode.toLower() ) ) ); 00070 00071 loadCurrency( file, language ); 00072 } 00073 00074 KCurrencyCodePrivate::KCurrencyCodePrivate( const QFileInfo ¤cyCodeFile, const QString &language ) 00075 { 00076 loadCurrency( currencyCodeFile, language ); 00077 } 00078 00079 KCurrencyCodePrivate::KCurrencyCodePrivate( const KCurrencyCodePrivate& other ) 00080 : QSharedData( other ), 00081 m_currencyCodeIsoAlpha3( other.m_currencyCodeIsoAlpha3 ), 00082 m_currencyCodeIsoNumeric3( other.m_currencyCodeIsoNumeric3 ), 00083 m_currencyNameIso( other.m_currencyNameIso ), 00084 m_currencyNameDisplay( other.m_currencyNameDisplay ), 00085 m_currencyUnitSymbols( other.m_currencyUnitSymbols ), 00086 m_currencyUnitSymbolDefault( other.m_currencyUnitSymbolDefault ), 00087 m_currencyUnitSymbolUnambiguous( other.m_currencyUnitSymbolUnambiguous ), 00088 m_currencyUnitSingular( other.m_currencyUnitSingular ), 00089 m_currencyUnitPlural( other.m_currencyUnitPlural ), 00090 m_currencySubunitSymbol( other.m_currencySubunitSymbol ), 00091 m_currencySubunitSingular( other.m_currencySubunitSingular ), 00092 m_currencySubunitPlural( other.m_currencySubunitPlural ), 00093 m_currencyIntroducedDate( other.m_currencyIntroducedDate ), 00094 m_currencySuspendedDate( other.m_currencySuspendedDate ), 00095 m_currencyWithdrawnDate( other.m_currencyWithdrawnDate ), 00096 m_currencySubunits( other.m_currencySubunits ), 00097 m_currencySubunitsPerUnit( other.m_currencySubunitsPerUnit ), 00098 m_currencySubunitsInCirculation( other.m_currencySubunitsInCirculation ), 00099 m_currencyDecimalPlacesDisplay( other.m_currencyDecimalPlacesDisplay ), 00100 m_currencyCountriesInUse( other.m_currencyCountriesInUse ) 00101 { 00102 } 00103 00104 KCurrencyCodePrivate::~KCurrencyCodePrivate() 00105 { 00106 } 00107 00108 void KCurrencyCodePrivate::loadCurrency( const QFileInfo ¤cyCodeFile, const QString &language ) 00109 { 00110 KConfig cgFile( currencyCodeFile.absoluteFilePath() ); 00111 00112 // If language is empty, means to stick with the global default, which is the default for any new KConfig 00113 if ( !language.isEmpty() ) { 00114 cgFile.setLocale( language ); 00115 } 00116 00117 KConfigGroup cg( &cgFile, "Currency Code" ); 00118 00119 m_currencyCodeIsoAlpha3 = cg.readEntry( "CurrencyCodeIsoAlpha3", QString() ); 00120 m_currencyCodeIsoNumeric3 = cg.readEntry( "CurrencyCodeIsoNumeric3", QString() ); 00121 m_currencyNameIso = cg.readEntry( "CurrencyNameIso", QString() ); 00122 m_currencyNameDisplay = cg.readEntry( "Name", QString() ); 00123 m_currencyUnitSymbols = cg.readEntry( "CurrencyUnitSymbols", QStringList() ); 00124 m_currencyUnitSymbolDefault = cg.readEntry( "CurrencyUnitSymbolDefault", QString() ); 00125 m_currencyUnitSymbolUnambiguous = cg.readEntry( "CurrencyUnitSymbolUnambiguous", QString() ); 00126 m_currencyUnitSingular = cg.readEntry( "CurrencyUnitSingular", QString() ); 00127 m_currencyUnitPlural = cg.readEntry( "CurrencyUnitPlural", QString() ); 00128 m_currencySubunitSymbol = cg.readEntry( "CurrencySubunitSymbol", QString() ); 00129 m_currencySubunitSingular = cg.readEntry( "CurrencySubunitSingular", QString() ); 00130 m_currencySubunitPlural = cg.readEntry( "CurrencySubunitPlural", QString() ); 00131 m_currencyIntroducedDate = cg.readEntry( "CurrencyIntroducedDate", QDate() ); 00132 m_currencySuspendedDate = cg.readEntry( "CurrencySuspendedDate", QDate() ); 00133 m_currencyWithdrawnDate = cg.readEntry( "CurrencyWithdrawnDate", QDate() ); 00134 m_currencySubunits = cg.readEntry( "CurrencySubunits", 1 ); 00135 m_currencySubunitsInCirculation = cg.readEntry( "CurrencySubunitsInCirculation", true ); 00136 m_currencySubunitsPerUnit = cg.readEntry( "CurrencySubunitsPerUnit", 100 ); 00137 m_currencyDecimalPlacesDisplay = cg.readEntry( "CurrencyDecimalPlacesDisplay", 2 ); 00138 m_currencyCountriesInUse = cg.readEntry( "CurrencyCountriesInUse", QStringList() ); 00139 } 00140 00141 KCurrencyCode::KCurrencyCode( const QString &isoCurrencyCode, const QString &language ) 00142 :d( new KCurrencyCodePrivate( isoCurrencyCode, language ) ) 00143 { 00144 } 00145 00146 KCurrencyCode::KCurrencyCode( const QFileInfo ¤cyCodeFile, const QString &language ) 00147 :d( new KCurrencyCodePrivate( currencyCodeFile, language ) ) 00148 { 00149 } 00150 00151 KCurrencyCode::KCurrencyCode( const KCurrencyCode &rhs ) 00152 :d( rhs.d ) 00153 { 00154 } 00155 00156 KCurrencyCode& KCurrencyCode::operator=( const KCurrencyCode &rhs ) 00157 { 00158 if (&rhs != this) 00159 d = rhs.d; 00160 return *this; 00161 } 00162 00163 KCurrencyCode::~KCurrencyCode() 00164 { 00165 } 00166 00167 QString KCurrencyCode::isoCurrencyCode() const 00168 { 00169 return d->m_currencyCodeIsoAlpha3; 00170 } 00171 00172 QString KCurrencyCode::isoCurrencyCodeNumeric() const 00173 { 00174 return d->m_currencyCodeIsoNumeric3; 00175 } 00176 00177 QString KCurrencyCode::name() const 00178 { 00179 return d->m_currencyNameDisplay; 00180 } 00181 00182 QString KCurrencyCode::isoName() const 00183 { 00184 return d->m_currencyNameIso; 00185 } 00186 00187 KCurrencyCode::CurrencyStatus KCurrencyCode::status() const 00188 { 00189 if ( dateWithdrawn() != QDate() ) { 00190 return ObsoleteCurrency; 00191 } else if ( dateSuspended() != QDate() ) { 00192 return SuspendedCurrency; 00193 } else { 00194 return ActiveCurrency; 00195 } 00196 } 00197 00198 QDate KCurrencyCode::dateIntroduced() const 00199 { 00200 return d->m_currencyIntroducedDate; 00201 } 00202 00203 QDate KCurrencyCode::dateSuspended() const 00204 { 00205 return d->m_currencySuspendedDate; 00206 } 00207 00208 QDate KCurrencyCode::dateWithdrawn() const 00209 { 00210 return d->m_currencyWithdrawnDate; 00211 } 00212 00213 QStringList KCurrencyCode::symbolList() const 00214 { 00215 return d->m_currencyUnitSymbols; 00216 } 00217 00218 QString KCurrencyCode::defaultSymbol() const 00219 { 00220 return d->m_currencyUnitSymbolDefault; 00221 } 00222 00223 QString KCurrencyCode::unambiguousSymbol() const 00224 { 00225 if ( d->m_currencyUnitSymbolUnambiguous.isEmpty() ) { 00226 return d->m_currencyUnitSymbolDefault; 00227 } else { 00228 return d->m_currencyUnitSymbolUnambiguous; 00229 } 00230 } 00231 00232 bool KCurrencyCode::hasSubunits() const 00233 { 00234 if ( d->m_currencySubunits > 0 ) { 00235 return true; 00236 } else { 00237 return false; 00238 } 00239 } 00240 00241 bool KCurrencyCode::hasSubunitsInCirculation() const 00242 { 00243 return ( hasSubunits() && d->m_currencySubunitsInCirculation ); 00244 } 00245 00246 QString KCurrencyCode::subunitSymbol() const 00247 { 00248 return d->m_currencySubunitSymbol; 00249 } 00250 00251 int KCurrencyCode::subunitsPerUnit() const 00252 { 00253 return d->m_currencySubunitsPerUnit; 00254 } 00255 00256 int KCurrencyCode::decimalPlaces() const 00257 { 00258 return d->m_currencyDecimalPlacesDisplay; 00259 } 00260 00261 QStringList KCurrencyCode::countriesUsingCurrency() const 00262 { 00263 return d->m_currencyCountriesInUse; 00264 } 00265 00266 bool KCurrencyCode::isValid() const 00267 { 00268 return !d->m_currencyCodeIsoAlpha3.isEmpty(); 00269 } 00270 00271 bool KCurrencyCode::isValid( const QString &isoCurrencyCode, CurrencyStatusFlags currencyStatusFlags ) 00272 { 00273 KCurrencyCode test = KCurrencyCode( isoCurrencyCode ); 00274 return test.isValid() && ( currencyStatusFlags & test.status() ); 00275 } 00276 00277 QStringList KCurrencyCode::allCurrencyCodesList( KCurrencyCode::CurrencyStatusFlags currencyStatus ) 00278 { 00279 QStringList currencyCodes; 00280 00281 const QStringList paths = KGlobal::dirs()->findAllResources( "locale", QLatin1String("currency/*.desktop") ); 00282 00283 foreach( const QString &path, paths ) 00284 { 00285 QString code = path.mid( path.length()-11, 3 ).toUpper(); 00286 00287 if ( KCurrencyCode::isValid( code, currencyStatus ) ) { 00288 currencyCodes.append( code ); 00289 } 00290 } 00291 00292 return currencyCodes; 00293 } 00294 00295 QString KCurrencyCode::currencyCodeToName( const QString &isoCurrencyCode, const QString &language ) 00296 { 00297 KCurrencyCode temp = KCurrencyCode( isoCurrencyCode, language ); 00298 if ( temp.isValid() ) { 00299 return temp.name(); 00300 } else { 00301 return QString(); 00302 } 00303 }
KDE 4.6 API Reference