• Skip to content
  • Skip to link menu
KDE 4.5 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

KHolidays Library

holidayregion.cpp

00001 /*
00002   This file is part of the kholidays library.
00003 
00004   Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
00005   Copyright (c) 2004 Allen Winter <winter@kde.org>
00006   Copyright (c) 2008 David Jarvie <djarvie@kde.org>
00007   Copyright 2010 John Layt <john@layt.net>
00008 
00009   This library is free software; you can redistribute it and/or
00010   modify it under the terms of the GNU Library General Public
00011   License as published by the Free Software Foundation; either
00012   version 2 of the License, or (at your option) any later version.
00013 
00014   This library is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017   GNU Library General Public License for more details.
00018 
00019   You should have received a copy of the GNU Library General Public License
00020   along with this library; see the file COPYING.LIB.  If not, write to the
00021   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00022   Boston, MA 02110-1301, USA.
00023 */
00024 
00025 #include "holidayregion.h"
00026 
00027 #include <QtCore/QDateTime>
00028 #include <QtCore/QFile>
00029 #include <QtCore/QSharedData>
00030 #include <QtCore/QFileInfo>
00031 
00032 #include <KStandardDirs>
00033 #include <KGlobal>
00034 #include <KLocale>
00035 #include <KDebug>
00036 
00037 #include "holiday_p.h"
00038 #include "parsers/plan2/holidayparserdriverplan_p.h"
00039 
00040 using namespace KHolidays;
00041 
00042 class HolidayRegion::Private
00043 {
00044   public:
00045     Private( const QString &regionCode ) : mDriver( 0 ),
00046                                            mRegionCode( regionCode )
00047     {
00048       if ( !mRegionCode.isEmpty() ) {
00049 
00050         if ( mRegionCode.length() == 2 ) { //Backwards compatible mode for old location code
00051           mLocation = mRegionCode;
00052           QStringList locationFiles = KGlobal::dirs()->findAllResources( "data",
00053                                                                          "libkholidays/plan2/holiday_" + mLocation + "*",
00054                                                                          KStandardDirs::NoDuplicates );
00055           if ( locationFiles.count() > 0 ) {
00056             mRegionCode = locationFiles.at( 0 ).mid( locationFiles.at( 0 ).lastIndexOf( "holiday_" ) + 8 );
00057           }
00058         }
00059 
00060         mHolidayFile.setFile( KStandardDirs::locate( "data", "libkholidays/plan2/holiday_" + mRegionCode ) );
00061       }
00062 
00063       init();
00064     }
00065 
00066     Private( const QFileInfo &regionFile ) : mDriver( 0 ),
00067                                              mHolidayFile( regionFile )
00068     {
00069       init();
00070     }
00071 
00072     ~Private()
00073     {
00074       delete mDriver;
00075     }
00076 
00077     void init()
00078     {
00079       if ( mHolidayFile.exists() ) {
00080         mDriver = new HolidayParserDriverPlan( mHolidayFile.absoluteFilePath() );
00081         if ( mDriver ) {
00082 
00083           if ( mLocation.isEmpty() ) {
00084             mLocation = mDriver->fileCountryCode().left( 2 );
00085           }
00086 
00087           if ( mRegionCode.isEmpty() ) {
00088             if ( mHolidayFile.fileName().startsWith( QLatin1String( "holiday_" ) ) ) {
00089               mRegionCode = mHolidayFile.fileName().mid( 8 );
00090             } else {
00091               mRegionCode = mHolidayFile.fileName();
00092             }
00093           }
00094 
00095         } else {
00096           mRegionCode.clear();
00097           mLocation.clear();
00098         }
00099       } else {
00100         mRegionCode.clear();
00101         mLocation.clear();
00102       }
00103     }
00104 
00105     HolidayParserDriver  *mDriver;  // The parser driver for the holiday file
00106     QString mRegionCode;            // region code of holiday region
00107     QString mLocation;              // old location code, use now deprecated
00108     QFileInfo mHolidayFile;         // file containing holiday data, or null
00109 };
00110 
00111 HolidayRegion::HolidayRegion( const QString &regionCode )
00112   : d( new Private( regionCode ) )
00113 {
00114 }
00115 
00116 HolidayRegion::HolidayRegion( const QFileInfo &regionFile )
00117              : d( new Private( regionFile ) )
00118 {
00119 }
00120 
00121 HolidayRegion::~HolidayRegion()
00122 {
00123   delete d;
00124 }
00125 
00126 QStringList HolidayRegion::locations()
00127 {
00128   const QStringList files =
00129     KGlobal::dirs()->findAllResources( "data", "libkholidays/plan2/holiday_*",
00130                                        KStandardDirs::NoDuplicates );
00131 
00132   QStringList locations;
00133   foreach ( const QString &filename, files ) {
00134     locations.append( filename.mid( filename.lastIndexOf( "holiday_" ) + 8, 2 ) );
00135   }
00136 
00137   locations.removeDuplicates();
00138   qSort( locations );
00139   return locations;
00140 }
00141 
00142 QString HolidayRegion::location() const
00143 {
00144   return d->mLocation;
00145 }
00146 
00147 QStringList HolidayRegion::regionCodes()
00148 {
00149   const QStringList files =
00150     KGlobal::dirs()->findAllResources( "data", "libkholidays/plan2/holiday_*",
00151                                        KStandardDirs::NoDuplicates );
00152 
00153   QStringList regionCodesList;
00154   foreach ( const QString &filename, files ) {
00155     regionCodesList.append( filename.mid( filename.lastIndexOf( "holiday_" ) + 8 ) );
00156   }
00157 
00158   qSort( regionCodesList );
00159   return regionCodesList;
00160 }
00161 
00162 QString HolidayRegion::regionCode() const
00163 {
00164   return d->mRegionCode;
00165 }
00166 
00167 QString HolidayRegion::countryCode() const
00168 {
00169   return d->mDriver->fileCountryCode();
00170 }
00171 
00172 QString HolidayRegion::countryCode( const QString &regionCode )
00173 {
00174   HolidayRegion temp = HolidayRegion( regionCode );
00175   if ( temp.isValid() ) {
00176     return temp.countryCode();
00177   } else {
00178     return QString();
00179   }
00180 }
00181 
00182 QString HolidayRegion::languageCode() const
00183 {
00184   return d->mDriver->fileLanguageCode();
00185 }
00186 
00187 QString HolidayRegion::languageCode( const QString &regionCode )
00188 {
00189   HolidayRegion temp = HolidayRegion( regionCode );
00190   if ( temp.isValid() ) {
00191     return temp.languageCode();
00192   } else {
00193     return QString();
00194   }
00195 }
00196 
00197 QString HolidayRegion::name() const
00198 {
00199   QString tempName = d->mDriver->fileName();
00200 
00201   if ( tempName.isEmpty() ) {
00202     QStringList countryParts = countryCode().toLower().split( '-' );
00203     QString country = countryParts.at( 0 );
00204     QString regionName, typeName;
00205 
00206     if ( country != "xx" ) {
00207       if ( countryParts.count() == 2 ) {
00208         // Temporary measure to get regions translated, only those files that already exist
00209         // In 4.6 hope to have isocodes project integration for translations via KLocale
00210         QString subdivision = countryParts.at( 1 );
00211         if ( country == "ca" && subdivision == "qc" ) {
00212           regionName = i18nc( "Canadian region", "Quebec" );
00213         } else if ( country == "de" && subdivision == "by" ) {
00214           regionName = i18nc( "German region", "Bavaria" );
00215         } else if ( country == "es" && subdivision == "ct" ) {
00216           regionName = i18nc( "Spanish region", "Catalonia" );
00217         } else if ( country == "gb" && subdivision == "eaw" ) {
00218           regionName = i18nc( "UK Region", "England and Wales" );
00219         } else if ( country == "gb" && subdivision == "eng" ) {
00220           regionName = i18nc( "UK Region", "England" );
00221         } else if ( country == "gb" && subdivision == "wls" ) {
00222           regionName = i18nc( "UK Region", "Wales" );
00223         } else if ( country == "gb" && subdivision == "sct" ) {
00224           regionName = i18nc( "UK Region", "Scotland" );
00225         } else if ( country == "gb" && subdivision == "nir" ) {
00226           regionName = i18nc( "UK Region", "Northern Ireland" );
00227         } else if ( country == "it" && subdivision == "bz" ) {
00228           regionName = i18nc( "Italian Region", "South Tyrol" );
00229         } else if ( country == "au" && subdivision == "nsw" ) {
00230           regionName = i18nc( "Australian Region", "New South Wales" );
00231         } else if ( country == "au" && subdivision == "qld" ) {
00232           regionName = i18nc( "Australian Region", "Queensland" );
00233         } else if ( country == "au" && subdivision == "vic" ) {
00234           regionName = i18nc( "Australian Region", "Victoria" );
00235         } else if ( country == "au" && subdivision == "sa" ) {
00236           regionName = i18nc( "Australian Region", "South Australia" );
00237         } else if ( country == "au" && subdivision == "nt" ) {
00238           regionName = i18nc( "Australian Region", "Northern Territory" );
00239         } else if ( country == "au" && subdivision == "act" ) {
00240           regionName = i18nc( "Australian Region", "Australian Capital Territory" );
00241         } else if ( country == "au" && subdivision == "wa" ) {
00242           regionName = i18nc( "Australian Region", "Western Australia" );
00243         } else if ( country == "au" && subdivision == "tas" ) {
00244           regionName = i18nc( "Australian Region", "Tasmania" );
00245         } else {
00246           regionName = KGlobal::locale()->countryCodeToName( country );
00247         }
00248       } else {
00249           regionName = KGlobal::locale()->countryCodeToName( country );
00250       }
00251     }
00252 
00253     //Cheat on type for now,take direct from region code until API is introduced in SC 4.6
00254     QStringList regionParts = regionCode().toLower().split( '_' );
00255     if ( regionParts.count() == 3 ) {
00256       QString type = regionParts.at( 2 );
00257       // Will create lots more in 4.6
00258       // Religious types, just simple for now
00259       if ( type == "public" ) {
00260         typeName = i18nc( "Holiday type", "Public" );
00261       } else if ( type == "religious" ) {
00262         typeName = i18nc( "Holiday type", "Religious" );
00263       } else if ( type == "financial" ) {
00264         typeName = i18nc( "Holiday type", "Financial" );
00265       } else if ( type == "cultural" ) {
00266         typeName = i18nc( "Holiday type", "Cultural" );
00267       } else if ( type == "school" ) {
00268         typeName = i18nc( "Holiday type", "School" );
00269       } else if ( type == "seasons" ) {
00270         typeName = i18nc( "Holiday type", "Seasons" );
00271       } else if ( type == "name" ) {
00272         typeName = i18nc( "Holiday type", "Name Days" );
00273       } else if ( type == "personal" ) {
00274         typeName = i18nc( "Holiday type", "Personal" );
00275       } else if ( type == "catholic" ) {
00276         typeName = i18nc( "Holiday type", "Catholic" );
00277       } else if ( type == "protestant" ) {
00278         typeName = i18nc( "Holiday type", "Protestant" );
00279       } else if ( type == "orthodox" ) {
00280         typeName = i18nc( "Holiday type", "Orthodox" );
00281       } else if ( type == "jewish" ) {
00282         typeName = i18nc( "Holiday type", "Jewish" );
00283       } else if ( type == "islamic" ) {
00284         typeName = i18nc( "Holiday type", "Islamic" );
00285       }
00286     }
00287 
00288     if ( !regionName.isEmpty() ) {
00289       if ( !typeName.isEmpty() ) {
00290         //TODO translate when not frozen
00291         tempName = QString( "%1 - %2" ).arg( regionName ).arg( typeName );
00292       } else {
00293         tempName = regionName;
00294       }
00295     } else if ( !typeName.isEmpty() ) {
00296       tempName = typeName;
00297     } else {
00298       tempName = i18nc( "Unknown holiday region", "Unknown" );
00299     }
00300   }
00301   return tempName;
00302 }
00303 
00304 QString HolidayRegion::name( const QString &regionCode )
00305 {
00306   HolidayRegion temp = HolidayRegion( regionCode );
00307   if ( temp.isValid() ) {
00308     return temp.name();
00309   } else {
00310     return QString();
00311   }
00312 }
00313 
00314 QString HolidayRegion::description() const
00315 {
00316   return d->mDriver->fileDescription();
00317 }
00318 
00319 QString HolidayRegion::description( const QString &regionCode )
00320 {
00321   HolidayRegion temp = HolidayRegion( regionCode );
00322   if ( temp.isValid() ) {
00323     return temp.description();
00324   } else {
00325     return QString();
00326   }
00327 }
00328 
00329 bool HolidayRegion::isValid() const
00330 {
00331   return d->mHolidayFile.exists() && d->mDriver;
00332 }
00333 
00334 bool HolidayRegion::isValid( const QString &regionCode )
00335 {
00336   HolidayRegion temp = HolidayRegion( regionCode );
00337   return temp.isValid();
00338 }
00339 
00340 Holiday::List HolidayRegion::holidays( const QDate &startDate, const QDate &endDate ) const
00341 {
00342   if ( isValid() ) {
00343     return d->mDriver->parseHolidays( startDate, endDate );
00344   } else {
00345     return Holiday::List();
00346   }
00347 }
00348 
00349 Holiday::List HolidayRegion::holidays( const QDate &date ) const
00350 {
00351   if ( isValid() ) {
00352     return d->mDriver->parseHolidays( date );
00353   } else {
00354     return Holiday::List();
00355   }
00356 }
00357 
00358 Holiday::List HolidayRegion::holidays( int calendarYear, const QString &calendarType ) const
00359 {
00360   if ( isValid() ) {
00361     return d->mDriver->parseHolidays( calendarYear, calendarType );
00362   } else {
00363     return Holiday::List();
00364   }
00365 }
00366 
00367 bool HolidayRegion::isHoliday( const QDate &date ) const
00368 {
00369   Holiday::List holidayList = holidays( date );
00370   if ( holidayList.count() > 0 ) {
00371     foreach ( const KHolidays::Holiday &holiday, holidayList ) {
00372       if ( holiday.dayType() == Holiday::NonWorkday ) {
00373         return true;
00374       }
00375     }
00376   }
00377   return false;
00378 }

KHolidays Library

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

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kblog
  • kcal
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.7.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal