libkcal

incidencebase.cpp

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