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