show status changes, save last hostname, add toolbar
[presencevnc] / src / mainwindow.cpp
1
2 #include "mainwindow.h"
3 #include "vncview.h"
4
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         QScrollArea(0)
14 {
15         swipe_start = QPoint(0,0);
16         setAttribute(Qt::WA_Maemo5StackedWindow);
17
18         //set up menu
19         QMenuBar *menu = new QMenuBar(this);
20         QAction *connect_action = new QAction("Connect", this);
21         disconnect_action = new QAction("Disconnect", this);
22         scaling = new QAction("Rescale Remote Screen", this);
23         scaling->setCheckable(true);
24         scaling->setChecked(true);
25         menu->addAction(scaling);
26         QAction *about_action = new QAction("About", this);
27         menu->addAction(about_action);
28
29         //menu->setAttribute(Qt::WA_Maemo5StackedWindow);
30         //menu->hide();
31
32         connect(about_action, SIGNAL(triggered()),
33                 this, SLOT(about()));
34         connect(connect_action, SIGNAL(triggered()),
35                 this, SLOT(connectDialog()));
36         connect(disconnect_action, SIGNAL(triggered()),
37                 this, SLOT(disconnectFromHost()));
38
39         grabZoomKeys(true);
40         setAttribute(Qt::WA_Maemo5AutoOrientation, true);
41         //setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
42
43         if(url.isNull()) {
44                 disconnect_action->setEnabled(false);
45                 connectDialog();
46         } else {
47                 vnc_view = new VncView(0, url, RemoteView::Quality(quality));
48                 connect(scaling, SIGNAL(toggled(bool)),
49                         vnc_view, SLOT(enableScaling(bool)));
50                 setWidget(vnc_view);
51                 vnc_view->start();
52         }
53 }
54
55 void MainWindow::grabZoomKeys(bool grab)
56 {
57         unsigned long val = (grab)?1:0;
58         Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
59         if(!atom) {
60                 qWarning("Couldn't get zoom key atom");
61                 return;
62         }
63         XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
64                 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
65 }
66
67 void MainWindow::closeEvent(QCloseEvent*) {
68         hide();
69         grabZoomKeys(false);
70         vnc_view->startQuitting();
71 }
72
73 void MainWindow::about() {
74         QMessageBox::about(this, tr("About Presence VNC"),
75                 tr("<center><h1>Presence VNC 0.1 alpha</h1>\
76 A touch screen friendly VNC client\
77 <small><p>&copy;2010 Christian Pulvermacher &lt;pulvermacher@gmx.de&gt</p>\
78 <p>Based on KRDC, &copy; 2007-2008 Urs Wolfer</small></center>\
79 <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>"));
80 }
81
82 /* swipe not used, and doesn't work without scaling anyway :/
83 virtual bool event(QEvent *event) {
84         if(event->type() == QEvent::MouseMove) {
85                 QMouseEvent *ev = dynamic_cast<QMouseEvent* >(event);
86                 if(!swipe_start.isNull()) {
87                         QPoint diff = swipe_start - ev->pos();
88                         const int swipe_dist = 60;
89                         if(diff.x() > swipe_dist and diff.y() < swipe_dist and diff.y() > -swipe_dist) { //
90                                 menu->show();
91                                 swipe_start = QPoint(0,0);
92                         }
93                 } else if((width() - ev->x()) < 10) {
94                         swipe_start = ev->pos();
95                 }
96                 std::cout << "mousex: " << width() - ev->x() << "\n";
97                 //TODO: make scrolling over border result in wheel events? i get weird (out of range) mouse events when that happens
98                 return true;
99         } else if(event->type() == QEvent::MouseButtonRelease) {
100                 swipe_start = QPoint(0,0);
101                 return true;
102         } else {
103 //                      std::cout << "event " << event->type() << "\n";
104                 return QScrollArea::event(event);
105         }
106 }
107 */
108
109 void MainWindow::connectDialog()
110 {
111         QString url = QInputDialog::getText(this, "Connect to Host", "VNC Server:");
112         if(url.isEmpty()) { //dialog dismissed or nothing entered
113                 return;
114         }
115         url = "vnc://" + url;
116
117         disconnectFromHost();
118
119         vnc_view = new VncView(0, url, RemoteView::Quality(2)); //TODO: get quality in dialog
120         setWidget(vnc_view);
121
122         connect(scaling, SIGNAL(toggled(bool)),
123                 vnc_view, SLOT(enableScaling(bool)));
124         vnc_view->start();
125         disconnect_action->setEnabled(true);
126 }
127
128 void MainWindow::disconnectFromHost()
129 {
130         vnc_view->startQuitting();
131         setWidget(0);
132
133         disconnect(scaling, SIGNAL(toggled(bool)),
134                 vnc_view, SLOT(enableScaling(bool)));
135         delete vnc_view;
136         vnc_view = 0;
137         disconnect_action->setEnabled(false);
138 }