summaryrefslogtreecommitdiffstats
path: root/kalarm/lib/messagebox.cpp
blob: 4e78ef085f14aba6db0f74e50050e6dc4f79143d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 *  messagebox.cpp  -  enhanced KMessageBox class
 *  Program:  kalarm
 *  Copyright (C) 2004 by David Jarvie <software@astrojar.org.uk>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "kalarm.h"
#include <tdeconfig.h>
#include "messagebox.h"


TDEConfig* MessageBox::mConfig = 0;
TQMap<TQString, KMessageBox::ButtonCode> MessageBox::mContinueDefaults;


/******************************************************************************
* Set the default button for continue/cancel message boxes with the specified
* 'dontAskAgainName'.
*/
void MessageBox::setContinueDefault(const TQString& dontAskAgainName, ButtonCode defaultButton)
{
	mContinueDefaults[dontAskAgainName] = (defaultButton == Cancel ? Cancel : Continue);
}

/******************************************************************************
* Get the default button for continue/cancel message boxes with the specified
* 'dontAskAgainName'.
*/
KMessageBox::ButtonCode MessageBox::getContinueDefault(const TQString& dontAskAgainName)
{
	ButtonCode defaultButton = Continue;
	if (!dontAskAgainName.isEmpty())
	{
		TQMap<TQString, ButtonCode>::ConstIterator it = mContinueDefaults.find(dontAskAgainName);
		if (it != mContinueDefaults.end())
			defaultButton = it.data();
	}
	return defaultButton;
}

/******************************************************************************
* Continue/cancel message box.
* If 'dontAskAgainName' is specified:
*   1) The message box will only be suppressed if the user chose Continue last time.
*   2) The default button is that last set with either setContinueDefault() or
*      warningContinueCancel() for that 'dontAskAgainName' value. If neither method
*      has set a default button, Continue is the default.
*/
int MessageBox::warningContinueCancel(TQWidget* parent, const TQString& text, const TQString& caption,
                                      const KGuiItem& buttonContinue, const TQString& dontAskAgainName)
{
	ButtonCode defaultButton = getContinueDefault(dontAskAgainName);
	return warningContinueCancel(parent, defaultButton, text, caption, buttonContinue, dontAskAgainName);
}

/******************************************************************************
* Continue/cancel message box with the option as to which button is the default.
* If 'dontAskAgainName' is specified, the message box will only be suppressed
* if the user chose Continue last time.
*/
int MessageBox::warningContinueCancel(TQWidget* parent, ButtonCode defaultButton, const TQString& text,
                                      const TQString& caption, const KGuiItem& buttonContinue,
                                      const TQString& dontAskAgainName)
{
	setContinueDefault(dontAskAgainName, defaultButton);
	if (defaultButton != Cancel)
		return KMessageBox::warningContinueCancel(parent, text, caption, buttonContinue, dontAskAgainName);

	// Cancel is the default button, so we have to use KMessageBox::warningYesNo()
	if (!dontAskAgainName.isEmpty())
	{
		ButtonCode b;
		if (!shouldBeShownYesNo(dontAskAgainName, b)
		&&  b != KMessageBox::Yes)
		{
			// Notification has been suppressed, but No (alias Cancel) is the default,
			// so unsuppress notification.
			saveDontShowAgain(dontAskAgainName, true, false);
		}
	}
	return warningYesNo(parent, text, caption, buttonContinue, KStdGuiItem::cancel(), dontAskAgainName);
}

/******************************************************************************
* If there is no current setting for whether a non-yes/no message box should be
* shown, set it to 'defaultShow'.
* If a continue/cancel message box has Cancel as the default button, either
* setContinueDefault() or warningContinueCancel() must have been called
* previously to set this for this 'dontShowAgainName' value.
* Reply = true if 'defaultShow' was written.
*/
bool MessageBox::setDefaultShouldBeShownContinue(const TQString& dontShowAgainName, bool defaultShow)
{
    if (dontShowAgainName.isEmpty())
		return false;
	// First check whether there is an existing setting
	TDEConfig* config = mConfig ? mConfig : TDEGlobal::config();
	config->setGroup(TQString::fromLatin1("Notification Messages"));
	if (config->hasKey(dontShowAgainName))
		return false;

	// There is no current setting, so write one
	saveDontShowAgainContinue(dontShowAgainName, !defaultShow);
	return true;
}

/******************************************************************************
* Return whether a non-yes/no message box should be shown.
* If the message box has Cancel as the default button, either setContinueDefault()
* or warningContinueCancel() must have been called previously to set this for this
* 'dontShowAgainName' value.
*/
bool MessageBox::shouldBeShownContinue(const TQString& dontShowAgainName)
{
	if (getContinueDefault(dontShowAgainName) != Cancel)
		return KMessageBox::shouldBeShownContinue(dontShowAgainName);
	// Cancel is the default button, so we have to use a yes/no message box
	ButtonCode b;
	return shouldBeShownYesNo(dontShowAgainName, b);
}


/******************************************************************************
* Save whether the yes/no message box should not be shown again.
* If 'dontShow' is true, the message box will be suppressed and it will return
* 'result'.
*/
void MessageBox::saveDontShowAgainYesNo(const TQString& dontShowAgainName, bool dontShow, ButtonCode result)
{
	saveDontShowAgain(dontShowAgainName, true, dontShow, (result == Yes ? "yes" : "no"));
}

/******************************************************************************
* Save whether a non-yes/no message box should not be shown again.
* If 'dontShow' is true, the message box will be suppressed and it will return
* Continue.
* If the message box has Cancel as the default button, either setContinueDefault()
* or warningContinueCancel() must have been called previously to set this for this
* 'dontShowAgainName' value.
*/
void MessageBox::saveDontShowAgainContinue(const TQString& dontShowAgainName, bool dontShow)
{
	if (getContinueDefault(dontShowAgainName) == Cancel)
		saveDontShowAgainYesNo(dontShowAgainName, dontShow, Yes);
	else
		saveDontShowAgain(dontShowAgainName, false, dontShow);
}

/******************************************************************************
* Save whether the message box should not be shown again.
*/
void MessageBox::saveDontShowAgain(const TQString& dontShowAgainName, bool yesno, bool dontShow, const char* yesnoResult)
{
	if (dontShowAgainName.isEmpty())
		return;
	TDEConfig* config = mConfig ? mConfig : TDEGlobal::config();
	config->setGroup(TQString::fromLatin1("Notification Messages"));
	bool global = (dontShowAgainName[0] == ':');
	if (yesno)
		config->writeEntry(dontShowAgainName, TQString::fromLatin1(dontShow ? yesnoResult : ""), true, global);
	else
		config->writeEntry(dontShowAgainName, !dontShow, true, global);
	config->sync();
}