libkcal

incidencebase.cpp

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   // The copied object is a new one, so it isn't observed by the observer
00063   // of the original object.
00064   mObservers.clear();
00065 
00066   mAttendees.setAutoDelete( true );
00067 }
00068 
00069 IncidenceBase::~IncidenceBase()
00070 {
00071 }
00072 
00073 IncidenceBase& IncidenceBase::operator=( const IncidenceBase& i )
00074 {
00075   CustomProperties::operator=( i );
00076   mReadOnly = i.mReadOnly;
00077   mDtStart = i.mDtStart;
00078   mDuration = i.mDuration;
00079   mHasDuration = i.mHasDuration;
00080   mOrganizer = i.mOrganizer;
00081   mUid = i.mUid;
00082   mAttendees.clear();
00083   Attendee::List attendees = i.attendees();
00084   Attendee::List::ConstIterator it;
00085   for( it = attendees.begin(); it != attendees.end(); ++it ) {
00086     mAttendees.append( new Attendee( *(*it) ) );
00087   }
00088   mFloats = i.mFloats;
00089   mLastModified = i.mLastModified;
00090   mPilotId = i.mPilotId;
00091   mSyncStatus = i.mSyncStatus;
00092   mComments = i.mComments;
00093 
00094   // The copied object is a new one, so it isn't observed by the observer
00095   // of the original object.
00096   mObservers.clear();
00097   return *this;
00098 }
00099 
00100 bool IncidenceBase::operator==( const IncidenceBase& i2 ) const
00101 {
00102   if( attendees().count() != i2.attendees().count() ) {
00103       return false; // no need to check further
00104   }
00105 
00106   Attendee::List al1 = attendees();
00107   Attendee::List al2 = i2.attendees();
00108   Attendee::List::ConstIterator a1 = al1.begin();
00109   Attendee::List::ConstIterator a2 = al2.begin();
00110   for( ; a1 != al1.end() && a2 != al2.end(); ++a1, ++a2 ) {
00111     if( **a1 == **a2 )
00112         continue;
00113     else {
00114         return false;
00115     }
00116   }
00117 
00118   if ( !CustomProperties::operator==(i2) )
00119     return false;
00120 
00121   return ( dtStart() == i2.dtStart() &&
00122            organizer() == i2.organizer() &&
00123            uid() == i2.uid() &&
00124            // Don't compare lastModified, otherwise the operator is not
00125            // of much use. We are not comparing for identity, after all.
00126            doesFloat() == i2.doesFloat() &&
00127            duration() == i2.duration() &&
00128            hasDuration() == i2.hasDuration() &&
00129            pilotId() == i2.pilotId() &&
00130            syncStatus() == i2.syncStatus() );
00131   // no need to compare mObserver
00132 }
00133 
00134 
00135 
00136 
00137 void IncidenceBase::setUid(const QString &uid)
00138 {
00139   mUid = uid;
00140   updated();
00141 }
00142 
00143 QString IncidenceBase::uid() const
00144 {
00145   return mUid;
00146 }
00147 
00148 void IncidenceBase::setLastModified(const QDateTime &lm)
00149 {
00150   // DON'T! updated() because we call this from
00151   // Calendar::updateEvent().
00152 
00153   // Remove milliseconds part.
00154   QDateTime current = lm;
00155   QTime t = current.time();
00156   t.setHMS( t.hour(), t.minute(), t.second(), 0 );
00157   current.setTime( t );
00158 
00159   mLastModified = current;
00160 }
00161 
00162 QDateTime IncidenceBase::lastModified() const
00163 {
00164   return mLastModified;
00165 }
00166 
00167 void IncidenceBase::setOrganizer( const Person &o )
00168 {
00169   // we don't check for readonly here, because it is
00170   // possible that by setting the organizer we are changing
00171   // the event's readonly status...
00172   mOrganizer = o;
00173 
00174   updated();
00175 }
00176 
00177 void IncidenceBase::setOrganizer(const QString &o)
00178 {
00179   QString mail( o );
00180   if ( mail.startsWith("MAILTO:", false) )
00181     mail = mail.remove( 0, 7 );
00182   // split the string into full name plus email.
00183   Person organizer( mail );
00184   setOrganizer( organizer );
00185 }
00186 
00187 Person IncidenceBase::organizer() const
00188 {
00189   return mOrganizer;
00190 }
00191 
00192 void IncidenceBase::setReadOnly( bool readOnly )
00193 {
00194   mReadOnly = readOnly;
00195 }
00196 
00197 void IncidenceBase::setDtStart(const QDateTime &dtStart)
00198 {
00199 //  if (mReadOnly) return;
00200   mDtStart = dtStart;
00201   updated();
00202 }
00203 
00204 QDateTime IncidenceBase::dtStart() const
00205 {
00206   return mDtStart;
00207 }
00208 
00209 QString IncidenceBase::dtStartTimeStr() const
00210 {
00211   return KGlobal::locale()->formatTime(dtStart().time());
00212 }
00213 
00214 QString IncidenceBase::dtStartDateStr(bool shortfmt) const
00215 {
00216   return KGlobal::locale()->formatDate(dtStart().date(),shortfmt);
00217 }
00218 
00219 QString IncidenceBase::dtStartStr() const
00220 {
00221   return KGlobal::locale()->formatDateTime(dtStart());
00222 }
00223 
00224 
00225 bool IncidenceBase::doesFloat() const
00226 {
00227   return mFloats;
00228 }
00229 
00230 void IncidenceBase::setFloats(bool f)
00231 {
00232   if (mReadOnly) return;
00233   mFloats = f;
00234   updated();
00235 }
00236 
00237 
00238 void IncidenceBase::addComment(const QString& comment)
00239 {
00240   mComments += comment;
00241 }
00242 
00243 bool IncidenceBase::removeComment( const QString& comment)
00244 {
00245   bool found = false;
00246   QStringList::Iterator i;
00247 
00248   for ( i = mComments.begin(); !found && i != mComments.end(); ++i ) {
00249     if ( (*i) == comment ) {
00250       found = true;
00251       mComments.remove(i);
00252     }
00253   }
00254 
00255   return found;
00256 }
00257 
00258 void IncidenceBase::clearComments()
00259 {
00260   mComments.clear();
00261 }
00262 
00263 QStringList IncidenceBase::comments() const
00264 {
00265   return mComments;
00266 }
00267 
00268 
00269 void IncidenceBase::addAttendee(Attendee *a, bool doupdate)
00270 {
00271 //  kdDebug(5800) << "IncidenceBase::addAttendee()" << endl;
00272   if (mReadOnly) return;
00273 //  kdDebug(5800) << "IncidenceBase::addAttendee() weiter" << endl;
00274   if (a->name().left(7).upper() == "MAILTO:")
00275     a->setName(a->name().remove(0,7));
00276 
00277   mAttendees.append(a);
00278   if (doupdate) updated();
00279 }
00280 
00281 #if 0
00282 void IncidenceBase::removeAttendee(Attendee *a)
00283 {
00284   if (mReadOnly) return;
00285   mAttendees.removeRef(a);
00286   updated();
00287 }
00288 
00289 void IncidenceBase::removeAttendee(const char *n)
00290 {
00291   Attendee *a;
00292 
00293   if (mReadOnly) return;
00294   for (a = mAttendees.first(); a; a = mAttendees.next())
00295     if (a->getName() == n) {
00296       mAttendees.remove();
00297       break;
00298     }
00299 }
00300 #endif
00301 
00302 void IncidenceBase::clearAttendees()
00303 {
00304   if (mReadOnly) return;
00305   mAttendees.clear();
00306 }
00307 
00308 Attendee *IncidenceBase::attendeeByMail( const QString &email ) const
00309 {
00310   Attendee::List::ConstIterator it;
00311   for( it = mAttendees.begin(); it != mAttendees.end(); ++it ) {
00312     if ( (*it)->email() == email ) return *it;
00313   }
00314 
00315   return 0;
00316 }
00317 
00318 Attendee *IncidenceBase::attendeeByMails( const QStringList &emails,
00319                                           const QString &email) const
00320 {
00321   QStringList mails = emails;
00322   if ( !email.isEmpty() ) mails.append( email );
00323 
00324   Attendee::List::ConstIterator itA;
00325   for( itA = mAttendees.begin(); itA != mAttendees.end(); ++itA ) {
00326     for ( QStringList::Iterator it = mails.begin(); it != mails.end(); ++it ) {
00327       if ( (*itA)->email() == (*it) ) return *itA;
00328     }
00329   }
00330 
00331   return 0;
00332 }
00333 
00334 Attendee *IncidenceBase::attendeeByUid( const QString &uid ) const
00335 {
00336   Attendee::List::ConstIterator it;
00337   for( it = mAttendees.begin(); it != mAttendees.end(); ++it ) {
00338     if ( (*it)->uid() == uid ) return *it;
00339   }
00340 
00341   return 0;
00342 }
00343 
00344 
00345 void IncidenceBase::setDuration(int seconds)
00346 {
00347   mDuration = seconds;
00348   setHasDuration(true);
00349   updated();
00350 }
00351 
00352 int IncidenceBase::duration() const
00353 {
00354   return mDuration;
00355 }
00356 
00357 void IncidenceBase::setHasDuration(bool hasDuration)
00358 {
00359   mHasDuration = hasDuration;
00360 }
00361 
00362 bool IncidenceBase::hasDuration() const
00363 {
00364   return mHasDuration;
00365 }
00366 
00367 void IncidenceBase::setSyncStatus(int stat)
00368 {
00369   if (mReadOnly) return;
00370   if ( mSyncStatus == stat ) return;
00371   mSyncStatus = stat;
00372   updatedSilent();
00373 }
00374 void IncidenceBase::setSyncStatusSilent(int stat)
00375 {
00376   if (mReadOnly) return;
00377   mSyncStatus = stat;
00378 }
00379 
00380 int IncidenceBase::syncStatus() const
00381 {
00382   return mSyncStatus;
00383 }
00384 
00385 void IncidenceBase::setPilotId( unsigned long id )
00386 {
00387   if (mReadOnly) return;
00388   if ( mPilotId == id) return;
00389   mPilotId = id;
00390   updatedSilent();
00391 }
00392 
00393 unsigned long IncidenceBase::pilotId() const
00394 {
00395   return mPilotId;
00396 }
00397 
00398 void IncidenceBase::registerObserver( IncidenceBase::Observer *observer )
00399 {
00400   if( !mObservers.contains( observer ) ) mObservers.append( observer );
00401 }
00402 
00403 void IncidenceBase::unRegisterObserver( IncidenceBase::Observer *observer )
00404 {
00405   mObservers.remove( observer );
00406 }
00407 
00408 void IncidenceBase::updated()
00409 {
00410   QPtrListIterator<Observer> it(mObservers);
00411   while( it.current() ) {
00412     Observer *o = it.current();
00413     ++it;
00414     o->incidenceUpdated( this );
00415   }
00416 }
00417 
00418 void IncidenceBase::customPropertyUpdated()
00419 {
00420   updated();
00421 }
00422 
00423 void IncidenceBase::updatedSilent()
00424 {
00425   QPtrListIterator<Observer> it(mObservers);
00426   while( it.current() ) {
00427     Observer *o = it.current();
00428     ++it;
00429     o->incidenceUpdatedSilent( this );
00430   }
00431 }
00432 
KDE Home | KDE Accessibility Home | Description of Access Keys