summaryrefslogtreecommitdiff
path: root/core/launcher/runningappbar.cpp
blob: 356200bc1f131d4e0078898931c69c9555e516c0 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/**********************************************************************
** Copyright (C) 2000-2002 Trolltech AS.  All rights reserved.
**
** This file is part of the Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************
*/

#define QTOPIA_INTERNAL_PRELOADACCESS

// For "kill"
#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>

#include <qdir.h>
#include <qtimer.h>
#include <qpopupmenu.h>
#include <qmessagebox.h>
#include <qpainter.h>
#include <opie/oprocess.h>
#include <qpe/qpeapplication.h>
#include <qpe/applnk.h>
#include <qpe/qcopenvelope_qws.h>
#include <qpe/global.h>
#include <qwindowsystem_qws.h>
#include "runningappbar.h"

RunningAppBar::RunningAppBar(QWidget* parent)
		: QFrame(parent), m_AppLnkSet(0L), m_SelectedAppIndex( -1)
{
	setBackgroundMode( PaletteBackground );

	m_AppLnkSet = new AppLnkSet( QPEApplication::qpeDir() + "apps" );

#ifdef QWS

	connect(qwsServer, SIGNAL(newChannel(const QString&)), this, SLOT(newQcopChannel(const QString&)));
	connect(qwsServer, SIGNAL(removedChannel(const QString&)), this, SLOT(removedQcopChannel(const QString&)));
#endif

	QCopChannel* channel = new QCopChannel( "QPE/System", this );
	connect( channel, SIGNAL(received(const QCString&, const QByteArray&)),
	         this, SLOT(received(const QCString&, const QByteArray&)) );

	spacing = AppLnk::smallIconSize() + 3;
}

RunningAppBar::~RunningAppBar()
{}

void RunningAppBar::newQcopChannel(const QString& channelName)
{
	QString prefix("QPE/Application/");
	if (channelName.startsWith(prefix)) {
		QString appName = channelName.mid(prefix.length());
		//     qDebug("App %s just connected!", appName.latin1());
		const AppLnk* newGuy = m_AppLnkSet->findExec(appName);
		if (newGuy && !newGuy->isPreloaded()) {
			addTask(*newGuy);
		}
	}
}

void RunningAppBar::removedQcopChannel(const QString& channelName)
{
	QString prefix("QPE/Application/");
	if (channelName.startsWith(prefix)) {
		QString appName = channelName.mid(prefix.length());
		qDebug("App %s just disconnected!", appName.latin1());
		const AppLnk* newGuy = m_AppLnkSet->findExec(appName);
		if (newGuy) {
			removeTask(*newGuy);
		}
	}
}

void RunningAppBar::received(const QCString& msg, const QByteArray& data)
{
	// Since fast apps appear and disappear without disconnecting from their
	// channel we need to watch for the showing/hiding events and update according.
	QDataStream stream( data, IO_ReadOnly );
	if ( msg == "fastAppShowing(QString)") {
		QString appName;
		stream >> appName;
		addTask(*m_AppLnkSet->findExec(appName));
	}
	else if ( msg == "fastAppHiding(QString)") {
		QString appName;
		stream >> appName;
		removeTask(*m_AppLnkSet->findExec(appName));
	}
}

void RunningAppBar::addTask(const AppLnk& appLnk)
{
	//   qDebug("Added %s to app list.", appLnk.name().latin1());
	AppLnk* newApp = new AppLnk(appLnk);
	newApp->setExec(appLnk.exec());
	m_AppList.prepend(newApp);
	update();
}

void RunningAppBar::removeTask(const AppLnk& appLnk)
{
	unsigned int i = 0;
	for (; i < m_AppList.count() ; i++) {
		AppLnk* target = m_AppList.at(i);
		if (target->exec() == appLnk.exec()) {
			qDebug("Removing %s from app list.", appLnk.name().latin1());
			m_AppList.remove();

			delete target;
		}
	}
	update();
}

void RunningAppBar::mousePressEvent(QMouseEvent *e)
{
	// Find out if the user is clicking on an app icon...
	// If so, snag the index so when we repaint we show it
	// as highlighed.
	m_SelectedAppIndex = 0;
	int x = 0;
	QListIterator<AppLnk> it( m_AppList );
	for ( ; it.current(); ++it, ++m_SelectedAppIndex, x += spacing ) {
		if ( x + spacing <= width() ) {
			if ( e->x() >= x && e->x() < x + spacing ) {
				if ( m_SelectedAppIndex < (int)m_AppList.count() ) {
					repaint(FALSE);
					return ;
				}
			}
		}
		else {
			break;
		}
	}
	m_SelectedAppIndex = -1;
	repaint( FALSE );
}

void RunningAppBar::mouseReleaseEvent(QMouseEvent *e)
{
	if (e->button() == QMouseEvent::RightButton) {
		return ;
	}
	if ( m_SelectedAppIndex >= 0 ) {
		QString channel = QString("QPE/Application/") + m_AppList.at(m_SelectedAppIndex)->exec();
		if (QCopChannel::isRegistered(channel.latin1())) {
			//       qDebug("%s is running!", m_AppList.at(m_SelectedAppIndex)->exec().latin1());
			QCopEnvelope e(channel.latin1(), "raise()");
			// This class will delete itself after hearing from the app or the timer expiring
			(void)new AppMonitor(*m_AppList.at(m_SelectedAppIndex), *this);
		}
		else {
			removeTask(*m_AppList.at(m_SelectedAppIndex));
		}

		m_SelectedAppIndex = -1;
		update();
	}
}

void RunningAppBar::paintEvent( QPaintEvent * )
{
	QPainter p( this );
	AppLnk *curApp;
	int x = 0;
	int y = (height() - AppLnk::smallIconSize()) / 2;
	int i = 0;

	//p.fillRect( 0, 0, width(), height(), colorGroup().background() );

	QListIterator<AppLnk> it(m_AppList);

	for (; it.current(); i++, ++it ) {
		if ( x + spacing <= width() ) {
			curApp = it.current();
			if ( (int)i == m_SelectedAppIndex )
				p.fillRect( x, y, spacing, curApp->pixmap().height() + 1, colorGroup().highlight() );
			else
				// p.eraseRect( x, y, spacing, curApp->pixmap().height()+1 );
				p.drawPixmap( x, y, curApp->pixmap() );
			x += spacing;
		}
	}
}

QSize RunningAppBar::sizeHint() const
{
	return QSize( frameWidth(), AppLnk::smallIconSize() + frameWidth()*2 + 3 );
}

const int AppMonitor::RAISE_TIMEOUT_MS = 500;

AppMonitor::AppMonitor(const AppLnk& app, RunningAppBar& owner)
		: QObject(0L), m_Owner(owner), m_App(app), m_AppKillerBox(0L)
{
	QCopChannel* channel = new QCopChannel( "QPE/System", this );
	connect( channel, SIGNAL(received(const QCString&, const QByteArray&)),
	         this, SLOT(received(const QCString&, const QByteArray&)) );
	connect(&m_Timer, SIGNAL(timeout()), this, SLOT(timerExpired()));
	m_Timer.start(RAISE_TIMEOUT_MS, TRUE);
}

AppMonitor::~AppMonitor()
{
	if (m_AppKillerBox) {
		delete m_AppKillerBox;
		m_AppKillerBox = 0L;
	}
}

void AppMonitor::received(const QCString& msg, const QByteArray& data)
{
	QDataStream stream( data, IO_ReadOnly );

	if (msg == "appRaised(QString)") {
		QString appName;
		stream >> appName;
		if (appName == m_App.exec()) {
			// qDebug("Got a heartbeat from %s", appName.latin1());
			m_Timer.stop();
			// Check to make sure we're not waiting on user input...
			if (m_AppKillerBox) {
				// If we are, we kill the dialog box, and the code waiting on the result
				// will clean us up (basically the user said "no").
				delete m_AppKillerBox;
				m_AppKillerBox = 0L;
			}
			else {
				// Ok, we're not waiting on user input, so clean us up now.
				// WE DELETE OURSELVES HERE!  Don't do anything else!!
				delete this;
			}
		}
	}
}

void AppMonitor::timerExpired()
{
	// We store this incase the application responds while we're
	// waiting for user input so we know not to delete ourselves.  This
	// will be cleaned up in the destructor.
	m_AppKillerBox = new QMessageBox(tr("Application Problem"),
	                                 tr("<p>%1 is not responding.</p>").arg(m_App.name()) +
	                                 tr("<p>Would you like to force the application to exit?</p>"),
	                                 QMessageBox::Warning, QMessageBox::Yes,
	                                 QMessageBox::No | QMessageBox::Default,
	                                 QMessageBox::NoButton);
	if ( m_AppKillerBox-> exec ( ) == QMessageBox::Yes ) {
		QDir proc ( "/proc/", "[0-9]*", QDir::Name | QDir::Reversed, QDir::Dirs );
		QStringList allprocs = proc. entryList ( );

		pid_t mypid = ::getpid ( );

		for ( QStringList::Iterator it = allprocs. begin ( ); it != allprocs. end ( ); ++it ) {
			if (( *it ). toInt ( ) <= mypid ) // only interested in children
				continue;

			QCString s = QString ( "/proc/" + *it + "/stat" ). local8Bit ( );

			FILE *fp = ::fopen ( s. data ( ), "r" );
			if ( fp ) {
				pid_t pid, ppid;
				char *execptr, *exec = 0;

				if ( ::fscanf ( fp, "%d %as %*c %d ", &pid, &execptr, &ppid ) == 3 ) {
					exec = execptr [0] ? execptr + 1 : execptr;
					if ( exec [0] )
						exec [::strlen ( exec ) - 1] = 0;

					if (( ppid == ::getpid ( )) && ( m_App. exec ( ). local8Bit ( ) == exec )) {
						bool success = false;

						qDebug ( "trying to kill pid=%d, exec=%s, ppid=%d", pid, exec, ppid );


						success |= ( ::kill ( pid, SIGTERM ) == 0 );
						::usleep ( 1000 * 500 );
						success |= ( ::kill ( pid, SIGKILL ) == 0 );

						if ( success )
							m_Owner. removeTask ( m_App );

						::free ( execptr );
						break;
					}
					::free ( execptr );
				}
				::fclose ( fp );
			}
		}
	}
	delete this;
}