| 1 | /* |
| 2 | This file is part of the KDE libraries |
| 3 | SPDX-FileCopyrightText: 2012 David Faure <[email protected]> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
| 6 | */ |
| 7 | |
| 8 | #include "frameworkintegrationplugin.h" |
| 9 | #include <KConfigGroup> |
| 10 | #include <KNotification> |
| 11 | #include <KSharedConfig> |
| 12 | |
| 13 | #include <QDebug> |
| 14 | #include <qplugin.h> |
| 15 | |
| 16 | #if KWIDGETSADDONS_BUILD_DEPRECATED_SINCE(5, 100) |
| 17 | bool KMessageBoxDontAskAgainConfigStorage::shouldBeShownYesNo(const QString &dontShowAgainName, KMessageBox::ButtonCode &result) |
| 18 | #else |
| 19 | bool KMessageBoxDontAskAgainConfigStorage::shouldBeShownTwoActions(const QString &dontShowAgainName, KMessageBox::ButtonCode &result) |
| 20 | #endif |
| 21 | { |
| 22 | KConfigGroup cg(KMessageBox_againConfig ? KMessageBox_againConfig : KSharedConfig::openConfig().data(), "Notification Messages"); |
| 23 | const QString dontAsk = cg.readEntry(dontShowAgainName, QString()).toLower(); |
| 24 | if (dontAsk == QLatin1String("yes") || dontAsk == QLatin1String("true")) { |
| 25 | result = KMessageBox::PrimaryAction; |
| 26 | return false; |
| 27 | } |
| 28 | if (dontAsk == QLatin1String("no") || dontAsk == QLatin1String("false")) { |
| 29 | result = KMessageBox::SecondaryAction; |
| 30 | return false; |
| 31 | } |
| 32 | return true; |
| 33 | } |
| 34 | |
| 35 | bool KMessageBoxDontAskAgainConfigStorage::shouldBeShownContinue(const QString &dontShowAgainName) |
| 36 | { |
| 37 | KConfigGroup cg(KMessageBox_againConfig ? KMessageBox_againConfig : KSharedConfig::openConfig().data(), "Notification Messages"); |
| 38 | return cg.readEntry(dontShowAgainName, true); |
| 39 | } |
| 40 | |
| 41 | #if KWIDGETSADDONS_BUILD_DEPRECATED_SINCE(5, 100) |
| 42 | void KMessageBoxDontAskAgainConfigStorage::saveDontShowAgainYesNo(const QString &dontShowAgainName, KMessageBox::ButtonCode result) |
| 43 | #else |
| 44 | void KMessageBoxDontAskAgainConfigStorage::saveDontShowAgainTwoActions(const QString &dontShowAgainName, KMessageBox::ButtonCode result) |
| 45 | #endif |
| 46 | { |
| 47 | KConfigGroup::WriteConfigFlags flags = KConfig::Persistent; |
| 48 | if (dontShowAgainName[0] == QLatin1Char(':')) { |
| 49 | flags |= KConfigGroup::Global; |
| 50 | } |
| 51 | KConfigGroup cg(KMessageBox_againConfig ? KMessageBox_againConfig : KSharedConfig::openConfig().data(), "Notification Messages"); |
| 52 | cg.writeEntry(dontShowAgainName, result == KMessageBox::PrimaryAction, flags); |
| 53 | cg.sync(); |
| 54 | } |
| 55 | |
| 56 | void KMessageBoxDontAskAgainConfigStorage::saveDontShowAgainContinue(const QString &dontShowAgainName) |
| 57 | { |
| 58 | KConfigGroup::WriteConfigFlags flags = KConfigGroup::Persistent; |
| 59 | if (dontShowAgainName[0] == QLatin1Char(':')) { |
| 60 | flags |= KConfigGroup::Global; |
| 61 | } |
| 62 | KConfigGroup cg(KMessageBox_againConfig ? KMessageBox_againConfig : KSharedConfig::openConfig().data(), "Notification Messages"); |
| 63 | cg.writeEntry(dontShowAgainName, false, flags); |
| 64 | cg.sync(); |
| 65 | } |
| 66 | |
| 67 | void KMessageBoxDontAskAgainConfigStorage::enableAllMessages() |
| 68 | { |
| 69 | KConfig *config = KMessageBox_againConfig ? KMessageBox_againConfig : KSharedConfig::openConfig().data(); |
| 70 | if (!config->hasGroup("Notification Messages")) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | KConfigGroup cg(config, "Notification Messages"); |
| 75 | |
| 76 | typedef QMap<QString, QString> configMap; |
| 77 | |
| 78 | const configMap map = cg.entryMap(); |
| 79 | |
| 80 | configMap::ConstIterator it; |
| 81 | for (it = map.begin(); it != map.end(); ++it) { |
| 82 | cg.deleteEntry(it.key()); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void KMessageBoxDontAskAgainConfigStorage::enableMessage(const QString &dontShowAgainName) |
| 87 | { |
| 88 | KConfig *config = KMessageBox_againConfig ? KMessageBox_againConfig : KSharedConfig::openConfig().data(); |
| 89 | if (!config->hasGroup("Notification Messages")) { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | KConfigGroup cg(config, "Notification Messages"); |
| 94 | |
| 95 | cg.deleteEntry(dontShowAgainName); |
| 96 | config->sync(); |
| 97 | } |
| 98 | |
| 99 | void KMessageBoxNotify::sendNotification(QMessageBox::Icon notificationType, const QString &message, QWidget *parent) |
| 100 | { |
| 101 | QString messageType; |
| 102 | switch (notificationType) { |
| 103 | case QMessageBox::Warning: |
| 104 | messageType = QStringLiteral("messageWarning"); |
| 105 | break; |
| 106 | case QMessageBox::Critical: |
| 107 | messageType = QStringLiteral("messageCritical"); |
| 108 | break; |
| 109 | case QMessageBox::Question: |
| 110 | messageType = QStringLiteral("messageQuestion"); |
| 111 | break; |
| 112 | default: |
| 113 | messageType = QStringLiteral("messageInformation"); |
| 114 | break; |
| 115 | } |
| 116 | |
| 117 | KNotification::event(messageType, message, QPixmap(), parent, KNotification::DefaultEvent | KNotification::CloseOnTimeout); |
| 118 | } |
| 119 | |
| 120 | KFrameworkIntegrationPlugin::KFrameworkIntegrationPlugin() |
| 121 | : QObject() |
| 122 | { |
| 123 | setProperty(KMESSAGEBOXDONTASKAGAIN_PROPERTY, QVariant::fromValue<KMessageBoxDontAskAgainInterface *>(&m_dontAskAgainConfigStorage)); |
| 124 | setProperty(KMESSAGEBOXNOTIFY_PROPERTY, QVariant::fromValue<KMessageBoxNotifyInterface *>(&m_notify)); |
| 125 | } |
| 126 | |
| 127 | void KFrameworkIntegrationPlugin::reparseConfiguration() |
| 128 | { |
| 129 | KSharedConfig::openConfig()->reparseConfiguration(); |
| 130 | } |
| 131 | |
| 132 | #include "moc_frameworkintegrationplugin.cpp" |