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>
36 MainWindow::MainWindow(QString url, int quality):
39 scroll_area(new ScrollArea(0)),
42 setWindowTitle("Presence VNC");
44 migrateConfiguration();
48 setAttribute(Qt::WA_Maemo5StackedWindow);
52 toolbar = new QToolBar(0);
53 toolbar->addAction(QChar(0x2026), this, SLOT(showKeyMenu())); //"..." button
54 toolbar->addAction(tr("Tab"), this, SLOT(sendTab()));
55 toolbar->addAction(tr("Esc"), this, SLOT(sendEsc()));
56 toolbar->addAction(tr("PgUp"), this, SLOT(sendPgUp()));
57 toolbar->addAction(tr("PgDn"), this, SLOT(sendPgDn()));
59 toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/chat_enter.png"), "", this, SLOT(sendReturn()));
60 toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/control_keyboard.png"), "", this, SLOT(showInputPanel()));
64 //move remaining buttons to the right
65 QWidget *spacer = new QWidget();
66 spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
67 toolbar->addWidget(spacer);
70 zoom_slider = new QSlider(Qt::Horizontal, 0);
71 zoom_slider->setRange(0, 100);
72 connect(zoom_slider, SIGNAL(valueChanged(int)),
73 this, SLOT(setZoomLevel(int)));
74 zoom_slider->setValue(settings.value("zoomlevel", 95).toInt());
75 toolbar->addWidget(zoom_slider);
77 toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_fullsize.png"), "", this, SLOT(toggleFullscreen()));
79 toolbar->setVisible(settings.value("show_toolbar", true).toBool());
82 QAction *connect_action = new QAction(tr("Connect"), this);
83 disconnect_action = new QAction(tr("Disconnect"), this);
84 show_toolbar = new QAction(tr("Show Toolbar"), this);
85 show_toolbar->setCheckable(true);
86 show_toolbar->setChecked(settings.value("show_toolbar", true).toBool());
87 QAction *pref_action = new QAction(tr("Preferences"), this);
88 QAction *about_action = new QAction(tr("About"), this);
91 menuBar()->addAction(connect_action);
92 menuBar()->addAction(disconnect_action);
93 menuBar()->addAction(show_toolbar);
94 menuBar()->addAction(pref_action);
95 menuBar()->addAction(about_action);
97 QMenu* session_menu = menuBar()->addMenu(tr("&Session"));
98 session_menu->addAction(connect_action);
99 session_menu->addAction(disconnect_action);
100 session_menu->addSeparator();
101 session_menu->addAction(pref_action);
102 session_menu->addSeparator();
103 session_menu->addAction(tr("&Quit"), this, SLOT(close()));
105 QMenu* view_menu = menuBar()->addMenu(tr("&View"));
106 view_menu->addAction(show_toolbar);
108 QMenu* help_menu = menuBar()->addMenu(tr("&Help"));
109 help_menu->addAction(about_action);
112 connect(about_action, SIGNAL(triggered()),
113 this, SLOT(about()));
114 connect(pref_action, SIGNAL(triggered()),
115 this, SLOT(showPreferences()));
116 connect(connect_action, SIGNAL(triggered()),
117 this, SLOT(showConnectDialog()));
118 connect(disconnect_action, SIGNAL(triggered()),
119 this, SLOT(disconnectFromHost()));
120 connect(show_toolbar, SIGNAL(toggled(bool)),
121 toolbar, SLOT(setVisible(bool)));
122 connect(show_toolbar, SIGNAL(toggled(bool)),
123 this, SLOT(forceResizeDelayed()));
125 setCentralWidget(scroll_area);
126 new FullScreenExitButton(this);
132 disconnect_action->setEnabled(false);
133 toolbar->setEnabled(false);
136 vnc_view = new VncView(this, url, RemoteView::Quality(quality));
137 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
138 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
139 scroll_area->setWidget(vnc_view);
141 key_menu = new KeyMenu(this);
145 void MainWindow::grabZoomKeys(bool grab)
148 unsigned long val = (grab)?1:0;
149 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
151 qWarning("Couldn't get zoom key atom");
154 XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
155 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
159 void MainWindow::closeEvent(QCloseEvent*) {
163 settings.setValue("show_toolbar", show_toolbar->isChecked());
164 settings.setValue("zoomlevel", zoom_slider->value());
169 disconnectFromHost();
172 void MainWindow::about() {
173 QMessageBox::about(this, tr("About Presence VNC"),
174 tr("<center><h1>Presence VNC 0.6</h1>\
175 <p>A touchscreen friendly VNC client</p>\
176 <p><a href=\"https://garage.maemo.org/projects/presencevnc/\">https://garage.maemo.org/projects/presencevnc</a></p></center>\
177 <small><p>©2010 Christian Pulvermacher <pulvermacher@gmx.de><br />\
178 Based on KRDC, © 2007-2008 Urs Wolfer<br />\
179 and LibVNCServer, © 2001-2003 Johannes E. Schindelin</p>\
180 <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>"));
183 void MainWindow::showConnectDialog()
185 ConnectDialog *connect_dialog = new ConnectDialog(this);
186 connect(connect_dialog, SIGNAL(connectToHost(QString, int)),
187 this, SLOT(connectToHost(QString, int)));
188 connect_dialog->exec();
191 void MainWindow::connectToHost(QString url, int quality)
193 disconnectFromHost();
195 vnc_view = new VncView(this, url, RemoteView::Quality(quality));
197 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
198 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
199 scroll_area->setWidget(vnc_view);
201 disconnect_action->setEnabled(true);
202 toolbar->setEnabled(true);
206 key_menu = new KeyMenu(this);
209 void MainWindow::disconnectFromHost()
214 scroll_area->setWidget(0);
218 disconnect_action->setEnabled(false);
219 toolbar->setEnabled(false);
222 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
224 static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
227 case RemoteView::Connecting:
229 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
232 case RemoteView::Connected:
234 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
236 vnc_view->setZoomLevel(zoom_slider->value());
237 vnc_view->forceFullRepaint();
239 case RemoteView::Disconnecting:
240 if(old_status != RemoteView::Disconnected) { //Disconnecting also occurs while connecting, so check last state
242 QMaemo5InformationBox::information(this, tr("Connection lost"));
246 scroll_area->setWidget(0);
248 disconnect_action->setEnabled(false);
249 toolbar->setEnabled(false);
251 //exit fullscreen mode
252 if(windowState() & Qt::WindowFullScreen)
253 setWindowState(windowState() ^ Qt::WindowFullScreen);
256 case RemoteView::Disconnected:
258 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
260 if(old_status == RemoteView::Disconnecting) {
261 scroll_area->setWidget(0); //remove widget
264 default: //avoid compiler warnings
271 //resizes the widget to use available screen space
272 //necessary when rotating, showing fullscreen, etc.
273 void MainWindow::forceResize()
276 vnc_view->resize(scroll_area->size());
280 void MainWindow::forceResizeDelayed()
282 QTimer::singleShot(500, this, SLOT(forceResize()));
285 void MainWindow::toggleFullscreen()
287 bool in_fullscreen = windowState() & Qt::WindowFullScreen;
289 //hide menu/toolbar in fullscreen (new state is !in_fullscreen)
290 toolbar->setVisible(show_toolbar->isChecked() and in_fullscreen);
293 //menu bar is invisible by default on maemo
294 menuBar()->setVisible(in_fullscreen);
297 setWindowState(windowState() ^ Qt::WindowFullScreen);
298 forceResizeDelayed();
301 void MainWindow::showKeyMenu()
304 vnc_view->sendKeySequence(key_menu->getKeySequence());
307 void MainWindow::showPreferences()
309 Preferences *p = new Preferences(this);
316 void MainWindow::reloadSettings()
320 int rotation = settings.value("screen_rotation", 0).toInt();
321 setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
322 setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
323 setAttribute(Qt::WA_Maemo5PortraitOrientation, rotation == 2);
327 vnc_view->reloadSettings();
330 void MainWindow::resizeEvent(QResizeEvent *event)
332 QMainWindow::resizeEvent(event);
336 vnc_view->setZoomLevel(zoom_slider->value());
339 //hide zoom slider in portrait mode
340 zoom_slider->setVisible(height() < width());
344 void MainWindow::showInputPanel()
347 //TODO: when hardware keyboard is open, this will only cause the IM to mess up 'real' key events
348 vnc_view->setAttribute(Qt::WA_InputMethodEnabled, true);
349 vnc_view->setInputMethodHints(Qt::ImhNoAutoUppercase); //without this, IM starts with caps lock
351 QEvent event(QEvent::RequestSoftwareInputPanel);
352 QApplication::sendEvent(vnc_view, &event);
356 void MainWindow::setZoomLevel(int level)
359 vnc_view->setZoomLevel(level);