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

0001 /*
0002     SPDX-FileCopyrightText: 2011 Tobias Koenig <tokoe@kde.org>
0003     SPDX-FileCopyrightText: 2011 Stephen Kelly <steveire@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "changemediator_p.h"
0009 
0010 #include <QCoreApplication>
0011 
0012 #include "collection.h"
0013 #include "item.h"
0014 
0015 using namespace Akonadi;
0016 
0017 class GlobalChangeMediator : public ChangeMediator
0018 {
0019     Q_OBJECT
0020 };
0021 
0022 Q_GLOBAL_STATIC(GlobalChangeMediator, s_globalChangeMediator) // NOLINT(readability-redundant-member-init)
0023 
0024 ChangeMediator *ChangeMediator::instance()
0025 {
0026     if (s_globalChangeMediator.isDestroyed()) {
0027         return nullptr;
0028     } else {
0029         return s_globalChangeMediator;
0030     }
0031 }
0032 
0033 ChangeMediator::ChangeMediator(QObject *parent)
0034     : QObject(parent)
0035 {
0036     if (auto app = QCoreApplication::instance(); app != nullptr) {
0037         this->moveToThread(app->thread());
0038     }
0039 }
0040 
0041 /* static */
0042 void ChangeMediator::registerMonitor(QObject *monitor)
0043 {
0044     QMetaObject::invokeMethod(instance(), [monitor]() {
0045         instance()->m_monitors.push_back(monitor);
0046     });
0047 }
0048 
0049 /* static */
0050 void ChangeMediator::unregisterMonitor(QObject *monitor)
0051 {
0052     QMetaObject::invokeMethod(instance(), [monitor]() {
0053         instance()->m_monitors.removeAll(monitor);
0054     });
0055 }
0056 
0057 /* static */
0058 void ChangeMediator::invalidateCollection(const Akonadi::Collection &collection)
0059 {
0060     QMetaObject::invokeMethod(instance(), [colId = collection.id()]() {
0061         for (auto monitor : std::as_const(instance()->m_monitors)) {
0062             const bool ok = QMetaObject::invokeMethod(monitor, "invalidateCollectionCache", Q_ARG(qint64, colId));
0063             Q_ASSERT(ok);
0064             Q_UNUSED(ok)
0065         }
0066     });
0067 }
0068 
0069 /* static */
0070 void ChangeMediator::invalidateItem(const Akonadi::Item &item)
0071 {
0072     QMetaObject::invokeMethod(instance(), [itemId = item.id()]() {
0073         for (auto monitor : std::as_const(instance()->m_monitors)) {
0074             const bool ok = QMetaObject::invokeMethod(monitor, "invalidateItemCache", Q_ARG(qint64, itemId));
0075             Q_ASSERT(ok);
0076             Q_UNUSED(ok)
0077         }
0078     });
0079 }
0080 
0081 /* static */
0082 void ChangeMediator::invalidateTag(const Tag &tag)
0083 {
0084     QMetaObject::invokeMethod(instance(), [tagId = tag.id()]() {
0085         for (auto monitor : std::as_const(instance()->m_monitors)) {
0086             const bool ok = QMetaObject::invokeMethod(monitor, "invalidateTagCache", Q_ARG(qint64, tagId));
0087             Q_ASSERT(ok);
0088             Q_UNUSED(ok)
0089         }
0090     });
0091 }
0092 
0093 #include "changemediator_p.moc"
0094 
0095 #include "moc_changemediator_p.cpp"