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 "mainwindow.h"
21 #include "preferences.h"
28 #include <X11/Xatom.h>
32 MainWindow::MainWindow(QString url, int quality):
35 scroll_area(new QScrollArea(0))
37 setWindowTitle("Presence VNC");
38 setAttribute(Qt::WA_Maemo5StackedWindow);
40 migrateConfiguration();
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()));
55 toolbar->setVisible(settings.value("show_toolbar", true).toBool());
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);
76 //menu->setAttribute(Qt::WA_Maemo5StackedWindow);
79 connect(about_action, SIGNAL(triggered()),
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()));
92 setCentralWidget(scroll_area);
97 connect(QApplication::desktop(), SIGNAL(resized(int)),
98 this, SLOT(forceResize()));
101 disconnect_action->setEnabled(false);
102 toolbar->setEnabled(false);
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);
112 vnc_view->enableScaling(scaling->isChecked());
116 void MainWindow::grabZoomKeys(bool grab)
118 unsigned long val = (grab)?1:0;
119 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
121 qWarning("Couldn't get zoom key atom");
124 XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
125 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
128 void MainWindow::closeEvent(QCloseEvent*) {
132 settings.setValue("show_toolbar", toolbar->isVisible());
133 settings.setValue("rescale", scaling->isChecked());
138 disconnectFromHost();
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>©2010 Christian Pulvermacher <pulvermacher@gmx.de></p>\
146 <p>Based on KRDC, © 2007-2008 Urs Wolfer</p>\
147 <p>and LibVNCServer, © 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>"));
151 void MainWindow::showConnectDialog()
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
161 ConnectDialog *connect_dialog = new ConnectDialog(this);
162 if(!connect_dialog->exec()) { //dialog rejected
163 delete connect_dialog;
167 QString url = connect_dialog->getUrl();
168 delete connect_dialog;
170 disconnectFromHost();
172 vnc_view = new VncView(this, url, RemoteView::Quality(2)); //TODO: get quality in dialog
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);
180 vnc_view->enableScaling(scaling->isChecked());
181 disconnect_action->setEnabled(true);
182 toolbar->setEnabled(true);
185 void MainWindow::disconnectFromHost()
190 scroll_area->setWidget(0);
192 vnc_view->disconnect(); //remove all signal-slot connections
195 disconnect_action->setEnabled(false);
196 toolbar->setEnabled(false);
199 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
201 static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
204 case RemoteView::Connecting:
205 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
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);
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");
221 scroll_area->setWidget(0);
223 disconnect_action->setEnabled(false);
224 toolbar->setEnabled(false);
226 //exit fullscreen mode
227 if(windowState() & Qt::WindowFullScreen)
228 setWindowState(windowState() ^ Qt::WindowFullScreen);
231 case RemoteView::Disconnected:
232 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
233 if(old_status == RemoteView::Disconnecting) {
234 scroll_area->setWidget(0); //remove widget
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()
246 if(vnc_view and scaling->isChecked()) {
247 vnc_view->resize(scroll_area->size());
251 void MainWindow::forceResizeDelayed()
253 QTimer::singleShot(500, this, SLOT(forceResize()));
256 void MainWindow::toggleFullscreen()
258 setWindowState(windowState() ^ Qt::WindowFullScreen);
259 forceResizeDelayed();
262 void MainWindow::showModifierMenu()
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);
270 //show menu at top-left corner of toolbar
271 QAction *chosen = mod_menu->exec(toolbar->mapToGlobal(QPoint(0,0)));
274 } else if(chosen == alt) {
275 vnc_view->sendKey(Qt::Key_Alt);
276 } else if(chosen == win) {
277 vnc_view->sendKey(Qt::Key_Meta);
279 std::cout << "unhandled action?\n";
283 void MainWindow::showPreferences()
285 Preferences *p = new Preferences(this);
292 void MainWindow::reloadSettings()
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);
301 vnc_view->reloadSettings();
304 void MainWindow::showInputPanel()
306 vnc_view->setAttribute(Qt::WA_InputMethodEnabled, true);
307 QEvent event(QEvent::RequestSoftwareInputPanel);
308 QApplication::sendEvent(vnc_view, &event);