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.
26 #include "kdmconfig.h"
27 #include "kdm_greet.h"
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
{
44 ChooserListViewItem( QTreeWidget
*parent
, int _id
, const QString
&nam
, const QString
&sts
)
45 : QTreeWidgetItem( parent
, QStringList() << nam
<< sts
) { id
= _id
; }
51 ChooserDlg::ChooserDlg()
54 completeMenu( LOGIN_REMOTE_ONLY
, ex_greet
, i18nc("@action:inmenu", "&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()
68 << i18nc("@title:column", "Hostname")
69 << i18nc("@title:column ... of named host", "Status") );
70 host_view
->setColumnWidth( 0, fontMetrics().width( "login.crap.net" ) );
71 host_view
->setMinimumWidth( fontMetrics().width( "login.crap.com Display not authorized to connect this server" ) );
72 host_view
->setAllColumnsShowFocus( true );
73 vbox
->addWidget( host_view
);
75 iline
= new QLineEdit( this );
76 iline
->setEnabled( true );
77 QLabel
*itxt
= new QLabel( i18nc("XDMCP server", "Hos&t:"), this );
78 itxt
->setBuddy( iline
);
79 QPushButton
*addButton
= new QPushButton( i18nc("@action:button", "A&dd"), this );
80 connect( addButton
, SIGNAL(clicked()), SLOT(addHostname()) );
81 QBoxLayout
*hibox
= new QHBoxLayout();
82 vbox
->addLayout( hibox
);
83 hibox
->addWidget( itxt
);
84 hibox
->addWidget( iline
);
85 hibox
->addWidget( addButton
);
88 QPushButton
*acceptButton
= new QPushButton( i18nc("@action:button", "&Accept"), this );
89 acceptButton
->setDefault( true );
90 QPushButton
*pingButton
= new QPushButton( i18nc("@action:button", "&Refresh"), this );
92 QBoxLayout
*hbox
= new QHBoxLayout();
93 vbox
->addLayout( hbox
);
94 hbox
->setSpacing( 20 );
95 hbox
->addWidget( acceptButton
);
96 hbox
->addWidget( pingButton
);
97 hbox
->addStretch( 1 );
100 QPushButton
*menuButton
= new QPushButton( i18nc("@action:button", "&Menu"), this );
101 menuButton
->setMenu( optMenu
);
102 hbox
->addWidget( menuButton
);
103 hbox
->addStretch( 1 );
106 // QPushButton *helpButton = new QPushButton( i18nc("@action:button", "&Help"), this );
107 // hbox->addWidget( helpButton );
109 #ifdef WITH_KDM_XCONSOLE
111 vbox
->addWidget( consoleView
);
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. :("));
135 void ChooserDlg::addHostname()
137 if (!iline
->text().isEmpty()) {
138 gSendInt( G_Ch_RegisterHost
);
139 gSendStr( iline
->text().toLatin1() );
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() );
158 } else /*if (focusWidget() == host_view)*/ {
159 QTreeWidgetItem
*item
= host_view
->currentItem();
162 gSendInt( ((ChooserListViewItem
*)item
)->id
);
168 void ChooserDlg::reject()
172 QString
ChooserDlg::recvStr()
174 char *arr
= gRecvStr();
176 QString str
= QString::fromLatin1( arr
);
180 return i18nc("hostname or status", "<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
));
194 void ChooserDlg::slotReadPipe()
199 int cmd
= gRecvInt();
202 case G_Ch_ChangeHost
:
206 gRecvInt(); /* swallow willing for now */
207 if (cmd
== G_Ch_AddHost
)
208 new ChooserListViewItem( host_view
, id
, nam
, sts
);
210 QTreeWidgetItem
*itm
= findItem( id
);
211 itm
->setText( 0, nam
);
212 itm
->setText( 1, sts
);
215 case G_Ch_RemoveHost
:
216 delete findItem( gRecvInt() );
219 KFMsgBox::box( this, QMessageBox::Warning
, i18n("Unknown host %1", recvStr() ) );
224 default: /* XXX huuh ...? */
229 #include "kchooser.moc"