• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepimlibs API Reference
  • KDE Home
  • Contact Us
 

KHolidays Library

  • sources
  • kde-4.12
  • kdepimlibs
  • kholidays
holidayregion.cpp
1 /*
2  This file is part of the kholidays library.
3 
4  Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
5  Copyright (c) 2004 Allen Winter <winter@kde.org>
6  Copyright (c) 2008 David Jarvie <djarvie@kde.org>
7  Copyright 2010 John Layt <john@layt.net>
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Library General Public
11  License as published by the Free Software Foundation; either
12  version 2 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Library General Public License for more details.
18 
19  You should have received a copy of the GNU Library General Public License
20  along with this library; see the file COPYING.LIB. If not, write to the
21  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22  Boston, MA 02110-1301, USA.
23 */
24 
25 #include "holidayregion.h"
26 
27 #include <QtCore/QDateTime>
28 #include <QtCore/QFile>
29 #include <QtCore/QSharedData>
30 #include <QtCore/QFileInfo>
31 
32 #include <KStandardDirs>
33 #include <KGlobal>
34 #include <KLocale>
35 #include <KLocalizedString>
36 #include <KDebug>
37 
38 #include "holiday_p.h"
39 #include "parsers/plan2/holidayparserdriverplan_p.h"
40 
41 using namespace KHolidays;
42 
43 class HolidayRegion::Private
44 {
45  public:
46  Private( const QString &regionCode ) : mDriver( 0 ),
47  mRegionCode( regionCode )
48  {
49  if ( !mRegionCode.isEmpty() ) {
50 
51  if ( mRegionCode.length() == 2 ) { //Backwards compatible mode for old location code
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 );
59  }
60  }
61 
62  mHolidayFile.setFile(
63  KStandardDirs::locate( "data", QLatin1String("libkholidays/plan2/holiday_") + mRegionCode ) );
64  }
65 
66  init();
67  }
68 
69  Private( const QFileInfo &regionFile ) : mDriver( 0 ),
70  mHolidayFile( regionFile )
71  {
72  init();
73  }
74 
75  ~Private()
76  {
77  delete mDriver;
78  }
79 
80  void init()
81  {
82  if ( mHolidayFile.exists() ) {
83  mDriver = new HolidayParserDriverPlan( mHolidayFile.absoluteFilePath() );
84  if ( mDriver ) {
85 
86  if ( mLocation.isEmpty() ) {
87  mLocation = mDriver->fileCountryCode().left( 2 );
88  }
89 
90  if ( mRegionCode.isEmpty() ) {
91  if ( mHolidayFile.fileName().startsWith( QLatin1String( "holiday_" ) ) ) {
92  mRegionCode = mHolidayFile.fileName().mid( 8 );
93  } else {
94  mRegionCode = mHolidayFile.fileName();
95  }
96  }
97 
98  } else {
99  mRegionCode.clear();
100  mLocation.clear();
101  }
102  } else {
103  mRegionCode.clear();
104  mLocation.clear();
105  }
106  }
107 
108  HolidayParserDriver *mDriver; // The parser driver for the holiday file
109  QString mRegionCode; // region code of holiday region
110  QString mLocation; // old location code, use now deprecated
111  QFileInfo mHolidayFile; // file containing holiday data, or null
112 };
113 
114 HolidayRegion::HolidayRegion( const QString &regionCode )
115  : d( new Private( regionCode ) )
116 {
117 }
118 
119 HolidayRegion::HolidayRegion( const QFileInfo &regionFile )
120  : d( new Private( regionFile ) )
121 {
122 }
123 
124 HolidayRegion::~HolidayRegion()
125 {
126  delete d;
127 }
128 
129 QStringList HolidayRegion::locations()
130 {
131  const QStringList files =
132  KGlobal::dirs()->findAllResources( "data", QLatin1String("libkholidays/plan2/holiday_*"),
133  KStandardDirs::NoDuplicates );
134 
135  QStringList locations;
136  foreach ( const QString &filename, files ) {
137  locations.append( filename.mid( filename.lastIndexOf( QLatin1String("holiday_") ) + 8, 2 ) );
138  }
139 
140  locations.removeDuplicates();
141  qSort( locations );
142  return locations;
143 }
144 
145 QString HolidayRegion::location() const
146 {
147  return d->mLocation;
148 }
149 
150 QStringList HolidayRegion::regionCodes()
151 {
152  const QStringList files =
153  KGlobal::dirs()->findAllResources( "data", QLatin1String("libkholidays/plan2/holiday_*"),
154  KStandardDirs::NoDuplicates );
155 
156  QStringList regionCodesList;
157  foreach ( const QString &filename, files ) {
158  regionCodesList.append( filename.mid( filename.lastIndexOf( QLatin1String("holiday_") ) + 8 ) );
159  }
160 
161  qSort( regionCodesList );
162  return regionCodesList;
163 }
164 
165 QString HolidayRegion::regionCode() const
166 {
167  return d->mRegionCode;
168 }
169 
170 QString HolidayRegion::countryCode() const
171 {
172  return d->mDriver->fileCountryCode();
173 }
174 
175 QString HolidayRegion::countryCode( const QString &regionCode )
176 {
177  HolidayRegion temp = HolidayRegion( regionCode );
178  if ( temp.isValid() ) {
179  return temp.countryCode();
180  } else {
181  return QString();
182  }
183 }
184 
185 QString HolidayRegion::languageCode() const
186 {
187  return d->mDriver->fileLanguageCode();
188 }
189 
190 QString HolidayRegion::languageCode( const QString &regionCode )
191 {
192  HolidayRegion temp = HolidayRegion( regionCode );
193  if ( temp.isValid() ) {
194  return temp.languageCode();
195  } else {
196  return QString();
197  }
198 }
199 
200 QString HolidayRegion::name() const
201 {
202  QString tempName = d->mDriver->fileName();
203 
204  if ( tempName.isEmpty() ) {
205  QStringList countryParts = countryCode().toLower().split( QLatin1Char('-') );
206  QString country = countryParts.at( 0 );
207  QString regionName, typeName;
208 
209  if ( country != QLatin1String("xx") ) {
210  if ( countryParts.count() == 2 ) {
211  // Temporary measure to get regions translated, only those files that already exist
212  // In 4.6 hope to have isocodes project integration for translations via KLocale
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" );
250  } else {
251  regionName = KGlobal::locale()->countryCodeToName( country );
252  }
253  } else {
254  regionName = KGlobal::locale()->countryCodeToName( country );
255  }
256  }
257 
258  //Cheat on type for now,take direct from region code until API is introduced in SC 4.6
259  QStringList regionParts = regionCode().toLower().split( QLatin1Char('_') );
260  if ( regionParts.count() == 3 ) {
261  QString type = regionParts.at( 2 );
262  // Will create lots more in 4.6
263  // Religious types, just simple for now
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" );
290  }
291  }
292 
293  if ( !regionName.isEmpty() ) {
294  if ( !typeName.isEmpty() ) {
295  //TODO translate when not frozen
296  tempName = QString::fromLatin1( "%1 - %2" ).arg( regionName ).arg( typeName );
297  } else {
298  tempName = regionName;
299  }
300  } else if ( !typeName.isEmpty() ) {
301  tempName = typeName;
302  } else {
303  tempName = i18nc( "Unknown holiday region", "Unknown" );
304  }
305  }
306  return tempName;
307 }
308 
309 QString HolidayRegion::name( const QString &regionCode )
310 {
311  HolidayRegion temp = HolidayRegion( regionCode );
312  if ( temp.isValid() ) {
313  return temp.name();
314  } else {
315  return QString();
316  }
317 }
318 
319 QString HolidayRegion::description() const
320 {
321  return d->mDriver->fileDescription();
322 }
323 
324 QString HolidayRegion::description( const QString &regionCode )
325 {
326  HolidayRegion temp = HolidayRegion( regionCode );
327  if ( temp.isValid() ) {
328  return temp.description();
329  } else {
330  return QString();
331  }
332 }
333 
334 bool HolidayRegion::isValid() const
335 {
336  return d->mHolidayFile.exists() && d->mDriver;
337 }
338 
339 bool HolidayRegion::isValid( const QString &regionCode )
340 {
341  HolidayRegion temp = HolidayRegion( regionCode );
342  return temp.isValid();
343 }
344 
345 Holiday::List HolidayRegion::holidays( const QDate &startDate, const QDate &endDate ) const
346 {
347  return holidays( startDate, endDate, Holiday::MultidayHolidaysAsMultipleEvents );
348 }
349 
350 Holiday::List HolidayRegion::holidays( const QDate &startDate, const QDate &endDate,
351  Holiday::MultidayMode multidayMode ) const
352 {
353  if ( isValid() ) {
354  return d->mDriver->parseHolidays( startDate, endDate, multidayMode );
355  } else {
356  return Holiday::List();
357  }
358 }
359 
360 Holiday::List HolidayRegion::holidays( const QDate &date ) const
361 {
362  return holidays( date, Holiday::MultidayHolidaysAsMultipleEvents );
363 }
364 
365 Holiday::List HolidayRegion::holidays( const QDate &date, Holiday::MultidayMode multidayMode ) const
366 {
367  if ( isValid() ) {
368  return d->mDriver->parseHolidays( date, multidayMode );
369  } else {
370  return Holiday::List();
371  }
372 }
373 
374 Holiday::List HolidayRegion::holidays( int calendarYear, const QString &calendarType ) const
375 {
376  return holidays( calendarYear, calendarType, Holiday::MultidayHolidaysAsMultipleEvents );
377 }
378 
379 Holiday::List HolidayRegion::holidays( int calendarYear, const QString &calendarType,
380  Holiday::MultidayMode multidayMode ) const
381 {
382  if ( isValid() ) {
383  return d->mDriver->parseHolidays( calendarYear, calendarType, multidayMode );
384  } else {
385  return Holiday::List();
386  }
387 }
388 
389 bool HolidayRegion::isHoliday( const QDate &date ) const
390 {
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 ) {
395  return true;
396  }
397  }
398  }
399  return false;
400 }
401 
402 QString HolidayRegion::defaultRegionCode( const QString &country, const QString &language )
403 {
404  // Try to match against the users country and language, or failing that the language country.
405  // Scan through all the regions finding the first match for each possible default
406  // Holiday Region Country Code can be a country subdivision or the country itself,
407  // e.g. US or US-CA for California, so we can try match on both but an exact match has priority
408  // The Holiday Region file is in one language only, so give priority to any file in the
409  // users language, e.g. bilingual countries with a separate file for each language
410  // Locale language can have a country code embedded in it e.g. en_GB, which we can try use if
411  // no country set, but a lot of countries use en_GB so it's a lower priority option
412 
413  QString localeCountry, localeLanguage, localeLanguageCountry;
414 
415  if ( country.isEmpty() ) {
416  localeCountry = KGlobal::locale()->country().toLower();
417  } else {
418  localeCountry = country.toLower();
419  }
420 
421  if ( language.isEmpty() ) {
422  localeLanguage = KGlobal::locale()->language().toLower();
423  } else {
424  localeLanguage = language.toLower();
425  }
426 
427  if ( localeLanguage.split( QLatin1Char('_') ).count() > 1 ) {
428  localeLanguageCountry = localeLanguage.split( QLatin1Char('_') ).at( 1 );
429  }
430 
431  QStringList regionList = KHolidays::HolidayRegion::regionCodes();
432 
433  QString countryAndLanguageMatch, countryOnlyMatch, subdivisionAndLanguageMatch,
434  subdivisionOnlyMatch, languageCountryAndLanguageMatch, languageCountryOnlyMatch,
435  languageSubdivisionAndLanguageMatch, languageSubdivisionOnlyMatch;
436 
437  foreach ( const QString &regionCode, 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 );
442  }
443  QString regionLanguage = KHolidays::HolidayRegion::languageCode( regionCode ).toLower();
444 
445  if ( regionCountry == localeCountry && regionLanguage == localeLanguage ) {
446  countryAndLanguageMatch = regionCode;
447  break; // exact match so don't look further
448  } else if ( regionCountry == localeCountry ) {
449  if ( countryOnlyMatch.isEmpty() ) {
450  countryOnlyMatch = regionCode;
451  }
452  } else if ( !regionSubdivisionCountry.isEmpty() &&
453  regionSubdivisionCountry == localeCountry &&
454  regionLanguage == localeLanguage ) {
455  if ( subdivisionAndLanguageMatch.isEmpty() ) {
456  subdivisionAndLanguageMatch = regionCode;
457  }
458  } else if ( !regionSubdivisionCountry.isEmpty() && regionSubdivisionCountry == localeCountry ) {
459  if ( subdivisionOnlyMatch.isEmpty() ) {
460  subdivisionOnlyMatch = regionCode;
461  }
462  } else if ( !localeLanguageCountry.isEmpty() &&
463  regionCountry == localeLanguageCountry &&
464  regionLanguage == localeLanguage ) {
465  if ( languageCountryAndLanguageMatch.isEmpty() ) {
466  languageCountryAndLanguageMatch = regionCode;
467  }
468  } else if ( !localeLanguageCountry.isEmpty() && regionCountry == localeLanguageCountry ) {
469  if ( languageCountryOnlyMatch.isEmpty() ) {
470  languageCountryOnlyMatch = regionCode;
471  }
472  } else if ( !regionSubdivisionCountry.isEmpty() &&
473  !localeLanguageCountry.isEmpty() &&
474  regionSubdivisionCountry == localeLanguageCountry &&
475  regionLanguage == localeLanguage ) {
476  if ( languageSubdivisionAndLanguageMatch.isEmpty() ) {
477  languageSubdivisionAndLanguageMatch = regionCode;
478  }
479  } else if ( !regionSubdivisionCountry.isEmpty() &&
480  !localeLanguageCountry.isEmpty() &&
481  regionSubdivisionCountry == localeLanguageCountry ) {
482  if ( languageSubdivisionOnlyMatch.isEmpty() ) {
483  languageSubdivisionOnlyMatch = regionCode;
484  }
485  }
486  }
487 
488  QString defaultRegionCode;
489 
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;
506  }
507 
508  return defaultRegionCode;
509 }
KHolidays::HolidayParserDriver
HolidayParserDriver abstract base class.
Definition: holidayparserdriver_p.h:44
KHolidays::HolidayParserDriver::fileCountryCode
virtual QString fileCountryCode() const
Return the ISO 3166 country/region code of the file.
Definition: holidayparserdriver.cpp:40
KHolidays::HolidayParserDriverPlan
HolidayParserDriverPlan implementation class.
Definition: holidayparserdriverplan_p.h:53
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:06 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KHolidays Library

Skip menu "KHolidays Library"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal