fix refresh issues
[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 "vncview.h"
25
26 #ifdef Q_WS_MAEMO_5
27 #include <QtMaemo5>
28 #include <QX11Info>
29
30 #include <X11/Xlib.h>
31 #include <X11/Xatom.h>
32 #endif
33
34
35 MainWindow::MainWindow(QString url, int quality):
36         QMainWindow(0),
37         vnc_view(0),
38         scroll_area(new ScrollArea(0)),
39         key_menu(new KeyMenu(this))
40 {
41         setWindowTitle("Presence VNC");
42 #ifdef Q_WS_MAEMO_5
43         setAttribute(Qt::WA_Maemo5StackedWindow);
44 #endif
45
46         migrateConfiguration();
47
48         //set up toolbar
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()));
55 #ifdef Q_WS_MAEMO_5
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()));
58 #endif
59
60         /*
61         //move remaining buttons to the right
62         QWidget *spacer = new QWidget();
63         spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
64         toolbar->addWidget(spacer);
65         */
66
67         QSettings settings;
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         connect(zoom_slider, SIGNAL(sliderReleased()),
73                 this, SLOT(forceRepaint()));
74         zoom_slider->setValue(settings.value("zoomlevel", 95).toInt());
75         toolbar->addWidget(zoom_slider);
76
77         toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_fullsize.png"), "", this, SLOT(toggleFullscreen()));
78         addToolBar(toolbar);
79         toolbar->setVisible(settings.value("show_toolbar", true).toBool());
80
81         //set up menu
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);
89
90 #ifdef Q_WS_MAEMO_5
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);
96 #else
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()));
104
105         QMenu* view_menu = menuBar()->addMenu(tr("&View"));
106         view_menu->addAction(show_toolbar);
107
108         QMenu* help_menu = menuBar()->addMenu(tr("&Help"));
109         help_menu->addAction(about_action);
110 #endif
111
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()));
124
125         setCentralWidget(scroll_area);
126         new FullScreenExitButton(this);
127
128         grabZoomKeys(true);
129         reloadSettings();
130
131         if(url.isNull()) {
132                 disconnect_action->setEnabled(false);
133                 toolbar->setEnabled(false);
134                 showConnectDialog();
135         } else {
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);
140                 vnc_view->start();
141         }
142 }
143
144 void MainWindow::grabZoomKeys(bool grab)
145 {
146 #ifdef Q_WS_MAEMO_5
147         unsigned long val = (grab)?1:0;
148         Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
149         if(!atom) {
150                 qWarning("Couldn't get zoom key atom");
151                 return;
152         }
153         XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
154                 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
155 #endif
156 }
157
158 void MainWindow::closeEvent(QCloseEvent*) {
159         grabZoomKeys(false);
160
161         QSettings settings;
162         settings.setValue("show_toolbar", show_toolbar->isChecked());
163         settings.setValue("zoomlevel", zoom_slider->value());
164         settings.sync();
165
166         hide();
167
168         disconnectFromHost();
169 }
170
171 void MainWindow::about() {
172         QMessageBox::about(this, tr("About Presence VNC"),
173                 tr("<center><h1>Presence VNC 0.6</h1>\
174 <p>A touchscreen friendly VNC client</p>\
175 <p><a href=\"https://garage.maemo.org/projects/presencevnc/\">https://garage.maemo.org/projects/presencevnc</a></p></center>\
176 <small><p>&copy;2010 Christian Pulvermacher &lt;pulvermacher@gmx.de&gt;<br />\
177 Based on KRDC, &copy; 2007-2008 Urs Wolfer<br />\
178 and LibVNCServer, &copy; 2001-2003 Johannes E. Schindelin</p>\
179 <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 }
181
182 void MainWindow::showConnectDialog()
183 {
184         ConnectDialog *connect_dialog = new ConnectDialog(this);
185         connect(connect_dialog, SIGNAL(connectToHost(QString, int, int)),
186                 this, SLOT(connectToHost(QString, int, int)));
187         connect_dialog->exec();
188 }
189
190 void MainWindow::connectToHost(QString url, int quality, int listen_port)
191 {
192         disconnectFromHost();
193
194         vnc_view = new VncView(this, url, RemoteView::Quality(quality), listen_port);
195
196         connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
197                 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
198         scroll_area->setWidget(vnc_view);
199         vnc_view->start();
200
201         disconnect_action->setEnabled(true);
202         toolbar->setEnabled(true);
203
204         //reset key menu
205         delete key_menu;
206         key_menu = new KeyMenu(this);
207 }
208
209 void MainWindow::disconnectFromHost()
210 {
211         if(!vnc_view)
212                 return;
213
214         scroll_area->setWidget(0);
215
216         delete vnc_view;
217         vnc_view = 0;
218         disconnect_action->setEnabled(false);
219         toolbar->setEnabled(false);
220 }
221
222 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
223 {
224         static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
225
226         switch(status) {
227         case RemoteView::Connecting:
228 #ifdef Q_WS_MAEMO_5
229                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
230 #endif
231                 break;
232         case RemoteView::Connected:
233 #ifdef Q_WS_MAEMO_5
234                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
235 #endif
236                 vnc_view->setZoomLevel(zoom_slider->value());
237                 vnc_view->forceFullRepaint();
238                 break;
239         case RemoteView::Disconnecting:
240                 if(old_status != RemoteView::Disconnected) { //Disconnecting also occurs while connecting, so check last state
241 #ifdef Q_WS_MAEMO_5
242                         QMaemo5InformationBox::information(this, tr("Connection lost"));
243 #endif
244                         
245                         //clean up
246                         scroll_area->setWidget(0);
247                         vnc_view = 0;
248                         disconnect_action->setEnabled(false);
249                         toolbar->setEnabled(false);
250
251                         //exit fullscreen mode
252                         if(windowState() & Qt::WindowFullScreen)
253                                 setWindowState(windowState() ^ Qt::WindowFullScreen);
254                 }
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 void MainWindow::forceRepaint()
272 {
273         if(vnc_view)
274                 vnc_view->forceFullRepaint();
275 }
276
277 //updates available screen space for current zoom level
278 //necessary when rotating, showing fullscreen, etc.
279 void MainWindow::forceResize()
280 {
281         if(vnc_view) {
282                 vnc_view->resize(scroll_area->size());
283         }
284
285
286 void MainWindow::forceResizeDelayed()
287 {
288         QTimer::singleShot(500, this, SLOT(forceResize()));
289 }
290
291 void MainWindow::toggleFullscreen()
292 {
293         bool in_fullscreen = windowState() & Qt::WindowFullScreen;
294
295         //hide menu/toolbar in fullscreen (new state is !in_fullscreen)
296         toolbar->setVisible(show_toolbar->isChecked() and in_fullscreen);
297
298 #ifndef Q_WS_MAEMO_5
299         //menu bar is invisible by default on maemo
300         menuBar()->setVisible(in_fullscreen);
301 #endif
302
303         setWindowState(windowState() ^ Qt::WindowFullScreen); 
304         forceResizeDelayed();
305 }
306
307 void MainWindow::showKeyMenu()
308 {
309         key_menu->exec();
310         vnc_view->sendKeySequence(key_menu->getKeySequence());
311 }
312
313 void MainWindow::showPreferences()
314 {
315         Preferences *p = new Preferences(this);
316         p->exec();
317         delete p;
318
319         reloadSettings();
320 }
321
322 void MainWindow::reloadSettings()
323 {
324         QSettings settings;
325 #ifdef Q_WS_MAEMO_5
326         int rotation = settings.value("screen_rotation", 0).toInt();
327         setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
328         setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
329         setAttribute(Qt::WA_Maemo5PortraitOrientation, rotation == 2);
330 #endif
331
332         if(vnc_view)
333                 vnc_view->reloadSettings();
334 }
335
336 void MainWindow::resizeEvent(QResizeEvent *event)
337 {
338         QMainWindow::resizeEvent(event);
339
340         forceResize();
341         if(vnc_view)
342                 vnc_view->setZoomLevel(zoom_slider->value());
343         
344 #ifdef Q_WS_MAEMO_5
345         //hide zoom slider in portrait mode
346         zoom_slider->setVisible(height() < width());
347 #endif
348 }
349
350 void MainWindow::showInputPanel()
351 {
352 #ifdef Q_WS_MAEMO_5
353         //TODO: when hardware keyboard is open, this will only cause the IM to mess up 'real' key events
354         vnc_view->setAttribute(Qt::WA_InputMethodEnabled, true);
355         vnc_view->setInputMethodHints(Qt::ImhNoAutoUppercase); //without this, IM starts with caps lock
356
357         QEvent event(QEvent::RequestSoftwareInputPanel);
358         QApplication::sendEvent(vnc_view, &event);
359 #endif
360 }
361
362 void MainWindow::setZoomLevel(int level)
363 {
364         if(!vnc_view)
365                 return;
366         
367         int old_width = vnc_view->width();
368         QPoint center = vnc_view->visibleRegion().boundingRect().center();
369
370         vnc_view->setZoomLevel(level);
371
372         int new_width = vnc_view->width();
373
374         //scroll to center, if zoom level actually changed
375         if(new_width != old_width) {
376                 center = center * (double(new_width)/old_width);
377                 scroll_area->ensureVisible(center.x(), center.y(),
378                         scroll_area->width()/2, scroll_area->height()/2);
379                 vnc_view->update();
380         }
381 }