summaryrefslogtreecommitdiffstats
path: root/kshutdown/mmessagedialog.cpp
blob: 9e9c3183dd163d3d2b14ddef57e37f032d1d23cb (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
179
180
181
182
183
184
185
/*
	mmessagedialog.cpp - A warning message dialog
	Copyright (C) 2004  Konrad Twardowski <kdtonline@poczta.onet.pl>

	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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "miscutils.h"
#include "mmainwindow.h"
#include "mmessagedialog.h"

#include <tqdatetime.h>
#include <tqhbox.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqlcdnumber.h>
#include <tqtimer.h>

#include <kdebug.h>
#include <kiconloader.h>
#include <tdelocale.h>
#include <tdepopupmenu.h>
#include <kpushbutton.h>
#include <twin.h>

// public

MMessageDialog *MMessageDialog::_instance = 0;

MMessageDialog::MMessageDialog(const int delay, const Action::Type actionToExecute, const TQString &text)
	: KDialog(ks_main, "MMessageDialog", true),
	_dialogDelay(delay),
	_lastTime(-1),
	_action(actionToExecute)
{
	setCaption(i18n("Message"));
	setPaletteBackgroundColor(yellow);
	setPaletteForegroundColor(black);

	// main layout
	TQVBoxLayout *l = new TQVBoxLayout(this, 5);

	// message text
	TQLabel *t_message = new TQLabel(this);
	TQFont f = TQFont(t_message->font());
	f.setPointSize(16);
	t_message->setFont(f);
	t_message->setPaletteBackgroundColor(yellow);
	t_message->setPaletteForegroundColor(black);
	t_message->setAlignment(AlignCenter);
	t_message->setFrameShape(TQFrame::StyledPanel);
	t_message->setFrameShadow(TQFrame::Plain);
	t_message->setLineWidth(4);
	t_message->setMargin(20);
	t_message->setSizePolicy(TQSizePolicy(TQSizePolicy::Preferred, TQSizePolicy::Expanding));
	t_message->setText(text);

	// bottom layout
	TQHBox *bottomBox = new TQHBox(this);
	bottomBox->setSpacing(5);

	// seconds
	l_seconds = new TQLCDNumber(bottomBox);
	l_seconds->display(_dialogDelay);
	l_seconds->setFrameShape(TQFrame::NoFrame);
	l_seconds->setSegmentStyle(TQLCDNumber::Flat);
	MiscUtils::setHint(l_seconds, i18n("Remaining time."));

	// continue button
	b_continue = new KPushButton(
		ks_actions->getIcon(_action), ks_actions->getName(_action),
		bottomBox, "KPushButton::b_continue"
	);
// FIXME: 2.0: no accelerator for "b_continue"
	b_continue->setPaletteBackgroundColor(yellow);
	b_continue->setPaletteForegroundColor(black);
	connect(b_continue, TQ_SIGNAL(clicked()), TQ_SLOT(slotAccept()));
	b_continue->setEnabled(false);
	TQTimer::singleShot(2000, this, TQ_SLOT(slotEnableContinue()));

	// cancel button
	KPushButton *b_cancel = new KPushButton(KStdGuiItem::cancel(), bottomBox, "KPushButton::b_cancel");
	b_cancel->setDefault(true);
	b_cancel->setPaletteBackgroundColor(yellow);
	b_cancel->setPaletteForegroundColor(black);
	connect(b_cancel, TQ_SIGNAL(clicked()), TQ_SLOT(slotReject()));

	l->addWidget(t_message);
	l->addWidget(bottomBox);

	// init time checker
	_checkTimer = new TQTimer(this);
	connect(_checkTimer, TQ_SIGNAL(timeout()), TQ_SLOT(slotCheckTime()));
	_checkTimer->start(500);

	setFixedSize(sizeHint());

	// make sure it is visible
	// code from KAlarm
	WId id = winId();
	KWin::setState(id, NET::StaysOnTop | NET::Sticky);
	KWin::setOnAllDesktops(id, true);
}

MMessageDialog::~MMessageDialog()
{
	// kdDebug() << "MMessageDialog::~MMessageDialog()" << endl;
}

void MMessageDialog::cancel() {
	if (_instance) {
		// kdDebug() << "MMessageDialog::cancel()" << endl;
		_instance->slotReject();
		delete _instance;
		_instance = 0;
	}
}

bool MMessageDialog::show(const int timeout) {
	cancel();
	KWin::setOnDesktop(ks_main->winId(), KWin::currentDesktop());
	// show warning message dialog
	_instance = new MMessageDialog(
		timeout,
		ks_actions->current(),
		ks_actions->getCurrentName()
	);
	bool result = (_instance->exec() == Accepted);
	delete _instance;
	_instance = 0;

	// kdDebug() << "result = " << result << endl;

	return result;
}

// private slots

void MMessageDialog::slotAccept() {
	done(Accepted);
}

void MMessageDialog::slotEnableContinue() {
	b_continue->setEnabled(true);
}

void MMessageDialog::slotCheckTime()
{
	TQTime t = TQTime::currentTime();

	if (t.second() == _lastTime)
		return;

	l_seconds->display(_dialogDelay);

	// timeout?
	if (_dialogDelay == 0)
	{
		slotAccept();

		return;
	}

	MiscUtils::notifyUser(_dialogDelay);

	_lastTime = t.second();
	_dialogDelay--;
}

void MMessageDialog::slotReject() {
	done(Rejected);
}
#include "mmessagedialog.moc"