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 <kstandarddirs.h>
24 #include <kdebug.h>
25 
26 #include "kholidays.h"
27 
28 extern "C" {
29  char *parse_holidays( const char *, int year, short force );
31  struct holiday {
32  char *string; /* name of holiday, 0=not a holiday */
33  int color; /* color code, see scanholiday.lex */
34  unsigned short dup; /* reference count */
35  holiday *next; /* single-linked list if more than one holida appears on a given date */
36  };
37  extern struct holiday holidays[366];
38 }
39 
40 TQStringList KHolidays::locations()
41 {
42  TQStringList files =
43  TDEGlobal::dirs()->findAllResources( "data", "libkholidays/" + generateFileName( "*" ),
44  false, true );
45  TQStringList locs;
46 
47  TQStringList::ConstIterator it;
48  for ( it = files.begin(); it != files.end(); ++it )
49  locs.append( (*it).mid((*it).findRev('_') + 1) );
50 
51  return locs;
52 }
53 
54 TQString KHolidays::fileForLocation( const TQString &location )
55 {
56  return locate( "data", "libkholidays/" + generateFileName( location ) );
57 }
58 
59 TQString KHolidays::userPath( bool create )
60 {
61  return TDEGlobal::dirs()->saveLocation( "data", "libkholidays/", create );
62 }
63 
64 TQString KHolidays::generateFileName( const TQString &location )
65 {
66  return "holiday_" + location;
67 }
68 
69 
70 
71 
72 KHolidays::KHolidays( const TQString& location )
73  : mLocation( location )
74 {
75  mHolidayFile = fileForLocation( location );
76 
77  mYearLast = 0;
78 }
79 
80 KHolidays::~KHolidays()
81 {
82 }
83 
84 TQString KHolidays::location() const
85 {
86  return mLocation;
87 }
88 
89 TQString KHolidays::shortText( const TQDate &date )
90 {
91  TQValueList<KHoliday> lst = getHolidays( date );
92  if ( !lst.isEmpty() )
93  return lst.first().text;
94  else return TQString();
95 }
96 
97 bool KHolidays::parseFile( const TQDate &date )
98 {
99 // kdDebug()<<"KHolidays::parseFile( date=" << date << ")"<<endl;
100  int lastYear = 0; //current year less 1900
101 
102  if ( mHolidayFile.isNull() || mHolidayFile.isEmpty() || date.isNull() || !date.isValid() )
103  return false;
104 
105  if ( ( date.year() != mYearLast ) || ( mYearLast == 0 ) ) {
106 // kdDebug()<<kdBacktrace();
107  mYearLast = date.year();
108  lastYear = date.year() - 1900; // silly parse_year takes 2 digit year...
109  parse_holidays( TQFile::encodeName( mHolidayFile ), lastYear, 1 );
110  }
111 
112  return true;
113 }
114 
115 TQString KHolidays::getHoliday( const TQDate &date )
116 {
117  TQValueList<KHoliday> lst = getHolidays( date );
118  if ( !lst.isEmpty() )
119  return lst.first().text;
120  else return TQString();
121 }
122 
123 TQValueList<KHoliday> KHolidays::getHolidays( const TQDate &date )
124 {
125  TQValueList<KHoliday> list;
126  if ( !date.isValid() ) {
127  return list;
128  }
129 
130  if ( !parseFile( date ) ) return list;
131  struct holiday *hd = &holidays[date.dayOfYear()-1];
132  while ( hd ) {
133  if ( hd->string ) {
134  KHoliday holiday;
135  holiday.text = TQString::fromUtf8( hd->string );
136  holiday.shortText = holiday.text;
137  holiday.Category = (hd->color == 2/*red*/) || (hd->color == 9/*weekend*/) ? HOLIDAY : WORKDAY;
138  list.append( holiday );
139  }
140  hd = hd->next;
141  }
142  return list;
143 }
144 
145 int KHolidays::category( const TQDate &date )
146 {
147  if ( !parseFile(date) ) return WORKDAY;
148 
149  return (holidays[date.dayOfYear()-1].color == 2/*red*/) ||
150  (holidays[date.dayOfYear()-1].color == 9/*weekend*/) ? HOLIDAY : WORKDAY;
151 }