summaryrefslogtreecommitdiffstats
path: root/libkholidays/kholidays.cpp
blob: 694a32dd5d6814766159229859537686703ee16a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
    This file is part of KOrganizer.
    Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
    Copyright (c) 2004 Allen Winter <winter@kde.org>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include <tqfile.h>
#include <kapplication.h>
#include <kstandarddirs.h>
#include <kdebug.h>

#include "kholidays.h"
#include "kholidays_version.h"

extern "C" {
  char *parse_holidays( const char *, int year, short force );
  /** \internal */
  struct holiday {
    char            *string;        /* name of holiday, 0=not a holiday */
    int             color;          /* color code, see scanholiday.lex */
    unsigned short  dup;            /* reference count */
    holiday         *next;          /* single-linked list if more than one holida appears on a given date */
  };
  extern struct holiday holidays[366];
}

TQStringList KHolidays::locations()
{
  TQStringList files =
    KGlobal::dirs()->findAllResources( "data", "libkholidays/" + generateFileName( "*" ),
                                       false, true );
  TQStringList locs;

  TQStringList::ConstIterator it;
  for ( it = files.begin(); it != files.end(); ++it )
    locs.append( (*it).mid((*it).findRev('_') + 1) );

  return locs;
}

TQString KHolidays::fileForLocation( const TQString &location )
{
  return locate( "data", "libkholidays/" + generateFileName( location ) );
}

TQString KHolidays::userPath( bool create )
{
  return KGlobal::dirs()->saveLocation( "data", "libkholidays/", create );
}

TQString KHolidays::generateFileName( const TQString &location )
{
  return "holiday_" + location;
}




KHolidays::KHolidays( const TQString& location )
  : mLocation( location )
{
  mHolidayFile = fileForLocation( location );

  mYearLast = 0;
}

KHolidays::~KHolidays()
{
}

TQString KHolidays::location() const
{
  return mLocation;
}

TQString KHolidays::shortText( const TQDate &date )
{
  TQValueList<KHoliday> lst = getHolidays( date );
  if ( !lst.isEmpty() ) 
    return lst.first().text;
  else return TQString::null;
}

bool KHolidays::parseFile( const TQDate &date )
{
// kdDebug()<<"KHolidays::parseFile( date=" << date << ")"<<endl;
  int lastYear = 0; //current year less 1900

  if ( mHolidayFile.isNull() || mHolidayFile.isEmpty() || date.isNull() || !date.isValid() )
    return false;

  if ( ( date.year() != mYearLast ) || ( mYearLast == 0 ) ) {
// kdDebug()<<kdBacktrace();
    mYearLast = date.year();
    lastYear = date.year() - 1900; // silly parse_year takes 2 digit year...
    parse_holidays( TQFile::encodeName( mHolidayFile ), lastYear, 1 );
  }

  return true;
}

TQString KHolidays::getHoliday( const TQDate &date )
{
  TQValueList<KHoliday> lst = getHolidays( date );
  if ( !lst.isEmpty() ) 
    return lst.first().text;
  else return TQString::null;
}

TQValueList<KHoliday> KHolidays::getHolidays( const TQDate &date )
{
  TQValueList<KHoliday> list;
  if ( !parseFile( date ) ) return list;
  struct holiday *hd = &holidays[date.dayOfYear()-1];
  while ( hd ) {
    if ( hd->string ) {
      KHoliday holiday;
      holiday.text = TQString::fromUtf8( hd->string );
      holiday.shortText = holiday.text;
      holiday.Category = (hd->color == 2/*red*/) || (hd->color == 9/*weekend*/) ? HOLIDAY : WORKDAY;
      list.append( holiday );
    }
    hd = hd->next;
  }
  return list;
}

int KHolidays::category( const TQDate &date )
{
  if ( !parseFile(date) ) return WORKDAY;

  return (holidays[date.dayOfYear()-1].color == 2/*red*/) ||
         (holidays[date.dayOfYear()-1].color == 9/*weekend*/) ? HOLIDAY : WORKDAY;
}