libkcal Library API Documentation

incidencebase.cpp

00001 /*
00002     This file is part of libkcal.
00003 
00004     Copyright (c) 2001,2004 Cornelius Schumacher <schumacher@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019     Boston, MA 02111-1307, USA.
00020 */
00021 
00022 #include <kglobal.h>
00023 #include <klocale.h>
00024 #include <kdebug.h>
00025 
00026 #include "calformat.h"
00027 
00028 #include "incidencebase.h"
00029 
00030 using namespace KCal;
00031 
00032 IncidenceBase::IncidenceBase()
00033   : mReadOnly( false ), mFloats( true ), mDuration( 0 ), mHasDuration( false ),
00034     mPilotId( 0 ), mSyncStatus( SYNCMOD )
00035 {
00036   setUid( CalFormat::createUniqueId() );
00037 
00038   mAttendees.setAutoDelete( true );
00039 }
00040 
00041 IncidenceBase::IncidenceBase(const IncidenceBase &i) :
00042   CustomProperties( i )
00043 {
00044   mReadOnly = i.mReadOnly;
00045   mDtStart = i.mDtStart;
00046   mDuration = i.mDuration;
00047   mHasDuration = i.mHasDuration;
00048   mOrganizer = i.mOrganizer;
00049   mUid = i.mUid;
00050   Attendee::List attendees = i.attendees();
00051   Attendee::List::ConstIterator it;
00052   for( it = attendees.begin(); it != attendees.end(); ++it ) {
00053     mAttendees.append( new Attendee( *(*it) ) );
00054   }
00055   mFloats = i.mFloats;
00056   mLastModified = i.mLastModified;
00057   mPilotId = i.mPilotId;
00058   mSyncStatus = i.mSyncStatus;
00059 
00060   // The copied object is a new one, so it isn't observed by the observer
00061   // of the original object.
00062   mObservers.clear();
00063 
00064   mAttendees.setAutoDelete( true );
00065 }
00066 
00067 IncidenceBase::~IncidenceBase()
00068 {
00069 }
00070 
00071 
00072 bool IncidenceBase::operator==( const IncidenceBase& i2 ) const
00073 {
00074   if( attendees().count() != i2.attendees().count() ) {
00075       return false; // no need to check further
00076   }
00077 
00078   Attendee::List al1 = attendees();
00079   Attendee::List al2 = i2.attendees();
00080   Attendee::List::ConstIterator a1 = al1.begin();
00081   Attendee::List::ConstIterator a2 = al2.begin();
00082   for( ; a1 != al1.end() && a2 != al2.end(); ++a1, ++a2 ) {
00083     if( **a1 == **a2 )
00084         continue;
00085     else {
00086         return false;
00087     }
00088   }
00089 
00090   return ( dtStart() == i2.dtStart() &&
00091            organizer() == i2.organizer() &&
00092            uid() == i2.uid() &&
00093            // Don't compare lastModified, otherwise the operator is not
00094            // of much use. We are not comparing for identity, after all.
00095            doesFloat() == i2.doesFloat() &&
00096            duration() == i2.duration() &&
00097            hasDuration() == i2.hasDuration() &&
00098            pilotId() == i2.pilotId() &&
00099            syncStatus() == i2.syncStatus() );
00100   // no need to compare mObserver
00101 }
00102 
00103 
00104 
00105 
00106 void IncidenceBase::setUid(const QString &uid)
00107 {
00108   mUid = uid;
00109   updated();
00110 }
00111 
00112 QString IncidenceBase::uid() const
00113 {
00114   return mUid;
00115 }
00116 
00117 void IncidenceBase::setLastModified(const QDateTime &lm)
00118 {
00119   // DON'T! updated() because we call this from
00120   // Calendar::updateEvent().
00121 
00122   // Remove milliseconds part.
00123   QDateTime current = lm;
00124   QTime t = current.time();
00125   t.setHMS( t.hour(), t.minute(), t.second(), 0 );
00126   current.setTime( t );
00127 
00128   mLastModified = current;
00129 }
00130 
00131 QDateTime IncidenceBase::lastModified() const
00132 {
00133   return mLastModified;
00134 }
00135 
00136 void IncidenceBase::setOrganizer( const Person &o )
00137 {
00138   // we don't check for readonly here, because it is
00139   // possible that by setting the organizer we are changing
00140   // the event's readonly status...
00141   mOrganizer = o;
00142 
00143   updated();
00144 }
00145 
00146 void IncidenceBase::setOrganizer(const QString &o)
00147 {
00148   QString mail( o );
00149   if ( mail.startsWith("mailto:", false) )
00150     mail = mail.remove( 0, 7 );
00151   // split the string into full name plus email.
00152   Person organizer( mail );
00153   setOrganizer( organizer );
00154 }
00155 
00156 Person IncidenceBase::organizer() const
00157 {
00158   return mOrganizer;
00159 }
00160 
00161 void IncidenceBase::setReadOnly( bool readOnly )
00162 {
00163   mReadOnly = readOnly;
00164 }
00165 
00166 void IncidenceBase::setDtStart(const QDateTime &dtStart)
00167 {
00168 //  if (mReadOnly) return;
00169   mDtStart = dtStart;
00170   updated();
00171 }
00172 
00173 QDateTime IncidenceBase::dtStart() const
00174 {
00175   return mDtStart;
00176 }
00177 
00178 QString IncidenceBase::dtStartTimeStr() const
00179 {
00180   return KGlobal::locale()->formatTime(dtStart().time());
00181 }
00182 
00183 QString IncidenceBase::dtStartDateStr(bool shortfmt) const
00184 {
00185   return KGlobal::locale()->formatDate(dtStart().date(),shortfmt);
00186 }
00187 
00188 QString IncidenceBase::dtStartStr() const
00189 {
00190   return KGlobal::locale()->formatDateTime(dtStart());
00191 }
00192 
00193 
00194 bool IncidenceBase::doesFloat() const
00195 {
00196   return mFloats;
00197 }
00198 
00199 void IncidenceBase::setFloats(bool f)
00200 {
00201   if (mReadOnly) return;
00202   mFloats = f;
00203   updated();
00204 }
00205 
00206 
00207 void IncidenceBase::addComment(const QString& comment)
00208 {
00209   mComments += comment;
00210 }
00211 
00212 bool IncidenceBase::removeComment(QString& comment)
00213 {
00214   bool found = false;
00215   QStringList::Iterator i;
00216 
00217   for ( i = mComments.begin(); !found && i != mComments.end(); ++i ) {
00218     if ( (*i) == comment) {
00219       found = true;
00220       mComments.remove(i);
00221     }
00222   }
00223 
00224   return found;
00225 }
00226 
00227 void IncidenceBase::clearComments()
00228 {
00229   mComments.clear();
00230 }
00231 
00232 QStringList IncidenceBase::comments() const
00233 {
00234   return mComments;
00235 }
00236 
00237 
00238 void IncidenceBase::addAttendee(Attendee *a, bool doupdate)
00239 {
00240 //  kdDebug(5800) << "IncidenceBase::addAttendee()" << endl;
00241   if (mReadOnly) return;
00242 //  kdDebug(5800) << "IncidenceBase::addAttendee() weiter" << endl;
00243   if (a->name().left(7).upper() == "mailto:")
00244     a->setName(a->name().remove(0,7));
00245 
00246   mAttendees.append(a);
00247   if (doupdate) updated();
00248 }
00249 
00250 #if 0
00251 void IncidenceBase::removeAttendee(Attendee *a)
00252 {
00253   if (mReadOnly) return;
00254   mAttendees.removeRef(a);
00255   updated();
00256 }
00257 
00258 void IncidenceBase::removeAttendee(const char *n)
00259 {
00260   Attendee *a;
00261 
00262   if (mReadOnly) return;
00263   for (a = mAttendees.first(); a; a = mAttendees.next())
00264     if (a->getName() == n) {
00265       mAttendees.remove();
00266       break;
00267     }
00268 }
00269 #endif
00270 
00271 void IncidenceBase::clearAttendees()
00272 {
00273   if (mReadOnly) return;
00274   mAttendees.clear();
00275 }
00276 
00277 Attendee *IncidenceBase::attendeeByMail( const QString &email ) const
00278 {
00279   Attendee::List::ConstIterator it;
00280   for( it = mAttendees.begin(); it != mAttendees.end(); ++it ) {
00281     if ( (*it)->email() == email ) return *it;
00282   }
00283 
00284   return 0;
00285 }
00286 
00287 Attendee *IncidenceBase::attendeeByMails( const QStringList &emails,
00288                                           const QString &email) const
00289 {
00290   QStringList mails = emails;
00291   if ( !email.isEmpty() ) mails.append( email );
00292 
00293   Attendee::List::ConstIterator itA;
00294   for( itA = mAttendees.begin(); itA != mAttendees.end(); ++itA ) {
00295     for ( QStringList::Iterator it = mails.begin(); it != mails.end(); ++it ) {
00296       if ( (*itA)->email() == (*it) ) return *itA;
00297     }
00298   }
00299 
00300   return 0;
00301 }
00302 
00303 Attendee *IncidenceBase::attendeeByUid( const QString &uid ) const
00304 {
00305   Attendee::List::ConstIterator it;
00306   for( it = mAttendees.begin(); it != mAttendees.end(); ++it ) {
00307     if ( (*it)->uid() == uid ) return *it;
00308   }
00309 
00310   return 0;
00311 }
00312 
00313 
00314 void IncidenceBase::setDuration(int seconds)
00315 {
00316   mDuration = seconds;
00317   setHasDuration(true);
00318   updated();
00319 }
00320 
00321 int IncidenceBase::duration() const
00322 {
00323   return mDuration;
00324 }
00325 
00326 void IncidenceBase::setHasDuration(bool hasDuration)
00327 {
00328   mHasDuration = hasDuration;
00329 }
00330 
00331 bool IncidenceBase::hasDuration() const
00332 {
00333   return mHasDuration;
00334 }
00335 
00336 void IncidenceBase::setSyncStatus(int stat)
00337 {
00338   if (mReadOnly) return;
00339   if ( mSyncStatus == stat ) return;
00340   mSyncStatus = stat;
00341   updatedSilent();
00342 }
00343 void IncidenceBase::setSyncStatusSilent(int stat)
00344 {
00345   if (mReadOnly) return;
00346   mSyncStatus = stat;
00347 }
00348 
00349 int IncidenceBase::syncStatus() const
00350 {
00351   return mSyncStatus;
00352 }
00353 
00354 void IncidenceBase::setPilotId( unsigned long id )
00355 {
00356   if (mReadOnly) return;
00357   if ( mPilotId == id) return;
00358   mPilotId = id;
00359   updatedSilent();
00360 }
00361 
00362 unsigned long IncidenceBase::pilotId() const
00363 {
00364   return mPilotId;
00365 }
00366 
00367 void IncidenceBase::registerObserver( IncidenceBase::Observer *observer )
00368 {
00369   if( !mObservers.contains( observer ) ) mObservers.append( observer );
00370 }
00371 
00372 void IncidenceBase::unRegisterObserver( IncidenceBase::Observer *observer )
00373 {
00374   mObservers.remove( observer );
00375 }
00376 
00377 void IncidenceBase::updated()
00378 {
00379   QPtrListIterator<Observer> it(mObservers);
00380   while( it.current() ) {
00381     Observer *o = it.current();
00382     ++it;
00383     o->incidenceUpdated( this );
00384   }
00385 }
00386 
00387 void IncidenceBase::updatedSilent()
00388 {
00389   QPtrListIterator<Observer> it(mObservers);
00390   while( it.current() ) {
00391     Observer *o = it.current();
00392     ++it;
00393     o->incidenceUpdatedSilent( this );
00394   }
00395 }
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 Oct 4 14:39:38 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003