make it working with with all tested browsers
[kdenetwork.git] / krdc / rdp / rdpview.cpp
blob3881e6bf78a6a669e9e803561f762b66cfe6493c
1 /****************************************************************************
2 **
3 ** Copyright (C) 2002 Arend van Beelen jr. <arend@auton.nl>
4 ** Copyright (C) 2007 Urs Wolfer <uwolfer @ kde.org>
5 **
6 ** This file is part of KDE.
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.
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; see the file COPYING. If not, write to
20 ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 ** Boston, MA 02110-1301, USA.
23 ****************************************************************************/
25 #include "rdpview.h"
27 #include "settings.h"
29 #include <KInputDialog>
30 #include <KMessageBox>
31 #include <KPasswordDialog>
33 #include <QX11EmbedContainer>
34 #include <QEvent>
36 RdpView::RdpView(QWidget *parent,
37 const KUrl &url,
38 const QString &user, const QString &password,
39 int flags, const QString &domain,
40 const QString &shell, const QString &directory,
41 const QString &caption)
42 : RemoteView(parent),
43 m_user(user),
44 m_password(password),
45 m_flags(flags),
46 m_domain(domain),
47 m_shell(shell),
48 m_directory(directory),
49 m_quitFlag(false),
50 m_process(NULL),
51 m_caption(caption)
53 m_url = url;
54 m_host = url.host();
55 m_port = url.port();
57 if (m_port <= 0) {
58 m_port = TCP_PORT_RDP;
61 m_container = new QX11EmbedContainer(this);
62 m_container->installEventFilter(this);
65 RdpView::~RdpView()
67 startQuitting();
70 // filter out key and mouse events to the container if we are view only
71 //FIXME: X11 events are passed to the app before getting caught in the Qt event processing
72 bool RdpView::eventFilter(QObject *obj, QEvent *event)
74 if (m_viewOnly) {
75 if (event->type() == QEvent::KeyPress ||
76 event->type() == QEvent::KeyRelease ||
77 event->type() == QEvent::MouseButtonDblClick ||
78 event->type() == QEvent::MouseButtonPress ||
79 event->type() == QEvent::MouseButtonRelease ||
80 event->type() == QEvent::MouseMove)
81 return true;
83 return RemoteView::eventFilter(obj, event);
86 QSize RdpView::framebufferSize()
88 return m_container->minimumSizeHint();
91 QSize RdpView::sizeHint() const
93 return maximumSize();
96 void RdpView::startQuitting()
98 kDebug(5012) << "About to quit";
99 m_quitFlag = true;
100 if (m_process) {
101 m_process->terminate();
102 m_process->waitForFinished(1000);
103 m_container->discardClient();
107 bool RdpView::isQuitting()
109 return m_quitFlag;
112 bool RdpView::start()
114 m_hostPreferences = new RdpHostPreferences(m_url.prettyUrl(KUrl::RemoveTrailingSlash), false, this);
116 m_container->show();
117 m_container->setWindowTitle(m_caption);
119 if (m_hostPreferences->walletSupport()) {
120 if (m_url.userName().isEmpty()) {
121 QString userName;
122 bool ok = true;
124 userName = KInputDialog::getText(i18n("Enter Username"),
125 i18n("Please enter the username you would like to use for login."),
126 QString(), &ok, this);
128 if (ok)
129 m_url.setUserName(userName);
132 if (!m_url.userName().isEmpty()) {
133 QString walletPassword = readWalletPassword();
135 if (!walletPassword.isNull())
136 m_url.setPassword(walletPassword);
137 else {
138 KPasswordDialog dialog(this);
139 dialog.setPixmap(KIcon("dialog-password").pixmap(48));
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_process = new QProcess(m_container);
153 QStringList arguments;
154 arguments << "-g" << (QString::number(m_hostPreferences->width()) + 'x' +
155 QString::number(m_hostPreferences->height()));
156 arguments << "-k" << m_hostPreferences->keyboardLayout();
158 if (!m_url.userName().isEmpty())
159 arguments << "-u" << m_url.userName();
160 else if (!Settings::sendCurrentUserName())
161 arguments << "-u" << "";
163 if (!m_url.password().isNull())
164 arguments << "-p" << m_url.password();
166 arguments << "-X" << QString::number(m_container->winId());
167 arguments << "-a" << QString::number((m_hostPreferences->colorDepth() + 1) * 8);
169 QString sound;
170 switch (m_hostPreferences->sound()) {
171 case 0:
172 sound = "local";
173 break;
174 case 1:
175 sound = "remote";
176 break;
177 case 2:
178 default:
179 sound = "off";
181 arguments << "-r" << "sound:" + sound;
183 if (!m_hostPreferences->extraOptions().isEmpty()) {
184 QStringList additionalArguments = m_hostPreferences->extraOptions().split(' ');
185 arguments += additionalArguments;
188 arguments << (m_host + ':' + QString::number(m_port));
190 kDebug(5012) << arguments;
192 setStatus(Connecting);
194 connect(m_process, SIGNAL(error(QProcess::ProcessError)), SLOT(processError(QProcess::ProcessError)));
195 connect(m_process, SIGNAL(readyReadStandardError()), SLOT(receivedStandardError()));
196 connect(m_container, SIGNAL(clientClosed()), SLOT(connectionClosed()));
197 connect(m_container, SIGNAL(clientIsEmbedded()), SLOT(connectionOpened()));
199 m_process->start("rdesktop", arguments);
201 return true;
204 void RdpView::switchFullscreen(bool on)
206 if (on == true) {
207 m_container->grabKeyboard();
211 void RdpView::connectionOpened()
213 kDebug(5012) << "Connection opened";
214 QSize size = m_container->minimumSizeHint();
215 kDebug(5012) << "Size hint: " << size.width() << " " << size.height();
216 setStatus(Connected);
217 setFixedSize(size);
218 resize(size);
219 m_container->setFixedSize(size);
220 emit changeSize(size.width(), size.height());
221 emit connected();
222 setFocus();
225 void RdpView::connectionClosed()
227 emit disconnected();
228 setStatus(Disconnected);
229 m_quitFlag = true;
232 void RdpView::processError(QProcess::ProcessError error)
234 if (m_status == Connecting) {
235 setStatus(Disconnected);
237 if (error == QProcess::FailedToStart) {
238 KMessageBox::error(0, i18n("Could not start rdesktop; make sure rdesktop is properly installed."),
239 i18n("rdesktop Failure"));
240 return;
243 if (m_clientVersion.isEmpty()) {
244 KMessageBox::error(0, i18n("Connection attempt to host failed."),
245 i18n("Connection Failure"));
246 } else {
247 KMessageBox::error(0, i18n("The version of rdesktop you are using (%1) is too old:\n"
248 "rdesktop 1.3.2 or greater is required.", m_clientVersion),
249 i18n("rdesktop Failure"));
251 emit disconnectedError();
255 void RdpView::receivedStandardError()
257 QString output(m_process->readAllStandardError());
258 QString line;
259 int i = 0;
260 while (!(line = output.section('\n', i, i)).isEmpty()) {
261 if (line.startsWith("Version ")) {
262 m_clientVersion = line.section(' ', 1, 1);
263 m_clientVersion = m_clientVersion.left(m_clientVersion.length() - 1);
264 return;
265 } else {
266 kDebug(5012) << "Process error output: " << line;
268 i++;
272 #include "rdpview.moc"