~ubuntu-branches/ubuntu/quantal/kdepimlibs/quantal-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*
  This file is part of the kholidays library.

  Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
  Copyright (c) 2004 Allen Winter <winter@kde.org>
  Copyright (c) 2008 David Jarvie <djarvie@kde.org>
  Copyright 2010 John Layt <john@layt.net>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Library General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Library General Public License for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; see the file COPYING.LIB.  If not, write to the
  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  Boston, MA 02110-1301, USA.
*/

#include "holidayregion.h"

#include <QtCore/QDateTime>
#include <QtCore/QFile>
#include <QtCore/QSharedData>
#include <QtCore/QFileInfo>

#include <KStandardDirs>
#include <KGlobal>
#include <KLocale>
#include <KDebug>

#include "holiday_p.h"
#include "parsers/plan2/holidayparserdriverplan_p.h"

using namespace KHolidays;

class HolidayRegion::Private
{
  public:
    Private( const QString &regionCode ) : mDriver( 0 ),
                                           mRegionCode( regionCode )
    {
      if ( !mRegionCode.isEmpty() ) {

        if ( mRegionCode.length() == 2 ) { //Backwards compatible mode for old location code
          mLocation = mRegionCode;
          QStringList locationFiles = KGlobal::dirs()->findAllResources( "data",
                                      "libkholidays/plan2/holiday_" + mLocation + '*',
                                      KStandardDirs::NoDuplicates );
          if ( locationFiles.count() > 0 ) {
            mRegionCode = locationFiles.at( 0 ).
                          mid( locationFiles.at( 0 ).lastIndexOf( "holiday_" ) + 8 );
          }
        }

        mHolidayFile.setFile(
          KStandardDirs::locate( "data", "libkholidays/plan2/holiday_" + mRegionCode ) );
      }

      init();
    }

    Private( const QFileInfo &regionFile ) : mDriver( 0 ),
                                             mHolidayFile( regionFile )
    {
      init();
    }

    ~Private()
    {
      delete mDriver;
    }

    void init()
    {
      if ( mHolidayFile.exists() ) {
        mDriver = new HolidayParserDriverPlan( mHolidayFile.absoluteFilePath() );
        if ( mDriver ) {

          if ( mLocation.isEmpty() ) {
            mLocation = mDriver->fileCountryCode().left( 2 );
          }

          if ( mRegionCode.isEmpty() ) {
            if ( mHolidayFile.fileName().startsWith( QLatin1String( "holiday_" ) ) ) {
              mRegionCode = mHolidayFile.fileName().mid( 8 );
            } else {
              mRegionCode = mHolidayFile.fileName();
            }
          }

        } else {
          mRegionCode.clear();
          mLocation.clear();
        }
      } else {
        mRegionCode.clear();
        mLocation.clear();
      }
    }

    HolidayParserDriver  *mDriver;  // The parser driver for the holiday file
    QString mRegionCode;            // region code of holiday region
    QString mLocation;              // old location code, use now deprecated
    QFileInfo mHolidayFile;         // file containing holiday data, or null
};

HolidayRegion::HolidayRegion( const QString &regionCode )
  : d( new Private( regionCode ) )
{
}

HolidayRegion::HolidayRegion( const QFileInfo &regionFile )
             : d( new Private( regionFile ) )
{
}

HolidayRegion::~HolidayRegion()
{
  delete d;
}

QStringList HolidayRegion::locations()
{
  const QStringList files =
    KGlobal::dirs()->findAllResources( "data", "libkholidays/plan2/holiday_*",
                                       KStandardDirs::NoDuplicates );

  QStringList locations;
  foreach ( const QString &filename, files ) {
    locations.append( filename.mid( filename.lastIndexOf( "holiday_" ) + 8, 2 ) );
  }

  locations.removeDuplicates();
  qSort( locations );
  return locations;
}

QString HolidayRegion::location() const
{
  return d->mLocation;
}

QStringList HolidayRegion::regionCodes()
{
  const QStringList files =
    KGlobal::dirs()->findAllResources( "data", "libkholidays/plan2/holiday_*",
                                       KStandardDirs::NoDuplicates );

  QStringList regionCodesList;
  foreach ( const QString &filename, files ) {
    regionCodesList.append( filename.mid( filename.lastIndexOf( "holiday_" ) + 8 ) );
  }

  qSort( regionCodesList );
  return regionCodesList;
}

QString HolidayRegion::regionCode() const
{
  return d->mRegionCode;
}

QString HolidayRegion::countryCode() const
{
  return d->mDriver->fileCountryCode();
}

QString HolidayRegion::countryCode( const QString &regionCode )
{
  HolidayRegion temp = HolidayRegion( regionCode );
  if ( temp.isValid() ) {
    return temp.countryCode();
  } else {
    return QString();
  }
}

QString HolidayRegion::languageCode() const
{
  return d->mDriver->fileLanguageCode();
}

QString HolidayRegion::languageCode( const QString &regionCode )
{
  HolidayRegion temp = HolidayRegion( regionCode );
  if ( temp.isValid() ) {
    return temp.languageCode();
  } else {
    return QString();
  }
}

QString HolidayRegion::name() const
{
  QString tempName = d->mDriver->fileName();

  if ( tempName.isEmpty() ) {
    QStringList countryParts = countryCode().toLower().split( '-' );
    QString country = countryParts.at( 0 );
    QString regionName, typeName;

    if ( country != "xx" ) {
      if ( countryParts.count() == 2 ) {
        // Temporary measure to get regions translated, only those files that already exist
        // In 4.6 hope to have isocodes project integration for translations via KLocale
        QString subdivision = countryParts.at( 1 );
        if ( country == "ca" && subdivision == "qc" ) {
          regionName = i18nc( "Canadian region", "Quebec" );
        } else if ( country == "de" && subdivision == "by" ) {
          regionName = i18nc( "German region", "Bavaria" );
        } else if ( country == "es" && subdivision == "ct" ) {
          regionName = i18nc( "Spanish region", "Catalonia" );
        } else if ( country == "gb" && subdivision == "eaw" ) {
          regionName = i18nc( "UK Region", "England and Wales" );
        } else if ( country == "gb" && subdivision == "eng" ) {
          regionName = i18nc( "UK Region", "England" );
        } else if ( country == "gb" && subdivision == "wls" ) {
          regionName = i18nc( "UK Region", "Wales" );
        } else if ( country == "gb" && subdivision == "sct" ) {
          regionName = i18nc( "UK Region", "Scotland" );
        } else if ( country == "gb" && subdivision == "nir" ) {
          regionName = i18nc( "UK Region", "Northern Ireland" );
        } else if ( country == "it" && subdivision == "bz" ) {
          regionName = i18nc( "Italian Region", "South Tyrol" );
        } else if ( country == "au" && subdivision == "nsw" ) {
          regionName = i18nc( "Australian Region", "New South Wales" );
        } else if ( country == "au" && subdivision == "qld" ) {
          regionName = i18nc( "Australian Region", "Queensland" );
        } else if ( country == "au" && subdivision == "vic" ) {
          regionName = i18nc( "Australian Region", "Victoria" );
        } else if ( country == "au" && subdivision == "sa" ) {
          regionName = i18nc( "Australian Region", "South Australia" );
        } else if ( country == "au" && subdivision == "nt" ) {
          regionName = i18nc( "Australian Region", "Northern Territory" );
        } else if ( country == "au" && subdivision == "act" ) {
          regionName = i18nc( "Australian Region", "Australian Capital Territory" );
        } else if ( country == "au" && subdivision == "wa" ) {
          regionName = i18nc( "Australian Region", "Western Australia" );
        } else if ( country == "au" && subdivision == "tas" ) {
          regionName = i18nc( "Australian Region", "Tasmania" );
        } else if ( country == "ba" && subdivision == "srp" ) {
          regionName = i18nc( "Bosnian and Herzegovinian Region", "Republic of Srpska" );
        } else {
          regionName = KGlobal::locale()->countryCodeToName( country );
        }
      } else {
          regionName = KGlobal::locale()->countryCodeToName( country );
      }
    }

    //Cheat on type for now,take direct from region code until API is introduced in SC 4.6
    QStringList regionParts = regionCode().toLower().split( '_' );
    if ( regionParts.count() == 3 ) {
      QString type = regionParts.at( 2 );
      // Will create lots more in 4.6
      // Religious types, just simple for now
      if ( type == "public" ) {
        typeName = i18nc( "Holiday type", "Public" );
      } else if ( type == "religious" ) {
        typeName = i18nc( "Holiday type", "Religious" );
      } else if ( type == "financial" ) {
        typeName = i18nc( "Holiday type", "Financial" );
      } else if ( type == "cultural" ) {
        typeName = i18nc( "Holiday type", "Cultural" );
      } else if ( type == "school" ) {
        typeName = i18nc( "Holiday type", "School" );
      } else if ( type == "seasons" ) {
        typeName = i18nc( "Holiday type", "Seasons" );
      } else if ( type == "name" ) {
        typeName = i18nc( "Holiday type", "Name Days" );
      } else if ( type == "personal" ) {
        typeName = i18nc( "Holiday type", "Personal" );
      } else if ( type == "catholic" ) {
        typeName = i18nc( "Holiday type", "Catholic" );
      } else if ( type == "protestant" ) {
        typeName = i18nc( "Holiday type", "Protestant" );
      } else if ( type == "orthodox" ) {
        typeName = i18nc( "Holiday type", "Orthodox" );
      } else if ( type == "jewish" ) {
        typeName = i18nc( "Holiday type", "Jewish" );
      } else if ( type == "islamic" ) {
        typeName = i18nc( "Holiday type", "Islamic" );
      }
    }

    if ( !regionName.isEmpty() ) {
      if ( !typeName.isEmpty() ) {
        //TODO translate when not frozen
        tempName = QString( "%1 - %2" ).arg( regionName ).arg( typeName );
      } else {
        tempName = regionName;
      }
    } else if ( !typeName.isEmpty() ) {
      tempName = typeName;
    } else {
      tempName = i18nc( "Unknown holiday region", "Unknown" );
    }
  }
  return tempName;
}

QString HolidayRegion::name( const QString &regionCode )
{
  HolidayRegion temp = HolidayRegion( regionCode );
  if ( temp.isValid() ) {
    return temp.name();
  } else {
    return QString();
  }
}

QString HolidayRegion::description() const
{
  return d->mDriver->fileDescription();
}

QString HolidayRegion::description( const QString &regionCode )
{
  HolidayRegion temp = HolidayRegion( regionCode );
  if ( temp.isValid() ) {
    return temp.description();
  } else {
    return QString();
  }
}

bool HolidayRegion::isValid() const
{
  return d->mHolidayFile.exists() && d->mDriver;
}

bool HolidayRegion::isValid( const QString &regionCode )
{
  HolidayRegion temp = HolidayRegion( regionCode );
  return temp.isValid();
}

Holiday::List HolidayRegion::holidays( const QDate &startDate, const QDate &endDate ) const
{
  return holidays( startDate, endDate, Holiday::MultidayHolidaysAsMultipleEvents );
}

Holiday::List HolidayRegion::holidays( const QDate &startDate, const QDate &endDate,
                                       Holiday::MultidayMode multidayMode ) const
{
  if ( isValid() ) {
    return d->mDriver->parseHolidays( startDate, endDate, multidayMode );
  } else {
    return Holiday::List();
  }
}

Holiday::List HolidayRegion::holidays( const QDate &date ) const
{
  return holidays( date, Holiday::MultidayHolidaysAsMultipleEvents );
}

Holiday::List HolidayRegion::holidays( const QDate &date, Holiday::MultidayMode multidayMode ) const
{
  if ( isValid() ) {
    return d->mDriver->parseHolidays( date, multidayMode );
  } else {
    return Holiday::List();
  }
}

Holiday::List HolidayRegion::holidays( int calendarYear, const QString &calendarType ) const
{
  return holidays( calendarYear, calendarType, Holiday::MultidayHolidaysAsMultipleEvents );
}

Holiday::List HolidayRegion::holidays( int calendarYear, const QString &calendarType,
                                       Holiday::MultidayMode multidayMode ) const
{
  if ( isValid() ) {
    return d->mDriver->parseHolidays( calendarYear, calendarType, multidayMode );
  } else {
    return Holiday::List();
  }
}

bool HolidayRegion::isHoliday( const QDate &date ) const
{
  Holiday::List holidayList = holidays( date, Holiday::MultidayHolidaysAsMultipleEvents );
  if ( holidayList.count() > 0 ) {
    foreach ( const KHolidays::Holiday &holiday, holidayList ) {
      if ( holiday.dayType() == Holiday::NonWorkday ) {
        return true;
      }
    }
  }
  return false;
}

QString HolidayRegion::defaultRegionCode( const QString &country, const QString &language )
{
  // Try to match against the users country and language, or failing that the language country.
  // Scan through all the regions finding the first match for each possible default
  // Holiday Region Country Code can be a country subdivision or the country itself,
  // e.g. US or US-CA for California, so we can try match on both but an exact match has priority
  // The Holiday Region file is in one language only, so give priority to any file in the
  // users language, e.g. bilingual countries with a separate file for each language
  // Locale language can have a country code embedded in it e.g. en_GB, which we can try use if
  // no country set, but a lot of countries use en_GB so it's a lower priority option

  QString localeCountry, localeLanguage, localeLanguageCountry;

  if ( country.isEmpty() ) {
    localeCountry = KGlobal::locale()->country().toLower();
  } else {
    localeCountry = country.toLower();
  }

  if ( language.isEmpty() ) {
    localeLanguage = KGlobal::locale()->language().toLower();
  } else {
    localeLanguage = language.toLower();
  }

  if ( localeLanguage.split( '_' ).count() > 1 ) {
    localeLanguageCountry = localeLanguage.split( '_' ).at( 1 );
  }

  QStringList regionList = KHolidays::HolidayRegion::regionCodes();

  QString countryAndLanguageMatch, countryOnlyMatch, subdivisionAndLanguageMatch,
          subdivisionOnlyMatch, languageCountryAndLanguageMatch, languageCountryOnlyMatch,
          languageSubdivisionAndLanguageMatch, languageSubdivisionOnlyMatch;

  foreach ( const QString &regionCode, regionList ) {
    QString regionCountry = KHolidays::HolidayRegion::countryCode( regionCode ).toLower();
    QString regionSubdivisionCountry;
    if ( regionCountry.split( '-' ).count() > 1 ) {
      regionSubdivisionCountry = regionCountry.split( '-' ).at( 0 );
    }
    QString regionLanguage = KHolidays::HolidayRegion::languageCode( regionCode ).toLower();

    if ( regionCountry == localeCountry && regionLanguage == localeLanguage ) {
      countryAndLanguageMatch = regionCode;
      break; // exact match so don't look further
    } else if ( regionCountry == localeCountry ) {
      if ( countryOnlyMatch.isEmpty() ) {
        countryOnlyMatch = regionCode;
      }
    } else if ( !regionSubdivisionCountry.isEmpty() &&
                regionSubdivisionCountry == localeCountry &&
                regionLanguage == localeLanguage ) {
      if ( subdivisionAndLanguageMatch.isEmpty() ) {
        subdivisionAndLanguageMatch = regionCode;
      }
    } else if ( !regionSubdivisionCountry.isEmpty() && regionSubdivisionCountry == localeCountry ) {
      if ( subdivisionOnlyMatch.isEmpty() ) {
        subdivisionOnlyMatch = regionCode;
      }
    } else if ( !localeLanguageCountry.isEmpty() &&
                regionCountry == localeLanguageCountry &&
                regionLanguage == localeLanguage ) {
      if ( languageCountryAndLanguageMatch.isEmpty() ) {
        languageCountryAndLanguageMatch = regionCode;
      }
    } else if ( !localeLanguageCountry.isEmpty() && regionCountry == localeLanguageCountry ) {
      if ( languageCountryOnlyMatch.isEmpty() ) {
        languageCountryOnlyMatch = regionCode;
      }
    } else if ( !regionSubdivisionCountry.isEmpty() &&
                !localeLanguageCountry.isEmpty() &&
                regionSubdivisionCountry == localeLanguageCountry &&
                regionLanguage == localeLanguage ) {
      if ( languageSubdivisionAndLanguageMatch.isEmpty() ) {
        languageSubdivisionAndLanguageMatch = regionCode;
      }
    } else if ( !regionSubdivisionCountry.isEmpty() &&
                !localeLanguageCountry.isEmpty() &&
                regionSubdivisionCountry == localeLanguageCountry ) {
      if ( languageSubdivisionOnlyMatch.isEmpty() ) {
        languageSubdivisionOnlyMatch = regionCode;
      }
    }
  }

  QString defaultRegionCode;

  if ( !countryAndLanguageMatch.isEmpty() ) {
    defaultRegionCode = countryAndLanguageMatch;
  } else if ( !countryOnlyMatch.isEmpty() ) {
    defaultRegionCode = countryOnlyMatch;
  } else if ( !subdivisionAndLanguageMatch.isEmpty() ) {
    defaultRegionCode = subdivisionAndLanguageMatch;
  } else if ( !subdivisionOnlyMatch.isEmpty() ) {
    defaultRegionCode = subdivisionOnlyMatch;
  } else if ( !languageCountryAndLanguageMatch.isEmpty() ) {
    defaultRegionCode = languageCountryAndLanguageMatch;
  } else if ( !languageCountryOnlyMatch.isEmpty() ) {
    defaultRegionCode = languageCountryOnlyMatch;
  } else if ( !languageSubdivisionAndLanguageMatch.isEmpty() ) {
    defaultRegionCode = languageSubdivisionAndLanguageMatch;
  } else if ( !languageSubdivisionOnlyMatch.isEmpty() ) {
    defaultRegionCode = languageSubdivisionOnlyMatch;
  }

  return defaultRegionCode;
}