add Tab, PgUp/Dn, Fullscreen buttons
[presencevnc] / src / mainwindow.cpp
1 #include "mainwindow.h"
2 #include "vncview.h"
3
4 #include <QtMaemo5>
5 #include <QX11Info>
6
7 #include <X11/Xlib.h>
8 #include <X11/Xatom.h>
9
10 #include <iostream>
11
12 MainWindow::MainWindow(QString url, int quality):
13         QMainWindow(0),
14         vnc_view(0),
15         scroll_area(new QScrollArea(0))
16 {
17         setWindowTitle("Presence VNC");
18         swipe_start = QPoint(0,0);
19         setAttribute(Qt::WA_Maemo5StackedWindow);
20
21         //set up toolbar
22         toolbar = new QToolBar(0);
23         toolbar->addAction("Mod"); //TODO
24         toolbar->addAction("Tab", this, SLOT(sendTab()));
25         toolbar->addAction("Esc", this, SLOT(sendEsc()));
26         toolbar->addAction("PgUp", this, SLOT(sendPgUp()));
27         toolbar->addAction("PgDn", this, SLOT(sendPgDn()));
28         toolbar->addAction("Fullscreen", this, SLOT(toggleFullscreen()));
29         addToolBar(toolbar);
30
31         //set up menu
32         QMenuBar *menu = new QMenuBar(this);
33         QAction *connect_action = new QAction("Connect", this);
34         disconnect_action = new QAction("Disconnect", this);
35         menu->addAction(connect_action);
36         menu->addAction(disconnect_action);
37         scaling = new QAction("Rescale Remote Screen", this);
38         scaling->setCheckable(true);
39         scaling->setChecked(true);
40         menu->addAction(scaling);
41         QAction *show_toolbar = new QAction("Show Toolbar", this);
42         show_toolbar->setCheckable(true);
43         show_toolbar->setChecked(true);
44         menu->addAction(show_toolbar);
45         QAction *about_action = new QAction("About", this);
46         menu->addAction(about_action);
47
48         //menu->setAttribute(Qt::WA_Maemo5StackedWindow);
49         //menu->hide();
50
51         connect(about_action, SIGNAL(triggered()),
52                 this, SLOT(about()));
53         connect(connect_action, SIGNAL(triggered()),
54                 this, SLOT(connectDialog()));
55         connect(disconnect_action, SIGNAL(triggered()),
56                 this, SLOT(disconnectFromHost()));
57         connect(show_toolbar, SIGNAL(toggled(bool)),
58                 toolbar, SLOT(setVisible(bool)));
59
60         setCentralWidget(scroll_area);
61
62         grabZoomKeys(true);
63         setAttribute(Qt::WA_Maemo5AutoOrientation, true);
64         //setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
65
66         if(url.isNull()) {
67                 disconnect_action->setEnabled(false);
68                 toolbar->setEnabled(false);
69                 connectDialog();
70         } else {
71                 vnc_view = new VncView(0, url, RemoteView::Quality(quality));
72                 connect(scaling, SIGNAL(toggled(bool)),
73                         vnc_view, SLOT(enableScaling(bool)));
74                 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
75                         this, SLOT(statusChanged(RemoteView::RemoteStatus)));
76                 scroll_area->setWidget(vnc_view);
77                 vnc_view->start();
78         }
79 }
80
81 void MainWindow::grabZoomKeys(bool grab)
82 {
83         unsigned long val = (grab)?1:0;
84         Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
85         if(!atom) {
86                 qWarning("Couldn't get zoom key atom");
87                 return;
88         }
89         XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
90                 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
91 }
92
93 void MainWindow::closeEvent(QCloseEvent*) {
94         hide();
95         grabZoomKeys(false);
96         disconnectFromHost();
97 }
98
99 void MainWindow::about() {
100         QMessageBox::about(this, tr("About Presence VNC"),
101                 tr("<center><h1>Presence VNC 0.1 alpha</h1>\
102 A touch screen friendly VNC client\
103 <small><p>&copy;2010 Christian Pulvermacher &lt;pulvermacher@gmx.de&gt</p>\
104 <p>Based on KRDC, &copy; 2007-2008 Urs Wolfer</small></center>\
105 <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>"));
106 }
107
108 /* swipe not used, and doesn't work without scaling anyway :/
109 virtual bool event(QEvent *event) {
110         if(event->type() == QEvent::MouseMove) {
111                 QMouseEvent *ev = dynamic_cast<QMouseEvent* >(event);
112                 if(!swipe_start.isNull()) {
113                         QPoint diff = swipe_start - ev->pos();
114                         const int swipe_dist = 60;
115                         if(diff.x() > swipe_dist and diff.y() < swipe_dist and diff.y() > -swipe_dist) { //
116                                 menu->show();
117                                 swipe_start = QPoint(0,0);
118                         }
119                 } else if((width() - ev->x()) < 10) {
120                         swipe_start = ev->pos();
121                 }
122                 std::cout << "mousex: " << width() - ev->x() << "\n";
123                 //TODO: make scrolling over border result in wheel events? i get weird (out of range) mouse events when that happens
124                 return true;
125         } else if(event->type() == QEvent::MouseButtonRelease) {
126                 swipe_start = QPoint(0,0);
127                 return true;
128         } else {
129 //                      std::cout << "event " << event->type() << "\n";
130                 return QScrollArea::event(event);
131         }
132 }
133 */
134
135 void MainWindow::connectDialog()
136 {
137         QSettings settings;
138         QString url = QInputDialog::getText(this, "Connect to Host", "VNC Server:", QLineEdit::Normal, settings.value("last_hostname", "").toString());
139         if(url.isEmpty()) { //dialog dismissed or nothing entered
140                 return;
141         }
142         settings.setValue("last_hostname", url);
143         url = "vnc://" + url;
144
145         disconnectFromHost();
146
147         vnc_view = new VncView(0, url, RemoteView::Quality(2)); //TODO: get quality in dialog
148         scroll_area->setWidget(vnc_view);
149
150         connect(scaling, SIGNAL(toggled(bool)),
151                 vnc_view, SLOT(enableScaling(bool)));
152         connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
153                 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
154         vnc_view->start();
155         disconnect_action->setEnabled(true);
156         toolbar->setEnabled(true);
157 }
158
159 void MainWindow::disconnectFromHost()
160 {
161         if(!vnc_view)
162                 return;
163
164         vnc_view->startQuitting();
165         scroll_area->setWidget(0);
166
167         vnc_view->disconnect(); //remove all connections
168         delete vnc_view;
169         vnc_view = 0;
170         disconnect_action->setEnabled(false);
171         toolbar->setEnabled(false);
172 }
173
174 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
175 {
176         static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
177
178         switch(status) {
179         case RemoteView::Connecting:
180                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
181                 break;
182         case RemoteView::Connected:
183                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
184                 break;
185         case RemoteView::Disconnecting:
186                 if(old_status != RemoteView::Disconnected) { //Disconnecting also occurs while connecting, so check last state
187                         QMaemo5InformationBox::information(this, "Connection lost");
188                 }
189                 break;
190         case RemoteView::Disconnected:
191                 if(old_status == RemoteView::Disconnecting) {
192                         scroll_area->setWidget(0); //remove widget
193                 }
194                 break;
195         }
196
197         old_status = status;
198 }