25 #include "holidayregion.h"
27 #include <QtCore/QDateTime>
28 #include <QtCore/QFile>
29 #include <QtCore/QSharedData>
30 #include <QtCore/QFileInfo>
32 #include <KStandardDirs>
35 #include <KLocalizedString>
38 #include "holiday_p.h"
39 #include "parsers/plan2/holidayparserdriverplan_p.h"
41 using namespace KHolidays;
43 class HolidayRegion::Private
46 Private(
const QString ®ionCode ) : mDriver( 0 ),
47 mRegionCode( regionCode )
49 if ( !mRegionCode.isEmpty() ) {
51 if ( mRegionCode.length() == 2 ) {
52 mLocation = mRegionCode;
53 QStringList locationFiles = KGlobal::dirs()->findAllResources(
"data",
55 KStandardDirs::NoDuplicates );
56 if ( locationFiles.
count() > 0 ) {
57 mRegionCode = locationFiles.
at( 0 ).
58 mid( locationFiles.
at( 0 ).lastIndexOf(
QLatin1String(
"holiday_") ) + 8 );
63 KStandardDirs::locate(
"data",
QLatin1String(
"libkholidays/plan2/holiday_") + mRegionCode ) );
69 Private(
const QFileInfo ®ionFile ) : mDriver( 0 ),
70 mHolidayFile( regionFile )
82 if ( mHolidayFile.exists() ) {
86 if ( mLocation.isEmpty() ) {
90 if ( mRegionCode.isEmpty() ) {
91 if ( mHolidayFile.fileName().startsWith(
QLatin1String(
"holiday_" ) ) ) {
92 mRegionCode = mHolidayFile.fileName().
mid( 8 );
94 mRegionCode = mHolidayFile.fileName();
114 HolidayRegion::HolidayRegion(
const QString ®ionCode )
115 : d( new Private( regionCode ) )
119 HolidayRegion::HolidayRegion(
const QFileInfo ®ionFile )
120 : d( new Private( regionFile ) )
124 HolidayRegion::~HolidayRegion()
132 KGlobal::dirs()->findAllResources(
"data",
QLatin1String(
"libkholidays/plan2/holiday_*"),
133 KStandardDirs::NoDuplicates );
136 foreach (
const QString &filename, files ) {
145 QString HolidayRegion::location()
const
153 KGlobal::dirs()->findAllResources(
"data",
QLatin1String(
"libkholidays/plan2/holiday_*"),
154 KStandardDirs::NoDuplicates );
157 foreach (
const QString &filename, files ) {
161 qSort( regionCodesList );
162 return regionCodesList;
165 QString HolidayRegion::regionCode()
const
167 return d->mRegionCode;
170 QString HolidayRegion::countryCode()
const
172 return d->mDriver->fileCountryCode();
177 HolidayRegion temp = HolidayRegion( regionCode );
178 if ( temp.isValid() ) {
179 return temp.countryCode();
185 QString HolidayRegion::languageCode()
const
187 return d->mDriver->fileLanguageCode();
192 HolidayRegion temp = HolidayRegion( regionCode );
193 if ( temp.isValid() ) {
194 return temp.languageCode();
200 QString HolidayRegion::name()
const
202 QString tempName = d->mDriver->fileName();
210 if ( countryParts.
count() == 2 ) {
213 QString subdivision = countryParts.
at( 1 );
215 regionName = i18nc(
"Canadian region",
"Quebec" );
217 regionName = i18nc(
"German region",
"Bavaria" );
219 regionName = i18nc(
"Spanish region",
"Catalonia" );
221 regionName = i18nc(
"UK Region",
"England and Wales" );
223 regionName = i18nc(
"UK Region",
"England" );
225 regionName = i18nc(
"UK Region",
"Wales" );
227 regionName = i18nc(
"UK Region",
"Scotland" );
229 regionName = i18nc(
"UK Region",
"Northern Ireland" );
231 regionName = i18nc(
"Italian Region",
"South Tyrol" );
233 regionName = i18nc(
"Australian Region",
"New South Wales" );
235 regionName = i18nc(
"Australian Region",
"Queensland" );
237 regionName = i18nc(
"Australian Region",
"Victoria" );
239 regionName = i18nc(
"Australian Region",
"South Australia" );
241 regionName = i18nc(
"Australian Region",
"Northern Territory" );
243 regionName = i18nc(
"Australian Region",
"Australian Capital Territory" );
245 regionName = i18nc(
"Australian Region",
"Western Australia" );
247 regionName = i18nc(
"Australian Region",
"Tasmania" );
249 regionName = i18nc(
"Bosnian and Herzegovinian Region",
"Republic of Srpska" );
251 regionName = KGlobal::locale()->countryCodeToName( country );
254 regionName = KGlobal::locale()->countryCodeToName( country );
260 if ( regionParts.
count() == 3 ) {
265 typeName = i18nc(
"Holiday type",
"Public" );
267 typeName = i18nc(
"Holiday type",
"Civil" );
269 typeName = i18nc(
"Holiday type",
"Religious" );
271 typeName = i18nc(
"Holiday type",
"Government" );
273 typeName = i18nc(
"Holiday type",
"Financial" );
275 typeName = i18nc(
"Holiday type",
"Cultural" );
277 typeName = i18nc(
"Holiday type",
"Commemorative" );
279 typeName = i18nc(
"Holiday type",
"Historical" );
281 typeName = i18nc(
"Holiday type",
"School" );
283 typeName = i18nc(
"Holiday type",
"Seasonal" );
285 typeName = i18nc(
"Holiday type",
"Name Days" );
287 typeName = i18nc(
"Holiday type",
"Personal" );
289 typeName = i18nc(
"Holiday type",
"Christian" );
291 typeName = i18nc(
"Holiday type",
"Anglican" );
293 typeName = i18nc(
"Holiday type",
"Catholic" );
295 typeName = i18nc(
"Holiday type",
"Protestant" );
297 typeName = i18nc(
"Holiday type",
"Orthodox" );
299 typeName = i18nc(
"Holiday type",
"Jewish" );
301 typeName = i18nc(
"Holiday type",
"Jewish Orthodox" );
303 typeName = i18nc(
"Holiday type",
"Jewish Conservative" );
305 typeName = i18nc(
"Holiday type",
"Jewish Reform" );
307 typeName = i18nc(
"Holiday type",
"Islamic" );
309 typeName = i18nc(
"Holiday type",
"Islamic Sunni" );
311 typeName = i18nc(
"Holiday type",
"Islamic Shia" );
313 typeName = i18nc(
"Holiday type",
"Islamic Sufi" );
319 tempName = i18nc(
"Holiday file display name, %1 = region name, %2 = holiday type",
"%1 - %2" ).
arg( regionName ).
arg( typeName );
321 tempName = regionName;
323 }
else if ( !typeName.
isEmpty() ) {
326 tempName = i18nc(
"Unknown holiday region",
"Unknown" );
334 HolidayRegion temp = HolidayRegion( regionCode );
335 if ( temp.isValid() ) {
342 QString HolidayRegion::description()
const
344 return d->mDriver->fileDescription();
349 HolidayRegion temp = HolidayRegion( regionCode );
350 if ( temp.isValid() ) {
351 return temp.description();
357 bool HolidayRegion::isValid()
const
359 return d->mHolidayFile.exists() && d->mDriver;
362 bool HolidayRegion::isValid(
const QString ®ionCode )
364 HolidayRegion temp = HolidayRegion( regionCode );
365 return temp.isValid();
370 return holidays( startDate, endDate, Holiday::MultidayHolidaysAsMultipleEvents );
374 Holiday::MultidayMode multidayMode )
const
377 return d->mDriver->parseHolidays( startDate, endDate, multidayMode );
385 return holidays( date, Holiday::MultidayHolidaysAsMultipleEvents );
388 Holiday::List HolidayRegion::holidays(
const QDate &date, Holiday::MultidayMode multidayMode )
const
391 return d->mDriver->parseHolidays( date, multidayMode );
399 return holidays( calendarYear, calendarType, Holiday::MultidayHolidaysAsMultipleEvents );
403 Holiday::MultidayMode multidayMode )
const
406 return d->mDriver->parseHolidays( calendarYear, calendarType, multidayMode );
412 bool HolidayRegion::isHoliday(
const QDate &date )
const
414 Holiday::List holidayList = holidays( date, Holiday::MultidayHolidaysAsMultipleEvents );
415 if ( holidayList.
count() > 0 ) {
416 foreach (
const KHolidays::Holiday &holiday, holidayList ) {
417 if ( holiday.dayType() == Holiday::NonWorkday ) {
436 QString localeCountry, localeLanguage, localeLanguageCountry;
439 localeCountry = KGlobal::locale()->country().
toLower();
441 localeCountry = country.
toLower();
445 localeLanguage = KGlobal::locale()->language().
toLower();
447 localeLanguage = language.
toLower();
454 QStringList regionList = KHolidays::HolidayRegion::regionCodes();
456 QString countryAndLanguageMatch, countryOnlyMatch, subdivisionAndLanguageMatch,
457 subdivisionOnlyMatch, languageCountryAndLanguageMatch, languageCountryOnlyMatch,
458 languageSubdivisionAndLanguageMatch, languageSubdivisionOnlyMatch;
460 foreach (
const QString ®ionCode, regionList ) {
461 QString regionCountry = KHolidays::HolidayRegion::countryCode( regionCode ).
toLower();
462 QString regionSubdivisionCountry;
466 QString regionLanguage = KHolidays::HolidayRegion::languageCode( regionCode ).
toLower();
468 if ( regionCountry == localeCountry && regionLanguage == localeLanguage ) {
469 countryAndLanguageMatch = regionCode;
471 }
else if ( regionCountry == localeCountry ) {
472 if ( countryOnlyMatch.
isEmpty() ) {
473 countryOnlyMatch = regionCode;
475 }
else if ( !regionSubdivisionCountry.
isEmpty() &&
476 regionSubdivisionCountry == localeCountry &&
477 regionLanguage == localeLanguage ) {
478 if ( subdivisionAndLanguageMatch.
isEmpty() ) {
479 subdivisionAndLanguageMatch = regionCode;
481 }
else if ( !regionSubdivisionCountry.
isEmpty() && regionSubdivisionCountry == localeCountry ) {
482 if ( subdivisionOnlyMatch.
isEmpty() ) {
483 subdivisionOnlyMatch = regionCode;
485 }
else if ( !localeLanguageCountry.
isEmpty() &&
486 regionCountry == localeLanguageCountry &&
487 regionLanguage == localeLanguage ) {
488 if ( languageCountryAndLanguageMatch.
isEmpty() ) {
489 languageCountryAndLanguageMatch = regionCode;
491 }
else if ( !localeLanguageCountry.
isEmpty() && regionCountry == localeLanguageCountry ) {
492 if ( languageCountryOnlyMatch.
isEmpty() ) {
493 languageCountryOnlyMatch = regionCode;
495 }
else if ( !regionSubdivisionCountry.
isEmpty() &&
496 !localeLanguageCountry.
isEmpty() &&
497 regionSubdivisionCountry == localeLanguageCountry &&
498 regionLanguage == localeLanguage ) {
499 if ( languageSubdivisionAndLanguageMatch.
isEmpty() ) {
500 languageSubdivisionAndLanguageMatch = regionCode;
502 }
else if ( !regionSubdivisionCountry.
isEmpty() &&
503 !localeLanguageCountry.
isEmpty() &&
504 regionSubdivisionCountry == localeLanguageCountry ) {
505 if ( languageSubdivisionOnlyMatch.
isEmpty() ) {
506 languageSubdivisionOnlyMatch = regionCode;
513 if ( !countryAndLanguageMatch.
isEmpty() ) {
514 defaultRegionCode = countryAndLanguageMatch;
515 }
else if ( !countryOnlyMatch.
isEmpty() ) {
516 defaultRegionCode = countryOnlyMatch;
517 }
else if ( !subdivisionAndLanguageMatch.
isEmpty() ) {
518 defaultRegionCode = subdivisionAndLanguageMatch;
519 }
else if ( !subdivisionOnlyMatch.
isEmpty() ) {
520 defaultRegionCode = subdivisionOnlyMatch;
521 }
else if ( !languageCountryAndLanguageMatch.
isEmpty() ) {
522 defaultRegionCode = languageCountryAndLanguageMatch;
523 }
else if ( !languageCountryOnlyMatch.
isEmpty() ) {
524 defaultRegionCode = languageCountryOnlyMatch;
525 }
else if ( !languageSubdivisionAndLanguageMatch.
isEmpty() ) {
526 defaultRegionCode = languageSubdivisionAndLanguageMatch;
527 }
else if ( !languageSubdivisionOnlyMatch.
isEmpty() ) {
528 defaultRegionCode = languageSubdivisionOnlyMatch;
531 return defaultRegionCode;
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
const T & at(int i) const
int lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
HolidayParserDriver abstract base class.
int count(const T &value) const
void append(const T &value)
virtual QString fileCountryCode() const
Return the ISO 3166 country/region code of the file.
QString mid(int position, int n) const
QStringList split(const QString &sep, const QString &str, bool allowEmptyEntries)
QString left(int n) const
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
HolidayParserDriverPlan implementation class.