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)),
39 key_menu(new KeyMenu(this))
41 setWindowTitle("Presence VNC");
43 setAttribute(Qt::WA_Maemo5StackedWindow);
46 migrateConfiguration();
49 toolbar = new QToolBar(0);
50 toolbar->addAction(QChar(0x2026), this, SLOT(showKeyMenu())); //"..." button
51 toolbar->addAction(tr("Tab"), this, SLOT(sendTab()));
52 toolbar->addAction(tr("Esc"), this, SLOT(sendEsc()));
53 toolbar->addAction(tr("PgUp"), this, SLOT(sendPgUp()));
54 toolbar->addAction(tr("PgDn"), this, SLOT(sendPgDn()));
56 toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/chat_enter.png"), "", this, SLOT(sendReturn()));
57 toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/control_keyboard.png"), "", this, SLOT(showInputPanel()));
61 //move remaining buttons to the right
62 QWidget *spacer = new QWidget();
63 spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
64 toolbar->addWidget(spacer);
68 zoom_slider = new QSlider(Qt::Horizontal, 0);
69 zoom_slider->setRange(0, 100);
70 connect(zoom_slider, SIGNAL(valueChanged(int)),
71 this, SLOT(setZoomLevel(int)));
72 zoom_slider->setValue(settings.value("zoomlevel", 95).toInt());
73 toolbar->addWidget(zoom_slider);
75 toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_fullsize.png"), "", this, SLOT(toggleFullscreen()));
77 toolbar->setVisible(settings.value("show_toolbar", true).toBool());
80 QAction *connect_action = new QAction(tr("Connect"), this);
81 disconnect_action = new QAction(tr("Disconnect"), this);
82 show_toolbar = new QAction(tr("Show Toolbar"), this);
83 show_toolbar->setCheckable(true);
84 show_toolbar->setChecked(settings.value("show_toolbar", true).toBool());
85 QAction *pref_action = new QAction(tr("Preferences"), this);
86 QAction *about_action = new QAction(tr("About"), this);
89 menuBar()->addAction(connect_action);
90 menuBar()->addAction(disconnect_action);
91 menuBar()->addAction(show_toolbar);
92 menuBar()->addAction(pref_action);
93 menuBar()->addAction(about_action);
95 QMenu* session_menu = menuBar()->addMenu(tr("&Session"));
96 session_menu->addAction(connect_action);
97 session_menu->addAction(disconnect_action);
98 session_menu->addSeparator();
99 session_menu->addAction(pref_action);
100 session_menu->addSeparator();
101 session_menu->addAction(tr("&Quit"), this, SLOT(close()));
103 QMenu* view_menu = menuBar()->addMenu(tr("&View"));
104 view_menu->addAction(show_toolbar);
106 QMenu* help_menu = menuBar()->addMenu(tr("&Help"));
107 help_menu->addAction(about_action);
110 connect(about_action, SIGNAL(triggered()),
111 this, SLOT(about()));
112 connect(pref_action, SIGNAL(triggered()),
113 this, SLOT(showPreferences()));
114 connect(connect_action, SIGNAL(triggered()),
115 this, SLOT(showConnectDialog()));
116 connect(disconnect_action, SIGNAL(triggered()),
117 this, SLOT(disconnectFromHost()));
118 connect(show_toolbar, SIGNAL(toggled(bool)),
119 toolbar, SLOT(setVisible(bool)));
120 connect(show_toolbar, SIGNAL(toggled(bool)),
121 this, SLOT(forceResizeDelayed()));
123 setCentralWidget(scroll_area);
124 new FullScreenExitButton(this);
130 disconnect_action->setEnabled(false);
131 toolbar->setEnabled(false);
134 vnc_view = new VncView(this, url, RemoteView::Quality(quality));
135 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
136 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
137 scroll_area->setWidget(vnc_view);
142 void MainWindow::grabZoomKeys(bool grab)
145 unsigned long val = (grab)?1:0;
146 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
148 qWarning("Couldn't get zoom key atom");
151 XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
152 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
156 void MainWindow::closeEvent(QCloseEvent*) {
160 settings.setValue("show_toolbar", show_toolbar->isChecked());
161 settings.setValue("zoomlevel", zoom_slider->value());
166 disconnectFromHost();
169 void MainWindow::about() {
170 QMessageBox::about(this, tr("About Presence VNC"),
171 tr("<center><h1>Presence VNC 0.6</h1>\
172 <p>A touchscreen friendly VNC client</p>\
173 <p><a href=\"https://garage.maemo.org/projects/presencevnc/\">https://garage.maemo.org/projects/presencevnc</a></p></center>\
174 <small><p>©2010 Christian Pulvermacher <pulvermacher@gmx.de><br />\
175 Based on KRDC, © 2007-2008 Urs Wolfer<br />\
176 and LibVNCServer, © 2001-2003 Johannes E. Schindelin</p>\
177 <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>"));
180 void MainWindow::showConnectDialog()
182 ConnectDialog *connect_dialog = new ConnectDialog(this);
183 connect(connect_dialog, SIGNAL(connectToHost(QString, int, int)),
184 this, SLOT(connectToHost(QString, int, int)));
185 connect_dialog->exec();
188 void MainWindow::connectToHost(QString url, int quality, int listen_port)
190 disconnectFromHost();
192 vnc_view = new VncView(this, url, RemoteView::Quality(quality), listen_port);
194 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
195 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
196 scroll_area->setWidget(vnc_view);
199 disconnect_action->setEnabled(true);
200 toolbar->setEnabled(true);
204 key_menu = new KeyMenu(this);
207 void MainWindow::disconnectFromHost()
212 scroll_area->setWidget(0);
216 disconnect_action->setEnabled(false);
217 toolbar->setEnabled(false);
220 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
222 static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
225 case RemoteView::Connecting:
227 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
230 case RemoteView::Connected:
232 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
234 vnc_view->setZoomLevel(zoom_slider->value());
235 vnc_view->forceFullRepaint();
237 case RemoteView::Disconnecting:
238 if(old_status != RemoteView::Disconnected) { //Disconnecting also occurs while connecting, so check last state
240 QMaemo5InformationBox::information(this, tr("Connection lost"));
244 scroll_area->setWidget(0);
246 disconnect_action->setEnabled(false);
247 toolbar->setEnabled(false);
249 //exit fullscreen mode
250 if(windowState() & Qt::WindowFullScreen)
251 setWindowState(windowState() ^ Qt::WindowFullScreen);
254 case RemoteView::Disconnected:
256 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
258 if(old_status == RemoteView::Disconnecting) {
259 scroll_area->setWidget(0); //remove widget
262 default: //avoid compiler warnings
269 //resizes the widget to use available screen space
270 //necessary when rotating, showing fullscreen, etc.
271 void MainWindow::forceResize()
274 vnc_view->resize(scroll_area->size());
278 void MainWindow::forceResizeDelayed()
280 QTimer::singleShot(500, this, SLOT(forceResize()));
283 void MainWindow::toggleFullscreen()
285 bool in_fullscreen = windowState() & Qt::WindowFullScreen;
287 //hide menu/toolbar in fullscreen (new state is !in_fullscreen)
288 toolbar->setVisible(show_toolbar->isChecked() and in_fullscreen);
291 //menu bar is invisible by default on maemo
292 menuBar()->setVisible(in_fullscreen);
295 setWindowState(windowState() ^ Qt::WindowFullScreen);
296 forceResizeDelayed();
299 void MainWindow::showKeyMenu()
302 vnc_view->sendKeySequence(key_menu->getKeySequence());
305 void MainWindow::showPreferences()
307 Preferences *p = new Preferences(this);
314 void MainWindow::reloadSettings()
318 int rotation = settings.value("screen_rotation", 0).toInt();
319 setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
320 setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
321 setAttribute(Qt::WA_Maemo5PortraitOrientation, rotation == 2);
325 vnc_view->reloadSettings();
328 void MainWindow::resizeEvent(QResizeEvent *event)
330 QMainWindow::resizeEvent(event);
334 vnc_view->setZoomLevel(zoom_slider->value());
337 //hide zoom slider in portrait mode
338 zoom_slider->setVisible(height() < width());
342 void MainWindow::showInputPanel()
345 //TODO: when hardware keyboard is open, this will only cause the IM to mess up 'real' key events
346 vnc_view->setAttribute(Qt::WA_InputMethodEnabled, true);
347 vnc_view->setInputMethodHints(Qt::ImhNoAutoUppercase); //without this, IM starts with caps lock
349 QEvent event(QEvent::RequestSoftwareInputPanel);
350 QApplication::sendEvent(vnc_view, &event);
354 void MainWindow::setZoomLevel(int level)
359 int old_width = vnc_view->width();
360 QPoint center = vnc_view->visibleRegion().boundingRect().center();
362 vnc_view->setZoomLevel(level);
364 int new_width = vnc_view->width();
365 center = center * (double(new_width)/old_width);
368 scroll_area->ensureVisible(center.x(), center.y(),
369 scroll_area->width()/2, scroll_area->height()/2);