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))
40 setWindowTitle("Presence VNC");
42 migrateConfiguration();
46 setAttribute(Qt::WA_Maemo5StackedWindow);
50 toolbar = new QToolBar(0);
51 toolbar->addAction(QChar(0x2026), this, SLOT(showKeyMenu())); //"..." button
52 toolbar->addAction(tr("Tab"), this, SLOT(sendTab()));
53 toolbar->addAction(tr("Esc"), this, SLOT(sendEsc()));
54 toolbar->addAction(tr("PgUp"), this, SLOT(sendPgUp()));
55 toolbar->addAction(tr("PgDn"), this, SLOT(sendPgDn()));
57 toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/chat_enter.png"), "", this, SLOT(sendReturn()));
58 toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/control_keyboard.png"), "", this, SLOT(showInputPanel()));
61 //move fullscreen button to the right
62 QWidget *spacer = new QWidget();
63 spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
64 toolbar->addWidget(spacer);
66 toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_fullsize.png"), "", this, SLOT(toggleFullscreen()));
68 toolbar->setVisible(settings.value("show_toolbar", true).toBool());
71 QAction *connect_action = new QAction(tr("Connect"), this);
72 disconnect_action = new QAction(tr("Disconnect"), this);
73 scaling = new QAction(tr("Fit to Screen"), this);
74 scaling->setCheckable(true);
75 scaling->setChecked(settings.value("rescale", true).toBool());
76 show_toolbar = new QAction(tr("Show Toolbar"), this);
77 show_toolbar->setCheckable(true);
78 show_toolbar->setChecked(settings.value("show_toolbar", true).toBool());
79 QAction *pref_action = new QAction(tr("Preferences"), this);
80 QAction *about_action = new QAction(tr("About"), this);
83 menuBar()->addAction(connect_action);
84 menuBar()->addAction(disconnect_action);
85 menuBar()->addAction(scaling);
86 menuBar()->addAction(show_toolbar);
87 menuBar()->addAction(pref_action);
88 menuBar()->addAction(about_action);
90 QMenu* session_menu = menuBar()->addMenu(tr("&Session"));
91 session_menu->addAction(connect_action);
92 session_menu->addAction(disconnect_action);
93 session_menu->addSeparator();
94 //session_menu->addAction(pref_action);
95 //session_menu->addSeparator();
96 session_menu->addAction(tr("&Quit"), this, SLOT(close()));
98 QMenu* view_menu = menuBar()->addMenu(tr("&View"));
99 view_menu->addAction(scaling);
100 view_menu->addAction(show_toolbar);
102 QMenu* help_menu = menuBar()->addMenu(tr("&Help"));
103 help_menu->addAction(about_action);
106 connect(about_action, SIGNAL(triggered()),
107 this, SLOT(about()));
108 connect(pref_action, SIGNAL(triggered()),
109 this, SLOT(showPreferences()));
110 connect(connect_action, SIGNAL(triggered()),
111 this, SLOT(showConnectDialog()));
112 connect(disconnect_action, SIGNAL(triggered()),
113 this, SLOT(disconnectFromHost()));
114 connect(show_toolbar, SIGNAL(toggled(bool)),
115 toolbar, SLOT(setVisible(bool)));
116 connect(show_toolbar, SIGNAL(toggled(bool)),
117 this, SLOT(forceResizeDelayed()));
119 setCentralWidget(scroll_area);
120 new FullScreenExitButton(this);
126 disconnect_action->setEnabled(false);
127 toolbar->setEnabled(false);
130 vnc_view = new VncView(this, url, RemoteView::Quality(quality));
131 connect(scaling, SIGNAL(toggled(bool)),
132 vnc_view, SLOT(enableScaling(bool)));
133 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
134 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
135 scroll_area->setWidget(vnc_view);
137 vnc_view->enableScaling(scaling->isChecked());
141 void MainWindow::grabZoomKeys(bool grab)
144 unsigned long val = (grab)?1:0;
145 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
147 qWarning("Couldn't get zoom key atom");
150 XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
151 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
155 void MainWindow::closeEvent(QCloseEvent*) {
159 settings.setValue("show_toolbar", show_toolbar->isChecked());
160 settings.setValue("rescale", scaling->isChecked());
165 disconnectFromHost();
168 void MainWindow::about() {
169 QMessageBox::about(this, tr("About Presence VNC"),
170 tr("<center><h1>Presence VNC 0.5</h1>\
171 <p>A touchscreen friendly VNC client</p>\
172 <p><a href=\"https://garage.maemo.org/projects/presencevnc/\">https://garage.maemo.org/projects/presencevnc</a></p></center>\
173 <small><p>©2010 Christian Pulvermacher <pulvermacher@gmx.de></p>\
174 <p>Based on KRDC, © 2007-2008 Urs Wolfer</p>\
175 <p>and LibVNCServer, © 2001-2003 Johannes E. Schindelin</p>\
176 <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>"));
179 void MainWindow::showConnectDialog()
181 ConnectDialog *connect_dialog = new ConnectDialog(this);
182 connect(connect_dialog, SIGNAL(connectToHost(QString)),
183 this, SLOT(connectToHost(QString)));
184 connect_dialog->exec();
187 void MainWindow::connectToHost(QString url)
189 disconnectFromHost();
191 vnc_view = new VncView(this, url, RemoteView::Quality(2));
193 connect(scaling, SIGNAL(toggled(bool)),
194 vnc_view, SLOT(enableScaling(bool)));
195 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
196 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
197 scroll_area->setWidget(vnc_view);
199 vnc_view->enableScaling(scaling->isChecked());
200 disconnect_action->setEnabled(true);
201 toolbar->setEnabled(true);
204 void MainWindow::disconnectFromHost()
209 scroll_area->setWidget(0);
211 vnc_view->disconnect(); //remove all signal-slot connections
214 disconnect_action->setEnabled(false);
215 toolbar->setEnabled(false);
218 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
220 static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
223 case RemoteView::Connecting:
225 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
228 case RemoteView::Connected:
230 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
232 if(!scaling->isChecked()) {
233 //ugly hack to force a refresh (forceFullRepaint() doesn't repaint?? -> vnc_view hidden???)
234 vnc_view->resize(scroll_area->size());
235 vnc_view->enableScaling(false);
238 case RemoteView::Disconnecting:
239 if(old_status != RemoteView::Disconnected) { //Disconnecting also occurs while connecting, so check last state
241 QMaemo5InformationBox::information(this, tr("Connection lost"));
245 scroll_area->setWidget(0);
247 disconnect_action->setEnabled(false);
248 toolbar->setEnabled(false);
250 //exit fullscreen mode
251 if(windowState() & Qt::WindowFullScreen)
252 setWindowState(windowState() ^ Qt::WindowFullScreen);
255 case RemoteView::Disconnected:
257 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
259 if(old_status == RemoteView::Disconnecting) {
260 scroll_area->setWidget(0); //remove widget
268 //when rescaling is enabled, this resizes the widget to use available screen space
269 //necessary when rotating, showing fullscreen, etc.
270 void MainWindow::forceResize()
272 if(vnc_view and scaling->isChecked()) {
273 vnc_view->resize(scroll_area->size());
277 void MainWindow::forceResizeDelayed()
279 QTimer::singleShot(500, this, SLOT(forceResize()));
282 void MainWindow::toggleFullscreen()
284 bool in_fullscreen = windowState() & Qt::WindowFullScreen;
286 //hide menu/toolbar in fullscreen (new state is !in_fullscreen)
287 toolbar->setVisible(show_toolbar->isChecked() and in_fullscreen);
290 //menu bar is invisible by default on maemo
291 menuBar()->setVisible(in_fullscreen);
294 setWindowState(windowState() ^ Qt::WindowFullScreen);
295 forceResizeDelayed();
298 void MainWindow::showKeyMenu()
300 static KeyMenu *key_menu = new KeyMenu(this);
303 vnc_view->sendKeySequence(key_menu->getKeySequence());
306 void MainWindow::showPreferences()
308 Preferences *p = new Preferences(this);
315 void MainWindow::reloadSettings()
319 int rotation = settings.value("screen_rotation", 0).toInt();
320 setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
321 setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
322 setAttribute(Qt::WA_Maemo5PortraitOrientation, rotation == 2);
326 vnc_view->reloadSettings();
329 void MainWindow::resizeEvent(QResizeEvent *event)
331 QMainWindow::resizeEvent(event);
336 void MainWindow::showInputPanel()
339 vnc_view->setAttribute(Qt::WA_InputMethodEnabled, true);
341 QEvent event(QEvent::RequestSoftwareInputPanel);
342 QApplication::sendEvent(vnc_view, &event);