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