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