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

KHolidays Library

holidays.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 
00008   This library is free software; you can redistribute it and/or
00009   modify it under the terms of the GNU Library General Public
00010   License as published by the Free Software Foundation; either
00011   version 2 of the License, or (at your option) any later version.
00012 
00013   This library is distributed in the hope that it will be useful,
00014   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016   GNU Library General Public License for more details.
00017 
00018   You should have received a copy of the GNU Library General Public License
00019   along with this library; see the file COPYING.LIB.  If not, write to the
00020   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00021   Boston, MA 02110-1301, USA.
00022 */
00023 
00024 #include "holidays.h"
00025 
00026 #include <KStandardDirs>
00027 
00028 #include <QtCore/QDateTime>
00029 #include <QtCore/QFile>
00030 #include <QtCore/QSharedData>
00031 
00032 using namespace KHolidays;
00033 
00034 extern "C" {
00035   char *parse_holidays( const char *, int year, short force );
00037   struct holiday {
00038     char            *string;   /* name of holiday, 0=not a holiday */
00039     int             color;     /* color code, see scanholiday.lex */
00040     unsigned short  dup;       /* reference count */
00041     holiday         *next;     /* single-linked list if more than one holiday
00042                                   appears on a given date */
00043   };
00044   extern struct holiday holidays[366];
00045 }
00046 
00047 class KHolidays::HolidayPrivate : public QSharedData
00048 {
00049   public:
00050     HolidayPrivate()
00051     {
00052     }
00053 
00054     HolidayPrivate( const HolidayPrivate &other )
00055       : QSharedData( other )
00056     {
00057       mText = other.mText;
00058       mShortText = other.mShortText;
00059       mDayType = other.mDayType;
00060     }
00061 
00062     QString mText;
00063     QString mShortText;
00064     Holiday::DayType mDayType;
00065 };
00066 
00067 Holiday::Holiday()
00068   : d( new HolidayPrivate )
00069 {
00070 }
00071 
00072 Holiday::Holiday( const Holiday &other )
00073   : d( other.d )
00074 {
00075 }
00076 
00077 Holiday::~Holiday()
00078 {
00079 }
00080 
00081 Holiday &Holiday::operator=( const Holiday &other )
00082 {
00083   if ( &other != this ) {
00084     d = other.d;
00085   }
00086 
00087   return *this;
00088 }
00089 
00090 QString Holiday::text() const
00091 {
00092   return d->mText;
00093 }
00094 
00095 QString Holiday::shortText() const
00096 {
00097   return d->mShortText;
00098 }
00099 
00100 Holiday::DayType Holiday::dayType() const
00101 {
00102   return d->mDayType;
00103 }
00104 
00105 class HolidayRegion::Private
00106 {
00107   public:
00108     Private( const QString &location )
00109       : mLocation( location ),
00110         mYearLast( 0 )
00111     {
00112       if ( !mLocation.isEmpty() ) {
00113         mHolidayFile = KStandardDirs::locate( "data", "libkholidays/holiday_" + mLocation );
00114         if ( mHolidayFile.isEmpty() ) {
00115           mLocation.clear();
00116         }
00117       }
00118     }
00119 
00120     bool parseFile( const QDate &date ) const
00121     {
00122       int lastYear = 0; //current year less 1900
00123 
00124       if ( mHolidayFile.isEmpty() || !date.isValid() ) {
00125         return false;
00126       }
00127 
00128       if ( ( date.year() != mYearLast ) || ( mYearLast == 0 ) ) {
00129         mYearLast = date.year();
00130         lastYear = date.year() - 1900; // silly parse_year takes 2 digit year...
00131         parse_holidays( QFile::encodeName( mHolidayFile ), lastYear, 1 );
00132       }
00133 
00134       return true;
00135     }
00136 
00137     QString mLocation;     // location string used to determine holidays file
00138     QString mHolidayFile;  // full path of file containing holiday data, or null
00139     mutable int mYearLast; // save of the last year we have seen
00140 };
00141 
00142 HolidayRegion::HolidayRegion( const QString &location )
00143   : d( new Private( location ) )
00144 {
00145 }
00146 
00147 HolidayRegion::~HolidayRegion()
00148 {
00149   delete d;
00150 }
00151 
00152 QStringList HolidayRegion::locations()
00153 {
00154   const QStringList files =
00155     KGlobal::dirs()->findAllResources( "data", "libkholidays/holiday_*",
00156                                        KStandardDirs::NoDuplicates );
00157   QStringList locs;
00158 
00159   QStringList::ConstIterator it;
00160   for ( it = files.constBegin(); it != files.constEnd(); ++it ) {
00161     locs.append( (*it).mid( (*it).lastIndexOf( '_' ) + 1 ) );
00162   }
00163 
00164   return locs;
00165 }
00166 
00167 QString HolidayRegion::location() const
00168 {
00169   return d->mLocation;
00170 }
00171 
00172 bool HolidayRegion::isValid() const
00173 {
00174   return !d->mHolidayFile.isEmpty();
00175 }
00176 
00177 Holiday::List HolidayRegion::holidays( const QDate &date ) const
00178 {
00179   Holiday::List list;
00180   if ( !date.isValid() ) {
00181     return list;
00182   }
00183 
00184   if ( !d->parseFile( date ) ) {
00185     return list;
00186   }
00187 
00188   struct holiday *hd = &::holidays[date.dayOfYear()-1];
00189   while ( hd ) {
00190     if ( hd->string ) {
00191       Holiday holiday;
00192       holiday.d->mText = QString::fromUtf8( hd->string );
00193       holiday.d->mShortText = holiday.d->mText;
00194       holiday.d->mDayType = ( hd->color == 2/*red*/ ) || ( hd->color == 9/*weekend*/ ) ?
00195                               Holiday::NonWorkday : Holiday::Workday;
00196       list.append( holiday );
00197     }
00198     hd = hd->next;
00199   }
00200   return list;
00201 }
00202 
00203 bool HolidayRegion::isHoliday( const QDate &date ) const
00204 {
00205   if ( !d->parseFile( date ) ) {
00206     return false;
00207   }
00208   struct holiday *hd = &::holidays[date.dayOfYear()-1];
00209   while ( hd ) {
00210     if ( hd->string && ( hd->color == 2/*red*/ || hd->color == 9/*weekend*/ ) ) {
00211       return true;
00212     }
00213     hd = hd->next;
00214   }
00215   return false;
00216 }

KHolidays Library

Skip menu "KHolidays Library"
  • Main Page
  • 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.6.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