~ubuntu-branches/ubuntu/gutsy/kdebase-workspace/gutsy

« back to all changes in this revision

Viewing changes to kdm/kfrontend/kchooser.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-09-05 20:45:14 UTC
  • Revision ID: james.westby@ubuntu.com-20070905204514-632hhspl0nvrc84i
Tags: upstream-3.93.0
ImportĀ upstreamĀ versionĀ 3.93.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
chooser widget for KDM
 
4
 
 
5
Copyright (C) 2002-2003 Oswald Buddenhagen <ossi@kde.org>
 
6
based on the chooser (C) 1999 by Harald Hoyer <Harald.Hoyer@RedHat.de>
 
7
 
 
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.
 
12
 
 
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.
 
17
 
 
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.
 
21
 
 
22
*/
 
23
 
 
24
#include "kchooser.h"
 
25
#include "kconsole.h"
 
26
#include "kdmconfig.h"
 
27
#include "kdm_greet.h"
 
28
 
 
29
#include <klocale.h>
 
30
 
 
31
#include <QLabel>
 
32
#include <QLineEdit>
 
33
#include <QPushButton>
 
34
#include <QSocketNotifier>
 
35
#include <QTreeWidget>
 
36
#include <QTreeWidgetItem>
 
37
#include <QHBoxLayout>
 
38
#include <QVBoxLayout>
 
39
 
 
40
#include <stdlib.h> // for free()
 
41
 
 
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; }
 
46
 
 
47
        int id;
 
48
};
 
49
 
 
50
 
 
51
ChooserDlg::ChooserDlg()
 
52
        : inherited()
 
53
{
 
54
        completeMenu( LOGIN_REMOTE_ONLY, ex_greet, i18n("&Local Login"), Qt::ALT+Qt::Key_L );
 
55
 
 
56
        QBoxLayout *vbox = new QVBoxLayout( this );
 
57
 
 
58
        QLabel *title = new QLabel( i18n("XDMCP Host Menu"), this );
 
59
        title->setAlignment( Qt::AlignCenter );
 
60
        vbox->addWidget( title );
 
61
 
 
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 );
 
72
 
 
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 );
 
85
 
 
86
        // Buttons
 
87
        QPushButton *acceptButton = new QPushButton( i18n("&Accept"), this );
 
88
        acceptButton->setDefault( true );
 
89
        QPushButton *pingButton = new QPushButton( i18n("&Refresh"), this );
 
90
 
 
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 );
 
98
 
 
99
        if (optMenu) {
 
100
                QPushButton *menuButton = new QPushButton( i18n("&Menu"), this );
 
101
                menuButton->setMenu( optMenu );
 
102
                hbox->addWidget( menuButton );
 
103
                hbox->addStretch( 1 );
 
104
        }
 
105
 
 
106
//      QPushButton *helpButton = new QPushButton( i18n("&Help"), this );
 
107
//      hbox->addWidget( helpButton );
 
108
 
 
109
#ifdef WITH_KDM_XCONSOLE
 
110
        if (consoleView)
 
111
                vbox->addWidget( consoleView );
 
112
#endif
 
113
 
 
114
        sn = new QSocketNotifier( rfd, QSocketNotifier::Read, this );
 
115
        connect( sn, SIGNAL(activated( int )), SLOT(slotReadPipe()) );
 
116
 
 
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()) );
 
121
 
 
122
        adjustGeometry();
 
123
}
 
124
 
 
125
/*
 
126
void ChooserDlg::slotHelp()
 
127
{
 
128
        KMessageBox::information( 0,
 
129
                                  i18n("Choose a host, you want to work on,\n"
 
130
                                       "in the list or add one.\n\n"
 
131
                                       "After this box, you must press cancel\n"
 
132
                                       "in the Host Menu to enter a host. :("));
 
133
        iline->setFocus();
 
134
}
 
135
*/
 
136
 
 
137
void ChooserDlg::addHostname()
 
138
{
 
139
        if (!iline->text().isEmpty()) {
 
140
                gSendInt( G_Ch_RegisterHost );
 
141
                gSendStr( iline->text().toLatin1() );
 
142
                iline->clear();
 
143
        }
 
144
}
 
145
 
 
146
void ChooserDlg::pingHosts()
 
147
{
 
148
        gSendInt( G_Ch_Refresh );
 
149
}
 
150
 
 
151
void ChooserDlg::accept()
 
152
{
 
153
        if (focusWidget() == iline) {
 
154
                if (!iline->text().isEmpty()) {
 
155
                        gSendInt( G_Ch_DirectChoice );
 
156
                        gSendStr( iline->text().toLatin1() );
 
157
                        iline->clear();
 
158
                }
 
159
                return;
 
160
        } else /*if (focusWidget() == host_view)*/ {
 
161
                QTreeWidgetItem *item = host_view->currentItem();
 
162
                if (item) {
 
163
                        gSendInt( G_Ready );
 
164
                        gSendInt( ((ChooserListViewItem *)item)->id );
 
165
                        ::exit( EX_NORMAL );
 
166
                }
 
167
        }
 
168
}
 
169
 
 
170
void ChooserDlg::reject()
 
171
{
 
172
}
 
173
 
 
174
QString ChooserDlg::recvStr()
 
175
{
 
176
        char *arr = gRecvStr();
 
177
        if (arr) {
 
178
                QString str = QString::fromLatin1( arr );
 
179
                free( arr );
 
180
                return str;
 
181
        } else
 
182
                return i18n("<unknown>"); //krazy:exclude=i18ncheckarg
 
183
}
 
184
 
 
185
ChooserListViewItem *ChooserDlg::findItem( int id )
 
186
{
 
187
        for (int i = 0, rc = host_view->model()->rowCount(); i < rc; i++) {
 
188
                ChooserListViewItem *itm = static_cast<ChooserListViewItem *>(
 
189
                        host_view->topLevelItem( i ));
 
190
                if (itm->id == id)
 
191
                        return itm;
 
192
        }
 
193
        return 0;
 
194
}
 
195
 
 
196
void ChooserDlg::slotReadPipe()
 
197
{
 
198
        int id;
 
199
        QString nam, sts;
 
200
 
 
201
        int cmd = gRecvInt();
 
202
        switch (cmd) {
 
203
        case G_Ch_AddHost:
 
204
        case G_Ch_ChangeHost:
 
205
                id = gRecvInt();
 
206
                nam = recvStr();
 
207
                sts = recvStr();
 
208
                gRecvInt(); /* swallow willing for now */
 
209
                if (cmd == G_Ch_AddHost)
 
210
                        new ChooserListViewItem( host_view, id, nam, sts );
 
211
                else {
 
212
                        QTreeWidgetItem *itm = findItem( id );
 
213
                        itm->setText( 0, nam );
 
214
                        itm->setText( 1, sts );
 
215
                }
 
216
                break;
 
217
        case G_Ch_RemoveHost:
 
218
                delete findItem( gRecvInt() );
 
219
                break;
 
220
        case G_Ch_BadHost:
 
221
                KFMsgBox::box( this, QMessageBox::Warning, i18n("Unknown host %1", recvStr() ) );
 
222
                break;
 
223
        case G_Ch_Exit:
 
224
                done( ex_exit );
 
225
                break;
 
226
        default: /* XXX huuh ...? */
 
227
                break;
 
228
        }
 
229
}
 
230
 
 
231
#include "kchooser.moc"