Increase portability
[presencevnc] / src / mainwindow.cpp
index 364d20b..3a3b9b6 100644 (file)
+/*
+    Presence VNC
+    Copyright (C) 2010 Christian Pulvermacher
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License along
+    with this program; if not, write to the Free Software Foundation, Inc.,
+    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+#include "connectdialog.h"
+#include "fullscreenexitbutton.h"
+#include "keymenu.h"
 #include "mainwindow.h"
+#include "preferences.h"
 #include "vncview.h"
 
+#ifdef Q_WS_HILDON
 #include <QtMaemo5>
 #include <QX11Info>
 
 #include <X11/Xlib.h>
 #include <X11/Xatom.h>
+#endif
 
-#include <iostream>
 
 MainWindow::MainWindow(QString url, int quality):
        QMainWindow(0),
        vnc_view(0),
-       scroll_area(new QScrollArea(0))
+       scroll_area(new ScrollArea(0))
 {
        setWindowTitle("Presence VNC");
-//     swipe_start = QPoint(0,0);
-       setAttribute(Qt::WA_Maemo5StackedWindow);
+
+       migrateConfiguration();
        QSettings settings;
 
+#ifdef Q_WS_HILDON
+       setAttribute(Qt::WA_Maemo5StackedWindow);
+#endif
+
        //set up toolbar
        toolbar = new QToolBar(0);
-       toolbar->addAction("Mod", this, SLOT(showModifierMenu()));
-       toolbar->addAction("Tab", this, SLOT(sendTab()));
-       toolbar->addAction("Esc", this, SLOT(sendEsc()));
-       toolbar->addAction("PgUp", this, SLOT(sendPgUp()));
-       toolbar->addAction("PgDn", this, SLOT(sendPgDn()));
+       toolbar->addAction(QChar(0x2026), this, SLOT(showKeyMenu())); //"..." button
+       toolbar->addAction(tr("Tab"), this, SLOT(sendTab()));
+       toolbar->addAction(tr("Esc"), this, SLOT(sendEsc()));
+       toolbar->addAction(tr("PgUp"), this, SLOT(sendPgUp()));
+       toolbar->addAction(tr("PgDn"), this, SLOT(sendPgDn()));
+#ifdef Q_WS_HILDON
+       toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/chat_enter.png"), "", this, SLOT(sendReturn()));
+       toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/control_keyboard.png"), "", this, SLOT(showInputPanel()));
+#endif
+
+       //move fullscreen button to the right
+       QWidget *spacer = new QWidget();
+       spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
+       toolbar->addWidget(spacer);
+
        toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_fullsize.png"), "", this, SLOT(toggleFullscreen()));
        addToolBar(toolbar);
        toolbar->setVisible(settings.value("show_toolbar", true).toBool());
 
        //set up menu
-       QMenuBar *menu = new QMenuBar(this);
-       QAction *connect_action = new QAction("Connect", this);
-       disconnect_action = new QAction("Disconnect", this);
-       menu->addAction(connect_action);
-       menu->addAction(disconnect_action);
-       scaling = new QAction("Rescale Remote Screen", this);
+       QAction *connect_action = new QAction(tr("Connect"), this);
+       disconnect_action = new QAction(tr("Disconnect"), this);
+       scaling = new QAction(tr("Fit to Screen"), this);
        scaling->setCheckable(true);
        scaling->setChecked(settings.value("rescale", true).toBool());
-       menu->addAction(scaling);
-       QAction *show_toolbar = new QAction("Show Toolbar", this);
+       show_toolbar = new QAction(tr("Show Toolbar"), this);
        show_toolbar->setCheckable(true);
        show_toolbar->setChecked(settings.value("show_toolbar", true).toBool());
-       menu->addAction(show_toolbar);
-       QAction *about_action = new QAction("About", this);
-       menu->addAction(about_action);
-
-       //menu->setAttribute(Qt::WA_Maemo5StackedWindow);
-       //menu->hide();
+       QAction *pref_action = new QAction(tr("Preferences"), this);
+       QAction *about_action = new QAction(tr("About"), this);
+
+#ifdef Q_WS_HILDON
+       menuBar()->addAction(connect_action);
+       menuBar()->addAction(disconnect_action);
+       menuBar()->addAction(scaling);
+       menuBar()->addAction(show_toolbar);
+       menuBar()->addAction(pref_action);
+       menuBar()->addAction(about_action);
+#else
+       QMenu* session_menu = menuBar()->addMenu(tr("&Session"));
+       session_menu->addAction(connect_action);
+       session_menu->addAction(disconnect_action);
+       session_menu->addSeparator();
+       //session_menu->addAction(pref_action);
+       //session_menu->addSeparator();
+       session_menu->addAction(tr("&Quit"), this, SLOT(close()));
+
+       QMenu* view_menu = menuBar()->addMenu(tr("&View"));
+       view_menu->addAction(scaling);
+       view_menu->addAction(show_toolbar);
+
+       QMenu* help_menu = menuBar()->addMenu(tr("&Help"));
+       help_menu->addAction(about_action);
+#endif
 
        connect(about_action, SIGNAL(triggered()),
                this, SLOT(about()));
+       connect(pref_action, SIGNAL(triggered()),
+               this, SLOT(showPreferences()));
        connect(connect_action, SIGNAL(triggered()),
-               this, SLOT(connectDialog()));
+               this, SLOT(showConnectDialog()));
        connect(disconnect_action, SIGNAL(triggered()),
                this, SLOT(disconnectFromHost()));
        connect(show_toolbar, SIGNAL(toggled(bool)),
@@ -62,20 +117,21 @@ MainWindow::MainWindow(QString url, int quality):
                this, SLOT(forceResizeDelayed()));
 
        setCentralWidget(scroll_area);
+       new FullScreenExitButton(this);
 
        grabZoomKeys(true);
-       setAttribute(Qt::WA_Maemo5AutoOrientation, true);
-       //setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
+       reloadSettings();
 
+       //TODO: can i replace this with resizeEvent?
        connect(QApplication::desktop(), SIGNAL(resized(int)),
                this, SLOT(forceResize()));
 
        if(url.isNull()) {
                disconnect_action->setEnabled(false);
                toolbar->setEnabled(false);
-               connectDialog();
+               showConnectDialog();
        } else {
-               vnc_view = new VncView(0, url, RemoteView::Quality(quality));
+               vnc_view = new VncView(this, url, RemoteView::Quality(quality));
                connect(scaling, SIGNAL(toggled(bool)),
                        vnc_view, SLOT(enableScaling(bool)));
                connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
@@ -88,6 +144,7 @@ MainWindow::MainWindow(QString url, int quality):
 
 void MainWindow::grabZoomKeys(bool grab)
 {
+#ifdef Q_WS_HILDON
        unsigned long val = (grab)?1:0;
        Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
        if(!atom) {
@@ -96,13 +153,14 @@ void MainWindow::grabZoomKeys(bool grab)
        }
        XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
                32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
+#endif
 }
 
 void MainWindow::closeEvent(QCloseEvent*) {
        grabZoomKeys(false);
 
        QSettings settings;
-       settings.setValue("show_toolbar", toolbar->isVisible());
+       settings.setValue("show_toolbar", show_toolbar->isChecked());
        settings.setValue("rescale", scaling->isChecked());
        settings.sync();
 
@@ -113,59 +171,34 @@ void MainWindow::closeEvent(QCloseEvent*) {
 
 void MainWindow::about() {
        QMessageBox::about(this, tr("About Presence VNC"),
-               tr("<center><h1>Presence VNC 0.1 beta</h1>\
-A touch screen friendly VNC client\
-<small><p>&copy;2010 Christian Pulvermacher &lt;pulvermacher@gmx.de&gt</p>\
-<p>Based on KRDC, &copy; 2007-2008 Urs Wolfer</small></center>\
-<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>"));
+               tr("<center><h1>Presence VNC 0.5</h1>\
+<p>A touchscreen friendly VNC client</p>\
+<p><a href=\"https://garage.maemo.org/projects/presencevnc/\">https://garage.maemo.org/projects/presencevnc</a></p></center>\
+<small><p>&copy;2010 Christian Pulvermacher &lt;pulvermacher@gmx.de&gt;</p>\
+<p>Based on KRDC, &copy; 2007-2008 Urs Wolfer</p>\
+<p>and LibVNCServer, &copy; 2001-2003 Johannes E. Schindelin</p>\
+<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></small>"));
 }
 
-/* swipe not used, and doesn't work without scaling anyway :/
-virtual bool event(QEvent *event) {
-       if(event->type() == QEvent::MouseMove) {
-               QMouseEvent *ev = dynamic_cast<QMouseEvent* >(event);
-               if(!swipe_start.isNull()) {
-                       QPoint diff = swipe_start - ev->pos();
-                       const int swipe_dist = 60;
-                       if(diff.x() > swipe_dist and diff.y() < swipe_dist and diff.y() > -swipe_dist) { //
-                               menu->show();
-                               swipe_start = QPoint(0,0);
-                       }
-               } else if((width() - ev->x()) < 10) {
-                       swipe_start = ev->pos();
-               }
-               std::cout << "mousex: " << width() - ev->x() << "\n";
-               //TODO: make scrolling over border result in wheel events? i get weird (out of range) mouse events when that happens
-               return true;
-       } else if(event->type() == QEvent::MouseButtonRelease) {
-               swipe_start = QPoint(0,0);
-               return true;
-       } else {
-//                     std::cout << "event " << event->type() << "\n";
-               return QScrollArea::event(event);
-       }
+void MainWindow::showConnectDialog()
+{
+       ConnectDialog *connect_dialog = new ConnectDialog(this);
+       connect(connect_dialog, SIGNAL(connectToHost(QString)),
+               this, SLOT(connectToHost(QString)));
+       connect_dialog->exec();
 }
-*/
 
-void MainWindow::connectDialog()
+void MainWindow::connectToHost(QString url)
 {
-       QSettings settings;
-       QString url = QInputDialog::getText(this, "Connect to Host", "VNC Server:", QLineEdit::Normal, settings.value("last_hostname", "").toString());
-       if(url.isEmpty()) { //dialog dismissed or nothing entered
-               return;
-       }
-       settings.setValue("last_hostname", url);
-       url = "vnc://" + url;
-
        disconnectFromHost();
 
-       vnc_view = new VncView(0, url, RemoteView::Quality(2)); //TODO: get quality in dialog
-       scroll_area->setWidget(vnc_view);
+       vnc_view = new VncView(this, url, RemoteView::Quality(2));
 
        connect(scaling, SIGNAL(toggled(bool)),
                vnc_view, SLOT(enableScaling(bool)));
        connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
                this, SLOT(statusChanged(RemoteView::RemoteStatus)));
+       scroll_area->setWidget(vnc_view);
        vnc_view->start();
        vnc_view->enableScaling(scaling->isChecked());
        disconnect_action->setEnabled(true);
@@ -177,10 +210,9 @@ void MainWindow::disconnectFromHost()
        if(!vnc_view)
                return;
 
-       vnc_view->startQuitting();
        scroll_area->setWidget(0);
 
-       vnc_view->disconnect(); //remove all connections
+       vnc_view->disconnect(); //remove all signal-slot connections
        delete vnc_view;
        vnc_view = 0;
        disconnect_action->setEnabled(false);
@@ -193,17 +225,41 @@ void MainWindow::statusChanged(RemoteView::RemoteStatus status)
 
        switch(status) {
        case RemoteView::Connecting:
+#ifdef Q_WS_HILDON
                setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
+#endif
                break;
        case RemoteView::Connected:
+#ifdef Q_WS_HILDON
                setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
+#endif
+               if(!scaling->isChecked()) {
+                       //ugly hack to force a refresh (forceFullRepaint() doesn't repaint?? -> vnc_view hidden???)
+                       vnc_view->resize(scroll_area->size());
+                       vnc_view->enableScaling(false);
+               }
                break;
        case RemoteView::Disconnecting:
                if(old_status != RemoteView::Disconnected) { //Disconnecting also occurs while connecting, so check last state
-                       QMaemo5InformationBox::information(this, "Connection lost");
+#ifdef Q_WS_HILDON
+                       QMaemo5InformationBox::information(this, tr("Connection lost"));
+#endif
+                       
+                       //clean up
+                       scroll_area->setWidget(0);
+                       vnc_view = 0;
+                       disconnect_action->setEnabled(false);
+                       toolbar->setEnabled(false);
+
+                       //exit fullscreen mode
+                       if(windowState() & Qt::WindowFullScreen)
+                               setWindowState(windowState() ^ Qt::WindowFullScreen);
                }
                break;
        case RemoteView::Disconnected:
+#ifdef Q_WS_HILDON
+               setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
+#endif
                if(old_status == RemoteView::Disconnecting) {
                        scroll_area->setWidget(0); //remove widget
                }
@@ -229,27 +285,54 @@ void MainWindow::forceResizeDelayed()
 
 void MainWindow::toggleFullscreen()
 {
+       toolbar->setVisible(show_toolbar->isChecked() and (windowState() & Qt::WindowFullScreen)); //hide toolbar in fullscreen
        setWindowState(windowState() ^ Qt::WindowFullScreen); 
        forceResizeDelayed();
 }
 
-void MainWindow::showModifierMenu()
+void MainWindow::showKeyMenu()
 {
-       static QMenu *mod_menu = new QMenu(tr("Modifiers"), this);
-       static QAction *win = mod_menu->addAction(tr("Win"));
-       static QAction *alt = mod_menu->addAction(tr("Alt"));
-       win->setCheckable(true);
-       alt->setCheckable(true);
-
-       //show menu at top-left corner of toolbar
-       QAction *chosen = mod_menu->exec(toolbar->mapToGlobal(QPoint(0,0)));
-       if(!chosen) {
-               return;
-       } else if(chosen == alt) {
-               vnc_view->sendKey(Qt::Key_Alt);
-       } else if(chosen == win) {
-               vnc_view->sendKey(Qt::Key_Meta);
-       } else {
-               std::cout << "unhandled action?\n";
-       }
+       static KeyMenu *key_menu = new KeyMenu(this);
+       key_menu->exec();
+
+       vnc_view->sendKeySequence(key_menu->getKeySequence());
+}
+
+void MainWindow::showPreferences()
+{
+       Preferences *p = new Preferences(this);
+       p->exec();
+       delete p;
+
+       reloadSettings();
+}
+
+void MainWindow::reloadSettings()
+{
+       QSettings settings;
+#ifdef Q_WS_HILDON
+       int rotation = settings.value("screen_rotation", 0).toInt();
+       setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
+       setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
+       setAttribute(Qt::WA_Maemo5PortraitOrientation, rotation == 2);
+#endif
+
+       if(vnc_view)
+               vnc_view->reloadSettings();
+}
+
+void MainWindow::resizeEvent(QResizeEvent *event)
+{
+       QMainWindow::resizeEvent(event);
+
+       forceResize();
+}
+
+void MainWindow::showInputPanel()
+{
+#ifdef Q_WS_HILDON
+       vnc_view->setAttribute(Qt::WA_InputMethodEnabled, true);
+       QEvent event(QEvent::RequestSoftwareInputPanel);
+       QApplication::sendEvent(vnc_view, &event);
+#endif
 }