settingschangenotifier.cpp 2.22 KB
Newer Older
1
/*
2
 * SPDX-FileCopyrightText: 2016 Daniel Vrátil <dvratil@kde.org>
3
 *
4
 * SPDX-License-Identifier: GPL-2.0-or-later
5
6
7
 *
 */

8
9
10
11
12
13
14
15
16
#include "settingschangenotifier.h"
#include "pimeventsplugin_debug.h"

#include <QCoreApplication>

#define APP_PROPERTY_NAME "PIMEventsPluginSettingsChangeNotifier"

SettingsChangeNotifier *SettingsChangeNotifier::self()
{
17
    // We can't easily use a global static (or a static member) to store the
18
19
20
21
22
23
24
25
26
    // global instance of SettingsChangeNotifier. We need the same instance to
    // be accessible by both PimEventsPlugin and PimCalendarsPlugin so I would
    // have to put this class to a .so and link it from both to get a
    // singleton that actually works across the plugins. But being the lazy
    // bastard that I am I decided to just abuse QObject::property() and the qApp
    // singleton which already comes from an .so linked by both plugins.
    //
    // Also note the cast to quintptr: we have the same problem as above with
    // SettingsChangeNotifier::staticMetaObject as each "copy" of the class
27
    // has its own instance of it, which causes  pointer comparison in
28
29
30
31
32
33
34
35
36
37
    // QMetaObject::inherits() to fail. This leads to v.isValid() being true but
    // v.value<SettingsChangeNotifier*>() returning a null pointer, because
    // the internal qobject_cast fails.
    //
    // Yeah, I could have totally spent 30 seconds of my time and write the 6
    // lines of CMake code to get my own .so and have it linked from both plugins,
    // but instead I decided to explain myself in this comment, probably because
    // short code with long comments makes it look like I know what I'm doing.
    const QVariant v = qApp->property(APP_PROPERTY_NAME);
    if (v.isValid()) {
Laurent Montel's avatar
Laurent Montel committed
38
        return reinterpret_cast<SettingsChangeNotifier *>(v.value<quintptr>());
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    }

    SettingsChangeNotifier *notifier = new SettingsChangeNotifier();
    qApp->setProperty(APP_PROPERTY_NAME, reinterpret_cast<quintptr>(notifier));
    return notifier;
}

SettingsChangeNotifier::SettingsChangeNotifier(QObject *parent)
    : QObject(parent)
{
    qCDebug(PIMEVENTSPLUGIN_LOG) << this << "created";
}

SettingsChangeNotifier::~SettingsChangeNotifier()
{
}

void SettingsChangeNotifier::notifySettingsChanged()
{
    Q_EMIT settingsChanged();
}