0.2: add libjpeg dependency
[presencevnc] / src / mainwindow.cpp
1 #include "mainwindow.h"
2 #include "preferences.h"
3 #include "vncview.h"
4
5 #include <QtMaemo5>
6 #include <QX11Info>
7
8 #include <X11/Xlib.h>
9 #include <X11/Xatom.h>
10
11 #include <iostream>
12
13 MainWindow::MainWindow(QString url, int quality):
14         QMainWindow(0),
15         vnc_view(0),
16         scroll_area(new QScrollArea(0))
17 {
18         setWindowTitle("Presence VNC");
19 //      swipe_start = QPoint(0,0);
20         setAttribute(Qt::WA_Maemo5StackedWindow);
21         QSettings settings;
22
23         //set up toolbar
24         toolbar = new QToolBar(0);
25         toolbar->setAttribute(Qt::WA_InputMethodEnabled, true);
26         toolbar->addAction("Mod", this, SLOT(showModifierMenu()));
27         toolbar->addAction("Tab", this, SLOT(sendTab()));
28         toolbar->addAction("Esc", this, SLOT(sendEsc()));
29         toolbar->addAction("PgUp", this, SLOT(sendPgUp()));
30         toolbar->addAction("PgDn", this, SLOT(sendPgDn()));
31 //      toolbar->addAction("IM", toolbar, SLOT(setFocus())); //doesn't work
32         toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_fullsize.png"), "", this, SLOT(toggleFullscreen()));
33         addToolBar(toolbar);
34         toolbar->setVisible(settings.value("show_toolbar", true).toBool());
35
36         //set up menu
37         QMenuBar *menu = new QMenuBar(this);
38         QAction *connect_action = new QAction("Connect", this);
39         disconnect_action = new QAction("Disconnect", this);
40 //      menu->addAction(connect_action);
41 //      menu->addAction(disconnect_action);
42         scaling = new QAction("Fit to Screen", this);
43         scaling->setCheckable(true);
44         scaling->setChecked(settings.value("rescale", true).toBool());
45         menu->addAction(scaling);
46         QAction *show_toolbar = new QAction("Show Toolbar", this);
47         show_toolbar->setCheckable(true);
48         show_toolbar->setChecked(settings.value("show_toolbar", true).toBool());
49         menu->addAction(show_toolbar);
50         QAction *pref_action = new QAction("Preferences", this);
51         menu->addAction(pref_action);
52         QAction *about_action = new QAction("About", this);
53         menu->addAction(about_action);
54
55         //menu->setAttribute(Qt::WA_Maemo5StackedWindow);
56         //menu->hide();
57
58         connect(about_action, SIGNAL(triggered()),
59                 this, SLOT(about()));
60         connect(pref_action, SIGNAL(triggered()),
61                 this, SLOT(showPreferences()));
62         connect(connect_action, SIGNAL(triggered()),
63                 this, SLOT(connectDialog()));
64         connect(disconnect_action, SIGNAL(triggered()),
65                 this, SLOT(disconnectFromHost()));
66         connect(show_toolbar, SIGNAL(toggled(bool)),
67                 toolbar, SLOT(setVisible(bool)));
68         connect(show_toolbar, SIGNAL(toggled(bool)),
69                 this, SLOT(forceResizeDelayed()));
70
71         setCentralWidget(scroll_area);
72
73         grabZoomKeys(true);
74         reloadSettings();
75
76         connect(QApplication::desktop(), SIGNAL(resized(int)),
77                 this, SLOT(forceResize()));
78
79         if(url.isNull()) {
80                 disconnect_action->setEnabled(false);
81                 toolbar->setEnabled(false);
82                 connectDialog();
83         } else {
84                 vnc_view = new VncView(this, url, RemoteView::Quality(quality));
85                 connect(scaling, SIGNAL(toggled(bool)),
86                         vnc_view, SLOT(enableScaling(bool)));
87                 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
88                         this, SLOT(statusChanged(RemoteView::RemoteStatus)));
89                 scroll_area->setWidget(vnc_view);
90                 vnc_view->start();
91                 vnc_view->enableScaling(scaling->isChecked());
92         }
93
94         if(!vnc_view) //not connected
95                 QTimer::singleShot(100, this, SLOT(close()));
96 }
97
98 void MainWindow::grabZoomKeys(bool grab)
99 {
100         unsigned long val = (grab)?1:0;
101         Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
102         if(!atom) {
103                 qWarning("Couldn't get zoom key atom");
104                 return;
105         }
106         XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
107                 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
108 }
109
110 void MainWindow::closeEvent(QCloseEvent*) {
111         grabZoomKeys(false);
112
113         QSettings settings;
114         settings.setValue("show_toolbar", toolbar->isVisible());
115         settings.setValue("rescale", scaling->isChecked());
116         settings.sync();
117
118         hide();
119
120         disconnectFromHost();
121 }
122
123 void MainWindow::about() {
124         QMessageBox::about(this, tr("About Presence VNC"),
125                 tr("<center><h1>Presence VNC 0.2</h1>\
126 A touchscreen friendly VNC client\
127 <small><p>&copy;2010 Christian Pulvermacher &lt;pulvermacher@gmx.de&gt</p>\
128 <p>Based on KRDC, &copy; 2007-2008 Urs Wolfer</small></center>\
129 <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>"));
130 }
131
132 /* swipe not used, and doesn't work without scaling anyway :/
133 virtual bool event(QEvent *event) {
134         if(event->type() == QEvent::MouseMove) {
135                 QMouseEvent *ev = dynamic_cast<QMouseEvent* >(event);
136                 if(!swipe_start.isNull()) {
137                         QPoint diff = swipe_start - ev->pos();
138                         const int swipe_dist = 60;
139                         if(diff.x() > swipe_dist and diff.y() < swipe_dist and diff.y() > -swipe_dist) { //
140                                 menu->show();
141                                 swipe_start = QPoint(0,0);
142                         }
143                 } else if((width() - ev->x()) < 10) {
144                         swipe_start = ev->pos();
145                 }
146                 std::cout << "mousex: " << width() - ev->x() << "\n";
147                 //TODO: make scrolling over border result in wheel events? i get weird (out of range) mouse events when that happens
148                 return true;
149         } else if(event->type() == QEvent::MouseButtonRelease) {
150                 swipe_start = QPoint(0,0);
151                 return true;
152         } else {
153 //                      std::cout << "event " << event->type() << "\n";
154                 return QScrollArea::event(event);
155         }
156 }
157 */
158
159 void MainWindow::connectDialog()
160 {
161         QSettings settings;
162         QString url = QInputDialog::getText(this, "Connect to Host", "VNC Server:", QLineEdit::Normal, settings.value("last_hostname", "").toString());
163         if(url.isEmpty()) { //dialog dismissed or nothing entered
164                 return;
165         }
166         settings.setValue("last_hostname", url);
167         url = "vnc://" + url;
168
169         disconnectFromHost();
170
171         vnc_view = new VncView(this, url, RemoteView::Quality(2)); //TODO: get quality in dialog
172         scroll_area->setWidget(vnc_view);
173
174         connect(scaling, SIGNAL(toggled(bool)),
175                 vnc_view, SLOT(enableScaling(bool)));
176         connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
177                 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
178         vnc_view->start();
179         vnc_view->enableScaling(scaling->isChecked());
180         disconnect_action->setEnabled(true);
181         toolbar->setEnabled(true);
182 }
183
184 void MainWindow::disconnectFromHost()
185 {
186         if(!vnc_view)
187                 return;
188
189 //TODO: crashes when deleting vnc_view - no idea why
190         //vnc_view->startQuitting();
191         //scroll_area->setWidget(0);
192
193 //      vnc_view->disconnect(); //remove all connections
194         //delete vnc_view;
195         //vnc_view = 0;
196         disconnect_action->setEnabled(false);
197         toolbar->setEnabled(false);
198 }
199
200 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
201 {
202         static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
203
204         switch(status) {
205         case RemoteView::Connecting:
206                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
207                 break;
208         case RemoteView::Connected:
209                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
210                 break;
211         case RemoteView::Disconnecting:
212                 if(old_status != RemoteView::Disconnected) { //Disconnecting also occurs while connecting, so check last state
213                         QMaemo5InformationBox::information(this, "Connection lost");
214                         close();
215                 }
216                 break;
217         case RemoteView::Disconnected:
218                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
219                 if(old_status == RemoteView::Disconnecting) {
220                         scroll_area->setWidget(0); //remove widget
221                 }
222                 break;
223         }
224
225         old_status = status;
226 }
227
228 //when rescaling is enabled, this resizes the widget to use available screen space
229 //necessary when rotating, showing fullscreen, etc.
230 void MainWindow::forceResize()
231 {
232         if(vnc_view and scaling->isChecked()) {
233                 vnc_view->resize(scroll_area->size());
234         }
235
236
237 void MainWindow::forceResizeDelayed()
238 {
239         QTimer::singleShot(500, this, SLOT(forceResize()));
240 }
241
242 void MainWindow::toggleFullscreen()
243 {
244         setWindowState(windowState() ^ Qt::WindowFullScreen); 
245         forceResizeDelayed();
246 }
247
248 void MainWindow::showModifierMenu()
249 {
250         static QMenu *mod_menu = new QMenu(tr("Modifiers"), this);
251         static QAction *win = mod_menu->addAction(tr("Win"));
252         static QAction *alt = mod_menu->addAction(tr("Alt"));
253         win->setCheckable(true);
254         alt->setCheckable(true);
255
256         //show menu at top-left corner of toolbar
257         QAction *chosen = mod_menu->exec(toolbar->mapToGlobal(QPoint(0,0)));
258         if(!chosen) {
259                 return;
260         } else if(chosen == alt) {
261                 vnc_view->sendKey(Qt::Key_Alt);
262         } else if(chosen == win) {
263                 vnc_view->sendKey(Qt::Key_Meta);
264         } else {
265                 std::cout << "unhandled action?\n";
266         }
267 }
268
269 void MainWindow::showPreferences()
270 {
271         Preferences *p = new Preferences(this);
272         p->exec();
273         delete p;
274
275         reloadSettings();
276 }
277
278 void MainWindow::reloadSettings()
279 {
280         QSettings settings;
281         int rotation = settings.value("screen_rotation", 0).toInt();
282         setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
283         setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
284         setAttribute(Qt::WA_Maemo5PortraitOrientation, rotation == 2);
285
286         if(vnc_view)
287                 vnc_view->reloadSettings();
288 }