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