~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kdm/kfrontend/kchooser.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

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 <QApplication>
 
32
#include <QLabel>
 
33
#include <QLineEdit>
 
34
#include <QPushButton>
 
35
#include <QSocketNotifier>
 
36
#include <QTreeWidget>
 
37
#include <QTreeWidgetItem>
 
38
#include <QHBoxLayout>
 
39
#include <QVBoxLayout>
 
40
 
 
41
#include <stdlib.h> // for free()
 
42
 
 
43
#define CHOOSE_TO 20 // inactivity timeout for reserve displays
 
44
#define SECONDS 1000 // reduce to 100 to speed up testing
 
45
 
 
46
 
 
47
class ChooserListViewItem : public QTreeWidgetItem {
 
48
  public:
 
49
    ChooserListViewItem(QTreeWidget *parent, int _id, const QString &nam, const QString &sts)
 
50
        : QTreeWidgetItem(parent, QStringList() << nam << sts) { id = _id; }
 
51
 
 
52
    int id;
 
53
};
 
54
 
 
55
 
 
56
ChooserDlg::ChooserDlg()
 
57
    : inherited()
 
58
{
 
59
    completeMenu(LOGIN_REMOTE_ONLY, ex_greet, i18nc("@action:inmenu", "&Local Login"), Qt::ALT + Qt::Key_L);
 
60
 
 
61
    QBoxLayout *vbox = new QVBoxLayout(this);
 
62
 
 
63
    QLabel *title = new QLabel(i18n("XDMCP Host Menu"), this);
 
64
    title->setAlignment(Qt::AlignCenter);
 
65
    vbox->addWidget(title);
 
66
 
 
67
    host_view = new QTreeWidget(this);
 
68
    host_view->setRootIsDecorated(false);
 
69
    host_view->setUniformRowHeights(true);
 
70
    host_view->setEditTriggers(QAbstractItemView::NoEditTriggers);
 
71
    host_view->setColumnCount(2);
 
72
    host_view->setHeaderLabels(QStringList()
 
73
        << i18nc("@title:column", "Hostname")
 
74
        << i18nc("@title:column ... of named host", "Status"));
 
75
    host_view->setColumnWidth(0, fontMetrics().width("login.domain.com"));
 
76
    host_view->setMinimumWidth(fontMetrics().width("login.domain.com Display not authorized to connect this server"));
 
77
    host_view->setAllColumnsShowFocus(true);
 
78
    vbox->addWidget(host_view);
 
79
 
 
80
    iline = new QLineEdit(this);
 
81
    iline->setEnabled(true);
 
82
    QLabel *itxt = new QLabel(i18nc("XDMCP server", "Hos&t:"), this);
 
83
    itxt->setBuddy(iline);
 
84
    QPushButton *addButton = new QPushButton(i18nc("@action:button", "A&dd"), this);
 
85
    connect(addButton, SIGNAL(clicked()), SLOT(addHostname()));
 
86
    QBoxLayout *hibox = new QHBoxLayout();
 
87
    vbox->addLayout(hibox);
 
88
    hibox->addWidget(itxt);
 
89
    hibox->addWidget(iline);
 
90
    hibox->addWidget(addButton);
 
91
 
 
92
    // Buttons
 
93
    QPushButton *acceptButton = new QPushButton(i18nc("@action:button", "&Accept"), this);
 
94
    acceptButton->setDefault(true);
 
95
    QPushButton *pingButton = new QPushButton(i18nc("@action:button", "&Refresh"), this);
 
96
 
 
97
    QBoxLayout *hbox = new QHBoxLayout();
 
98
    vbox->addLayout(hbox);
 
99
    hbox->setSpacing(20);
 
100
    hbox->addWidget(acceptButton);
 
101
    hbox->addWidget(pingButton);
 
102
    hbox->addStretch(1);
 
103
 
 
104
    if (optMenu) {
 
105
        QPushButton *menuButton = new QPushButton(i18nc("@action:button", "&Menu"), this);
 
106
        menuButton->setMenu(optMenu);
 
107
        hbox->addWidget(menuButton);
 
108
        hbox->addStretch(1);
 
109
    }
 
110
 
 
111
//    QPushButton *helpButton = new QPushButton(i18nc("@action:button", "&Help"), this);
 
112
//    hbox->addWidget(helpButton);
 
113
 
 
114
#ifdef WITH_KDM_XCONSOLE
 
115
    if (consoleView)
 
116
        vbox->addWidget(consoleView);
 
117
#endif
 
118
 
 
119
    sn = new QSocketNotifier(rfd, QSocketNotifier::Read, this);
 
120
    connect(sn, SIGNAL(activated(int)), SLOT(slotReadPipe()));
 
121
 
 
122
    connect(pingButton, SIGNAL(clicked()), SLOT(pingHosts()));
 
123
    connect(acceptButton, SIGNAL(clicked()), SLOT(accept()));
 
124
//    connect(helpButton, SIGNAL(clicked()), SLOT(slotHelp()));
 
125
    connect(host_view, SIGNAL(itemActivated(QTreeWidgetItem *, int)), SLOT(accept()));
 
126
 
 
127
    if (_isReserve) {
 
128
        connect(&timer, SIGNAL(timeout()), SLOT(slotTimeout()));
 
129
        connect(qApp, SIGNAL(activity()), SLOT(slotActivity()));
 
130
        slotActivity();
 
131
    }
 
132
}
 
133
 
 
134
void
 
135
ChooserDlg::slotTimeout()
 
136
{
 
137
    ::exit(EX_RESERVE);
 
138
}
 
139
 
 
140
void
 
141
ChooserDlg::slotActivity()
 
142
{
 
143
    timer.start(CHOOSE_TO * SECONDS);
 
144
}
 
145
 
 
146
/*
 
147
void ChooserDlg::slotHelp()
 
148
{
 
149
    KMessageBox::information(0,
 
150
                              i18n("Choose a host, you want to work on,\n"
 
151
                                   "in the list or add one.\n\n"
 
152
                                   "After this box, you must press cancel\n"
 
153
                                   "in the Host Menu to enter a host. :("));
 
154
    iline->setFocus();
 
155
}
 
156
*/
 
157
 
 
158
void ChooserDlg::addHostname()
 
159
{
 
160
    if (!iline->text().isEmpty()) {
 
161
        gSendInt(G_Ch_RegisterHost);
 
162
        gSendStr(iline->text().toLatin1());
 
163
        iline->clear();
 
164
    }
 
165
}
 
166
 
 
167
void ChooserDlg::pingHosts()
 
168
{
 
169
    gSendInt(G_Ch_Refresh);
 
170
}
 
171
 
 
172
void ChooserDlg::accept()
 
173
{
 
174
    if (focusWidget() == iline) {
 
175
        if (!iline->text().isEmpty()) {
 
176
            gSendInt(G_Ch_DirectChoice);
 
177
            gSendStr(iline->text().toLatin1());
 
178
            iline->clear();
 
179
        }
 
180
        return;
 
181
    } else /*if (focusWidget() == host_view)*/ {
 
182
        QTreeWidgetItem *item = host_view->currentItem();
 
183
        if (item) {
 
184
            gSendInt(G_Ready);
 
185
            gSendInt(((ChooserListViewItem *)item)->id);
 
186
            ::exit(EX_NORMAL);
 
187
        }
 
188
    }
 
189
}
 
190
 
 
191
void ChooserDlg::reject()
 
192
{
 
193
}
 
194
 
 
195
QString ChooserDlg::recvStr()
 
196
{
 
197
    char *arr = gRecvStr();
 
198
    if (arr) {
 
199
        QString str = QString::fromLatin1(arr);
 
200
        free(arr);
 
201
        return str;
 
202
    } else
 
203
        return i18nc("hostname or status", "<unknown>"); //krazy:exclude=i18ncheckarg
 
204
}
 
205
 
 
206
ChooserListViewItem *ChooserDlg::findItem(int id)
 
207
{
 
208
    for (int i = 0, rc = host_view->model()->rowCount(); i < rc; i++) {
 
209
        ChooserListViewItem *itm = static_cast<ChooserListViewItem *>(
 
210
            host_view->topLevelItem(i));
 
211
        if (itm->id == id)
 
212
            return itm;
 
213
    }
 
214
    return 0;
 
215
}
 
216
 
 
217
void ChooserDlg::slotReadPipe()
 
218
{
 
219
    int id;
 
220
    QString nam, sts;
 
221
 
 
222
    int cmd = gRecvInt();
 
223
    switch (cmd) {
 
224
    case G_Ch_AddHost:
 
225
    case G_Ch_ChangeHost:
 
226
        id = gRecvInt();
 
227
        nam = recvStr();
 
228
        sts = recvStr();
 
229
        gRecvInt(); /* swallow willing for now */
 
230
        if (cmd == G_Ch_AddHost) {
 
231
            new ChooserListViewItem(host_view, id, nam, sts);
 
232
        } else {
 
233
            QTreeWidgetItem *itm = findItem(id);
 
234
            itm->setText(0, nam);
 
235
            itm->setText(1, sts);
 
236
        }
 
237
        break;
 
238
    case G_Ch_RemoveHost:
 
239
        delete findItem(gRecvInt());
 
240
        break;
 
241
    case G_Ch_BadHost:
 
242
        KFMsgBox::box(this, QMessageBox::Warning, i18n("Unknown host %1", recvStr()));
 
243
        break;
 
244
    case G_Ch_Exit:
 
245
        done(ex_exit);
 
246
        break;
 
247
    default: /* XXX huuh ...? */
 
248
        break;
 
249
    }
 
250
}
 
251
 
 
252
#include "kchooser.moc"