3 Copyright (C) 2010 Christian Pulvermacher
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.
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.
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.
19 #include "connectdialog.h"
20 #include "fullscreenexitbutton.h"
21 #include "mainwindow.h"
22 #include "preferences.h"
29 #include <X11/Xatom.h>
33 MainWindow::MainWindow(QString url, int quality):
36 scroll_area(new ScrollArea(0))
38 setWindowTitle("Presence VNC");
39 setAttribute(Qt::WA_Maemo5StackedWindow);
41 migrateConfiguration();
46 toolbar = new QToolBar(0);
47 toolbar->addAction("Mod", this, SLOT(showModifierMenu()));
48 toolbar->addAction("Tab", this, SLOT(sendTab()));
49 toolbar->addAction("Esc", this, SLOT(sendEsc()));
50 toolbar->addAction("PgUp", this, SLOT(sendPgUp()));
51 toolbar->addAction("PgDn", this, SLOT(sendPgDn()));
52 toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/chat_enter.png"), "", this, SLOT(sendReturn()));
53 toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/control_keyboard.png"), "", this, SLOT(showInputPanel()));
55 //move fullscreen button to the right
56 QWidget *spacer = new QWidget();
57 spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
58 toolbar->addWidget(spacer);
60 toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_fullsize.png"), "", this, SLOT(toggleFullscreen()));
62 toolbar->setVisible(settings.value("show_toolbar", true).toBool());
65 QMenuBar *menu = new QMenuBar(this);
66 QAction *connect_action = new QAction(tr("Connect"), this);
67 disconnect_action = new QAction(tr("Disconnect"), this);
68 menu->addAction(connect_action);
69 menu->addAction(disconnect_action);
70 scaling = new QAction(tr("Fit to Screen"), this);
71 scaling->setCheckable(true);
72 scaling->setChecked(settings.value("rescale", true).toBool());
73 menu->addAction(scaling);
74 show_toolbar = new QAction(tr("Show Toolbar"), this);
75 show_toolbar->setCheckable(true);
76 show_toolbar->setChecked(settings.value("show_toolbar", true).toBool());
77 menu->addAction(show_toolbar);
78 QAction *pref_action = new QAction(tr("Preferences"), this);
79 menu->addAction(pref_action);
80 QAction *about_action = new QAction(tr("About"), this);
81 menu->addAction(about_action);
83 connect(about_action, SIGNAL(triggered()),
85 connect(pref_action, SIGNAL(triggered()),
86 this, SLOT(showPreferences()));
87 connect(connect_action, SIGNAL(triggered()),
88 this, SLOT(showConnectDialog()));
89 connect(disconnect_action, SIGNAL(triggered()),
90 this, SLOT(disconnectFromHost()));
91 connect(show_toolbar, SIGNAL(toggled(bool)),
92 toolbar, SLOT(setVisible(bool)));
93 connect(show_toolbar, SIGNAL(toggled(bool)),
94 this, SLOT(forceResizeDelayed()));
96 setCentralWidget(scroll_area);
97 new FullScreenExitButton(this);
102 connect(QApplication::desktop(), SIGNAL(resized(int)),
103 this, SLOT(forceResize()));
106 disconnect_action->setEnabled(false);
107 toolbar->setEnabled(false);
110 vnc_view = new VncView(this, url, RemoteView::Quality(quality));
111 connect(scaling, SIGNAL(toggled(bool)),
112 vnc_view, SLOT(enableScaling(bool)));
113 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
114 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
115 scroll_area->setWidget(vnc_view);
117 vnc_view->enableScaling(scaling->isChecked());
121 void MainWindow::grabZoomKeys(bool grab)
123 unsigned long val = (grab)?1:0;
124 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
126 qWarning("Couldn't get zoom key atom");
129 XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
130 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
133 void MainWindow::closeEvent(QCloseEvent*) {
137 settings.setValue("show_toolbar", show_toolbar->isChecked());
138 settings.setValue("rescale", scaling->isChecked());
143 disconnectFromHost();
146 void MainWindow::about() {
147 QMessageBox::about(this, tr("About Presence VNC"),
148 tr("<center><h1>Presence VNC 0.4</h1>\
149 A touchscreen friendly VNC client\
150 <a href=\"https://garage.maemo.org/projects/presencevnc/\">https://garage.maemo.org/projects/presencevnc</a></center>\
151 <small><p>©2010 Christian Pulvermacher <pulvermacher@gmx.de></p>\
152 <p>Based on KRDC, © 2007-2008 Urs Wolfer</p>\
153 <p>and LibVNCServer, © 2001-2003 Johannes E. Schindelin</p>\
154 <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>"));
157 void MainWindow::showConnectDialog()
159 ConnectDialog *connect_dialog = new ConnectDialog(this);
160 connect(connect_dialog, SIGNAL(connectToHost(QString)),
161 this, SLOT(connectToHost(QString)));
162 connect_dialog->exec();
165 void MainWindow::connectToHost(QString url)
167 disconnectFromHost();
169 vnc_view = new VncView(this, url, RemoteView::Quality(2)); //TODO: get quality in dialog
171 connect(scaling, SIGNAL(toggled(bool)),
172 vnc_view, SLOT(enableScaling(bool)));
173 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
174 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
175 scroll_area->setWidget(vnc_view);
177 vnc_view->enableScaling(scaling->isChecked());
178 disconnect_action->setEnabled(true);
179 toolbar->setEnabled(true);
182 void MainWindow::disconnectFromHost()
187 scroll_area->setWidget(0);
189 vnc_view->disconnect(); //remove all signal-slot connections
192 disconnect_action->setEnabled(false);
193 toolbar->setEnabled(false);
196 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
198 static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
201 case RemoteView::Connecting:
202 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
204 case RemoteView::Connected:
205 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
206 if(!scaling->isChecked()) {
207 //if remote desktop is shown in full size, 2nd connection will have black screen
208 //ugly hack to force a refresh (forceFullRepaint() doesn't repaint?? -> vnc_view hidden???)
209 vnc_view->resize(scroll_area->size());
210 vnc_view->enableScaling(false);
213 case RemoteView::Disconnecting:
214 if(old_status != RemoteView::Disconnected) { //Disconnecting also occurs while connecting, so check last state
215 QMaemo5InformationBox::information(this, tr("Connection lost"));
218 scroll_area->setWidget(0);
220 disconnect_action->setEnabled(false);
221 toolbar->setEnabled(false);
223 //exit fullscreen mode
224 if(windowState() & Qt::WindowFullScreen)
225 setWindowState(windowState() ^ Qt::WindowFullScreen);
228 case RemoteView::Disconnected:
229 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
230 if(old_status == RemoteView::Disconnecting) {
231 scroll_area->setWidget(0); //remove widget
239 //when rescaling is enabled, this resizes the widget to use available screen space
240 //necessary when rotating, showing fullscreen, etc.
241 void MainWindow::forceResize()
243 if(vnc_view and scaling->isChecked()) {
244 vnc_view->resize(scroll_area->size());
248 void MainWindow::forceResizeDelayed()
250 QTimer::singleShot(500, this, SLOT(forceResize()));
253 void MainWindow::toggleFullscreen()
255 toolbar->setVisible(show_toolbar->isChecked() and (windowState() & Qt::WindowFullScreen)); //hide toolbar in fullscreen
256 setWindowState(windowState() ^ Qt::WindowFullScreen);
257 forceResizeDelayed();
260 void MainWindow::showModifierMenu()
262 static QMenu *mod_menu = new QMenu(tr("Modifiers"), this);
263 static QAction *win = mod_menu->addAction(tr("Win"));
264 static QAction *alt = mod_menu->addAction(tr("Alt"));
265 win->setCheckable(true);
266 alt->setCheckable(true);
268 //show menu at top-left corner of toolbar
269 QAction *chosen = mod_menu->exec(toolbar->mapToGlobal(QPoint(0,0)));
272 } else if(chosen == alt) {
273 vnc_view->sendKey(Qt::Key_Alt);
274 } else if(chosen == win) {
275 vnc_view->sendKey(Qt::Key_Meta);
277 std::cout << "unhandled action?\n";
281 void MainWindow::showPreferences()
283 Preferences *p = new Preferences(this);
290 void MainWindow::reloadSettings()
293 int rotation = settings.value("screen_rotation", 0).toInt();
294 setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
295 setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
296 setAttribute(Qt::WA_Maemo5PortraitOrientation, rotation == 2);
299 vnc_view->reloadSettings();
302 void MainWindow::showInputPanel()
304 vnc_view->setAttribute(Qt::WA_InputMethodEnabled, true);
305 QEvent event(QEvent::RequestSoftwareInputPanel);
306 QApplication::sendEvent(vnc_view, &event);