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() << "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->events();
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     // This whole for loop is for recurring events, it loops through 
00059     // each of the days of the freebusy request
00060 
00061     // First check if this is transparent. If it is, it shouldn't be in the
00062     // freebusy list
00063     if ( event->transparency() == Event::Transparent )
00064       // Transparent
00065       continue;
00066 
00067     for(i=0; i<=duration; i++) {
00068       day=(start.addDays(i).date());
00069       tmpStart.setDate(day);
00070       tmpEnd.setDate(day);
00071 
00072       if( event->doesRecur() ) {
00073         if ( event->isMultiDay() ) {
00074           extraDays = event->dtStart().date().daysTo(event->dtEnd().date());
00075           for (x=0; x<=extraDays; x++) {
00076             if ( event->recursOn(day.addDays(-x))) {
00077               tmpStart.setDate(day.addDays(-x));
00078               tmpStart.setTime(event->dtStart().time());
00079               tmpEnd=tmpStart.addSecs( (event->duration()) );
00080 
00081               addLocalPeriod( tmpStart, tmpEnd );
00082               break;
00083             }
00084           }
00085         } else {
00086           if (event->recursOn(day)) {
00087             tmpStart.setTime(event->dtStart().time());
00088             tmpEnd.setTime(event->dtEnd().time());
00089 
00090             addLocalPeriod (tmpStart, tmpEnd);
00091           }
00092         }
00093       }
00094     
00095     }
00096     // Non-recurring events
00097     addLocalPeriod(event->dtStart(), event->dtEnd());
00098   }
00099 
00100   sortList();
00101 }
00102 
00103 FreeBusy::~FreeBusy()
00104 {
00105 }
00106 
00107 bool FreeBusy::setDtEnd( const QDateTime &end )
00108 {
00109   mDtEnd = end;
00110   return true;
00111 }
00112 
00113 QDateTime FreeBusy::dtEnd() const
00114 {
00115   return mDtEnd;
00116 }
00117 
00118 QValueList<Period> FreeBusy::busyPeriods() const
00119 {
00120   return mBusyPeriods;
00121 }
00122 
00123 bool FreeBusy::addLocalPeriod(const QDateTime &eventStart, const QDateTime &eventEnd ) {
00124   QDateTime tmpStart;
00125   QDateTime tmpEnd;
00126 
00127   //Check to see if the start *or* end of the event is
00128   //between the start and end of the freebusy dates.
00129   if (!((((this->dtStart()).secsTo(eventStart)>=0)&&(eventStart.secsTo(this->dtEnd())>=0)) 
00130     ||(((this->dtStart()).secsTo(eventEnd) >= 0)&&(eventEnd.secsTo(this->dtEnd()) >= 0))))
00131     return false;
00132 
00133   if ( eventStart.secsTo(this->dtStart())>=0) {
00134     tmpStart = this->dtStart();
00135   } else {
00136     tmpStart = eventStart;
00137   }
00138 
00139   if ( eventEnd.secsTo(this->dtEnd())<=0 ) {
00140     tmpEnd = this->dtEnd();
00141   } else {
00142     tmpEnd = eventEnd;
00143   }  
00144 
00145   Period p(tmpStart, tmpEnd);  
00146   mBusyPeriods.append( p );
00147 
00148   return true;
00149 }
00150 
00151 FreeBusy::FreeBusy(QValueList<Period> busyPeriods)
00152 {
00153   mBusyPeriods = busyPeriods;
00154 }
00155 
00156 void FreeBusy::sortList()
00157 {
00158   typedef QValueList<Period> PeriodList;
00159 
00160   PeriodList::Iterator tmpPeriod, earlyPeriod;
00161   PeriodList sortedList;
00162   QDateTime earlyTime;
00163 
00164   while( mBusyPeriods.count() > 0 ) {
00165     earlyTime=(*mBusyPeriods.begin()).start();
00166     for (tmpPeriod=mBusyPeriods.begin(); tmpPeriod!=mBusyPeriods.end(); tmpPeriod++) {
00167       if (earlyTime.secsTo((*tmpPeriod).start()) <= 0) {
00168         earlyTime=(*tmpPeriod).start();
00169         earlyPeriod=tmpPeriod;
00170       }
00171     } 
00172     //Move tmpPeriod to sortedList
00173     Period tmpPeriod( (*earlyPeriod).start(), (*earlyPeriod).end() );
00174     sortedList.append( tmpPeriod );
00175     mBusyPeriods.remove( earlyPeriod );
00176   }
00177   mBusyPeriods=sortedList;
00178 }
00179 
00180 void FreeBusy::addPeriod(const QDateTime &start, const QDateTime &end)
00181 {
00182   Period p(start, end);
00183   mBusyPeriods.append( p );
00184 
00185   sortList();
00186 }
KDE Logo
This file is part of the documentation for libkcal Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:36:21 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003