233e8ed72f65f9497927289ea2d64a6e0467ca74
[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 #include <iostream>
32
33 MainWindow::MainWindow(QString url, int quality):
34         QMainWindow(0),
35         vnc_view(0),
36         scroll_area(new ScrollArea(0))
37 {
38         setWindowTitle("Presence VNC");
39         setAttribute(Qt::WA_Maemo5StackedWindow);
40
41         migrateConfiguration();
42
43         QSettings settings;
44
45         //set up toolbar
46         toolbar = new QToolBar(0);
47         toolbar->addAction("Mod", this, SLOT(showModifierMenu()));
48         toolbar->addAction("Tab", this, SLOT(sendTab()));
49         toolbar->addAction("Esc", this, SLOT(sendEsc()));
50         toolbar->addAction("PgUp", this, SLOT(sendPgUp()));
51         toolbar->addAction("PgDn", this, SLOT(sendPgDn()));
52         toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/chat_enter.png"), "", this, SLOT(sendReturn()));
53         toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/control_keyboard.png"), "", this, SLOT(showInputPanel()));
54
55         //move fullscreen button to the right
56         QWidget *spacer = new QWidget();
57         spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
58         toolbar->addWidget(spacer);
59
60         toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_fullsize.png"), "", this, SLOT(toggleFullscreen()));
61         addToolBar(toolbar);
62         toolbar->setVisible(settings.value("show_toolbar", true).toBool());
63
64         //set up menu
65         QMenuBar *menu = new QMenuBar(this);
66         QAction *connect_action = new QAction(tr("Connect"), this);
67         disconnect_action = new QAction(tr("Disconnect"), this);
68         menu->addAction(connect_action);
69         menu->addAction(disconnect_action);
70         scaling = new QAction(tr("Fit to Screen"), this);
71         scaling->setCheckable(true);
72         scaling->setChecked(settings.value("rescale", true).toBool());
73         menu->addAction(scaling);
74         show_toolbar = new QAction(tr("Show Toolbar"), this);
75         show_toolbar->setCheckable(true);
76         show_toolbar->setChecked(settings.value("show_toolbar", true).toBool());
77         menu->addAction(show_toolbar);
78         QAction *pref_action = new QAction(tr("Preferences"), this);
79         menu->addAction(pref_action);
80         QAction *about_action = new QAction(tr("About"), this);
81         menu->addAction(about_action);
82
83         connect(about_action, SIGNAL(triggered()),
84                 this, SLOT(about()));
85         connect(pref_action, SIGNAL(triggered()),
86                 this, SLOT(showPreferences()));
87         connect(connect_action, SIGNAL(triggered()),
88                 this, SLOT(showConnectDialog()));
89         connect(disconnect_action, SIGNAL(triggered()),
90                 this, SLOT(disconnectFromHost()));
91         connect(show_toolbar, SIGNAL(toggled(bool)),
92                 toolbar, SLOT(setVisible(bool)));
93         connect(show_toolbar, SIGNAL(toggled(bool)),
94                 this, SLOT(forceResizeDelayed()));
95
96         setCentralWidget(scroll_area);
97         new FullScreenExitButton(this);
98
99         grabZoomKeys(true);
100         reloadSettings();
101
102         connect(QApplication::desktop(), SIGNAL(resized(int)),
103                 this, SLOT(forceResize()));
104
105         if(url.isNull()) {
106                 disconnect_action->setEnabled(false);
107                 toolbar->setEnabled(false);
108                 showConnectDialog();
109         } else {
110                 vnc_view = new VncView(this, url, RemoteView::Quality(quality));
111                 connect(scaling, SIGNAL(toggled(bool)),
112                         vnc_view, SLOT(enableScaling(bool)));
113                 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
114                         this, SLOT(statusChanged(RemoteView::RemoteStatus)));
115                 scroll_area->setWidget(vnc_view);
116                 vnc_view->start();
117                 vnc_view->enableScaling(scaling->isChecked());
118         }
119 }
120
121 void MainWindow::grabZoomKeys(bool grab)
122 {
123         unsigned long val = (grab)?1:0;
124         Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
125         if(!atom) {
126                 qWarning("Couldn't get zoom key atom");
127                 return;
128         }
129         XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
130                 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
131 }
132
133 void MainWindow::closeEvent(QCloseEvent*) {
134         grabZoomKeys(false);
135
136         QSettings settings;
137         settings.setValue("show_toolbar", show_toolbar->isChecked());
138         settings.setValue("rescale", scaling->isChecked());
139         settings.sync();
140
141         hide();
142
143         disconnectFromHost();
144 }
145
146 void MainWindow::about() {
147         QMessageBox::about(this, tr("About Presence VNC"),
148                 tr("<center><h1>Presence VNC 0.4</h1>\
149 A touchscreen friendly VNC client\
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></small></center>\
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>"));
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)); //TODO: get quality in dialog
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                         //if remote desktop is shown in full size, 2nd connection will have black screen
207                         //ugly hack to force a refresh (forceFullRepaint() doesn't repaint?? -> vnc_view hidden???)
208                         vnc_view->resize(scroll_area->size());
209                         vnc_view->enableScaling(false);
210                 }
211                 break;
212         case RemoteView::Disconnecting:
213                 if(old_status != RemoteView::Disconnected) { //Disconnecting also occurs while connecting, so check last state
214                         QMaemo5InformationBox::information(this, tr("Connection lost"));
215                         
216                         //clean up
217                         scroll_area->setWidget(0);
218                         vnc_view = 0;
219                         disconnect_action->setEnabled(false);
220                         toolbar->setEnabled(false);
221
222                         //exit fullscreen mode
223                         if(windowState() & Qt::WindowFullScreen)
224                                 setWindowState(windowState() ^ Qt::WindowFullScreen);
225                 }
226                 break;
227         case RemoteView::Disconnected:
228                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
229                 if(old_status == RemoteView::Disconnecting) {
230                         scroll_area->setWidget(0); //remove widget
231                 }
232                 break;
233         }
234
235         old_status = status;
236 }
237
238 //when rescaling is enabled, this resizes the widget to use available screen space
239 //necessary when rotating, showing fullscreen, etc.
240 void MainWindow::forceResize()
241 {
242         if(vnc_view and scaling->isChecked()) {
243                 vnc_view->resize(scroll_area->size());
244         }
245
246
247 void MainWindow::forceResizeDelayed()
248 {
249         QTimer::singleShot(500, this, SLOT(forceResize()));
250 }
251
252 void MainWindow::toggleFullscreen()
253 {
254         setWindowState(windowState() ^ Qt::WindowFullScreen); 
255         toolbar->setVisible(show_toolbar->isChecked() and !(windowState() & Qt::WindowFullScreen)); //hide toolbar in fullscreen
256         forceResizeDelayed();
257 }
258
259 void MainWindow::showModifierMenu()
260 {
261         static QMenu *mod_menu = new QMenu(tr("Modifiers"), this);
262         static QAction *win = mod_menu->addAction(tr("Win"));
263         static QAction *alt = mod_menu->addAction(tr("Alt"));
264         win->setCheckable(true);
265         alt->setCheckable(true);
266
267         //show menu at top-left corner of toolbar
268         QAction *chosen = mod_menu->exec(toolbar->mapToGlobal(QPoint(0,0)));
269         if(!chosen) {
270                 return;
271         } else if(chosen == alt) {
272                 vnc_view->sendKey(Qt::Key_Alt);
273         } else if(chosen == win) {
274                 vnc_view->sendKey(Qt::Key_Meta);
275         } else {
276                 std::cout << "unhandled action?\n";
277         }
278 }
279
280 void MainWindow::showPreferences()
281 {
282         Preferences *p = new Preferences(this);
283         p->exec();
284         delete p;
285
286         reloadSettings();
287 }
288
289 void MainWindow::reloadSettings()
290 {
291         QSettings settings;
292         int rotation = settings.value("screen_rotation", 0).toInt();
293         setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
294         setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
295         setAttribute(Qt::WA_Maemo5PortraitOrientation, rotation == 2);
296
297         if(vnc_view)
298                 vnc_view->reloadSettings();
299 }
300
301 void MainWindow::showInputPanel()
302 {
303         vnc_view->setAttribute(Qt::WA_InputMethodEnabled, true);
304         QEvent event(QEvent::RequestSoftwareInputPanel);
305         QApplication::sendEvent(vnc_view, &event);
306 }