Proof-reading.
[kdenetwork.git] / krdc / nx / nxview.cpp
blob1f7854b6fe688065962f0300709a0c7208a5c344
1 /****************************************************************************
2 **
3 ** Copyright (C) 2008 David Gross <gdavid.devel@gmail.com>
4 **
5 ** This file is part of KDE.
6 **
7 ** This program is free software; you can redistribute it and/or modify
8 ** it under the terms of the GNU General Public License as published by
9 ** the Free Software Foundation; either version 2 of the License, or
10 ** (at your option) any later version.
12 ** This program is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ** GNU General Public License for more details.
17 ** You should have received a copy of the GNU General Public License
18 ** along with this program; see the file COPYING. If not, write to
19 ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 ** Boston, MA 02110-1301, USA.
22 ****************************************************************************/
24 #include "nxview.h"
25 #include "settings.h"
27 #include <nxcl/nxdata.h>
29 #include <KInputDialog>
30 #include <KMessageBox>
31 #include <KPasswordDialog>
33 #include <QEvent>
34 #include <QMetaType>
36 NxView::NxView(QWidget *parent, const KUrl &url, KConfigGroup configGroup)
37 : RemoteView(parent),
38 m_quitFlag(false),
39 m_container(NULL),
40 m_hostPreferences(NULL)
42 m_url = url;
43 m_host = url.host();
44 m_port = url.port();
46 if (m_port <= 0 || m_port >= 65536)
47 m_port = TCP_PORT_NX;
49 m_container = new QX11EmbedContainer(this);
50 m_container->installEventFilter(this);
52 qRegisterMetaType<QList<nxcl::NXResumeData> >("QList<nxcl::NXResumeData>");
54 m_clientThread.setCallbacks(&m_callbacks);
56 connect(&m_clientThread, SIGNAL(hasXid(int)), this, SLOT(hasXid(int)));
57 connect(&m_callbacks, SIGNAL(progress(int, QString)), this, SLOT(handleProgress(int, QString)));
58 connect(&m_callbacks, SIGNAL(suspendedSessions(QList<nxcl::NXResumeData>)), this, SLOT(handleSuspendedSessions(QList<nxcl::NXResumeData>)));
59 connect(&m_callbacks, SIGNAL(noSessions()), this, SLOT(handleNoSessions()));
60 connect(&m_callbacks, SIGNAL(atCapacity()), this, SLOT(handleAtCapacity()));
61 connect(&m_resumeSessions, SIGNAL(newSession()), this, SLOT(handleNewSession()));
62 connect(&m_resumeSessions, SIGNAL(resumeSession(QString)), this, SLOT(handleResumeSession(QString)));
64 m_hostPreferences = new NxHostPreferences(configGroup, this);
67 NxView::~NxView()
71 // filter out key and mouse events to the container if we are view only
72 //FIXME: X11 events are passed to the app before getting caught in the Qt event processing
73 bool NxView::eventFilter(QObject *obj, QEvent *event)
75 if (m_viewOnly) {
76 if (event->type() == QEvent::KeyPress ||
77 event->type() == QEvent::KeyRelease ||
78 event->type() == QEvent::MouseButtonDblClick ||
79 event->type() == QEvent::MouseButtonPress ||
80 event->type() == QEvent::MouseButtonRelease ||
81 event->type() == QEvent::MouseMove)
82 return true;
85 return RemoteView::eventFilter(obj, event);
88 void NxView::startQuitting()
90 kDebug(5013) << "about to quit";
92 const bool connected = status() == RemoteView::Connected;
93 setStatus(Disconnecting);
94 m_quitFlag = true;
96 if (connected)
97 m_clientThread.stop();
98 else
99 m_clientThread.quit();
101 m_clientThread.wait(500);
102 setStatus(Disconnected);
103 m_container->discardClient();
106 bool NxView::isQuitting()
108 return m_quitFlag;
111 bool NxView::start()
113 m_clientThread.setResolution(m_hostPreferences->width(), m_hostPreferences->height());
114 m_clientThread.setDesktopType(m_hostPreferences->desktopType());
115 m_clientThread.setKeyboardLayout(m_hostPreferences->keyboardLayout());
116 m_clientThread.setPrivateKey(m_hostPreferences->privateKey());
118 m_container->show();
120 if (m_hostPreferences->walletSupport()) {
121 if (m_url.userName().isEmpty()) {
122 QString userName;
123 bool ok = false;
125 userName = KInputDialog::getText(i18n("Enter Username"),
126 i18n("Please enter the username you would like to use for login."),
127 QString(), &ok, this);
129 if (ok)
130 m_url.setUserName(userName);
133 if (!m_url.userName().isEmpty()) {
134 QString walletPassword = readWalletPassword();
136 if (!walletPassword.isNull())
137 m_url.setPassword(walletPassword);
138 else {
139 KPasswordDialog dialog(this);
140 dialog.setPrompt(i18n("Access to the system requires a password."));
141 if (dialog.exec() == KPasswordDialog::Accepted) {
142 m_url.setPassword(dialog.password());
144 if (m_hostPreferences->walletSupport())
145 saveWalletPassword(dialog.password());
151 m_clientThread.setHost(m_host);
152 m_clientThread.setPort(m_port);
153 m_clientThread.setUserName(m_url.userName());
154 m_clientThread.setPassword(m_url.password());
155 m_clientThread.setResolution(m_hostPreferences->width(), m_hostPreferences->height());
157 setStatus(Connecting);
158 m_clientThread.start();
160 connect(m_container, SIGNAL(clientIsEmbedded()), SLOT(connectionOpened()));
161 connect(m_container, SIGNAL(clientClosed()), SLOT(connectionClosed()));
163 return true;
166 HostPreferences* NxView::hostPreferences()
168 return m_hostPreferences;
171 QSize NxView::framebufferSize()
173 return m_container->minimumSizeHint();
176 QSize NxView::sizeHint() const
178 return maximumSize();
182 void NxView::switchFullscreen(bool on)
184 setGrabAllKeys(on);
187 void NxView::setGrabAllKeys(bool grabAllKeys)
189 m_grabAllKeys = grabAllKeys;
191 if (grabAllKeys) {
192 m_keyboardIsGrabbed = true;
193 m_container->grabKeyboard();
194 } else if (m_keyboardIsGrabbed)
195 m_container->releaseKeyboard();
198 void NxView::hasXid(int xid)
200 m_container->embedClient(xid);
203 void NxView::handleProgress(int id, QString msg)
205 Q_UNUSED(msg);
206 switch (id) {
207 case NXCL_AUTH_FAILED:
208 KMessageBox::error(this, i18n("The authentication key is invalid."), i18n("Invalid authentication key"));
209 break;
210 case NXCL_LOGIN_FAILED:
211 KMessageBox::error(this, i18n("The username or password that you have entered is invalid."), i18n("Invalid username or password"));
212 break;
213 case NXCL_HOST_KEY_VERIFAILED:
214 KMessageBox::error(this, i18n("The host key verification has failed."), i18n("Host key verification failed"));
215 break;
216 case NXCL_PROCESS_ERROR:
217 KMessageBox::error(this, i18n("An error has occurred during the connection to the NX server."), i18n("Process error"));
218 break;
219 default:
220 break;
224 void NxView::handleSuspendedSessions(QList<nxcl::NXResumeData> sessions)
226 if (!m_resumeSessions.empty())
227 m_resumeSessions.clear();
229 m_resumeSessions.addSessions(sessions);
230 m_resumeSessions.show();
233 void NxView::handleNoSessions()
235 m_clientThread.setSuspended(false);
236 m_clientThread.startSession();
239 void NxView::handleAtCapacity()
241 KMessageBox::error(this, i18n("This NX server is running at capacity."), i18n("Server at capacity"));
244 void NxView::handleNewSession()
246 m_clientThread.setSuspended(false);
247 m_clientThread.startSession();
250 void NxView::handleResumeSession(QString id)
252 m_clientThread.setId(id);
253 m_clientThread.setSuspended(true);
254 m_clientThread.startSession();
257 void NxView::connectionOpened()
259 kDebug(5013) << "Connection opened";
260 QSize size(m_hostPreferences->width(), m_hostPreferences->height());
261 kDebug(5013) << "Size hint: " << size.width() << size.height();
262 setStatus(Connected);
263 setFixedSize(size);
264 resize(size);
265 m_container->setFixedSize(size);
266 emit framebufferSizeChanged(size.width(), size.height());
267 emit connected();
268 setFocus();
271 void NxView::connectionClosed()
273 emit disconnected();
274 setStatus(Disconnected);
275 m_quitFlag = true;
278 #include "nxview.moc"