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