KHolidays Library
holidays.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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;
00039 int color;
00040 unsigned short dup;
00041 holiday *next;
00042
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;
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;
00131 parse_holidays( QFile::encodeName( mHolidayFile ), lastYear, 1 );
00132 }
00133
00134 return true;
00135 }
00136
00137 QString mLocation;
00138 QString mHolidayFile;
00139 mutable int mYearLast;
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 ) || ( hd->color == 9 ) ?
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 || hd->color == 9 ) ) {
00211 return true;
00212 }
00213 hd = hd->next;
00214 }
00215 return false;
00216 }