libkcal

incidencebase.cpp
1 /*
2  This file is part of libkcal.
3 
4  Copyright (c) 2001,2004 Cornelius Schumacher <schumacher@kde.org>
5  Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
22 
23 #include <tdeglobal.h>
24 #include <tdelocale.h>
25 #include <kdebug.h>
26 
27 #include "calformat.h"
28 
29 #include "incidencebase.h"
30 
31 using namespace KCal;
32 
33 IncidenceBase::IncidenceBase()
34  : mReadOnly( false ), mFloats( true ), mDuration( 0 ), mHasDuration( false ),
35  mPilotId( 0 ), mSyncStatus( SYNCMOD )
36 {
37  setUid( CalFormat::createUniqueId() );
38 
39  mAttendees.setAutoDelete( true );
40 }
41 
42 IncidenceBase::IncidenceBase(const IncidenceBase &i) :
43  CustomProperties( i )
44 {
45  mReadOnly = i.mReadOnly;
46  mDtStart = i.mDtStart;
47  mDuration = i.mDuration;
48  mHasDuration = i.mHasDuration;
49  mOrganizer = i.mOrganizer;
50  mUid = i.mUid;
51  Attendee::List attendees = i.attendees();
52  Attendee::List::ConstIterator it;
53  for( it = attendees.begin(); it != attendees.end(); ++it ) {
54  mAttendees.append( new Attendee( *(*it) ) );
55  }
56  mFloats = i.mFloats;
57  mLastModified = i.mLastModified;
58  mPilotId = i.mPilotId;
59  mSyncStatus = i.mSyncStatus;
60  mComments = i.mComments;
61 
62  // The copied object is a new one, so it isn't observed by the observer
63  // of the original object.
64  mObservers.clear();
65 
66  mAttendees.setAutoDelete( true );
67 }
68 
69 IncidenceBase::~IncidenceBase()
70 {
71 }
72 
73 IncidenceBase& IncidenceBase::operator=( const IncidenceBase& i )
74 {
75  CustomProperties::operator=( i );
76  mReadOnly = i.mReadOnly;
77  mDtStart = i.mDtStart;
78  mDuration = i.mDuration;
79  mHasDuration = i.mHasDuration;
80  mOrganizer = i.mOrganizer;
81  mUid = i.mUid;
82  mAttendees.clear();
84  Attendee::List::ConstIterator it;
85  for( it = attendees.begin(); it != attendees.end(); ++it ) {
86  mAttendees.append( new Attendee( *(*it) ) );
87  }
88  mFloats = i.mFloats;
89  mLastModified = i.mLastModified;
90  mPilotId = i.mPilotId;
91  mSyncStatus = i.mSyncStatus;
92  mComments = i.mComments;
93 
94  return *this;
95 }
96 
97 bool IncidenceBase::operator==( const IncidenceBase& i2 ) const
98 {
99  if( attendees().count() != i2.attendees().count() ) {
100  return false; // no need to check further
101  }
102 
103  Attendee::List al1 = attendees();
104  Attendee::List al2 = i2.attendees();
105  Attendee::List::ConstIterator a1 = al1.begin();
106  Attendee::List::ConstIterator a2 = al2.begin();
107  for( ; a1 != al1.end() && a2 != al2.end(); ++a1, ++a2 ) {
108  if( **a1 == **a2 )
109  continue;
110  else {
111  return false;
112  }
113  }
114 
115  if ( !CustomProperties::operator==(i2) )
116  return false;
117 
118  return ( dtStart() == i2.dtStart() &&
119  organizer() == i2.organizer() &&
120  uid() == i2.uid() &&
121  // Don't compare lastModified, otherwise the operator is not
122  // of much use. We are not comparing for identity, after all.
123  doesFloat() == i2.doesFloat() &&
124  duration() == i2.duration() &&
125  hasDuration() == i2.hasDuration() &&
126  pilotId() == i2.pilotId() &&
127  syncStatus() == i2.syncStatus() );
128  // no need to compare mObserver
129 }
130 
131 
132 
133 
134 void IncidenceBase::setUid(const TQString &uid)
135 {
136  mUid = uid;
137  updated();
138 }
139 
140 TQString IncidenceBase::uid() const
141 {
142  return mUid;
143 }
144 
145 void IncidenceBase::setLastModified(const TQDateTime &lm)
146 {
147  // DON'T! updated() because we call this from
148  // Calendar::updateEvent().
149 
150  // Remove milliseconds part.
151  TQDateTime current = lm;
152  TQTime t = current.time();
153  t.setHMS( t.hour(), t.minute(), t.second(), 0 );
154  current.setTime( t );
155 
156  mLastModified = current;
157 }
158 
159 TQDateTime IncidenceBase::lastModified() const
160 {
161  return mLastModified;
162 }
163 
165 {
166  // we don't check for readonly here, because it is
167  // possible that by setting the organizer we are changing
168  // the event's readonly status...
169  mOrganizer = o;
170 
171  updated();
172 }
173 
174 void IncidenceBase::setOrganizer(const TQString &o)
175 {
176  TQString mail( o );
177  if ( mail.startsWith("MAILTO:", false) )
178  mail = mail.remove( 0, 7 );
179  // split the string into full name plus email.
180  Person organizer( mail );
181  setOrganizer( organizer );
182 }
183 
184 Person IncidenceBase::organizer() const
185 {
186  return mOrganizer;
187 }
188 
189 void IncidenceBase::setReadOnly( bool readOnly )
190 {
191  mReadOnly = readOnly;
192 }
193 
194 void IncidenceBase::setDtStart(const TQDateTime &dtStart)
195 {
196 // if (mReadOnly) return;
197  mDtStart = dtStart;
198  updated();
199 }
200 
201 TQDateTime IncidenceBase::dtStart() const
202 {
203  return mDtStart;
204 }
205 
207 {
208  return TDEGlobal::locale()->formatTime(dtStart().time());
209 }
210 
211 TQString IncidenceBase::dtStartDateStr(bool shortfmt) const
212 {
213  return TDEGlobal::locale()->formatDate(dtStart().date(),shortfmt);
214 }
215 
217 {
218  return TDEGlobal::locale()->formatDateTime(dtStart());
219 }
220 
221 
223 {
224  return mFloats;
225 }
226 
228 {
229  if (mReadOnly) return;
230  mFloats = f;
231  updated();
232 }
233 
234 
235 void IncidenceBase::addComment(const TQString& comment)
236 {
237  mComments += comment;
238 }
239 
240 bool IncidenceBase::removeComment( const TQString& comment)
241 {
242  bool found = false;
243  TQStringList::Iterator i;
244 
245  for ( i = mComments.begin(); !found && i != mComments.end(); ++i ) {
246  if ( (*i) == comment ) {
247  found = true;
248  mComments.remove(i);
249  }
250  }
251 
252  return found;
253 }
254 
256 {
257  mComments.clear();
258 }
259 
260 TQStringList IncidenceBase::comments() const
261 {
262  return mComments;
263 }
264 
265 
266 void IncidenceBase::addAttendee(Attendee *a, bool doupdate)
267 {
268 // kdDebug(5800) << "IncidenceBase::addAttendee()" << endl;
269  if (mReadOnly) return;
270 // kdDebug(5800) << "IncidenceBase::addAttendee() weiter" << endl;
271  if (a->name().left(7).upper() == "MAILTO:")
272  a->setName(a->name().remove(0,7));
273 
274  mAttendees.append(a);
275  if (doupdate) updated();
276 }
277 
278 #if 0
279 void IncidenceBase::removeAttendee(Attendee *a)
280 {
281  if (mReadOnly) return;
282  mAttendees.removeRef(a);
283  updated();
284 }
285 
286 void IncidenceBase::removeAttendee(const char *n)
287 {
288  Attendee *a;
289 
290  if (mReadOnly) return;
291  for (a = mAttendees.first(); a; a = mAttendees.next())
292  if (a->getName() == n) {
293  mAttendees.remove();
294  break;
295  }
296 }
297 #endif
298 
300 {
301  if (mReadOnly) return;
302  mAttendees.clear();
303 }
304 
305 Attendee *IncidenceBase::attendeeByMail( const TQString &email ) const
306 {
307  Attendee::List::ConstIterator it;
308  for( it = mAttendees.begin(); it != mAttendees.end(); ++it ) {
309  if ( (*it)->email() == email ) return *it;
310  }
311 
312  return 0;
313 }
314 
315 Attendee *IncidenceBase::attendeeByMails( const TQStringList &emails,
316  const TQString &email) const
317 {
318  TQStringList mails = emails;
319  if ( !email.isEmpty() ) mails.append( email );
320 
321  Attendee::List::ConstIterator itA;
322  for( itA = mAttendees.begin(); itA != mAttendees.end(); ++itA ) {
323  for ( TQStringList::Iterator it = mails.begin(); it != mails.end(); ++it ) {
324  if ( (*itA)->email() == (*it) ) return *itA;
325  }
326  }
327 
328  return 0;
329 }
330 
331 Attendee *IncidenceBase::attendeeByUid( const TQString &uid ) const
332 {
333  Attendee::List::ConstIterator it;
334  for( it = mAttendees.begin(); it != mAttendees.end(); ++it ) {
335  if ( (*it)->uid() == uid ) return *it;
336  }
337 
338  return 0;
339 }
340 
341 
342 void IncidenceBase::setDuration(int seconds)
343 {
344  mDuration = seconds;
345  setHasDuration(true);
346  updated();
347 }
348 
349 int IncidenceBase::duration() const
350 {
351  return mDuration;
352 }
353 
354 void IncidenceBase::setHasDuration(bool hasDuration)
355 {
356  mHasDuration = hasDuration;
357 }
358 
359 bool IncidenceBase::hasDuration() const
360 {
361  return mHasDuration;
362 }
363 
365 {
366  if (mReadOnly) return;
367  if ( mSyncStatus == stat ) return;
368  mSyncStatus = stat;
369  updatedSilent();
370 }
371 void IncidenceBase::setSyncStatusSilent(int stat)
372 {
373  if (mReadOnly) return;
374  mSyncStatus = stat;
375 }
376 
378 {
379  return mSyncStatus;
380 }
381 
382 void IncidenceBase::setPilotId( unsigned long id )
383 {
384  if (mReadOnly) return;
385  if ( mPilotId == id) return;
386  mPilotId = id;
387  updatedSilent();
388 }
389 
390 unsigned long IncidenceBase::pilotId() const
391 {
392  return mPilotId;
393 }
394 
395 void IncidenceBase::registerObserver( IncidenceBase::Observer *observer )
396 {
397  if( !mObservers.contains( observer ) ) mObservers.append( observer );
398 }
399 
400 void IncidenceBase::unRegisterObserver( IncidenceBase::Observer *observer )
401 {
402  mObservers.remove( observer );
403 }
404 
406 {
407  TQPtrListIterator<Observer> it(mObservers);
408  while( it.current() ) {
409  Observer *o = it.current();
410  ++it;
411  if ( o ) {
412  o->incidenceUpdated( this );
413  }
414  }
415 }
416 
418 {
419  updated();
420 }
421 
422 void IncidenceBase::updatedSilent()
423 {
424  TQPtrListIterator<Observer> it(mObservers);
425  while( it.current() ) {
426  Observer *o = it.current();
427  ++it;
428  o->incidenceUpdatedSilent( this );
429  }
430 }
431 
This class represents information related to an attendee of an event.
Definition: attendee.h:37
This class represents custom calendar properties.
This class provides the base class common to all calendar components.
Definition: incidencebase.h:46
Attendee * attendeeByUid(const TQString &uid) const
Return attendee with given uid.
void setOrganizer(const Person &o)
sets the organizer for the event
void updated()
Call this to notify the observers after the IncidenceBas object has changed.
void setFloats(bool f)
Set whether the incidence floats, i.e.
void setPilotId(unsigned long id)
Set Pilot Id.
unsigned long pilotId() const
Return Pilot Id.
virtual TDE_DEPRECATED TQString dtStartTimeStr() const
returns an event's starting time as a string formatted according to the users locale settings.
void registerObserver(Observer *)
Register observer.
TQStringList comments() const
Return all comments associated with this incidence.
bool doesFloat() const
Return true or false depending on whether the incidence "floats," i.e.
bool removeComment(const TQString &comment)
Remove a comment from the event.
const Attendee::List & attendees() const
Return list of attendees.
void clearComments()
Delete all comments associated with this incidence.
void clearAttendees()
Remove all Attendees.
TQString uid() const
Return the unique id for the event.
virtual void customPropertyUpdated()
void unRegisterObserver(Observer *)
Unregister observer.
Attendee * attendeeByMail(const TQString &) const
Return the Attendee with this email address.
virtual TDE_DEPRECATED TQString dtStartStr() const
returns an event's starting date and time as a string formatted according to the users locale setting...
Attendee * attendeeByMails(const TQStringList &, const TQString &email=TQString()) const
Return first Attendee with one of the given email addresses.
void addComment(const TQString &comment)
Add a comment to this incidence.
void setUid(const TQString &)
Set the unique id for the event.
virtual void setReadOnly(bool)
Set readonly status.
int syncStatus() const
Return synchronisation status.
virtual TQDateTime dtStart() const
returns an event's starting date/time as a TQDateTime.
virtual void setDtStart(const TQDateTime &dtStart)
for setting the event's starting date/time with a TQDateTime.
TQDateTime lastModified() const
Return the time the incidence was last modified.
void setSyncStatus(int status)
Set synchronisation satus.
void setLastModified(const TQDateTime &lm)
Sets the time the incidence was last modified.
void addAttendee(Attendee *attendee, bool doUpdate=true)
Add Attendee to this incidence.
virtual TDE_DEPRECATED TQString dtStartDateStr(bool shortfmt=true) const
returns an event's starting date as a string formatted according to the users locale settings
This class represents a person.
Definition: person.h:35
Namespace KCal is for global classes, objects and/or functions in libkcal.
Definition: alarm.h:38