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 #include <kdebug.h>
00025
00026 #include "kholidays.h"
00027 #include "kholidays_version.h"
00028
00029 extern "C" {
00030 char *parse_holidays( const char *, int year, short force );
00032 struct holiday {
00033 char *string;
00034 int color;
00035 unsigned short dup;
00036 holiday *next;
00037 };
00038 extern struct holiday holidays[366];
00039 }
00040
00041 QStringList KHolidays::locations()
00042 {
00043 QStringList files =
00044 KGlobal::dirs()->findAllResources( "data", "libkholidays/holiday_*",
00045 false, true );
00046 QStringList locs;
00047
00048 QStringList::ConstIterator it;
00049 for ( it = files.begin(); it != files.end(); ++it )
00050 locs.append( (*it).mid((*it).findRev('_') + 1) );
00051
00052 return locs;
00053 }
00054
00055 KHolidays::KHolidays( const QString& location )
00056 : mLocation( location )
00057 {
00058 mHolidayFile = locate( "data", "libkholidays/holiday_" + location );
00059
00060 mYearLast = 0;
00061 }
00062
00063 KHolidays::~KHolidays()
00064 {
00065 }
00066
00067 QString KHolidays::location() const
00068 {
00069 return mLocation;
00070 }
00071
00072 QString KHolidays::shortText( const QDate &date )
00073 {
00074 QValueList<KHoliday> lst = getHolidays( date );
00075 if ( !lst.isEmpty() )
00076 return lst.first().text;
00077 else return QString::null;
00078 }
00079
00080 bool KHolidays::parseFile( const QDate &date )
00081 {
00082
00083 int lastYear = 0;
00084
00085 if ( mHolidayFile.isNull() || mHolidayFile.isEmpty() || date.isNull() || !date.isValid() )
00086 return false;
00087
00088 if ( ( date.year() != mYearLast ) || ( mYearLast == 0 ) ) {
00089
00090 mYearLast = date.year();
00091 lastYear = date.year() - 1900;
00092 parse_holidays( QFile::encodeName( mHolidayFile ), lastYear, 1 );
00093 }
00094
00095 return true;
00096 }
00097
00098 QString KHolidays::getHoliday( const QDate &date )
00099 {
00100 QValueList<KHoliday> lst = getHolidays( date );
00101 if ( !lst.isEmpty() )
00102 return lst.first().text;
00103 else return QString::null;
00104 }
00105
00106 QValueList<KHoliday> KHolidays::getHolidays( const QDate &date )
00107 {
00108 QValueList<KHoliday> list;
00109 if ( !parseFile( date ) ) return list;
00110 struct holiday *hd = &holidays[date.dayOfYear()-1];
00111 while ( hd ) {
00112 if ( hd->string ) {
00113 KHoliday holiday;
00114 holiday.text = QString::fromUtf8( hd->string );
00115 holiday.shortText = holiday.text;
00116 holiday.Category = (hd->color == 2) || (hd->color == 9) ? HOLIDAY : WORKDAY;
00117 list.append( holiday );
00118 }
00119 hd = hd->next;
00120 }
00121 return list;
00122 }
00123
00124 int KHolidays::category( const QDate &date )
00125 {
00126 if ( !parseFile(date) ) return WORKDAY;
00127
00128 return (holidays[date.dayOfYear()-1].color == 2) ||
00129 (holidays[date.dayOfYear()-1].color == 9) ? HOLIDAY : WORKDAY;
00130 }
|