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