libkcal
recurrencerule.h00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef KCAL_RECURRENCERULE_H
00025 #define KCAL_RECURRENCERULE_H
00026
00027 #include <qdatetime.h>
00028 #include <libkcal/listbase.h>
00029
00030 #include "libkcal_export.h"
00031
00032 template <class T>
00033 Q_INLINE_TEMPLATES void qSortUnique( QValueList<T> &lst )
00034 {
00035 qHeapSort( lst );
00036 if ( lst.isEmpty() ) return;
00037
00038
00039 QValueListIterator<T> it = lst.begin();
00040 T last = *it;
00041 ++it;
00042 T newlast;
00043 while ( it != lst.end() ) {
00044 newlast = (*it);
00045 if ( newlast == last ) it = lst.remove( it );
00046 else {
00047 last = newlast;
00048 ++it;
00049 }
00050 }
00051 }
00052
00053 template <class T>
00054 Q_INLINE_TEMPLATES int findGE( QValueList<T> &lst, const T &value, int start )
00055 {
00056
00057 int st = start - 1;
00058 int end = lst.count();
00059 while ( end - st > 1 ) {
00060 int i = ( st + end ) / 2;
00061 if ( value <= lst[i] ) {
00062 end = i;
00063 } else {
00064 st = i;
00065 }
00066 }
00067 ++st;
00068 return ( st == int( lst.count() ) ) ? -1 : st;
00069 }
00070
00071 template <class T>
00072 Q_INLINE_TEMPLATES int findGT( QValueList<T> &lst, const T &value, int start )
00073 {
00074
00075 int st = start - 1;
00076 int end = lst.count();
00077 while ( end - st > 1 ) {
00078 int i = ( st + end ) / 2;
00079 if ( value < lst[i] ) {
00080 end = i;
00081 } else {
00082 st = i;
00083 }
00084 }
00085 ++st;
00086 return ( st == int( lst.count() ) ) ? -1 : st;
00087 }
00088
00089 template <class T>
00090 Q_INLINE_TEMPLATES int findSorted( QValueList<T> &lst, const T &value, int start )
00091 {
00092
00093 int st = start - 1;
00094 int end = lst.count();
00095 while ( end - st > 1 ) {
00096 int i = ( st + end ) / 2;
00097 if ( value < lst[i] ) {
00098 end = i;
00099 } else {
00100 st = i;
00101 }
00102 }
00103 return ( end > start && value == lst[st] ) ? st : -1;
00104 }
00105
00106 template <class T>
00107 Q_INLINE_TEMPLATES int removeSorted( QValueList<T> &lst, const T &value, int start )
00108 {
00109 int i = findSorted( lst, value, start );
00110 if ( i >= 0 ) {
00111 lst.remove( lst.at( i ) );
00112 }
00113 return i;
00114 }
00115
00116 namespace KCal {
00117
00118 typedef QValueList<QDateTime> DateTimeList;
00119 typedef QValueList<QDate> DateList;
00120 typedef QValueList<QTime> TimeList;
00121
00122
00123
00124
00128 class LIBKCAL_EXPORT RecurrenceRule
00129 {
00130 public:
00131 class Observer {
00132 public:
00133 virtual ~Observer() {}
00135 virtual void recurrenceChanged( RecurrenceRule * ) = 0;
00136 };
00137 typedef ListBase<RecurrenceRule> List;
00139 enum PeriodType { rNone = 0,
00140 rSecondly, rMinutely, rHourly,
00141 rDaily, rWeekly, rMonthly, rYearly
00142 };
00144 class WDayPos {
00145 public:
00146 WDayPos( int ps = 0 , short dy = 0 ) : mDay(dy), mPos(ps) {}
00147 short day() const { return mDay; }
00148 int pos() const { return mPos; }
00149 void setDay( short dy ) { mDay = dy; }
00150 void setPos( int ps ) { mPos = ps; }
00151
00152 bool operator==( const RecurrenceRule::WDayPos &pos2 ) const {
00153 return ( mDay == pos2.mDay ) && ( mPos == pos2.mPos );
00154 }
00155 protected:
00156 short mDay;
00157 int mPos;
00158
00159 };
00160
00161 RecurrenceRule( );
00162 RecurrenceRule(const RecurrenceRule&);
00163 ~RecurrenceRule();
00164
00165 bool operator==( const RecurrenceRule& ) const;
00166 bool operator!=( const RecurrenceRule& r ) const { return !operator==(r); }
00167 RecurrenceRule &operator=(const RecurrenceRule&);
00168
00169
00170
00171
00173 void setReadOnly(bool readOnly) { mIsReadOnly = readOnly; }
00175 bool isReadOnly() const { return mIsReadOnly; }
00176
00177
00180 bool doesRecur() const { return mPeriod!=rNone; }
00181 void setRecurrenceType( PeriodType period );
00182 PeriodType recurrenceType() const { return mPeriod; }
00184 void clear();
00185
00186
00188 uint frequency() const { return mFrequency; }
00190 void setFrequency( int freq );
00191
00192
00194 QDateTime startDt() const { return mDateStart; }
00196 void setStartDt(const QDateTime &start);
00197
00200 bool doesFloat() const { return mFloating; }
00202 void setFloats( bool floats );
00203
00204
00210 QDateTime endDt( bool* result = 0 ) const;
00213 void setEndDt(const QDateTime &endDateTime);
00214
00215
00220 int duration() const { return mDuration; }
00223 void setDuration(int duration);
00224
00225
00227 int durationTo(const QDateTime &) const;
00229 int durationTo( const QDate &date ) const { return durationTo( QDateTime( date, QTime( 23, 59, 59 ) ) ); }
00230
00231
00232
00235 bool recursOn( const QDate &qd ) const;
00239 bool recursAt( const QDateTime & ) const;
00244 bool dateMatchesRules( const QDateTime &qdt ) const;
00245
00246
00251 TimeList recurTimesOn( const QDate &date ) const;
00252
00264 DateTimeList timesInInterval( const QDateTime &start, const QDateTime &end ) const;
00265
00271 QDateTime getNextDate( const QDateTime& preDateTime ) const;
00278 QDateTime getPreviousDate( const QDateTime& afterDateTime ) const;
00279
00280
00281
00282
00283 void setBySeconds( const QValueList<int> bySeconds );
00284 void setByMinutes( const QValueList<int> byMinutes );
00285 void setByHours( const QValueList<int> byHours );
00286
00287 void setByDays( const QValueList<WDayPos> byDays );
00288 void setByMonthDays( const QValueList<int> byMonthDays );
00289 void setByYearDays( const QValueList<int> byYearDays );
00290 void setByWeekNumbers( const QValueList<int> byWeekNumbers );
00291 void setByMonths( const QValueList<int> byMonths );
00292 void setBySetPos( const QValueList<int> bySetPos );
00293 void setWeekStart( short weekStart );
00294
00295 const QValueList<int> &bySeconds() const { return mBySeconds; }
00296 const QValueList<int> &byMinutes() const { return mByMinutes; }
00297 const QValueList<int> &byHours() const { return mByHours; }
00298
00299 const QValueList<WDayPos> &byDays() const { return mByDays; }
00300 const QValueList<int> &byMonthDays() const { return mByMonthDays; }
00301 const QValueList<int> &byYearDays() const { return mByYearDays; }
00302 const QValueList<int> &byWeekNumbers() const { return mByWeekNumbers; }
00303 const QValueList<int> &byMonths() const { return mByMonths; }
00304 const QValueList<int> &bySetPos() const { return mBySetPos; }
00305 short weekStart() const { return mWeekStart; }
00306
00307
00308 void setDirty();
00316 void addObserver( Observer *observer );
00323 void removeObserver( Observer *observer );
00324
00328 void dump() const;
00329 QString mRRule;
00330
00331 private:
00332 class Constraint {
00333 public:
00334 typedef QValueList<Constraint> List;
00335
00336 Constraint( int wkst = 1 );
00337
00338
00339
00340
00341
00342 Constraint( const QDateTime &preDate, PeriodType type, int wkst );
00343 void clear();
00344
00345 int year;
00346 int month;
00347 int day;
00348 int hour;
00349 int minute;
00350 int second;
00351 int weekday;
00352 int weekdaynr;
00353 int weeknumber;
00354 int yearday;
00355 int weekstart;
00356
00357 bool readDateTime( const QDateTime &preDate, PeriodType type );
00358 bool matches( const QDate &dt, RecurrenceRule::PeriodType type ) const;
00359 bool matches( const QDateTime &dt, RecurrenceRule::PeriodType type ) const;
00360 bool isConsistent() const;
00361 bool isConsistent( PeriodType period ) const;
00362 bool increase( PeriodType type, int freq );
00363 QDateTime intervalDateTime( PeriodType type ) const;
00364 DateTimeList dateTimes( PeriodType type ) const;
00365 void dump() const;
00366 };
00367
00368 Constraint getNextValidDateInterval( const QDateTime &preDate, PeriodType type ) const;
00369 Constraint getPreviousValidDateInterval( const QDateTime &preDate, PeriodType type ) const;
00370 DateTimeList datesForInterval( const Constraint &interval, PeriodType type ) const;
00371 bool mergeIntervalConstraint( Constraint *merged, const Constraint &conit,
00372 const Constraint &interval ) const;
00373 bool buildCache() const;
00374
00375
00376 PeriodType mPeriod;
00377 QDateTime mDateStart;
00382 int mDuration;
00383 QDateTime mDateEnd;
00384 uint mFrequency;
00385
00386 bool mIsReadOnly;
00387 bool mFloating;
00388
00389 QValueList<int> mBySeconds;
00390 QValueList<int> mByMinutes;
00391 QValueList<int> mByHours;
00392
00393 QValueList<WDayPos> mByDays;
00394 QValueList<int> mByMonthDays;
00395 QValueList<int> mByYearDays;
00396 QValueList<int> mByWeekNumbers;
00397 QValueList<int> mByMonths;
00398 QValueList<int> mBySetPos;
00399 short mWeekStart;
00400
00401 Constraint::List mConstraints;
00402 void buildConstraints();
00403 bool mDirty;
00404 QValueList<Observer*> mObservers;
00405
00406
00407 mutable DateTimeList mCachedDates;
00408 mutable QDateTime mCachedDateEnd;
00409 mutable QDateTime mCachedLastDate;
00410 mutable bool mCached;
00411
00412 bool mNoByRules;
00413 uint mTimedRepetition;
00414
00415 class Private;
00416 Private *d;
00417 };
00418
00419 }
00420
00421 #endif
|