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