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