KCalCore Library
event.cpp
Go to the documentation of this file.
00001 /* 00002 This file is part of the kcalcore library. 00003 00004 Copyright (c) 2001 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., 51 Franklin Street, Fifth Floor, 00019 Boston, MA 02110-1301, USA. 00020 */ 00032 #include "event.h" 00033 #include "visitor.h" 00034 00035 #include <KDebug> 00036 00037 using namespace KCalCore; 00038 00043 //@cond PRIVATE 00044 class KCalCore::Event::Private 00045 { 00046 public: 00047 Private() 00048 : mHasEndDate( false ), 00049 mTransparency( Opaque ), 00050 mMultiDayValid( false ), 00051 mMultiDay( false ) 00052 {} 00053 Private( const KCalCore::Event::Private &other ) 00054 : mDtEnd( other.mDtEnd ), 00055 mHasEndDate( other.mHasEndDate ), 00056 mTransparency( other.mTransparency ), 00057 mMultiDayValid( false ), 00058 mMultiDay( false ) 00059 {} 00060 00061 KDateTime mDtEnd; 00062 bool mHasEndDate; 00063 Transparency mTransparency; 00064 bool mMultiDayValid; 00065 bool mMultiDay; 00066 }; 00067 //@endcond 00068 00069 Event::Event() 00070 : d( new KCalCore::Event::Private ) 00071 { 00072 } 00073 00074 Event::Event( const Event &other ) 00075 : Incidence( other ), d( new KCalCore::Event::Private( *other.d ) ) 00076 { 00077 } 00078 00079 Event::~Event() 00080 { 00081 delete d; 00082 } 00083 00084 Event *Event::clone() const 00085 { 00086 return new Event( *this ); 00087 } 00088 00089 IncidenceBase &Event::assign( const IncidenceBase &other ) 00090 { 00091 if ( &other != this ) { 00092 Incidence::assign( other ); 00093 const Event *e = static_cast<const Event*>( &other ); 00094 *d = *( e->d ); 00095 } 00096 return *this; 00097 } 00098 00099 bool Event::equals( const IncidenceBase &event ) const 00100 { 00101 if ( !Incidence::equals( event ) ) { 00102 return false; 00103 } else { 00104 // If they weren't the same type IncidenceBase::equals would had returned false already 00105 const Event *e = static_cast<const Event*>( &event ); 00106 return 00107 ( ( dtEnd() == e->dtEnd() ) || 00108 ( !dtEnd().isValid() && !e->dtEnd().isValid() ) ) && 00109 hasEndDate() == e->hasEndDate() && 00110 transparency() == e->transparency(); 00111 } 00112 } 00113 00114 Incidence::IncidenceType Event::type() const 00115 { 00116 return TypeEvent; 00117 } 00118 00119 QByteArray Event::typeStr() const 00120 { 00121 return "Event"; 00122 } 00123 00124 void Event::setDtStart( const KDateTime &dt ) 00125 { 00126 d->mMultiDayValid = false; 00127 Incidence::setDtStart( dt ); 00128 } 00129 00130 void Event::setDtEnd( const KDateTime &dtEnd ) 00131 { 00132 if ( mReadOnly ) { 00133 return; 00134 } 00135 00136 update(); 00137 00138 d->mDtEnd = dtEnd; 00139 d->mMultiDayValid = false; 00140 setHasEndDate( true ); 00141 setHasDuration( false ); 00142 setFieldDirty( FieldDtEnd ); 00143 updated(); 00144 } 00145 00146 KDateTime Event::dtEnd() const 00147 { 00148 if ( hasEndDate() ) { 00149 return d->mDtEnd; 00150 } 00151 00152 if ( hasDuration() ) { 00153 if ( allDay() ) { 00154 // For all day events, dtEnd is always inclusive 00155 KDateTime end = duration().end( dtStart() ).addDays( -1 ); 00156 return end >= dtStart() ? end : dtStart(); 00157 } else { 00158 return duration().end( dtStart() ); 00159 } 00160 } 00161 00162 // It is valid for a VEVENT to be without a DTEND. See RFC2445, Sect4.6.1. 00163 // Be careful to use Event::dateEnd() as appropriate due to this possibility. 00164 return dtStart(); 00165 } 00166 00167 QDate Event::dateEnd() const 00168 { 00169 KDateTime end = dtEnd().toTimeSpec( dtStart() ); 00170 if ( allDay() ) { 00171 return end.date(); 00172 } else { 00173 return end.addSecs(-1).date(); 00174 } 00175 } 00176 00177 void Event::setHasEndDate( bool b ) 00178 { 00179 d->mHasEndDate = b; 00180 setFieldDirty( FieldDtEnd ); 00181 } 00182 00183 bool Event::hasEndDate() const 00184 { 00185 return d->mHasEndDate; 00186 } 00187 00188 bool Event::isMultiDay( const KDateTime::Spec &spec ) const 00189 { 00190 // First off, if spec's not valid, we can check for cache 00191 if ( ( !spec.isValid() ) && d->mMultiDayValid ) { 00192 return d->mMultiDay; 00193 } 00194 00195 // Not in cache -> do it the hard way 00196 KDateTime start, end; 00197 00198 if ( !spec.isValid() ) { 00199 start = dtStart(); 00200 end = dtEnd(); 00201 } else { 00202 start = dtStart().toTimeSpec( spec ); 00203 end = dtEnd().toTimeSpec( spec ); 00204 } 00205 00206 // End date is non inclusive, so subtract 1 second... except if we 00207 // got the event from some braindead implementation which gave us 00208 // start == end one (those do happen) 00209 if ( start != end ) { 00210 end = end.addSecs( -1 ); 00211 } 00212 00213 const bool multi = ( start.date() != end.date() && start <= end ); 00214 00215 // Update the cache 00216 if ( spec.isValid() ) { 00217 d->mMultiDayValid = true; 00218 d->mMultiDay = multi; 00219 } 00220 return multi; 00221 } 00222 00223 void Event::shiftTimes( const KDateTime::Spec &oldSpec, 00224 const KDateTime::Spec &newSpec ) 00225 { 00226 Incidence::shiftTimes( oldSpec, newSpec ); 00227 if ( hasEndDate() ) { 00228 d->mDtEnd = d->mDtEnd.toTimeSpec( oldSpec ); 00229 d->mDtEnd.setTimeSpec( newSpec ); 00230 } 00231 } 00232 00233 void Event::setTransparency( Event::Transparency transparency ) 00234 { 00235 if ( mReadOnly ) { 00236 return; 00237 } 00238 update(); 00239 d->mTransparency = transparency; 00240 setFieldDirty( FieldTransparency ); 00241 updated(); 00242 } 00243 00244 Event::Transparency Event::transparency() const 00245 { 00246 return d->mTransparency; 00247 } 00248 00249 void Event::setDuration( const Duration &duration ) 00250 { 00251 setHasEndDate( false ); 00252 Incidence::setDuration( duration ); 00253 } 00254 00255 void Event::setAllDay( bool allday ) 00256 { 00257 if ( allday != allDay() && !mReadOnly ) { 00258 setFieldDirty( FieldDtEnd ); 00259 Incidence::setAllDay( allday ); 00260 } 00261 } 00262 00263 bool Event::accept( Visitor &v, IncidenceBase::Ptr incidence ) 00264 { 00265 return v.visit( incidence.staticCast<Event>() ); 00266 } 00267 00268 KDateTime Event::dateTime( DateTimeRole role ) const 00269 { 00270 switch ( role ) { 00271 case RoleRecurrenceStart: 00272 case RoleAlarmStartOffset: 00273 case RoleStartTimeZone: 00274 case RoleSort: 00275 return dtStart(); 00276 case RoleCalendarHashing: 00277 return !recurs() && !isMultiDay() ? dtStart() : 00278 KDateTime(); 00279 case RoleAlarmEndOffset: 00280 case RoleEndTimeZone: 00281 case RoleEndRecurrenceBase: 00282 case RoleEnd: 00283 case RoleDisplayEnd: 00284 return dtEnd(); 00285 case RoleDisplayStart: 00286 return dtStart(); 00287 case RoleAlarm: 00288 if ( alarms().isEmpty() ) { 00289 return KDateTime(); 00290 } else { 00291 Alarm::Ptr alarm = alarms().first(); 00292 return alarm->hasStartOffset() ? dtStart() : dtEnd(); 00293 } 00294 break; 00295 default: 00296 return KDateTime(); 00297 } 00298 } 00299 00300 void Event::setDateTime( const KDateTime &dateTime, DateTimeRole role ) 00301 { 00302 Q_UNUSED( dateTime ); 00303 Q_UNUSED( role ); 00304 } 00305 00306 void Event::virtual_hook( int id, void *data ) 00307 { 00308 Q_UNUSED( id ); 00309 Q_UNUSED( data ); 00310 Q_ASSERT( false ); 00311 } 00312 00313 QLatin1String KCalCore::Event::mimeType() const 00314 { 00315 return Event::eventMimeType(); 00316 } 00317 00318 QLatin1String Event::eventMimeType() 00319 { 00320 return QLatin1String( "application/x-vnd.akonadi.calendar.event" ); 00321 } 00322 00323 QLatin1String Event::iconName( const KDateTime & ) const 00324 { 00325 return QLatin1String( "view-calendar-day" ); 00326 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 7 2012 23:54:01 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 7 2012 23:54:01 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.