File indexing completed on 2025-01-05 04:49:49

0001 /*
0002  * SPDX-FileCopyrightText: 2016 Daniel Vrátil <dvratil@kde.org>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  *
0006  */
0007 
0008 #include "settingschangenotifier.h"
0009 #include "pimeventsplugin_debug.h"
0010 
0011 #include <QCoreApplication>
0012 #include <QVariant>
0013 
0014 #define APP_PROPERTY_NAME "PIMEventsPluginSettingsChangeNotifier"
0015 
0016 SettingsChangeNotifier *SettingsChangeNotifier::self()
0017 {
0018     // We can't easily use a global static (or a static member) to store the
0019     // global instance of SettingsChangeNotifier. We need the same instance to
0020     // be accessible by both PimEventsPlugin and PimCalendarsPlugin so I would
0021     // have to put this class to a .so and link it from both to get a
0022     // singleton that actually works across the plugins. But being the lazy
0023     // bastard that I am I decided to just abuse QObject::property() and the qApp
0024     // singleton which already comes from an .so linked by both plugins.
0025     //
0026     // Also note the cast to quintptr: we have the same problem as above with
0027     // SettingsChangeNotifier::staticMetaObject as each "copy" of the class
0028     // has its own instance of it, which causes  pointer comparison in
0029     // QMetaObject::inherits() to fail. This leads to v.isValid() being true but
0030     // v.value<SettingsChangeNotifier*>() returning a null pointer, because
0031     // the internal qobject_cast fails.
0032     //
0033     // Yeah, I could have totally spent 30 seconds of my time and write the 6
0034     // lines of CMake code to get my own .so and have it linked from both plugins,
0035     // but instead I decided to explain myself in this comment, probably because
0036     // short code with long comments makes it look like I know what I'm doing.
0037     const QVariant v = qApp->property(APP_PROPERTY_NAME);
0038     if (v.isValid()) {
0039         return reinterpret_cast<SettingsChangeNotifier *>(v.value<quintptr>());
0040     }
0041 
0042     auto notifier = new SettingsChangeNotifier();
0043     qApp->setProperty(APP_PROPERTY_NAME, reinterpret_cast<quintptr>(notifier));
0044     return notifier;
0045 }
0046 
0047 SettingsChangeNotifier::SettingsChangeNotifier(QObject *parent)
0048     : QObject(parent)
0049 {
0050     qCDebug(PIMEVENTSPLUGIN_LOG) << this << "created";
0051 }
0052 
0053 SettingsChangeNotifier::~SettingsChangeNotifier() = default;
0054 
0055 void SettingsChangeNotifier::notifySettingsChanged()
0056 {
0057     Q_EMIT settingsChanged();
0058 }
0059 
0060 #include "moc_settingschangenotifier.cpp"