c1a9b1c0ef4e2b9c2035b91976e6d1c91d8aa644
[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 {
163     grabZoomKeys(false);
164
165     QSettings settings;
166     settings.setValue("show_toolbar", show_toolbar->isChecked());
167     settings.setValue("zoomlevel", zoom_slider->value());
168     settings.sync();
169
170     hide();
171
172     disconnectFromHost();
173 }
174
175 void MainWindow::about()
176 {
177     QMessageBox::about(this, tr("About Presence VNC"),
178                        tr("<center><h1>Presence VNC 0.8</h1>\
179                 <p>A touchscreen friendly VNC client</p>\
180                 <p><a href=\"http://presencevnc.garage.maemo.org/\">http://presencevnc.garage.maemo.org/</a></p></center>\
181                 <small><p>&copy;2010 Christian Pulvermacher &lt;pulvermacher@gmx.de&gt;<br />\
182                 Based on KRDC, &copy; 2007-2008 Urs Wolfer<br />\
183                 and LibVNCServer, &copy; 2001-2003 Johannes E. Schindelin</p>\
184                 <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>"));
185 }
186
187 void MainWindow::showConnectDialog()
188 {
189     ConnectDialog *connect_dialog = new ConnectDialog(this);
190     connect(connect_dialog, SIGNAL(connectToHost(QString, int, int, bool)),
191             this, SLOT(connectToHost(QString, int, int, bool)));
192     connect_dialog->exec();
193     //ConnectDialog cleans up after itself
194 }
195
196 void MainWindow::connectToHost(QString url, int quality, int listen_port, bool view_only)
197 {
198     disconnectFromHost();
199
200     vnc_view = new VncView(this, url, RemoteView::Quality(quality), listen_port);
201     vnc_view->setViewOnly(view_only);
202
203     connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
204             this, SLOT(statusChanged(RemoteView::RemoteStatus)));
205     scroll_area->setWidget(vnc_view);
206     vnc_view->start();
207     setWindowTitle(QString("Presence VNC - %1").arg(vnc_view->host()));
208
209     disconnect_action->setEnabled(true);
210
211     //reset key menu
212     delete key_menu;
213     key_menu = new KeyMenu(this);
214 }
215
216 void MainWindow::disconnectFromHost()
217 {
218     if(!vnc_view)
219         return;
220
221     setWindowTitle("Presence VNC");
222     disconnect_action->setEnabled(false);
223     toolbar->setEnabled(false);
224     scroll_area->setWidget(0);
225
226     delete vnc_view;
227     vnc_view = 0;
228 }
229
230 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
231 {
232     static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
233
234     switch(status) {
235     case RemoteView::Connecting:
236 #ifdef Q_WS_MAEMO_5
237         setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
238 #endif
239         break;
240     case RemoteView::Connected:
241 #ifdef Q_WS_MAEMO_5
242         setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
243 #endif
244         toolbar->setEnabled(true);
245
246         //disable key input buttons in view only mode
247         input_toolbuttons->setEnabled(!vnc_view->viewOnly());
248
249         vnc_view->setZoomLevel(zoom_slider->value());
250         vnc_view->useFastTransformations(false);
251         vnc_view->repaint();
252         break;
253     case RemoteView::Disconnecting:
254         if(old_status == RemoteView::Disconnected) //Disconnecting also occurs while connecting, so check last state
255             break;
256
257         if(disconnect_action->isEnabled()) //don't show when manually disconnecting
258             scroll_area->showMessage(tr("Connection lost"));
259
260         //clean up
261         scroll_area->setWidget(0);
262         vnc_view = 0;
263         setWindowTitle("Presence VNC");
264         disconnect_action->setEnabled(false);
265         toolbar->setEnabled(false);
266
267         //exit fullscreen mode
268         if(windowState() & Qt::WindowFullScreen)
269             setWindowState(windowState() ^ Qt::WindowFullScreen);
270         break;
271     case RemoteView::Disconnected:
272 #ifdef Q_WS_MAEMO_5
273         setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
274 #endif
275         if(old_status == RemoteView::Disconnecting) {
276             scroll_area->setWidget(0); //remove widget
277         }
278         break;
279     default: //avoid compiler warnings
280         break;
281     }
282
283     old_status = status;
284 }
285
286 //updates available screen space for current zoom level
287 //necessary when rotating, showing fullscreen, etc.
288 void MainWindow::updateScreenSpace()
289 {
290     if(vnc_view) {
291         vnc_view->setZoomLevel();
292     }
293 }
294
295 void MainWindow::updateScreenSpaceDelayed()
296 {
297     QTimer::singleShot(500, this, SLOT(updateScreenSpace()));
298 }
299
300 void MainWindow::toggleFullscreen()
301 {
302     bool in_fullscreen = windowState() & Qt::WindowFullScreen;
303
304     //hide menu/toolbar in fullscreen (new state is !in_fullscreen)
305     toolbar->setVisible(show_toolbar->isChecked() and in_fullscreen);
306
307 #ifndef Q_WS_MAEMO_5
308     //menu bar is invisible by default on maemo
309     menuBar()->setVisible(in_fullscreen);
310 #endif
311
312     setWindowState(windowState() ^ Qt::WindowFullScreen);
313     updateScreenSpaceDelayed();
314 }
315
316 void MainWindow::showKeyMenu()
317 {
318     key_menu->exec();
319     vnc_view->sendKeySequence(key_menu->getKeySequence());
320 }
321
322 void MainWindow::showPreferences()
323 {
324     Preferences *p = new Preferences(this);
325     p->exec();
326     delete p;
327
328     reloadSettings();
329 }
330
331 void MainWindow::reloadSettings()
332 {
333     QSettings settings;
334     zoom_to_cursor = settings.value("zoom_to_cursor", true).toBool();
335
336 #ifdef Q_WS_MAEMO_5
337     int rotation = settings.value("screen_rotation", 0).toInt();
338     setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
339     setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
340     setAttribute(Qt::WA_Maemo5PortraitOrientation, rotation == 2);
341 #endif
342
343     if(vnc_view)
344         vnc_view->reloadSettings();
345 }
346
347 void MainWindow::resizeEvent(QResizeEvent *event)
348 {
349     QMainWindow::resizeEvent(event);
350
351     updateScreenSpace();
352     if(vnc_view)
353         vnc_view->setZoomLevel(zoom_slider->value());
354
355 #ifdef Q_WS_MAEMO_5
356     //hide zoom slider in portrait mode
357     zoom_slider->setVisible(height() < width());
358 #endif
359 }
360
361 void MainWindow::showInputPanel()
362 {
363 #ifdef Q_WS_MAEMO_5
364     //TODO: when hardware keyboard is open, this will only cause the IM to mess up 'real' key events
365     vnc_view->setAttribute(Qt::WA_InputMethodEnabled, true);
366     vnc_view->setInputMethodHints(Qt::ImhNoAutoUppercase); //without this, IM starts with caps lock
367
368     QEvent event(QEvent::RequestSoftwareInputPanel);
369     QApplication::sendEvent(vnc_view, &event);
370 #endif
371 }
372
373 void MainWindow::setZoomLevel(int level)
374 {
375     if(!vnc_view)
376         return;
377
378     const qreal old_factor = vnc_view->zoomFactor();
379     QPoint center = vnc_view->visibleRegion().boundingRect().center();
380
381     vnc_view->setZoomLevel(level);
382
383     const qreal new_factor = vnc_view->zoomFactor();
384
385     //scroll to center, if zoom level actually changed
386     if(old_factor != new_factor) {
387         if(zoom_to_cursor)
388             center = new_factor * vnc_view->cursorPosition();
389         else //zoom to center of visible region
390             center = center * (double(new_factor)/old_factor);
391
392         scroll_area->ensureVisible(center.x(), center.y(),
393                                    vnc_view->visibleRegion().boundingRect().width()/2,
394                                    vnc_view->visibleRegion().boundingRect().height()/2);
395
396         vnc_view->useFastTransformations(zoom_slider->isSliderDown());
397         vnc_view->update();
398
399         scroll_area->showMessage(tr("Zoom: %1\%").arg(qRound(100*new_factor)));
400     }
401 }
402
403 void MainWindow::zoomSliderReleased()
404 {
405     static QTime time;
406     if(!time.isNull() and time.elapsed() < 500) //double clicked
407         zoom_slider->setValue(95); //100%
408
409     time.restart();
410
411     //stopped zooming, reenable high quality
412     vnc_view->useFastTransformations(false);
413 }
414
415 void MainWindow::displayStateChanged(QString state)
416 {
417     const bool display_on = (state != "off");
418     if(vnc_view)
419         vnc_view->setDisplayOff(!display_on);
420 }
421
422 void MainWindow::sendTab() { vnc_view->sendKey(Qt::Key_Tab); }
423 void MainWindow::sendEsc() { vnc_view->sendKey(Qt::Key_Escape); }
424 void MainWindow::sendPgUp() { vnc_view->sendKey(Qt::Key_PageUp); }
425 void MainWindow::sendPgDn() { vnc_view->sendKey(Qt::Key_PageDown); }
426 void MainWindow::sendReturn() { vnc_view->sendKey(Qt::Key_Return); }