00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "icaltimezones.h"
00023 #include "icalformat.h"
00024 #include "icalformat_p.h"
00025
00026 extern "C" {
00027 #include <ical.h>
00028 #include <icaltimezone.h>
00029 }
00030 #include <ksystemtimezone.h>
00031 #include <kdatetime.h>
00032 #include <kdebug.h>
00033
00034 #include <QtCore/QDateTime>
00035 #include <QtCore/QString>
00036 #include <QtCore/QList>
00037 #include <QtCore/QVector>
00038 #include <QtCore/QSet>
00039 #include <QtCore/QFile>
00040 #include <QtCore/QTextStream>
00041
00042 using namespace KCal;
00043
00044
00045 static const int minRuleCount = 5;
00046 static const int minPhaseCount = 8;
00047
00048
00049 static QDateTime toQDateTime( const icaltimetype &t )
00050 {
00051 return QDateTime( QDate( t.year, t.month, t.day ),
00052 QTime( t.hour, t.minute, t.second ),
00053 ( t.is_utc ? Qt::UTC : Qt::LocalTime ) );
00054 }
00055
00056
00057
00058
00059 static QDateTime MAX_DATE()
00060 {
00061 static QDateTime dt;
00062 if ( !dt.isValid() ) {
00063 dt = QDateTime( QDate::currentDate().addYears( 20 ), QTime( 0, 0, 0 ) );
00064 }
00065 return dt;
00066 }
00067
00068 static icaltimetype writeLocalICalDateTime( const QDateTime &utc, int offset )
00069 {
00070 QDateTime local = utc.addSecs( offset );
00071 icaltimetype t = icaltime_null_time();
00072 t.year = local.date().year();
00073 t.month = local.date().month();
00074 t.day = local.date().day();
00075 t.hour = local.time().hour();
00076 t.minute = local.time().minute();
00077 t.second = local.time().second();
00078 t.is_date = 0;
00079 t.zone = 0;
00080 t.is_utc = 0;
00081 return t;
00082 }
00083
00084 namespace KCal {
00085
00086
00087
00088
00089 class ICalTimeZonesPrivate
00090 {
00091 public:
00092 ICalTimeZonesPrivate() {}
00093 ICalTimeZones::ZoneMap zones;
00094 };
00095
00096
00097 ICalTimeZones::ICalTimeZones()
00098 : d( new ICalTimeZonesPrivate )
00099 {
00100 }
00101
00102 ICalTimeZones::~ICalTimeZones()
00103 {
00104 delete d;
00105 }
00106
00107 const ICalTimeZones::ZoneMap ICalTimeZones::zones() const
00108 {
00109 return d->zones;
00110 }
00111
00112 bool ICalTimeZones::add( const ICalTimeZone &zone )
00113 {
00114 if ( !zone.isValid() ) {
00115 return false;
00116 }
00117 if ( d->zones.find( zone.name() ) != d->zones.end() ) {
00118 return false;
00119 }
00120
00121 d->zones.insert( zone.name(), zone );
00122 return true;
00123 }
00124
00125 ICalTimeZone ICalTimeZones::remove( const ICalTimeZone &zone )
00126 {
00127 if ( zone.isValid() ) {
00128 for ( ZoneMap::Iterator it = d->zones.begin(), end = d->zones.end(); it != end; ++it ) {
00129 if ( it.value() == zone ) {
00130 d->zones.erase( it );
00131 return ( zone == ICalTimeZone::utc() ) ? ICalTimeZone() : zone;
00132 }
00133 }
00134 }
00135 return ICalTimeZone();
00136 }
00137
00138 ICalTimeZone ICalTimeZones::remove( const QString &name )
00139 {
00140 if ( !name.isEmpty() ) {
00141 ZoneMap::Iterator it = d->zones.find( name );
00142 if ( it != d->zones.end() ) {
00143 ICalTimeZone zone = it.value();
00144 d->zones.erase(it);
00145 return ( zone == ICalTimeZone::utc() ) ? ICalTimeZone() : zone;
00146 }
00147 }
00148 return ICalTimeZone();
00149 }
00150
00151 void ICalTimeZones::clear()
00152 {
00153 d->zones.clear();
00154 }
00155
00156 ICalTimeZone ICalTimeZones::zone( const QString &name ) const
00157 {
00158 if ( !name.isEmpty() ) {
00159 ZoneMap::ConstIterator it = d->zones.find( name );
00160 if ( it != d->zones.end() ) {
00161 return it.value();
00162 }
00163 }
00164 return ICalTimeZone();
00165 }
00166
00167
00168
00169 ICalTimeZoneBackend::ICalTimeZoneBackend()
00170 : KTimeZoneBackend()
00171 {}
00172
00173 ICalTimeZoneBackend::ICalTimeZoneBackend( ICalTimeZoneSource *source,
00174 const QString &name,
00175 const QString &countryCode,
00176 float latitude, float longitude,
00177 const QString &comment )
00178 : KTimeZoneBackend( source, name, countryCode, latitude, longitude, comment )
00179 {}
00180
00181 ICalTimeZoneBackend::ICalTimeZoneBackend( const KTimeZone &tz, const QDate &earliest )
00182 : KTimeZoneBackend( 0, tz.name(), tz.countryCode(), tz.latitude(), tz.longitude(), tz.comment() )
00183 {
00184 Q_UNUSED( earliest );
00185 }
00186
00187 ICalTimeZoneBackend::~ICalTimeZoneBackend()
00188 {}
00189
00190 KTimeZoneBackend *ICalTimeZoneBackend::clone() const
00191 {
00192 return new ICalTimeZoneBackend( *this );
00193 }
00194
00195 QByteArray ICalTimeZoneBackend::type() const
00196 {
00197 return "ICalTimeZone";
00198 }
00199
00200 bool ICalTimeZoneBackend::hasTransitions( const KTimeZone *caller ) const
00201 {
00202 Q_UNUSED( caller );
00203 return true;
00204 }
00205
00206
00207
00208 ICalTimeZone::ICalTimeZone()
00209 : KTimeZone( new ICalTimeZoneBackend() )
00210 {}
00211
00212 ICalTimeZone::ICalTimeZone( ICalTimeZoneSource *source, const QString &name,
00213 ICalTimeZoneData *data )
00214 : KTimeZone( new ICalTimeZoneBackend( source, name ) )
00215 {
00216 setData( data );
00217 }
00218
00219 ICalTimeZone::ICalTimeZone( const KTimeZone &tz, const QDate &earliest )
00220 : KTimeZone( new ICalTimeZoneBackend( 0, tz.name(), tz.countryCode(),
00221 tz.latitude(), tz.longitude(),
00222 tz.comment() ) )
00223 {
00224 const KTimeZoneData *data = tz.data( true );
00225 if ( data ) {
00226 const ICalTimeZoneData *icaldata = dynamic_cast<const ICalTimeZoneData*>( data );
00227 if ( icaldata ) {
00228 setData( new ICalTimeZoneData( *icaldata ) );
00229 } else {
00230 setData( new ICalTimeZoneData( *data, tz, earliest ) );
00231 }
00232 }
00233 }
00234
00235 ICalTimeZone::~ICalTimeZone()
00236 {}
00237
00238 QString ICalTimeZone::city() const
00239 {
00240 const ICalTimeZoneData *dat = static_cast<const ICalTimeZoneData*>( data() );
00241 return dat ? dat->city() : QString();
00242 }
00243
00244 QByteArray ICalTimeZone::url() const
00245 {
00246 const ICalTimeZoneData *dat = static_cast<const ICalTimeZoneData*>( data() );
00247 return dat ? dat->url() : QByteArray();
00248 }
00249
00250 QDateTime ICalTimeZone::lastModified() const
00251 {
00252 const ICalTimeZoneData *dat = static_cast<const ICalTimeZoneData*>( data() );
00253 return dat ? dat->lastModified() : QDateTime();
00254 }
00255
00256 QByteArray ICalTimeZone::vtimezone() const
00257 {
00258 const ICalTimeZoneData *dat = static_cast<const ICalTimeZoneData*>( data() );
00259 return dat ? dat->vtimezone() : QByteArray();
00260 }
00261
00262 icaltimezone *ICalTimeZone::icalTimezone() const
00263 {
00264 const ICalTimeZoneData *dat = static_cast<const ICalTimeZoneData*>( data() );
00265 return dat ? dat->icalTimezone() : 0;
00266 }
00267
00268 bool ICalTimeZone::update( const ICalTimeZone &other )
00269 {
00270 if ( !updateBase( other ) ) {
00271 return false;
00272 }
00273
00274 setData( other.data()->clone(), other.source() );
00275 return true;
00276 }
00277
00278 ICalTimeZone ICalTimeZone::utc()
00279 {
00280 static ICalTimeZone utcZone;
00281 if ( !utcZone.isValid() ) {
00282 ICalTimeZoneSource tzs;
00283 utcZone = tzs.parse( icaltimezone_get_utc_timezone() );
00284 }
00285 return utcZone;
00286 }
00287
00288
00289
00290
00291 class ICalTimeZoneDataPrivate
00292 {
00293 public:
00294 ICalTimeZoneDataPrivate() : icalComponent(0) {}
00295 ~ICalTimeZoneDataPrivate()
00296 {
00297 if ( icalComponent ) {
00298 icalcomponent_free( icalComponent );
00299 }
00300 }
00301 icalcomponent *component() const { return icalComponent; }
00302 void setComponent( icalcomponent *c )
00303 {
00304 if ( icalComponent ) {
00305 icalcomponent_free( icalComponent );
00306 }
00307 icalComponent = c;
00308 }
00309 QString location;
00310 QByteArray url;
00311 QDateTime lastModified;
00312 private:
00313 icalcomponent *icalComponent;
00314 };
00315
00316
00317 ICalTimeZoneData::ICalTimeZoneData()
00318 : d ( new ICalTimeZoneDataPrivate() )
00319 {
00320 }
00321
00322 ICalTimeZoneData::ICalTimeZoneData( const ICalTimeZoneData &rhs )
00323 : KTimeZoneData( rhs ),
00324 d( new ICalTimeZoneDataPrivate() )
00325 {
00326 d->location = rhs.d->location;
00327 d->url = rhs.d->url;
00328 d->lastModified = rhs.d->lastModified;
00329 d->setComponent( icalcomponent_new_clone( rhs.d->component() ) );
00330 }
00331
00332 ICalTimeZoneData::ICalTimeZoneData( const KTimeZoneData &rhs,
00333 const KTimeZone &tz, const QDate &earliest )
00334 : KTimeZoneData( rhs ),
00335 d( new ICalTimeZoneDataPrivate() )
00336 {
00337
00338 enum {
00339 DAY_OF_MONTH = 0x01,
00340 WEEKDAY_OF_MONTH = 0x02,
00341 LAST_WEEKDAY_OF_MONTH = 0x04
00342 };
00343
00344 if ( tz.type() == "KSystemTimeZone" ) {
00345
00346
00347
00348 icalcomponent *c = 0;
00349 KTimeZone ktz = KSystemTimeZones::readZone( tz.name() );
00350 if ( ktz.isValid() ) {
00351 if ( ktz.data(true) ) {
00352 ICalTimeZone icaltz( ktz, earliest );
00353 icaltimezone *itz = icaltz.icalTimezone();
00354 c = icalcomponent_new_clone( icaltimezone_get_component( itz ) );
00355 icaltimezone_free( itz, 1 );
00356 }
00357 }
00358 if ( !c ) {
00359
00360 icaltimezone *itz = icaltimezone_get_builtin_timezone( tz.name().toUtf8() );
00361 c = icalcomponent_new_clone( icaltimezone_get_component( itz ) );
00362 }
00363 if ( c ) {
00364
00365
00366
00367 icalproperty *prop = icalcomponent_get_first_property( c, ICAL_TZID_PROPERTY );
00368 if ( prop ) {
00369 icalvalue *value = icalproperty_get_value( prop );
00370 const char *tzid = icalvalue_get_text( value );
00371 QByteArray icalprefix = ICalTimeZoneSource::icalTzidPrefix();
00372 int len = icalprefix.size();
00373 if ( !strncmp( icalprefix, tzid, len ) ) {
00374 const char *s = strchr( tzid + len, '/' );
00375 if ( s ) {
00376 QByteArray tzidShort( s + 1 );
00377 icalvalue_set_text( value, tzidShort );
00378
00379
00380 prop = icalcomponent_get_first_property( c, ICAL_X_PROPERTY );
00381 const char *xname = icalproperty_get_x_name( prop );
00382 if ( xname && !strcmp( xname, "X-LIC-LOCATION" ) ) {
00383 icalcomponent_remove_property( c, prop );
00384 }
00385 }
00386 }
00387 }
00388 }
00389 d->setComponent( c );
00390 } else {
00391
00392 icalcomponent *tzcomp = icalcomponent_new(ICAL_VTIMEZONE_COMPONENT);
00393 icalcomponent_add_property( tzcomp, icalproperty_new_tzid( tz.name().toUtf8() ) );
00394
00395
00396
00397
00398 QList<KTimeZone::Transition> transits = transitions();
00399 if ( earliest.isValid() ) {
00400
00401 for ( int i = 0, end = transits.count(); i < end; ++i ) {
00402 if ( transits[i].time().date() >= earliest ) {
00403 if ( i > 0 ) {
00404 transits.erase( transits.begin(), transits.begin() + i );
00405 }
00406 break;
00407 }
00408 }
00409 }
00410 int trcount = transits.count();
00411 QVector<bool> transitionsDone(trcount);
00412 transitionsDone.fill(false);
00413
00414
00415
00416 icaldatetimeperiodtype dtperiod;
00417 dtperiod.period = icalperiodtype_null_period();
00418 for ( ; ; ) {
00419 int i = 0;
00420 for ( ; i < trcount && transitionsDone[i]; ++i ) {
00421 ;
00422 }
00423 if ( i >= trcount ) {
00424 break;
00425 }
00426
00427 int preOffset = ( i > 0 ) ? transits[i - 1].phase().utcOffset() : rhs.previousUtcOffset();
00428 KTimeZone::Phase phase = transits[i].phase();
00429 if ( phase.utcOffset() == preOffset ) {
00430 transitionsDone[i] = true;
00431 while ( ++i < trcount ) {
00432 if ( transitionsDone[i] ||
00433 transits[i].phase() != phase ||
00434 transits[i - 1].phase().utcOffset() != preOffset ) {
00435 continue;
00436 }
00437 transitionsDone[i] = true;
00438 }
00439 continue;
00440 }
00441 icalcomponent *phaseComp =
00442 icalcomponent_new( phase.isDst() ? ICAL_XDAYLIGHT_COMPONENT : ICAL_XSTANDARD_COMPONENT );
00443 QList<QByteArray> abbrevs = phase.abbreviations();
00444 for ( int a = 0, aend = abbrevs.count(); a < aend; ++a ) {
00445 icalcomponent_add_property( phaseComp,
00446 icalproperty_new_tzname(
00447 static_cast<const char*>( abbrevs[a]) ) );
00448 }
00449 if ( !phase.comment().isEmpty() ) {
00450 icalcomponent_add_property( phaseComp,
00451 icalproperty_new_comment( phase.comment().toUtf8() ) );
00452 }
00453 icalcomponent_add_property( phaseComp,
00454 icalproperty_new_tzoffsetfrom( preOffset ) );
00455 icalcomponent_add_property( phaseComp,
00456 icalproperty_new_tzoffsetto( phase.utcOffset() ) );
00457
00458 icalcomponent *phaseComp1 = icalcomponent_new_clone( phaseComp );
00459 icalcomponent_add_property( phaseComp1,
00460 icalproperty_new_dtstart(
00461 writeLocalICalDateTime( transits[i].time(), preOffset ) ) );
00462 bool useNewRRULE = false;
00463
00464
00465
00466 QTime time;
00467 QDate date;
00468 int year = 0, month = 0, daysInMonth = 0, dayOfMonth = 0;
00469 int dayOfWeek = 0;
00470 int nthFromStart = 0;
00471 int nthFromEnd = 0;
00472 int newRule;
00473 int rule = 0;
00474 QList<QDateTime> rdates;
00475 QList<QDateTime> times;
00476 QDateTime qdt = transits[i].time();
00477 times += qdt;
00478 transitionsDone[i] = true;
00479 do {
00480 if ( !rule ) {
00481
00482 rule = DAY_OF_MONTH | WEEKDAY_OF_MONTH | LAST_WEEKDAY_OF_MONTH;
00483 time = qdt.time();
00484 date = qdt.date();
00485 year = date.year();
00486 month = date.month();
00487 daysInMonth = date.daysInMonth();
00488 dayOfWeek = date.dayOfWeek();
00489 dayOfMonth = date.day();
00490 nthFromStart = ( dayOfMonth - 1 ) / 7 + 1;
00491 nthFromEnd = ( daysInMonth - dayOfMonth ) / 7 + 1;
00492 }
00493 if ( ++i >= trcount ) {
00494 newRule = 0;
00495 times += QDateTime();
00496 } else {
00497 if ( transitionsDone[i] ||
00498 transits[i].phase() != phase ||
00499 transits[i - 1].phase().utcOffset() != preOffset ) {
00500 continue;
00501 }
00502 transitionsDone[i] = true;
00503 qdt = transits[i].time();
00504 if ( !qdt.isValid() ) {
00505 continue;
00506 }
00507 newRule = rule;
00508 times += qdt;
00509 date = qdt.date();
00510 if ( qdt.time() != time ||
00511 date.month() != month ||
00512 date.year() != ++year ) {
00513 newRule = 0;
00514 } else {
00515 int day = date.day();
00516 if ( ( newRule & DAY_OF_MONTH ) && day != dayOfMonth ) {
00517 newRule &= ~DAY_OF_MONTH;
00518 }
00519 if ( newRule & ( WEEKDAY_OF_MONTH | LAST_WEEKDAY_OF_MONTH ) ) {
00520 if ( date.dayOfWeek() != dayOfWeek ) {
00521 newRule &= ~( WEEKDAY_OF_MONTH | LAST_WEEKDAY_OF_MONTH );
00522 } else {
00523 if ( ( newRule & WEEKDAY_OF_MONTH ) &&
00524 ( day - 1 ) / 7 + 1 != nthFromStart ) {
00525 newRule &= ~WEEKDAY_OF_MONTH;
00526 }
00527 if ( ( newRule & LAST_WEEKDAY_OF_MONTH ) &&
00528 ( daysInMonth - day ) / 7 + 1 != nthFromEnd ) {
00529 newRule &= ~LAST_WEEKDAY_OF_MONTH;
00530 }
00531 }
00532 }
00533 }
00534 }
00535 if ( !newRule ) {
00536
00537
00538
00539 int yr = times[0].date().year();
00540 while ( !rdates.isEmpty() ) {
00541 qdt = rdates.last();
00542 date = qdt.date();
00543 if ( qdt.time() != time ||
00544 date.month() != month ||
00545 date.year() != --yr ) {
00546 break;
00547 }
00548 int day = date.day();
00549 if ( rule & DAY_OF_MONTH ) {
00550 if ( day != dayOfMonth ) {
00551 break;
00552 }
00553 } else {
00554 if ( date.dayOfWeek() != dayOfWeek ||
00555 ( rule & WEEKDAY_OF_MONTH ) &&
00556 ( day - 1 ) / 7 + 1 != nthFromStart ||
00557 ( rule & LAST_WEEKDAY_OF_MONTH ) &&
00558 ( daysInMonth - day ) / 7 + 1 != nthFromEnd ) {
00559 break;
00560 }
00561 }
00562 times.prepend( qdt );
00563 rdates.pop_back();
00564 }
00565 if ( times.count() > ( useNewRRULE ? minPhaseCount : minRuleCount ) ) {
00566
00567 icalrecurrencetype r;
00568 icalrecurrencetype_clear( &r );
00569 r.freq = ICAL_YEARLY_RECURRENCE;
00570 r.count = ( year >= 2030 ) ? 0 : times.count() - 1;
00571 r.by_month[0] = month;
00572 if ( rule & DAY_OF_MONTH ) {
00573 r.by_month_day[0] = dayOfMonth;
00574 } else if ( rule & WEEKDAY_OF_MONTH ) {
00575 r.by_day[0] = ( dayOfWeek % 7 + 1 ) + ( nthFromStart * 8 );
00576 } else if ( rule & LAST_WEEKDAY_OF_MONTH ) {
00577 r.by_day[0] = -( dayOfWeek % 7 + 1 ) - ( nthFromEnd * 8 );
00578 }
00579 icalproperty *prop = icalproperty_new_rrule( r );
00580 if ( useNewRRULE ) {
00581
00582
00583 icalcomponent *c = icalcomponent_new_clone( phaseComp );
00584 icalcomponent_add_property(
00585 c, icalproperty_new_dtstart( writeLocalICalDateTime( times[0], preOffset ) ) );
00586 icalcomponent_add_property( c, prop );
00587 icalcomponent_add_component( tzcomp, c );
00588 } else {
00589 icalcomponent_add_property( phaseComp1, prop );
00590 }
00591 } else {
00592
00593 for ( int t = 0, tend = times.count() - 1; t < tend; ++t ) {
00594 rdates += times[t];
00595 }
00596 }
00597 useNewRRULE = true;
00598
00599
00600 qdt = times.last();
00601 times.clear();
00602 times += qdt;
00603 }
00604 rule = newRule;
00605 } while ( i < trcount );
00606
00607
00608 for ( int rd = 0, rdend = rdates.count(); rd < rdend; ++rd ) {
00609 dtperiod.time = writeLocalICalDateTime( rdates[rd], preOffset );
00610 icalcomponent_add_property( phaseComp1, icalproperty_new_rdate( dtperiod ) );
00611 }
00612 icalcomponent_add_component( tzcomp, phaseComp1 );
00613 icalcomponent_free( phaseComp );
00614 }
00615
00616 d->setComponent( tzcomp );
00617 }
00618 }
00619
00620 ICalTimeZoneData::~ICalTimeZoneData()
00621 {
00622 delete d;
00623 }
00624
00625 ICalTimeZoneData &ICalTimeZoneData::operator=( const ICalTimeZoneData &rhs )
00626 {
00627 KTimeZoneData::operator=( rhs );
00628 d->location = rhs.d->location;
00629 d->url = rhs.d->url;
00630 d->lastModified = rhs.d->lastModified;
00631 d->setComponent( icalcomponent_new_clone( rhs.d->component() ) );
00632 return *this;
00633 }
00634
00635 KTimeZoneData *ICalTimeZoneData::clone() const
00636 {
00637 return new ICalTimeZoneData( *this );
00638 }
00639
00640 QString ICalTimeZoneData::city() const
00641 {
00642 return d->location;
00643 }
00644
00645 QByteArray ICalTimeZoneData::url() const
00646 {
00647 return d->url;
00648 }
00649
00650 QDateTime ICalTimeZoneData::lastModified() const
00651 {
00652 return d->lastModified;
00653 }
00654
00655 QByteArray ICalTimeZoneData::vtimezone() const
00656 {
00657 return icalcomponent_as_ical_string( d->component() );
00658 }
00659
00660 icaltimezone *ICalTimeZoneData::icalTimezone() const
00661 {
00662 icaltimezone *icaltz = icaltimezone_new();
00663 if ( !icaltz ) {
00664 return 0;
00665 }
00666 icalcomponent *c = icalcomponent_new_clone( d->component() );
00667 if ( !icaltimezone_set_component( icaltz, c ) ) {
00668 icalcomponent_free( c );
00669 icaltimezone_free( icaltz, 1 );
00670 return 0;
00671 }
00672 return icaltz;
00673 }
00674
00675 bool ICalTimeZoneData::hasTransitions() const
00676 {
00677 return true;
00678 }
00679
00680
00681
00682
00683 class ICalTimeZoneSourcePrivate
00684 {
00685 public:
00686 static QList<QDateTime> parsePhase( icalcomponent *, bool daylight,
00687 int &prevOffset, KTimeZone::Phase & );
00688 static QByteArray icalTzidPrefix;
00689 };
00690
00691 QByteArray ICalTimeZoneSourcePrivate::icalTzidPrefix;
00692
00693
00694 ICalTimeZoneSource::ICalTimeZoneSource()
00695 : KTimeZoneSource( false ),
00696 d( 0 )
00697 {
00698 }
00699
00700 ICalTimeZoneSource::~ICalTimeZoneSource()
00701 {
00702 }
00703
00704 bool ICalTimeZoneSource::parse( const QString &fileName, ICalTimeZones &zones )
00705 {
00706 QFile file( fileName );
00707 if ( !file.open( QIODevice::ReadOnly ) ) {
00708 return false;
00709 }
00710 QTextStream ts( &file );
00711 ts.setCodec( "ISO 8859-1" );
00712 QByteArray text = ts.readAll().trimmed().toLatin1();
00713 file.close();
00714
00715 bool result = false;
00716 icalcomponent *calendar = icalcomponent_new_from_string( text.data() );
00717 if ( calendar ) {
00718 if ( icalcomponent_isa( calendar ) == ICAL_VCALENDAR_COMPONENT ) {
00719 result = parse( calendar, zones );
00720 }
00721 icalcomponent_free( calendar );
00722 }
00723 return result;
00724 }
00725
00726 bool ICalTimeZoneSource::parse( icalcomponent *calendar, ICalTimeZones &zones )
00727 {
00728 for ( icalcomponent *c = icalcomponent_get_first_component( calendar, ICAL_VTIMEZONE_COMPONENT );
00729 c; c = icalcomponent_get_next_component( calendar, ICAL_VTIMEZONE_COMPONENT ) ) {
00730 ICalTimeZone zone = parse( c );
00731 if ( !zone.isValid() ) {
00732 return false;
00733 }
00734 ICalTimeZone oldzone = zones.zone( zone.name() );
00735 if ( oldzone.isValid() ) {
00736
00737
00738 oldzone.update( zone );
00739 } else if ( !zones.add( zone ) ) {
00740 return false;
00741 }
00742 }
00743 return true;
00744 }
00745
00746 ICalTimeZone ICalTimeZoneSource::parse( icalcomponent *vtimezone )
00747 {
00748 QString name;
00749 QString xlocation;
00750 ICalTimeZoneData *data = new ICalTimeZoneData();
00751
00752
00753 icalproperty *p = icalcomponent_get_first_property( vtimezone, ICAL_ANY_PROPERTY );
00754 while ( p ) {
00755 icalproperty_kind kind = icalproperty_isa( p );
00756 switch ( kind ) {
00757
00758 case ICAL_TZID_PROPERTY:
00759 name = QString::fromUtf8( icalproperty_get_tzid( p ) );
00760 break;
00761
00762 case ICAL_TZURL_PROPERTY:
00763 data->d->url = icalproperty_get_tzurl( p );
00764 break;
00765
00766 case ICAL_LOCATION_PROPERTY:
00767
00768 data->d->location = QString::fromUtf8( icalproperty_get_location( p ) );
00769 break;
00770
00771 case ICAL_X_PROPERTY:
00772 {
00773 const char *xname = icalproperty_get_x_name( p );
00774 if ( xname && !strcmp( xname, "X-LIC-LOCATION" ) ) {
00775 xlocation = QString::fromUtf8( icalproperty_get_x( p ) );
00776 }
00777 break;
00778 }
00779 case ICAL_LASTMODIFIED_PROPERTY:
00780 {
00781 icaltimetype t = icalproperty_get_lastmodified(p);
00782 if ( t.is_utc ) {
00783 data->d->lastModified = toQDateTime( t );
00784 } else {
00785 kDebug() << "LAST-MODIFIED not UTC";
00786 }
00787 break;
00788 }
00789 default:
00790 break;
00791 }
00792 p = icalcomponent_get_next_property( vtimezone, ICAL_ANY_PROPERTY );
00793 }
00794
00795 if ( name.isEmpty() ) {
00796 kDebug() << "TZID missing";
00797 delete data;
00798 return ICalTimeZone();
00799 }
00800 if ( data->d->location.isEmpty() && !xlocation.isEmpty() ) {
00801 data->d->location = xlocation;
00802 }
00803 QString prefix = QString::fromUtf8( icalTzidPrefix() );
00804 if ( name.startsWith( prefix ) ) {
00805
00806 int i = name.indexOf( '/', prefix.length() );
00807 if ( i > 0 ) {
00808 name = name.mid( i + 1 );
00809 }
00810 }
00811
00812
00813
00814
00815
00816
00817 int prevOffset = 0;
00818 QList<KTimeZone::Transition> transitions;
00819 QDateTime earliest;
00820 QList<KTimeZone::Phase> phases;
00821 for ( icalcomponent *c = icalcomponent_get_first_component( vtimezone, ICAL_ANY_COMPONENT );
00822 c; c = icalcomponent_get_next_component( vtimezone, ICAL_ANY_COMPONENT ) )
00823 {
00824 int prevoff;
00825 KTimeZone::Phase phase;
00826 QList<QDateTime> times;
00827 icalcomponent_kind kind = icalcomponent_isa( c );
00828 switch ( kind ) {
00829
00830 case ICAL_XSTANDARD_COMPONENT:
00831
00832 times = ICalTimeZoneSourcePrivate::parsePhase( c, false, prevoff, phase );
00833 break;
00834
00835 case ICAL_XDAYLIGHT_COMPONENT:
00836
00837 times = ICalTimeZoneSourcePrivate::parsePhase( c, true, prevoff, phase );
00838 break;
00839
00840 default:
00841 kDebug() << "Unknown component:" << kind;
00842 break;
00843 }
00844 int tcount = times.count();
00845 if ( tcount ) {
00846 phases += phase;
00847 for ( int t = 0; t < tcount; ++t ) {
00848 transitions += KTimeZone::Transition( times[t], phase );
00849 }
00850 if ( !earliest.isValid() || times[0] < earliest ) {
00851 prevOffset = prevoff;
00852 earliest = times[0];
00853 }
00854 }
00855 }
00856 data->setPhases( phases, prevOffset );
00857
00858
00859 qSort( transitions );
00860 for ( int t = 1, tend = transitions.count(); t < tend; ) {
00861 if ( transitions[t].phase() == transitions[t - 1].phase() ) {
00862 transitions.removeAt( t );
00863 --tend;
00864 } else {
00865 ++t;
00866 }
00867 }
00868 data->setTransitions( transitions );
00869
00870 data->d->setComponent( icalcomponent_new_clone( vtimezone ) );
00871 kDebug() << "VTIMEZONE" << name;
00872 return ICalTimeZone( this, name, data );
00873 }
00874
00875 ICalTimeZone ICalTimeZoneSource::parse( icaltimezone *tz )
00876 {
00877
00878
00879
00880
00881 return parse( icaltimezone_get_component( tz ) );
00882 }
00883
00884
00885 QList<QDateTime> ICalTimeZoneSourcePrivate::parsePhase( icalcomponent *c,
00886 bool daylight,
00887 int &prevOffset,
00888 KTimeZone::Phase &phase )
00889 {
00890 QList<QDateTime> transitions;
00891
00892
00893 QList<QByteArray> abbrevs;
00894 QString comment;
00895 prevOffset = 0;
00896 int utcOffset = 0;
00897 bool recurs = false;
00898 bool found_dtstart = false;
00899 bool found_tzoffsetfrom = false;
00900 bool found_tzoffsetto = false;
00901 icaltimetype dtstart = icaltime_null_time();
00902
00903
00904 icalproperty *p = icalcomponent_get_first_property( c, ICAL_ANY_PROPERTY );
00905 while ( p ) {
00906 icalproperty_kind kind = icalproperty_isa( p );
00907 switch ( kind ) {
00908
00909 case ICAL_TZNAME_PROPERTY:
00910 {
00911
00912
00913 #ifdef __GNUC__
00914 #warning Does this cope with multiple language specifications?
00915 #endif
00916 QByteArray tzname = icalproperty_get_tzname( p );
00917
00918
00919 if ( !daylight && tzname == "Standard Time" ||
00920 daylight && tzname == "Daylight Time" ) {
00921 break;
00922 }
00923 if ( !abbrevs.contains( tzname ) ) {
00924 abbrevs += tzname;
00925 }
00926 break;
00927 }
00928 case ICAL_DTSTART_PROPERTY:
00929 dtstart = icalproperty_get_dtstart( p );
00930 found_dtstart = true;
00931 break;
00932
00933 case ICAL_TZOFFSETFROM_PROPERTY:
00934 prevOffset = icalproperty_get_tzoffsetfrom( p );
00935 found_tzoffsetfrom = true;
00936 break;
00937
00938 case ICAL_TZOFFSETTO_PROPERTY:
00939 utcOffset = icalproperty_get_tzoffsetto( p );
00940 found_tzoffsetto = true;
00941 break;
00942
00943 case ICAL_COMMENT_PROPERTY:
00944 comment = QString::fromUtf8( icalproperty_get_comment( p ) );
00945 break;
00946
00947 case ICAL_RDATE_PROPERTY:
00948 case ICAL_RRULE_PROPERTY:
00949 recurs = true;
00950 break;
00951
00952 default:
00953 kDebug() << "Unknown property:" << kind;
00954 break;
00955 }
00956 p = icalcomponent_get_next_property( c, ICAL_ANY_PROPERTY );
00957 }
00958
00959
00960 if ( !found_dtstart || !found_tzoffsetfrom || !found_tzoffsetto ) {
00961 kDebug() << "DTSTART/TZOFFSETFROM/TZOFFSETTO missing";
00962 return transitions;
00963 }
00964
00965
00966 QDateTime localStart = toQDateTime( dtstart );
00967 dtstart.second -= prevOffset;
00968 dtstart.is_utc = 1;
00969 QDateTime utcStart = toQDateTime( icaltime_normalize( dtstart ) );
00970
00971 transitions += utcStart;
00972 if ( recurs ) {
00973
00974
00975
00976
00977
00978 KDateTime klocalStart( localStart, KDateTime::Spec::ClockTime() );
00979 KDateTime maxTime( MAX_DATE(), KDateTime::Spec::ClockTime() );
00980 Recurrence recur;
00981 icalproperty *p = icalcomponent_get_first_property( c, ICAL_ANY_PROPERTY );
00982 while ( p ) {
00983 icalproperty_kind kind = icalproperty_isa( p );
00984 switch ( kind ) {
00985
00986 case ICAL_RDATE_PROPERTY:
00987 {
00988 icaltimetype t = icalproperty_get_rdate(p).time;
00989 if ( icaltime_is_date( t ) ) {
00990
00991 t.hour = dtstart.hour;
00992 t.minute = dtstart.minute;
00993 t.second = dtstart.second;
00994 t.is_date = 0;
00995 t.is_utc = 0;
00996 }
00997
00998
00999 if ( !t.is_utc ) {
01000 t.second -= prevOffset;
01001 t.is_utc = 1;
01002 t = icaltime_normalize( t );
01003 }
01004 transitions += toQDateTime( t );
01005 break;
01006 }
01007 case ICAL_RRULE_PROPERTY:
01008 {
01009 RecurrenceRule r;
01010 ICalFormat icf;
01011 ICalFormatImpl impl( &icf );
01012 impl.readRecurrence( icalproperty_get_rrule( p ), &r );
01013 r.setStartDt( klocalStart );
01014
01015
01016 if ( r.duration() == 0 ) {
01017 KDateTime end( r.endDt() );
01018 if ( end.timeSpec() == KDateTime::Spec::UTC() ) {
01019 end.setTimeSpec( KDateTime::Spec::ClockTime() );
01020 r.setEndDt( end.addSecs( prevOffset ) );
01021 }
01022 }
01023 DateTimeList dts = r.timesInInterval( klocalStart, maxTime );
01024 for ( int i = 0, end = dts.count(); i < end; ++i ) {
01025 QDateTime utc = dts[i].dateTime();
01026 utc.setTimeSpec( Qt::UTC );
01027 transitions += utc.addSecs( -prevOffset );
01028 }
01029 break;
01030 }
01031 default:
01032 break;
01033 }
01034 p = icalcomponent_get_next_property( c, ICAL_ANY_PROPERTY );
01035 }
01036 qSortUnique( transitions );
01037 }
01038
01039 phase = KTimeZone::Phase( utcOffset, abbrevs, daylight, comment );
01040 return transitions;
01041 }
01042
01043
01044 ICalTimeZone ICalTimeZoneSource::standardZone( const QString &zone, bool icalBuiltIn )
01045 {
01046 if ( !icalBuiltIn ) {
01047
01048
01049
01050 QString tzid = zone;
01051 QString prefix = QString::fromUtf8( icalTzidPrefix() );
01052 if ( zone.startsWith( prefix ) ) {
01053 int i = zone.indexOf( '/', prefix.length() );
01054 if ( i > 0 ) {
01055 tzid = zone.mid( i + 1 );
01056 }
01057 }
01058 KTimeZone ktz = KSystemTimeZones::readZone( tzid );
01059 if ( ktz.isValid() ) {
01060 if ( ktz.data( true ) ) {
01061 ICalTimeZone icaltz( ktz );
01062 kDebug() << zone << " read from system database";
01063 return icaltz;
01064 }
01065 }
01066 }
01067
01068
01069 QByteArray zoneName = zone.toUtf8();
01070 icaltimezone *icaltz = icaltimezone_get_builtin_timezone( zoneName );
01071 if ( !icaltz ) {
01072
01073 icaltz = icaltimezone_get_builtin_timezone_from_tzid( zoneName );
01074 if ( !icaltz ) {
01075 return ICalTimeZone();
01076 }
01077 }
01078 return parse( icaltz );
01079 }
01080
01081 QByteArray ICalTimeZoneSource::icalTzidPrefix()
01082 {
01083 if ( ICalTimeZoneSourcePrivate::icalTzidPrefix.isEmpty() ) {
01084 icaltimezone *icaltz = icaltimezone_get_builtin_timezone( "Europe/London" );
01085 QByteArray tzid = icaltimezone_get_tzid( icaltz );
01086 if ( tzid.right( 13 ) == "Europe/London" ) {
01087 int i = tzid.indexOf( '/', 1 );
01088 if ( i > 0 ) {
01089 ICalTimeZoneSourcePrivate::icalTzidPrefix = tzid.left( i + 1 );
01090 return ICalTimeZoneSourcePrivate::icalTzidPrefix;
01091 }
01092 }
01093 kError() << "failed to get libical TZID prefix";
01094 }
01095 return ICalTimeZoneSourcePrivate::icalTzidPrefix;
01096 }
01097
01098 }