File indexing completed on 2024-12-22 04:57:14

0001 /*
0002     SPDX-FileCopyrightText: 2006 Till Adam <adam@kde.org>
0003     SPDX-FileCopyrightText: 2009 David Jarvie <djarvie@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "icalresourcebase.h"
0009 #include "icalsettingsadaptor.h"
0010 
0011 #include <QDBusConnection>
0012 
0013 #include <KCalendarCore/ICalFormat>
0014 #include <KCalendarCore/Incidence>
0015 
0016 #include <KLocalizedString>
0017 #include <QDebug>
0018 #include <QTimeZone>
0019 
0020 using namespace Akonadi;
0021 using namespace KCalendarCore;
0022 using namespace SETTINGS_NAMESPACE;
0023 
0024 ICalResourceBase::ICalResourceBase(const QString &id)
0025     : SingleFileResource<Settings>(id)
0026 {
0027 }
0028 
0029 void ICalResourceBase::initialise(const QStringList &mimeTypes, const QString &icon)
0030 {
0031     setSupportedMimetypes(mimeTypes, icon);
0032     new ICalSettingsAdaptor(mSettings);
0033     QDBusConnection::sessionBus().registerObject(QStringLiteral("/Settings"), mSettings, QDBusConnection::ExportAdaptors);
0034 }
0035 
0036 ICalResourceBase::~ICalResourceBase() = default;
0037 
0038 bool ICalResourceBase::retrieveItems(const Akonadi::Item::List &items, const QSet<QByteArray> &parts)
0039 {
0040     for (const Akonadi::Item &item : items) {
0041         qDebug() << "Item:" << item.url();
0042     }
0043 
0044     if (!mCalendar) {
0045         qCritical() << "akonadi_ical_resource: Calendar not loaded";
0046         Q_EMIT error(i18n("Calendar not loaded."));
0047         return false;
0048     }
0049 
0050     return doRetrieveItems(items, parts);
0051 }
0052 
0053 bool ICalResourceBase::retrieveItem(const Akonadi::Item &item, const QSet<QByteArray> &parts)
0054 {
0055     qDebug() << "Item:" << item.url();
0056 
0057     if (!mCalendar) {
0058         qCritical() << "akonadi_ical_resource: Calendar not loaded";
0059         Q_EMIT error(i18n("Calendar not loaded."));
0060         return false;
0061     }
0062 
0063     return doRetrieveItems({item}, parts);
0064 }
0065 
0066 void ICalResourceBase::aboutToQuit()
0067 {
0068     if (!mSettings->readOnly()) {
0069         writeFile();
0070     }
0071     mSettings->save();
0072 }
0073 
0074 bool ICalResourceBase::readFromFile(const QString &fileName)
0075 {
0076     mCalendar = KCalendarCore::MemoryCalendar::Ptr(new KCalendarCore::MemoryCalendar(QTimeZone::utc()));
0077     mFileStorage = KCalendarCore::FileStorage::Ptr(new KCalendarCore::FileStorage(mCalendar, fileName, new KCalendarCore::ICalFormat()));
0078     const bool result = mFileStorage->load();
0079     if (!result) {
0080         qCritical() << "akonadi_ical_resource: Error loading file " << fileName;
0081     }
0082 
0083     return result;
0084 }
0085 
0086 void ICalResourceBase::itemRemoved(const Akonadi::Item &item)
0087 {
0088     if (!mCalendar) {
0089         qCritical() << "akonadi_ical_resource: mCalendar is 0!";
0090         cancelTask(i18n("Calendar not loaded."));
0091         return;
0092     }
0093 
0094     const Incidence::Ptr i = mCalendar->instance(item.remoteId());
0095     if (i) {
0096         if (!mCalendar->deleteIncidence(i)) {
0097             qCritical() << "akonadi_ical_resource: Can't delete incidence with instance identifier " << item.remoteId() << "; item.id() = " << item.id();
0098             cancelTask();
0099             return;
0100         }
0101     } else {
0102         qCritical() << "akonadi_ical_resource: itemRemoved(): Can't find incidence with instance identifier " << item.remoteId()
0103                     << "; item.id() = " << item.id();
0104     }
0105     scheduleWrite();
0106     changeProcessed();
0107 }
0108 
0109 void ICalResourceBase::retrieveItems(const Akonadi::Collection &col)
0110 {
0111     reloadFile();
0112     if (mCalendar) {
0113         doRetrieveItems(col);
0114     } else {
0115         qCritical() << "akonadi_ical_resource: retrieveItems(): mCalendar is 0!";
0116     }
0117 }
0118 
0119 bool ICalResourceBase::writeToFile(const QString &fileName)
0120 {
0121     if (!mCalendar) {
0122         qCritical() << "akonadi_ical_resource: writeToFile() mCalendar is 0!";
0123         return false;
0124     }
0125 
0126     KCalendarCore::FileStorage *fileStorage = mFileStorage.data();
0127     if (fileName != mFileStorage->fileName()) {
0128         fileStorage = new KCalendarCore::FileStorage(mCalendar, fileName, new KCalendarCore::ICalFormat());
0129     }
0130 
0131     bool success = true;
0132     if (!fileStorage->save()) {
0133         qCritical() << QStringLiteral("akonadi_ical_resource: Failed to save calendar to file ") + fileName;
0134         Q_EMIT error(i18n("Failed to save calendar file to %1", fileName));
0135         success = false;
0136     }
0137 
0138     if (fileStorage != mFileStorage.data()) {
0139         delete fileStorage;
0140     }
0141 
0142     return success;
0143 }
0144 
0145 KCalendarCore::MemoryCalendar::Ptr ICalResourceBase::calendar() const
0146 {
0147     return mCalendar;
0148 }
0149 
0150 KCalendarCore::FileStorage::Ptr ICalResourceBase::fileStorage() const
0151 {
0152     return mFileStorage;
0153 }
0154 
0155 #include "moc_icalresourcebase.cpp"