libkholidays

kholidays.cpp
1/*
2 This file is part of KOrganizer.
3 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
4 Copyright (c) 2004 Allen Winter <winter@kde.org>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19*/
20
21#include <tqfile.h>
22#include <tdeapplication.h>
23#include <tdestandarddirs.h>
24#include <kdebug.h>
25
26#include "kholidays.h"
27#include "kholidays_version.h"
28
29extern "C" {
30 char *parse_holidays( const char *, int year, short force );
32 struct holiday {
33 char *string; /* name of holiday, 0=not a holiday */
34 int color; /* color code, see scanholiday.lex */
35 unsigned short dup; /* reference count */
36 holiday *next; /* single-linked list if more than one holida appears on a given date */
37 };
38 extern struct holiday holidays[366];
39}
40
41TQStringList KHolidays::locations()
42{
43 TQStringList files =
44 TDEGlobal::dirs()->findAllResources( "data", "libkholidays/" + generateFileName( "*" ),
45 false, true );
46 TQStringList locs;
47
48 TQStringList::ConstIterator it;
49 for ( it = files.begin(); it != files.end(); ++it )
50 locs.append( (*it).mid((*it).findRev('_') + 1) );
51
52 return locs;
53}
54
55TQString KHolidays::fileForLocation( const TQString &location )
56{
57 return locate( "data", "libkholidays/" + generateFileName( location ) );
58}
59
60TQString KHolidays::userPath( bool create )
61{
62 return TDEGlobal::dirs()->saveLocation( "data", "libkholidays/", create );
63}
64
65TQString KHolidays::generateFileName( const TQString &location )
66{
67 return "holiday_" + location;
68}
69
70
71
72
73KHolidays::KHolidays( const TQString& location )
74 : mLocation( location )
75{
76 mHolidayFile = fileForLocation( location );
77
78 mYearLast = 0;
79}
80
81KHolidays::~KHolidays()
82{
83}
84
85TQString KHolidays::location() const
86{
87 return mLocation;
88}
89
90TQString KHolidays::shortText( const TQDate &date )
91{
92 TQValueList<KHoliday> lst = getHolidays( date );
93 if ( !lst.isEmpty() )
94 return lst.first().text;
95 else return TQString();
96}
97
98bool KHolidays::parseFile( const TQDate &date )
99{
100// kdDebug()<<"KHolidays::parseFile( date=" << date << ")"<<endl;
101 int lastYear = 0; //current year less 1900
102
103 if ( mHolidayFile.isNull() || mHolidayFile.isEmpty() || date.isNull() || !date.isValid() )
104 return false;
105
106 if ( ( date.year() != mYearLast ) || ( mYearLast == 0 ) ) {
107// kdDebug()<<kdBacktrace();
108 mYearLast = date.year();
109 lastYear = date.year() - 1900; // silly parse_year takes 2 digit year...
110 parse_holidays( TQFile::encodeName( mHolidayFile ), lastYear, 1 );
111 }
112
113 return true;
114}
115
116TQString KHolidays::getHoliday( const TQDate &date )
117{
118 TQValueList<KHoliday> lst = getHolidays( date );
119 if ( !lst.isEmpty() )
120 return lst.first().text;
121 else return TQString();
122}
123
124TQValueList<KHoliday> KHolidays::getHolidays( const TQDate &date )
125{
126 TQValueList<KHoliday> list;
127 if ( !date.isValid() ) {
128 return list;
129 }
130
131 if ( !parseFile( date ) ) return list;
132 struct holiday *hd = &holidays[date.dayOfYear()-1];
133 while ( hd ) {
134 if ( hd->string ) {
135 KHoliday holiday;
136 holiday.text = TQString::fromUtf8( hd->string );
137 holiday.shortText = holiday.text;
138 holiday.Category = (hd->color == 2/*red*/) || (hd->color == 9/*weekend*/) ? HOLIDAY : WORKDAY;
139 list.append( holiday );
140 }
141 hd = hd->next;
142 }
143 return list;
144}
145
146int KHolidays::category( const TQDate &date )
147{
148 if ( !parseFile(date) ) return WORKDAY;
149
150 return (holidays[date.dayOfYear()-1].color == 2/*red*/) ||
151 (holidays[date.dayOfYear()-1].color == 9/*weekend*/) ? HOLIDAY : WORKDAY;
152}