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 <small><p>©2010 Christian Pulvermacher <pulvermacher@gmx.de></p>\
151 <p>Based on KRDC, © 2007-2008 Urs Wolfer</p>\
152 <p>and LibVNCServer, © 2001-2003 Johannes E. Schindelin</p></small></center>\
153 <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>"));
156 void MainWindow::showConnectDialog()
158 ConnectDialog *connect_dialog = new ConnectDialog(this);
159 connect(connect_dialog, SIGNAL(connectToHost(QString)),
160 this, SLOT(connectToHost(QString)));
161 connect_dialog->exec();
164 void MainWindow::connectToHost(QString url)
166 disconnectFromHost();
168 vnc_view = new VncView(this, url, RemoteView::Quality(2)); //TODO: get quality in dialog
170 connect(scaling, SIGNAL(toggled(bool)),
171 vnc_view, SLOT(enableScaling(bool)));
172 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
173 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
174 scroll_area->setWidget(vnc_view);
176 vnc_view->enableScaling(scaling->isChecked());
177 disconnect_action->setEnabled(true);
178 toolbar->setEnabled(true);
181 void MainWindow::disconnectFromHost()
186 scroll_area->setWidget(0);
188 vnc_view->disconnect(); //remove all signal-slot connections
191 disconnect_action->setEnabled(false);
192 toolbar->setEnabled(false);
195 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
197 static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
200 case RemoteView::Connecting:
201 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
203 case RemoteView::Connected:
204 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
205 if(!scaling->isChecked()) {
206 //if remote desktop is shown in full size, 2nd connection will have black screen
207 //ugly hack to force a refresh (forceFullRepaint() doesn't repaint?? -> vnc_view hidden???)
208 vnc_view->resize(scroll_area->size());
209 vnc_view->enableScaling(false);
212 case RemoteView::Disconnecting:
213 if(old_status != RemoteView::Disconnected) { //Disconnecting also occurs while connecting, so check last state
214 QMaemo5InformationBox::information(this, tr("Connection lost"));
217 scroll_area->setWidget(0);
219 disconnect_action->setEnabled(false);
220 toolbar->setEnabled(false);
222 //exit fullscreen mode
223 if(windowState() & Qt::WindowFullScreen)
224 setWindowState(windowState() ^ Qt::WindowFullScreen);
227 case RemoteView::Disconnected:
228 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
229 if(old_status == RemoteView::Disconnecting) {
230 scroll_area->setWidget(0); //remove widget
238 //when rescaling is enabled, this resizes the widget to use available screen space
239 //necessary when rotating, showing fullscreen, etc.
240 void MainWindow::forceResize()
242 if(vnc_view and scaling->isChecked()) {
243 vnc_view->resize(scroll_area->size());
247 void MainWindow::forceResizeDelayed()
249 QTimer::singleShot(500, this, SLOT(forceResize()));
252 void MainWindow::toggleFullscreen()
254 toolbar->setVisible(show_toolbar->isChecked() and (windowState() & Qt::WindowFullScreen)); //hide toolbar in fullscreen
255 setWindowState(windowState() ^ Qt::WindowFullScreen);
256 forceResizeDelayed();
259 void MainWindow::showModifierMenu()
261 static QMenu *mod_menu = new QMenu(tr("Modifiers"), this);
262 static QAction *win = mod_menu->addAction(tr("Win"));
263 static QAction *alt = mod_menu->addAction(tr("Alt"));
264 win->setCheckable(true);
265 alt->setCheckable(true);
267 //show menu at top-left corner of toolbar
268 QAction *chosen = mod_menu->exec(toolbar->mapToGlobal(QPoint(0,0)));
271 } else if(chosen == alt) {
272 vnc_view->sendKey(Qt::Key_Alt);
273 } else if(chosen == win) {
274 vnc_view->sendKey(Qt::Key_Meta);
276 std::cout << "unhandled action?\n";
280 void MainWindow::showPreferences()
282 Preferences *p = new Preferences(this);
289 void MainWindow::reloadSettings()
292 int rotation = settings.value("screen_rotation", 0).toInt();
293 setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
294 setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
295 setAttribute(Qt::WA_Maemo5PortraitOrientation, rotation == 2);
298 vnc_view->reloadSettings();
301 void MainWindow::showInputPanel()
303 vnc_view->setAttribute(Qt::WA_InputMethodEnabled, true);
304 QEvent event(QEvent::RequestSoftwareInputPanel);
305 QApplication::sendEvent(vnc_view, &event);