File indexing completed on 2024-04-28 15:32:05

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2012 David Faure <faure+bluesystems@kde.org>
0004     SPDX-FileCopyrightText: 2013 Aurélien Gâteau <agateau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 #include "kmessagebox_p.h"
0009 
0010 #include "loggingcategory.h"
0011 
0012 #include <QCoreApplication>
0013 #include <QPluginLoader>
0014 #include <QSettings>
0015 #include <QStandardPaths>
0016 
0017 namespace KMessageBox
0018 {
0019 class KMessageBoxDontAskAgainQSettingsStorage : public KMessageBoxDontAskAgainInterface
0020 {
0021 public:
0022     KMessageBoxDontAskAgainQSettingsStorage()
0023     {
0024         m_filePath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + QCoreApplication::instance()->applicationName()
0025             + QLatin1String(".kmessagebox");
0026         QSettings s(m_filePath, QSettings::IniFormat);
0027         const auto keys = s.allKeys();
0028         for (const QString &key : keys) {
0029             m_saved.insert(key, static_cast<KMessageBox::ButtonCode>(s.value(key).toInt()));
0030         }
0031     }
0032 
0033     ~KMessageBoxDontAskAgainQSettingsStorage() override
0034     {
0035         QSettings s(m_filePath, QSettings::IniFormat);
0036         for (auto it = m_saved.constBegin(); it != m_saved.constEnd(); ++it) {
0037             s.setValue(it.key(), static_cast<int>(it.value()));
0038         }
0039     }
0040 
0041 #if KWIDGETSADDONS_BUILD_DEPRECATED_SINCE(5, 100)
0042     bool shouldBeShownYesNo(const QString &dontShowAgainName, KMessageBox::ButtonCode &result) override
0043 #else
0044     bool shouldBeShownTwoActions(const QString &dontShowAgainName, KMessageBox::ButtonCode &result) override
0045 #endif
0046     {
0047         KMessageBox::ButtonCode code = m_saved.value(dontShowAgainName, KMessageBox::ButtonCode(0));
0048         if (code == KMessageBox::PrimaryAction || code == KMessageBox::SecondaryAction) {
0049             result = code;
0050             return false;
0051         }
0052         return true;
0053     }
0054     bool shouldBeShownContinue(const QString &dontShowAgainName) override
0055     {
0056         KMessageBox::ButtonCode code = m_saved.value(dontShowAgainName, KMessageBox::PrimaryAction);
0057         return code == KMessageBox::PrimaryAction;
0058     }
0059 #if KWIDGETSADDONS_BUILD_DEPRECATED_SINCE(5, 100)
0060     void saveDontShowAgainYesNo(const QString &dontShowAgainName, KMessageBox::ButtonCode result) override
0061 #else
0062     void saveDontShowAgainTwoActions(const QString &dontShowAgainName, KMessageBox::ButtonCode result) override
0063 #endif
0064     {
0065         m_saved[dontShowAgainName] = result;
0066     }
0067     void saveDontShowAgainContinue(const QString &dontShowAgainName) override
0068     {
0069         m_saved[dontShowAgainName] = KMessageBox::SecondaryAction;
0070     }
0071     void enableAllMessages() override
0072     {
0073         m_saved.clear();
0074     }
0075     void enableMessage(const QString &dontShowAgainName) override
0076     {
0077         m_saved.remove(dontShowAgainName);
0078     }
0079     void setConfig(KConfig *) override
0080     {
0081         qCWarning(KWidgetsAddonsLog) << "Using QSettings based KMessageBoxDontAskAgainInterface. KMessageBox::setDontShowAgainConfig ignored";
0082     }
0083 
0084 private:
0085     QString m_filePath;
0086     QHash<QString, KMessageBox::ButtonCode> m_saved;
0087 };
0088 
0089 class KMessageBoxNotifyDummy : public KMessageBoxNotifyInterface
0090 {
0091 public:
0092     void sendNotification(QMessageBox::Icon /*notificationType*/, const QString & /*message*/, QWidget * /*parent*/) override
0093     {
0094     }
0095 };
0096 
0097 Q_GLOBAL_STATIC(KMessageBoxDontAskAgainQSettingsStorage, s_defaultDontAskAgainInterface)
0098 Q_GLOBAL_STATIC(KMessageBoxNotifyDummy, s_defaultNotifyInterface)
0099 
0100 static KMessageBoxDontAskAgainInterface *s_dontAskAgainInterface = nullptr;
0101 static KMessageBoxNotifyInterface *s_notifyInterface = nullptr;
0102 
0103 static void loadKMessageBoxPlugin()
0104 {
0105     static bool triedLoadingPlugin = false;
0106     if (!triedLoadingPlugin) {
0107         triedLoadingPlugin = true;
0108 
0109         QPluginLoader lib(QStringLiteral("kf" QT_STRINGIFY(QT_VERSION_MAJOR) "/FrameworkIntegrationPlugin"));
0110         QObject *rootObj = lib.instance();
0111         if (rootObj) {
0112             s_dontAskAgainInterface = rootObj->property(KMESSAGEBOXDONTASKAGAIN_PROPERTY).value<KMessageBoxDontAskAgainInterface *>();
0113             s_notifyInterface = rootObj->property(KMESSAGEBOXNOTIFY_PROPERTY).value<KMessageBoxNotifyInterface *>();
0114         }
0115     }
0116     if (!s_dontAskAgainInterface) {
0117         s_dontAskAgainInterface = s_defaultDontAskAgainInterface;
0118     }
0119     if (!s_notifyInterface) {
0120         s_notifyInterface = s_defaultNotifyInterface;
0121     }
0122 }
0123 
0124 KMessageBoxDontAskAgainInterface *dontAskAgainInterface()
0125 {
0126     if (!s_dontAskAgainInterface) {
0127         loadKMessageBoxPlugin();
0128     }
0129     return s_dontAskAgainInterface;
0130 }
0131 
0132 KMessageBoxNotifyInterface *notifyInterface()
0133 {
0134     if (!s_notifyInterface) {
0135         loadKMessageBoxPlugin();
0136     }
0137     return s_notifyInterface;
0138 }
0139 
0140 bool isNotifyInterfaceLoaded()
0141 {
0142     return s_notifyInterface;
0143 }
0144 
0145 void setDontShowAgainInterface(KMessageBoxDontAskAgainInterface *dontAskAgainInterface)
0146 {
0147     Q_ASSERT(dontAskAgainInterface != nullptr);
0148     s_dontAskAgainInterface = dontAskAgainInterface;
0149 }
0150 
0151 void setNotifyInterface(KMessageBoxNotifyInterface *notifyInterface)
0152 {
0153     Q_ASSERT(notifyInterface != nullptr);
0154     s_notifyInterface = notifyInterface;
0155 }
0156 
0157 } // KMessageBox namespace