summaryrefslogtreecommitdiffstats
path: root/libkcal/freebusy.cpp
blob: 75d0e7db456ec0490800d0fe2cd3241d842279bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
    This file is part of libkcal.

    Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
    Copyright (C) 2004 Reinhold Kainhofer <reinhold@kainhofer.com>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include <kdebug.h>

#include "freebusy.h"

using namespace KCal;

FreeBusy::FreeBusy()
{
}

FreeBusy::FreeBusy(const TQDateTime &start, const TQDateTime &end)
{
  setDtStart(start);
  setDtEnd(end);
}

FreeBusy::FreeBusy( Calendar *calendar, const TQDateTime &start, const TQDateTime &end )
{
  kdDebug(5800) << "FreeBusy::FreeBusy" << endl;
  mCalendar = calendar;

  setDtStart(start);
  setDtEnd(end);

  // Get all the events in the calendar
  Event::List eventList = mCalendar->rawEvents( start.date(), end.date() );

  int extraDays, i, x, duration;
  duration = start.daysTo(end);
  TQDate day;
  TQDateTime tmpStart;
  TQDateTime tmpEnd;
  // Loops through every event in the calendar
  Event::List::ConstIterator it;
  for( it = eventList.begin(); it != eventList.end(); ++it ) {
    Event *event = *it;

    // The code below can not handle floating events. Fixing this resulted
    // in a lot of duplicated code. Instead, make a copy of the event and
    // set the period to the full day(s). This trick works for recurring,
    // multiday, and single day floating events.
    Event *floatingEvent = 0;
    if ( event->doesFloat() ) {
      // Floating event. Do the hack
      kdDebug(5800) << "Floating event\n";
      floatingEvent = new Event( *event );

      // Set the start and end times to be on midnight
      TQDateTime start( floatingEvent->dtStart().date(), TQTime( 0, 0 ) );
      TQDateTime end( floatingEvent->dtEnd().date(), TQTime( 23, 59, 59, 999 ) );
      floatingEvent->setFloats( false );
      floatingEvent->setDtStart( start );
      floatingEvent->setDtEnd( end );

      kdDebug(5800) << "Use: " << start.toString() << " to " << end.toString()
                    << endl;
      // Finally, use this event for the setting below
      event = floatingEvent;
    }

    // This whole for loop is for recurring events, it loops through
    // each of the days of the freebusy request

    // First check if this is transparent. If it is, it shouldn't be in the
    // freebusy list
    if ( event->transparency() == Event::Transparent )
      // Transparent
      continue;

    for( i = 0; i <= duration; ++i ) {
      day=(start.addDays(i).date());
      tmpStart.setDate(day);
      tmpEnd.setDate(day);

      if( event->doesRecur() ) {
        if ( event->isMultiDay() ) {
// FIXME: This doesn't work for sub-daily recurrences or recurrences with
//        a different time than the original event.
          extraDays = event->dtStart().date().daysTo(event->dtEnd().date());
          for ( x = 0; x <= extraDays; ++x ) {
            if ( event->recursOn(day.addDays(-x))) {
              tmpStart.setDate(day.addDays(-x));
              tmpStart.setTime(event->dtStart().time());
              tmpEnd=tmpStart.addSecs( (event->duration()) );

              addLocalPeriod( tmpStart, tmpEnd );
              break;
            }
          }
        } else {
          if (event->recursOn(day)) {
            tmpStart.setTime(event->dtStart().time());
            tmpEnd.setTime(event->dtEnd().time());

            addLocalPeriod (tmpStart, tmpEnd);
          }
        }
      }

    }
    // Non-recurring events
    addLocalPeriod(event->dtStart(), event->dtEnd());

    // Clean up
    delete floatingEvent;
  }

  sortList();
}

FreeBusy::~FreeBusy()
{
}

bool FreeBusy::setDtEnd( const TQDateTime &end )
{
  mDtEnd = end;
  return true;
}

TQDateTime FreeBusy::dtEnd() const
{
  return mDtEnd;
}

PeriodList FreeBusy::busyPeriods() const
{
  return mBusyPeriods;
}

bool FreeBusy::addLocalPeriod(const TQDateTime &eventStart, const TQDateTime &eventEnd ) {
  TQDateTime tmpStart;
  TQDateTime tmpEnd;

  //Check to see if the start *or* end of the event is
  //between the start and end of the freebusy dates.
  if ( !( ( ( dtStart().secsTo(eventStart) >= 0 ) &&
            ( eventStart.secsTo(dtEnd()) >= 0 ) )
       || ( ( dtStart().secsTo(eventEnd) >= 0 ) &&
            ( eventEnd.secsTo(dtEnd()) >= 0 ) ) ) )
    return false;

  if ( eventStart.secsTo( dtStart() ) >= 0 ) {
    tmpStart = dtStart();
  } else {
    tmpStart = eventStart;
  }

  if ( eventEnd.secsTo( dtEnd() ) <= 0 ) {
    tmpEnd = dtEnd();
  } else {
    tmpEnd = eventEnd;
  }

  Period p(tmpStart, tmpEnd);
  mBusyPeriods.append( p );

  return true;
}

FreeBusy::FreeBusy( PeriodList busyPeriods)
{
  mBusyPeriods = busyPeriods;
}

void FreeBusy::sortList()
{
  qHeapSort( mBusyPeriods );
  return;
}

void FreeBusy::addPeriods(const PeriodList &list )
{
  mBusyPeriods += list;
  sortList();
}

void FreeBusy::addPeriod(const TQDateTime &start, const TQDateTime &end)
{
  mBusyPeriods.append( Period(start, end) );
  sortList();
}

void FreeBusy::addPeriod( const TQDateTime &start, const Duration &dur )
{
  mBusyPeriods.append( Period(start, dur) );
  sortList();
}

void FreeBusy::merge( FreeBusy *freeBusy )
{
  if ( freeBusy->dtStart() < dtStart() )
    setDtStart( freeBusy->dtStart() );

  if ( freeBusy->dtEnd() > dtEnd() )
    setDtEnd( freeBusy->dtEnd() );

  TQValueList<Period> periods = freeBusy->busyPeriods();
  TQValueList<Period>::ConstIterator it;
  for ( it = periods.begin(); it != periods.end(); ++it )
    addPeriod( (*it).start(), (*it).end() );
}