reworked about dialog
[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 #include <QSlider>
27
28 #ifdef Q_WS_MAEMO_5
29 #include <QtMaemo5>
30 #include <QX11Info>
31
32 #include <X11/Xlib.h>
33 #include <X11/Xatom.h>
34 #endif
35
36
37
38 MainWindow::MainWindow(QString url, int quality):
39         QMainWindow(0),
40         vnc_view(0),
41         scroll_area(new ScrollArea(0)),
42         key_menu(0)
43 {
44         setWindowTitle("Presence VNC");
45
46         migrateConfiguration();
47         QSettings settings;
48
49 #ifdef Q_WS_MAEMO_5
50         setAttribute(Qt::WA_Maemo5StackedWindow);
51 #endif
52
53         //set up toolbar
54         toolbar = new QToolBar(0);
55         toolbar->addAction(QChar(0x2026), this, SLOT(showKeyMenu())); //"..." button
56         toolbar->addAction(tr("Tab"), this, SLOT(sendTab()));
57         toolbar->addAction(tr("Esc"), this, SLOT(sendEsc()));
58         toolbar->addAction(tr("PgUp"), this, SLOT(sendPgUp()));
59         toolbar->addAction(tr("PgDn"), this, SLOT(sendPgDn()));
60 #ifdef Q_WS_MAEMO_5
61         toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/chat_enter.png"), "", this, SLOT(sendReturn()));
62         toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/control_keyboard.png"), "", this, SLOT(showInputPanel()));
63 #endif
64
65         //move fullscreen button to the right
66         QWidget *spacer = new QWidget();
67         spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
68         toolbar->addWidget(spacer);
69
70         toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_fullsize.png"), "", this, SLOT(toggleFullscreen()));
71         addToolBar(toolbar);
72         toolbar->setVisible(settings.value("show_toolbar", true).toBool());
73
74         //set up zoombar
75         zoombar = new QToolBar(0);
76         QSlider *zoom_slider = new QSlider(Qt::Horizontal, 0);
77         zoom_slider->setRange(0, 100);
78         connect(zoom_slider, SIGNAL(valueChanged(int)),
79                 this, SLOT(setZoomLevel(int)));
80         zoombar->addWidget(zoom_slider);
81         addToolBar(zoombar);
82
83         //set up menu
84         QAction *connect_action = new QAction(tr("Connect"), this);
85         disconnect_action = new QAction(tr("Disconnect"), this);
86         scaling = new QAction(tr("Fit to Screen"), this);
87         scaling->setCheckable(true);
88         scaling->setChecked(settings.value("rescale", true).toBool());
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(scaling);
99         menuBar()->addAction(show_toolbar);
100         menuBar()->addAction(pref_action);
101         menuBar()->addAction(about_action);
102 #else
103         QMenu* session_menu = menuBar()->addMenu(tr("&Session"));
104         session_menu->addAction(connect_action);
105         session_menu->addAction(disconnect_action);
106         session_menu->addSeparator();
107         session_menu->addAction(pref_action);
108         session_menu->addSeparator();
109         session_menu->addAction(tr("&Quit"), this, SLOT(close()));
110
111         QMenu* view_menu = menuBar()->addMenu(tr("&View"));
112         view_menu->addAction(scaling);
113         view_menu->addAction(show_toolbar);
114
115         QMenu* help_menu = menuBar()->addMenu(tr("&Help"));
116         help_menu->addAction(about_action);
117 #endif
118
119         connect(about_action, SIGNAL(triggered()),
120                 this, SLOT(about()));
121         connect(pref_action, SIGNAL(triggered()),
122                 this, SLOT(showPreferences()));
123         connect(connect_action, SIGNAL(triggered()),
124                 this, SLOT(showConnectDialog()));
125         connect(disconnect_action, SIGNAL(triggered()),
126                 this, SLOT(disconnectFromHost()));
127         connect(show_toolbar, SIGNAL(toggled(bool)),
128                 toolbar, SLOT(setVisible(bool)));
129         connect(show_toolbar, SIGNAL(toggled(bool)),
130                 this, SLOT(forceResizeDelayed()));
131
132         setCentralWidget(scroll_area);
133         new FullScreenExitButton(this);
134
135         grabZoomKeys(true);
136         reloadSettings();
137
138         if(url.isNull()) {
139                 disconnect_action->setEnabled(false);
140                 toolbar->setEnabled(false);
141                 showConnectDialog();
142         } else {
143                 vnc_view = new VncView(this, url, RemoteView::Quality(quality));
144                 connect(scaling, SIGNAL(toggled(bool)),
145                         vnc_view, SLOT(enableScaling(bool)));
146                 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
147                         this, SLOT(statusChanged(RemoteView::RemoteStatus)));
148                 scroll_area->setWidget(vnc_view);
149                 vnc_view->start();
150                 vnc_view->enableScaling(scaling->isChecked());
151                 key_menu = new KeyMenu(this);
152         }
153 }
154
155 void MainWindow::grabZoomKeys(bool grab)
156 {
157 #ifdef Q_WS_MAEMO_5
158         unsigned long val = (grab)?1:0;
159         Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
160         if(!atom) {
161                 qWarning("Couldn't get zoom key atom");
162                 return;
163         }
164         XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
165                 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
166 #endif
167 }
168
169 void MainWindow::closeEvent(QCloseEvent*) {
170         grabZoomKeys(false);
171
172         QSettings settings;
173         settings.setValue("show_toolbar", show_toolbar->isChecked());
174         settings.setValue("rescale", scaling->isChecked());
175         settings.sync();
176
177         hide();
178
179         disconnectFromHost();
180 }
181
182 void MainWindow::about() {
183         QMessageBox::about(this, tr("About Presence VNC"),
184                 tr("<center><h1>Presence VNC 0.6</h1>\
185 <p>A touchscreen friendly VNC client</p>\
186 <p><a href=\"https://garage.maemo.org/projects/presencevnc/\">https://garage.maemo.org/projects/presencevnc</a></p></center>\
187 <small><p>&copy;2010 Christian Pulvermacher &lt;pulvermacher@gmx.de&gt;<br />\
188 Based on KRDC, &copy; 2007-2008 Urs Wolfer<br />\
189 and LibVNCServer, &copy; 2001-2003 Johannes E. Schindelin</p>\
190 <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>"));
191 }
192
193 void MainWindow::showConnectDialog()
194 {
195         ConnectDialog *connect_dialog = new ConnectDialog(this);
196         connect(connect_dialog, SIGNAL(connectToHost(QString, int)),
197                 this, SLOT(connectToHost(QString, int)));
198         connect_dialog->exec();
199 }
200
201 void MainWindow::connectToHost(QString url, int quality)
202 {
203         disconnectFromHost();
204
205         vnc_view = new VncView(this, url, RemoteView::Quality(quality));
206
207         connect(scaling, SIGNAL(toggled(bool)),
208                 vnc_view, SLOT(enableScaling(bool)));
209         connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
210                 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
211         scroll_area->setWidget(vnc_view);
212         vnc_view->start();
213         vnc_view->enableScaling(scaling->isChecked());
214         disconnect_action->setEnabled(true);
215         toolbar->setEnabled(true);
216
217         if(key_menu) //reset
218                 delete key_menu;
219         key_menu = new KeyMenu(this);
220 }
221
222 void MainWindow::disconnectFromHost()
223 {
224         if(!vnc_view)
225                 return;
226
227         scroll_area->setWidget(0);
228
229         vnc_view->disconnect(); //remove all signal-slot connections
230         delete vnc_view;
231         vnc_view = 0;
232         disconnect_action->setEnabled(false);
233         toolbar->setEnabled(false);
234 }
235
236 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
237 {
238         static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
239
240         switch(status) {
241         case RemoteView::Connecting:
242 #ifdef Q_WS_MAEMO_5
243                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
244 #endif
245                 break;
246         case RemoteView::Connected:
247 #ifdef Q_WS_MAEMO_5
248                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
249 #endif
250                 if(!scaling->isChecked()) {
251                         //ugly hack to force a refresh (forceFullRepaint() doesn't repaint?? -> vnc_view hidden???)
252                         vnc_view->resize(scroll_area->size());
253                         vnc_view->enableScaling(false);
254                 }
255                 break;
256         case RemoteView::Disconnecting:
257                 if(old_status != RemoteView::Disconnected) { //Disconnecting also occurs while connecting, so check last state
258 #ifdef Q_WS_MAEMO_5
259                         QMaemo5InformationBox::information(this, tr("Connection lost"));
260 #endif
261                         
262                         //clean up
263                         scroll_area->setWidget(0);
264                         vnc_view = 0;
265                         disconnect_action->setEnabled(false);
266                         toolbar->setEnabled(false);
267
268                         //exit fullscreen mode
269                         if(windowState() & Qt::WindowFullScreen)
270                                 setWindowState(windowState() ^ Qt::WindowFullScreen);
271                 }
272                 break;
273         case RemoteView::Disconnected:
274 #ifdef Q_WS_MAEMO_5
275                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
276 #endif
277                 if(old_status == RemoteView::Disconnecting) {
278                         scroll_area->setWidget(0); //remove widget
279                 }
280                 break;
281         }
282
283         old_status = status;
284 }
285
286 //when rescaling is enabled, this resizes the widget to use available screen space
287 //necessary when rotating, showing fullscreen, etc.
288 void MainWindow::forceResize()
289 {
290         if(vnc_view and scaling->isChecked()) {
291                 vnc_view->resize(scroll_area->size());
292         }
293
294
295 void MainWindow::forceResizeDelayed()
296 {
297         QTimer::singleShot(500, this, SLOT(forceResize()));
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         forceResizeDelayed();
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 #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         forceResize();
350 }
351
352 void MainWindow::showInputPanel()
353 {
354 #ifdef Q_WS_MAEMO_5
355         //TODO: when hardware keyboard is open, this will only cause the IM to mess up 'real' key events
356         vnc_view->setAttribute(Qt::WA_InputMethodEnabled, true);
357         vnc_view->setInputMethodHints(Qt::ImhNoAutoUppercase); //without this, IM starts with caps lock
358
359         QEvent event(QEvent::RequestSoftwareInputPanel);
360         QApplication::sendEvent(vnc_view, &event);
361 #endif
362 }
363
364 void MainWindow::setZoomLevel(int level)
365 {
366         if(vnc_view)
367                 vnc_view->setZoomLevel(level);
368 }