• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepimlibs API Reference
  • KDE Home
  • Contact Us
 

KHolidays Library

  • sources
  • kde-4.12
  • kdepimlibs
  • kholidays
  • parsers
  • plan1
holidayparserdriverplanold.cpp
1 /*
2  This file is part of the kholidays library.
3 
4  Copyright 2010 John Layt <john@layt.net>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library 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 Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include "holidayparserdriverplanold_p.h"
23 
24 #include <QStringList>
25 #include <QFileInfo>
26 
27 #include <KCalendarSystem>
28 #include <kdebug.h>
29 
30 #include "holiday_p.h"
31 
32 // Extern for C implementation of parser generated by Bison/Flex
33 extern "C" {
34 #ifdef Q_OS_WIN32
35  KHOLIDAYS_EXPORT char *parse_holidays( const char *, int year, short force );
36 #else
37  char *parse_holidays( const char *, int year, short force );
38 #endif
39 
40  struct holiday {
41  char *string;
42  int color;
43  unsigned short dup;
44  holiday *next;
45  };
46 
47 #ifdef Q_OS_WIN32
48  KHOLIDAYS_EXPORT extern struct holiday holidays[366];
49 #else
50  extern struct holiday holidays[366];
51 #endif
52 }
53 
54 using namespace KHolidays;
55 
56 HolidayParserDriverPlanOld::HolidayParserDriverPlanOld( const QString &planFilePath )
57  :HolidayParserDriver( planFilePath )
58 {
59  parseMetadata();
60 }
61 
62 HolidayParserDriverPlanOld::~HolidayParserDriverPlanOld()
63 {
64 }
65 
66 void HolidayParserDriverPlanOld::error( const QString &errorMessage )
67 {
68  kDebug() << errorMessage;
69 }
70 
71 void HolidayParserDriverPlanOld::parse()
72 {
73  // We only parse for the Gregorian calendar
74  setParseCalendar( "gregorian" );
75  // Set the parse start/end years from the request start/end dates
76  setParseStartEnd();
77 
78  QDate thisDate;
79 
80  // Generate all events for this calendar in the requested year(s)
81  for ( m_parseYear = m_parseStartYear; m_parseYear <= m_parseEndYear; ++m_parseYear ) {
82 
83  QDate parseYearStart;
84  m_parseCalendar->setDate( parseYearStart, m_parseYear, 1, 1 );
85 
86  // Parser takes a 2-digit year assumed to be in range 1950-2050
87  parse_holidays( QFile::encodeName( m_filePath ), m_parseYear - 1900, 1 );
88 
89  // Add this years holidays to holiday list
90  for ( int i = 0; i < 366; ++i ) {
91  struct holiday *hd = &::holidays[i];
92  thisDate = parseYearStart.addDays( i );
93  // Only add holidays if they fall inside requested range
94  if ( thisDate >= m_requestStart && thisDate <= m_requestEnd ) {
95  while ( hd ) {
96  if ( hd->string ) {
97  Holiday holiday;
98  holiday.d->mObservedDate = thisDate;
99  holiday.d->mText = QString::fromUtf8( hd->string );
100  holiday.d->mShortText = holiday.d->mText;
101  if ( hd->color == 2 || hd->color == 9 ) {
102  holiday.d->mDayType = Holiday::NonWorkday;
103  } else {
104  holiday.d->mDayType = Holiday::Workday;
105  }
106  m_resultList.append( holiday );
107  }
108  hd = hd->next;
109  }
110  }
111  }
112  }
113 }
114 
115 void HolidayParserDriverPlanOld::parseMetadata()
116 {
117  // metadata is encoded in filename in form holiday_<region>_<type>_<language>_<name>
118  // with region, type and language sub groups separated by -, and with name optional
119  m_fileCountryCode.clear();
120  m_fileLanguageCode.clear();
121  m_fileName.clear();
122  m_fileDescription.clear();
123 
124  QFileInfo file( m_filePath );
125  if ( file.exists() ) {
126  QStringList metadata = file.fileName().split( '_' );
127  if ( metadata[0] == "holiday" && metadata.count() > 2 ) {
128  m_fileCountryCode = metadata[1].toUpper();
129  QStringList language = metadata[2].split( '-' );
130  m_fileLanguageCode = language[0];
131  if ( language.count() > 1 ) {
132  m_fileLanguageCode.append( '_' ).append( language[1].toUpper() );
133  }
134  if ( metadata.count() > 3 ) {
135  m_fileName = metadata[3];
136  }
137  }
138  }
139 }
KHolidays::HolidayParserDriverPlanOld::parse
void parse()
Actually parse the file, old plan format implementation.
Definition: holidayparserdriverplanold.cpp:71
KHolidays::HolidayParserDriverPlanOld::error
void error(const QString &errorMessage)
Standard error message handling.
Definition: holidayparserdriverplanold.cpp:66
KHolidays::HolidayParserDriverPlanOld::parseMetadata
void parseMetadata()
Parse the file for metadata only and populate the metadata variables.
Definition: holidayparserdriverplanold.cpp:115
KHolidays::HolidayParserDriver::setParseStartEnd
virtual void setParseStartEnd()
Initialise parse year variables for calendar system.
Definition: holidayparserdriver.cpp:116
KHolidays::HolidayParserDriver
HolidayParserDriver abstract base class.
Definition: holidayparserdriver_p.h:44
KHolidays::HolidayParserDriverPlanOld::HolidayParserDriverPlanOld
HolidayParserDriverPlanOld(const QString &planFilename)
Constructor of old Plan file parser driver.
Definition: holidayparserdriverplanold.cpp:56
KHolidays::HolidayParserDriverPlanOld::~HolidayParserDriverPlanOld
~HolidayParserDriverPlanOld()
Destructor.
Definition: holidayparserdriverplanold.cpp:62
KHolidays::HolidayParserDriver::setParseCalendar
virtual void setParseCalendar(const QString &calendarType)
Set the calendar system to use.
Definition: holidayparserdriver.cpp:110
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:06 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KHolidays Library

Skip menu "KHolidays Library"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal