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",
54 QLatin1String(
"libkholidays/plan2/holiday_") + mLocation + QLatin1Char(
'*'),
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();
111 QFileInfo mHolidayFile;
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()
129 QStringList HolidayRegion::locations()
131 const QStringList files =
132 KGlobal::dirs()->findAllResources(
"data", QLatin1String(
"libkholidays/plan2/holiday_*"),
133 KStandardDirs::NoDuplicates );
135 QStringList locations;
136 foreach (
const QString &filename, files ) {
137 locations.append( filename.mid( filename.lastIndexOf( QLatin1String(
"holiday_") ) + 8, 2 ) );
140 locations.removeDuplicates();
145 QString HolidayRegion::location()
const
150 QStringList HolidayRegion::regionCodes()
152 const QStringList files =
153 KGlobal::dirs()->findAllResources(
"data", QLatin1String(
"libkholidays/plan2/holiday_*"),
154 KStandardDirs::NoDuplicates );
156 QStringList regionCodesList;
157 foreach (
const QString &filename, files ) {
158 regionCodesList.append( filename.mid( filename.lastIndexOf( QLatin1String(
"holiday_") ) + 8 ) );
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();
175 QString HolidayRegion::countryCode(
const QString ®ionCode )
177 HolidayRegion temp = HolidayRegion( regionCode );
178 if ( temp.isValid() ) {
179 return temp.countryCode();
185 QString HolidayRegion::languageCode()
const
187 return d->mDriver->fileLanguageCode();
190 QString HolidayRegion::languageCode(
const QString ®ionCode )
192 HolidayRegion temp = HolidayRegion( regionCode );
193 if ( temp.isValid() ) {
194 return temp.languageCode();
200 QString HolidayRegion::name()
const
202 QString tempName = d->mDriver->fileName();
204 if ( tempName.isEmpty() ) {
205 QStringList countryParts = countryCode().toLower().split( QLatin1Char(
'-') );
206 QString country = countryParts.at( 0 );
207 QString regionName, typeName;
209 if ( country != QLatin1String(
"xx") ) {
210 if ( countryParts.count() == 2 ) {
213 QString subdivision = countryParts.at( 1 );
214 if ( country == QLatin1String(
"ca") && subdivision == QLatin1String(
"qc") ) {
215 regionName = i18nc(
"Canadian region",
"Quebec" );
216 }
else if ( country == QLatin1String(
"de") && subdivision == QLatin1String(
"by") ) {
217 regionName = i18nc(
"German region",
"Bavaria" );
218 }
else if ( country == QLatin1String(
"es") && subdivision == QLatin1String(
"ct") ) {
219 regionName = i18nc(
"Spanish region",
"Catalonia" );
220 }
else if ( country == QLatin1String(
"gb") && subdivision == QLatin1String(
"eaw") ) {
221 regionName = i18nc(
"UK Region",
"England and Wales" );
222 }
else if ( country == QLatin1String(
"gb") && subdivision == QLatin1String(
"eng") ) {
223 regionName = i18nc(
"UK Region",
"England" );
224 }
else if ( country == QLatin1String(
"gb") && subdivision == QLatin1String(
"wls") ) {
225 regionName = i18nc(
"UK Region",
"Wales" );
226 }
else if ( country == QLatin1String(
"gb") && subdivision == QLatin1String(
"sct") ) {
227 regionName = i18nc(
"UK Region",
"Scotland" );
228 }
else if ( country == QLatin1String(
"gb") && subdivision == QLatin1String(
"nir") ) {
229 regionName = i18nc(
"UK Region",
"Northern Ireland" );
230 }
else if ( country == QLatin1String(
"it") && subdivision == QLatin1String(
"bz") ) {
231 regionName = i18nc(
"Italian Region",
"South Tyrol" );
232 }
else if ( country == QLatin1String(
"au") && subdivision == QLatin1String(
"nsw") ) {
233 regionName = i18nc(
"Australian Region",
"New South Wales" );
234 }
else if ( country == QLatin1String(
"au") && subdivision == QLatin1String(
"qld") ) {
235 regionName = i18nc(
"Australian Region",
"Queensland" );
236 }
else if ( country == QLatin1String(
"au") && subdivision == QLatin1String(
"vic") ) {
237 regionName = i18nc(
"Australian Region",
"Victoria" );
238 }
else if ( country == QLatin1String(
"au") && subdivision == QLatin1String(
"sa") ) {
239 regionName = i18nc(
"Australian Region",
"South Australia" );
240 }
else if ( country == QLatin1String(
"au") && subdivision == QLatin1String(
"nt") ) {
241 regionName = i18nc(
"Australian Region",
"Northern Territory" );
242 }
else if ( country == QLatin1String(
"au") && subdivision == QLatin1String(
"act") ) {
243 regionName = i18nc(
"Australian Region",
"Australian Capital Territory" );
244 }
else if ( country == QLatin1String(
"au") && subdivision == QLatin1String(
"wa") ) {
245 regionName = i18nc(
"Australian Region",
"Western Australia" );
246 }
else if ( country == QLatin1String(
"au") && subdivision == QLatin1String(
"tas") ) {
247 regionName = i18nc(
"Australian Region",
"Tasmania" );
248 }
else if ( country == QLatin1String(
"ba") && subdivision == QLatin1String(
"srp") ) {
249 regionName = i18nc(
"Bosnian and Herzegovinian Region",
"Republic of Srpska" );
251 regionName = KGlobal::locale()->countryCodeToName( country );
254 regionName = KGlobal::locale()->countryCodeToName( country );
259 QStringList regionParts = regionCode().toLower().split( QLatin1Char(
'_') );
260 if ( regionParts.count() == 3 ) {
261 QString type = regionParts.at( 2 );
264 if ( type == QLatin1String(
"public") ) {
265 typeName = i18nc(
"Holiday type",
"Public" );
266 }
else if ( type == QLatin1String(
"religious") ) {
267 typeName = i18nc(
"Holiday type",
"Religious" );
268 }
else if ( type == QLatin1String(
"financial") ) {
269 typeName = i18nc(
"Holiday type",
"Financial" );
270 }
else if ( type == QLatin1String(
"cultural") ) {
271 typeName = i18nc(
"Holiday type",
"Cultural" );
272 }
else if ( type == QLatin1String(
"school") ) {
273 typeName = i18nc(
"Holiday type",
"School" );
274 }
else if ( type == QLatin1String(
"seasons") ) {
275 typeName = i18nc(
"Holiday type",
"Seasons" );
276 }
else if ( type == QLatin1String(
"name") ) {
277 typeName = i18nc(
"Holiday type",
"Name Days" );
278 }
else if ( type == QLatin1String(
"personal") ) {
279 typeName = i18nc(
"Holiday type",
"Personal" );
280 }
else if ( type == QLatin1String(
"catholic") ) {
281 typeName = i18nc(
"Holiday type",
"Catholic" );
282 }
else if ( type == QLatin1String(
"protestant") ) {
283 typeName = i18nc(
"Holiday type",
"Protestant" );
284 }
else if ( type == QLatin1String(
"orthodox") ) {
285 typeName = i18nc(
"Holiday type",
"Orthodox" );
286 }
else if ( type == QLatin1String(
"jewish") ) {
287 typeName = i18nc(
"Holiday type",
"Jewish" );
288 }
else if ( type == QLatin1String(
"islamic") ) {
289 typeName = i18nc(
"Holiday type",
"Islamic" );
293 if ( !regionName.isEmpty() ) {
294 if ( !typeName.isEmpty() ) {
296 tempName = QString::fromLatin1(
"%1 - %2" ).arg( regionName ).arg( typeName );
298 tempName = regionName;
300 }
else if ( !typeName.isEmpty() ) {
303 tempName = i18nc(
"Unknown holiday region",
"Unknown" );
309 QString HolidayRegion::name(
const QString ®ionCode )
311 HolidayRegion temp = HolidayRegion( regionCode );
312 if ( temp.isValid() ) {
319 QString HolidayRegion::description()
const
321 return d->mDriver->fileDescription();
324 QString HolidayRegion::description(
const QString ®ionCode )
326 HolidayRegion temp = HolidayRegion( regionCode );
327 if ( temp.isValid() ) {
328 return temp.description();
334 bool HolidayRegion::isValid()
const
336 return d->mHolidayFile.exists() && d->mDriver;
339 bool HolidayRegion::isValid(
const QString ®ionCode )
341 HolidayRegion temp = HolidayRegion( regionCode );
342 return temp.isValid();
345 Holiday::List HolidayRegion::holidays(
const QDate &startDate,
const QDate &endDate )
const
347 return holidays( startDate, endDate, Holiday::MultidayHolidaysAsMultipleEvents );
350 Holiday::List HolidayRegion::holidays(
const QDate &startDate,
const QDate &endDate,
351 Holiday::MultidayMode multidayMode )
const
354 return d->mDriver->parseHolidays( startDate, endDate, multidayMode );
356 return Holiday::List();
360 Holiday::List HolidayRegion::holidays(
const QDate &date )
const
362 return holidays( date, Holiday::MultidayHolidaysAsMultipleEvents );
365 Holiday::List HolidayRegion::holidays(
const QDate &date, Holiday::MultidayMode multidayMode )
const
368 return d->mDriver->parseHolidays( date, multidayMode );
370 return Holiday::List();
374 Holiday::List HolidayRegion::holidays(
int calendarYear,
const QString &calendarType )
const
376 return holidays( calendarYear, calendarType, Holiday::MultidayHolidaysAsMultipleEvents );
379 Holiday::List HolidayRegion::holidays(
int calendarYear,
const QString &calendarType,
380 Holiday::MultidayMode multidayMode )
const
383 return d->mDriver->parseHolidays( calendarYear, calendarType, multidayMode );
385 return Holiday::List();
389 bool HolidayRegion::isHoliday(
const QDate &date )
const
391 Holiday::List holidayList = holidays( date, Holiday::MultidayHolidaysAsMultipleEvents );
392 if ( holidayList.count() > 0 ) {
393 foreach (
const KHolidays::Holiday &holiday, holidayList ) {
394 if ( holiday.dayType() == Holiday::NonWorkday ) {
402 QString HolidayRegion::defaultRegionCode(
const QString &country,
const QString &language )
413 QString localeCountry, localeLanguage, localeLanguageCountry;
415 if ( country.isEmpty() ) {
416 localeCountry = KGlobal::locale()->country().toLower();
418 localeCountry = country.toLower();
421 if ( language.isEmpty() ) {
422 localeLanguage = KGlobal::locale()->language().toLower();
424 localeLanguage = language.toLower();
427 if ( localeLanguage.split( QLatin1Char(
'_') ).count() > 1 ) {
428 localeLanguageCountry = localeLanguage.split( QLatin1Char(
'_') ).at( 1 );
431 QStringList regionList = KHolidays::HolidayRegion::regionCodes();
433 QString countryAndLanguageMatch, countryOnlyMatch, subdivisionAndLanguageMatch,
434 subdivisionOnlyMatch, languageCountryAndLanguageMatch, languageCountryOnlyMatch,
435 languageSubdivisionAndLanguageMatch, languageSubdivisionOnlyMatch;
437 foreach (
const QString ®ionCode, regionList ) {
438 QString regionCountry = KHolidays::HolidayRegion::countryCode( regionCode ).toLower();
439 QString regionSubdivisionCountry;
440 if ( regionCountry.split( QLatin1Char(
'-') ).count() > 1 ) {
441 regionSubdivisionCountry = regionCountry.split( QLatin1Char(
'-') ).at( 0 );
443 QString regionLanguage = KHolidays::HolidayRegion::languageCode( regionCode ).toLower();
445 if ( regionCountry == localeCountry && regionLanguage == localeLanguage ) {
446 countryAndLanguageMatch = regionCode;
448 }
else if ( regionCountry == localeCountry ) {
449 if ( countryOnlyMatch.isEmpty() ) {
450 countryOnlyMatch = regionCode;
452 }
else if ( !regionSubdivisionCountry.isEmpty() &&
453 regionSubdivisionCountry == localeCountry &&
454 regionLanguage == localeLanguage ) {
455 if ( subdivisionAndLanguageMatch.isEmpty() ) {
456 subdivisionAndLanguageMatch = regionCode;
458 }
else if ( !regionSubdivisionCountry.isEmpty() && regionSubdivisionCountry == localeCountry ) {
459 if ( subdivisionOnlyMatch.isEmpty() ) {
460 subdivisionOnlyMatch = regionCode;
462 }
else if ( !localeLanguageCountry.isEmpty() &&
463 regionCountry == localeLanguageCountry &&
464 regionLanguage == localeLanguage ) {
465 if ( languageCountryAndLanguageMatch.isEmpty() ) {
466 languageCountryAndLanguageMatch = regionCode;
468 }
else if ( !localeLanguageCountry.isEmpty() && regionCountry == localeLanguageCountry ) {
469 if ( languageCountryOnlyMatch.isEmpty() ) {
470 languageCountryOnlyMatch = regionCode;
472 }
else if ( !regionSubdivisionCountry.isEmpty() &&
473 !localeLanguageCountry.isEmpty() &&
474 regionSubdivisionCountry == localeLanguageCountry &&
475 regionLanguage == localeLanguage ) {
476 if ( languageSubdivisionAndLanguageMatch.isEmpty() ) {
477 languageSubdivisionAndLanguageMatch = regionCode;
479 }
else if ( !regionSubdivisionCountry.isEmpty() &&
480 !localeLanguageCountry.isEmpty() &&
481 regionSubdivisionCountry == localeLanguageCountry ) {
482 if ( languageSubdivisionOnlyMatch.isEmpty() ) {
483 languageSubdivisionOnlyMatch = regionCode;
488 QString defaultRegionCode;
490 if ( !countryAndLanguageMatch.isEmpty() ) {
491 defaultRegionCode = countryAndLanguageMatch;
492 }
else if ( !countryOnlyMatch.isEmpty() ) {
493 defaultRegionCode = countryOnlyMatch;
494 }
else if ( !subdivisionAndLanguageMatch.isEmpty() ) {
495 defaultRegionCode = subdivisionAndLanguageMatch;
496 }
else if ( !subdivisionOnlyMatch.isEmpty() ) {
497 defaultRegionCode = subdivisionOnlyMatch;
498 }
else if ( !languageCountryAndLanguageMatch.isEmpty() ) {
499 defaultRegionCode = languageCountryAndLanguageMatch;
500 }
else if ( !languageCountryOnlyMatch.isEmpty() ) {
501 defaultRegionCode = languageCountryOnlyMatch;
502 }
else if ( !languageSubdivisionAndLanguageMatch.isEmpty() ) {
503 defaultRegionCode = languageSubdivisionAndLanguageMatch;
504 }
else if ( !languageSubdivisionOnlyMatch.isEmpty() ) {
505 defaultRegionCode = languageSubdivisionOnlyMatch;
508 return defaultRegionCode;
HolidayParserDriver abstract base class.
virtual QString fileCountryCode() const
Return the ISO 3166 country/region code of the file.
HolidayParserDriverPlan implementation class.