set button text
[presencevnc] / src / mainwindow.cpp
1 /*
2     Presence VNC
3     Copyright (C) 2010 Christian Pulvermacher
4
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.
9
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.
14
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.
18 */
19 #include "connectdialog.h"
20 #include "fullscreenexitbutton.h"
21 #include "keymenu.h"
22 #include "mainwindow.h"
23 #include "preferences.h"
24 #include "scrollarea.h"
25 #include "vncview.h"
26
27 #ifdef Q_WS_MAEMO_5
28 #include <QtMaemo5>
29 #include <QX11Info>
30
31 #include <X11/Xlib.h>
32 #include <X11/Xatom.h>
33 #endif
34
35
36 MainWindow::MainWindow(QString url, int quality):
37         QMainWindow(0),
38         vnc_view(0),
39         scroll_area(new ScrollArea(0)),
40         key_menu(new KeyMenu(this))
41 {
42         setWindowTitle("Presence VNC");
43         setContextMenuPolicy(Qt::NoContextMenu);
44 #ifdef Q_WS_MAEMO_5
45         setAttribute(Qt::WA_Maemo5StackedWindow);
46 #endif
47
48         migrateConfiguration();
49
50         //set up toolbar
51         toolbar = new QToolBar(0);
52         toolbar->addAction(QChar(0x2026), this, SLOT(showKeyMenu())); //"..." button
53         toolbar->addAction(tr("Tab"), this, SLOT(sendTab()));
54         toolbar->addAction(tr("Esc"), this, SLOT(sendEsc()));
55         toolbar->addAction(tr("PgUp"), this, SLOT(sendPgUp()));
56         toolbar->addAction(tr("PgDn"), this, SLOT(sendPgDn()));
57 #ifdef Q_WS_MAEMO_5
58         toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/chat_enter.png"), "", this, SLOT(sendReturn()));
59         toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/control_keyboard.png"), "", this, SLOT(showInputPanel()));
60 #endif
61
62         QSettings settings;
63         zoom_slider = new QSlider(Qt::Horizontal, 0);
64         zoom_slider->setRange(0, 100);
65         connect(zoom_slider, SIGNAL(valueChanged(int)),
66                 this, SLOT(setZoomLevel(int)));
67         connect(zoom_slider, SIGNAL(sliderReleased()),
68                 this, SLOT(zoomSliderReleased()));
69         zoom_slider->setValue(settings.value("zoomlevel", 95).toInt());
70         toolbar->addWidget(zoom_slider);
71
72         toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_fullsize.png"), "", this, SLOT(toggleFullscreen()));
73         addToolBar(toolbar);
74         toolbar->setVisible(settings.value("show_toolbar", true).toBool());
75         toolbar->setEnabled(false);
76
77         //set up menu
78         QAction *connect_action = new QAction(tr("Connect"), this);
79         disconnect_action = new QAction(tr("Disconnect"), this);
80         show_toolbar = new QAction(tr("Show Toolbar"), this);
81         show_toolbar->setCheckable(true);
82         show_toolbar->setChecked(settings.value("show_toolbar", true).toBool());
83         QAction *pref_action = new QAction(tr("Preferences"), this);
84         QAction *about_action = new QAction(tr("About"), this);
85
86 #ifdef Q_WS_MAEMO_5
87         menuBar()->addAction(connect_action);
88         menuBar()->addAction(disconnect_action);
89         menuBar()->addAction(show_toolbar);
90         menuBar()->addAction(pref_action);
91         menuBar()->addAction(about_action);
92 #else
93         QMenu* session_menu = menuBar()->addMenu(tr("&Session"));
94         session_menu->addAction(connect_action);
95         session_menu->addAction(disconnect_action);
96         session_menu->addSeparator();
97         session_menu->addAction(pref_action);
98         session_menu->addSeparator();
99         session_menu->addAction(tr("&Quit"), this, SLOT(close()));
100
101         QMenu* view_menu = menuBar()->addMenu(tr("&View"));
102         view_menu->addAction(show_toolbar);
103
104         QMenu* help_menu = menuBar()->addMenu(tr("&Help"));
105         help_menu->addAction(about_action);
106 #endif
107
108         connect(about_action, SIGNAL(triggered()),
109                 this, SLOT(about()));
110         connect(pref_action, SIGNAL(triggered()),
111                 this, SLOT(showPreferences()));
112         connect(connect_action, SIGNAL(triggered()),
113                 this, SLOT(showConnectDialog()));
114         connect(disconnect_action, SIGNAL(triggered()),
115                 this, SLOT(disconnectFromHost()));
116         connect(show_toolbar, SIGNAL(toggled(bool)),
117                 toolbar, SLOT(setVisible(bool)));
118         connect(show_toolbar, SIGNAL(toggled(bool)),
119                 this, SLOT(updateScreenSpaceDelayed()));
120
121         setCentralWidget(scroll_area);
122
123         FullScreenExitButton* fullscreen_exit_button = new FullScreenExitButton(this);
124         connect(fullscreen_exit_button, SIGNAL(clicked()),
125                 this, SLOT(toggleFullscreen()));
126
127         grabZoomKeys(true);
128         reloadSettings();
129
130         if(url.isNull()) {
131                 disconnect_action->setEnabled(false);
132                 showConnectDialog();
133         } else {
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);
138                 vnc_view->start();
139         }
140 }
141
142 void MainWindow::grabZoomKeys(bool grab)
143 {
144 #ifdef Q_WS_MAEMO_5
145         unsigned long val = (grab)?1:0;
146         Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
147         if(!atom) {
148                 qWarning("Couldn't get zoom key atom");
149                 return;
150         }
151         XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
152                 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
153 #endif
154 }
155
156 void MainWindow::closeEvent(QCloseEvent*) {
157         grabZoomKeys(false);
158
159         QSettings settings;
160         settings.setValue("show_toolbar", show_toolbar->isChecked());
161         settings.setValue("zoomlevel", zoom_slider->value());
162         settings.sync();
163
164         hide();
165
166         disconnectFromHost();
167 }
168
169 void MainWindow::about() {
170         QMessageBox::about(this, tr("About Presence VNC"),
171                 tr("<center><h1>Presence VNC 0.7</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>&copy;2010 Christian Pulvermacher &lt;pulvermacher@gmx.de&gt;<br />\
175 Based on KRDC, &copy; 2007-2008 Urs Wolfer<br />\
176 and LibVNCServer, &copy; 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>"));
178 }
179
180 void MainWindow::showConnectDialog()
181 {
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();
186 }
187
188 void MainWindow::connectToHost(QString url, int quality, int listen_port)
189 {
190         disconnectFromHost();
191
192         vnc_view = new VncView(this, url, RemoteView::Quality(quality), listen_port);
193
194         connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
195                 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
196         scroll_area->setWidget(vnc_view);
197         vnc_view->start();
198
199         disconnect_action->setEnabled(true);
200
201         //reset key menu
202         delete key_menu;
203         key_menu = new KeyMenu(this);
204 }
205
206 void MainWindow::disconnectFromHost()
207 {
208         if(!vnc_view)
209                 return;
210
211         disconnect_action->setEnabled(false);
212         toolbar->setEnabled(false);
213         scroll_area->setWidget(0);
214
215         delete vnc_view;
216         vnc_view = 0;
217 }
218
219 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
220 {
221         static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
222
223         switch(status) {
224         case RemoteView::Connecting:
225 #ifdef Q_WS_MAEMO_5
226                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
227 #endif
228                 break;
229         case RemoteView::Connected:
230 #ifdef Q_WS_MAEMO_5
231                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
232 #endif
233                 toolbar->setEnabled(true);
234
235                 vnc_view->setZoomLevel(zoom_slider->value());
236                 vnc_view->useFastTransformations(false);
237                 vnc_view->repaint();
238                 break;
239         case RemoteView::Disconnecting:
240                 if(old_status == RemoteView::Disconnected) //Disconnecting also occurs while connecting, so check last state
241                         break;
242
243                 if(disconnect_action->isEnabled()) //don't show when manually disconnecting
244                         scroll_area->showMessage(tr("Connection lost"));
245                 
246                 //clean up
247                 scroll_area->setWidget(0);
248                 vnc_view = 0;
249                 disconnect_action->setEnabled(false);
250                 toolbar->setEnabled(false);
251
252                 //exit fullscreen mode
253                 if(windowState() & Qt::WindowFullScreen)
254                         setWindowState(windowState() ^ Qt::WindowFullScreen);
255                 break;
256         case RemoteView::Disconnected:
257 #ifdef Q_WS_MAEMO_5
258                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
259 #endif
260                 if(old_status == RemoteView::Disconnecting) {
261                         scroll_area->setWidget(0); //remove widget
262                 }
263                 break;
264         default: //avoid compiler warnings
265                 break;
266         }
267
268         old_status = status;
269 }
270
271 //updates available screen space for current zoom level
272 //necessary when rotating, showing fullscreen, etc.
273 void MainWindow::updateScreenSpace()
274 {
275         if(vnc_view) {
276                 vnc_view->setZoomLevel();
277         }
278
279
280 void MainWindow::updateScreenSpaceDelayed()
281 {
282         QTimer::singleShot(500, this, SLOT(updateScreenSpace()));
283 }
284
285 void MainWindow::toggleFullscreen()
286 {
287         bool in_fullscreen = windowState() & Qt::WindowFullScreen;
288
289         //hide menu/toolbar in fullscreen (new state is !in_fullscreen)
290         toolbar->setVisible(show_toolbar->isChecked() and in_fullscreen);
291
292 #ifndef Q_WS_MAEMO_5
293         //menu bar is invisible by default on maemo
294         menuBar()->setVisible(in_fullscreen);
295 #endif
296
297         setWindowState(windowState() ^ Qt::WindowFullScreen); 
298         updateScreenSpaceDelayed();
299 }
300
301 void MainWindow::showKeyMenu()
302 {
303         key_menu->exec();
304         vnc_view->sendKeySequence(key_menu->getKeySequence());
305 }
306
307 void MainWindow::showPreferences()
308 {
309         Preferences *p = new Preferences(this);
310         p->exec();
311         delete p;
312
313         reloadSettings();
314 }
315
316 void MainWindow::reloadSettings()
317 {
318         QSettings settings;
319         zoom_to_cursor = settings.value("zoom_to_cursor", true).toBool();
320         
321 #ifdef Q_WS_MAEMO_5
322         int rotation = settings.value("screen_rotation", 0).toInt();
323         setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
324         setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
325         setAttribute(Qt::WA_Maemo5PortraitOrientation, rotation == 2);
326 #endif
327
328         if(vnc_view)
329                 vnc_view->reloadSettings();
330 }
331
332 void MainWindow::resizeEvent(QResizeEvent *event)
333 {
334         QMainWindow::resizeEvent(event);
335
336         updateScreenSpace();
337         if(vnc_view)
338                 vnc_view->setZoomLevel(zoom_slider->value());
339         
340 #ifdef Q_WS_MAEMO_5
341         //hide zoom slider in portrait mode
342         zoom_slider->setVisible(height() < width());
343 #endif
344 }
345
346 void MainWindow::showInputPanel()
347 {
348 #ifdef Q_WS_MAEMO_5
349         //TODO: when hardware keyboard is open, this will only cause the IM to mess up 'real' key events
350         vnc_view->setAttribute(Qt::WA_InputMethodEnabled, true);
351         vnc_view->setInputMethodHints(Qt::ImhNoAutoUppercase); //without this, IM starts with caps lock
352
353         QEvent event(QEvent::RequestSoftwareInputPanel);
354         QApplication::sendEvent(vnc_view, &event);
355 #endif
356 }
357
358 void MainWindow::setZoomLevel(int level)
359 {
360         if(!vnc_view)
361                 return;
362         
363         const qreal old_factor = vnc_view->zoomFactor();
364         QPoint center = vnc_view->visibleRegion().boundingRect().center();
365
366         vnc_view->setZoomLevel(level);
367
368         const qreal new_factor = vnc_view->zoomFactor();
369
370         //scroll to center, if zoom level actually changed
371         if(old_factor != new_factor) {
372                 if(zoom_to_cursor)
373                         center = new_factor * vnc_view->cursorPosition();
374                 else //zoom to center of visible region
375                         center = center * (double(new_factor)/old_factor);
376
377                 scroll_area->ensureVisible(center.x(), center.y(),
378                         vnc_view->visibleRegion().boundingRect().width()/2,
379                         vnc_view->visibleRegion().boundingRect().height()/2);
380
381                 vnc_view->useFastTransformations(zoom_slider->isSliderDown());
382                 vnc_view->update();
383
384                 scroll_area->showMessage(tr("Zoom: %1\%").arg(qRound(100*new_factor)));
385         }
386 }
387
388 void MainWindow::zoomSliderReleased()
389 {
390         static QTime time;
391         if(!time.isNull() and time.elapsed() < 500) //double clicked
392                 zoom_slider->setValue(95); //100%
393         
394         time.restart();
395
396         //stopped zooming, reenable high quality
397         vnc_view->useFastTransformations(false);
398 }