summaryrefslogtreecommitdiffstats
path: root/tdm/kfrontend/kchooser.cpp
blob: 9f0c714c05b3ca1e9909bdb1229006bd00129891 (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
/*

chooser widget for TDM

Copyright (C) 2002-2003 Oswald Buddenhagen <ossi@kde.org>
based on the chooser (C) 1999 by Harald Hoyer <Harald.Hoyer@RedHat.de>

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 <config.h>

#ifdef XDMCP

#include "kchooser.h"
#include "kconsole.h"
#include "tdmconfig.h"
#include "tdm_greet.h"

#include <tdelocale.h>

#include <tqlayout.h>
#include <tqlabel.h>
#include <tqpushbutton.h>
#include <tqpopupmenu.h>
#include <tqsocketnotifier.h>
#include <tqlistview.h>
#include <tqlineedit.h>

#include <stdlib.h> // for free()

class ChooserListViewItem : public TQListViewItem {
  public:
	ChooserListViewItem( TQListView* parent, int _id, const TQString& nam, const TQString& sts )
		: TQListViewItem( parent, nam, sts ) { id = _id; };

	int id;
};


ChooserDlg::ChooserDlg()
	: inherited()
{
	completeMenu( LOGIN_REMOTE_ONLY, ex_greet, i18n("&Local Login"), ALT+Key_L );

	TQBoxLayout *vbox = new TQVBoxLayout( this, 10, 10 );

	TQLabel *title = new TQLabel( i18n("XDMCP Host Menu"), this );
	title->setAlignment( AlignCenter );
	vbox->addWidget( title );

	host_view = new TQListView( this, "hosts" );
	host_view->addColumn( i18n("Hostname") );
	host_view->setColumnWidth( 0, fontMetrics().width( "login.crap.net" ) );
	host_view->addColumn( i18n("Status") );
	host_view->setMinimumWidth( fontMetrics().width( "login.crap.com Display not authorized to connect this server" ) );
	host_view->setResizeMode( TQListView::LastColumn );
	host_view->setAllColumnsShowFocus( true );
	vbox->addWidget( host_view );

	iline = new TQLineEdit( this );
	iline->setEnabled( TRUE );
	TQLabel *itxt = new TQLabel( iline, i18n("Hos&t:"), this );
	TQPushButton *addButton = new TQPushButton( i18n("A&dd"), this );
	connect( addButton, TQT_SIGNAL(clicked()), TQT_SLOT(addHostname()) );
	TQBoxLayout *hibox = new TQHBoxLayout( vbox, 10 );
	hibox->addWidget( itxt );
	hibox->addWidget( iline );
	hibox->addWidget( addButton );

	// Buttons
	TQPushButton *acceptButton = new TQPushButton( i18n("&Accept"), this );
	acceptButton->setDefault( true );
	TQPushButton *pingButton = new TQPushButton( i18n("&Refresh"), this );

	TQBoxLayout *hbox = new TQHBoxLayout( vbox, 20 );
	hbox->addWidget( acceptButton );
	hbox->addWidget( pingButton );
	hbox->addStretch( 1 );

	if (optMenu) {
		TQPushButton *menuButton = new TQPushButton( i18n("&Menu"), this );
		menuButton->setPopup( optMenu );
		hbox->addWidget( menuButton );
		hbox->addStretch( 1 );
	}

//	TQPushButton *helpButton = new TQPushButton( i18n("&Help"), this );
//	hbox->addWidget( helpButton );

#ifdef WITH_TDM_XCONSOLE
	if (consoleView)
		vbox->addWidget( consoleView );
#endif

	sn = new TQSocketNotifier( rfd, TQSocketNotifier::Read, TQT_TQOBJECT(this) );
	connect( sn, TQT_SIGNAL(activated( int )), TQT_SLOT(slotReadPipe()) );

	connect( pingButton, TQT_SIGNAL(clicked()), TQT_SLOT(pingHosts()) );
	connect( acceptButton, TQT_SIGNAL(clicked()), TQT_SLOT(accept()) );
//	connect( helpButton, TQT_SIGNAL(clicked()), TQT_SLOT(slotHelp()) );
	connect( host_view, TQT_SIGNAL(doubleClicked(TQListViewItem *)), TQT_SLOT(accept()) );

	adjustGeometry();
}

/*
void ChooserDlg::slotHelp()
{
	KMessageBox::information(0,
	                         i18n("Choose a host, you want to work on,\n"
	                              "in the list or add one.\n\n"
	                              "After this box, you must press cancel\n"
	                              "in the Host Menu to enter a host. :("));
	iline->setFocus();
}
*/

void ChooserDlg::addHostname()
{
	if (!iline->text().isEmpty()) {
		GSendInt( G_Ch_RegisterHost );
		GSendStr( iline->text().latin1() );
		iline->clear();
	}
}

void ChooserDlg::pingHosts()
{
	GSendInt( G_Ch_Refresh );
}

void ChooserDlg::accept()
{
	if (focusWidget() == iline) {
		if (!iline->text().isEmpty()) {
			GSendInt( G_Ch_DirectChoice );
			GSendStr( iline->text().latin1() );
			iline->clear();
		}
		return;
	} else /*if (focusWidget() == host_view)*/ {
		TQListViewItem *item = host_view->currentItem();
		if (item) {
			GSendInt( G_Ready );
			GSendInt( ((ChooserListViewItem *)item)->id );
			::exit( EX_NORMAL );
		}
	}
}

void ChooserDlg::reject()
{
}

TQString ChooserDlg::recvStr()
{
	char *arr = GRecvStr();
	if (arr) {
		TQString str = TQString::fromLatin1( arr );
		free( arr );
		return str;
	} else
		return i18n("<unknown>");
}

TQListViewItem *ChooserDlg::findItem( int id )
{
	TQListViewItem *itm;
	for (TQListViewItemIterator it( host_view ); (itm = it.current()); ++it)
		if (((ChooserListViewItem *)itm)->id == id)
			return itm;
	return 0;
}

void ChooserDlg::slotReadPipe()
{
	int id;
	TQString nam, sts;

	int cmd = GRecvInt();
	switch (cmd) {
	case G_Ch_AddHost:
	case G_Ch_ChangeHost:
		id = GRecvInt();
		nam = recvStr();
		sts = recvStr();
		GRecvInt(); /* swallow willing for now */
		if (cmd == G_Ch_AddHost)
			host_view->insertItem(
				new ChooserListViewItem( host_view, id, nam, sts ) );
		else {
			TQListViewItem *itm = findItem( id );
			itm->setText( 0, nam );
			itm->setText( 1, sts );
		}
		break;
	case G_Ch_RemoveHost:
		delete findItem( GRecvInt() );
		break;
	case G_Ch_BadHost:
		KFMsgBox::box( this, TQMessageBox::Warning, i18n("Unknown host %1").arg( recvStr() ) );
		break;
	case G_Ch_Exit:
		done( ex_exit );
		break;
	default: /* XXX huuh ...? */
		break;
	}
}

#include "kchooser.moc"

#endif