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"
22 #include "mainwindow.h"
23 #include "preferences.h"
31 #include <X11/Xatom.h>
35 MainWindow::MainWindow(QString url, int quality):
38 scroll_area(new ScrollArea(0)),
41 setWindowTitle("Presence VNC");
43 migrateConfiguration();
47 setAttribute(Qt::WA_Maemo5StackedWindow);
51 toolbar = new QToolBar(0);
52 toolbar->addAction(QChar(0x2026), this, SLOT(showKeyMenu())); //"..." button
53 toolbar->addAction(tr("Tab"), this, SLOT(sendTab()));
54 toolbar->addAction(tr("Esc"), this, SLOT(sendEsc()));
55 toolbar->addAction(tr("PgUp"), this, SLOT(sendPgUp()));
56 toolbar->addAction(tr("PgDn"), this, SLOT(sendPgDn()));
58 toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/chat_enter.png"), "", this, SLOT(sendReturn()));
59 toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/control_keyboard.png"), "", this, SLOT(showInputPanel()));
62 //move fullscreen button to the right
63 QWidget *spacer = new QWidget();
64 spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
65 toolbar->addWidget(spacer);
67 toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_fullsize.png"), "", this, SLOT(toggleFullscreen()));
69 toolbar->setVisible(settings.value("show_toolbar", true).toBool());
72 QAction *connect_action = new QAction(tr("Connect"), this);
73 disconnect_action = new QAction(tr("Disconnect"), this);
74 scaling = new QAction(tr("Fit to Screen"), this);
75 scaling->setCheckable(true);
76 scaling->setChecked(settings.value("rescale", true).toBool());
77 show_toolbar = new QAction(tr("Show Toolbar"), this);
78 show_toolbar->setCheckable(true);
79 show_toolbar->setChecked(settings.value("show_toolbar", true).toBool());
80 QAction *pref_action = new QAction(tr("Preferences"), this);
81 QAction *about_action = new QAction(tr("About"), this);
84 menuBar()->addAction(connect_action);
85 menuBar()->addAction(disconnect_action);
86 menuBar()->addAction(scaling);
87 menuBar()->addAction(show_toolbar);
88 menuBar()->addAction(pref_action);
89 menuBar()->addAction(about_action);
91 QMenu* session_menu = menuBar()->addMenu(tr("&Session"));
92 session_menu->addAction(connect_action);
93 session_menu->addAction(disconnect_action);
94 session_menu->addSeparator();
95 session_menu->addAction(pref_action);
96 session_menu->addSeparator();
97 session_menu->addAction(tr("&Quit"), this, SLOT(close()));
99 QMenu* view_menu = menuBar()->addMenu(tr("&View"));
100 view_menu->addAction(scaling);
101 view_menu->addAction(show_toolbar);
103 QMenu* help_menu = menuBar()->addMenu(tr("&Help"));
104 help_menu->addAction(about_action);
107 connect(about_action, SIGNAL(triggered()),
108 this, SLOT(about()));
109 connect(pref_action, SIGNAL(triggered()),
110 this, SLOT(showPreferences()));
111 connect(connect_action, SIGNAL(triggered()),
112 this, SLOT(showConnectDialog()));
113 connect(disconnect_action, SIGNAL(triggered()),
114 this, SLOT(disconnectFromHost()));
115 connect(show_toolbar, SIGNAL(toggled(bool)),
116 toolbar, SLOT(setVisible(bool)));
117 connect(show_toolbar, SIGNAL(toggled(bool)),
118 this, SLOT(forceResizeDelayed()));
120 setCentralWidget(scroll_area);
121 new FullScreenExitButton(this);
127 disconnect_action->setEnabled(false);
128 toolbar->setEnabled(false);
131 vnc_view = new VncView(this, url, RemoteView::Quality(quality));
132 connect(scaling, SIGNAL(toggled(bool)),
133 vnc_view, SLOT(enableScaling(bool)));
134 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
135 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
136 scroll_area->setWidget(vnc_view);
138 vnc_view->enableScaling(scaling->isChecked());
139 key_menu = new KeyMenu(this);
143 void MainWindow::grabZoomKeys(bool grab)
146 unsigned long val = (grab)?1:0;
147 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
149 qWarning("Couldn't get zoom key atom");
152 XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
153 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
157 void MainWindow::closeEvent(QCloseEvent*) {
161 settings.setValue("show_toolbar", show_toolbar->isChecked());
162 settings.setValue("rescale", scaling->isChecked());
167 disconnectFromHost();
170 void MainWindow::about() {
171 QMessageBox::about(this, tr("About Presence VNC"),
172 tr("<center><h1>Presence VNC 0.5</h1>\
173 <p>A touchscreen friendly VNC client</p>\
174 <p><a href=\"https://garage.maemo.org/projects/presencevnc/\">https://garage.maemo.org/projects/presencevnc</a></p></center>\
175 <small><p>©2010 Christian Pulvermacher <pulvermacher@gmx.de></p>\
176 <p>Based on KRDC, © 2007-2008 Urs Wolfer</p>\
177 <p>and LibVNCServer, © 2001-2003 Johannes E. Schindelin</p>\
178 <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>"));
181 void MainWindow::showConnectDialog()
183 ConnectDialog *connect_dialog = new ConnectDialog(this);
184 connect(connect_dialog, SIGNAL(connectToHost(QString, int)),
185 this, SLOT(connectToHost(QString, int)));
186 connect_dialog->exec();
189 void MainWindow::connectToHost(QString url, int quality)
191 disconnectFromHost();
193 vnc_view = new VncView(this, url, RemoteView::Quality(quality));
195 connect(scaling, SIGNAL(toggled(bool)),
196 vnc_view, SLOT(enableScaling(bool)));
197 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
198 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
199 scroll_area->setWidget(vnc_view);
201 vnc_view->enableScaling(scaling->isChecked());
202 disconnect_action->setEnabled(true);
203 toolbar->setEnabled(true);
207 key_menu = new KeyMenu(this);
210 void MainWindow::disconnectFromHost()
215 scroll_area->setWidget(0);
217 vnc_view->disconnect(); //remove all signal-slot connections
220 disconnect_action->setEnabled(false);
221 toolbar->setEnabled(false);
224 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
226 static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
229 case RemoteView::Connecting:
231 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
234 case RemoteView::Connected:
236 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
238 if(!scaling->isChecked()) {
239 //ugly hack to force a refresh (forceFullRepaint() doesn't repaint?? -> vnc_view hidden???)
240 vnc_view->resize(scroll_area->size());
241 vnc_view->enableScaling(false);
244 case RemoteView::Disconnecting:
245 if(old_status != RemoteView::Disconnected) { //Disconnecting also occurs while connecting, so check last state
247 QMaemo5InformationBox::information(this, tr("Connection lost"));
251 scroll_area->setWidget(0);
253 disconnect_action->setEnabled(false);
254 toolbar->setEnabled(false);
256 //exit fullscreen mode
257 if(windowState() & Qt::WindowFullScreen)
258 setWindowState(windowState() ^ Qt::WindowFullScreen);
261 case RemoteView::Disconnected:
263 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
265 if(old_status == RemoteView::Disconnecting) {
266 scroll_area->setWidget(0); //remove widget
274 //when rescaling is enabled, this resizes the widget to use available screen space
275 //necessary when rotating, showing fullscreen, etc.
276 void MainWindow::forceResize()
278 if(vnc_view and scaling->isChecked()) {
279 vnc_view->resize(scroll_area->size());
283 void MainWindow::forceResizeDelayed()
285 QTimer::singleShot(500, this, SLOT(forceResize()));
288 void MainWindow::toggleFullscreen()
290 bool in_fullscreen = windowState() & Qt::WindowFullScreen;
292 //hide menu/toolbar in fullscreen (new state is !in_fullscreen)
293 toolbar->setVisible(show_toolbar->isChecked() and in_fullscreen);
296 //menu bar is invisible by default on maemo
297 menuBar()->setVisible(in_fullscreen);
300 setWindowState(windowState() ^ Qt::WindowFullScreen);
301 forceResizeDelayed();
304 void MainWindow::showKeyMenu()
307 vnc_view->sendKeySequence(key_menu->getKeySequence());
310 void MainWindow::showPreferences()
312 Preferences *p = new Preferences(this);
319 void MainWindow::reloadSettings()
323 int rotation = settings.value("screen_rotation", 0).toInt();
324 setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
325 setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
326 setAttribute(Qt::WA_Maemo5PortraitOrientation, rotation == 2);
330 vnc_view->reloadSettings();
333 void MainWindow::resizeEvent(QResizeEvent *event)
335 QMainWindow::resizeEvent(event);
340 void MainWindow::showInputPanel()
343 //TODO: when hardware keyboard is open, this will only cause the IM to mess up 'real' key events
344 vnc_view->setAttribute(Qt::WA_InputMethodEnabled, true);
345 vnc_view->setInputMethodHints(Qt::ImhNoAutoUppercase); //without this, IM starts with caps lock
347 QEvent event(QEvent::RequestSoftwareInputPanel);
348 QApplication::sendEvent(vnc_view, &event);