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("IM", this, SLOT(showInputPanel()));
52 toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_fullsize.png"), "", this, SLOT(toggleFullscreen()));
54 toolbar->setVisible(settings.value("show_toolbar", true).toBool());
57 QMenuBar *menu = new QMenuBar(this);
58 QAction *connect_action = new QAction("Connect", this);
59 disconnect_action = new QAction("Disconnect", this);
60 menu->addAction(connect_action);
61 menu->addAction(disconnect_action);
62 scaling = new QAction("Fit to Screen", this);
63 scaling->setCheckable(true);
64 scaling->setChecked(settings.value("rescale", true).toBool());
65 menu->addAction(scaling);
66 QAction *show_toolbar = new QAction("Show Toolbar", this);
67 show_toolbar->setCheckable(true);
68 show_toolbar->setChecked(settings.value("show_toolbar", true).toBool());
69 menu->addAction(show_toolbar);
70 QAction *pref_action = new QAction("Preferences", this);
71 menu->addAction(pref_action);
72 QAction *about_action = new QAction("About", this);
73 menu->addAction(about_action);
75 //menu->setAttribute(Qt::WA_Maemo5StackedWindow);
78 connect(about_action, SIGNAL(triggered()),
80 connect(pref_action, SIGNAL(triggered()),
81 this, SLOT(showPreferences()));
82 connect(connect_action, SIGNAL(triggered()),
83 this, SLOT(showConnectDialog()));
84 connect(disconnect_action, SIGNAL(triggered()),
85 this, SLOT(disconnectFromHost()));
86 connect(show_toolbar, SIGNAL(toggled(bool)),
87 toolbar, SLOT(setVisible(bool)));
88 connect(show_toolbar, SIGNAL(toggled(bool)),
89 this, SLOT(forceResizeDelayed()));
91 setCentralWidget(scroll_area);
96 connect(QApplication::desktop(), SIGNAL(resized(int)),
97 this, SLOT(forceResize()));
100 disconnect_action->setEnabled(false);
101 toolbar->setEnabled(false);
104 vnc_view = new VncView(this, url, RemoteView::Quality(quality));
105 connect(scaling, SIGNAL(toggled(bool)),
106 vnc_view, SLOT(enableScaling(bool)));
107 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
108 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
109 scroll_area->setWidget(vnc_view);
111 vnc_view->enableScaling(scaling->isChecked());
115 void MainWindow::grabZoomKeys(bool grab)
117 unsigned long val = (grab)?1:0;
118 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
120 qWarning("Couldn't get zoom key atom");
123 XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
124 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
127 void MainWindow::closeEvent(QCloseEvent*) {
131 settings.setValue("show_toolbar", toolbar->isVisible());
132 settings.setValue("rescale", scaling->isChecked());
137 disconnectFromHost();
140 void MainWindow::about() {
141 QMessageBox::about(this, tr("About Presence VNC"),
142 tr("<center><h1>Presence VNC 0.4</h1>\
143 A touchscreen friendly VNC client\
144 <small><p>©2010 Christian Pulvermacher <pulvermacher@gmx.de></p>\
145 <p>Based on KRDC, © 2007-2008 Urs Wolfer</p>\
146 <p>and LibVNCServer, © 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>"));
150 void MainWindow::showConnectDialog()
154 QString url = QInputDialog::getText(this, "Connect to Host", "VNC Server:", QLineEdit::Normal, settings.value("last_hostname", "").toString());
155 if(url.isEmpty()) { //dialog dismissed or nothing entered
160 ConnectDialog *connect_dialog = new ConnectDialog(this);
161 if(!connect_dialog->exec()) { //dialog rejected
162 delete connect_dialog;
166 QString url = connect_dialog->getUrl();
167 delete connect_dialog;
169 disconnectFromHost();
171 vnc_view = new VncView(this, url, RemoteView::Quality(2)); //TODO: get quality in dialog
173 connect(scaling, SIGNAL(toggled(bool)),
174 vnc_view, SLOT(enableScaling(bool)));
175 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
176 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
177 scroll_area->setWidget(vnc_view);
179 vnc_view->enableScaling(scaling->isChecked());
180 disconnect_action->setEnabled(true);
181 toolbar->setEnabled(true);
184 void MainWindow::disconnectFromHost()
189 scroll_area->setWidget(0);
191 vnc_view->disconnect(); //remove all signal-slot connections
194 disconnect_action->setEnabled(false);
195 toolbar->setEnabled(false);
198 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
200 static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
203 case RemoteView::Connecting:
204 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
206 case RemoteView::Connected:
207 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
208 if(!scaling->isChecked()) {
209 //if remote desktop is shown in full size, 2nd connection will have black screen
210 //ugly hack to force a refresh (forceFullRepaint() doesn't repaint?? -> vnc_view hidden???)
211 vnc_view->resize(scroll_area->size());
212 vnc_view->enableScaling(false);
215 case RemoteView::Disconnecting:
216 if(old_status != RemoteView::Disconnected) { //Disconnecting also occurs while connecting, so check last state
217 QMaemo5InformationBox::information(this, "Connection lost");
220 scroll_area->setWidget(0);
222 disconnect_action->setEnabled(false);
223 toolbar->setEnabled(false);
225 //exit fullscreen mode
226 if(windowState() & Qt::WindowFullScreen)
227 setWindowState(windowState() ^ Qt::WindowFullScreen);
230 case RemoteView::Disconnected:
231 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
232 if(old_status == RemoteView::Disconnecting) {
233 scroll_area->setWidget(0); //remove widget
241 //when rescaling is enabled, this resizes the widget to use available screen space
242 //necessary when rotating, showing fullscreen, etc.
243 void MainWindow::forceResize()
245 if(vnc_view and scaling->isChecked()) {
246 vnc_view->resize(scroll_area->size());
250 void MainWindow::forceResizeDelayed()
252 QTimer::singleShot(500, this, SLOT(forceResize()));
255 void MainWindow::toggleFullscreen()
257 setWindowState(windowState() ^ Qt::WindowFullScreen);
258 forceResizeDelayed();
261 void MainWindow::showModifierMenu()
263 static QMenu *mod_menu = new QMenu(tr("Modifiers"), this);
264 static QAction *win = mod_menu->addAction(tr("Win"));
265 static QAction *alt = mod_menu->addAction(tr("Alt"));
266 win->setCheckable(true);
267 alt->setCheckable(true);
269 //show menu at top-left corner of toolbar
270 QAction *chosen = mod_menu->exec(toolbar->mapToGlobal(QPoint(0,0)));
273 } else if(chosen == alt) {
274 vnc_view->sendKey(Qt::Key_Alt);
275 } else if(chosen == win) {
276 vnc_view->sendKey(Qt::Key_Meta);
278 std::cout << "unhandled action?\n";
282 void MainWindow::showPreferences()
284 Preferences *p = new Preferences(this);
291 void MainWindow::reloadSettings()
294 int rotation = settings.value("screen_rotation", 0).toInt();
295 setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
296 setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
297 setAttribute(Qt::WA_Maemo5PortraitOrientation, rotation == 2);
300 vnc_view->reloadSettings();
303 void MainWindow::showInputPanel()
305 vnc_view->setAttribute(Qt::WA_InputMethodEnabled, true);
306 QEvent event(QEvent::RequestSoftwareInputPanel);
307 QApplication::sendEvent(vnc_view, &event);