ef629d6fc3d71b1ec11dd928290f30d25ab08bd7
[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         setAttribute(Qt::WA_Maemo5StackedWindow);
20         QSettings settings;
21
22         //set up toolbar
23         toolbar = new QToolBar(0);
24         //toolbar->setAttribute(Qt::WA_InputMethodEnabled, true);
25         toolbar->addAction("Mod", this, SLOT(showModifierMenu()));
26         toolbar->addAction("Tab", this, SLOT(sendTab()));
27         toolbar->addAction("Esc", this, SLOT(sendEsc()));
28         toolbar->addAction("PgUp", this, SLOT(sendPgUp()));
29         toolbar->addAction("PgDn", this, SLOT(sendPgDn()));
30 //      toolbar->addAction("IM", toolbar, SLOT(setFocus())); //doesn't work
31         toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_fullsize.png"), "", this, SLOT(toggleFullscreen()));
32         addToolBar(toolbar);
33         toolbar->setVisible(settings.value("show_toolbar", true).toBool());
34
35         //set up menu
36         QMenuBar *menu = new QMenuBar(this);
37         QAction *connect_action = new QAction("Connect", this);
38         disconnect_action = new QAction("Disconnect", this);
39 //      menu->addAction(connect_action);
40 //      menu->addAction(disconnect_action);
41         scaling = new QAction("Fit to Screen", this);
42         scaling->setCheckable(true);
43         scaling->setChecked(settings.value("rescale", true).toBool());
44         menu->addAction(scaling);
45         QAction *show_toolbar = new QAction("Show Toolbar", this);
46         show_toolbar->setCheckable(true);
47         show_toolbar->setChecked(settings.value("show_toolbar", true).toBool());
48         menu->addAction(show_toolbar);
49         QAction *pref_action = new QAction("Preferences", this);
50         menu->addAction(pref_action);
51         QAction *about_action = new QAction("About", this);
52         menu->addAction(about_action);
53
54         //menu->setAttribute(Qt::WA_Maemo5StackedWindow);
55         //menu->hide();
56
57         connect(about_action, SIGNAL(triggered()),
58                 this, SLOT(about()));
59         connect(pref_action, SIGNAL(triggered()),
60                 this, SLOT(showPreferences()));
61         connect(connect_action, SIGNAL(triggered()),
62                 this, SLOT(connectDialog()));
63         connect(disconnect_action, SIGNAL(triggered()),
64                 this, SLOT(disconnectFromHost()));
65         connect(show_toolbar, SIGNAL(toggled(bool)),
66                 toolbar, SLOT(setVisible(bool)));
67         connect(show_toolbar, SIGNAL(toggled(bool)),
68                 this, SLOT(forceResizeDelayed()));
69
70         setCentralWidget(scroll_area);
71
72         grabZoomKeys(true);
73         reloadSettings();
74
75         connect(QApplication::desktop(), SIGNAL(resized(int)),
76                 this, SLOT(forceResize()));
77
78         if(url.isNull()) {
79                 disconnect_action->setEnabled(false);
80                 toolbar->setEnabled(false);
81                 connectDialog();
82         } else {
83                 vnc_view = new VncView(this, url, RemoteView::Quality(quality));
84                 connect(scaling, SIGNAL(toggled(bool)),
85                         vnc_view, SLOT(enableScaling(bool)));
86                 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
87                         this, SLOT(statusChanged(RemoteView::RemoteStatus)));
88                 scroll_area->setWidget(vnc_view);
89                 vnc_view->start();
90                 vnc_view->enableScaling(scaling->isChecked());
91         }
92
93         if(!vnc_view) //not connected
94                 QTimer::singleShot(100, this, SLOT(close()));
95 }
96
97 void MainWindow::grabZoomKeys(bool grab)
98 {
99         unsigned long val = (grab)?1:0;
100         Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
101         if(!atom) {
102                 qWarning("Couldn't get zoom key atom");
103                 return;
104         }
105         XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
106                 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
107 }
108
109 void MainWindow::closeEvent(QCloseEvent*) {
110         grabZoomKeys(false);
111
112         QSettings settings;
113         settings.setValue("show_toolbar", toolbar->isVisible());
114         settings.setValue("rescale", scaling->isChecked());
115         settings.sync();
116
117         hide();
118
119         disconnectFromHost();
120 }
121
122 void MainWindow::about() {
123         QMessageBox::about(this, tr("About Presence VNC"),
124                 tr("<center><h1>Presence VNC 0.3</h1>\
125 A touchscreen friendly VNC client\
126 <small><p>&copy;2010 Christian Pulvermacher &lt;pulvermacher@gmx.de&gt</p>\
127 <p>Based on KRDC, &copy; 2007-2008 Urs Wolfer</small></center>\
128 <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>"));
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(this, 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         vnc_view->enableScaling(scaling->isChecked());
152         disconnect_action->setEnabled(true);
153         toolbar->setEnabled(true);
154 }
155
156 void MainWindow::disconnectFromHost()
157 {
158         if(!vnc_view)
159                 return;
160
161 //TODO: crashes when deleting vnc_view - no idea why
162         vnc_view->startQuitting();
163         scroll_area->setWidget(0);
164
165         vnc_view->disconnect(); //remove all connections
166         //delete vnc_view;
167         //vnc_view = 0;
168         disconnect_action->setEnabled(false);
169         toolbar->setEnabled(false);
170 }
171
172 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
173 {
174         static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
175
176         switch(status) {
177         case RemoteView::Connecting:
178                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
179                 break;
180         case RemoteView::Connected:
181                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
182                 break;
183         case RemoteView::Disconnecting:
184                 if(old_status != RemoteView::Disconnected) { //Disconnecting also occurs while connecting, so check last state
185                         QMaemo5InformationBox::information(this, "Connection lost");
186                         close();
187                 }
188                 break;
189         case RemoteView::Disconnected:
190                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
191                 if(old_status == RemoteView::Disconnecting) {
192                         scroll_area->setWidget(0); //remove widget
193                 }
194                 break;
195         }
196
197         old_status = status;
198 }
199
200 //when rescaling is enabled, this resizes the widget to use available screen space
201 //necessary when rotating, showing fullscreen, etc.
202 void MainWindow::forceResize()
203 {
204         if(vnc_view and scaling->isChecked()) {
205                 vnc_view->resize(scroll_area->size());
206         }
207
208
209 void MainWindow::forceResizeDelayed()
210 {
211         QTimer::singleShot(500, this, SLOT(forceResize()));
212 }
213
214 void MainWindow::toggleFullscreen()
215 {
216         setWindowState(windowState() ^ Qt::WindowFullScreen); 
217         forceResizeDelayed();
218 }
219
220 void MainWindow::showModifierMenu()
221 {
222         static QMenu *mod_menu = new QMenu(tr("Modifiers"), this);
223         static QAction *win = mod_menu->addAction(tr("Win"));
224         static QAction *alt = mod_menu->addAction(tr("Alt"));
225         win->setCheckable(true);
226         alt->setCheckable(true);
227
228         //show menu at top-left corner of toolbar
229         QAction *chosen = mod_menu->exec(toolbar->mapToGlobal(QPoint(0,0)));
230         if(!chosen) {
231                 return;
232         } else if(chosen == alt) {
233                 vnc_view->sendKey(Qt::Key_Alt);
234         } else if(chosen == win) {
235                 vnc_view->sendKey(Qt::Key_Meta);
236         } else {
237                 std::cout << "unhandled action?\n";
238         }
239 }
240
241 void MainWindow::showPreferences()
242 {
243         Preferences *p = new Preferences(this);
244         p->exec();
245         delete p;
246
247         reloadSettings();
248 }
249
250 void MainWindow::reloadSettings()
251 {
252         QSettings settings;
253         int rotation = settings.value("screen_rotation", 0).toInt();
254         setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
255         setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
256         setAttribute(Qt::WA_Maemo5PortraitOrientation, rotation == 2);
257
258         if(vnc_view)
259                 vnc_view->reloadSettings();
260 }