File: icalendar.cpp

package info (click to toggle)
kdepim-runtime 4%3A20.08.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 25,680 kB
  • sloc: cpp: 94,670; xml: 981; sh: 101; javascript: 60; makefile: 13
file content (159 lines) | stat: -rw-r--r-- 6,581 bytes parent folder | download
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
/*

    Copyright (C) 2012  Christian Mollekopf <chrigi_1@fastmail.fm>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 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
    Lesser General Public License for more details.

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

#include "icalendar.h"
#include "imip.h"
#include "pimkolab_debug.h"
#include "libkolab-version.h"
#include <conversion/kcalconversion.h>
#include <conversion/commonconversion.h>
#include <mime/mimeutils.h>
#include <kcalendarcore/event.h>
#include <KCalendarCore/MemoryCalendar>
#include <KCalendarCore/ICalFormat>
#include <kmime/kmime_message.h>
#include <KLocalizedString>
#include <iostream>
#include <QTimeZone>

namespace Kolab {
std::string toICal(const std::vector<Event> &events)
{
    KCalendarCore::Calendar::Ptr calendar(new KCalendarCore::MemoryCalendar(Kolab::Conversion::getTimeSpec(true, std::string())));
    for (const Event &event : events) {
        KCalendarCore::Event::Ptr kcalEvent = Conversion::toKCalendarCore(event);
        kcalEvent->setCreated(QDateTime::currentDateTimeUtc()); //sets dtstamp
        calendar->addEvent(kcalEvent);
    }
    KCalendarCore::ICalFormat format;
    format.setApplication(QStringLiteral("libkolab"), QStringLiteral(LIBKOLAB_LIB_VERSION_STRING));
//     qCDebug(PIMKOLAB_LOG) << format.createScheduleMessage(calendar->events().first(), KCalendarCore::iTIPRequest);

    return Conversion::toStdString(format.toString(calendar));
}

std::vector< Event > fromICalEvents(const std::string &input)
{
    KCalendarCore::Calendar::Ptr calendar(new KCalendarCore::MemoryCalendar(Kolab::Conversion::getTimeSpec(true, std::string())));
    KCalendarCore::ICalFormat format;
    format.setApplication(QStringLiteral("libkolab"), QStringLiteral(LIBKOLAB_LIB_VERSION_STRING));
    format.fromString(calendar, Conversion::fromStdString(input));
    std::vector<Event> events;
    foreach (const KCalendarCore::Event::Ptr &event, calendar->events()) {
        events.push_back(Conversion::fromKCalendarCore(*event));
    }
    return events;
}

ITipHandler::ITipHandler()
    :   mMethod(iTIPNoMethod)
{
}

ITipHandler::ITipMethod mapFromKCalendarCore(KCalendarCore::iTIPMethod method)
{
    Q_ASSERT((int)KCalendarCore::iTIPPublish == (int)ITipHandler::iTIPPublish);
    Q_ASSERT((int)KCalendarCore::iTIPNoMethod == (int)ITipHandler::iTIPNoMethod);
    return static_cast<ITipHandler::ITipMethod>(method);
}

KCalendarCore::iTIPMethod mapToKCalendarCore(ITipHandler::ITipMethod method)
{
    Q_ASSERT((int)KCalendarCore::iTIPPublish == (int)ITipHandler::iTIPPublish);
    Q_ASSERT((int)KCalendarCore::iTIPNoMethod == (int)ITipHandler::iTIPNoMethod);
    return static_cast<KCalendarCore::iTIPMethod>(method);
}

std::string ITipHandler::toITip(const Event &event, ITipHandler::ITipMethod method) const
{
    KCalendarCore::ICalFormat format;
    format.setApplication(QStringLiteral("libkolab"), QStringLiteral(LIBKOLAB_LIB_VERSION_STRING));
    KCalendarCore::iTIPMethod m = mapToKCalendarCore(method);
    if (m == KCalendarCore::iTIPNoMethod) {
        return std::string();
    }
//     qCDebug(PIMKOLAB_LOG) << event.start().
/* TODO
 * DTSTAMP is created
 * CREATED is current timestamp
 * LASTMODIFIED is lastModified
 *
 * Double check if that is correct.
 *
 * I think DTSTAMP should be the current timestamp, and CREATED should be the creation date.
 */
    KCalendarCore::Event::Ptr e = Conversion::toKCalendarCore(event);
    return Conversion::toStdString(format.createScheduleMessage(e, m));
}

std::vector< Event > ITipHandler::fromITip(const std::string &string)
{
    KCalendarCore::Calendar::Ptr calendar(new KCalendarCore::MemoryCalendar(QTimeZone::utc()));
    KCalendarCore::ICalFormat format;
    KCalendarCore::ScheduleMessage::Ptr msg = format.parseScheduleMessage(calendar, Conversion::fromStdString(string));
    KCalendarCore::Event::Ptr event = msg->event().dynamicCast<KCalendarCore::Event>();
    std::vector< Event > events;
    events.push_back(Conversion::fromKCalendarCore(*event));
    mMethod = mapFromKCalendarCore(msg->method());
    return events;
}

ITipHandler::ITipMethod ITipHandler::method() const
{
    return mMethod;
}

std::string ITipHandler::toIMip(const Event &event, ITipHandler::ITipMethod m, const std::string &from, bool bccMe) const
{
    KCalendarCore::Event::Ptr e = Conversion::toKCalendarCore(event);
//     e->recurrence()->addRDateTime(e->dtStart()); //FIXME The createScheduleMessage converts everything to utc without a recurrence.
    KCalendarCore::ICalFormat format;
    format.setApplication(QStringLiteral("libkolab"), QStringLiteral(LIBKOLAB_LIB_VERSION_STRING));
    KCalendarCore::iTIPMethod method = mapToKCalendarCore(m);
    const QString &messageText = format.createScheduleMessage(e, method);
    //This code is mostly from MailScheduler::performTransaction
    if (method == KCalendarCore::iTIPRequest
        || method == KCalendarCore::iTIPCancel
        || method == KCalendarCore::iTIPAdd
        || method == KCalendarCore::iTIPDeclineCounter) {
        return Conversion::toStdString(QString::fromUtf8(mailAttendees(e, bccMe, messageText)));
    } else {
        QString subject;
        if (e && method == KCalendarCore::iTIPCounter) {
            subject = i18n("Counter proposal: %1", e->summary());
        }
        return Conversion::toStdString(QString::fromUtf8(mailOrganizer(e, Conversion::fromStdString(from), bccMe, messageText, subject)));
    }
}

std::vector< Event > ITipHandler::fromIMip(const std::string &input)
{
    KMime::Message::Ptr msg = KMime::Message::Ptr(new KMime::Message);
    msg->setContent(Conversion::fromStdString(input).toUtf8());
    msg->parse();
    msg->content(KMime::ContentIndex());

    KMime::Content *c = Kolab::Mime::findContentByType(msg, "text/calendar");
    if (!c) {
        qCWarning(PIMKOLAB_LOG) << "could not find text/calendar part";
        return std::vector< Event >();
    }
    return fromITip(Conversion::toStdString(QString::fromUtf8(c->decodedContent())));
}
}