fe39fbcafa92967ade375efee09efae1e5387d43
[presencevnc] / src / mainwindow.cpp
1 /*
2     Presence VNC
3     Copyright (C) 2010 Christian Pulvermacher
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 #include "connectdialog.h"
20 #include "fullscreenexitbutton.h"
21 #include "keymenu.h"
22 #include "mainwindow.h"
23 #include "preferences.h"
24 #include "vncview.h"
25
26 #ifdef Q_WS_MAEMO_5
27 #include <QtMaemo5>
28 #include <QX11Info>
29
30 #include <X11/Xlib.h>
31 #include <X11/Xatom.h>
32 #endif
33
34
35 MainWindow::MainWindow(QString url, int quality):
36         QMainWindow(0),
37         vnc_view(0),
38         scroll_area(new ScrollArea(0)),
39         key_menu(new KeyMenu(this))
40 {
41         setWindowTitle("Presence VNC");
42         setContextMenuPolicy(Qt::NoContextMenu);
43 #ifdef Q_WS_MAEMO_5
44         setAttribute(Qt::WA_Maemo5StackedWindow);
45 #endif
46
47         migrateConfiguration();
48
49         //set up toolbar
50         toolbar = new QToolBar(0);
51         toolbar->addAction(QChar(0x2026), this, SLOT(showKeyMenu())); //"..." button
52         toolbar->addAction(tr("Tab"), this, SLOT(sendTab()));
53         toolbar->addAction(tr("Esc"), this, SLOT(sendEsc()));
54         toolbar->addAction(tr("PgUp"), this, SLOT(sendPgUp()));
55         toolbar->addAction(tr("PgDn"), this, SLOT(sendPgDn()));
56 #ifdef Q_WS_MAEMO_5
57         toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/chat_enter.png"), "", this, SLOT(sendReturn()));
58         toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/control_keyboard.png"), "", this, SLOT(showInputPanel()));
59 #endif
60
61         /*
62         //move remaining buttons to the right
63         QWidget *spacer = new QWidget();
64         spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
65         toolbar->addWidget(spacer);
66         */
67
68         QSettings settings;
69         zoom_slider = new QSlider(Qt::Horizontal, 0);
70         zoom_slider->setRange(0, 100);
71         connect(zoom_slider, SIGNAL(valueChanged(int)),
72                 this, SLOT(setZoomLevel(int)));
73         connect(zoom_slider, SIGNAL(sliderReleased()),
74                 this, SLOT(forceRepaint()));
75         zoom_slider->setValue(settings.value("zoomlevel", 95).toInt());
76         toolbar->addWidget(zoom_slider);
77
78         toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_fullsize.png"), "", this, SLOT(toggleFullscreen()));
79         addToolBar(toolbar);
80         toolbar->setVisible(settings.value("show_toolbar", true).toBool());
81         toolbar->setEnabled(false);
82
83         //set up menu
84         QAction *connect_action = new QAction(tr("Connect"), this);
85         disconnect_action = new QAction(tr("Disconnect"), this);
86         show_toolbar = new QAction(tr("Show Toolbar"), this);
87         show_toolbar->setCheckable(true);
88         show_toolbar->setChecked(settings.value("show_toolbar", true).toBool());
89         QAction *pref_action = new QAction(tr("Preferences"), this);
90         QAction *about_action = new QAction(tr("About"), this);
91
92 #ifdef Q_WS_MAEMO_5
93         menuBar()->addAction(connect_action);
94         menuBar()->addAction(disconnect_action);
95         menuBar()->addAction(show_toolbar);
96         menuBar()->addAction(pref_action);
97         menuBar()->addAction(about_action);
98 #else
99         QMenu* session_menu = menuBar()->addMenu(tr("&Session"));
100         session_menu->addAction(connect_action);
101         session_menu->addAction(disconnect_action);
102         session_menu->addSeparator();
103         session_menu->addAction(pref_action);
104         session_menu->addSeparator();
105         session_menu->addAction(tr("&Quit"), this, SLOT(close()));
106
107         QMenu* view_menu = menuBar()->addMenu(tr("&View"));
108         view_menu->addAction(show_toolbar);
109
110         QMenu* help_menu = menuBar()->addMenu(tr("&Help"));
111         help_menu->addAction(about_action);
112 #endif
113
114         connect(about_action, SIGNAL(triggered()),
115                 this, SLOT(about()));
116         connect(pref_action, SIGNAL(triggered()),
117                 this, SLOT(showPreferences()));
118         connect(connect_action, SIGNAL(triggered()),
119                 this, SLOT(showConnectDialog()));
120         connect(disconnect_action, SIGNAL(triggered()),
121                 this, SLOT(disconnectFromHost()));
122         connect(show_toolbar, SIGNAL(toggled(bool)),
123                 toolbar, SLOT(setVisible(bool)));
124         connect(show_toolbar, SIGNAL(toggled(bool)),
125                 this, SLOT(updateScreenSpaceDelayed()));
126
127         setCentralWidget(scroll_area);
128         new FullScreenExitButton(this);
129
130         grabZoomKeys(true);
131         reloadSettings();
132
133         if(url.isNull()) {
134                 disconnect_action->setEnabled(false);
135                 showConnectDialog();
136         } else {
137                 vnc_view = new VncView(this, url, RemoteView::Quality(quality));
138                 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
139                         this, SLOT(statusChanged(RemoteView::RemoteStatus)));
140                 scroll_area->setWidget(vnc_view);
141                 vnc_view->start();
142         }
143 }
144
145 void MainWindow::grabZoomKeys(bool grab)
146 {
147 #ifdef Q_WS_MAEMO_5
148         unsigned long val = (grab)?1:0;
149         Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
150         if(!atom) {
151                 qWarning("Couldn't get zoom key atom");
152                 return;
153         }
154         XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
155                 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
156 #endif
157 }
158
159 void MainWindow::closeEvent(QCloseEvent*) {
160         grabZoomKeys(false);
161
162         QSettings settings;
163         settings.setValue("show_toolbar", show_toolbar->isChecked());
164         settings.setValue("zoomlevel", zoom_slider->value());
165         settings.sync();
166
167         hide();
168
169         disconnectFromHost();
170 }
171
172 void MainWindow::about() {
173         QMessageBox::about(this, tr("About Presence VNC"),
174                 tr("<center><h1>Presence VNC 0.6</h1>\
175 <p>A touchscreen friendly VNC client</p>\
176 <p><a href=\"https://garage.maemo.org/projects/presencevnc/\">https://garage.maemo.org/projects/presencevnc</a></p></center>\
177 <small><p>&copy;2010 Christian Pulvermacher &lt;pulvermacher@gmx.de&gt;<br />\
178 Based on KRDC, &copy; 2007-2008 Urs Wolfer<br />\
179 and LibVNCServer, &copy; 2001-2003 Johannes E. Schindelin</p>\
180 <p>This program is free software; License: <a href=\"http://www.gnu.org/licenses/gpl-2.0.html\">GNU GPL 2</a> or later.</p></small>"));
181 }
182
183 void MainWindow::showConnectDialog()
184 {
185         ConnectDialog *connect_dialog = new ConnectDialog(this);
186         connect(connect_dialog, SIGNAL(connectToHost(QString, int, int)),
187                 this, SLOT(connectToHost(QString, int, int)));
188         connect_dialog->exec();
189 }
190
191 void MainWindow::connectToHost(QString url, int quality, int listen_port)
192 {
193         disconnectFromHost();
194
195         vnc_view = new VncView(this, url, RemoteView::Quality(quality), listen_port);
196
197         connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
198                 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
199         scroll_area->setWidget(vnc_view);
200         vnc_view->start();
201
202         disconnect_action->setEnabled(true);
203
204         //reset key menu
205         delete key_menu;
206         key_menu = new KeyMenu(this);
207 }
208
209 void MainWindow::disconnectFromHost()
210 {
211         if(!vnc_view)
212                 return;
213
214         disconnect_action->setEnabled(false);
215         toolbar->setEnabled(false);
216         scroll_area->setWidget(0);
217
218         delete vnc_view;
219         vnc_view = 0;
220 }
221
222 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
223 {
224         static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
225
226         switch(status) {
227         case RemoteView::Connecting:
228 #ifdef Q_WS_MAEMO_5
229                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
230 #endif
231                 break;
232         case RemoteView::Connected:
233 #ifdef Q_WS_MAEMO_5
234                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
235 #endif
236                 toolbar->setEnabled(true);
237
238                 vnc_view->setZoomLevel(zoom_slider->value());
239                 vnc_view->forceFullRepaint();
240                 break;
241         case RemoteView::Disconnecting:
242                 if(old_status != RemoteView::Disconnected) { //Disconnecting also occurs while connecting, so check last state
243 #ifdef Q_WS_MAEMO_5
244                         if(disconnect_action->isEnabled()) //don't show when manually disconnecting
245                                 QMaemo5InformationBox::information(this, tr("Connection lost"));
246 #endif
247                         
248                         //clean up
249                         scroll_area->setWidget(0);
250                         vnc_view = 0;
251                         disconnect_action->setEnabled(false);
252                         toolbar->setEnabled(false);
253
254                         //exit fullscreen mode
255                         if(windowState() & Qt::WindowFullScreen)
256                                 setWindowState(windowState() ^ Qt::WindowFullScreen);
257                 }
258                 break;
259         case RemoteView::Disconnected:
260 #ifdef Q_WS_MAEMO_5
261                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
262 #endif
263                 if(old_status == RemoteView::Disconnecting) {
264                         scroll_area->setWidget(0); //remove widget
265                 }
266                 break;
267         default: //avoid compiler warnings
268                 break;
269         }
270
271         old_status = status;
272 }
273
274 void MainWindow::forceRepaint()
275 {
276         if(vnc_view)
277                 vnc_view->forceFullRepaint();
278 }
279
280 //updates available screen space for current zoom level
281 //necessary when rotating, showing fullscreen, etc.
282 void MainWindow::updateScreenSpace()
283 {
284         if(vnc_view) {
285                 vnc_view->setZoomLevel();
286         }
287
288
289 void MainWindow::updateScreenSpaceDelayed()
290 {
291         QTimer::singleShot(500, this, SLOT(updateScreenSpace()));
292 }
293
294 void MainWindow::toggleFullscreen()
295 {
296         bool in_fullscreen = windowState() & Qt::WindowFullScreen;
297
298         //hide menu/toolbar in fullscreen (new state is !in_fullscreen)
299         toolbar->setVisible(show_toolbar->isChecked() and in_fullscreen);
300
301 #ifndef Q_WS_MAEMO_5
302         //menu bar is invisible by default on maemo
303         menuBar()->setVisible(in_fullscreen);
304 #endif
305
306         setWindowState(windowState() ^ Qt::WindowFullScreen); 
307         updateScreenSpaceDelayed();
308 }
309
310 void MainWindow::showKeyMenu()
311 {
312         key_menu->exec();
313         vnc_view->sendKeySequence(key_menu->getKeySequence());
314 }
315
316 void MainWindow::showPreferences()
317 {
318         Preferences *p = new Preferences(this);
319         p->exec();
320         delete p;
321
322         reloadSettings();
323 }
324
325 void MainWindow::reloadSettings()
326 {
327 #ifdef Q_WS_MAEMO_5
328         QSettings settings;
329         int rotation = settings.value("screen_rotation", 0).toInt();
330         setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
331         setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
332         setAttribute(Qt::WA_Maemo5PortraitOrientation, rotation == 2);
333 #endif
334
335         if(vnc_view)
336                 vnc_view->reloadSettings();
337 }
338
339 void MainWindow::resizeEvent(QResizeEvent *event)
340 {
341         QMainWindow::resizeEvent(event);
342
343         updateScreenSpace();
344         if(vnc_view)
345                 vnc_view->setZoomLevel(zoom_slider->value());
346         
347 #ifdef Q_WS_MAEMO_5
348         //hide zoom slider in portrait mode
349         zoom_slider->setVisible(height() < width());
350 #endif
351 }
352
353 void MainWindow::showInputPanel()
354 {
355 #ifdef Q_WS_MAEMO_5
356         //TODO: when hardware keyboard is open, this will only cause the IM to mess up 'real' key events
357         vnc_view->setAttribute(Qt::WA_InputMethodEnabled, true);
358         vnc_view->setInputMethodHints(Qt::ImhNoAutoUppercase); //without this, IM starts with caps lock
359
360         QEvent event(QEvent::RequestSoftwareInputPanel);
361         QApplication::sendEvent(vnc_view, &event);
362 #endif
363 }
364
365 void MainWindow::setZoomLevel(int level)
366 {
367         if(!vnc_view)
368                 return;
369         
370         int old_width = vnc_view->width();
371         QPoint center = vnc_view->visibleRegion().boundingRect().center();
372
373         vnc_view->setZoomLevel(level);
374
375         int new_width = vnc_view->width();
376
377         //scroll to center, if zoom level actually changed
378         if(new_width != old_width) {
379                 center = center * (double(new_width)/old_width);
380                 scroll_area->ensureVisible(center.x(), center.y(),
381                         vnc_view->visibleRegion().boundingRect().width()/2,
382                         vnc_view->visibleRegion().boundingRect().height()/2);
383                 vnc_view->update();
384
385 #ifdef Q_WS_MAEMO_5
386                 QMaemo5InformationBox::information(this, tr("%1\%").arg(qRound(100*vnc_view->getZoomFactor())));
387 #endif
388         }
389 }