libkholidays
kholidays.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <tqfile.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 TQStringList KHolidays::locations()
00042 {
00043 TQStringList files =
00044 KGlobal::dirs()->findAllResources( "data", "libkholidays/" + generateFileName( "*" ),
00045 false, true );
00046 TQStringList locs;
00047
00048 TQStringList::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 TQString KHolidays::fileForLocation( const TQString &location )
00056 {
00057 return locate( "data", "libkholidays/" + generateFileName( location ) );
00058 }
00059
00060 TQString KHolidays::userPath( bool create )
00061 {
00062 return KGlobal::dirs()->saveLocation( "data", "libkholidays/", create );
00063 }
00064
00065 TQString KHolidays::generateFileName( const TQString &location )
00066 {
00067 return "holiday_" + location;
00068 }
00069
00070
00071
00072
00073 KHolidays::KHolidays( const TQString& location )
00074 : mLocation( location )
00075 {
00076 mHolidayFile = fileForLocation( location );
00077
00078 mYearLast = 0;
00079 }
00080
00081 KHolidays::~KHolidays()
00082 {
00083 }
00084
00085 TQString KHolidays::location() const
00086 {
00087 return mLocation;
00088 }
00089
00090 TQString KHolidays::shortText( const TQDate &date )
00091 {
00092 TQValueList<KHoliday> lst = getHolidays( date );
00093 if ( !lst.isEmpty() )
00094 return lst.first().text;
00095 else return TQString::null;
00096 }
00097
00098 bool KHolidays::parseFile( const TQDate &date )
00099 {
00100
00101 int lastYear = 0;
00102
00103 if ( mHolidayFile.isNull() || mHolidayFile.isEmpty() || date.isNull() || !date.isValid() )
00104 return false;
00105
00106 if ( ( date.year() != mYearLast ) || ( mYearLast == 0 ) ) {
00107
00108 mYearLast = date.year();
00109 lastYear = date.year() - 1900;
00110 parse_holidays( TQFile::encodeName( mHolidayFile ), lastYear, 1 );
00111 }
00112
00113 return true;
00114 }
00115
00116 TQString KHolidays::getHoliday( const TQDate &date )
00117 {
00118 TQValueList<KHoliday> lst = getHolidays( date );
00119 if ( !lst.isEmpty() )
00120 return lst.first().text;
00121 else return TQString::null;
00122 }
00123
00124 TQValueList<KHoliday> KHolidays::getHolidays( const TQDate &date )
00125 {
00126 TQValueList<KHoliday> list;
00127 if ( !date.isValid() ) {
00128 return list;
00129 }
00130
00131 if ( !parseFile( date ) ) return list;
00132 struct holiday *hd = &holidays[date.dayOfYear()-1];
00133 while ( hd ) {
00134 if ( hd->string ) {
00135 KHoliday holiday;
00136 holiday.text = TQString::fromUtf8( hd->string );
00137 holiday.shortText = holiday.text;
00138 holiday.Category = (hd->color == 2) || (hd->color == 9) ? HOLIDAY : WORKDAY;
00139 list.append( holiday );
00140 }
00141 hd = hd->next;
00142 }
00143 return list;
00144 }
00145
00146 int KHolidays::category( const TQDate &date )
00147 {
00148 if ( !parseFile(date) ) return WORKDAY;
00149
00150 return (holidays[date.dayOfYear()-1].color == 2) ||
00151 (holidays[date.dayOfYear()-1].color == 9) ? HOLIDAY : WORKDAY;
00152 }
|