File indexing completed on 2024-05-26 07:42:21

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2012 David Faure <faure+bluesystems@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #include "frameworkintegrationplugin.h"
0009 #include <KConfigGroup>
0010 #include <KNotification>
0011 #include <KSharedConfig>
0012 
0013 #include <QDebug>
0014 #include <qplugin.h>
0015 
0016 bool KMessageBoxDontAskAgainConfigStorage::shouldBeShownTwoActions(const QString &dontShowAgainName, KMessageBox::ButtonCode &result)
0017 {
0018     KConfigGroup cg(KMessageBox_againConfig ? KMessageBox_againConfig : KSharedConfig::openConfig().data(), QStringLiteral("Notification Messages"));
0019     const QString dontAsk = cg.readEntry(dontShowAgainName, QString()).toLower();
0020     if (dontAsk == QLatin1String("yes") || dontAsk == QLatin1String("true")) {
0021         result = KMessageBox::PrimaryAction;
0022         return false;
0023     }
0024     if (dontAsk == QLatin1String("no") || dontAsk == QLatin1String("false")) {
0025         result = KMessageBox::SecondaryAction;
0026         return false;
0027     }
0028     return true;
0029 }
0030 
0031 bool KMessageBoxDontAskAgainConfigStorage::shouldBeShownContinue(const QString &dontShowAgainName)
0032 {
0033     KConfigGroup cg(KMessageBox_againConfig ? KMessageBox_againConfig : KSharedConfig::openConfig().data(), QStringLiteral("Notification Messages"));
0034     return cg.readEntry(dontShowAgainName, true);
0035 }
0036 
0037 void KMessageBoxDontAskAgainConfigStorage::saveDontShowAgainTwoActions(const QString &dontShowAgainName, KMessageBox::ButtonCode result)
0038 {
0039     KConfigGroup::WriteConfigFlags flags = KConfig::Persistent;
0040     if (dontShowAgainName[0] == QLatin1Char(':')) {
0041         flags |= KConfigGroup::Global;
0042     }
0043     KConfigGroup cg(KMessageBox_againConfig ? KMessageBox_againConfig : KSharedConfig::openConfig().data(), QStringLiteral("Notification Messages"));
0044     cg.writeEntry(dontShowAgainName, result == KMessageBox::PrimaryAction, flags);
0045     cg.sync();
0046 }
0047 
0048 void KMessageBoxDontAskAgainConfigStorage::saveDontShowAgainContinue(const QString &dontShowAgainName)
0049 {
0050     KConfigGroup::WriteConfigFlags flags = KConfigGroup::Persistent;
0051     if (dontShowAgainName[0] == QLatin1Char(':')) {
0052         flags |= KConfigGroup::Global;
0053     }
0054     KConfigGroup cg(KMessageBox_againConfig ? KMessageBox_againConfig : KSharedConfig::openConfig().data(), QStringLiteral("Notification Messages"));
0055     cg.writeEntry(dontShowAgainName, false, flags);
0056     cg.sync();
0057 }
0058 
0059 void KMessageBoxDontAskAgainConfigStorage::enableAllMessages()
0060 {
0061     KConfig *config = KMessageBox_againConfig ? KMessageBox_againConfig : KSharedConfig::openConfig().data();
0062     if (!config->hasGroup(QStringLiteral("Notification Messages"))) {
0063         return;
0064     }
0065 
0066     KConfigGroup cg(config, QStringLiteral("Notification Messages"));
0067 
0068     typedef QMap<QString, QString> configMap;
0069 
0070     const configMap map = cg.entryMap();
0071 
0072     configMap::ConstIterator it;
0073     for (it = map.begin(); it != map.end(); ++it) {
0074         cg.deleteEntry(it.key());
0075     }
0076 }
0077 
0078 void KMessageBoxDontAskAgainConfigStorage::enableMessage(const QString &dontShowAgainName)
0079 {
0080     KConfig *config = KMessageBox_againConfig ? KMessageBox_againConfig : KSharedConfig::openConfig().data();
0081     if (!config->hasGroup(QStringLiteral("Notification Messages"))) {
0082         return;
0083     }
0084 
0085     KConfigGroup cg(config, QStringLiteral("Notification Messages"));
0086 
0087     cg.deleteEntry(dontShowAgainName);
0088     config->sync();
0089 }
0090 
0091 void KMessageBoxNotify::sendNotification(QMessageBox::Icon notificationType, const QString &message, QWidget * /*parent*/)
0092 {
0093     QString messageType;
0094     switch (notificationType) {
0095     case QMessageBox::Warning:
0096         messageType = QStringLiteral("messageWarning");
0097         break;
0098     case QMessageBox::Critical:
0099         messageType = QStringLiteral("messageCritical");
0100         break;
0101     case QMessageBox::Question:
0102         messageType = QStringLiteral("messageQuestion");
0103         break;
0104     default:
0105         messageType = QStringLiteral("messageInformation");
0106         break;
0107     }
0108 
0109     KNotification::event(messageType, message, QPixmap(), KNotification::DefaultEvent | KNotification::CloseOnTimeout);
0110 }
0111 
0112 KFrameworkIntegrationPlugin::KFrameworkIntegrationPlugin()
0113     : QObject()
0114 {
0115     setProperty(KMESSAGEBOXDONTASKAGAIN_PROPERTY, QVariant::fromValue<KMessageBoxDontAskAgainInterface *>(&m_dontAskAgainConfigStorage));
0116     setProperty(KMESSAGEBOXNOTIFY_PROPERTY, QVariant::fromValue<KMessageBoxNotifyInterface *>(&m_notify));
0117 }
0118 
0119 void KFrameworkIntegrationPlugin::reparseConfiguration()
0120 {
0121     KSharedConfig::openConfig()->reparseConfiguration();
0122 }
0123 
0124 #include "moc_frameworkintegrationplugin.cpp"