fix symbol key
[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 {
40         setWindowTitle("Presence VNC");
41
42         migrateConfiguration();
43         QSettings settings;
44
45 #ifdef Q_WS_MAEMO_5
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_MAEMO_5
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_MAEMO_5
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         if(url.isNull()) {
126                 disconnect_action->setEnabled(false);
127                 toolbar->setEnabled(false);
128                 showConnectDialog();
129         } else {
130                 vnc_view = new VncView(this, url, RemoteView::Quality(quality));
131                 connect(scaling, SIGNAL(toggled(bool)),
132                         vnc_view, SLOT(enableScaling(bool)));
133                 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
134                         this, SLOT(statusChanged(RemoteView::RemoteStatus)));
135                 scroll_area->setWidget(vnc_view);
136                 vnc_view->start();
137                 vnc_view->enableScaling(scaling->isChecked());
138         }
139 }
140
141 void MainWindow::grabZoomKeys(bool grab)
142 {
143 #ifdef Q_WS_MAEMO_5
144         unsigned long val = (grab)?1:0;
145         Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
146         if(!atom) {
147                 qWarning("Couldn't get zoom key atom");
148                 return;
149         }
150         XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
151                 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
152 #endif
153 }
154
155 void MainWindow::closeEvent(QCloseEvent*) {
156         grabZoomKeys(false);
157
158         QSettings settings;
159         settings.setValue("show_toolbar", show_toolbar->isChecked());
160         settings.setValue("rescale", scaling->isChecked());
161         settings.sync();
162
163         hide();
164
165         disconnectFromHost();
166 }
167
168 void MainWindow::about() {
169         QMessageBox::about(this, tr("About Presence VNC"),
170                 tr("<center><h1>Presence VNC 0.5</h1>\
171 <p>A touchscreen friendly VNC client</p>\
172 <p><a href=\"https://garage.maemo.org/projects/presencevnc/\">https://garage.maemo.org/projects/presencevnc</a></p></center>\
173 <small><p>&copy;2010 Christian Pulvermacher &lt;pulvermacher@gmx.de&gt;</p>\
174 <p>Based on KRDC, &copy; 2007-2008 Urs Wolfer</p>\
175 <p>and LibVNCServer, &copy; 2001-2003 Johannes E. Schindelin</p>\
176 <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>"));
177 }
178
179 void MainWindow::showConnectDialog()
180 {
181         ConnectDialog *connect_dialog = new ConnectDialog(this);
182         connect(connect_dialog, SIGNAL(connectToHost(QString)),
183                 this, SLOT(connectToHost(QString)));
184         connect_dialog->exec();
185 }
186
187 void MainWindow::connectToHost(QString url)
188 {
189         disconnectFromHost();
190
191         vnc_view = new VncView(this, url, RemoteView::Quality(2));
192
193         connect(scaling, SIGNAL(toggled(bool)),
194                 vnc_view, SLOT(enableScaling(bool)));
195         connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
196                 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
197         scroll_area->setWidget(vnc_view);
198         vnc_view->start();
199         vnc_view->enableScaling(scaling->isChecked());
200         disconnect_action->setEnabled(true);
201         toolbar->setEnabled(true);
202 }
203
204 void MainWindow::disconnectFromHost()
205 {
206         if(!vnc_view)
207                 return;
208
209         scroll_area->setWidget(0);
210
211         vnc_view->disconnect(); //remove all signal-slot connections
212         delete vnc_view;
213         vnc_view = 0;
214         disconnect_action->setEnabled(false);
215         toolbar->setEnabled(false);
216 }
217
218 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
219 {
220         static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
221
222         switch(status) {
223         case RemoteView::Connecting:
224 #ifdef Q_WS_MAEMO_5
225                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
226 #endif
227                 break;
228         case RemoteView::Connected:
229 #ifdef Q_WS_MAEMO_5
230                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
231 #endif
232                 if(!scaling->isChecked()) {
233                         //ugly hack to force a refresh (forceFullRepaint() doesn't repaint?? -> vnc_view hidden???)
234                         vnc_view->resize(scroll_area->size());
235                         vnc_view->enableScaling(false);
236                 }
237                 break;
238         case RemoteView::Disconnecting:
239                 if(old_status != RemoteView::Disconnected) { //Disconnecting also occurs while connecting, so check last state
240 #ifdef Q_WS_MAEMO_5
241                         QMaemo5InformationBox::information(this, tr("Connection lost"));
242 #endif
243                         
244                         //clean up
245                         scroll_area->setWidget(0);
246                         vnc_view = 0;
247                         disconnect_action->setEnabled(false);
248                         toolbar->setEnabled(false);
249
250                         //exit fullscreen mode
251                         if(windowState() & Qt::WindowFullScreen)
252                                 setWindowState(windowState() ^ Qt::WindowFullScreen);
253                 }
254                 break;
255         case RemoteView::Disconnected:
256 #ifdef Q_WS_MAEMO_5
257                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
258 #endif
259                 if(old_status == RemoteView::Disconnecting) {
260                         scroll_area->setWidget(0); //remove widget
261                 }
262                 break;
263         }
264
265         old_status = status;
266 }
267
268 //when rescaling is enabled, this resizes the widget to use available screen space
269 //necessary when rotating, showing fullscreen, etc.
270 void MainWindow::forceResize()
271 {
272         if(vnc_view and scaling->isChecked()) {
273                 vnc_view->resize(scroll_area->size());
274         }
275
276
277 void MainWindow::forceResizeDelayed()
278 {
279         QTimer::singleShot(500, this, SLOT(forceResize()));
280 }
281
282 void MainWindow::toggleFullscreen()
283 {
284         bool in_fullscreen = windowState() & Qt::WindowFullScreen;
285
286         //hide menu/toolbar in fullscreen (new state is !in_fullscreen)
287         toolbar->setVisible(show_toolbar->isChecked() and in_fullscreen);
288
289 #ifndef Q_WS_MAEMO_5
290         //menu bar is invisible by default on maemo
291         menuBar()->setVisible(in_fullscreen);
292 #endif
293
294         setWindowState(windowState() ^ Qt::WindowFullScreen); 
295         forceResizeDelayed();
296 }
297
298 void MainWindow::showKeyMenu()
299 {
300         static KeyMenu *key_menu = new KeyMenu(this);
301         key_menu->exec();
302
303         vnc_view->sendKeySequence(key_menu->getKeySequence());
304 }
305
306 void MainWindow::showPreferences()
307 {
308         Preferences *p = new Preferences(this);
309         p->exec();
310         delete p;
311
312         reloadSettings();
313 }
314
315 void MainWindow::reloadSettings()
316 {
317         QSettings settings;
318 #ifdef Q_WS_MAEMO_5
319         int rotation = settings.value("screen_rotation", 0).toInt();
320         setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
321         setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
322         setAttribute(Qt::WA_Maemo5PortraitOrientation, rotation == 2);
323 #endif
324
325         if(vnc_view)
326                 vnc_view->reloadSettings();
327 }
328
329 void MainWindow::resizeEvent(QResizeEvent *event)
330 {
331         QMainWindow::resizeEvent(event);
332
333         forceResize();
334 }
335
336 void MainWindow::showInputPanel()
337 {
338 #ifdef Q_WS_MAEMO_5
339         vnc_view->setAttribute(Qt::WA_InputMethodEnabled, true);
340
341         QEvent event(QEvent::RequestSoftwareInputPanel);
342         QApplication::sendEvent(vnc_view, &event);
343 #endif
344 }