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"
33 #include <X11/Xatom.h>
38 MainWindow::MainWindow(QString url, int quality):
41 scroll_area(new ScrollArea(0)),
44 setWindowTitle("Presence VNC");
46 migrateConfiguration();
50 setAttribute(Qt::WA_Maemo5StackedWindow);
54 toolbar = new QToolBar(0);
55 toolbar->addAction(QChar(0x2026), this, SLOT(showKeyMenu())); //"..." button
56 toolbar->addAction(tr("Tab"), this, SLOT(sendTab()));
57 toolbar->addAction(tr("Esc"), this, SLOT(sendEsc()));
58 toolbar->addAction(tr("PgUp"), this, SLOT(sendPgUp()));
59 toolbar->addAction(tr("PgDn"), this, SLOT(sendPgDn()));
61 toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/chat_enter.png"), "", this, SLOT(sendReturn()));
62 toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/control_keyboard.png"), "", this, SLOT(showInputPanel()));
65 //move fullscreen button to the right
66 QWidget *spacer = new QWidget();
67 spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
68 toolbar->addWidget(spacer);
70 toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_fullsize.png"), "", this, SLOT(toggleFullscreen()));
72 toolbar->setVisible(settings.value("show_toolbar", true).toBool());
75 zoombar = new QToolBar(0);
76 QSlider *zoom_slider = new QSlider(Qt::Horizontal, 0);
77 zoom_slider->setRange(0, 100);
78 connect(zoom_slider, SIGNAL(valueChanged(int)),
79 this, SLOT(setZoomLevel(int)));
80 zoombar->addWidget(zoom_slider);
84 QAction *connect_action = new QAction(tr("Connect"), this);
85 disconnect_action = new QAction(tr("Disconnect"), this);
86 scaling = new QAction(tr("Fit to Screen"), this);
87 scaling->setCheckable(true);
88 scaling->setChecked(settings.value("rescale", true).toBool());
89 show_toolbar = new QAction(tr("Show Toolbar"), this);
90 show_toolbar->setCheckable(true);
91 show_toolbar->setChecked(settings.value("show_toolbar", true).toBool());
92 QAction *pref_action = new QAction(tr("Preferences"), this);
93 QAction *about_action = new QAction(tr("About"), this);
96 menuBar()->addAction(connect_action);
97 menuBar()->addAction(disconnect_action);
98 menuBar()->addAction(scaling);
99 menuBar()->addAction(show_toolbar);
100 menuBar()->addAction(pref_action);
101 menuBar()->addAction(about_action);
103 QMenu* session_menu = menuBar()->addMenu(tr("&Session"));
104 session_menu->addAction(connect_action);
105 session_menu->addAction(disconnect_action);
106 session_menu->addSeparator();
107 session_menu->addAction(pref_action);
108 session_menu->addSeparator();
109 session_menu->addAction(tr("&Quit"), this, SLOT(close()));
111 QMenu* view_menu = menuBar()->addMenu(tr("&View"));
112 view_menu->addAction(scaling);
113 view_menu->addAction(show_toolbar);
115 QMenu* help_menu = menuBar()->addMenu(tr("&Help"));
116 help_menu->addAction(about_action);
119 connect(about_action, SIGNAL(triggered()),
120 this, SLOT(about()));
121 connect(pref_action, SIGNAL(triggered()),
122 this, SLOT(showPreferences()));
123 connect(connect_action, SIGNAL(triggered()),
124 this, SLOT(showConnectDialog()));
125 connect(disconnect_action, SIGNAL(triggered()),
126 this, SLOT(disconnectFromHost()));
127 connect(show_toolbar, SIGNAL(toggled(bool)),
128 toolbar, SLOT(setVisible(bool)));
129 connect(show_toolbar, SIGNAL(toggled(bool)),
130 this, SLOT(forceResizeDelayed()));
132 setCentralWidget(scroll_area);
133 new FullScreenExitButton(this);
139 disconnect_action->setEnabled(false);
140 toolbar->setEnabled(false);
143 vnc_view = new VncView(this, url, RemoteView::Quality(quality));
144 connect(scaling, SIGNAL(toggled(bool)),
145 vnc_view, SLOT(enableScaling(bool)));
146 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
147 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
148 scroll_area->setWidget(vnc_view);
150 vnc_view->enableScaling(scaling->isChecked());
151 key_menu = new KeyMenu(this);
155 void MainWindow::grabZoomKeys(bool grab)
158 unsigned long val = (grab)?1:0;
159 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
161 qWarning("Couldn't get zoom key atom");
164 XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
165 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
169 void MainWindow::closeEvent(QCloseEvent*) {
173 settings.setValue("show_toolbar", show_toolbar->isChecked());
174 settings.setValue("rescale", scaling->isChecked());
179 disconnectFromHost();
182 void MainWindow::about() {
183 QMessageBox::about(this, tr("About Presence VNC"),
184 tr("<center><h1>Presence VNC 0.6</h1>\
185 <p>A touchscreen friendly VNC client</p>\
186 <p><a href=\"https://garage.maemo.org/projects/presencevnc/\">https://garage.maemo.org/projects/presencevnc</a></p></center>\
187 <small><p>©2010 Christian Pulvermacher <pulvermacher@gmx.de><br />\
188 Based on KRDC, © 2007-2008 Urs Wolfer<br />\
189 and LibVNCServer, © 2001-2003 Johannes E. Schindelin</p>\
190 <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>"));
193 void MainWindow::showConnectDialog()
195 ConnectDialog *connect_dialog = new ConnectDialog(this);
196 connect(connect_dialog, SIGNAL(connectToHost(QString, int)),
197 this, SLOT(connectToHost(QString, int)));
198 connect_dialog->exec();
201 void MainWindow::connectToHost(QString url, int quality)
203 disconnectFromHost();
205 vnc_view = new VncView(this, url, RemoteView::Quality(quality));
207 connect(scaling, SIGNAL(toggled(bool)),
208 vnc_view, SLOT(enableScaling(bool)));
209 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
210 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
211 scroll_area->setWidget(vnc_view);
213 vnc_view->enableScaling(scaling->isChecked());
214 disconnect_action->setEnabled(true);
215 toolbar->setEnabled(true);
219 key_menu = new KeyMenu(this);
222 void MainWindow::disconnectFromHost()
227 scroll_area->setWidget(0);
229 vnc_view->disconnect(); //remove all signal-slot connections
232 disconnect_action->setEnabled(false);
233 toolbar->setEnabled(false);
236 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
238 static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
241 case RemoteView::Connecting:
243 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
246 case RemoteView::Connected:
248 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
250 if(!scaling->isChecked()) {
251 //ugly hack to force a refresh (forceFullRepaint() doesn't repaint?? -> vnc_view hidden???)
252 vnc_view->resize(scroll_area->size());
253 vnc_view->enableScaling(false);
256 case RemoteView::Disconnecting:
257 if(old_status != RemoteView::Disconnected) { //Disconnecting also occurs while connecting, so check last state
259 QMaemo5InformationBox::information(this, tr("Connection lost"));
263 scroll_area->setWidget(0);
265 disconnect_action->setEnabled(false);
266 toolbar->setEnabled(false);
268 //exit fullscreen mode
269 if(windowState() & Qt::WindowFullScreen)
270 setWindowState(windowState() ^ Qt::WindowFullScreen);
273 case RemoteView::Disconnected:
275 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
277 if(old_status == RemoteView::Disconnecting) {
278 scroll_area->setWidget(0); //remove widget
286 //when rescaling is enabled, this resizes the widget to use available screen space
287 //necessary when rotating, showing fullscreen, etc.
288 void MainWindow::forceResize()
290 if(vnc_view and scaling->isChecked()) {
291 vnc_view->resize(scroll_area->size());
295 void MainWindow::forceResizeDelayed()
297 QTimer::singleShot(500, this, SLOT(forceResize()));
300 void MainWindow::toggleFullscreen()
302 bool in_fullscreen = windowState() & Qt::WindowFullScreen;
304 //hide menu/toolbar in fullscreen (new state is !in_fullscreen)
305 toolbar->setVisible(show_toolbar->isChecked() and in_fullscreen);
308 //menu bar is invisible by default on maemo
309 menuBar()->setVisible(in_fullscreen);
312 setWindowState(windowState() ^ Qt::WindowFullScreen);
313 forceResizeDelayed();
316 void MainWindow::showKeyMenu()
319 vnc_view->sendKeySequence(key_menu->getKeySequence());
322 void MainWindow::showPreferences()
324 Preferences *p = new Preferences(this);
331 void MainWindow::reloadSettings()
335 int rotation = settings.value("screen_rotation", 0).toInt();
336 setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
337 setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
338 setAttribute(Qt::WA_Maemo5PortraitOrientation, rotation == 2);
342 vnc_view->reloadSettings();
345 void MainWindow::resizeEvent(QResizeEvent *event)
347 QMainWindow::resizeEvent(event);
352 void MainWindow::showInputPanel()
355 //TODO: when hardware keyboard is open, this will only cause the IM to mess up 'real' key events
356 vnc_view->setAttribute(Qt::WA_InputMethodEnabled, true);
357 vnc_view->setInputMethodHints(Qt::ImhNoAutoUppercase); //without this, IM starts with caps lock
359 QEvent event(QEvent::RequestSoftwareInputPanel);
360 QApplication::sendEvent(vnc_view, &event);
364 void MainWindow::setZoomLevel(int level)
367 vnc_view->setZoomLevel(level);