| 1 | /* |
| 2 | This file is part of the KDE libraries |
| 3 | SPDX-FileCopyrightText: 2012 David Faure <faure+bluesystems@kde.org> |
| 4 | SPDX-FileCopyrightText: 2013 Aurélien Gâteau <agateau@kde.org> |
| 5 | |
| 6 | SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
| 7 | */ |
| 8 | #include "kmessagebox_p.h" |
| 9 | |
| 10 | #include "loggingcategory.h" |
| 11 | |
| 12 | #include <QCoreApplication> |
| 13 | #include <QPluginLoader> |
| 14 | #include <QSettings> |
| 15 | #include <QStandardPaths> |
| 16 | |
| 17 | namespace KMessageBox |
| 18 | { |
| 19 | class KMessageBoxDontAskAgainQSettingsStorage : public KMessageBoxDontAskAgainInterface |
| 20 | { |
| 21 | public: |
| 22 | KMessageBoxDontAskAgainQSettingsStorage() |
| 23 | { |
| 24 | m_filePath = QStandardPaths::writableLocation(type: QStandardPaths::ConfigLocation) + QLatin1Char('/') + QCoreApplication::instance()->applicationName() |
| 25 | + QLatin1String(".kmessagebox" ); |
| 26 | QSettings s(m_filePath, QSettings::IniFormat); |
| 27 | const auto keys = s.allKeys(); |
| 28 | for (const QString &key : keys) { |
| 29 | m_saved.insert(key, value: static_cast<KMessageBox::ButtonCode>(s.value(key).toInt())); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | ~KMessageBoxDontAskAgainQSettingsStorage() override |
| 34 | { |
| 35 | QSettings s(m_filePath, QSettings::IniFormat); |
| 36 | for (auto it = m_saved.constBegin(); it != m_saved.constEnd(); ++it) { |
| 37 | s.setValue(key: it.key(), value: static_cast<int>(it.value())); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | bool shouldBeShownTwoActions(const QString &dontShowAgainName, KMessageBox::ButtonCode &result) override |
| 42 | { |
| 43 | KMessageBox::ButtonCode code = m_saved.value(key: dontShowAgainName, defaultValue: KMessageBox::ButtonCode(0)); |
| 44 | if (code == KMessageBox::PrimaryAction || code == KMessageBox::SecondaryAction) { |
| 45 | result = code; |
| 46 | return false; |
| 47 | } |
| 48 | return true; |
| 49 | } |
| 50 | bool shouldBeShownContinue(const QString &dontShowAgainName) override |
| 51 | { |
| 52 | KMessageBox::ButtonCode code = m_saved.value(key: dontShowAgainName, defaultValue: KMessageBox::PrimaryAction); |
| 53 | return code == KMessageBox::PrimaryAction; |
| 54 | } |
| 55 | void saveDontShowAgainTwoActions(const QString &dontShowAgainName, KMessageBox::ButtonCode result) override |
| 56 | { |
| 57 | m_saved[dontShowAgainName] = result; |
| 58 | } |
| 59 | void saveDontShowAgainContinue(const QString &dontShowAgainName) override |
| 60 | { |
| 61 | m_saved[dontShowAgainName] = KMessageBox::SecondaryAction; |
| 62 | } |
| 63 | void enableAllMessages() override |
| 64 | { |
| 65 | m_saved.clear(); |
| 66 | } |
| 67 | void enableMessage(const QString &dontShowAgainName) override |
| 68 | { |
| 69 | m_saved.remove(key: dontShowAgainName); |
| 70 | } |
| 71 | void setConfig(KConfig *) override |
| 72 | { |
| 73 | qCWarning(KWidgetsAddonsLog) << "Using QSettings based KMessageBoxDontAskAgainInterface. KMessageBox::setDontShowAgainConfig ignored" ; |
| 74 | } |
| 75 | |
| 76 | private: |
| 77 | QString m_filePath; |
| 78 | QHash<QString, KMessageBox::ButtonCode> m_saved; |
| 79 | }; |
| 80 | |
| 81 | class KMessageBoxNotifyDummy : public KMessageBoxNotifyInterface |
| 82 | { |
| 83 | public: |
| 84 | void sendNotification(QMessageBox::Icon /*notificationType*/, const QString & /*message*/, QWidget * /*parent*/) override |
| 85 | { |
| 86 | } |
| 87 | }; |
| 88 | |
| 89 | Q_GLOBAL_STATIC(KMessageBoxDontAskAgainQSettingsStorage, s_defaultDontAskAgainInterface) |
| 90 | Q_GLOBAL_STATIC(KMessageBoxNotifyDummy, s_defaultNotifyInterface) |
| 91 | |
| 92 | static KMessageBoxDontAskAgainInterface *s_dontAskAgainInterface = nullptr; |
| 93 | static KMessageBoxNotifyInterface *s_notifyInterface = nullptr; |
| 94 | |
| 95 | static void loadKMessageBoxPlugin() |
| 96 | { |
| 97 | static bool triedLoadingPlugin = false; |
| 98 | if (!triedLoadingPlugin) { |
| 99 | triedLoadingPlugin = true; |
| 100 | |
| 101 | QPluginLoader lib(QStringLiteral("kf6/FrameworkIntegrationPlugin" )); |
| 102 | QObject *rootObj = lib.instance(); |
| 103 | if (rootObj) { |
| 104 | s_dontAskAgainInterface = rootObj->property(KMESSAGEBOXDONTASKAGAIN_PROPERTY).value<KMessageBoxDontAskAgainInterface *>(); |
| 105 | s_notifyInterface = rootObj->property(KMESSAGEBOXNOTIFY_PROPERTY).value<KMessageBoxNotifyInterface *>(); |
| 106 | } |
| 107 | } |
| 108 | if (!s_dontAskAgainInterface) { |
| 109 | s_dontAskAgainInterface = s_defaultDontAskAgainInterface; |
| 110 | } |
| 111 | if (!s_notifyInterface) { |
| 112 | s_notifyInterface = s_defaultNotifyInterface; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | KMessageBoxDontAskAgainInterface *dontAskAgainInterface() |
| 117 | { |
| 118 | if (!s_dontAskAgainInterface) { |
| 119 | loadKMessageBoxPlugin(); |
| 120 | } |
| 121 | return s_dontAskAgainInterface; |
| 122 | } |
| 123 | |
| 124 | KMessageBoxNotifyInterface *notifyInterface() |
| 125 | { |
| 126 | if (!s_notifyInterface) { |
| 127 | loadKMessageBoxPlugin(); |
| 128 | } |
| 129 | return s_notifyInterface; |
| 130 | } |
| 131 | |
| 132 | bool isNotifyInterfaceLoaded() |
| 133 | { |
| 134 | return s_notifyInterface; |
| 135 | } |
| 136 | |
| 137 | void setDontShowAgainInterface(KMessageBoxDontAskAgainInterface *dontAskAgainInterface) |
| 138 | { |
| 139 | Q_ASSERT(dontAskAgainInterface != nullptr); |
| 140 | s_dontAskAgainInterface = dontAskAgainInterface; |
| 141 | } |
| 142 | |
| 143 | void setNotifyInterface(KMessageBoxNotifyInterface *notifyInterface) |
| 144 | { |
| 145 | Q_ASSERT(notifyInterface != nullptr); |
| 146 | s_notifyInterface = notifyInterface; |
| 147 | } |
| 148 | |
| 149 | } // KMessageBox namespace |
| 150 | |