summaryrefslogtreecommitdiff
path: root/libopie2/opiepim/core/opimtemplatebase.cpp
blob: 0a073200654a497b7b840e81e434a09b49e1c985 (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
/*
                             This file is part of the Opie Project
                             Copyright (C) 2004 Holger Freyther <zecke@handhelds.org>
              =.             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 "opimtemplatebase.h"

#include <opie2/opimoccurrence.h>
#include <opie2/private/opimoccurrence_p.h>

namespace Opie {

static void setPeriod( OPimOccurrence& oc, bool period, const QDate& d,
                       const QTime& s, const QTime& t ) {
    if ( period )
        oc.setPeriod( d );
    else
        oc.setPeriod( d, s, t );
}

// namespace Opie {
OPimBase::OPimBase() {}
OPimBase::~OPimBase() {}

/**
 * @internal Convert internal Occurrence representation
 * to the external
 */
OPimOccurrence::List OPimBase::convertOccurrenceFromBackend( const OPimBackendOccurrence::List& lst )const {
    OPimOccurrence::List oc_lst;

    /*
     * Split multiday events up. Create the internal data structure
     * and then iterate over the days and create the OPimOccurrecne.
     */
    for ( OPimBackendOccurrence::List::ConstIterator it = lst.begin(); it != lst.end(); ++it ) {
        OPimBackendOccurrence boc = *it;

        /*
         * Create the Shared Data Structure
         */
        OPimOccurrence::Data *data = new OPimOccurrence::Data();
        data->summary = boc.summary();
        data->location = boc.location();
        data->note = boc.note();
        data->uid = boc.uid();
        data->backend = const_cast<OPimBase*>(this);

        QDateTime start = boc.startDateTime();
        QDateTime end = boc.endDateTime();

        /*
         * Start and End are on the same day
         * Start and End are on two different ways.
         *      - Add Start and End and the days inbetween
         */
        int dto = start.daysTo( end );
        bool allDay = boc.isAllDay();

        if ( dto == 0 ) {
            OPimOccurrence oc = OPimOccurrence( data, OPimOccurrence::StartEnd );
            setPeriod( oc, allDay, start.date(), start.time(), end.time() );
            oc_lst.append( oc );
        }else {

            OPimOccurrence oc = OPimOccurrence( data, OPimOccurrence::Start );
            setPeriod( oc, allDay, start.date(), start.time(), QTime(23,59,59));
            oc_lst.append( oc );

            QDate next = start.addDays( 1 ).date();
            while ( next != end.date() ) {
                oc = OPimOccurrence( data, OPimOccurrence::MidWay );
                setPeriod( oc, allDay, next, QTime(0, 0, 0), QTime(23, 59, 59));
                oc_lst.append( oc );
                next = next.addDays( 1 );
            }

            oc = OPimOccurrence( data, OPimOccurrence::End );
            setPeriod( oc, allDay, end.date(), QTime(0, 0, 0 ), end.time() );
            oc_lst.append( oc );
        }
    }

    return oc_lst;
}
// }

}