fix includes
[presencevnc] / src / remoteview.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2002-2003 Tim Jansen <tim@tjansen.de>
4 ** Copyright (C) 2007-2008 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.
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; 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.
22 **
23 ****************************************************************************/
24
25 #include "remoteview.h"
26
27 #ifndef QTONLY
28 #include <KDebug>
29 #include <KStandardDirs>
30 #endif
31
32 #include <QBitmap>
33
34 RemoteView::RemoteView(QWidget *parent)
35     : QWidget(parent),
36       m_status(Disconnected),
37       m_host(QString()),
38       m_port(0),
39       m_viewOnly(false),
40       m_grabAllKeys(false),
41       m_scale(true),
42       m_keyboardIsGrabbed(false),
43 #ifndef QTONLY
44       m_wallet(0),
45 #endif
46       m_dotCursorState(CursorOff)
47 {
48 }
49
50 RemoteView::~RemoteView()
51 {
52 #ifndef QTONLY
53     delete m_wallet;
54 #endif
55 }
56
57 RemoteView::RemoteStatus RemoteView::status()
58 {
59     return m_status;
60 }
61
62 void RemoteView::setStatus(RemoteView::RemoteStatus s)
63 {
64     if (m_status == s)
65         return;
66
67     if (((1+ m_status) != s) && (s != Disconnected)) {
68         // follow state transition rules
69
70         if (s == Disconnecting) {
71             if (m_status == Disconnected)
72                 return;
73         } else {
74             Q_ASSERT(((int) s) >= 0);
75             if (m_status > s) {
76                 m_status = Disconnected;
77                 emit statusChanged(Disconnected);
78             }
79             // smooth state transition
80             RemoteStatus origState = m_status;
81             for (int i = origState; i < s; ++i) {
82                 m_status = (RemoteStatus) i;
83                 emit statusChanged((RemoteStatus) i);
84             }
85         }
86     }
87     m_status = s;
88     emit statusChanged(m_status);
89 }
90
91 bool RemoteView::supportsScaling() const
92 {
93     return false;
94 }
95
96 bool RemoteView::supportsLocalCursor() const
97 {
98     return false;
99 }
100
101 QString RemoteView::host()
102 {
103     return m_host;
104 }
105
106 QSize RemoteView::framebufferSize()
107 {
108     return QSize(0, 0);
109 }
110
111 void RemoteView::startQuitting()
112 {
113 }
114
115 bool RemoteView::isQuitting()
116 {
117     return false;
118 }
119
120 int RemoteView::port()
121 {
122     return m_port;
123 }
124
125 void RemoteView::updateConfiguration()
126 {
127 }
128
129 void RemoteView::keyEvent(QKeyEvent *)
130 {
131 }
132
133 bool RemoteView::viewOnly()
134 {
135     return m_viewOnly;
136 }
137
138 void RemoteView::setViewOnly(bool viewOnly)
139 {
140     m_viewOnly = viewOnly;
141 }
142
143 bool RemoteView::grabAllKeys()
144 {
145     return m_grabAllKeys;
146 }
147
148 void RemoteView::setGrabAllKeys(bool grabAllKeys)
149 {
150     m_grabAllKeys = grabAllKeys;
151
152     if (grabAllKeys) {
153         m_keyboardIsGrabbed = true;
154         grabKeyboard();
155     } else if (m_keyboardIsGrabbed) {
156         releaseKeyboard();
157     }
158 }
159
160 void RemoteView::showDotCursor(DotCursorState state)
161 {
162     m_dotCursorState = state;
163 }
164
165 RemoteView::DotCursorState RemoteView::dotCursorState() const
166 {
167     return m_dotCursorState;
168 }
169
170 bool RemoteView::scaling() const
171 {
172     return m_scale;
173 }
174
175 void RemoteView::enableScaling(bool scale)
176 {
177     m_scale = scale;
178 }
179
180 void RemoteView::switchFullscreen(bool)
181 {
182 }
183
184 void RemoteView::scaleResize(int, int)
185 {
186 }
187
188 KUrl RemoteView::url()
189 {
190     return m_url;
191 }
192
193 #ifndef QTONLY
194 QString RemoteView::readWalletPassword(bool fromUserNameOnly)
195 {
196     const QString KRDCFOLDER = "KRDC";
197
198     window()->setDisabled(true); // WORKAROUND: disable inputs so users cannot close the current tab (see #181230)
199     m_wallet = KWallet::Wallet::openWallet(KWallet::Wallet::NetworkWallet(), window()->winId());
200     window()->setDisabled(false);
201
202     if (m_wallet) {
203         bool walletOK = m_wallet->hasFolder(KRDCFOLDER);
204         if (!walletOK) {
205             walletOK = m_wallet->createFolder(KRDCFOLDER);
206             kDebug(5010) << "Wallet folder created";
207         }
208         if (walletOK) {
209             kDebug(5010) << "Wallet OK";
210             m_wallet->setFolder(KRDCFOLDER);
211             QString password;
212
213             QString key;
214             if (fromUserNameOnly)
215                 key = m_url.userName();
216             else
217                 key = m_url.prettyUrl(KUrl::RemoveTrailingSlash);
218
219             if (m_wallet->hasEntry(key) &&
220                     !m_wallet->readPassword(key, password)) {
221                 kDebug(5010) << "Password read OK";
222
223                 return password;
224             }
225         }
226     }
227     return QString();
228 }
229
230 void RemoteView::saveWalletPassword(const QString &password, bool fromUserNameOnly)
231 {
232     QString key;
233     if (fromUserNameOnly)
234         key = m_url.userName();
235     else
236         key = m_url.prettyUrl(KUrl::RemoveTrailingSlash);
237
238     if (m_wallet && m_wallet->isOpen()) {
239         kDebug(5010) << "Write wallet password";
240         m_wallet->writePassword(key, password);
241     }
242 }
243 #endif
244
245 QCursor RemoteView::localDotCursor() const
246 {
247     //we take care of this in paintEvent()
248     return QCursor(Qt::BlankCursor);
249 }
250
251 void RemoteView::focusInEvent(QFocusEvent *event)
252 {
253     if (m_grabAllKeys) {
254         m_keyboardIsGrabbed = true;
255         grabKeyboard();
256     }
257
258     QWidget::focusInEvent(event);
259 }
260
261 void RemoteView::focusOutEvent(QFocusEvent *event)
262 {
263     if (m_grabAllKeys || m_keyboardIsGrabbed) {
264         m_keyboardIsGrabbed = false;
265         releaseKeyboard();
266     }
267
268     QWidget::focusOutEvent(event);
269 }