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 "icalresource.h" 0009 0010 #include <KCalendarCore/FreeBusy> 0011 0012 #include <KLocalizedString> 0013 #include <QDebug> 0014 0015 using namespace Akonadi; 0016 using namespace KCalendarCore; 0017 0018 ICalResource::ICalResource(const QString &id) 0019 : ICalResourceBase(id) 0020 { 0021 QStringList mimeTypes; 0022 mimeTypes << QStringLiteral("text/calendar"); 0023 mimeTypes += allMimeTypes(); 0024 initialise(mimeTypes, QStringLiteral("office-calendar")); 0025 } 0026 0027 ICalResource::ICalResource(const QString &id, const QStringList &mimeTypes, const QString &icon) 0028 : ICalResourceBase(id) 0029 { 0030 initialise(mimeTypes, icon); 0031 } 0032 0033 ICalResource::~ICalResource() = default; 0034 0035 bool ICalResource::doRetrieveItems(const Akonadi::Item::List &items, const QSet<QByteArray> &parts) 0036 { 0037 Q_UNUSED(parts) 0038 0039 Akonadi::Item::List resultItems; 0040 resultItems.reserve(items.size()); 0041 0042 for (const Akonadi::Item &item : items) { 0043 const QString rid = item.remoteId(); 0044 Incidence::Ptr incidence = calendar()->instance(rid); 0045 if (!incidence) { 0046 qCritical() << "akonadi_ical_resource: Can't find incidence with uid " << rid << "; item.id() = " << item.id(); 0047 Q_EMIT error(i18n("Incidence with uid '%1' not found.", rid)); 0048 return false; 0049 } 0050 0051 Incidence::Ptr incidencePtr(incidence->clone()); 0052 0053 Item i = item; 0054 i.setMimeType(incidencePtr->mimeType()); 0055 i.setPayload<Incidence::Ptr>(incidencePtr); 0056 resultItems.append(i); 0057 } 0058 0059 itemsRetrieved(resultItems); 0060 0061 return true; 0062 } 0063 0064 void ICalResource::itemAdded(const Akonadi::Item &item, const Akonadi::Collection &) 0065 { 0066 if (!checkItemAddedChanged<Incidence::Ptr>(item, CheckForAdded)) { 0067 return; 0068 } 0069 0070 auto i = item.payload<Incidence::Ptr>(); 0071 if (!calendar()->addIncidence(Incidence::Ptr(i->clone()))) { 0072 // qCritical() << "akonadi_ical_resource: Error adding incidence with uid " 0073 // << i->uid() << "; item.id() " << item.id() << i->recurrenceId(); 0074 cancelTask(); 0075 return; 0076 } 0077 0078 Item it(item); 0079 it.setRemoteId(i->instanceIdentifier()); 0080 scheduleWrite(); 0081 changeCommitted(it); 0082 } 0083 0084 void ICalResource::itemChanged(const Akonadi::Item &item, const QSet<QByteArray> &parts) 0085 { 0086 Q_UNUSED(parts) 0087 0088 if (!checkItemAddedChanged<Incidence::Ptr>(item, CheckForChanged)) { 0089 return; 0090 } 0091 0092 auto payload = item.payload<Incidence::Ptr>(); 0093 Incidence::Ptr incidence = calendar()->instance(item.remoteId()); 0094 if (!incidence) { 0095 // not in the calendar yet, should not happen -> add it 0096 calendar()->addIncidence(Incidence::Ptr(payload->clone())); 0097 } else { 0098 // make sure any observer the resource might have installed gets properly notified 0099 incidence->startUpdates(); 0100 0101 if (incidence->type() == payload->type()) { 0102 // IncidenceBase::operator= calls virtual method assign, so it's safe. 0103 *incidence.staticCast<IncidenceBase>().data() = *payload.data(); 0104 incidence->updated(); 0105 incidence->endUpdates(); 0106 } else { 0107 incidence->endUpdates(); 0108 qWarning() << "akonadi_ical_resource: Item changed incidence type. Replacing it."; 0109 0110 calendar()->deleteIncidence(incidence); 0111 calendar()->addIncidence(Incidence::Ptr(payload->clone())); 0112 } 0113 } 0114 scheduleWrite(); 0115 changeCommitted(item); 0116 } 0117 0118 void ICalResource::doRetrieveItems(const Akonadi::Collection &col) 0119 { 0120 Q_UNUSED(col) 0121 const Incidence::List incidences = calendar()->incidences(); 0122 Item::List items; 0123 items.reserve(incidences.count()); 0124 for (const Incidence::Ptr &incidence : incidences) { 0125 Item item(incidence->mimeType()); 0126 item.setRemoteId(incidence->instanceIdentifier()); 0127 item.setPayload(Incidence::Ptr(incidence->clone())); 0128 items << item; 0129 } 0130 itemsRetrieved(items); 0131 } 0132 0133 QStringList ICalResource::allMimeTypes() const 0134 { 0135 return QStringList() << KCalendarCore::Event::eventMimeType() << KCalendarCore::Todo::todoMimeType() << KCalendarCore::Journal::journalMimeType() 0136 << KCalendarCore::FreeBusy::freeBusyMimeType(); 0137 } 0138 0139 QString ICalResource::mimeType(const IncidenceBase::Ptr &incidence) const 0140 { 0141 return incidence->mimeType(); 0142 } 0143 0144 #include "moc_icalresource.cpp"