kalarm/lib

messagebox.cpp
1 /*
2  * messagebox.cpp - enhanced KMessageBox class
3  * Program: kalarm
4  * Copyright (C) 2004 by David Jarvie <software@astrojar.org.uk>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 
21 #include "kalarm.h"
22 #include <tdeconfig.h>
23 #include "messagebox.h"
24 
25 
26 TDEConfig* MessageBox::mConfig = 0;
27 TQMap<TQString, KMessageBox::ButtonCode> MessageBox::mContinueDefaults;
28 
29 
30 /******************************************************************************
31 * Set the default button for continue/cancel message boxes with the specified
32 * 'dontAskAgainName'.
33 */
34 void MessageBox::setContinueDefault(const TQString& dontAskAgainName, ButtonCode defaultButton)
35 {
36  mContinueDefaults[dontAskAgainName] = (defaultButton == Cancel ? Cancel : Continue);
37 }
38 
39 /******************************************************************************
40 * Get the default button for continue/cancel message boxes with the specified
41 * 'dontAskAgainName'.
42 */
43 KMessageBox::ButtonCode MessageBox::getContinueDefault(const TQString& dontAskAgainName)
44 {
45  ButtonCode defaultButton = Continue;
46  if (!dontAskAgainName.isEmpty())
47  {
48  TQMap<TQString, ButtonCode>::ConstIterator it = mContinueDefaults.find(dontAskAgainName);
49  if (it != mContinueDefaults.end())
50  defaultButton = it.data();
51  }
52  return defaultButton;
53 }
54 
55 /******************************************************************************
56 * Continue/cancel message box.
57 * If 'dontAskAgainName' is specified:
58 * 1) The message box will only be suppressed if the user chose Continue last time.
59 * 2) The default button is that last set with either setContinueDefault() or
60 * warningContinueCancel() for that 'dontAskAgainName' value. If neither method
61 * has set a default button, Continue is the default.
62 */
63 int MessageBox::warningContinueCancel(TQWidget* parent, const TQString& text, const TQString& caption,
64  const KGuiItem& buttonContinue, const TQString& dontAskAgainName)
65 {
66  ButtonCode defaultButton = getContinueDefault(dontAskAgainName);
67  return warningContinueCancel(parent, defaultButton, text, caption, buttonContinue, dontAskAgainName);
68 }
69 
70 /******************************************************************************
71 * Continue/cancel message box with the option as to which button is the default.
72 * If 'dontAskAgainName' is specified, the message box will only be suppressed
73 * if the user chose Continue last time.
74 */
75 int MessageBox::warningContinueCancel(TQWidget* parent, ButtonCode defaultButton, const TQString& text,
76  const TQString& caption, const KGuiItem& buttonContinue,
77  const TQString& dontAskAgainName)
78 {
79  setContinueDefault(dontAskAgainName, defaultButton);
80  if (defaultButton != Cancel)
81  return KMessageBox::warningContinueCancel(parent, text, caption, buttonContinue, dontAskAgainName);
82 
83  // Cancel is the default button, so we have to use KMessageBox::warningYesNo()
84  if (!dontAskAgainName.isEmpty())
85  {
86  ButtonCode b;
87  if (!shouldBeShownYesNo(dontAskAgainName, b)
88  && b != KMessageBox::Yes)
89  {
90  // Notification has been suppressed, but No (alias Cancel) is the default,
91  // so unsuppress notification.
92  saveDontShowAgain(dontAskAgainName, true, false);
93  }
94  }
95  return warningYesNo(parent, text, caption, buttonContinue, KStdGuiItem::cancel(), dontAskAgainName);
96 }
97 
98 /******************************************************************************
99 * If there is no current setting for whether a non-yes/no message box should be
100 * shown, set it to 'defaultShow'.
101 * If a continue/cancel message box has Cancel as the default button, either
102 * setContinueDefault() or warningContinueCancel() must have been called
103 * previously to set this for this 'dontShowAgainName' value.
104 * Reply = true if 'defaultShow' was written.
105 */
106 bool MessageBox::setDefaultShouldBeShownContinue(const TQString& dontShowAgainName, bool defaultShow)
107 {
108  if (dontShowAgainName.isEmpty())
109  return false;
110  // First check whether there is an existing setting
111  TDEConfig* config = mConfig ? mConfig : TDEGlobal::config();
112  config->setGroup(TQString::fromLatin1("Notification Messages"));
113  if (config->hasKey(dontShowAgainName))
114  return false;
115 
116  // There is no current setting, so write one
117  saveDontShowAgainContinue(dontShowAgainName, !defaultShow);
118  return true;
119 }
120 
121 /******************************************************************************
122 * Return whether a non-yes/no message box should be shown.
123 * If the message box has Cancel as the default button, either setContinueDefault()
124 * or warningContinueCancel() must have been called previously to set this for this
125 * 'dontShowAgainName' value.
126 */
127 bool MessageBox::shouldBeShownContinue(const TQString& dontShowAgainName)
128 {
129  if (getContinueDefault(dontShowAgainName) != Cancel)
130  return KMessageBox::shouldBeShownContinue(dontShowAgainName);
131  // Cancel is the default button, so we have to use a yes/no message box
132  ButtonCode b;
133  return shouldBeShownYesNo(dontShowAgainName, b);
134 }
135 
136 
137 /******************************************************************************
138 * Save whether the yes/no message box should not be shown again.
139 * If 'dontShow' is true, the message box will be suppressed and it will return
140 * 'result'.
141 */
142 void MessageBox::saveDontShowAgainYesNo(const TQString& dontShowAgainName, bool dontShow, ButtonCode result)
143 {
144  saveDontShowAgain(dontShowAgainName, true, dontShow, (result == Yes ? "yes" : "no"));
145 }
146 
147 /******************************************************************************
148 * Save whether a non-yes/no message box should not be shown again.
149 * If 'dontShow' is true, the message box will be suppressed and it will return
150 * Continue.
151 * If the message box has Cancel as the default button, either setContinueDefault()
152 * or warningContinueCancel() must have been called previously to set this for this
153 * 'dontShowAgainName' value.
154 */
155 void MessageBox::saveDontShowAgainContinue(const TQString& dontShowAgainName, bool dontShow)
156 {
157  if (getContinueDefault(dontShowAgainName) == Cancel)
158  saveDontShowAgainYesNo(dontShowAgainName, dontShow, Yes);
159  else
160  saveDontShowAgain(dontShowAgainName, false, dontShow);
161 }
162 
163 /******************************************************************************
164 * Save whether the message box should not be shown again.
165 */
166 void MessageBox::saveDontShowAgain(const TQString& dontShowAgainName, bool yesno, bool dontShow, const char* yesnoResult)
167 {
168  if (dontShowAgainName.isEmpty())
169  return;
170  TDEConfig* config = mConfig ? mConfig : TDEGlobal::config();
171  config->setGroup(TQString::fromLatin1("Notification Messages"));
172  bool global = (dontShowAgainName[0] == ':');
173  if (yesno)
174  config->writeEntry(dontShowAgainName, TQString::fromLatin1(dontShow ? yesnoResult : ""), true, global);
175  else
176  config->writeEntry(dontShowAgainName, !dontShow, true, global);
177  config->sync();
178 }
static bool shouldBeShownContinue(const TQString &dontShowAgainName)
Returns whether a non-Yes/No message box should be shown.
Definition: messagebox.cpp:127
static void saveDontShowAgainContinue(const TQString &dontShowAgainName, bool dontShow=true)
Stores whether a non-Yes/No message box should or should not be shown again.
Definition: messagebox.cpp:155
static void setContinueDefault(const TQString &dontAskAgainName, ButtonCode defaultButton)
Sets the default button for the Continue/Cancel message box with the specified "don't ask again" name...
Definition: messagebox.cpp:34
static int warningContinueCancel(TQWidget *parent, ButtonCode defaultButton, const TQString &text, const TQString &caption=TQString(), const KGuiItem &buttonContinue=KStdGuiItem::cont(), const TQString &dontAskAgainName=TQString())
Displays a Continue/Cancel message box with the option as to which button is the default.
Definition: messagebox.cpp:75
static bool setDefaultShouldBeShownContinue(const TQString &dontShowAgainName, bool defaultShow)
If there is no current setting for whether a non-Yes/No message box should be shown,...
Definition: messagebox.cpp:106
static ButtonCode getContinueDefault(const TQString &dontAskAgainName)
Gets the default button for the Continue/Cancel message box with the specified "don't ask again" name...
Definition: messagebox.cpp:43
static void saveDontShowAgainYesNo(const TQString &dontShowAgainName, bool dontShow=true, ButtonCode result=No)
Stores whether the Yes/No message box should or should not be shown again.
Definition: messagebox.cpp:142