KHolidays Library
holidayparserdriverplanold.cpp
00001 /* 00002 This file is part of the kholidays library. 00003 00004 Copyright 2010 John Layt <john@layt.net> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library 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 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to the 00018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 Boston, MA 02110-1301, USA. 00020 */ 00021 00022 #include "holidayparserdriverplanold_p.h" 00023 00024 #include <QStringList> 00025 #include <QFileInfo> 00026 00027 #include <KCalendarSystem> 00028 #include <kdebug.h> 00029 00030 #include "holiday_p.h" 00031 00032 // Extern for C implementation of parser generated by Bison/Flex 00033 extern "C" { 00034 #ifdef Q_OS_WIN32 00035 KHOLIDAYS_EXPORT char *parse_holidays( const char *, int year, short force ); 00036 #else 00037 char *parse_holidays( const char *, int year, short force ); 00038 #endif 00039 00040 struct holiday { 00041 char *string; 00042 int color; 00043 unsigned short dup; 00044 holiday *next; 00045 }; 00046 00047 #ifdef Q_OS_WIN32 00048 KHOLIDAYS_EXPORT extern struct holiday holidays[366]; 00049 #else 00050 extern struct holiday holidays[366]; 00051 #endif 00052 } 00053 00054 using namespace KHolidays; 00055 00056 HolidayParserDriverPlanOld::HolidayParserDriverPlanOld( const QString &planFilePath ) 00057 :HolidayParserDriver( planFilePath ) 00058 { 00059 parseMetadata(); 00060 } 00061 00062 HolidayParserDriverPlanOld::~HolidayParserDriverPlanOld() 00063 { 00064 } 00065 00066 void HolidayParserDriverPlanOld::error( const QString &errorMessage ) 00067 { 00068 kDebug() << errorMessage; 00069 } 00070 00071 void HolidayParserDriverPlanOld::parse() 00072 { 00073 // We only parse for the Gregorian calendar 00074 setParseCalendar( "gregorian" ); 00075 // Set the parse start/end years from the request start/end dates 00076 setParseStartEnd(); 00077 00078 QDate thisDate; 00079 00080 // Generate all events for this calendar in the requested year(s) 00081 for ( m_parseYear = m_parseStartYear; m_parseYear <= m_parseEndYear; ++m_parseYear ) { 00082 00083 QDate parseYearStart; 00084 m_parseCalendar->setDate( parseYearStart, m_parseYear, 1, 1 ); 00085 00086 // Parser takes a 2-digit year assumed to be in range 1950-2050 00087 parse_holidays( QFile::encodeName( m_filePath ), m_parseYear - 1900, 1 ); 00088 00089 // Add this years holidays to holiday list 00090 for ( int i = 0; i < 366; ++i ) { 00091 struct holiday *hd = &::holidays[i]; 00092 thisDate = parseYearStart.addDays(i); 00093 // Only add holidays if they fall inside requested range 00094 if ( thisDate >= m_requestStart && thisDate <= m_requestEnd ) { 00095 while ( hd ) { 00096 if ( hd->string ) { 00097 Holiday holiday; 00098 holiday.d->mObservedDate = thisDate; 00099 holiday.d->mText = QString::fromUtf8( hd->string ); 00100 holiday.d->mShortText = holiday.d->mText; 00101 if ( hd->color == 2 || hd->color == 9 ) { 00102 holiday.d->mDayType = Holiday::NonWorkday; 00103 } else { 00104 holiday.d->mDayType = Holiday::Workday; 00105 } 00106 m_resultList.append( holiday ); 00107 } 00108 hd = hd->next; 00109 } 00110 } 00111 } 00112 } 00113 } 00114 00115 void HolidayParserDriverPlanOld::parseMetadata() 00116 { 00117 // metadata is encoded in filename in form holiday_<region>_<type>_<language>_<name> 00118 // with region, type and language sub groups separated by -, and with name optional 00119 m_fileCountryCode.clear(); 00120 m_fileLanguageCode.clear(); 00121 m_fileName.clear(); 00122 m_fileDescription.clear(); 00123 00124 QFileInfo file( m_filePath ); 00125 if ( file.exists() ) { 00126 QStringList metadata = file.fileName().split('_'); 00127 if ( metadata[0] == "holiday" && metadata.count() > 2 ) { 00128 m_fileCountryCode = metadata[1].toUpper(); 00129 QStringList language = metadata[2].split('-'); 00130 m_fileLanguageCode = language[0]; 00131 if ( language.count() > 1 ) { 00132 m_fileLanguageCode.append( '_' ).append( language[1].toUpper() ); 00133 } 00134 if ( metadata.count() > 3 ) { 00135 m_fileName = metadata[3]; 00136 } 00137 } 00138 } 00139 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu Aug 2 2012 15:24:21 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu Aug 2 2012 15:24:21 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.