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(QDateTime start, 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() << "FreeBusy::FreeBusy" << endl;
00040   mCalendar = calendar;
00041 
00042   setDtStart(start);
00043   setDtEnd(end);
00044 
00045   //Gets all the events in the calendar
00046   QPtrList<Event> eventList = mCalendar->events();
00047   Event *event;
00048 
00049   int extraDays, i, x, duration;
00050   duration = start.daysTo(end);
00051   QDate day;
00052   QDateTime tmpStart;
00053   QDateTime tmpEnd;
00054   //Loops through every event in the calendar
00055   for( event = eventList.first(); event; event = eventList.next() ) {
00056     //This whole for loop is for recurring events, it loops through 
00057     //each of the days of the freebusy request
00058     for(i=0; i<=duration; i++) {
00059       day=(start.addDays(i).date());
00060       tmpStart.setDate(day);
00061       tmpEnd.setDate(day);
00062 
00063       if( (*(event->recurrence())).doesRecur() ) {
00064         if ( event->isMultiDay() ) {
00065           extraDays = event->dtStart().date().daysTo(event->dtEnd().date());
00066           for (x=0; x<=extraDays; x++) {
00067             if ( event->recursOn(day.addDays(-x))) {
00068               tmpStart.setDate(day.addDays(-x));
00069               tmpStart.setTime(event->dtStart().time());
00070               tmpEnd=tmpStart.addSecs( (event->duration()) );
00071 
00072               addLocalPeriod( tmpStart, tmpEnd );
00073               break;
00074             }
00075           }
00076         } else {
00077           if (event->recursOn(day)) {
00078             tmpStart.setTime(event->dtStart().time());
00079             tmpEnd.setTime(event->dtEnd().time());
00080 
00081             addLocalPeriod (tmpStart, tmpEnd);
00082           }
00083         }
00084       }
00085     
00086     }
00087     //Non-reocurring events
00088     addLocalPeriod(event->dtStart(), event->dtEnd());
00089   }
00090 
00091   sortList();
00092 }
00093 
00094 FreeBusy::~FreeBusy()
00095 {
00096 }
00097 
00098 bool FreeBusy::setDtEnd(QDateTime end) {
00099   mDtEnd = end;
00100   return true;
00101 }
00102 
00103 QDateTime FreeBusy::dtEnd() {
00104   return mDtEnd;
00105 }
00106 
00107 QValueList<Period> FreeBusy::busyPeriods() const
00108 {
00109   return mBusyPeriods;
00110 }
00111 
00112 bool FreeBusy::addLocalPeriod( QDateTime eventStart, QDateTime eventEnd ) {
00113   QDateTime tmpStart;
00114   QDateTime tmpEnd;
00115 
00116   //Check to see if the start *or* end of the event is
00117   //between the start and end of the freebusy dates.
00118   if (!((((this->dtStart()).secsTo(eventStart)>=0)&&(eventStart.secsTo(this->dtEnd())>=0)) 
00119     ||(((this->dtStart()).secsTo(eventEnd) >= 0)&&(eventEnd.secsTo(this->dtEnd()) >= 0))))
00120     return false;
00121 
00122   if ( eventStart.secsTo(this->dtStart())>=0) {
00123     tmpStart = this->dtStart();
00124   } else {
00125     tmpStart = eventStart;
00126   }
00127 
00128   if ( eventEnd.secsTo(this->dtEnd())<=0 ) {
00129     tmpEnd = this->dtEnd();
00130   } else {
00131     tmpEnd = eventEnd;
00132   }  
00133 
00134   Period p(tmpStart, tmpEnd);  
00135   mBusyPeriods.append( p );
00136 
00137   return true;
00138 }
00139 
00140 FreeBusy::FreeBusy(QValueList<Period> busyPeriods)
00141 {
00142   mBusyPeriods = busyPeriods;
00143 }
00144 
00145 void FreeBusy::sortList()
00146 {
00147   typedef QValueList<Period> PeriodList;
00148 
00149   PeriodList::Iterator tmpPeriod, earlyPeriod;
00150   PeriodList sortedList;
00151   QDateTime earlyTime;
00152 
00153   while( mBusyPeriods.count() > 0 ) {
00154     earlyTime=(*mBusyPeriods.begin()).start();
00155     for (tmpPeriod=mBusyPeriods.begin(); tmpPeriod!=mBusyPeriods.end(); tmpPeriod++) {
00156       if (earlyTime.secsTo((*tmpPeriod).start()) <= 0) {
00157         earlyTime=(*tmpPeriod).start();
00158         earlyPeriod=tmpPeriod;
00159       }
00160     } 
00161     //Move tmpPeriod to sortedList
00162     Period tmpPeriod( (*earlyPeriod).start(), (*earlyPeriod).end() );
00163     sortedList.append( tmpPeriod );
00164     mBusyPeriods.remove( earlyPeriod );
00165   }
00166   mBusyPeriods=sortedList;
00167 }
00168 
00169 void FreeBusy::addPeriod(QDateTime start, QDateTime end)
00170 {
00171   Period p(start, end);
00172   mBusyPeriods.append( p );
00173 
00174   sortList();
00175 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 15 11:40:27 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001