add return key to toolbar as input method is broken
[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 "mainwindow.h"
21 #include "preferences.h"
22 #include "vncview.h"
23
24 #include <QtMaemo5>
25 #include <QX11Info>
26
27 #include <X11/Xlib.h>
28 #include <X11/Xatom.h>
29
30 #include <iostream>
31
32 MainWindow::MainWindow(QString url, int quality):
33         QMainWindow(0),
34         vnc_view(0),
35         scroll_area(new QScrollArea(0))
36 {
37         setWindowTitle("Presence VNC");
38         setAttribute(Qt::WA_Maemo5StackedWindow);
39
40         migrateConfiguration();
41
42         QSettings settings;
43
44         //set up toolbar
45         toolbar = new QToolBar(0);
46         toolbar->addAction("Mod", this, SLOT(showModifierMenu()));
47         toolbar->addAction("Tab", this, SLOT(sendTab()));
48         toolbar->addAction("Esc", this, SLOT(sendEsc()));
49         toolbar->addAction("PgUp", this, SLOT(sendPgUp()));
50         toolbar->addAction("PgDn", this, SLOT(sendPgDn()));
51         toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/chat_enter.png"), "", this, SLOT(sendReturn()));
52         toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/control_keyboard.png"), "", this, SLOT(showInputPanel()));
53         toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_fullsize.png"), "", this, SLOT(toggleFullscreen()));
54         addToolBar(toolbar);
55         toolbar->setVisible(settings.value("show_toolbar", true).toBool());
56
57         //set up menu
58         QMenuBar *menu = new QMenuBar(this);
59         QAction *connect_action = new QAction("Connect", this);
60         disconnect_action = new QAction("Disconnect", this);
61         menu->addAction(connect_action);
62         menu->addAction(disconnect_action);
63         scaling = new QAction("Fit to Screen", this);
64         scaling->setCheckable(true);
65         scaling->setChecked(settings.value("rescale", true).toBool());
66         menu->addAction(scaling);
67         QAction *show_toolbar = new QAction("Show Toolbar", this);
68         show_toolbar->setCheckable(true);
69         show_toolbar->setChecked(settings.value("show_toolbar", true).toBool());
70         menu->addAction(show_toolbar);
71         QAction *pref_action = new QAction("Preferences", this);
72         menu->addAction(pref_action);
73         QAction *about_action = new QAction("About", this);
74         menu->addAction(about_action);
75
76         //menu->setAttribute(Qt::WA_Maemo5StackedWindow);
77         //menu->hide();
78
79         connect(about_action, SIGNAL(triggered()),
80                 this, SLOT(about()));
81         connect(pref_action, SIGNAL(triggered()),
82                 this, SLOT(showPreferences()));
83         connect(connect_action, SIGNAL(triggered()),
84                 this, SLOT(showConnectDialog()));
85         connect(disconnect_action, SIGNAL(triggered()),
86                 this, SLOT(disconnectFromHost()));
87         connect(show_toolbar, SIGNAL(toggled(bool)),
88                 toolbar, SLOT(setVisible(bool)));
89         connect(show_toolbar, SIGNAL(toggled(bool)),
90                 this, SLOT(forceResizeDelayed()));
91
92         setCentralWidget(scroll_area);
93
94         grabZoomKeys(true);
95         reloadSettings();
96
97         connect(QApplication::desktop(), SIGNAL(resized(int)),
98                 this, SLOT(forceResize()));
99
100         if(url.isNull()) {
101                 disconnect_action->setEnabled(false);
102                 toolbar->setEnabled(false);
103                 showConnectDialog();
104         } else {
105                 vnc_view = new VncView(this, url, RemoteView::Quality(quality));
106                 connect(scaling, SIGNAL(toggled(bool)),
107                         vnc_view, SLOT(enableScaling(bool)));
108                 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
109                         this, SLOT(statusChanged(RemoteView::RemoteStatus)));
110                 scroll_area->setWidget(vnc_view);
111                 vnc_view->start();
112                 vnc_view->enableScaling(scaling->isChecked());
113         }
114 }
115
116 void MainWindow::grabZoomKeys(bool grab)
117 {
118         unsigned long val = (grab)?1:0;
119         Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
120         if(!atom) {
121                 qWarning("Couldn't get zoom key atom");
122                 return;
123         }
124         XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
125                 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
126 }
127
128 void MainWindow::closeEvent(QCloseEvent*) {
129         grabZoomKeys(false);
130
131         QSettings settings;
132         settings.setValue("show_toolbar", toolbar->isVisible());
133         settings.setValue("rescale", scaling->isChecked());
134         settings.sync();
135
136         hide();
137
138         disconnectFromHost();
139 }
140
141 void MainWindow::about() {
142         QMessageBox::about(this, tr("About Presence VNC"),
143                 tr("<center><h1>Presence VNC 0.4</h1>\
144 A touchscreen friendly VNC client\
145 <small><p>&copy;2010 Christian Pulvermacher &lt;pulvermacher@gmx.de&gt;</p>\
146 <p>Based on KRDC, &copy; 2007-2008 Urs Wolfer</p>\
147 <p>and LibVNCServer, &copy; 2001-2003 Johannes E. Schindelin</p></small></center>\
148 <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>"));
149 }
150
151 void MainWindow::showConnectDialog()
152 {
153         /*
154         QSettings settings;
155         QString url = QInputDialog::getText(this, "Connect to Host", "VNC Server:", QLineEdit::Normal, settings.value("last_hostname", "").toString());
156         if(url.isEmpty()) { //dialog dismissed or nothing entered
157                 return;
158         }
159         */
160
161         ConnectDialog *connect_dialog = new ConnectDialog(this);
162         if(!connect_dialog->exec()) { //dialog rejected
163                 delete connect_dialog;
164                 return;
165         }
166
167         QString url = connect_dialog->getUrl();
168         delete connect_dialog;
169
170         disconnectFromHost();
171
172         vnc_view = new VncView(this, url, RemoteView::Quality(2)); //TODO: get quality in dialog
173
174         connect(scaling, SIGNAL(toggled(bool)),
175                 vnc_view, SLOT(enableScaling(bool)));
176         connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
177                 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
178         scroll_area->setWidget(vnc_view);
179         vnc_view->start();
180         vnc_view->enableScaling(scaling->isChecked());
181         disconnect_action->setEnabled(true);
182         toolbar->setEnabled(true);
183 }
184
185 void MainWindow::disconnectFromHost()
186 {
187         if(!vnc_view)
188                 return;
189
190         scroll_area->setWidget(0);
191
192         vnc_view->disconnect(); //remove all signal-slot connections
193         delete vnc_view;
194         vnc_view = 0;
195         disconnect_action->setEnabled(false);
196         toolbar->setEnabled(false);
197 }
198
199 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
200 {
201         static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
202
203         switch(status) {
204         case RemoteView::Connecting:
205                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
206                 break;
207         case RemoteView::Connected:
208                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
209                 if(!scaling->isChecked()) {
210                         //if remote desktop is shown in full size, 2nd connection will have black screen
211                         //ugly hack to force a refresh (forceFullRepaint() doesn't repaint?? -> vnc_view hidden???)
212                         vnc_view->resize(scroll_area->size());
213                         vnc_view->enableScaling(false);
214                 }
215                 break;
216         case RemoteView::Disconnecting:
217                 if(old_status != RemoteView::Disconnected) { //Disconnecting also occurs while connecting, so check last state
218                         QMaemo5InformationBox::information(this, "Connection lost");
219                         
220                         //clean up
221                         scroll_area->setWidget(0);
222                         vnc_view = 0;
223                         disconnect_action->setEnabled(false);
224                         toolbar->setEnabled(false);
225
226                         //exit fullscreen mode
227                         if(windowState() & Qt::WindowFullScreen)
228                                 setWindowState(windowState() ^ Qt::WindowFullScreen);
229                 }
230                 break;
231         case RemoteView::Disconnected:
232                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
233                 if(old_status == RemoteView::Disconnecting) {
234                         scroll_area->setWidget(0); //remove widget
235                 }
236                 break;
237         }
238
239         old_status = status;
240 }
241
242 //when rescaling is enabled, this resizes the widget to use available screen space
243 //necessary when rotating, showing fullscreen, etc.
244 void MainWindow::forceResize()
245 {
246         if(vnc_view and scaling->isChecked()) {
247                 vnc_view->resize(scroll_area->size());
248         }
249
250
251 void MainWindow::forceResizeDelayed()
252 {
253         QTimer::singleShot(500, this, SLOT(forceResize()));
254 }
255
256 void MainWindow::toggleFullscreen()
257 {
258         setWindowState(windowState() ^ Qt::WindowFullScreen); 
259         forceResizeDelayed();
260 }
261
262 void MainWindow::showModifierMenu()
263 {
264         static QMenu *mod_menu = new QMenu(tr("Modifiers"), this);
265         static QAction *win = mod_menu->addAction(tr("Win"));
266         static QAction *alt = mod_menu->addAction(tr("Alt"));
267         win->setCheckable(true);
268         alt->setCheckable(true);
269
270         //show menu at top-left corner of toolbar
271         QAction *chosen = mod_menu->exec(toolbar->mapToGlobal(QPoint(0,0)));
272         if(!chosen) {
273                 return;
274         } else if(chosen == alt) {
275                 vnc_view->sendKey(Qt::Key_Alt);
276         } else if(chosen == win) {
277                 vnc_view->sendKey(Qt::Key_Meta);
278         } else {
279                 std::cout << "unhandled action?\n";
280         }
281 }
282
283 void MainWindow::showPreferences()
284 {
285         Preferences *p = new Preferences(this);
286         p->exec();
287         delete p;
288
289         reloadSettings();
290 }
291
292 void MainWindow::reloadSettings()
293 {
294         QSettings settings;
295         int rotation = settings.value("screen_rotation", 0).toInt();
296         setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
297         setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
298         setAttribute(Qt::WA_Maemo5PortraitOrientation, rotation == 2);
299
300         if(vnc_view)
301                 vnc_view->reloadSettings();
302 }
303
304 void MainWindow::showInputPanel()
305 {
306         vnc_view->setAttribute(Qt::WA_InputMethodEnabled, true);
307         QEvent event(QEvent::RequestSoftwareInputPanel);
308         QApplication::sendEvent(vnc_view, &event);
309 }