libkcal Library API Documentation

freebusy.cpp

00001 /* 00002 This file is part of libkcal. 00003 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00018 Boston, MA 02111-1307, USA. 00019 */ 00020 00021 #include <kdebug.h> 00022 00023 #include "freebusy.h" 00024 00025 using namespace KCal; 00026 00027 FreeBusy::FreeBusy() 00028 { 00029 } 00030 00031 FreeBusy::FreeBusy(const QDateTime &start, const QDateTime &end) 00032 { 00033 setDtStart(start); 00034 setDtEnd(end); 00035 } 00036 00037 FreeBusy::FreeBusy( Calendar *calendar, const QDateTime &start, const QDateTime &end ) 00038 { 00039 kdDebug(5800) << "FreeBusy::FreeBusy" << endl; 00040 mCalendar = calendar; 00041 00042 setDtStart(start); 00043 setDtEnd(end); 00044 00045 // Get all the events in the calendar 00046 Event::List eventList = mCalendar->rawEvents( start.date(), end.date() ); 00047 00048 int extraDays, i, x, duration; 00049 duration = start.daysTo(end); 00050 QDate day; 00051 QDateTime tmpStart; 00052 QDateTime tmpEnd; 00053 // Loops through every event in the calendar 00054 Event::List::ConstIterator it; 00055 for( it = eventList.begin(); it != eventList.end(); ++it ) { 00056 Event *event = *it; 00057 00058 // The code below can not handle floating events. Fixing this resulted 00059 // in a lot of duplicated code. Instead, make a copy of the event and 00060 // set the period to the full day(s). This trick works for recurring, 00061 // multiday, and single day floating events. 00062 Event *floatingEvent = 0; 00063 if ( event->doesFloat() ) { 00064 // Floating event. Do the hack 00065 kdDebug(5800) << "Floating event\n"; 00066 floatingEvent = new Event( *event ); 00067 00068 // Set the start and end times to be on midnight 00069 QDateTime start( floatingEvent->dtStart().date(), QTime( 0, 0 ) ); 00070 QDateTime end( floatingEvent->dtEnd().date(), QTime( 23, 59, 59, 999 ) ); 00071 floatingEvent->setFloats( false ); 00072 floatingEvent->setDtStart( start ); 00073 floatingEvent->setDtEnd( end ); 00074 00075 kdDebug(5800) << "Use: " << start.toString() << " to " << end.toString() 00076 << endl; 00077 // Finally, use this event for the setting below 00078 event = floatingEvent; 00079 } 00080 00081 // This whole for loop is for recurring events, it loops through 00082 // each of the days of the freebusy request 00083 00084 // First check if this is transparent. If it is, it shouldn't be in the 00085 // freebusy list 00086 if ( event->transparency() == Event::Transparent ) 00087 // Transparent 00088 continue; 00089 00090 for(i=0; i<=duration; i++) { 00091 day=(start.addDays(i).date()); 00092 tmpStart.setDate(day); 00093 tmpEnd.setDate(day); 00094 00095 if( event->doesRecur() ) { 00096 if ( event->isMultiDay() ) { 00097 extraDays = event->dtStart().date().daysTo(event->dtEnd().date()); 00098 for (x=0; x<=extraDays; x++) { 00099 if ( event->recursOn(day.addDays(-x))) { 00100 tmpStart.setDate(day.addDays(-x)); 00101 tmpStart.setTime(event->dtStart().time()); 00102 tmpEnd=tmpStart.addSecs( (event->duration()) ); 00103 00104 addLocalPeriod( tmpStart, tmpEnd ); 00105 break; 00106 } 00107 } 00108 } else { 00109 if (event->recursOn(day)) { 00110 tmpStart.setTime(event->dtStart().time()); 00111 tmpEnd.setTime(event->dtEnd().time()); 00112 00113 addLocalPeriod (tmpStart, tmpEnd); 00114 } 00115 } 00116 } 00117 00118 } 00119 // Non-recurring events 00120 addLocalPeriod(event->dtStart(), event->dtEnd()); 00121 00122 // Clean up 00123 delete floatingEvent; 00124 } 00125 00126 sortList(); 00127 } 00128 00129 FreeBusy::~FreeBusy() 00130 { 00131 } 00132 00133 bool FreeBusy::setDtEnd( const QDateTime &end ) 00134 { 00135 mDtEnd = end; 00136 return true; 00137 } 00138 00139 QDateTime FreeBusy::dtEnd() const 00140 { 00141 return mDtEnd; 00142 } 00143 00144 QValueList<Period> FreeBusy::busyPeriods() const 00145 { 00146 return mBusyPeriods; 00147 } 00148 00149 bool FreeBusy::addLocalPeriod(const QDateTime &eventStart, const QDateTime &eventEnd ) { 00150 QDateTime tmpStart; 00151 QDateTime tmpEnd; 00152 00153 //Check to see if the start *or* end of the event is 00154 //between the start and end of the freebusy dates. 00155 if (!((((this->dtStart()).secsTo(eventStart)>=0)&&(eventStart.secsTo(this->dtEnd())>=0)) 00156 ||(((this->dtStart()).secsTo(eventEnd) >= 0)&&(eventEnd.secsTo(this->dtEnd()) >= 0)))) 00157 return false; 00158 00159 if ( eventStart.secsTo(this->dtStart())>=0) { 00160 tmpStart = this->dtStart(); 00161 } else { 00162 tmpStart = eventStart; 00163 } 00164 00165 if ( eventEnd.secsTo(this->dtEnd())<=0 ) { 00166 tmpEnd = this->dtEnd(); 00167 } else { 00168 tmpEnd = eventEnd; 00169 } 00170 00171 Period p(tmpStart, tmpEnd); 00172 mBusyPeriods.append( p ); 00173 00174 return true; 00175 } 00176 00177 FreeBusy::FreeBusy(QValueList<Period> busyPeriods) 00178 { 00179 mBusyPeriods = busyPeriods; 00180 } 00181 00182 void FreeBusy::sortList() 00183 { 00184 typedef QValueList<Period> PeriodList; 00185 00186 PeriodList::Iterator tmpPeriod, earlyPeriod; 00187 PeriodList sortedList; 00188 QDateTime earlyTime; 00189 00190 while( mBusyPeriods.count() > 0 ) { 00191 earlyTime=(*mBusyPeriods.begin()).start(); 00192 for (tmpPeriod=mBusyPeriods.begin(); tmpPeriod!=mBusyPeriods.end(); tmpPeriod++) { 00193 if (earlyTime.secsTo((*tmpPeriod).start()) <= 0) { 00194 earlyTime=(*tmpPeriod).start(); 00195 earlyPeriod=tmpPeriod; 00196 } 00197 } 00198 //Move tmpPeriod to sortedList 00199 Period tmpPeriod( (*earlyPeriod).start(), (*earlyPeriod).end() ); 00200 sortedList.append( tmpPeriod ); 00201 mBusyPeriods.remove( earlyPeriod ); 00202 } 00203 mBusyPeriods=sortedList; 00204 } 00205 00206 void FreeBusy::addPeriod(const QDateTime &start, const QDateTime &end) 00207 { 00208 Period p(start, end); 00209 mBusyPeriods.append( p ); 00210 00211 sortList(); 00212 }
KDE Logo
This file is part of the documentation for libkcal Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Aug 27 12:49:08 2004 by doxygen 1.3.8 written by Dimitri van Heesch, © 1997-2003