Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / kdm / kfrontend / kchooser.cpp
blobac680b5073547a2c7ede8e7d6d2edc5d6eba1efa
1 /*
3 chooser widget for KDM
5 Copyright (C) 2002-2003 Oswald Buddenhagen <ossi@kde.org>
6 based on the chooser (C) 1999 by Harald Hoyer <Harald.Hoyer@RedHat.de>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "kchooser.h"
25 #include "kconsole.h"
26 #include "kdmconfig.h"
27 #include "kdm_greet.h"
29 #include <klocale.h>
31 #include <QLabel>
32 #include <QLineEdit>
33 #include <QPushButton>
34 #include <QSocketNotifier>
35 #include <QTreeWidget>
36 #include <QTreeWidgetItem>
37 #include <QHBoxLayout>
38 #include <QVBoxLayout>
40 #include <stdlib.h> // for free()
42 class ChooserListViewItem : public QTreeWidgetItem {
43 public:
44 ChooserListViewItem( QTreeWidget *parent, int _id, const QString &nam, const QString &sts )
45 : QTreeWidgetItem( parent, QStringList() << nam << sts ) { id = _id; }
47 int id;
51 ChooserDlg::ChooserDlg()
52 : inherited()
54 completeMenu( LOGIN_REMOTE_ONLY, ex_greet, i18n("&Local Login"), Qt::ALT+Qt::Key_L );
56 QBoxLayout *vbox = new QVBoxLayout( this );
58 QLabel *title = new QLabel( i18n("XDMCP Host Menu"), this );
59 title->setAlignment( Qt::AlignCenter );
60 vbox->addWidget( title );
62 host_view = new QTreeWidget( this );
63 host_view->setRootIsDecorated( false );
64 host_view->setUniformRowHeights( true );
65 host_view->setEditTriggers( QAbstractItemView::NoEditTriggers );
66 host_view->setColumnCount( 2 );
67 host_view->setHeaderLabels( QStringList() << i18n("Hostname") << i18n("Status") );
68 host_view->setColumnWidth( 0, fontMetrics().width( "login.crap.net" ) );
69 host_view->setMinimumWidth( fontMetrics().width( "login.crap.com Display not authorized to connect this server" ) );
70 host_view->setAllColumnsShowFocus( true );
71 vbox->addWidget( host_view );
73 iline = new QLineEdit( this );
74 iline->setEnabled( true );
75 QLabel *itxt = new QLabel( i18n("Hos&t:"), this );
76 itxt->setBuddy( iline );
77 QPushButton *addButton = new QPushButton( i18n("A&dd"), this );
78 connect( addButton, SIGNAL(clicked()), SLOT(addHostname()) );
79 QBoxLayout *hibox = new QHBoxLayout();
80 vbox->addLayout( hibox );
81 hibox->setParent( vbox );
82 hibox->addWidget( itxt );
83 hibox->addWidget( iline );
84 hibox->addWidget( addButton );
86 // Buttons
87 QPushButton *acceptButton = new QPushButton( i18n("&Accept"), this );
88 acceptButton->setDefault( true );
89 QPushButton *pingButton = new QPushButton( i18n("&Refresh"), this );
91 QBoxLayout *hbox = new QHBoxLayout();
92 vbox->addLayout( hbox );
93 hbox->setParent( vbox );
94 hbox->setSpacing( 20 );
95 hbox->addWidget( acceptButton );
96 hbox->addWidget( pingButton );
97 hbox->addStretch( 1 );
99 if (optMenu) {
100 QPushButton *menuButton = new QPushButton( i18n("&Menu"), this );
101 menuButton->setMenu( optMenu );
102 hbox->addWidget( menuButton );
103 hbox->addStretch( 1 );
106 // QPushButton *helpButton = new QPushButton( i18n("&Help"), this );
107 // hbox->addWidget( helpButton );
109 #ifdef WITH_KDM_XCONSOLE
110 if (consoleView)
111 vbox->addWidget( consoleView );
112 #endif
114 sn = new QSocketNotifier( rfd, QSocketNotifier::Read, this );
115 connect( sn, SIGNAL(activated( int )), SLOT(slotReadPipe()) );
117 connect( pingButton, SIGNAL(clicked()), SLOT(pingHosts()) );
118 connect( acceptButton, SIGNAL(clicked()), SLOT(accept()) );
119 // connect( helpButton, SIGNAL(clicked()), SLOT(slotHelp()) );
120 connect( host_view, SIGNAL(itemActivated( QTreeWidgetItem *, int )), SLOT(accept()) );
124 void ChooserDlg::slotHelp()
126 KMessageBox::information( 0,
127 i18n("Choose a host, you want to work on,\n"
128 "in the list or add one.\n\n"
129 "After this box, you must press cancel\n"
130 "in the Host Menu to enter a host. :("));
131 iline->setFocus();
135 void ChooserDlg::addHostname()
137 if (!iline->text().isEmpty()) {
138 gSendInt( G_Ch_RegisterHost );
139 gSendStr( iline->text().toLatin1() );
140 iline->clear();
144 void ChooserDlg::pingHosts()
146 gSendInt( G_Ch_Refresh );
149 void ChooserDlg::accept()
151 if (focusWidget() == iline) {
152 if (!iline->text().isEmpty()) {
153 gSendInt( G_Ch_DirectChoice );
154 gSendStr( iline->text().toLatin1() );
155 iline->clear();
157 return;
158 } else /*if (focusWidget() == host_view)*/ {
159 QTreeWidgetItem *item = host_view->currentItem();
160 if (item) {
161 gSendInt( G_Ready );
162 gSendInt( ((ChooserListViewItem *)item)->id );
163 ::exit( EX_NORMAL );
168 void ChooserDlg::reject()
172 QString ChooserDlg::recvStr()
174 char *arr = gRecvStr();
175 if (arr) {
176 QString str = QString::fromLatin1( arr );
177 free( arr );
178 return str;
179 } else
180 return i18n("<unknown>"); //krazy:exclude=i18ncheckarg
183 ChooserListViewItem *ChooserDlg::findItem( int id )
185 for (int i = 0, rc = host_view->model()->rowCount(); i < rc; i++) {
186 ChooserListViewItem *itm = static_cast<ChooserListViewItem *>(
187 host_view->topLevelItem( i ));
188 if (itm->id == id)
189 return itm;
191 return 0;
194 void ChooserDlg::slotReadPipe()
196 int id;
197 QString nam, sts;
199 int cmd = gRecvInt();
200 switch (cmd) {
201 case G_Ch_AddHost:
202 case G_Ch_ChangeHost:
203 id = gRecvInt();
204 nam = recvStr();
205 sts = recvStr();
206 gRecvInt(); /* swallow willing for now */
207 if (cmd == G_Ch_AddHost)
208 new ChooserListViewItem( host_view, id, nam, sts );
209 else {
210 QTreeWidgetItem *itm = findItem( id );
211 itm->setText( 0, nam );
212 itm->setText( 1, sts );
214 break;
215 case G_Ch_RemoveHost:
216 delete findItem( gRecvInt() );
217 break;
218 case G_Ch_BadHost:
219 KFMsgBox::box( this, QMessageBox::Warning, i18n("Unknown host %1", recvStr() ) );
220 break;
221 case G_Ch_Exit:
222 done( ex_exit );
223 break;
224 default: /* XXX huuh ...? */
225 break;
229 #include "kchooser.moc"