summaryrefslogtreecommitdiffstats
path: root/clients/tde/src/app/remotemdi.cpp
blob: 11b67f1c20db0cd824002d88984657ed2cc94207 (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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
//Author:    Timothy Pearson <kb9vqf@pearsoncomputing.net>, (C) 2012
//Copyright: See COPYING file that comes with this distribution

// TDE MDI interface based on a (passable) tutorial by Andrea Bergia et al.

#include "remotemdi.h"

#include <cassert>
using namespace std;

#include <pwd.h>

#include <tdeapplication.h>
#include <tdelocale.h>
#include <kdebug.h>
#include <tdeconfig.h>
#include <tdemessagebox.h>

#include <tqlabel.h>
#include <tqtimer.h>
#include <tqlayout.h>
#include <kiconloader.h>
#include <kstdaction.h>
#include <kstatusbar.h>
#include <tdemdichildview.h>
#include <tdelistbox.h>
#include <tdeactionclasses.h>
#include <kedittoolbar.h>
#include <kkeydialog.h>
#include <libtdeldap.h>

#include "views/instrumentview.h"
#include "dialogs/selectserverdlg.h"

#define STATUSBAR_TIMEOUT_ID 5

RemoteMDI::RemoteMDI()
    : KMdiMainFrm(0, "RemoteMDI", KMdi::ChildframeMode), m_children(0), m_rsvSvrSocket(NULL), connToServerConnecting(false), connToServerState(-1), connToServerTimeoutTimer(NULL)
{
	setXMLFile("remotelabui.rc");
	setIcon(SmallIcon("remote_laboratory_client"));

	masterPollTimer = new TQTimer();
	connect(masterPollTimer, SIGNAL(timeout()), this, SLOT(masterPoll()));

	// Create some actions
	KStdAction::close(this, SLOT(closeCurrent()), actionCollection());
	KStdAction::quit(this, SLOT(close()), actionCollection());

	TDEActionCollection *const ac = actionCollection();
	setStandardToolBarMenuEnabled(true);
	KStdAction::quit(TQT_TQOBJECT(this), TQT_SLOT(close()), ac);
	KStdAction::configureToolbars(TQT_TQOBJECT(this), TQT_SLOT(configToolbars()), ac);
	KStdAction::keyBindings(TQT_TQOBJECT(this), TQT_SLOT(configKeys()), ac);
	connect_action = new TDEAction(i18n("Connect to Server"), "connect_creating", TDEShortcut(), TQT_TQOBJECT(this), TQT_SLOT(connectToServer()), ac, "connect_server");
	disconnect_action = new TDEAction(i18n("Disconnect from Server"), "connect_no", TDEShortcut(), TQT_TQOBJECT(this), TQT_SLOT(disconnectFromServer()), ac, "disconnect_server");

	setMenuForSDIModeSysButtons(menuBar());

	createGUI(0);

	// Add Window menu
	if ( !isFakingSDIApplication() ) {
		menuBar()->insertItem(i18n("&Window"), windowMenu(), -1, 4);
	}
	// Hide task bar as no windows are currently active
	hideViewTaskBar();

	// When we change view, change the status bar text
	connect(this, SIGNAL(viewActivated(KMdiChildView*)), this, SLOT(currentChanged(KMdiChildView*)));

	ac->setHighlightingEnabled(true);
	connect(ac, TQT_SIGNAL(actionStatusText(const TQString&)), this, TQT_SLOT(updateStatusBarMainMessage(const TQString&) ));
	connect(ac, TQT_SIGNAL(clearStatusText()), statusBar(), TQT_SLOT(clear()));

	// Create the status bar
	updateStatusBarMainMessage(i18n("No active instruments"));
	KStatusBar* sb = statusBar();
	if (sb) {
		sb->insertItem(i18n("Unknown Time Remaining"), STATUSBAR_TIMEOUT_ID, 0, true);
	}

	processActions();

	processLockouts();

	showMaximized();
}

RemoteMDI::~RemoteMDI()
{
	if (masterPollTimer) {
		masterPollTimer->stop();
		delete masterPollTimer;
	}

	while (m_pCurrentWindow) {
		closeCurrent();
	}

	if (m_rsvSvrSocket) {
		m_rsvSvrSocket->clearPendingData();
		m_rsvSvrSocket->close();
		delete m_rsvSvrSocket;
		m_rsvSvrSocket = NULL;
	}
}

void RemoteMDI::updateStatusBarMessage() {
	TQString windowStatusBarMessage;
	if (m_pCurrentWindow) {
		windowStatusBarMessage = m_windowStatusBarMapping[TQT_TQOBJECT(m_pCurrentWindow)];
	}

	KStatusBar* sb = statusBar();
	if (sb) {
		sb->message(m_mainStatusBarMessage + ((windowStatusBarMessage != "")?" [" + i18n("Instrument") + ": " + windowStatusBarMessage + "]":""));
	}
}

void RemoteMDI::updateStatusBarMainMessage(const TQString& message) {
	m_mainStatusBarMessage = message;
	updateStatusBarMessage();
}

void RemoteMDI::updateStatusBarWindowMessage(const TQString& message, const TQObject* window) {
	const TQObject* windowObject = window;
	if (!windowObject) {
		windowObject = sender();
	}

	if (windowObject) {
		m_windowStatusBarMapping[windowObject] = message;
	}

	updateStatusBarMessage();
}

void RemoteMDI::resizeEvent(TQResizeEvent *e) {
	KMdiMainFrm::resizeEvent(e);
	setSysButtonsAtMenuPosition();
}

void RemoteMDI::processActions() {
	// Add dynamic actions
	TDEActionCollection *const ac = actionCollection();

	TDEAction* action;
	ServiceType st;
	for (ServiceList::Iterator it(m_activeStation.services.begin()); it != m_activeStation.services.end(); ++it) {
		st = *it;
		action = new TDEAction(i18n("Launch")+" "+st.name, st.clientLibrary, TDEShortcut(), TQT_TQOBJECT(this), TQT_SLOT(startModule()), ac, st.clientLibrary.ascii());
		m_instrumentActionList.append(action);
	}
	plugActionList("instrumentMenu_actionlist", m_instrumentActionList);
	plugActionList("instrumentToolBar_actionlist", m_instrumentActionList);
}

void RemoteMDI::startModule() {
	const TDEAction* sendingAction = dynamic_cast<const TDEAction*>(sender());
	if (sendingAction) {
		bool serviceFound = false;
		ServiceType st;
		for (ServiceList::Iterator it(m_activeStation.services.begin()); it != m_activeStation.services.end(); ++it) {
			st = *it;
			if (st.clientLibrary == sendingAction->name()) {
				serviceFound = true;
				break;
			}
		}

		if (!serviceFound) {
			KMessageBox::error(this, i18n("<qt>The active laboratory workspace does not support the requested service</qt>"), i18n("Service Unavailable"));
			return;
		}

		RemoteLab::InstrumentView* view = new RemoteLab::InstrumentView(st.clientLibrary, st.name, (mdiMode() == KMdi::ToplevelMode) ? 0 : this);
		view->setName(st.clientLibrary.ascii());
		connect(view, SIGNAL(statusMessageSet(const TQString&)), this, SLOT(updateStatusBarWindowMessage(const TQString&)));
		if (st.singleInstance) {
			const_cast<TDEAction*>(sendingAction)->setEnabled(false);
		}
		openNewWindow(view);
		showViewTaskBar();
		if (m_serverHost != "") {
			view->connectServer(m_serverHost);
		}
	}
}

int RemoteMDI::getNewTicket() {
	int ret = -1;

	LDAPCredentials credentials;
	KerberosTicketInfoList ticketList = LDAPManager::getKerberosTicketList();
	if (ticketList.count() > 0) {
		TQStringList princParts = TQStringList::split("@", ticketList[0].cachePrincipal);
		credentials.username = princParts[0];
		credentials.realm = princParts[1];
	}
	else {
		struct passwd* pwd = getpwuid(geteuid());
		if (pwd) {
			credentials.username = TQString(pwd->pw_name);
		}
	}
	int result = LDAPManager::getKerberosPassword(credentials, i18n("Please provide Kerberos credentials"), false, this);
	if (result == KDialog::Accepted) {
		TQString errorstring;
		TQString service;
		if (LDAPManager::obtainKerberosTicket(credentials, service, &errorstring) != 0) {
			KMessageBox::error(this, i18n("<qt>Failed to obtain ticket<p>%1</qt>").arg(errorstring), i18n("Failed to obtain Kerberos ticket"));
		}
		else {
			ret = 0;
		}
	}

	return ret;
}

void RemoteMDI::finishConnectingToServer() {
	if (!m_rsvSvrSocket) {
		connToServerState = -1;
		connToServerConnecting = false;
		processLockouts();
		return;
	}

	if (connToServerConnecting) {
		switch(connToServerState) {
			case 0:
				if (!connToServerTimeoutTimer) {
					connToServerTimeoutTimer = new TQTimer;
					connToServerTimeoutTimer->start(5000, TRUE);
				}
				if ((m_rsvSvrSocket->state() == TQSocket::Connecting) || (m_rsvSvrSocket->state() == TQSocket::HostLookup)) {
					if (!connToServerTimeoutTimer->isActive()) {
						connToServerState = -3;
						connToServerConnecting = false;
						disconnectFromServer();
						KMessageBox::error(this, i18n("<qt>Unable to establish connection to remote server</qt>"), i18n("Connection Failed"));
						return;
					}
				}
				else {
					if (m_rsvSvrSocket->state() == TQSocket::Connected) {
						printf("[DEBUG] Initial connection established...\n\r"); fflush(stdout);
						m_rsvSvrSocket->setDataTimeout(5000);
						m_rsvSvrSocket->setUsingKerberos(true);
						delete connToServerTimeoutTimer;
						connToServerTimeoutTimer= NULL;
						connToServerState = 1;
					}
					else {
						connToServerState = -1;
						connToServerConnecting = false;
						disconnectFromServer();
						KMessageBox::error(this, i18n("<qt>Unable to establish connection to remote server</qt>"), i18n("Connection Failed"));
						return;
					}
				}
				break;
			case 1:
				if (m_rsvSvrSocket->kerberosStatus() == TDEKerberosClientSocket::KerberosInitializing) {
					// Do nothing
				}
				else {
					if (m_rsvSvrSocket->kerberosStatus() != TDEKerberosClientSocket::KerberosInUse) {
						connToServerState = -1;
						connToServerConnecting = false;
						disconnectFromServer();

						// Try to get a valid ticket
						if (getNewTicket() == 0) {
							// Retry connection if no obvious errors were detected
							TQTimer::singleShot(0, this, SLOT(connectToServer()));
							return;
						}
						else {
							KMessageBox::error(this, i18n("<qt>Unable to establish Kerberos protocol with remote server<p>Please verify that you currently hold a valid Kerberos ticket</qt>"), i18n("Connection Failed"));
							return;
						}
					}
					else {
						connect(m_rsvSvrSocket, SIGNAL(readyRead()), m_rsvSvrSocket, SLOT(processPendingData()));
						m_rsvSvrSocket->processPendingData();
						connToServerState = 2;
					}
				}
				break;
			case 2:
				// Connection established!
				// Read magic number and proto version from server
				TQDataStream* ds = new TQDataStream(m_rsvSvrSocket);
				ds->setPrintableData(true);
				while (!m_rsvSvrSocket->canReadFrame()) {
					tqApp->processEvents();
					if (!m_rsvSvrSocket) {
						return;
					}
				}
				TQ_UINT32 magicnum;
				TQ_UINT32 protover;
				*ds >> magicnum;
				*ds >> protover;
				m_rsvSvrSocket->clearFrameTail();
				printf("[DEBUG] Got magic number %d and protocol version %d\n\r", magicnum, protover); fflush(stdout);
				if ((magicnum == MAGIC_NUMBER) && (protover == PROTOCOL_VERSION)) {
					// Request server name
					TQString serverName;
					*ds << TQString("NAME");
					m_rsvSvrSocket->writeEndOfFrame();
					while (!m_rsvSvrSocket->canReadFrame()) {
						tqApp->processEvents();
						if (!m_rsvSvrSocket) {
							return;
						}
					}
					*ds >> serverName;
					m_rsvSvrSocket->clearFrameTail();

					// Set caption if a valid server name was received
					if ((serverName != "") && (serverName != "ERRINVCMD")) {
						setCaption(TQString("%1 - %2").arg(serverName).arg(kapp->caption()));
					}

					delete ds;
					disconnect_action->setEnabled(true);
					promptForStationType();
				}
				else {
					delete ds;
					disconnectFromServer();
					KMessageBox::error(this, i18n("<qt>The remote server is not compatible with this client</qt>"), i18n("Connection Failed"));
				}
				connToServerState = 3;
				connToServerConnecting = false;
				masterPollTimer->start(0, TRUE);
				processLockouts();
				break;
		}

		TQTimer::singleShot(0, this, SLOT(finishConnectingToServer()));
	}
}

void RemoteMDI::masterPoll() {
	// Query current termination timestamp
	if (m_rsvSvrSocket) {
		if ((m_rsvSvrSocket->state() == TQSocket::Connected) && (!connToServerConnecting)) {
			TQDataStream ds(m_rsvSvrSocket);
			ds.setPrintableData(true);
			TQ_ULLONG terminationStamp;
			long long currentStamp;
			ds << TQString("TSTP");
			m_rsvSvrSocket->writeEndOfFrame();
			while (!m_rsvSvrSocket->canReadFrame()) {
				tqApp->processEvents();
				if (!m_rsvSvrSocket) {
					masterPollTimer->start(1000, TRUE);
					return;
				}
			}
			ds >> terminationStamp;
			m_rsvSvrSocket->clearFrameTail();
			currentStamp = TQDateTime::currentDateTime().toTime_t();

			KStatusBar* sb = statusBar();
			if (sb) {
				if (terminationStamp == 0) {
					sb->changeItem(i18n("Unlimited Time Remaining"), STATUSBAR_TIMEOUT_ID);
				}
				else {
					long long difference = terminationStamp - currentStamp;
					int seconds = 0;
					int minutes = 0;
					int hours = 0;
					int days = 0;
					if (difference >= 0) {
						days = (difference / 86400);
						difference = difference - (days * 86400);
						hours = (difference / 3600);
						difference = difference - (hours * 3600);
						minutes = (difference / 60);
						difference = difference - (minutes * 60);
						seconds = difference;
					}
					TQString differenceString;
					if (days > 0) {
						differenceString.append(i18n("%1 day(s), ").arg(days));
					}
					if ((days > 0) || (hours > 0)) {
						differenceString.append(i18n("%1 hours(s), ").arg(hours));
					}
					if ((days > 0) || (hours > 0) || (minutes > 0)) {
						differenceString.append(i18n("%1 minutes(s), ").arg(minutes));
					}
					differenceString.append(i18n("%1 seconds(s)").arg(seconds));
					sb->changeItem(i18n("%1 Remaining").arg(differenceString), STATUSBAR_TIMEOUT_ID);
				}
			}
		}
	}

	masterPollTimer->start(1000, TRUE);
}

void RemoteMDI::connectToServer() {
	if (m_rsvSvrSocket) {
		if (m_rsvSvrSocket->state() != TQSocket::Idle) {
			printf("[DEBUG] Not connecting because the socket is still in state %d\n\r", m_rsvSvrSocket->state()); fflush(stdout);
			return;
		}
	}

	connect_action->setEnabled(false);
	disconnect_action->setEnabled(true);

	// Connect to the central reservation/control server
	if (!m_rsvSvrSocket) {
		m_rsvSvrSocket = new TDEKerberosClientSocket(this);
		connect(m_rsvSvrSocket, SIGNAL(connectionClosed()), this, SLOT(connectionClosedHandler()));
		connect(m_rsvSvrSocket, TQT_SIGNAL(statusMessageUpdated(const TQString&)), this, TQT_SLOT(updateStatusBarMainMessage(const TQString&) ));
	}
	m_rsvSvrSocket->setServiceName("ulab");
	if (m_serverHost != "") {
		m_rsvSvrSocket->setServerFQDN(m_serverHost);
		m_rsvSvrSocket->connectToHost(m_serverHost, 4004);

		// Finish connecting when appropriate
		connToServerState = 0;
		connToServerConnecting = true;
		TQTimer::singleShot(0, this, SLOT(finishConnectingToServer()));
	}
}

void RemoteMDI::promptForStationType() {
	if (!m_rsvSvrSocket) {
		return;
	}
	if (m_rsvSvrSocket->state() != TQSocket::Connected) {
		return;
	}

	TQDataStream ds(m_rsvSvrSocket);
	ds.setPrintableData(true);

	// Request list of laboratory stations
	StationList slist;
	ds << TQString("LIST");
	m_rsvSvrSocket->writeEndOfFrame();
	while (!m_rsvSvrSocket->canReadFrame()) {
		tqApp->processEvents();
		if (!m_rsvSvrSocket) {
			return;
		}
	}
	ds >> slist;
	m_rsvSvrSocket->clearFrameTail();

	SelectServerDialog select(this, 0, slist);
	const int ret = select.exec();
	if (ret == KDialog::Accepted) {
		TQString result;
		ds << TQString("BIND");
		m_rsvSvrSocket->writeEndOfFrame();
		ds << select.m_selectedStation;
		m_rsvSvrSocket->writeEndOfFrame();
		while (!m_rsvSvrSocket->canReadFrame()) {
			tqApp->processEvents();
			if (!m_rsvSvrSocket) {
				return;
			}
		}
		ds >> result;
		m_rsvSvrSocket->clearFrameTail();
		if (result == "OK") {
			// Success!
			m_activeStation = select.m_selectedStation;
			processActions();
		}
		else if (result == "ERRUNAVAL") {
			KMessageBox::error(this, i18n("<qt>No stations of the specified type are currently available<p>Please try again later</qt>"), i18n("Insufficient Laboratory Resources"));
			disconnectFromServer();
		}
		else if (result == "ERRPREVCN") {
			KMessageBox::error(this, i18n("<qt>You are already connected to a laboratory station<p>Please disconnect and try again</qt>"), i18n("Multiple Connections Detected"));
			disconnectFromServer();
		}
		else {
			KMessageBox::error(this, i18n("<qt>Unknown server error<p>Please reconnect and try again</qt>"), i18n("Internal Error"));
			disconnectFromServer();
		}
	}
	else {
		disconnectFromServer();
	}
}

void RemoteMDI::disconnectFromServer() {
	connect_action->setEnabled(false);
	disconnect_action->setEnabled(false);

	m_instrumentActionList.clear();
	unplugActionList("instrumentMenu_actionlist");
	unplugActionList("instrumentToolBar_actionlist");

	// Close all windows
	closeAllViews();

	if (m_rsvSvrSocket) {
		m_rsvSvrSocket->clearPendingData();
		m_rsvSvrSocket->close();
		delete m_rsvSvrSocket;
		m_rsvSvrSocket = NULL;
	}

	connect_action->setEnabled(true);
	processLockouts();
}

void RemoteMDI::connectionClosedHandler() {
	disconnectFromServer();
	KMessageBox::error(this, i18n("<qt>The remote server has closed the connection</qt>"), i18n("Connection Terminated"));
}

void RemoteMDI::processLockouts() {
	bool connected = false;
	if (m_rsvSvrSocket) {
		connected = ((m_rsvSvrSocket->state() == TQSocket::Connected) && (connToServerConnecting == false) && (connToServerState > 0));
	}

	connect_action->setEnabled(!connected);
	disconnect_action->setEnabled(connected);

	for (TQPtrList<TDEAction>::Iterator it(m_instrumentActionList.begin()); it != m_instrumentActionList.end(); ++it) {
		(*it)->setEnabled(connected);
	}

	if (!connected) {
		KStatusBar* sb = statusBar();
		if (sb) {
			sb->changeItem(i18n("Unknown Time Remaining"), STATUSBAR_TIMEOUT_ID);
		}
	}
}

void RemoteMDI::configToolbars() {
	KEditToolbar dialog(factory(), this);
	dialog.showButtonApply(false);

	if (dialog.exec()) {
		applyMainWindowSettings(kapp->config(), "window");
	}
}

void RemoteMDI::configKeys() {
	KKeyDialog::configure(actionCollection(), this);
}

void RemoteMDI::setServerHost(TQString server) {
	m_serverHost = server;
	connectToServer();
}

void RemoteMDI::openNewWindow(KMdiChildView *view) {
	// Add a child view
	m_children++;

	// The child view will be our child only if we aren't in Toplevel mode
	if (!view) {
		view = new KMdiChildView(i18n("View %1").arg(m_children), (mdiMode() == KMdi::ToplevelMode) ? 0 : this);
	}
	(new TQHBoxLayout(view))->setAutoAdd( true );

	// Add to the MDI and set as current
	if (mdiMode() == KMdi::ToplevelMode) {
		addWindow(view, KMdi::Detach);
	}
	else {
		addWindow(view);
	}
	currentChanged(view);

	// Handle termination
	connect(view, SIGNAL(childWindowCloseRequest(KMdiChildView*)), this, SLOT(childClosed(KMdiChildView*)));
}

void RemoteMDI::childWindowCloseRequest(KMdiChildView *pWnd) {
	RemoteLab::InstrumentView* iview = dynamic_cast<RemoteLab::InstrumentView*>(pWnd);
	if (iview) {
		// Give the child a chance to finish what it was doing and exit cleanly (i.e. without crashing!)
		iview->closeConnections();
		iview->hide();

		KMdiMainFrm::childWindowCloseRequest(pWnd);
	}
}

void RemoteMDI::currentChanged(KMdiChildView *current) {
	RemoteLab::InstrumentView* view = dynamic_cast<RemoteLab::InstrumentView*>(current);

	// Plug/unplug menus
	if (view) {
		unplugActionList("selectedInstrument_actionlist");
		plugActionList("selectedInstrument_actionlist", view->menuActionList());
	}

	// Update status bar and list box
	updateStatusBarMainMessage(i18n("Instrument %1 activated").arg(current->tabCaption()));
}

void RemoteMDI::closeCurrent() {
	// If there's a current view, close it
	if (m_pCurrentWindow) {
		closeSpecifiedWindow(m_pCurrentWindow);
	}
}

void RemoteMDI::closeSpecifiedWindow(KMdiChildView *window) {
	if (window) {
		// Notify the status bar of the removal of the window
		updateStatusBarWindowMessage(TQString::null, TQT_TQOBJECT(window));
		updateStatusBarMainMessage(i18n("Instrument %1 removed").arg(window->tabCaption()));

		// Unplug menus
		unplugActionList("selectedInstrument_actionlist");

		// We could also call removeWindowFromMdi, but it doesn't delete the
		// pointer. This way, we're sure that the view will get deleted.
		closeWindow(window);

		// Synchronize combo box
		if (m_pCurrentWindow) {
			currentChanged(m_pCurrentWindow);
		}
	}
}

void RemoteMDI::childClosed(KMdiChildView * w) {
	assert(w);

	// Set as active
	w->activate();
	assert(w == m_pCurrentWindow);

	// Unplug menus
	unplugActionList("selectedInstrument_actionlist");

	// Notify the status bar of the removal of the window
	updateStatusBarWindowMessage(TQString::null, TQT_TQOBJECT(w));
	updateStatusBarMainMessage(i18n("Instrument %1 removed").arg(w->tabCaption()));

	// Re-enable associated action
	RemoteLab::InstrumentView* view = dynamic_cast<RemoteLab::InstrumentView*>(w);
	if (view) {
		TQString libraryName = view->name();
		for (TQPtrList<TDEAction>::Iterator it(m_instrumentActionList.begin()); it != m_instrumentActionList.end(); ++it) {
			if ((*it)->name() == libraryName) {
				(*it)->setEnabled(true);
			}
		}
	}

	// Remove status bar text
	m_windowStatusBarMapping.remove(TQT_TQOBJECT(w));

	// Remove the view from MDI, BUT DO NOT DELETE IT! It is automatically deleted by TQt since it was closed.
	removeWindowFromMdi(w);
}

bool RemoteMDI::queryClose() {
	// Close all open connections
	KMdiIterator<KMdiChildView*> *it = createIterator();
	while (it->currentItem()) {
		KMdiChildView *c = dynamic_cast<KMdiChildView*>(it->currentItem());
		if (c) {
			RemoteLab::InstrumentView* iview = dynamic_cast<RemoteLab::InstrumentView*>(c);
			if (iview) {
				iview->closeConnections();
			}
		}
		it->next();
	}
	deleteIterator(it);

	// Save current MDI settings (window positions, etc.)
	// FIXME
	TDEConfig *c = kapp->config();

	c->sync();

	// Allow this window to close
	return true;
}

#include "remotemdi.moc"