File indexing completed on 2024-04-28 03:59:07

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     bool shouldBeShownTwoActions(const QString &dontShowAgainName, KMessageBox::ButtonCode &result) override
0042     {
0043         KMessageBox::ButtonCode code = m_saved.value(dontShowAgainName, KMessageBox::ButtonCode(0));
0044         if (code == KMessageBox::PrimaryAction || code == KMessageBox::SecondaryAction) {
0045             result = code;
0046             return false;
0047         }
0048         return true;
0049     }
0050     bool shouldBeShownContinue(const QString &dontShowAgainName) override
0051     {
0052         KMessageBox::ButtonCode code = m_saved.value(dontShowAgainName, KMessageBox::PrimaryAction);
0053         return code == KMessageBox::PrimaryAction;
0054     }
0055     void saveDontShowAgainTwoActions(const QString &dontShowAgainName, KMessageBox::ButtonCode result) override
0056     {
0057         m_saved[dontShowAgainName] = result;
0058     }
0059     void saveDontShowAgainContinue(const QString &dontShowAgainName) override
0060     {
0061         m_saved[dontShowAgainName] = KMessageBox::SecondaryAction;
0062     }
0063     void enableAllMessages() override
0064     {
0065         m_saved.clear();
0066     }
0067     void enableMessage(const QString &dontShowAgainName) override
0068     {
0069         m_saved.remove(dontShowAgainName);
0070     }
0071     void setConfig(KConfig *) override
0072     {
0073         qCWarning(KWidgetsAddonsLog) << "Using QSettings based KMessageBoxDontAskAgainInterface. KMessageBox::setDontShowAgainConfig ignored";
0074     }
0075 
0076 private:
0077     QString m_filePath;
0078     QHash<QString, KMessageBox::ButtonCode> m_saved;
0079 };
0080 
0081 class KMessageBoxNotifyDummy : public KMessageBoxNotifyInterface
0082 {
0083 public:
0084     void sendNotification(QMessageBox::Icon /*notificationType*/, const QString & /*message*/, QWidget * /*parent*/) override
0085     {
0086     }
0087 };
0088 
0089 Q_GLOBAL_STATIC(KMessageBoxDontAskAgainQSettingsStorage, s_defaultDontAskAgainInterface)
0090 Q_GLOBAL_STATIC(KMessageBoxNotifyDummy, s_defaultNotifyInterface)
0091 
0092 static KMessageBoxDontAskAgainInterface *s_dontAskAgainInterface = nullptr;
0093 static KMessageBoxNotifyInterface *s_notifyInterface = nullptr;
0094 
0095 static void loadKMessageBoxPlugin()
0096 {
0097     static bool triedLoadingPlugin = false;
0098     if (!triedLoadingPlugin) {
0099         triedLoadingPlugin = true;
0100 
0101         QPluginLoader lib(QStringLiteral("kf6/FrameworkIntegrationPlugin"));
0102         QObject *rootObj = lib.instance();
0103         if (rootObj) {
0104             s_dontAskAgainInterface = rootObj->property(KMESSAGEBOXDONTASKAGAIN_PROPERTY).value<KMessageBoxDontAskAgainInterface *>();
0105             s_notifyInterface = rootObj->property(KMESSAGEBOXNOTIFY_PROPERTY).value<KMessageBoxNotifyInterface *>();
0106         }
0107     }
0108     if (!s_dontAskAgainInterface) {
0109         s_dontAskAgainInterface = s_defaultDontAskAgainInterface;
0110     }
0111     if (!s_notifyInterface) {
0112         s_notifyInterface = s_defaultNotifyInterface;
0113     }
0114 }
0115 
0116 KMessageBoxDontAskAgainInterface *dontAskAgainInterface()
0117 {
0118     if (!s_dontAskAgainInterface) {
0119         loadKMessageBoxPlugin();
0120     }
0121     return s_dontAskAgainInterface;
0122 }
0123 
0124 KMessageBoxNotifyInterface *notifyInterface()
0125 {
0126     if (!s_notifyInterface) {
0127         loadKMessageBoxPlugin();
0128     }
0129     return s_notifyInterface;
0130 }
0131 
0132 bool isNotifyInterfaceLoaded()
0133 {
0134     return s_notifyInterface;
0135 }
0136 
0137 void setDontShowAgainInterface(KMessageBoxDontAskAgainInterface *dontAskAgainInterface)
0138 {
0139     Q_ASSERT(dontAskAgainInterface != nullptr);
0140     s_dontAskAgainInterface = dontAskAgainInterface;
0141 }
0142 
0143 void setNotifyInterface(KMessageBoxNotifyInterface *notifyInterface)
0144 {
0145     Q_ASSERT(notifyInterface != nullptr);
0146     s_notifyInterface = notifyInterface;
0147 }
0148 
0149 } // KMessageBox namespace