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