fix indentation
[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 "scrollarea.h"
25 #include "vncview.h"
26
27 #ifdef Q_WS_MAEMO_5
28 #include <QtMaemo5>
29 #include <QX11Info>
30 #include <QDBusConnection>
31
32 #include <mce/mode-names.h>
33 #include <mce/dbus-names.h>
34
35 #include <X11/Xlib.h>
36 #include <X11/Xatom.h>
37 #endif
38
39
40 MainWindow::MainWindow(QString url, int quality, int listen_port, bool view_only):
41     QMainWindow(0),
42     vnc_view(0),
43     scroll_area(new ScrollArea(0)),
44     input_toolbuttons(new QActionGroup(this)),
45     key_menu(0)
46 {
47     setWindowTitle("Presence VNC");
48 #ifdef Q_WS_MAEMO_5
49     setContextMenuPolicy(Qt::NoContextMenu);
50     setAttribute(Qt::WA_Maemo5StackedWindow);
51 #endif
52
53     migrateConfiguration();
54
55     //set up toolbar
56     toolbar = new QToolBar(0);
57     input_toolbuttons->addAction(toolbar->addAction(QChar(0x2026), this, SLOT(showKeyMenu()))); //"..." button
58     input_toolbuttons->addAction(toolbar->addAction(tr("Tab"), this, SLOT(sendTab())));
59     input_toolbuttons->addAction(toolbar->addAction(tr("Esc"), this, SLOT(sendEsc())));
60     input_toolbuttons->addAction(toolbar->addAction(tr("PgUp"), this, SLOT(sendPgUp())));
61     input_toolbuttons->addAction(toolbar->addAction(tr("PgDn"), this, SLOT(sendPgDn())));
62 #ifdef Q_WS_MAEMO_5
63     input_toolbuttons->addAction(toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/chat_enter.png"), "", this, SLOT(sendReturn())));
64     input_toolbuttons->addAction(toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/control_keyboard.png"), "", this, SLOT(showInputPanel())));
65 #endif
66
67     QSettings settings;
68     zoom_slider = new QSlider(Qt::Horizontal, 0);
69     zoom_slider->setRange(0, 100);
70     connect(zoom_slider, SIGNAL(valueChanged(int)),
71             this, SLOT(setZoomLevel(int)));
72     connect(zoom_slider, SIGNAL(sliderReleased()),
73             this, SLOT(zoomSliderReleased()));
74     zoom_slider->setValue(settings.value("zoomlevel", 95).toInt());
75     toolbar->addWidget(zoom_slider);
76
77     toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_fullsize.png"), "", this, SLOT(toggleFullscreen()));
78     addToolBar(toolbar);
79     toolbar->setVisible(settings.value("show_toolbar", true).toBool());
80     toolbar->setEnabled(false);
81
82     //set up menu
83     QAction *connect_action = new QAction(tr("Connect"), this);
84     disconnect_action = new QAction(tr("Disconnect"), this);
85     show_toolbar = new QAction(tr("Show toolbar"), this);
86     show_toolbar->setCheckable(true);
87     show_toolbar->setChecked(settings.value("show_toolbar", true).toBool());
88     QAction *pref_action = new QAction(tr("Preferences"), this);
89     QAction *about_action = new QAction(tr("About"), this);
90
91 #ifdef Q_WS_MAEMO_5
92     menuBar()->addAction(connect_action);
93     menuBar()->addAction(disconnect_action);
94     menuBar()->addAction(show_toolbar);
95     menuBar()->addAction(pref_action);
96     menuBar()->addAction(about_action);
97 #else
98     QMenu* session_menu = menuBar()->addMenu(tr("&Session"));
99     session_menu->addAction(connect_action);
100     session_menu->addAction(disconnect_action);
101     session_menu->addSeparator();
102     session_menu->addAction(pref_action);
103     session_menu->addSeparator();
104     session_menu->addAction(tr("&Quit"), this, SLOT(close()));
105
106     QMenu* view_menu = menuBar()->addMenu(tr("&View"));
107     view_menu->addAction(show_toolbar);
108
109     QMenu* help_menu = menuBar()->addMenu(tr("&Help"));
110     help_menu->addAction(about_action);
111 #endif
112
113     connect(about_action, SIGNAL(triggered()),
114             this, SLOT(about()));
115     connect(pref_action, SIGNAL(triggered()),
116             this, SLOT(showPreferences()));
117     connect(connect_action, SIGNAL(triggered()),
118             this, SLOT(showConnectDialog()));
119     connect(disconnect_action, SIGNAL(triggered()),
120             this, SLOT(disconnectFromHost()));
121     connect(show_toolbar, SIGNAL(toggled(bool)),
122             toolbar, SLOT(setVisible(bool)));
123     connect(show_toolbar, SIGNAL(toggled(bool)),
124             this, SLOT(updateScreenSpaceDelayed()));
125 #ifdef Q_WS_MAEMO_5
126     QDBusConnection::systemBus().connect("", MCE_SIGNAL_PATH, MCE_SIGNAL_IF, MCE_DISPLAY_SIG,
127             this, SLOT(displayStateChanged(QString)));
128 #endif
129
130     setCentralWidget(scroll_area);
131
132     FullScreenExitButton* fullscreen_exit_button = new FullScreenExitButton(this);
133     connect(fullscreen_exit_button, SIGNAL(clicked()),
134             this, SLOT(toggleFullscreen()));
135
136     grabZoomKeys(true);
137     reloadSettings();
138
139     if(url.isEmpty() and listen_port == 0) {
140         disconnect_action->setEnabled(false);
141         showConnectDialog();
142     } else {
143         connectToHost(url, quality, listen_port, view_only);
144     }
145 }
146
147 void MainWindow::grabZoomKeys(bool grab)
148 {
149 #ifdef Q_WS_MAEMO_5
150     unsigned long val = (grab)?1:0;
151     Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
152     if(!atom) {
153         qWarning("Couldn't get zoom key atom");
154         return;
155     }
156     XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
157             32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
158 #endif
159 }
160
161 void MainWindow::closeEvent(QCloseEvent*) {
162     grabZoomKeys(false);
163
164     QSettings settings;
165     settings.setValue("show_toolbar", show_toolbar->isChecked());
166     settings.setValue("zoomlevel", zoom_slider->value());
167     settings.sync();
168
169     hide();
170
171     disconnectFromHost();
172 }
173
174 void MainWindow::about() {
175     QMessageBox::about(this, tr("About Presence VNC"),
176             tr("<center><h1>Presence VNC 0.8</h1>\
177                 <p>A touchscreen friendly VNC client</p>\
178                 <p><a href=\"http://presencevnc.garage.maemo.org/\">http://presencevnc.garage.maemo.org/</a></p></center>\
179                 <small><p>&copy;2010 Christian Pulvermacher &lt;pulvermacher@gmx.de&gt;<br />\
180                 Based on KRDC, &copy; 2007-2008 Urs Wolfer<br />\
181                 and LibVNCServer, &copy; 2001-2003 Johannes E. Schindelin</p>\
182                 <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>"));
183 }
184
185 void MainWindow::showConnectDialog()
186 {
187     ConnectDialog *connect_dialog = new ConnectDialog(this);
188     connect(connect_dialog, SIGNAL(connectToHost(QString, int, int, bool)),
189             this, SLOT(connectToHost(QString, int, int, bool)));
190     connect_dialog->exec();
191     //ConnectDialog cleans up after itself
192 }
193
194 void MainWindow::connectToHost(QString url, int quality, int listen_port, bool view_only)
195 {
196     disconnectFromHost();
197
198     vnc_view = new VncView(this, url, RemoteView::Quality(quality), listen_port);
199     vnc_view->setViewOnly(view_only);
200
201     connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
202             this, SLOT(statusChanged(RemoteView::RemoteStatus)));
203     scroll_area->setWidget(vnc_view);
204     vnc_view->start();
205
206     disconnect_action->setEnabled(true);
207
208     //reset key menu
209     delete key_menu;
210     key_menu = new KeyMenu(this);
211 }
212
213 void MainWindow::disconnectFromHost()
214 {
215     if(!vnc_view)
216         return;
217
218     disconnect_action->setEnabled(false);
219     toolbar->setEnabled(false);
220     scroll_area->setWidget(0);
221
222     delete vnc_view;
223     vnc_view = 0;
224 }
225
226 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
227 {
228     static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
229
230     switch(status) {
231         case RemoteView::Connecting:
232 #ifdef Q_WS_MAEMO_5
233             setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
234 #endif
235             break;
236         case RemoteView::Connected:
237 #ifdef Q_WS_MAEMO_5
238             setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
239 #endif
240             toolbar->setEnabled(true);
241
242             //disable key input buttons in view only mode
243             input_toolbuttons->setEnabled(!vnc_view->viewOnly());
244
245             vnc_view->setZoomLevel(zoom_slider->value());
246             vnc_view->useFastTransformations(false);
247             vnc_view->repaint();
248             break;
249         case RemoteView::Disconnecting:
250             if(old_status == RemoteView::Disconnected) //Disconnecting also occurs while connecting, so check last state
251                 break;
252
253             if(disconnect_action->isEnabled()) //don't show when manually disconnecting
254                 scroll_area->showMessage(tr("Connection lost"));
255
256             //clean up
257             scroll_area->setWidget(0);
258             vnc_view = 0;
259             disconnect_action->setEnabled(false);
260             toolbar->setEnabled(false);
261
262             //exit fullscreen mode
263             if(windowState() & Qt::WindowFullScreen)
264                 setWindowState(windowState() ^ Qt::WindowFullScreen);
265             break;
266         case RemoteView::Disconnected:
267 #ifdef Q_WS_MAEMO_5
268             setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
269 #endif
270             if(old_status == RemoteView::Disconnecting) {
271                 scroll_area->setWidget(0); //remove widget
272             }
273             break;
274         default: //avoid compiler warnings
275             break;
276     }
277
278     old_status = status;
279 }
280
281 //updates available screen space for current zoom level
282 //necessary when rotating, showing fullscreen, etc.
283 void MainWindow::updateScreenSpace()
284 {
285     if(vnc_view) {
286         vnc_view->setZoomLevel();
287     }
288
289
290 void MainWindow::updateScreenSpaceDelayed()
291 {
292     QTimer::singleShot(500, this, SLOT(updateScreenSpace()));
293 }
294
295 void MainWindow::toggleFullscreen()
296 {
297     bool in_fullscreen = windowState() & Qt::WindowFullScreen;
298
299     //hide menu/toolbar in fullscreen (new state is !in_fullscreen)
300     toolbar->setVisible(show_toolbar->isChecked() and in_fullscreen);
301
302 #ifndef Q_WS_MAEMO_5
303     //menu bar is invisible by default on maemo
304     menuBar()->setVisible(in_fullscreen);
305 #endif
306
307     setWindowState(windowState() ^ Qt::WindowFullScreen); 
308     updateScreenSpaceDelayed();
309 }
310
311 void MainWindow::showKeyMenu()
312 {
313     key_menu->exec();
314     vnc_view->sendKeySequence(key_menu->getKeySequence());
315 }
316
317 void MainWindow::showPreferences()
318 {
319     Preferences *p = new Preferences(this);
320     p->exec();
321     delete p;
322
323     reloadSettings();
324 }
325
326 void MainWindow::reloadSettings()
327 {
328     QSettings settings;
329     zoom_to_cursor = settings.value("zoom_to_cursor", true).toBool();
330
331 #ifdef Q_WS_MAEMO_5
332     int rotation = settings.value("screen_rotation", 0).toInt();
333     setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
334     setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
335     setAttribute(Qt::WA_Maemo5PortraitOrientation, rotation == 2);
336 #endif
337
338     if(vnc_view)
339         vnc_view->reloadSettings();
340 }
341
342 void MainWindow::resizeEvent(QResizeEvent *event)
343 {
344     QMainWindow::resizeEvent(event);
345
346     updateScreenSpace();
347     if(vnc_view)
348         vnc_view->setZoomLevel(zoom_slider->value());
349
350 #ifdef Q_WS_MAEMO_5
351     //hide zoom slider in portrait mode
352     zoom_slider->setVisible(height() < width());
353 #endif
354 }
355
356 void MainWindow::showInputPanel()
357 {
358 #ifdef Q_WS_MAEMO_5
359     //TODO: when hardware keyboard is open, this will only cause the IM to mess up 'real' key events
360     vnc_view->setAttribute(Qt::WA_InputMethodEnabled, true);
361     vnc_view->setInputMethodHints(Qt::ImhNoAutoUppercase); //without this, IM starts with caps lock
362
363     QEvent event(QEvent::RequestSoftwareInputPanel);
364     QApplication::sendEvent(vnc_view, &event);
365 #endif
366 }
367
368 void MainWindow::setZoomLevel(int level)
369 {
370     if(!vnc_view)
371         return;
372
373     const qreal old_factor = vnc_view->zoomFactor();
374     QPoint center = vnc_view->visibleRegion().boundingRect().center();
375
376     vnc_view->setZoomLevel(level);
377
378     const qreal new_factor = vnc_view->zoomFactor();
379
380     //scroll to center, if zoom level actually changed
381     if(old_factor != new_factor) {
382         if(zoom_to_cursor)
383             center = new_factor * vnc_view->cursorPosition();
384         else //zoom to center of visible region
385             center = center * (double(new_factor)/old_factor);
386
387         scroll_area->ensureVisible(center.x(), center.y(),
388                 vnc_view->visibleRegion().boundingRect().width()/2,
389                 vnc_view->visibleRegion().boundingRect().height()/2);
390
391         vnc_view->useFastTransformations(zoom_slider->isSliderDown());
392         vnc_view->update();
393
394         scroll_area->showMessage(tr("Zoom: %1\%").arg(qRound(100*new_factor)));
395     }
396 }
397
398 void MainWindow::zoomSliderReleased()
399 {
400     static QTime time;
401     if(!time.isNull() and time.elapsed() < 500) //double clicked
402         zoom_slider->setValue(95); //100%
403
404     time.restart();
405
406     //stopped zooming, reenable high quality
407     vnc_view->useFastTransformations(false);
408 }
409
410 void MainWindow::displayStateChanged(QString state)
411 {
412     const bool display_on = (state != "off");
413     if(vnc_view)
414         vnc_view->setDisplayOff(!display_on);
415 }
416
417 void MainWindow::sendTab() { vnc_view->sendKey(Qt::Key_Tab); }
418 void MainWindow::sendEsc() { vnc_view->sendKey(Qt::Key_Escape); }
419 void MainWindow::sendPgUp() { vnc_view->sendKey(Qt::Key_PageUp); }
420 void MainWindow::sendPgDn() { vnc_view->sendKey(Qt::Key_PageDown); }
421 void MainWindow::sendReturn() { vnc_view->sendKey(Qt::Key_Return); }