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"
21 #include "mainwindow.h"
22 #include "preferences.h"
29 #include <X11/Xatom.h>
32 MainWindow::MainWindow(QString url, int quality):
35 scroll_area(new ScrollArea(0))
37 setWindowTitle("Presence VNC");
38 setAttribute(Qt::WA_Maemo5StackedWindow);
40 migrateConfiguration();
45 toolbar = new QToolBar(0);
46 toolbar->addAction(tr("Mod"), this, SLOT(showModifierMenu()));
47 toolbar->addAction(tr("Tab"), this, SLOT(sendTab()));
48 toolbar->addAction(tr("Esc"), this, SLOT(sendEsc()));
49 toolbar->addAction(tr("PgUp"), this, SLOT(sendPgUp()));
50 toolbar->addAction(tr("PgDn"), this, SLOT(sendPgDn()));
51 toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/chat_enter.png"), "", this, SLOT(sendReturn()));
52 toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/control_keyboard.png"), "", this, SLOT(showInputPanel()));
54 //move fullscreen button to the right
55 QWidget *spacer = new QWidget();
56 spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
57 toolbar->addWidget(spacer);
59 toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_fullsize.png"), "", this, SLOT(toggleFullscreen()));
61 toolbar->setVisible(settings.value("show_toolbar", true).toBool());
64 QMenuBar *menu = new QMenuBar(this);
65 QAction *connect_action = new QAction(tr("Connect"), this);
66 disconnect_action = new QAction(tr("Disconnect"), this);
67 menu->addAction(connect_action);
68 menu->addAction(disconnect_action);
69 scaling = new QAction(tr("Fit to Screen"), this);
70 scaling->setCheckable(true);
71 scaling->setChecked(settings.value("rescale", true).toBool());
72 menu->addAction(scaling);
73 show_toolbar = new QAction(tr("Show Toolbar"), this);
74 show_toolbar->setCheckable(true);
75 show_toolbar->setChecked(settings.value("show_toolbar", true).toBool());
76 menu->addAction(show_toolbar);
77 QAction *pref_action = new QAction(tr("Preferences"), this);
78 menu->addAction(pref_action);
79 QAction *about_action = new QAction(tr("About"), this);
80 menu->addAction(about_action);
82 connect(about_action, SIGNAL(triggered()),
84 connect(pref_action, SIGNAL(triggered()),
85 this, SLOT(showPreferences()));
86 connect(connect_action, SIGNAL(triggered()),
87 this, SLOT(showConnectDialog()));
88 connect(disconnect_action, SIGNAL(triggered()),
89 this, SLOT(disconnectFromHost()));
90 connect(show_toolbar, SIGNAL(toggled(bool)),
91 toolbar, SLOT(setVisible(bool)));
92 connect(show_toolbar, SIGNAL(toggled(bool)),
93 this, SLOT(forceResizeDelayed()));
95 setCentralWidget(scroll_area);
96 new FullScreenExitButton(this);
101 connect(QApplication::desktop(), SIGNAL(resized(int)),
102 this, SLOT(forceResize()));
105 disconnect_action->setEnabled(false);
106 toolbar->setEnabled(false);
109 vnc_view = new VncView(this, url, RemoteView::Quality(quality));
110 connect(scaling, SIGNAL(toggled(bool)),
111 vnc_view, SLOT(enableScaling(bool)));
112 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
113 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
114 scroll_area->setWidget(vnc_view);
116 vnc_view->enableScaling(scaling->isChecked());
120 void MainWindow::grabZoomKeys(bool grab)
122 unsigned long val = (grab)?1:0;
123 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
125 qWarning("Couldn't get zoom key atom");
128 XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
129 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
132 void MainWindow::closeEvent(QCloseEvent*) {
136 settings.setValue("show_toolbar", show_toolbar->isChecked());
137 settings.setValue("rescale", scaling->isChecked());
142 disconnectFromHost();
145 void MainWindow::about() {
146 QMessageBox::about(this, tr("About Presence VNC"),
147 tr("<center><h1>Presence VNC 0.4</h1>\
148 A touchscreen friendly VNC client\
149 <a href=\"https://garage.maemo.org/projects/presencevnc/\">https://garage.maemo.org/projects/presencevnc</a></center>\
150 <small><p>©2010 Christian Pulvermacher <pulvermacher@gmx.de></p>\
151 <p>Based on KRDC, © 2007-2008 Urs Wolfer</p>\
152 <p>and LibVNCServer, © 2001-2003 Johannes E. Schindelin</p>\
153 <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>"));
156 void MainWindow::showConnectDialog()
158 ConnectDialog *connect_dialog = new ConnectDialog(this);
159 connect(connect_dialog, SIGNAL(connectToHost(QString)),
160 this, SLOT(connectToHost(QString)));
161 connect_dialog->exec();
164 void MainWindow::connectToHost(QString url)
166 disconnectFromHost();
168 vnc_view = new VncView(this, url, RemoteView::Quality(2));
170 connect(scaling, SIGNAL(toggled(bool)),
171 vnc_view, SLOT(enableScaling(bool)));
172 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
173 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
174 scroll_area->setWidget(vnc_view);
176 vnc_view->enableScaling(scaling->isChecked());
177 disconnect_action->setEnabled(true);
178 toolbar->setEnabled(true);
181 void MainWindow::disconnectFromHost()
186 scroll_area->setWidget(0);
188 vnc_view->disconnect(); //remove all signal-slot connections
191 disconnect_action->setEnabled(false);
192 toolbar->setEnabled(false);
195 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
197 static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
200 case RemoteView::Connecting:
201 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
203 case RemoteView::Connected:
204 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
205 if(!scaling->isChecked()) {
206 //ugly hack to force a refresh (forceFullRepaint() doesn't repaint?? -> vnc_view hidden???)
207 vnc_view->resize(scroll_area->size());
208 vnc_view->enableScaling(false);
211 case RemoteView::Disconnecting:
212 if(old_status != RemoteView::Disconnected) { //Disconnecting also occurs while connecting, so check last state
213 QMaemo5InformationBox::information(this, tr("Connection lost"));
216 scroll_area->setWidget(0);
218 disconnect_action->setEnabled(false);
219 toolbar->setEnabled(false);
221 //exit fullscreen mode
222 if(windowState() & Qt::WindowFullScreen)
223 setWindowState(windowState() ^ Qt::WindowFullScreen);
226 case RemoteView::Disconnected:
227 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
228 if(old_status == RemoteView::Disconnecting) {
229 scroll_area->setWidget(0); //remove widget
237 //when rescaling is enabled, this resizes the widget to use available screen space
238 //necessary when rotating, showing fullscreen, etc.
239 void MainWindow::forceResize()
241 if(vnc_view and scaling->isChecked()) {
242 vnc_view->resize(scroll_area->size());
246 void MainWindow::forceResizeDelayed()
248 QTimer::singleShot(500, this, SLOT(forceResize()));
251 void MainWindow::toggleFullscreen()
253 toolbar->setVisible(show_toolbar->isChecked() and (windowState() & Qt::WindowFullScreen)); //hide toolbar in fullscreen
254 setWindowState(windowState() ^ Qt::WindowFullScreen);
255 forceResizeDelayed();
258 void MainWindow::showModifierMenu()
260 static QMenu *mod_menu = new QMenu(tr("Modifiers"), this);
261 static QAction *win = mod_menu->addAction(tr("Win"));
262 static QAction *alt = mod_menu->addAction(tr("Alt"));
263 win->setCheckable(true);
264 alt->setCheckable(true);
266 //show menu at top-left corner of toolbar
267 QAction *chosen = mod_menu->exec(toolbar->mapToGlobal(QPoint(0,0)));
270 } else if(chosen == alt) {
271 vnc_view->sendKey(Qt::Key_Alt);
272 } else if(chosen == win) {
273 vnc_view->sendKey(Qt::Key_Meta);
277 void MainWindow::showPreferences()
279 Preferences *p = new Preferences(this);
286 void MainWindow::reloadSettings()
289 int rotation = settings.value("screen_rotation", 0).toInt();
290 setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
291 setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
292 setAttribute(Qt::WA_Maemo5PortraitOrientation, rotation == 2);
295 vnc_view->reloadSettings();
298 void MainWindow::showInputPanel()
300 vnc_view->setAttribute(Qt::WA_InputMethodEnabled, true);
301 QEvent event(QEvent::RequestSoftwareInputPanel);
302 QApplication::sendEvent(vnc_view, &event);