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