summaryrefslogtreecommitdiff
path: root/noncore/unsupported/libopie/pim/odatebookaccessbackend.cpp
blob: f0c5d65af9e6c4a5500aabfb1de484ab144a7a38 (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
#include <qtl.h>

#include "orecur.h"

#include "odatebookaccessbackend.h"

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

        for ( OEvent::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 ) {
                OEffectiveEvent eff;
                eff.setEvent( (*it) );
                eff.setDate( dtStart.date() );
                eff.setStartTime( dtStart.time() );

                /* if not on the same day */
                if ( dtStart.date() != dtEnd.date() )
                    eff.setEndTime( QTime(23, 59, 0 ) );
                else
                    eff.setEndTime( dtEnd.time() );

                tmpList.append( eff );
            }

            /* we must also check for end date information... */
            if ( dtEnd.date() != dtStart.date() && dtEnd.date() >= from ) {
                QDateTime dt = dtStart.addDays( 1 );
                dt.setTime( QTime(0, 0, 0 ) );
                QDateTime dtStop;
                if ( dtEnd > to )
                    dtStop = to;
                else
                    dtStop = dtEnd;

                while ( dt <= dtStop ) {
                    OEffectiveEvent eff;
                    eff.setEvent( (*it) );
                    eff.setDate( dt.date() );

                    if ( dt >= from ) {
                        eff.setStartTime( QTime(0, 0, 0 ) );
                        if ( dt.date() == dtEnd.date() )
                            eff.setEndTime( dtEnd.time() );
                        else
                            eff.setEndTime( QTime(23, 59, 0 ) );
                        tmpList.append( eff );
                    }
                    dt = dt.addDays( 1 );
                }
            }
        }
    }

    void repeat( OEffectiveEvent::ValueList& tmpList, const OEvent::ValueList& list,
                 const QDate& from, const QDate& to ) {
        QDate repeat;
        for ( OEvent::ValueList::ConstIterator it = list.begin(); it != list.end(); ++it ) {
            int dur = (*it).startDateTime().date().daysTo( (*it).endDateTime().date() );
            QDate itDate = from.addDays(-dur );
            ORecur rec = (*it).recurrence();
            if ( !rec.hasEndDate() || rec.endDate() > to ) {
                rec.setEndDate( to );
                rec.setHasEndDate( true );
            }
            while (rec.nextOcurrence(itDate, repeat ) ) {
                if (repeat > to ) break;
                OEffectiveEvent eff;
                eff.setDate( repeat );
                if ( (*it).isAllDay() ) {
                    eff.setStartTime( QTime(0, 0, 0 ) );
                    eff.setEndTime( QTime(23, 59, 59 ) );
                }else {
                    /* we only occur by days, not hours/minutes/seconds.  Hence
                     * the actual end and start times will be the same for
                     * every repeated event.  For multi day events this is
                     * fixed up later if on wronge day span
                     */
                    eff.setStartTime( (*it).startDateTime().time() );
                    eff.setEndTime( (*it).endDateTime().time() );
                }
                if ( dur != 0 ) {
                    // multi-day repeating events
                    QDate sub_it = QMAX( repeat, from );
                    QDate startDate = repeat;
                    QDate endDate = startDate.addDays( dur );

                    while ( sub_it <= endDate && sub_it <= to ) {
                        OEffectiveEvent tmpEff = eff;
                        tmpEff.setEvent( (*it) );
                        if ( sub_it != startDate )
                            tmpEff.setStartTime( QTime(0, 0, 0 ) );
                        if ( sub_it != endDate )
                            tmpEff.setEndTime( QTime( 23, 59, 59 ) );

                        tmpEff.setDate( sub_it );
                        tmpEff.setEffectiveDates( startDate, endDate );
                        tmpList.append( tmpEff );

                        sub_it = sub_it.addDays( 1 );
                    }
                    itDate = endDate;
                }else {
                    eff.setEvent( (*it) );
                    tmpList.append( eff );
                    itDate = repeat.addDays( 1 );
                }
            }
        }
    }
}

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

}
ODateBookAccessBackend::~ODateBookAccessBackend() {

}
OEffectiveEvent::ValueList ODateBookAccessBackend::effectiveEvents( const QDate& from,
                                                                    const QDate& to ) {
    OEffectiveEvent::ValueList tmpList;
    OEvent::ValueList list = directNonRepeats();

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

    list = directRawRepeats();  // Useless, isn't it ? (eilers)

    qHeapSort( tmpList );
    return tmpList;
}
OEffectiveEvent::ValueList ODateBookAccessBackend::effectiveEvents( const QDateTime& dt ) {
    OEffectiveEvent::ValueList day = effectiveEvents( dt.date(), dt.date() );
    OEffectiveEvent::ValueList::Iterator it;

    OEffectiveEvent::ValueList tmpList;
    QDateTime dtTmp;
    for ( it = day.begin(); it != day.end(); ++it ) {
        dtTmp = QDateTime( (*it).date(), (*it).startTime() );
        if ( QABS(dt.secsTo(dtTmp) ) < 60 )
            tmpList.append( (*it) );
    }

    return tmpList;
}

OEffectiveEvent::ValueList ODateBookAccessBackend::effectiveNonRepeatingEvents( const QDate& from,
                                                                    const QDate& to ) {
    OEffectiveEvent::ValueList tmpList;
    OEvent::ValueList list = directNonRepeats();

    events( tmpList, list, from, to );

    qHeapSort( tmpList );
    return tmpList;
}

OEffectiveEvent::ValueList ODateBookAccessBackend::effectiveNonRepeatingEvents( const QDateTime& dt ) {
    OEffectiveEvent::ValueList day = effectiveNonRepeatingEvents( dt.date(), dt.date() );
    OEffectiveEvent::ValueList::Iterator it;

    OEffectiveEvent::ValueList tmpList;
    QDateTime dtTmp;
    for ( it = day.begin(); it != day.end(); ++it ) {
        dtTmp = QDateTime( (*it).date(), (*it).startTime() );
        if ( QABS(dt.secsTo(dtTmp) ) < 60 )
            tmpList.append( (*it) );
    }

    return tmpList;
}