-rw-r--r-- | libkcal/freebusy.cpp | 184 |
1 files changed, 184 insertions, 0 deletions
diff --git a/libkcal/freebusy.cpp b/libkcal/freebusy.cpp new file mode 100644 index 0000000..ba15d6d --- a/dev/null +++ b/libkcal/freebusy.cpp | |||
@@ -0,0 +1,184 @@ | |||
1 | /* | ||
2 | This file is part of libkcal. | ||
3 | Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> | ||
4 | |||
5 | This library is free software; you can redistribute it and/or | ||
6 | modify it under the terms of the GNU Library General Public | ||
7 | License as published by the Free Software Foundation; either | ||
8 | version 2 of the License, or (at your option) any later version. | ||
9 | |||
10 | This library is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Library General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Library General Public License | ||
16 | along with this library; see the file COPYING.LIB. If not, write to | ||
17 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | #include <kdebug.h> | ||
22 | |||
23 | #include "freebusy.h" | ||
24 | |||
25 | using namespace KCal; | ||
26 | |||
27 | FreeBusy::FreeBusy() | ||
28 | { | ||
29 | } | ||
30 | |||
31 | FreeBusy::FreeBusy(const QDateTime &start, const QDateTime &end) | ||
32 | { | ||
33 | setDtStart(start); | ||
34 | setDtEnd(end); | ||
35 | } | ||
36 | |||
37 | FreeBusy::FreeBusy( Calendar *calendar, const QDateTime &start, const QDateTime &end ) | ||
38 | { | ||
39 | kdDebug() << "FreeBusy::FreeBusy" << endl; | ||
40 | mCalendar = calendar; | ||
41 | |||
42 | setDtStart(start); | ||
43 | setDtEnd(end); | ||
44 | |||
45 | //Gets all the events in the calendar | ||
46 | QPtrList<Event> eventList = mCalendar->events(); | ||
47 | Event *event; | ||
48 | |||
49 | int extraDays, i, x, duration; | ||
50 | duration = start.daysTo(end); | ||
51 | QDate day; | ||
52 | QDateTime tmpStart; | ||
53 | QDateTime tmpEnd; | ||
54 | //Loops through every event in the calendar | ||
55 | for( event = eventList.first(); event; event = eventList.next() ) { | ||
56 | //This whole for loop is for recurring events, it loops through | ||
57 | //each of the days of the freebusy request | ||
58 | |||
59 | //First check if this is transparent. If it is, it shouldn't be in the | ||
60 | //freebusy list | ||
61 | if ( event->transparency() == Event::Transparent ) | ||
62 | // Transparent | ||
63 | continue; | ||
64 | |||
65 | for(i=0; i<=duration; i++) { | ||
66 | day=(start.addDays(i).date()); | ||
67 | tmpStart.setDate(day); | ||
68 | tmpEnd.setDate(day); | ||
69 | |||
70 | if( (*(event->recurrence())).doesRecur() ) { | ||
71 | if ( event->isMultiDay() ) { | ||
72 | extraDays = event->dtStart().date().daysTo(event->dtEnd().date()); | ||
73 | for (x=0; x<=extraDays; x++) { | ||
74 | if ( event->recursOn(day.addDays(-x))) { | ||
75 | tmpStart.setDate(day.addDays(-x)); | ||
76 | tmpStart.setTime(event->dtStart().time()); | ||
77 | tmpEnd=tmpStart.addSecs( (event->duration()) ); | ||
78 | |||
79 | addLocalPeriod( tmpStart, tmpEnd ); | ||
80 | break; | ||
81 | } | ||
82 | } | ||
83 | } else { | ||
84 | if (event->recursOn(day)) { | ||
85 | tmpStart.setTime(event->dtStart().time()); | ||
86 | tmpEnd.setTime(event->dtEnd().time()); | ||
87 | |||
88 | addLocalPeriod (tmpStart, tmpEnd); | ||
89 | } | ||
90 | } | ||
91 | } | ||
92 | |||
93 | } | ||
94 | //Non-reocurring events | ||
95 | addLocalPeriod(event->dtStart(), event->dtEnd()); | ||
96 | } | ||
97 | |||
98 | sortList(); | ||
99 | } | ||
100 | |||
101 | FreeBusy::~FreeBusy() | ||
102 | { | ||
103 | } | ||
104 | |||
105 | bool FreeBusy::setDtEnd( const QDateTime &end ) | ||
106 | { | ||
107 | mDtEnd = end; | ||
108 | return true; | ||
109 | } | ||
110 | |||
111 | QDateTime FreeBusy::dtEnd() const | ||
112 | { | ||
113 | return mDtEnd; | ||
114 | } | ||
115 | |||
116 | QValueList<Period> FreeBusy::busyPeriods() const | ||
117 | { | ||
118 | return mBusyPeriods; | ||
119 | } | ||
120 | |||
121 | bool FreeBusy::addLocalPeriod(const QDateTime &eventStart, const QDateTime &eventEnd ) { | ||
122 | QDateTime tmpStart; | ||
123 | QDateTime tmpEnd; | ||
124 | |||
125 | //Check to see if the start *or* end of the event is | ||
126 | //between the start and end of the freebusy dates. | ||
127 | if (!((((this->dtStart()).secsTo(eventStart)>=0)&&(eventStart.secsTo(this->dtEnd())>=0)) | ||
128 | ||(((this->dtStart()).secsTo(eventEnd) >= 0)&&(eventEnd.secsTo(this->dtEnd()) >= 0)))) | ||
129 | return false; | ||
130 | |||
131 | if ( eventStart.secsTo(this->dtStart())>=0) { | ||
132 | tmpStart = this->dtStart(); | ||
133 | } else { | ||
134 | tmpStart = eventStart; | ||
135 | } | ||
136 | |||
137 | if ( eventEnd.secsTo(this->dtEnd())<=0 ) { | ||
138 | tmpEnd = this->dtEnd(); | ||
139 | } else { | ||
140 | tmpEnd = eventEnd; | ||
141 | } | ||
142 | |||
143 | Period p(tmpStart, tmpEnd); | ||
144 | mBusyPeriods.append( p ); | ||
145 | |||
146 | return true; | ||
147 | } | ||
148 | |||
149 | FreeBusy::FreeBusy(QValueList<Period> busyPeriods) | ||
150 | { | ||
151 | mBusyPeriods = busyPeriods; | ||
152 | } | ||
153 | |||
154 | void FreeBusy::sortList() | ||
155 | { | ||
156 | typedef QValueList<Period> PeriodList; | ||
157 | |||
158 | PeriodList::Iterator tmpPeriod, earlyPeriod; | ||
159 | PeriodList sortedList; | ||
160 | QDateTime earlyTime; | ||
161 | |||
162 | while( mBusyPeriods.count() > 0 ) { | ||
163 | earlyTime=(*mBusyPeriods.begin()).start(); | ||
164 | for (tmpPeriod=mBusyPeriods.begin(); tmpPeriod!=mBusyPeriods.end(); tmpPeriod++) { | ||
165 | if (earlyTime.secsTo((*tmpPeriod).start()) <= 0) { | ||
166 | earlyTime=(*tmpPeriod).start(); | ||
167 | earlyPeriod=tmpPeriod; | ||
168 | } | ||
169 | } | ||
170 | //Move tmpPeriod to sortedList | ||
171 | Period tmpPeriod( (*earlyPeriod).start(), (*earlyPeriod).end() ); | ||
172 | sortedList.append( tmpPeriod ); | ||
173 | mBusyPeriods.remove( earlyPeriod ); | ||
174 | } | ||
175 | mBusyPeriods=sortedList; | ||
176 | } | ||
177 | |||
178 | void FreeBusy::addPeriod(const QDateTime &start, const QDateTime &end) | ||
179 | { | ||
180 | Period p(start, end); | ||
181 | mBusyPeriods.append( p ); | ||
182 | |||
183 | sortList(); | ||
184 | } | ||