libkholidays
kholidays.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <qfile.h>
00022 #include <kapplication.h>
00023 #include <kstandarddirs.h>
00024
00025 #include "kholidays.h"
00026 #include "kholidays_version.h"
00027
00028 extern "C" {
00029 char *parse_holidays( const char *, int year, short force );
00031 struct holiday {
00032 char *string;
00033 int color;
00034 unsigned short dup;
00035 };
00036 extern struct holiday holiday[366];
00037 }
00038
00039 QStringList KHolidays::locations()
00040 {
00041 QStringList files =
00042 KGlobal::dirs()->findAllResources( "data", "libkholidays/holiday_*",
00043 false, true );
00044 QStringList locs;
00045
00046 QStringList::ConstIterator it;
00047 for ( it = files.begin(); it != files.end(); ++it )
00048 locs.append( (*it).mid((*it).findRev('_') + 1) );
00049
00050 return locs;
00051 }
00052
00053 KHolidays::KHolidays( const QString& location )
00054 : mLocation( location )
00055 {
00056 mHolidayFile = locate( "data", "libkholidays/holiday_" + location );
00057
00058 mYearLast = 0;
00059 }
00060
00061 KHolidays::~KHolidays()
00062 {
00063 }
00064
00065 QString KHolidays::location() const
00066 {
00067 return mLocation;
00068 }
00069
00070 QString KHolidays::shortText( const QDate &date )
00071 {
00072 return getHoliday( date );
00073 }
00074
00075 bool KHolidays::parseFile( const QDate &date )
00076 {
00077 int lastYear = 0;
00078
00079 if ( mHolidayFile.isNull() || mHolidayFile.isEmpty() || date.isNull() )
00080 return false;
00081
00082 if ( ( mYearLast == 0 ) || ( date.year() != mYearLast ) ) {
00083 mYearLast = date.year();
00084 lastYear = date.year() - 1900;
00085 parse_holidays( QFile::encodeName( mHolidayFile ), lastYear, 1 );
00086 }
00087
00088 return true;
00089 }
00090
00091 QString KHolidays::getHoliday( const QDate &date )
00092 {
00093 if ( !parseFile( date ) ) return QString::null;
00094
00095 if ( holiday[date.dayOfYear()-1].string ) {
00096 return QString::fromUtf8( holiday[date.dayOfYear()-1].string );
00097 } else {
00098 return QString::null;
00099 }
00100 }
00101
00102 int KHolidays::category( const QDate &date )
00103 {
00104 if ( !parseFile(date) ) return WORKDAY;
00105
00106 return (holiday[date.dayOfYear()-1].color == 2) ||
00107 (holiday[date.dayOfYear()-1].color == 9) ? HOLIDAY : WORKDAY;
00108 }
|