summaryrefslogtreecommitdiff
path: root/libopie2/opiepim/backend/odatebookaccessbackend.cpp
blob: e44912ad94fdf3cff2ff78afb3f02dbf32f90467 (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
/*
                             This file is part of the Opie Project
                             Copyright (C) Stefan Eilers (Eilers.Stefan@epost.de)
              =.             Copyright (C) The Opie Team <opie-devel@handhelds.org>
            .=l.
           .>+-=
 _;:,     .>    :=|.         This program is free software; you can
.> <`_,   >  .   <=          redistribute it and/or  modify it under
:`=1 )Y*s>-.--   :           the terms of the GNU Library General Public
.="- .-=="i,     .._         License as published by the Free Software
 - .   .-<_>     .<>         Foundation; either version 2 of the License,
     ._= =}       :          or (at your option) any later version.
    .%`+i>       _;_.
    .i_,=:_.      -<s.       This program 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., 59 Temple Place - Suite 330,
                             Boston, MA 02111-1307, USA.
*/
#include <qtl.h>

#include <opie2/opimrecurrence.h>

#include <opie2/odatebookaccessbackend.h>

using namespace Opie;

namespace {
/* a small helper to get all NonRepeating events for a range of time */
void events( OPimBackendOccurrence::List& tmpList,
             const OPimEvent::ValueList& events,
             const QDate& from, const QDate& to ) {
    QDateTime dtStart, dtEnd;

    for ( OPimEvent::ValueList::ConstIterator it = events.begin(); it != events.end(); ++it ) {
        dtStart = (*it).startDateTime();
        dtEnd   = (*it).endDateTime();

        /*
         * If in range
         */
        if (dtStart.date() >= from && dtEnd.date() <= to ) {
            OPimBackendOccurrence eff( dtStart, dtEnd, (*it).uid() );;
            tmpList.append( eff );
        }
    }
}

void repeat( OPimBackendOccurrence::List& tmpList, const OPimEvent::ValueList& list,
             const QDate& from, const QDate& to ) {
    QDate repeat;
    for ( OPimEvent::ValueList::ConstIterator it = list.begin(); it != list.end(); ++it ) {
        int dur = (*it).startDateTime().date().daysTo( (*it).endDateTime().date() );
        QDate itDate = from.addDays(-dur );
        OPimRecurrence rec = (*it).recurrence();
        if ( !rec.hasEndDate() || rec.endDate() > to ) {
            rec.setEndDate( to );
            rec.setHasEndDate( true );
        }

        QDateTime start, end;
        while (rec.nextOcurrence(itDate, repeat ) ) {
            if (repeat > to ) break;

            OPimEvent event = *it;
            start = QDateTime( repeat, event.startDateTime().time() );
            end   = QDateTime( repeat.addDays(dur), event.endDateTime().time() );
            OPimBackendOccurrence eff(start, end, event.uid() );
            tmpList.append( eff );
        }
    }
}
}

namespace Opie {

ODateBookAccessBackend::ODateBookAccessBackend()
    : OPimAccessBackend<OPimEvent>()
{

}
ODateBookAccessBackend::~ODateBookAccessBackend() {

}
OPimBackendOccurrence::List ODateBookAccessBackend::occurrences( const QDate& from,
                                                                 const QDate& to )const {
    OPimBackendOccurrence::List tmpList;

    events( tmpList, directNonRepeats(), from, to );
    repeat( tmpList, directRawRepeats(),from,to );

    return tmpList;
}

OPimBackendOccurrence::List ODateBookAccessBackend::occurrences( const QDateTime& dt )const 
{
    OPimBackendOccurrence::List day = occurrences( dt.date(), dt.date() );

    return filterOccurrences( day, dt );
}

OPimBackendOccurrence::List ODateBookAccessBackend::effectiveNonRepeatingEvents( const QDate& from,
                                                                                 const QDate& to )const 
{
    OPimBackendOccurrence::List tmpList;
    OPimEvent::ValueList list = directNonRepeats();

    events( tmpList, list, from, to );

    return tmpList;
}

OPimBackendOccurrence::List ODateBookAccessBackend::effectiveNonRepeatingEvents( const QDateTime& dt )const 
{
    OPimBackendOccurrence::List day = effectiveNonRepeatingEvents( dt.date(), dt.date() );
    return filterOccurrences( day,dt );
}

const uint ODateBookAccessBackend::querySettings() const
{
	return 0;
}

bool ODateBookAccessBackend::hasQuerySettings (uint querySettings) const
{
	return false;
}



UIDArray ODateBookAccessBackend::queryByExample( const UIDArray& uidlist, const OPimEvent&, int settings,
                                                 const QDateTime& d )const 
{
	qDebug( "Accessing ODateBookAccessBackend::queryByExample() which is not implemented!" );
    return UIDArray();
}

UIDArray ODateBookAccessBackend::sorted( const UIDArray&, bool asc, int, int, const QArray<int>& )const {
    return UIDArray();
}

OPimBackendOccurrence::List ODateBookAccessBackend::filterOccurrences( const OPimBackendOccurrence::List dayList,
                                                                       const QDateTime& dt ) {
    OPimBackendOccurrence::List tmpList;
    OPimBackendOccurrence::List::ConstIterator it;

    for ( it = dayList.begin(); it != dayList.end(); ++it ) {
        OPimBackendOccurrence occ = *it;

        /*
         * Let us find occurrences that are 'now'!
         * If the dt.date() is on the same day as start or end of the Occurrence
         * check how near start/end are.
         * If it is in the middle of a multiday occurrence list it.
         *
         * We might want to 'lose' the sixty second offset and list
         * all Events which are active at that time.
         */
        if ( dt.date() == occ.startDateTime().date() ) {
            if ( QABS( dt.time().secsTo( occ.startDateTime().time() ) ) < 60 )
                tmpList.append( occ );
        }else if ( dt.date() == occ.endDateTime().date() ) {
            if ( QABS( dt.time().secsTo( occ.endDateTime().time()   ) ) < 60 )
                tmpList.append( occ );
        }else if ( dt.date() >= occ.startDateTime().date() &&
                   dt.date() >= occ.endDateTime().date()  )
            tmpList.append( occ );
    }

    return tmpList;
}
}