messagebox.cpp
00001 /* 00002 * messagebox.cpp - enhanced KMessageBox class 00003 * Program: kalarm 00004 * Copyright (C) 2004 by David Jarvie <software@astrojar.org.uk> 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License along 00017 * with this program; if not, write to the Free Software Foundation, Inc., 00018 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "kalarm.h" 00022 #include <kconfig.h> 00023 #include "messagebox.h" 00024 00025 00026 KConfig* MessageBox::mConfig = 0; 00027 TQMap<TQString, KMessageBox::ButtonCode> MessageBox::mContinueDefaults; 00028 00029 00030 /****************************************************************************** 00031 * Set the default button for continue/cancel message boxes with the specified 00032 * 'dontAskAgainName'. 00033 */ 00034 void MessageBox::setContinueDefault(const TQString& dontAskAgainName, ButtonCode defaultButton) 00035 { 00036 mContinueDefaults[dontAskAgainName] = (defaultButton == Cancel ? Cancel : Continue); 00037 } 00038 00039 /****************************************************************************** 00040 * Get the default button for continue/cancel message boxes with the specified 00041 * 'dontAskAgainName'. 00042 */ 00043 KMessageBox::ButtonCode MessageBox::getContinueDefault(const TQString& dontAskAgainName) 00044 { 00045 ButtonCode defaultButton = Continue; 00046 if (!dontAskAgainName.isEmpty()) 00047 { 00048 TQMap<TQString, ButtonCode>::ConstIterator it = mContinueDefaults.find(dontAskAgainName); 00049 if (it != mContinueDefaults.end()) 00050 defaultButton = it.data(); 00051 } 00052 return defaultButton; 00053 } 00054 00055 /****************************************************************************** 00056 * Continue/cancel message box. 00057 * If 'dontAskAgainName' is specified: 00058 * 1) The message box will only be suppressed if the user chose Continue last time. 00059 * 2) The default button is that last set with either setContinueDefault() or 00060 * warningContinueCancel() for that 'dontAskAgainName' value. If neither method 00061 * has set a default button, Continue is the default. 00062 */ 00063 int MessageBox::warningContinueCancel(TQWidget* parent, const TQString& text, const TQString& caption, 00064 const KGuiItem& buttonContinue, const TQString& dontAskAgainName) 00065 { 00066 ButtonCode defaultButton = getContinueDefault(dontAskAgainName); 00067 return warningContinueCancel(parent, defaultButton, text, caption, buttonContinue, dontAskAgainName); 00068 } 00069 00070 /****************************************************************************** 00071 * Continue/cancel message box with the option as to which button is the default. 00072 * If 'dontAskAgainName' is specified, the message box will only be suppressed 00073 * if the user chose Continue last time. 00074 */ 00075 int MessageBox::warningContinueCancel(TQWidget* parent, ButtonCode defaultButton, const TQString& text, 00076 const TQString& caption, const KGuiItem& buttonContinue, 00077 const TQString& dontAskAgainName) 00078 { 00079 setContinueDefault(dontAskAgainName, defaultButton); 00080 if (defaultButton != Cancel) 00081 return KMessageBox::warningContinueCancel(parent, text, caption, buttonContinue, dontAskAgainName); 00082 00083 // Cancel is the default button, so we have to use KMessageBox::warningYesNo() 00084 if (!dontAskAgainName.isEmpty()) 00085 { 00086 ButtonCode b; 00087 if (!shouldBeShownYesNo(dontAskAgainName, b) 00088 && b != KMessageBox::Yes) 00089 { 00090 // Notification has been suppressed, but No (alias Cancel) is the default, 00091 // so unsuppress notification. 00092 saveDontShowAgain(dontAskAgainName, true, false); 00093 } 00094 } 00095 return warningYesNo(parent, text, caption, buttonContinue, KStdGuiItem::cancel(), dontAskAgainName); 00096 } 00097 00098 /****************************************************************************** 00099 * If there is no current setting for whether a non-yes/no message box should be 00100 * shown, set it to 'defaultShow'. 00101 * If a continue/cancel message box has Cancel as the default button, either 00102 * setContinueDefault() or warningContinueCancel() must have been called 00103 * previously to set this for this 'dontShowAgainName' value. 00104 * Reply = true if 'defaultShow' was written. 00105 */ 00106 bool MessageBox::setDefaultShouldBeShownContinue(const TQString& dontShowAgainName, bool defaultShow) 00107 { 00108 if (dontShowAgainName.isEmpty()) 00109 return false; 00110 // First check whether there is an existing setting 00111 KConfig* config = mConfig ? mConfig : KGlobal::config(); 00112 config->setGroup(TQString::fromLatin1("Notification Messages")); 00113 if (config->hasKey(dontShowAgainName)) 00114 return false; 00115 00116 // There is no current setting, so write one 00117 saveDontShowAgainContinue(dontShowAgainName, !defaultShow); 00118 return true; 00119 } 00120 00121 /****************************************************************************** 00122 * Return whether a non-yes/no message box should be shown. 00123 * If the message box has Cancel as the default button, either setContinueDefault() 00124 * or warningContinueCancel() must have been called previously to set this for this 00125 * 'dontShowAgainName' value. 00126 */ 00127 bool MessageBox::shouldBeShownContinue(const TQString& dontShowAgainName) 00128 { 00129 if (getContinueDefault(dontShowAgainName) != Cancel) 00130 return KMessageBox::shouldBeShownContinue(dontShowAgainName); 00131 // Cancel is the default button, so we have to use a yes/no message box 00132 ButtonCode b; 00133 return shouldBeShownYesNo(dontShowAgainName, b); 00134 } 00135 00136 00137 /****************************************************************************** 00138 * Save whether the yes/no message box should not be shown again. 00139 * If 'dontShow' is true, the message box will be suppressed and it will return 00140 * 'result'. 00141 */ 00142 void MessageBox::saveDontShowAgainYesNo(const TQString& dontShowAgainName, bool dontShow, ButtonCode result) 00143 { 00144 saveDontShowAgain(dontShowAgainName, true, dontShow, (result == Yes ? "yes" : "no")); 00145 } 00146 00147 /****************************************************************************** 00148 * Save whether a non-yes/no message box should not be shown again. 00149 * If 'dontShow' is true, the message box will be suppressed and it will return 00150 * Continue. 00151 * If the message box has Cancel as the default button, either setContinueDefault() 00152 * or warningContinueCancel() must have been called previously to set this for this 00153 * 'dontShowAgainName' value. 00154 */ 00155 void MessageBox::saveDontShowAgainContinue(const TQString& dontShowAgainName, bool dontShow) 00156 { 00157 if (getContinueDefault(dontShowAgainName) == Cancel) 00158 saveDontShowAgainYesNo(dontShowAgainName, dontShow, Yes); 00159 else 00160 saveDontShowAgain(dontShowAgainName, false, dontShow); 00161 } 00162 00163 /****************************************************************************** 00164 * Save whether the message box should not be shown again. 00165 */ 00166 void MessageBox::saveDontShowAgain(const TQString& dontShowAgainName, bool yesno, bool dontShow, const char* yesnoResult) 00167 { 00168 if (dontShowAgainName.isEmpty()) 00169 return; 00170 KConfig* config = mConfig ? mConfig : KGlobal::config(); 00171 config->setGroup(TQString::fromLatin1("Notification Messages")); 00172 bool global = (dontShowAgainName[0] == ':'); 00173 if (yesno) 00174 config->writeEntry(dontShowAgainName, TQString::fromLatin1(dontShow ? yesnoResult : ""), true, global); 00175 else 00176 config->writeEntry(dontShowAgainName, !dontShow, true, global); 00177 config->sync(); 00178 }