libkcal Library API Documentation

incidence.cpp

00001 /*
00002     This file is part of libkcal.
00003     Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018     Boston, MA 02111-1307, USA.
00019 */
00020 
00021 #include <kglobal.h>
00022 #include <klocale.h>
00023 #include <kdebug.h>
00024 
00025 #include "calformat.h"
00026 
00027 #include "listbase.h"
00028 #include "incidence.h"
00029 
00030 using namespace KCal;
00031 
00032 Incidence::Incidence() :
00033   IncidenceBase(),
00034   mRelatedTo(0), mStatus(StatusNone), mSecrecy(SecrecyPublic),
00035   mPriority(3), mRecurrence(0)
00036 {
00037   recreate();
00038 
00039   mAlarms.setAutoDelete(true);
00040   mAttachments.setAutoDelete(true);
00041 }
00042 
00043 Incidence::Incidence( const Incidence &i ) : IncidenceBase( i )
00044 {
00045 // TODO: reenable attributes currently commented out.
00046   mRevision = i.mRevision;
00047   mCreated = i.mCreated;
00048   mDescription = i.mDescription;
00049   mSummary = i.mSummary;
00050   mCategories = i.mCategories;
00051 //  Incidence *mRelatedTo;          Incidence *mRelatedTo;
00052   mRelatedTo = 0;
00053   mRelatedToUid = i.mRelatedToUid;
00054 //  Incidence::List mRelations;    Incidence::List mRelations;
00055   mExDates = i.mExDates;
00056   mExDateTimes = i.mExDateTimes;
00057   mResources = i.mResources;
00058   mStatusString = i.mStatusString;
00059   mStatus = i.mStatus;
00060   mSecrecy = i.mSecrecy;
00061   mPriority = i.mPriority;
00062   mLocation = i.mLocation;
00063 
00064   // Alarms and Attachments are stored in ListBase<...>, which is a QValueList<...*>.
00065   // We need to really duplicate the objects stored therein, otherwise deleting
00066   // i will also delete all attachments from this object (setAutoDelete...)
00067   Alarm::List::ConstIterator it;
00068   for( it = i.mAlarms.begin(); it != i.mAlarms.end(); ++it ) {
00069     Alarm *b = new Alarm( **it );
00070     b->setParent( this );
00071     mAlarms.append( b );
00072   }
00073   mAlarms.setAutoDelete(true);
00074 
00075   Attachment::List::ConstIterator it1;
00076   for ( it1 = i.mAttachments.begin(); it1 != i.mAttachments.end(); ++it1 ) {
00077     Attachment *a = new Attachment( **it1 );
00078     mAttachments.append( a );
00079   }
00080   mAttachments.setAutoDelete( true );
00081 
00082   if (i.mRecurrence)
00083     mRecurrence = new Recurrence( *(i.mRecurrence), this );
00084   else
00085     mRecurrence = 0;
00086 
00087   mSchedulingID = i.mSchedulingID;
00088 }
00089 
00090 Incidence::~Incidence()
00091 {
00092     Incidence::List Relations = mRelations;
00093     List::ConstIterator it;
00094     for ( it = Relations.begin(); it != Relations.end(); ++it ) {
00095         if ( (*it)->relatedTo() == this ) (*it)->setRelatedTo( 0 );
00096     }
00097     if ( relatedTo() ) relatedTo()->removeRelation( this );
00098 
00099     delete mRecurrence;
00100 }
00101 
00102 // A string comparison that considers that null and empty are the same
00103 static bool stringCompare( const QString& s1, const QString& s2 )
00104 {
00105     if ( s1.isEmpty() && s2.isEmpty() )
00106         return true;
00107     return s1 == s2;
00108 }
00109 
00110 bool Incidence::operator==( const Incidence& i2 ) const
00111 {
00112     if( alarms().count() != i2.alarms().count() ) {
00113         return false; // no need to check further
00114     }
00115 
00116     Alarm::List::ConstIterator a1 = alarms().begin();
00117     Alarm::List::ConstIterator a2 = i2.alarms().begin();
00118     for( ; a1 != alarms().end() && a2 != i2.alarms().end(); ++a1, ++a2 )
00119         if( **a1 == **a2 )
00120             continue;
00121         else {
00122             return false;
00123         }
00124 
00125     if ( !IncidenceBase::operator==(i2) )
00126         return false;
00127 
00128     bool recurrenceEqual = ( mRecurrence == 0 && i2.mRecurrence == 0 );
00129     if ( !recurrenceEqual )
00130     {
00131         recurrenceEqual = mRecurrence != 0 &&
00132                           i2.mRecurrence != 0 &&
00133                           *mRecurrence == *i2.mRecurrence;
00134     }
00135 
00136     return
00137         recurrenceEqual &&
00138         created() == i2.created() &&
00139         stringCompare( description(), i2.description() ) &&
00140         stringCompare( summary(), i2.summary() ) &&
00141         categories() == i2.categories() &&
00142         // no need to compare mRelatedTo
00143         stringCompare( relatedToUid(), i2.relatedToUid() ) &&
00144         relations() == i2.relations() &&
00145         exDates() == i2.exDates() &&
00146         exDateTimes() == i2.exDateTimes() &&
00147         attachments() == i2.attachments() &&
00148         resources() == i2.resources() &&
00149         mStatus == i2.mStatus &&
00150         ( mStatus == StatusNone || stringCompare( mStatusString, i2.mStatusString ) ) &&
00151         secrecy() == i2.secrecy() &&
00152         priority() == i2.priority() &&
00153         stringCompare( location(), i2.location() ) &&
00154         stringCompare( schedulingID(), i2.schedulingID() );
00155 }
00156 
00157 
00158 void Incidence::recreate()
00159 {
00160   setCreated(QDateTime::currentDateTime());
00161 
00162   setUid(CalFormat::createUniqueId());
00163   setSchedulingID( QString::null );
00164 
00165   setRevision(0);
00166 
00167   setLastModified(QDateTime::currentDateTime());
00168   setPilotId( 0 );
00169   setSyncStatus( SYNCNONE );
00170 }
00171 
00172 void Incidence::setReadOnly( bool readOnly )
00173 {
00174   IncidenceBase::setReadOnly( readOnly );
00175   if (mRecurrence)
00176     mRecurrence->setRecurReadOnly(readOnly);
00177 }
00178 
00179 void Incidence::setCreated( const QDateTime &created )
00180 {
00181   if (mReadOnly) return;
00182   mCreated = created;
00183 }
00184 
00185 QDateTime Incidence::created() const
00186 {
00187   return mCreated;
00188 }
00189 
00190 void Incidence::setRevision( int rev )
00191 {
00192   if (mReadOnly) return;
00193   mRevision = rev;
00194 
00195   updated();
00196 }
00197 
00198 int Incidence::revision() const
00199 {
00200   return mRevision;
00201 }
00202 
00203 void Incidence::setDtStart(const QDateTime &dtStart)
00204 {
00205   if (mRecurrence)
00206     mRecurrence->setRecurStart( dtStart );
00207   IncidenceBase::setDtStart( dtStart );
00208 }
00209 
00210 void Incidence::setDescription(const QString &description)
00211 {
00212   if (mReadOnly) return;
00213   mDescription = description;
00214   updated();
00215 }
00216 
00217 QString Incidence::description() const
00218 {
00219   return mDescription;
00220 }
00221 
00222 
00223 void Incidence::setSummary(const QString &summary)
00224 {
00225   if (mReadOnly) return;
00226   mSummary = summary;
00227   updated();
00228 }
00229 
00230 QString Incidence::summary() const
00231 {
00232   return mSummary;
00233 }
00234 
00235 void Incidence::setCategories(const QStringList &categories)
00236 {
00237   if (mReadOnly) return;
00238   mCategories = categories;
00239   updated();
00240 }
00241 
00242 // TODO: remove setCategories(QString) function
00243 void Incidence::setCategories(const QString &catStr)
00244 {
00245   if (mReadOnly) return;
00246   mCategories.clear();
00247 
00248   if (catStr.isEmpty()) return;
00249 
00250   mCategories = QStringList::split(",",catStr);
00251 
00252   QStringList::Iterator it;
00253   for(it = mCategories.begin();it != mCategories.end(); ++it) {
00254     *it = (*it).stripWhiteSpace();
00255   }
00256 
00257   updated();
00258 }
00259 
00260 QStringList Incidence::categories() const
00261 {
00262   return mCategories;
00263 }
00264 
00265 QString Incidence::categoriesStr() const
00266 {
00267   return mCategories.join(",");
00268 }
00269 
00270 void Incidence::setRelatedToUid(const QString &relatedToUid)
00271 {
00272   if ( mReadOnly || mRelatedToUid == relatedToUid ) return;
00273   mRelatedToUid = relatedToUid;
00274   updated();
00275 }
00276 
00277 QString Incidence::relatedToUid() const
00278 {
00279   return mRelatedToUid;
00280 }
00281 
00282 void Incidence::setRelatedTo(Incidence *relatedTo)
00283 {
00284   if (mReadOnly || mRelatedTo == relatedTo) return;
00285   if(mRelatedTo)
00286     mRelatedTo->removeRelation(this);
00287   mRelatedTo = relatedTo;
00288   if (mRelatedTo) {
00289     mRelatedTo->addRelation(this);
00290     if ( mRelatedTo->uid() != mRelatedToUid )
00291       setRelatedToUid( mRelatedTo->uid() );
00292   }
00293 }
00294 
00295 Incidence *Incidence::relatedTo() const
00296 {
00297   return mRelatedTo;
00298 }
00299 
00300 Incidence::List Incidence::relations() const
00301 {
00302   return mRelations;
00303 }
00304 
00305 void Incidence::addRelation( Incidence *event )
00306 {
00307   if ( mRelations.find( event ) == mRelations.end() ) {
00308     mRelations.append( event );
00309   }
00310 }
00311 
00312 void Incidence::removeRelation(Incidence *event)
00313 {
00314   mRelations.removeRef(event);
00315 //  if (event->getRelatedTo() == this) event->setRelatedTo(0);
00316 }
00317 
00318 bool Incidence::recursOn(const QDate &qd) const
00319 {
00320   return (mRecurrence && mRecurrence->recursOnPure(qd) && !isException(qd));
00321 }
00322 
00323 bool Incidence::recursAt(const QDateTime &qdt) const
00324 {
00325   return (mRecurrence && mRecurrence->recursAtPure(qdt) && !isException(qdt.date()) && !isException(qdt));
00326 }
00327 
00328 void Incidence::setExDates(const DateList &exDates)
00329 {
00330   if (mReadOnly) return;
00331   mExDates = exDates;
00332   updated();
00333 }
00334 
00335 void Incidence::setExDateTimes(const DateTimeList &exDateTimes)
00336 {
00337   if (mReadOnly) return;
00338   mExDateTimes = exDateTimes;
00339   updated();
00340 }
00341 
00342 void Incidence::addExDate(const QDate &date)
00343 {
00344   if (mReadOnly) return;
00345   mExDates.append(date);
00346   updated();
00347 }
00348 
00349 void Incidence::addExDateTime(const QDateTime &dateTime)
00350 {
00351   if (mReadOnly) return;
00352   mExDateTimes.append(dateTime);
00353   updated();
00354 }
00355 
00356 DateList Incidence::exDates() const
00357 {
00358   return mExDates;
00359 }
00360 
00361 DateTimeList Incidence::exDateTimes() const
00362 {
00363   return mExDateTimes;
00364 }
00365 
00366 bool Incidence::isException(const QDate &date) const
00367 {
00368   DateList::ConstIterator it;
00369   for( it = mExDates.begin(); it != mExDates.end(); ++it ) {
00370     if ( (*it) == date ) {
00371       return true;
00372     }
00373   }
00374 
00375   return false;
00376 }
00377 
00378 bool Incidence::isException(const QDateTime &dateTime) const
00379 {
00380   DateTimeList::ConstIterator it;
00381   for( it = mExDateTimes.begin(); it != mExDateTimes.end(); ++it ) {
00382     if ( (*it) == dateTime ) {
00383       return true;
00384     }
00385   }
00386 
00387   return false;
00388 }
00389 
00390 void Incidence::addAttachment(Attachment *attachment)
00391 {
00392   if (mReadOnly || !attachment) return;
00393   mAttachments.append(attachment);
00394   updated();
00395 }
00396 
00397 void Incidence::deleteAttachment(Attachment *attachment)
00398 {
00399   mAttachments.removeRef(attachment);
00400 }
00401 
00402 void Incidence::deleteAttachments( const QString &mime )
00403 {
00404   Attachment::List::Iterator it = mAttachments.begin();
00405   while( it != mAttachments.end() ) {
00406     if ( (*it)->mimeType() == mime ) mAttachments.remove( it );
00407     else ++it;
00408   }
00409 }
00410 
00411 Attachment::List Incidence::attachments() const
00412 {
00413   return mAttachments;
00414 }
00415 
00416 Attachment::List Incidence::attachments(const QString& mime) const
00417 {
00418   Attachment::List attachments;
00419   Attachment::List::ConstIterator it;
00420   for( it = mAttachments.begin(); it != mAttachments.end(); ++it ) {
00421     if ( (*it)->mimeType() == mime ) attachments.append( *it );
00422   }
00423 
00424   return attachments;
00425 }
00426 
00427 void Incidence::clearAttachments()
00428 {
00429   mAttachments.clear();
00430 }
00431 
00432 void Incidence::setResources(const QStringList &resources)
00433 {
00434   if (mReadOnly) return;
00435   mResources = resources;
00436   updated();
00437 }
00438 
00439 QStringList Incidence::resources() const
00440 {
00441   return mResources;
00442 }
00443 
00444 
00445 void Incidence::setPriority(int priority)
00446 {
00447   if (mReadOnly) return;
00448   mPriority = priority;
00449   updated();
00450 }
00451 
00452 int Incidence::priority() const
00453 {
00454   return mPriority;
00455 }
00456 
00457 void Incidence::setStatus(Incidence::Status status)
00458 {
00459   if (mReadOnly || status == StatusX) return;
00460   mStatus = status;
00461   mStatusString = QString::null;
00462   updated();
00463 }
00464 
00465 void Incidence::setCustomStatus(const QString &status)
00466 {
00467   if (mReadOnly) return;
00468   mStatus = status.isEmpty() ? StatusNone : StatusX;
00469   mStatusString = status;
00470   updated();
00471 }
00472 
00473 Incidence::Status Incidence::status() const
00474 {
00475   return mStatus;
00476 }
00477 
00478 QString Incidence::statusStr() const
00479 {
00480   if (mStatus == StatusX)
00481     return mStatusString;
00482   return statusName(mStatus);
00483 }
00484 
00485 QString Incidence::statusName(Incidence::Status status)
00486 {
00487   switch (status) {
00488     case StatusTentative:    return i18n("Tentative");
00489     case StatusConfirmed:    return i18n("Confirmed");
00490     case StatusCompleted:    return i18n("Completed");
00491     case StatusNeedsAction:  return i18n("Needs-Action");
00492     case StatusCanceled:     return i18n("Canceled");
00493     case StatusInProcess:    return i18n("In-Process");
00494     case StatusDraft:        return i18n("Draft");
00495     case StatusFinal:        return i18n("Final");
00496     case StatusX:
00497     case StatusNone:
00498     default:                 return QString::null;
00499   }
00500 }
00501 
00502 void Incidence::setSecrecy(int sec)
00503 {
00504   if (mReadOnly) return;
00505   mSecrecy = sec;
00506   updated();
00507 }
00508 
00509 int Incidence::secrecy() const
00510 {
00511   return mSecrecy;
00512 }
00513 
00514 QString Incidence::secrecyStr() const
00515 {
00516   return secrecyName(mSecrecy);
00517 }
00518 
00519 QString Incidence::secrecyName(int secrecy)
00520 {
00521   switch (secrecy) {
00522     case SecrecyPublic:
00523       return i18n("Public");
00524     case SecrecyPrivate:
00525       return i18n("Private");
00526     case SecrecyConfidential:
00527       return i18n("Confidential");
00528     default:
00529       return i18n("Undefined");
00530   }
00531 }
00532 
00533 QStringList Incidence::secrecyList()
00534 {
00535   QStringList list;
00536   list << secrecyName(SecrecyPublic);
00537   list << secrecyName(SecrecyPrivate);
00538   list << secrecyName(SecrecyConfidential);
00539 
00540   return list;
00541 }
00542 
00543 
00544 const Alarm::List &Incidence::alarms() const
00545 {
00546   return mAlarms;
00547 }
00548 
00549 Alarm* Incidence::newAlarm()
00550 {
00551   Alarm* alarm = new Alarm(this);
00552   mAlarms.append(alarm);
00553 //  updated();
00554   return alarm;
00555 }
00556 
00557 void Incidence::addAlarm(Alarm *alarm)
00558 {
00559   mAlarms.append(alarm);
00560   updated();
00561 }
00562 
00563 void Incidence::removeAlarm(Alarm *alarm)
00564 {
00565   mAlarms.removeRef(alarm);
00566   updated();
00567 }
00568 
00569 void Incidence::clearAlarms()
00570 {
00571   mAlarms.clear();
00572   updated();
00573 }
00574 
00575 bool Incidence::isAlarmEnabled() const
00576 {
00577   Alarm::List::ConstIterator it;
00578   for( it = mAlarms.begin(); it != mAlarms.end(); ++it ) {
00579     if ( (*it)->enabled() ) return true;
00580   }
00581   return false;
00582 }
00583 
00584 Recurrence *Incidence::recurrence() const
00585 {
00586   if (!mRecurrence)
00587   {
00588     const_cast<KCal::Incidence*>(this)->mRecurrence = new Recurrence(const_cast<KCal::Incidence*>(this));
00589     mRecurrence->setRecurReadOnly(mReadOnly);
00590     mRecurrence->setRecurStart(dtStart());
00591   }
00592 
00593   return mRecurrence;
00594 }
00595 
00596 void Incidence::setLocation(const QString &location)
00597 {
00598   if (mReadOnly) return;
00599   mLocation = location;
00600   updated();
00601 }
00602 
00603 QString Incidence::location() const
00604 {
00605   return mLocation;
00606 }
00607 
00608 ushort Incidence::doesRecur() const
00609 {
00610   if ( mRecurrence ) return mRecurrence->doesRecur();
00611   else return Recurrence::rNone;
00612 }
00613 
00614 void Incidence::setSchedulingID( const QString& sid )
00615 {
00616   mSchedulingID = sid;
00617 }
00618 
00619 QString Incidence::schedulingID() const
00620 {
00621   if ( mSchedulingID.isNull() )
00622     // Nothing set, so use the normal uid
00623     return uid();
00624   return mSchedulingID;
00625 }
KDE Logo
This file is part of the documentation for libkcal Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu May 3 20:17:56 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003