libkholidays

kholidays.cpp

00001 /*
00002     This file is part of KOrganizer.
00003     Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
00004     Copyright (c) 2004 Allen Winter <winter@kde.org>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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;        /* name of holiday, 0=not a holiday */
00033     int             color;          /* color code, see scanholiday.lex */
00034     unsigned short  dup;            /* reference count */
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; //current year less 1900
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; // silly parse_year takes 2 digit year...
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/*red*/) ||
00107          (holiday[date.dayOfYear()-1].color == 9/*weekend*/) ? HOLIDAY : WORKDAY;
00108 }
KDE Home | KDE Accessibility Home | Description of Access Keys