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