[Kde-bindings] KDE/kdepimlibs/kholidays
John Layt
john at layt.net
Sat May 29 20:56:40 UTC 2010
SVN commit 1132134 by jlayt:
Add constructor to allow creation of HolidayRegion from any file, not just the
standard resource files.
CCMAIL: kde-bindings at kde.org
M +48 -12 holidayregion.cpp
M +10 -1 holidayregion.h
M +54 -49 tests/testholidayregion.cpp
M +5 -4 tests/testholidayregion.h
--- trunk/KDE/kdepimlibs/kholidays/holidayregion.cpp #1132133:1132134
@@ -27,6 +27,7 @@
#include <QtCore/QDateTime>
#include <QtCore/QFile>
#include <QtCore/QSharedData>
+#include <QtCore/QFileInfo>
#include <KStandardDirs>
#include <KGlobal>
@@ -41,10 +42,11 @@
class HolidayRegion::Private
{
public:
- Private( const QString ®ionCode )
- : mDriver( 0 ), mRegionCode( regionCode )
+ Private( const QString ®ionCode ) : mDriver( 0 ),
+ mRegionCode( regionCode )
{
if ( !mRegionCode.isEmpty() ) {
+
if ( mRegionCode.length() == 2 ) { //Backwards compatible mode for old location code
mLocation = mRegionCode;
QStringList locationFiles = KGlobal::dirs()->findAllResources( "data",
@@ -53,17 +55,18 @@
if ( locationFiles.count() > 0 ) {
mRegionCode = locationFiles.at( 0 ).mid( locationFiles.at( 0 ).lastIndexOf( "holiday_" ) + 8 );
}
- } else {
- mLocation = mRegionCode.left( 2 );
}
- mHolidayFile = KStandardDirs::locate( "data", "libkholidays/plan2/holiday_" + mRegionCode );
- if ( mHolidayFile.isEmpty() ) {
- mRegionCode.clear();
- mLocation.clear();
- } else {
- mDriver = new HolidayParserDriverPlan( mHolidayFile );
+
+ mHolidayFile.setFile( KStandardDirs::locate( "data", "libkholidays/plan2/holiday_" + mRegionCode ) );
}
+
+ init();
}
+
+ Private( const QFileInfo ®ionFile ) : mDriver( 0 ),
+ mHolidayFile( regionFile )
+ {
+ init();
}
~Private()
@@ -71,10 +74,38 @@
delete mDriver;
}
+ void init()
+ {
+ if ( mHolidayFile.exists() ) {
+ mDriver = new HolidayParserDriverPlan( mHolidayFile.absoluteFilePath() );
+ if ( mDriver ) {
+
+ if ( mLocation.isEmpty() ) {
+ mLocation = mDriver->fileCountryCode().left( 2 );
+ }
+
+ if ( mRegionCode.isEmpty() ) {
+ if ( mHolidayFile.fileName().startsWith( QLatin1String( "holiday_" ) ) ) {
+ mRegionCode = mHolidayFile.fileName().mid( 8 );
+ } else {
+ mRegionCode = mHolidayFile.fileName();
+ }
+ }
+
+ } else {
+ mRegionCode.clear();
+ mLocation.clear();
+ }
+ } else {
+ mRegionCode.clear();
+ mLocation.clear();
+ }
+ }
+
HolidayParserDriver *mDriver; // The parser driver for the holiday file
QString mRegionCode; // region code of holiday region
QString mLocation; // old location code, use now deprecated
- QString mHolidayFile; // full path of file containing holiday data, or null
+ QFileInfo mHolidayFile; // file containing holiday data, or null
};
HolidayRegion::HolidayRegion( const QString ®ionCode )
@@ -82,6 +113,11 @@
{
}
+HolidayRegion::HolidayRegion( const QFileInfo ®ionFile )
+ : d( new Private( regionFile ) )
+{
+}
+
HolidayRegion::~HolidayRegion()
{
delete d;
@@ -266,7 +302,7 @@
bool HolidayRegion::isValid() const
{
- return !d->mHolidayFile.isEmpty() && d->mDriver;
+ return d->mHolidayFile.exists() && d->mDriver;
}
bool HolidayRegion::isValid( const QString ®ionCode )
--- trunk/KDE/kdepimlibs/kholidays/holidayregion.h #1132133:1132134
@@ -35,6 +35,7 @@
class QDate;
class QStringList;
+class QFileInfo;
namespace KHolidays {
@@ -42,7 +43,7 @@
{
public:
/**
- * Creates a new Holiday Region object.
+ * Creates a new Holiday Region object for a given standard Region Code.
*
* From 4.5 onwards this constructor requires the new Region Code not the
* deprecated Location Code. If a Location Code is provided the first
@@ -54,6 +55,14 @@
explicit HolidayRegion( const QString ®ionCode = QString() );
/**
+ * Creates a new Holiday Region object from a given holiday file.
+ * If file doesn't exist, an empty instance will be created.
+ *
+ * @param regionFile The code for the Holiday Region.
+ */
+ explicit HolidayRegion( const QFileInfo ®ionFile );
+
+ /**
* Destroys the holidays object.
*/
~HolidayRegion();
--- trunk/KDE/kdepimlibs/kholidays/tests/testholidayregion.cpp #1132133:1132134
@@ -30,10 +30,8 @@
QTEST_KDEMAIN( HolidayRegionTest, NoGUI )
-void HolidayRegionTest::printMetadata( const QString ®ionCode )
+void HolidayRegionTest::printMetadata( const KHolidays::HolidayRegion ®ion )
{
- KHolidays::HolidayRegion region( regionCode );
-
if ( region.isValid() ) {
kDebug() << "This regionCode = " << region.regionCode();
kDebug() << "Is valid? = " << region.isValid();
@@ -58,75 +56,81 @@
}
}
-void HolidayRegionTest::parseRegionCalendarYear( const QString ®ionCode, int year, const QString &calendarType )
+void HolidayRegionTest::parseRegionCalendarYear( const KHolidays::HolidayRegion ®ion, int year, const QString &calendarType )
{
- kDebug() << "Parsing region = " << regionCode << " year = " << year << " calendar = " << calendarType;
-
- KHolidays::HolidayRegion region( regionCode );
- KHolidays::Holiday::List holidays = region.holidays( year, calendarType );
-
- printHolidays( holidays );
-
+ kDebug() << "Parsing region = " << region.regionCode() << " year = " << year << " calendar = " << calendarType;
+ printHolidays( region.holidays( year, calendarType ) );
kDebug() << "";
}
-void HolidayRegionTest::parseRegionDateRange( const QString ®ionCode, const QDate &startDate, const QDate &endDate )
+void HolidayRegionTest::parseRegionDateRange( const KHolidays::HolidayRegion ®ion, const QDate &startDate, const QDate &endDate )
{
- kDebug() << "Parsing regionCode = " << regionCode <<
+ kDebug() << "Parsing regionCode = " << region.regionCode() <<
" start date = " << startDate.toString( Qt::ISODate ) <<
" end date = " << endDate.toString( Qt::ISODate );
+ printHolidays( region.holidays( startDate, endDate ) );
+ kDebug() << "";
+}
- KHolidays::HolidayRegion region( regionCode );
- KHolidays::Holiday::List holidays = region.holidays( startDate, endDate );
-
- printHolidays( holidays );
-
+void HolidayRegionTest::parseRegionDate( const KHolidays::HolidayRegion ®ion, const QDate &date )
+{
+ kDebug() << "Parsing regionCode = " << region.regionCode() << " date = " << date.toString( Qt::ISODate );
+ printHolidays( region.holidays( date ) );
kDebug() << "";
}
-void HolidayRegionTest::parseRegionDate( const QString ®ionCode, const QDate &date )
+void HolidayRegionTest::testLoadFile()
{
- kDebug() << "Parsing regionCode = " << regionCode << " date = " << date.toString( Qt::ISODate );
-
- KHolidays::HolidayRegion region( regionCode );
- KHolidays::Holiday::List holidays = region.holidays( date );
-
- printHolidays( holidays );
-
+ KHolidays::HolidayRegion region( QFileInfo( KDESRCDIR "/holiday_gb-eaw_en-gb_Test" ) );
+ printMetadata( region );
+ parseRegionCalendarYear( region, 2010 );
+ parseRegionCalendarYear( region, 2011 );
+ parseRegionCalendarYear( region, 2012 );
+ parseRegionCalendarYear( region, 2013 );
+ parseRegionCalendarYear( region, 2014 );
+ parseRegionCalendarYear( region, 2015 );
kDebug() << "";
}
void HolidayRegionTest::testGb()
{
- printMetadata( "gb-eaw_en-gb" );
- parseRegionDateRange( "gb-eaw_en-gb", QDate( 2010, 7, 1), QDate( 2011, 6, 30 ) );
- parseRegionDateRange( "gb-eaw_en-gb", QDate( 2010, 1, 1), QDate( 2012, 12, 31 ) );
- parseRegionDateRange( "gb-eaw_en-gb", QDate( 2010, 1, 1), QDate( 2010, 12, 31 ) );
- parseRegionCalendarYear( "gb-eaw_en-gb", 2010, "gregorian" );
- parseRegionDate( "gb-eaw_en-gb", QDate( 2010, 1, 1 ) );
+ KHolidays::HolidayRegion region( "gb-eaw_en-gb" );
+ printMetadata( region );
+ parseRegionDateRange( region, QDate( 2010, 7, 1), QDate( 2011, 6, 30 ) );
+ parseRegionDateRange( region, QDate( 2010, 1, 1), QDate( 2012, 12, 31 ) );
+ parseRegionDateRange( region, QDate( 2010, 1, 1), QDate( 2010, 12, 31 ) );
+ parseRegionDate( region, QDate( 2010, 1, 1 ) );
+ parseRegionCalendarYear( region, 2010 );
+ parseRegionCalendarYear( region, 2010 );
+ parseRegionCalendarYear( region, 2011 );
+ parseRegionCalendarYear( region, 2012 );
+ parseRegionCalendarYear( region, 2013 );
+ parseRegionCalendarYear( region, 2014 );
+ parseRegionCalendarYear( region, 2015 );
}
void HolidayRegionTest::testIran()
{
- printMetadata( "ir_en-us" );
- parseRegionCalendarYear( "ir_en-us", 2010 );
- parseRegionCalendarYear( "ir_en-us", 2011 );
- parseRegionCalendarYear( "ir_en-us", 2012 );
- parseRegionCalendarYear( "ir_en-us", 2013 );
- parseRegionCalendarYear( "ir_en-us", 2014 );
- parseRegionCalendarYear( "ir_en-us", 2015 );
+ KHolidays::HolidayRegion region( "ir_en-us" );
+ printMetadata( region );
+ parseRegionCalendarYear( region, 2010 );
+ parseRegionCalendarYear( region, 2011 );
+ parseRegionCalendarYear( region, 2012 );
+ parseRegionCalendarYear( region, 2013 );
+ parseRegionCalendarYear( region, 2014 );
+ parseRegionCalendarYear( region, 2015 );
}
void HolidayRegionTest::testIsrael()
{
- printMetadata( "il_en-us" );
- parseRegionCalendarYear( "il_en-us", 2010 );
- parseRegionCalendarYear( "il_en-us", 2011 );
- parseRegionCalendarYear( "il_en-us", 2012 );
- parseRegionCalendarYear( "il_en-us", 2013 );
- parseRegionCalendarYear( "il_en-us", 2014 );
- parseRegionCalendarYear( "il_en-us", 2015 );
-
+ KHolidays::HolidayRegion region( "il_en-us" );
+ printMetadata( region );
+ parseRegionCalendarYear( region, 2010 );
+ parseRegionCalendarYear( region, 2011 );
+ parseRegionCalendarYear( region, 2012 );
+ parseRegionCalendarYear( region, 2013 );
+ parseRegionCalendarYear( region, 2014 );
+ parseRegionCalendarYear( region, 2015 );
}
void HolidayRegionTest::testRegions()
@@ -141,8 +145,9 @@
kDebug() << "This years holidays:";
foreach ( const QString ®ionCode, regions ) {
- printMetadata( regionCode );
- parseRegionCalendarYear( regionCode, QDate::currentDate().year() );
+ KHolidays::HolidayRegion region( regionCode );
+ printMetadata( region );
+ parseRegionCalendarYear( region, QDate::currentDate().year() );
kDebug() << "";
}
kDebug() << "";
--- trunk/KDE/kdepimlibs/kholidays/tests/testholidayregion.h #1132133:1132134
@@ -33,6 +33,7 @@
{
Q_OBJECT
private Q_SLOTS:
+ void testLoadFile();
void testGb();
void testIran();
void testIsrael();
@@ -40,11 +41,11 @@
void testRegions();
private:
- void printMetadata( const QString ®ionCode );
+ void printMetadata( const KHolidays::HolidayRegion ®ion );
void printHolidays( KHolidays::Holiday::List holidays );
- void parseRegionCalendarYear( const QString ®ionCode, int year, const QString &calendarType = "gregorian" );
- void parseRegionDateRange( const QString ®ionCode, const QDate &startDate, const QDate &endDate );
- void parseRegionDate( const QString ®ionCode, const QDate &date );
+ void parseRegionCalendarYear( const KHolidays::HolidayRegion ®ion, int year, const QString &calendarType = "gregorian" );
+ void parseRegionDateRange( const KHolidays::HolidayRegion ®ion, const QDate &startDate, const QDate &endDate );
+ void parseRegionDate( const KHolidays::HolidayRegion ®ion, const QDate &date );
};
#endif // TESTHOLIDAYREGION_H
More information about the Kde-bindings
mailing list