File indexing completed on 2024-11-10 04:40:42

0001 /*
0002     SPDX-FileCopyrightText: 2006-2007 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "monitor.h"
0008 #include "monitor_p.h"
0009 
0010 #include "changemediator_p.h"
0011 #include "collectionfetchscope.h"
0012 #include "session.h"
0013 
0014 #include "shared/akranges.h"
0015 
0016 #include <QMetaMethod>
0017 
0018 using namespace Akonadi;
0019 using namespace AkRanges;
0020 
0021 Monitor::Monitor(QObject *parent)
0022     : QObject(parent)
0023     , d_ptr(new MonitorPrivate(nullptr, this))
0024 {
0025     d_ptr->init();
0026     d_ptr->connectToNotificationManager();
0027 
0028     ChangeMediator::registerMonitor(this);
0029 }
0030 
0031 /// @cond PRIVATE
0032 Monitor::Monitor(MonitorPrivate *d, QObject *parent)
0033     : QObject(parent)
0034     , d_ptr(d)
0035 {
0036     d_ptr->init();
0037     d_ptr->connectToNotificationManager();
0038 
0039     ChangeMediator::registerMonitor(this);
0040 }
0041 /// @endcond
0042 
0043 Monitor::~Monitor()
0044 {
0045     ChangeMediator::unregisterMonitor(this);
0046 }
0047 
0048 void Monitor::setCollectionMonitored(const Collection &collection, bool monitored)
0049 {
0050     Q_D(Monitor);
0051     if (!d->collections.contains(collection) && monitored) {
0052         d->collections << collection;
0053         d->pendingModification.startMonitoringCollection(collection.id());
0054         d->scheduleSubscriptionUpdate();
0055     } else if (!monitored) {
0056         if (d->collections.removeAll(collection)) {
0057             d->pendingModification.stopMonitoringCollection(collection.id());
0058             d->scheduleSubscriptionUpdate();
0059         }
0060     }
0061 
0062     Q_EMIT collectionMonitored(collection, monitored); // NOLINT(readability-misleading-indentation): false positive
0063 }
0064 
0065 void Monitor::setItemMonitored(const Item &item, bool monitored)
0066 {
0067     Q_D(Monitor);
0068     if (!d->items.contains(item.id()) && monitored) {
0069         d->items.insert(item.id());
0070         d->pendingModification.startMonitoringItem(item.id());
0071         d->scheduleSubscriptionUpdate();
0072     } else if (!monitored) {
0073         if (d->items.remove(item.id())) {
0074             d->pendingModification.stopMonitoringItem(item.id());
0075             d->scheduleSubscriptionUpdate();
0076         }
0077     }
0078 
0079     Q_EMIT itemMonitored(item, monitored); // NOLINT(readability-misleading-indentation): false positive
0080 }
0081 
0082 void Monitor::setResourceMonitored(const QByteArray &resource, bool monitored)
0083 {
0084     Q_D(Monitor);
0085     if (!d->resources.contains(resource) && monitored) {
0086         d->resources.insert(resource);
0087         d->pendingModification.startMonitoringResource(resource);
0088         d->scheduleSubscriptionUpdate();
0089     } else if (!monitored) {
0090         if (d->resources.remove(resource)) {
0091             d->pendingModification.stopMonitoringResource(resource);
0092             d->scheduleSubscriptionUpdate();
0093         }
0094     }
0095 
0096     Q_EMIT resourceMonitored(resource, monitored); // NOLINT(readability-misleading-indentation): false positive
0097 }
0098 
0099 void Monitor::setMimeTypeMonitored(const QString &mimetype, bool monitored)
0100 {
0101     Q_D(Monitor);
0102     if (!d->mimetypes.contains(mimetype) && monitored) {
0103         d->mimetypes.insert(mimetype);
0104         d->pendingModification.startMonitoringMimeType(mimetype);
0105         d->scheduleSubscriptionUpdate();
0106     } else if (!monitored) {
0107         if (d->mimetypes.remove(mimetype)) {
0108             d->pendingModification.stopMonitoringMimeType(mimetype);
0109             d->scheduleSubscriptionUpdate();
0110         }
0111     }
0112 
0113     Q_EMIT mimeTypeMonitored(mimetype, monitored); // NOLINT(readability-misleading-indentation): false positive
0114 }
0115 
0116 void Monitor::setTagMonitored(const Akonadi::Tag &tag, bool monitored)
0117 {
0118     Q_D(Monitor);
0119     if (!d->tags.contains(tag.id()) && monitored) {
0120         d->tags.insert(tag.id());
0121         d->pendingModification.startMonitoringTag(tag.id());
0122         d->scheduleSubscriptionUpdate();
0123     } else if (!monitored) {
0124         if (d->tags.remove(tag.id())) {
0125             d->pendingModification.stopMonitoringTag(tag.id());
0126             d->scheduleSubscriptionUpdate();
0127         }
0128     }
0129 
0130     Q_EMIT tagMonitored(tag, monitored); // NOLINT(readability-misleading-indentation): false positive
0131 }
0132 
0133 void Monitor::setTypeMonitored(Monitor::Type type, bool monitored)
0134 {
0135     Q_D(Monitor);
0136     if (!d->types.contains(type) && monitored) {
0137         d->types.insert(type);
0138         d->pendingModification.startMonitoringType(MonitorPrivate::monitorTypeToProtocol(type));
0139         d->scheduleSubscriptionUpdate();
0140     } else if (!monitored) {
0141         if (d->types.remove(type)) {
0142             d->pendingModification.stopMonitoringType(MonitorPrivate::monitorTypeToProtocol(type));
0143             d->scheduleSubscriptionUpdate();
0144         }
0145     }
0146 
0147     Q_EMIT typeMonitored(type, monitored); // NOLINT(readability-misleading-indentation): false positive
0148 }
0149 
0150 void Akonadi::Monitor::setAllMonitored(bool monitored)
0151 {
0152     Q_D(Monitor);
0153     if (d->monitorAll == monitored) {
0154         return;
0155     }
0156 
0157     d->monitorAll = monitored;
0158 
0159     d->pendingModification.setAllMonitored(monitored);
0160     d->scheduleSubscriptionUpdate();
0161 
0162     Q_EMIT allMonitored(monitored);
0163 }
0164 
0165 void Monitor::setExclusive(bool exclusive)
0166 {
0167     Q_D(Monitor);
0168     d->exclusive = exclusive;
0169     d->pendingModification.setIsExclusive(exclusive);
0170     d->scheduleSubscriptionUpdate();
0171 }
0172 
0173 bool Monitor::exclusive() const
0174 {
0175     Q_D(const Monitor);
0176     return d->exclusive;
0177 }
0178 
0179 void Monitor::ignoreSession(Session *session)
0180 {
0181     Q_D(Monitor);
0182 
0183     if (!d->sessions.contains(session->sessionId())) {
0184         d->sessions << session->sessionId();
0185         connect(session, &Session::destroyed, this, [d](QObject *o) {
0186             d->slotSessionDestroyed(o);
0187         });
0188         d->pendingModification.startIgnoringSession(session->sessionId());
0189         d->scheduleSubscriptionUpdate();
0190     }
0191 }
0192 
0193 void Monitor::fetchCollection(bool enable)
0194 {
0195     Q_D(Monitor);
0196     d->fetchCollection = enable;
0197 }
0198 
0199 void Monitor::fetchCollectionStatistics(bool enable)
0200 {
0201     Q_D(Monitor);
0202     d->fetchCollectionStatistics = enable;
0203 }
0204 
0205 void Monitor::setItemFetchScope(const ItemFetchScope &fetchScope)
0206 {
0207     Q_D(Monitor);
0208     d->mItemFetchScope = fetchScope;
0209     d->pendingModificationChanges |= Protocol::ModifySubscriptionCommand::ItemFetchScope;
0210     d->scheduleSubscriptionUpdate();
0211 }
0212 
0213 ItemFetchScope &Monitor::itemFetchScope()
0214 {
0215     Q_D(Monitor);
0216     d->pendingModificationChanges |= Protocol::ModifySubscriptionCommand::ItemFetchScope;
0217     d->scheduleSubscriptionUpdate();
0218     return d->mItemFetchScope;
0219 }
0220 
0221 void Monitor::fetchChangedOnly(bool enable)
0222 {
0223     Q_D(Monitor);
0224     d->mFetchChangedOnly = enable;
0225 }
0226 
0227 void Monitor::setCollectionFetchScope(const CollectionFetchScope &fetchScope)
0228 {
0229     Q_D(Monitor);
0230     d->mCollectionFetchScope = fetchScope;
0231     d->pendingModificationChanges |= Protocol::ModifySubscriptionCommand::CollectionFetchScope;
0232     d->scheduleSubscriptionUpdate();
0233 }
0234 
0235 CollectionFetchScope &Monitor::collectionFetchScope()
0236 {
0237     Q_D(Monitor);
0238     d->pendingModificationChanges |= Protocol::ModifySubscriptionCommand::CollectionFetchScope;
0239     d->scheduleSubscriptionUpdate();
0240     return d->mCollectionFetchScope;
0241 }
0242 
0243 void Monitor::setTagFetchScope(const TagFetchScope &fetchScope)
0244 {
0245     Q_D(Monitor);
0246     d->mTagFetchScope = fetchScope;
0247     d->pendingModificationChanges |= Protocol::ModifySubscriptionCommand::TagFetchScope;
0248     d->scheduleSubscriptionUpdate();
0249 }
0250 
0251 TagFetchScope &Monitor::tagFetchScope()
0252 {
0253     Q_D(Monitor);
0254     d->pendingModificationChanges |= Protocol::ModifySubscriptionCommand::TagFetchScope;
0255     d->scheduleSubscriptionUpdate();
0256     return d->mTagFetchScope;
0257 }
0258 
0259 Akonadi::Collection::List Monitor::collectionsMonitored() const
0260 {
0261     Q_D(const Monitor);
0262     return d->collections;
0263 }
0264 
0265 QList<Item::Id> Monitor::itemsMonitoredEx() const
0266 {
0267     Q_D(const Monitor);
0268     QList<Item::Id> result;
0269     result.reserve(d->items.size());
0270     std::copy(d->items.begin(), d->items.end(), std::back_inserter(result));
0271     return result;
0272 }
0273 
0274 int Monitor::numItemsMonitored() const
0275 {
0276     Q_D(const Monitor);
0277     return d->items.size();
0278 }
0279 
0280 QList<Tag::Id> Monitor::tagsMonitored() const
0281 {
0282     Q_D(const Monitor);
0283     QList<Tag::Id> result;
0284     result.reserve(d->tags.size());
0285     std::copy(d->tags.begin(), d->tags.end(), std::back_inserter(result));
0286     return result;
0287 }
0288 
0289 QList<Monitor::Type> Monitor::typesMonitored() const
0290 {
0291     Q_D(const Monitor);
0292     QList<Monitor::Type> result;
0293     result.reserve(d->types.size());
0294     std::copy(d->types.begin(), d->types.end(), std::back_inserter(result));
0295     return result;
0296 }
0297 
0298 QStringList Monitor::mimeTypesMonitored() const
0299 {
0300     Q_D(const Monitor);
0301     return d->mimetypes | Actions::toQList;
0302 }
0303 
0304 int Monitor::numMimeTypesMonitored() const
0305 {
0306     Q_D(const Monitor);
0307     return d->mimetypes.count();
0308 }
0309 
0310 QList<QByteArray> Monitor::resourcesMonitored() const
0311 {
0312     Q_D(const Monitor);
0313     return d->resources | Actions::toQList;
0314 }
0315 
0316 int Monitor::numResourcesMonitored() const
0317 {
0318     Q_D(const Monitor);
0319     return d->resources.count();
0320 }
0321 
0322 bool Monitor::isAllMonitored() const
0323 {
0324     Q_D(const Monitor);
0325     return d->monitorAll;
0326 }
0327 
0328 void Monitor::setSession(Akonadi::Session *session)
0329 {
0330     Q_D(Monitor);
0331     if (session == d->session) {
0332         return;
0333     }
0334 
0335     if (!session) {
0336         d->session = Session::defaultSession();
0337     } else {
0338         d->session = session;
0339     }
0340 
0341     d->itemCache->setSession(d->session);
0342     d->collectionCache->setSession(d->session);
0343     d->tagCache->setSession(d->session);
0344 
0345     // Reconnect with a new session
0346     d->connectToNotificationManager();
0347 }
0348 
0349 Session *Monitor::session() const
0350 {
0351     Q_D(const Monitor);
0352     return d->session;
0353 }
0354 
0355 void Monitor::setCollectionMoveTranslationEnabled(bool enabled)
0356 {
0357     Q_D(Monitor);
0358     d->collectionMoveTranslationEnabled = enabled;
0359 }
0360 
0361 void Monitor::connectNotify(const QMetaMethod &signal)
0362 {
0363     Q_D(Monitor);
0364     d->updateListeners(signal, MonitorPrivate::AddListener);
0365 }
0366 
0367 void Monitor::disconnectNotify(const QMetaMethod &signal)
0368 {
0369     Q_D(Monitor);
0370     d->updateListeners(signal, MonitorPrivate::RemoveListener);
0371 }
0372 
0373 #include "moc_monitor.cpp"