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