KCalCore Library
period.cpp
Go 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
00033 #include "period.h"
00034
00035 #include <KDateTime>
00036 #include <KSystemTimeZone>
00037
00038 #include <QtCore/QHash>
00039
00040 using namespace KCalCore;
00041
00042
00043 class KCalCore::Period::Private
00044 {
00045 public:
00046 Private() : mHasDuration( false ), mDailyDuration( false ) {}
00047 Private( const KDateTime &start, const KDateTime &end, bool hasDuration )
00048 : mStart( start ),
00049 mEnd( end ),
00050 mHasDuration( hasDuration ),
00051 mDailyDuration( false )
00052 {}
00053 KDateTime mStart;
00054 KDateTime mEnd;
00055 bool mHasDuration;
00056 bool mDailyDuration;
00057 };
00058
00059
00060 Period::Period() : d( new KCalCore::Period::Private() )
00061 {
00062 }
00063
00064 Period::Period( const KDateTime &start, const KDateTime &end )
00065 : d( new KCalCore::Period::Private( start, end, false ) )
00066 {
00067 }
00068
00069 Period::Period( const KDateTime &start, const Duration &duration )
00070 : d( new KCalCore::Period::Private( start, duration.end( start ), true ) )
00071 {
00072 d->mDailyDuration = duration.isDaily();
00073 }
00074
00075 Period::Period( const Period &period )
00076 : d( new KCalCore::Period::Private( *period.d ) )
00077 {
00078 }
00079
00080 Period::~Period()
00081 {
00082 delete d;
00083 }
00084
00085 bool Period::operator<( const Period &other ) const
00086 {
00087 return d->mStart < other.d->mStart;
00088 }
00089
00090 bool Period::operator==( const Period &other ) const
00091 {
00092 return
00093 ( ( d->mStart == other.d->mStart ) ||
00094 ( !d->mStart.isValid() && !other.d->mStart.isValid() ) ) &&
00095 ( ( d->mEnd == other.d->mEnd ) ||
00096 ( !d->mEnd.isValid() && !other.d->mEnd.isValid() ) ) &&
00097 d->mHasDuration == other.d->mHasDuration;
00098 }
00099
00100 Period &Period::operator=( const Period &other )
00101 {
00102
00103 if ( &other == this ) {
00104 return *this;
00105 }
00106
00107 *d = *other.d;
00108 return *this;
00109 }
00110
00111 KDateTime Period::start() const
00112 {
00113 return d->mStart;
00114 }
00115
00116 KDateTime Period::end() const
00117 {
00118 return d->mEnd;
00119 }
00120
00121 Duration Period::duration() const
00122 {
00123 if ( d->mHasDuration ) {
00124 return Duration( d->mStart, d->mEnd,
00125 d->mDailyDuration ? Duration::Days : Duration::Seconds );
00126 } else {
00127 return Duration( d->mStart, d->mEnd );
00128 }
00129 }
00130
00131 Duration Period::duration( Duration::Type type ) const
00132 {
00133 return Duration( d->mStart, d->mEnd, type );
00134 }
00135
00136 bool Period::hasDuration() const
00137 {
00138 return d->mHasDuration;
00139 }
00140
00141 void Period::shiftTimes( const KDateTime::Spec &oldSpec,
00142 const KDateTime::Spec &newSpec )
00143 {
00144 if ( oldSpec.isValid() && newSpec.isValid() && oldSpec != newSpec ) {
00145 d->mStart = d->mStart.toTimeSpec( oldSpec );
00146 d->mStart.setTimeSpec( newSpec );
00147 d->mEnd = d->mEnd.toTimeSpec( oldSpec );
00148 d->mEnd.setTimeSpec( newSpec );
00149 }
00150 }
00151
00152 QDataStream &KCalCore::operator<<( QDataStream &stream, const KCalCore::Period &period )
00153 {
00154 return stream << period.d->mStart
00155 << period.d->mEnd
00156 << period.d->mDailyDuration
00157 << period.d->mHasDuration;
00158 }
00159
00160 QDataStream &KCalCore::operator>>( QDataStream &stream, KCalCore::Period &period )
00161 {
00162 stream >> period.d->mStart
00163 >> period.d->mEnd
00164 >> period.d->mDailyDuration
00165 >> period.d->mHasDuration;
00166 return stream;
00167 }
00168
00169 uint qHash( const KCalCore::Period &key )
00170 {
00171 QString strToHash = key.start().toString();
00172 if( key.hasDuration() ) {
00173 strToHash += key.duration();
00174 } else {
00175 strToHash += key.end().toString();
00176 }
00177 return qHash( strToHash );
00178 }
00179