~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/generic/dataengines/calendar/eventdatacontainer.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (c) 2010 Frederik Gladhorn <gladhorn@kde.org>
 
3
 
 
4
    This library is free software; you can redistribute it and/or modify it
 
5
    under the terms of the GNU Library General Public License as published by
 
6
    the Free Software Foundation; either version 2 of the License, or (at your
 
7
    option) any later version.
 
8
 
 
9
    This library is distributed in the hope that it will be useful, but WITHOUT
 
10
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
11
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
 
12
    License for more details.
 
13
 
 
14
    You should have received a copy of the GNU Library General Public License
 
15
    along with this library; see the file COPYING.LIB.  If not, write to the
 
16
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
17
    02110-1301, USA.
 
18
*/
 
19
 
 
20
#include "eventdatacontainer.h"
 
21
 
 
22
#include <KSystemTimeZones>
 
23
 
 
24
#include <KCalCore/Calendar>
 
25
#include <KCalCore/Event>
 
26
#include <KCalCore/Todo>
 
27
#include <KCalCore/Journal>
 
28
#include <KCalUtils/Stringify>
 
29
 
 
30
#include "akonadi/calendar.h"
 
31
#include "akonadi/calendarmodel.h"
 
32
 
 
33
using namespace Akonadi;
 
34
using namespace CalendarSupport;
 
35
 
 
36
EventDataContainer::EventDataContainer(CalendarSupport::Calendar* calendar, const QString& name, const KDateTime& start, const KDateTime& end, QObject* parent)
 
37
                  : Plasma::DataContainer(parent),
 
38
                    m_calendar(calendar),
 
39
                    m_name(name),
 
40
                    m_startDate(start),
 
41
                    m_endDate(end)
 
42
{
 
43
    // name under which this dataEngine source appears
 
44
    setObjectName(name);
 
45
 
 
46
    // Connect directly to the calendar for now
 
47
    connect(calendar, SIGNAL(calendarChanged()), this, SLOT(updateData()));
 
48
 
 
49
    // create the initial data
 
50
    updateData();
 
51
}
 
52
 
 
53
void EventDataContainer::updateData()
 
54
{
 
55
    removeAllData();
 
56
    updateEventData();
 
57
    updateTodoData();
 
58
    updateJournalData();
 
59
    checkForUpdate();
 
60
}
 
61
 
 
62
void EventDataContainer::updateEventData()
 
63
{
 
64
    Akonadi::Item::List events = m_calendar->events(m_startDate.date(), m_endDate.date(), m_calendar->timeSpec());
 
65
 
 
66
    foreach (const Akonadi::Item &item, events) {
 
67
        Q_ASSERT(item.hasPayload<KCalCore::Event::Ptr>());
 
68
        const KCalCore::Event::Ptr event = item.payload<KCalCore::Event::Ptr>();
 
69
 
 
70
        Plasma::DataEngine::Data eventData;
 
71
 
 
72
        populateIncidenceData(event, eventData);
 
73
 
 
74
        // Event specific fields
 
75
        eventData["EventMultiDay"] = event->allDay();
 
76
        eventData["EventHasEndDate"] = event->hasEndDate();
 
77
        if (event->transparency() == KCalCore::Event::Opaque) {
 
78
            eventData["EventTransparency"] = "Opaque";
 
79
        } else if (event->transparency() == KCalCore::Event::Transparent) {
 
80
            eventData["EventTransparency"] = "Transparent";
 
81
        } else {
 
82
            eventData["EventTransparency"] = "Unknown";
 
83
        }
 
84
 
 
85
        setData(event->uid(), eventData);
 
86
    }
 
87
}
 
88
 
 
89
void EventDataContainer::updateTodoData()
 
90
{
 
91
    QDate todoDate = m_startDate.date();
 
92
    while(todoDate <= m_endDate.date()) {
 
93
        Akonadi::Item::List todos = m_calendar->todos(todoDate);
 
94
 
 
95
        foreach (const Akonadi::Item &item, todos) {
 
96
            Q_ASSERT(item.hasPayload<KCalCore::Todo::Ptr>());
 
97
            const KCalCore::Todo::Ptr todo = item.payload<KCalCore::Todo::Ptr>();
 
98
 
 
99
            Plasma::DataEngine::Data todoData;
 
100
 
 
101
            populateIncidenceData(todo, todoData);
 
102
 
 
103
            QVariant var;
 
104
            // Todo specific fields
 
105
            todoData["TodoHasStartDate"] = todo->hasStartDate();
 
106
            todoData["TodoIsOpenEnded"] = todo->isOpenEnded();
 
107
            todoData["TodoHasDueDate"] = todo->hasDueDate();
 
108
            var.setValue(todo->dtDue());
 
109
            todoData["TodoDueDate"] = var;
 
110
            todoData["TodoIsCompleted"] = todo->isCompleted();
 
111
            todoData["TodoIsInProgress"] = todo->isInProgress(false); // ???
 
112
            todoData["TodoIsNotStarted"] = todo->isNotStarted(false); // ???
 
113
            todoData["TodoPercentComplete"] = todo->percentComplete();
 
114
            todoData["TodoHasCompletedDate"] = todo->hasCompletedDate();
 
115
            var.setValue(todo->completed());
 
116
            todoData["TodoCompletedDate"] = var;
 
117
 
 
118
            setData(todo->uid(), todoData);
 
119
        }
 
120
 
 
121
        todoDate = todoDate.addDays(1);
 
122
    }
 
123
}
 
124
 
 
125
void EventDataContainer::updateJournalData()
 
126
{
 
127
    QDate journalDate = m_startDate.date();
 
128
    while(journalDate <= m_endDate.date()) {
 
129
        Akonadi::Item::List journals = m_calendar->journals(journalDate);
 
130
 
 
131
        foreach (const Akonadi::Item &item, journals) {
 
132
            Q_ASSERT(item.hasPayload<KCalCore::Journal::Ptr>());
 
133
            const KCalCore::Journal::Ptr journal = item.payload<KCalCore::Journal::Ptr>();
 
134
 
 
135
            Plasma::DataEngine::Data journalData;
 
136
 
 
137
            populateIncidenceData(journal, journalData);
 
138
 
 
139
            // No Journal specific fields
 
140
 
 
141
            setData(journal->uid(), journalData);
 
142
        }
 
143
 
 
144
        journalDate = journalDate.addDays(1);
 
145
    }
 
146
}
 
147
 
 
148
void EventDataContainer::populateIncidenceData(KCalCore::Incidence::Ptr incidence, Plasma::DataEngine::Data &incidenceData)
 
149
{
 
150
    QVariant var;
 
151
    incidenceData["UID"] = incidence->uid();
 
152
    incidenceData["Type"] = incidence->typeStr();
 
153
    incidenceData["Summary"] = incidence->summary();
 
154
    incidenceData["Description"] = incidence->description();
 
155
    incidenceData["Comments"] = incidence->comments();
 
156
    incidenceData["Location"] = incidence->location();
 
157
    incidenceData["OrganizerName"] = incidence->organizer()->name();
 
158
    incidenceData["OrganizerEmail"] = incidence->organizer()->email();
 
159
    incidenceData["Priority"] = incidence->priority();
 
160
    var.setValue(incidence->dtStart());
 
161
    incidenceData["StartDate"] = var;
 
162
 
 
163
    KCalCore::Event* event = dynamic_cast<KCalCore::Event*>(incidence.data());
 
164
    if (event) {
 
165
        var.setValue(event->dtEnd());
 
166
        incidenceData["EndDate"] = var;
 
167
    }
 
168
    // Build the Occurance Index, this lists all occurences of the Incidence in the required range
 
169
    // Single occurance events just repeat the standard start/end dates
 
170
    // Recurring Events use each recurrence start/end date
 
171
    // The OccurenceUid is redundant, but it makes it easy for clients to just take() the data structure intact as a separate index
 
172
    QList<QVariant> occurences;
 
173
    // Build the recurrence list of start dates only for recurring incidences only
 
174
    QList<QVariant> recurrences;
 
175
    if (incidence->recurs()) {
 
176
        KCalCore::DateTimeList recurList = incidence->recurrence()->timesInInterval(m_startDate, m_endDate);
 
177
        foreach(const KDateTime &recurDateTime, recurList) {
 
178
            var.setValue(recurDateTime);
 
179
            recurrences.append(var);
 
180
            Plasma::DataEngine::Data occurence;
 
181
            occurence.insert("OccurrenceUid", incidence->uid());
 
182
            occurence.insert("OccurrenceStartDate", var);
 
183
            var.setValue(incidence->endDateForStart(recurDateTime));
 
184
            occurence.insert("OccurrenceEndDate", var);
 
185
            occurences.append(QVariant(occurence));
 
186
        }
 
187
    } else {
 
188
        Plasma::DataEngine::Data occurence;
 
189
        occurence.insert("OccurrenceUid", incidence->uid());
 
190
        var.setValue(incidence->dtStart());
 
191
        occurence.insert("OccurrenceStartDate", var);
 
192
        if (event) {
 
193
            var.setValue(event->dtEnd());
 
194
            occurence.insert("OccurrenceEndDate", var);
 
195
        }
 
196
        occurences.append(QVariant(occurence));
 
197
    }
 
198
    incidenceData["RecurrenceDates"] = QVariant(recurrences);
 
199
    incidenceData["Occurrences"] = QVariant(occurences);
 
200
    if (incidence->status() == KCalCore::Incidence::StatusNone) {
 
201
        incidenceData["Status"] = "None";
 
202
    } else if (incidence->status() == KCalCore::Incidence::StatusTentative) {
 
203
        incidenceData["Status"] = "Tentative";
 
204
    } else if (incidence->status() == KCalCore::Incidence::StatusConfirmed) {
 
205
        incidenceData["Status"] = "Confirmed";
 
206
    } else if (incidence->status() == KCalCore::Incidence::StatusDraft) {
 
207
        incidenceData["Status"] = "Draft";
 
208
    } else if (incidence->status() == KCalCore::Incidence::StatusFinal) {
 
209
        incidenceData["Status"] = "Final";
 
210
    } else if (incidence->status() == KCalCore::Incidence::StatusCompleted) {
 
211
        incidenceData["Status"] = "Completed";
 
212
    } else if (incidence->status() == KCalCore::Incidence::StatusInProcess) {
 
213
        incidenceData["Status"] = "InProcess";
 
214
    } else if (incidence->status() == KCalCore::Incidence::StatusCanceled) {
 
215
        incidenceData["Status"] = "Cancelled";
 
216
    } else if (incidence->status() == KCalCore::Incidence::StatusNeedsAction) {
 
217
        incidenceData["Status"] = "NeedsAction";
 
218
    } else if (incidence->status() == KCalCore::Incidence::StatusX) {
 
219
        incidenceData["Status"] = "NonStandard";
 
220
    } else {
 
221
        incidenceData["Status"] = "Unknown";
 
222
    }
 
223
    incidenceData["StatusName"] = KCalUtils::Stringify::incidenceStatus( incidence->status() );
 
224
 
 
225
    if (incidence->secrecy() == KCalCore::Incidence::SecrecyPublic) {
 
226
        incidenceData["Secrecy"] = "Public";
 
227
    } else if (incidence->secrecy() == KCalCore::Incidence::SecrecyPrivate) {
 
228
        incidenceData["Secrecy"] = "Private";
 
229
    } else if (incidence->secrecy() == KCalCore::Incidence::SecrecyConfidential) {
 
230
        incidenceData["Secrecy"] = "Confidential";
 
231
    } else {
 
232
        incidenceData["Secrecy"] = "Unknown";
 
233
    }
 
234
    incidenceData["SecrecyName"] = KCalUtils::Stringify::secrecyName( incidence->secrecy() );
 
235
    incidenceData["Recurs"] = incidence->recurs();
 
236
    incidenceData["AllDay"] = incidence->allDay();
 
237
    incidenceData["Categories"] = incidence->categories();
 
238
    incidenceData["Resources"] = incidence->resources();
 
239
    incidenceData["DurationDays"] = incidence->duration().asDays();
 
240
    incidenceData["DurationSeconds"] = incidence->duration().asSeconds();
 
241
 
 
242
    //TODO Attendees
 
243
    //TODO Attachments
 
244
    //TODO Relations
 
245
    //TODO Alarms
 
246
    //TODO Custom Properties
 
247
    //TODO Lat/Lon
 
248
    //TODO Collection/Source
 
249
}
 
250
 
 
251
#include "eventdatacontainer.moc"