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 PeriodList 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( PeriodList busyPeriods)
00178 {
00179   mBusyPeriods = busyPeriods;
00180 }
00181 
00182 void FreeBusy::sortList()
00183 {
00184   qHeapSort( mBusyPeriods );
00185   return;
00186 }
00187 
00188 void FreeBusy::addPeriods(const PeriodList &list )
00189 {
00190   mBusyPeriods += list;
00191   sortList();
00192 }
00193 
00194 void FreeBusy::addPeriod(const QDateTime &start, const QDateTime &end)
00195 {
00196   mBusyPeriods.append( Period(start, end) );
00197   sortList();
00198 }
KDE Logo
This file is part of the documentation for libkcal Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Jul 25 11:17:25 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003