Commit 39f53536 authored by Shashwat Jolly's avatar Shashwat Jolly
Browse files

Use KCalendarCore::FileStorage instead of manual QFile operations

Reading and writing calendars to files was being done using QFile operations. KCalendarCore::FileStorage is more convenient and might be faster on larger files.
parent 520849ae
Pipeline #33076 passed with stage
in 30 minutes and 31 seconds
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include <AkonadiCore/CollectionColorAttribute> #include <AkonadiCore/CollectionColorAttribute>
#include <AkonadiCore/CollectionModifyJob> #include <AkonadiCore/CollectionModifyJob>
#include <AkonadiCore/ItemModifyJob> #include <AkonadiCore/ItemModifyJob>
#include <KCalendarCore/FileStorage>
#include <KCalendarCore/ICalFormat> #include <KCalendarCore/ICalFormat>
#include <KCalendarCore/MemoryCalendar> #include <KCalendarCore/MemoryCalendar>
#include <KLocalizedString> #include <KLocalizedString>
...@@ -109,28 +110,29 @@ QString CalendarTaskBaseHandler::getLocalCalendar(const QString &incidenceUid) c ...@@ -109,28 +110,29 @@ QString CalendarTaskBaseHandler::getLocalCalendar(const QString &incidenceUid) c
{ {
const QString path = baseDirectoryPath() + QLatin1Char('/') + incidenceUid + QLatin1String(".ical"); const QString path = baseDirectoryPath() + QLatin1Char('/') + incidenceUid + QLatin1String(".ical");
QFile file(path); const auto calendar = Calendar::Ptr(new MemoryCalendar(QTimeZone::utc()));
if (!file.open(QFile::ReadOnly | QFile::Text)) { const auto fileStorage = FileStorage::Ptr(new FileStorage(calendar, path, new ICalFormat()));
qCDebug(ETESYNC_LOG) << "Unable to read " << path << file.errorString();
if (!fileStorage->load()) {
qCDebug(ETESYNC_LOG) << "Unable to read " << path;
return QString(); return QString();
} }
QTextStream in(&file); KCalendarCore::ICalFormat format;
return in.readAll(); return format.toString(calendar);
} }
bool CalendarTaskBaseHandler::updateLocalCalendar(const KCalendarCore::Incidence::Ptr &incidence) bool CalendarTaskBaseHandler::updateLocalCalendar(const KCalendarCore::Incidence::Ptr &incidence)
{ {
const QString path = baseDirectoryPath() + QLatin1Char('/') + incidence->uid() + QLatin1String(".ical"); const QString path = baseDirectoryPath() + QLatin1Char('/') + incidence->uid() + QLatin1String(".ical");
QFile file(path); const auto calendar = Calendar::Ptr(new MemoryCalendar(QTimeZone::utc()));
if (!file.open(QIODevice::WriteOnly)) { calendar->addIncidence(incidence);
qCDebug(ETESYNC_LOG) << "Unable to open " << path << file.errorString(); const auto fileStorage = FileStorage::Ptr(new FileStorage(calendar, path, new ICalFormat()));
if (!fileStorage->save()) {
qCDebug(ETESYNC_LOG) << "Unable to read " << path;
return false; return false;
} }
KCalendarCore::Calendar::Ptr calendar(new MemoryCalendar(QTimeZone::utc()));
calendar->addIncidence(incidence);
KCalendarCore::ICalFormat format;
file.write(format.toString(calendar).toUtf8());
return true; return true;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment