KCal Library
incidencebase.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
00035 #include "incidencebase.h"
00036 #include "calformat.h"
00037
00038 #include <kglobal.h>
00039 #include <klocale.h>
00040 #include <kdebug.h>
00041 #include <kurl.h>
00042 #include <ksystemtimezone.h>
00043
00044 #include <QtCore/QList>
00045
00046 using namespace KCal;
00047
00052
00053 class KCal::IncidenceBase::Private
00054 {
00055 public:
00056 Private()
00057 : mUpdateGroupLevel( 0 ),
00058 mUpdatedPending( false ),
00059 mAllDay( true ),
00060 mHasDuration( false )
00061 { mAttendees.setAutoDelete( true ); }
00062
00063 Private( const Private &other )
00064 {
00065 mAttendees.setAutoDelete( true );
00066 init( other );
00067 }
00068
00069 void init( const Private &other );
00070
00071 KDateTime mLastModified;
00072 KDateTime mDtStart;
00073 Person mOrganizer;
00074 QString mUid;
00075 Duration mDuration;
00076 int mUpdateGroupLevel;
00077 bool mUpdatedPending;
00078 bool mAllDay;
00079 bool mHasDuration;
00080
00081 Attendee::List mAttendees;
00082 QStringList mComments;
00083 QList<IncidenceObserver*> mObservers;
00084 };
00085
00086 void IncidenceBase::Private::init( const Private &other )
00087 {
00088 mLastModified = other.mLastModified;
00089 mDtStart = other.mDtStart;
00090 mOrganizer = other.mOrganizer;
00091 mUid = other.mUid;
00092 mDuration = other.mDuration;
00093 mAllDay = other.mAllDay;
00094 mHasDuration = other.mHasDuration;
00095 mComments = other.mComments;
00096
00097 mAttendees.clearAll();
00098 Attendee::List::ConstIterator it;
00099 for ( it = other.mAttendees.begin(); it != other.mAttendees.end(); ++it ) {
00100 mAttendees.append( new Attendee( *(*it) ) );
00101 }
00102 }
00103
00104
00105 IncidenceBase::IncidenceBase()
00106 : d( new KCal::IncidenceBase::Private )
00107 {
00108 mReadOnly = false;
00109
00110 setUid( CalFormat::createUniqueId() );
00111 }
00112
00113 IncidenceBase::IncidenceBase( const IncidenceBase &i )
00114 : CustomProperties( i ),
00115 d( new KCal::IncidenceBase::Private( *i.d ) )
00116 {
00117 mReadOnly = i.mReadOnly;
00118 }
00119
00120 IncidenceBase::~IncidenceBase()
00121 {
00122 delete d;
00123 }
00124
00125 IncidenceBase &IncidenceBase::operator=( const IncidenceBase &other )
00126 {
00127 CustomProperties::operator=( other );
00128 d->init( *other.d );
00129 mReadOnly = other.mReadOnly;
00130 return *this;
00131 }
00132
00133 bool IncidenceBase::operator==( const IncidenceBase &i2 ) const
00134 {
00135 if ( attendees().count() != i2.attendees().count() ) {
00136 return false;
00137 }
00138
00139 Attendee::List al1 = attendees();
00140 Attendee::List al2 = i2.attendees();
00141 Attendee::List::ConstIterator a1 = al1.begin();
00142 Attendee::List::ConstIterator a2 = al2.begin();
00143
00144
00145 for ( ; a1 != al1.end() && a2 != al2.end(); ++a1, ++a2 ) {
00146 if ( !( **a1 == **a2 ) ) {
00147 return false;
00148 }
00149 }
00150
00151 if ( !CustomProperties::operator == (i2) ) {
00152 return false;
00153 }
00154
00155 return
00156 dtStart() == i2.dtStart() &&
00157 organizer() == i2.organizer() &&
00158 uid() == i2.uid() &&
00159
00160
00161 allDay() == i2.allDay() &&
00162 duration() == i2.duration() &&
00163 hasDuration() == i2.hasDuration();
00164
00165 }
00166
00167 void IncidenceBase::setUid( const QString &uid )
00168 {
00169 d->mUid = uid;
00170 updated();
00171 }
00172
00173 QString IncidenceBase::uid() const
00174 {
00175 return d->mUid;
00176 }
00177
00178 void IncidenceBase::setLastModified( const KDateTime &lm )
00179 {
00180
00181
00182
00183
00184 KDateTime current = lm.toUtc();
00185 QTime t = current.time();
00186 t.setHMS( t.hour(), t.minute(), t.second(), 0 );
00187 current.setTime( t );
00188
00189 d->mLastModified = current;
00190 }
00191
00192 KDateTime IncidenceBase::lastModified() const
00193 {
00194 return d->mLastModified;
00195 }
00196
00197 void IncidenceBase::setOrganizer( const Person &o )
00198 {
00199
00200
00201
00202 d->mOrganizer = o;
00203
00204 updated();
00205 }
00206
00207 void IncidenceBase::setOrganizer( const QString &o )
00208 {
00209 QString mail( o );
00210 if ( mail.startsWith( "MAILTO:", Qt::CaseInsensitive ) ) {
00211 mail = mail.remove( 0, 7 );
00212 }
00213
00214
00215 const Person organizer = Person::fromFullName( mail );
00216 setOrganizer( organizer );
00217 }
00218
00219 Person IncidenceBase::organizer() const
00220 {
00221 return d->mOrganizer;
00222 }
00223
00224 void IncidenceBase::setReadOnly( bool readOnly )
00225 {
00226 mReadOnly = readOnly;
00227 }
00228
00229 void IncidenceBase::setDtStart( const KDateTime &dtStart )
00230 {
00231
00232 d->mDtStart = dtStart;
00233 d->mAllDay = dtStart.isDateOnly();
00234 updated();
00235 }
00236
00237 KDateTime IncidenceBase::dtStart() const
00238 {
00239 return d->mDtStart;
00240 }
00241
00242 QString IncidenceBase::dtStartTimeStr( bool shortfmt, const KDateTime::Spec &spec ) const
00243 {
00244 if ( spec.isValid() ) {
00245
00246 QString timeZone;
00247 if ( spec.timeZone() != KSystemTimeZones::local() ) {
00248 timeZone = ' ' + spec.timeZone().name();
00249 }
00250
00251 return KGlobal::locale()->formatTime( dtStart().toTimeSpec( spec ).time(), shortfmt )
00252 + timeZone;
00253 } else {
00254 return KGlobal::locale()->formatTime( dtStart().time(), shortfmt );
00255 }
00256 }
00257
00258 QString IncidenceBase::dtStartDateStr( bool shortfmt, const KDateTime::Spec &spec ) const
00259 {
00260 if ( spec.isValid() ) {
00261
00262 QString timeZone;
00263 if ( spec.timeZone() != KSystemTimeZones::local() ) {
00264 timeZone = ' ' + spec.timeZone().name();
00265 }
00266
00267 return KGlobal::locale()->formatDate(
00268 dtStart().toTimeSpec( spec ).date(), ( shortfmt ? KLocale::ShortDate : KLocale::LongDate ) )
00269 + timeZone;
00270 } else {
00271 return KGlobal::locale()->formatDate(
00272 dtStart().date(), ( shortfmt ? KLocale::ShortDate : KLocale::LongDate ) );
00273 }
00274 }
00275
00276 QString IncidenceBase::dtStartStr( bool shortfmt, const KDateTime::Spec &spec ) const
00277 {
00278 if ( allDay() ) {
00279 return dtStartDateStr( shortfmt, spec );
00280 }
00281
00282 if ( spec.isValid() ) {
00283
00284 QString timeZone;
00285 if ( spec.timeZone() != KSystemTimeZones::local() ) {
00286 timeZone = ' ' + spec.timeZone().name();
00287 }
00288
00289 return KGlobal::locale()->formatDateTime(
00290 dtStart().toTimeSpec( spec ).dateTime(),
00291 ( shortfmt ? KLocale::ShortDate : KLocale::LongDate ) ) + timeZone;
00292 } else {
00293 return KGlobal::locale()->formatDateTime(
00294 dtStart().dateTime(),
00295 ( shortfmt ? KLocale::ShortDate : KLocale::LongDate ) );
00296 }
00297 }
00298
00299 bool IncidenceBase::allDay() const
00300 {
00301 return d->mAllDay;
00302 }
00303
00304 void IncidenceBase::setAllDay( bool f )
00305 {
00306 if ( mReadOnly || f == d->mAllDay ) {
00307 return;
00308 }
00309 d->mAllDay = f;
00310 updated();
00311 }
00312
00313 void IncidenceBase::shiftTimes( const KDateTime::Spec &oldSpec,
00314 const KDateTime::Spec &newSpec )
00315 {
00316 d->mDtStart = d->mDtStart.toTimeSpec( oldSpec );
00317 d->mDtStart.setTimeSpec( newSpec );
00318 updated();
00319 }
00320
00321 void IncidenceBase::addComment( const QString &comment )
00322 {
00323 d->mComments += comment;
00324 }
00325
00326 bool IncidenceBase::removeComment( const QString &comment )
00327 {
00328 bool found = false;
00329 QStringList::Iterator i;
00330
00331 for ( i = d->mComments.begin(); !found && i != d->mComments.end(); ++i ) {
00332 if ( (*i) == comment ) {
00333 found = true;
00334 d->mComments.erase( i );
00335 }
00336 }
00337
00338 return found;
00339 }
00340
00341 void IncidenceBase::clearComments()
00342 {
00343 d->mComments.clear();
00344 }
00345
00346 QStringList IncidenceBase::comments() const
00347 {
00348 return d->mComments;
00349 }
00350
00351 void IncidenceBase::addAttendee( Attendee *a, bool doupdate )
00352 {
00353 if ( mReadOnly ) {
00354 return;
00355 }
00356
00357 if ( a->name().left(7).toUpper() == "MAILTO:" ) {
00358 a->setName( a->name().remove( 0, 7 ) );
00359 }
00360
00361 d->mAttendees.append( a );
00362 if ( doupdate ) {
00363 updated();
00364 }
00365 }
00366
00367 const Attendee::List &IncidenceBase::attendees() const
00368 {
00369 return d->mAttendees;
00370 }
00371
00372 int IncidenceBase::attendeeCount() const
00373 {
00374 return d->mAttendees.count();
00375 }
00376
00377 void IncidenceBase::clearAttendees()
00378 {
00379 if ( mReadOnly ) {
00380 return;
00381 }
00382 qDeleteAll( d->mAttendees );
00383 d->mAttendees.clear();
00384 }
00385
00386 Attendee *IncidenceBase::attendeeByMail( const QString &email ) const
00387 {
00388 Attendee::List::ConstIterator it;
00389 for ( it = d->mAttendees.begin(); it != d->mAttendees.end(); ++it ) {
00390 if ( (*it)->email() == email ) {
00391 return *it;
00392 }
00393 }
00394
00395 return 0;
00396 }
00397
00398 Attendee *IncidenceBase::attendeeByMails( const QStringList &emails,
00399 const QString &email ) const
00400 {
00401 QStringList mails = emails;
00402 if ( !email.isEmpty() ) {
00403 mails.append( email );
00404 }
00405
00406 Attendee::List::ConstIterator itA;
00407 for ( itA = d->mAttendees.begin(); itA != d->mAttendees.end(); ++itA ) {
00408 for ( QStringList::Iterator it = mails.begin(); it != mails.end(); ++it ) {
00409 if ( (*itA)->email() == (*it) ) {
00410 return *itA;
00411 }
00412 }
00413 }
00414
00415 return 0;
00416 }
00417
00418 Attendee *IncidenceBase::attendeeByUid( const QString &uid ) const
00419 {
00420 Attendee::List::ConstIterator it;
00421 for ( it = d->mAttendees.begin(); it != d->mAttendees.end(); ++it ) {
00422 if ( (*it)->uid() == uid ) {
00423 return *it;
00424 }
00425 }
00426
00427 return 0;
00428 }
00429
00430 void IncidenceBase::setDuration( const Duration &duration )
00431 {
00432 d->mDuration = duration;
00433 setHasDuration( true );
00434 updated();
00435 }
00436
00437 Duration IncidenceBase::duration() const
00438 {
00439 return d->mDuration;
00440 }
00441
00442 void IncidenceBase::setHasDuration( bool hasDuration )
00443 {
00444 d->mHasDuration = hasDuration;
00445 }
00446
00447 bool IncidenceBase::hasDuration() const
00448 {
00449 return d->mHasDuration;
00450 }
00451
00452 void IncidenceBase::registerObserver( IncidenceBase::IncidenceObserver *observer )
00453 {
00454 if ( !d->mObservers.contains( observer ) ) {
00455 d->mObservers.append( observer );
00456 }
00457 }
00458
00459 void IncidenceBase::unRegisterObserver( IncidenceBase::IncidenceObserver *observer )
00460 {
00461 d->mObservers.removeAll( observer );
00462 }
00463
00464 void IncidenceBase::updated()
00465 {
00466 if ( d->mUpdateGroupLevel ) {
00467 d->mUpdatedPending = true;
00468 } else {
00469 foreach ( IncidenceObserver *o, d->mObservers ) {
00470 o->incidenceUpdated( this );
00471 }
00472 }
00473 }
00474
00475 void IncidenceBase::startUpdates()
00476 {
00477 ++d->mUpdateGroupLevel;
00478 }
00479
00480 void IncidenceBase::endUpdates()
00481 {
00482 if ( d->mUpdateGroupLevel > 0 ) {
00483 if ( --d->mUpdateGroupLevel == 0 && d->mUpdatedPending ) {
00484 d->mUpdatedPending = false;
00485 updated();
00486 }
00487 }
00488 }
00489
00490 void IncidenceBase::customPropertyUpdated()
00491 {
00492 updated();
00493 }
00494
00495 KUrl IncidenceBase::uri() const
00496 {
00497 return KUrl( QString( "urn:x-ical:" ) + uid() );
00498 }
00499
00500 bool IncidenceBase::Visitor::visit( Event *event )
00501 {
00502 Q_UNUSED( event );
00503 return false;
00504 }
00505
00506 bool IncidenceBase::Visitor::visit( Todo *todo )
00507 {
00508 Q_UNUSED( todo );
00509 return false;
00510 }
00511
00512 bool IncidenceBase::Visitor::visit( Journal *journal )
00513 {
00514 Q_UNUSED( journal );
00515 return false;
00516 }
00517
00518 bool IncidenceBase::Visitor::visit( FreeBusy *freebusy )
00519 {
00520 Q_UNUSED( freebusy );
00521 return false;
00522 }