add bugtracker & project page link
[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 <a href=\"https://garage.maemo.org/projects/presencevnc/\">https://garage.maemo.org/projects/presencevnc</a></center>\
151 <small><p>&copy;2010 Christian Pulvermacher &lt;pulvermacher@gmx.de&gt;</p>\
152 <p>Based on KRDC, &copy; 2007-2008 Urs Wolfer</p>\
153 <p>and LibVNCServer, &copy; 2001-2003 Johannes E. Schindelin</p>\
154 <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>"));
155 }
156
157 void MainWindow::showConnectDialog()
158 {
159         ConnectDialog *connect_dialog = new ConnectDialog(this);
160         connect(connect_dialog, SIGNAL(connectToHost(QString)),
161                 this, SLOT(connectToHost(QString)));
162         connect_dialog->exec();
163 }
164
165 void MainWindow::connectToHost(QString url)
166 {
167         disconnectFromHost();
168
169         vnc_view = new VncView(this, url, RemoteView::Quality(2)); //TODO: get quality in dialog
170
171         connect(scaling, SIGNAL(toggled(bool)),
172                 vnc_view, SLOT(enableScaling(bool)));
173         connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
174                 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
175         scroll_area->setWidget(vnc_view);
176         vnc_view->start();
177         vnc_view->enableScaling(scaling->isChecked());
178         disconnect_action->setEnabled(true);
179         toolbar->setEnabled(true);
180 }
181
182 void MainWindow::disconnectFromHost()
183 {
184         if(!vnc_view)
185                 return;
186
187         scroll_area->setWidget(0);
188
189         vnc_view->disconnect(); //remove all signal-slot connections
190         delete vnc_view;
191         vnc_view = 0;
192         disconnect_action->setEnabled(false);
193         toolbar->setEnabled(false);
194 }
195
196 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
197 {
198         static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
199
200         switch(status) {
201         case RemoteView::Connecting:
202                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
203                 break;
204         case RemoteView::Connected:
205                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
206                 if(!scaling->isChecked()) {
207                         //if remote desktop is shown in full size, 2nd connection will have black screen
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::showModifierMenu()
261 {
262         static QMenu *mod_menu = new QMenu(tr("Modifiers"), this);
263         static QAction *win = mod_menu->addAction(tr("Win"));
264         static QAction *alt = mod_menu->addAction(tr("Alt"));
265         win->setCheckable(true);
266         alt->setCheckable(true);
267
268         //show menu at top-left corner of toolbar
269         QAction *chosen = mod_menu->exec(toolbar->mapToGlobal(QPoint(0,0)));
270         if(!chosen) {
271                 return;
272         } else if(chosen == alt) {
273                 vnc_view->sendKey(Qt::Key_Alt);
274         } else if(chosen == win) {
275                 vnc_view->sendKey(Qt::Key_Meta);
276         } else {
277                 std::cout << "unhandled action?\n";
278         }
279 }
280
281 void MainWindow::showPreferences()
282 {
283         Preferences *p = new Preferences(this);
284         p->exec();
285         delete p;
286
287         reloadSettings();
288 }
289
290 void MainWindow::reloadSettings()
291 {
292         QSettings settings;
293         int rotation = settings.value("screen_rotation", 0).toInt();
294         setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
295         setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
296         setAttribute(Qt::WA_Maemo5PortraitOrientation, rotation == 2);
297
298         if(vnc_view)
299                 vnc_view->reloadSettings();
300 }
301
302 void MainWindow::showInputPanel()
303 {
304         vnc_view->setAttribute(Qt::WA_InputMethodEnabled, true);
305         QEvent event(QEvent::RequestSoftwareInputPanel);
306         QApplication::sendEvent(vnc_view, &event);
307 }