File indexing completed on 2024-11-24 04:42:25
0001 /* 0002 * messagebox.cpp - enhanced KMessageBox class 0003 * Program: kalarm 0004 * SPDX-FileCopyrightText: 2004, 2005, 2007, 2008, 2011, 2014 David Jarvie <djarvie@kde.org> 0005 * 0006 * SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 #include "messagebox.h" 0010 0011 #include <KConfigGroup> 0012 #include <KSharedConfig> 0013 0014 0015 QMap<QString, KMessageBox::ButtonCode> KAMessageBox::mContinueDefaults; 0016 0017 const KMessageBox::Options KAMessageBox::NoAppModal = KMessageBox::Options(KMessageBox::Notify | KMessageBox::WindowModal); 0018 0019 /****************************************************************************** 0020 * Set the default button for continue/cancel message boxes with the specified 0021 * 'dontAskAgainName'. 0022 */ 0023 void KAMessageBox::setContinueDefault(const QString& dontAskAgainName, KMessageBox::ButtonCode defaultButton) 0024 { 0025 mContinueDefaults[dontAskAgainName] = (defaultButton == KMessageBox::Cancel ? KMessageBox::Cancel : KMessageBox::Continue); 0026 } 0027 0028 /****************************************************************************** 0029 * Get the default button for continue/cancel message boxes with the specified 0030 * 'dontAskAgainName'. 0031 */ 0032 KMessageBox::ButtonCode KAMessageBox::getContinueDefault(const QString& dontAskAgainName) 0033 { 0034 KMessageBox::ButtonCode defaultButton = KMessageBox::Continue; 0035 if (!dontAskAgainName.isEmpty()) 0036 { 0037 QMap<QString, KMessageBox::ButtonCode>::ConstIterator it = mContinueDefaults.constFind(dontAskAgainName); 0038 if (it != mContinueDefaults.constEnd()) 0039 defaultButton = it.value(); 0040 } 0041 return defaultButton; 0042 } 0043 0044 /****************************************************************************** 0045 * If there is no current setting for whether a non-yes/no message box should be 0046 * shown, set it to 'defaultShow'. 0047 * If a continue/cancel message box has Cancel as the default button, either 0048 * setContinueDefault() or warningContinueCancel() must have been called 0049 * previously to set this for this 'dontShowAgainName' value. 0050 * Reply = true if 'defaultShow' was written. 0051 */ 0052 bool KAMessageBox::setDefaultShouldBeShownContinue(const QString& dontShowAgainName, bool defaultShow) 0053 { 0054 if (dontShowAgainName.isEmpty()) 0055 return false; 0056 // First check whether there is an existing setting 0057 KConfigGroup config(KSharedConfig::openConfig(), QStringLiteral("Notification Messages")); 0058 if (config.hasKey(dontShowAgainName)) 0059 return false; 0060 0061 // There is no current setting, so write one 0062 saveDontShowAgainContinue(dontShowAgainName, !defaultShow); 0063 return true; 0064 } 0065 0066 /****************************************************************************** 0067 * Return whether a non-yes/no message box should be shown. 0068 * If the message box has Cancel as the default button, either setContinueDefault() 0069 * or warningContinueCancel() must have been called previously to set this for this 0070 * 'dontShowAgainName' value. 0071 */ 0072 bool KAMessageBox::shouldBeShownContinue(const QString& dontShowAgainName) 0073 { 0074 if (getContinueDefault(dontShowAgainName) != KMessageBox::Cancel) 0075 return KMessageBox::shouldBeShownContinue(dontShowAgainName); 0076 // Cancel is the default button, so we have to use a yes/no message box 0077 KMessageBox::ButtonCode b; 0078 return shouldBeShownTwoActions(dontShowAgainName, b); 0079 } 0080 0081 0082 /****************************************************************************** 0083 * Save whether the yes/no message box should not be shown again. 0084 * If 'dontShow' is true, the message box will be suppressed and it will return 0085 * 'result'. 0086 */ 0087 void KAMessageBox::saveDontShowAgainYesNo(const QString& dontShowAgainName, bool dontShow, KMessageBox::ButtonCode result) 0088 { 0089 saveDontShowAgain(dontShowAgainName, true, dontShow, (result == KMessageBox::ButtonCode::PrimaryAction ? "yes" : "no")); 0090 } 0091 0092 /****************************************************************************** 0093 * Save whether a non-yes/no message box should not be shown again. 0094 * If 'dontShow' is true, the message box will be suppressed and it will return 0095 * Continue. 0096 * If the message box has Cancel as the default button, either setContinueDefault() 0097 * or warningContinueCancel() must have been called previously to set this for this 0098 * 'dontShowAgainName' value. 0099 */ 0100 void KAMessageBox::saveDontShowAgainContinue(const QString& dontShowAgainName, bool dontShow) 0101 { 0102 if (getContinueDefault(dontShowAgainName) == KMessageBox::Cancel) 0103 saveDontShowAgainYesNo(dontShowAgainName, dontShow, KMessageBox::ButtonCode::PrimaryAction); 0104 else 0105 saveDontShowAgain(dontShowAgainName, false, dontShow); 0106 } 0107 0108 /****************************************************************************** 0109 * Save whether the message box should not be shown again. 0110 */ 0111 void KAMessageBox::saveDontShowAgain(const QString& dontShowAgainName, bool yesno, bool dontShow, const char* yesnoResult) 0112 { 0113 if (dontShowAgainName.isEmpty()) 0114 return; 0115 KConfigGroup config(KSharedConfig::openConfig(), QStringLiteral("Notification Messages")); 0116 KConfig::WriteConfigFlags flags = (dontShowAgainName[0] == QLatin1Char(':')) ? KConfig::Global | KConfig::Persistent : KConfig::Persistent; 0117 if (yesno) 0118 config.writeEntry(dontShowAgainName, QString::fromLatin1(dontShow ? yesnoResult : ""), flags); 0119 else 0120 config.writeEntry(dontShowAgainName, !dontShow, flags); 0121 config.sync(); 0122 } 0123 0124 // vim: et sw=4: