libkcal
recurrencerule.hGo to the documentation of this file.00001
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
00054 namespace KCal {
00055
00056 typedef QValueList<QDateTime> DateTimeList;
00057 typedef QValueList<QDate> DateList;
00058 typedef QValueList<QTime> TimeList;
00059
00060
00061
00062
00066 class LIBKCAL_EXPORT RecurrenceRule
00067 {
00068 public:
00069 class Observer {
00070 public:
00071 virtual ~Observer() {}
00073 virtual void recurrenceChanged( RecurrenceRule * ) = 0;
00074 };
00075 typedef ListBase<RecurrenceRule> List;
00077 enum PeriodType { rNone = 0,
00078 rSecondly, rMinutely, rHourly,
00079 rDaily, rWeekly, rMonthly, rYearly
00080 };
00082 class WDayPos {
00083 public:
00084 WDayPos( int ps = 0 , short dy = 0 ) : mDay(dy), mPos(ps) {}
00085 short day() const { return mDay; }
00086 int pos() const { return mPos; }
00087 void setDay( short dy ) { mDay = dy; }
00088 void setPos( int ps ) { mPos = ps; }
00089
00090 bool operator==( const RecurrenceRule::WDayPos &pos2 ) const {
00091 return ( mDay == pos2.mDay ) && ( mPos == pos2.mPos );
00092 }
00093 protected:
00094 short mDay;
00095 int mPos;
00096
00097 };
00098
00099 RecurrenceRule( );
00100 RecurrenceRule(const RecurrenceRule&);
00101 ~RecurrenceRule();
00102
00103 bool operator==( const RecurrenceRule& ) const;
00104 bool operator!=( const RecurrenceRule& r ) const { return !operator==(r); }
00105 RecurrenceRule &operator=(const RecurrenceRule&);
00106
00107
00108
00109
00111 void setReadOnly(bool readOnly) { mIsReadOnly = readOnly; }
00113 bool isReadOnly() const { return mIsReadOnly; }
00114
00115
00118 bool doesRecur() const { return mPeriod!=rNone; }
00119 void setRecurrenceType( PeriodType period );
00120 PeriodType recurrenceType() const { return mPeriod; }
00122 void clear();
00123
00124
00126 uint frequency() const { return mFrequency; }
00128 void setFrequency( int freq );
00129
00130
00132 QDateTime startDt() const { return mDateStart; }
00134 void setStartDt(const QDateTime &start);
00135
00138 bool doesFloat() const { return mFloating; }
00140 void setFloats( bool floats );
00141
00142
00148 QDateTime endDt( bool* result = 0 ) const;
00151 void setEndDt(const QDateTime &endDateTime);
00152
00153
00158 int duration() const { return mDuration; }
00161 void setDuration(int duration);
00162
00163
00165 int durationTo(const QDateTime &) const;
00167 int durationTo( const QDate &date ) const { return durationTo( QDateTime( date, QTime( 23, 59, 59 ) ) ); }
00168
00169
00170
00173 bool recursOn( const QDate &qd ) const;
00177 bool recursAt( const QDateTime & ) const;
00182 bool dateMatchesRules( const QDateTime &qdt ) const;
00183
00184
00189 TimeList recurTimesOn( const QDate &date ) const;
00190
00191
00197 QDateTime getNextDate( const QDateTime& preDateTime ) const;
00204 QDateTime getPreviousDate( const QDateTime& afterDateTime ) const;
00205
00206
00207
00208
00209 void setBySeconds( const QValueList<int> bySeconds );
00210 void setByMinutes( const QValueList<int> byMinutes );
00211 void setByHours( const QValueList<int> byHours );
00212
00213 void setByDays( const QValueList<WDayPos> byDays );
00214 void setByMonthDays( const QValueList<int> byMonthDays );
00215 void setByYearDays( const QValueList<int> byYearDays );
00216 void setByWeekNumbers( const QValueList<int> byWeekNumbers );
00217 void setByMonths( const QValueList<int> byMonths );
00218 void setBySetPos( const QValueList<int> bySetPos );
00219 void setWeekStart( short weekStart );
00220
00221 const QValueList<int> &bySeconds() const { return mBySeconds; }
00222 const QValueList<int> &byMinutes() const { return mByMinutes; }
00223 const QValueList<int> &byHours() const { return mByHours; }
00224
00225 const QValueList<WDayPos> &byDays() const { return mByDays; }
00226 const QValueList<int> &byMonthDays() const { return mByMonthDays; }
00227 const QValueList<int> &byYearDays() const { return mByYearDays; }
00228 const QValueList<int> &byWeekNumbers() const { return mByWeekNumbers; }
00229 const QValueList<int> &byMonths() const { return mByMonths; }
00230 const QValueList<int> &bySetPos() const { return mBySetPos; }
00231 short weekStart() const { return mWeekStart; }
00232
00233
00234 void setDirty();
00242 void addObserver( Observer *observer );
00249 void removeObserver( Observer *observer );
00250
00254 void dump() const;
00255 QString mRRule;
00256
00257 private:
00258 class Constraint {
00259 public:
00260 typedef QValueList<Constraint> List;
00261
00262 Constraint( int wkst = 1 );
00263
00264
00265
00266
00267
00268 Constraint( const QDateTime &preDate, PeriodType type, int wkst );
00269 void clear();
00270
00271 int year;
00272 int month;
00273 int day;
00274 int hour;
00275 int minute;
00276 int second;
00277 int weekday;
00278 int weekdaynr;
00279 int weeknumber;
00280 int yearday;
00281 int weekstart;
00282
00283 bool readDateTime( const QDateTime &preDate, PeriodType type );
00284 bool matches( const QDate &dt, RecurrenceRule::PeriodType type ) const;
00285 bool matches( const QDateTime &dt, RecurrenceRule::PeriodType type ) const;
00286 bool isConsistent() const;
00287 bool isConsistent( PeriodType period ) const;
00288 bool increase( PeriodType type, int freq );
00289 QDateTime intervalDateTime( PeriodType type ) const;
00290 DateTimeList dateTimes( PeriodType type ) const;
00291 void dump() const;
00292 };
00293
00294 Constraint getNextValidDateInterval( const QDateTime &preDate, PeriodType type ) const;
00295 Constraint getPreviousValidDateInterval( const QDateTime &preDate, PeriodType type ) const;
00296 DateTimeList datesForInterval( const Constraint &interval, PeriodType type ) const;
00297 bool mergeIntervalConstraint( Constraint *merged, const Constraint &conit,
00298 const Constraint &interval ) const;
00299 bool buildCache() const;
00300
00301
00302 PeriodType mPeriod;
00303 QDateTime mDateStart;
00308 int mDuration;
00309 QDateTime mDateEnd;
00310 uint mFrequency;
00311
00312 bool mIsReadOnly;
00313 bool mFloating;
00314
00315 QValueList<int> mBySeconds;
00316 QValueList<int> mByMinutes;
00317 QValueList<int> mByHours;
00318
00319 QValueList<WDayPos> mByDays;
00320 QValueList<int> mByMonthDays;
00321 QValueList<int> mByYearDays;
00322 QValueList<int> mByWeekNumbers;
00323 QValueList<int> mByMonths;
00324 QValueList<int> mBySetPos;
00325 short mWeekStart;
00326
00327 Constraint::List mConstraints;
00328 void buildConstraints();
00329 bool mDirty;
00330 QValueList<Observer*> mObservers;
00331
00332
00333 mutable DateTimeList mCachedDates;
00334 mutable bool mCached;
00335 mutable QDateTime mCachedDateEnd;
00336
00337 class Private;
00338 Private *d;
00339 };
00340
00341 }
00342
00343 #endif
|