just unselect 'listen ...' entry when edited by user
[presencevnc] / src / connectdialog.cpp
index 5019bd1..e3f52de 100644 (file)
 #endif
 
 #include "connectdialog.h"
+#include "rfb/rfbclient.h"
 
 
+const QString LISTEN_FOR_INCOMING_CONNECTIONS_STRING = QObject::tr("Listen for Incoming Connections");
+
 ConnectDialog::ConnectDialog(QWidget *parent):
-       QDialog(parent)
+       QDialog(parent),
+       done(new QPushButton)
 {
        setWindowTitle(tr("Connect to VNC Server"));
        QSettings settings;
@@ -50,15 +54,13 @@ ConnectDialog::ConnectDialog(QWidget *parent):
        //set up combobox
        hosts.addItems(hostnames_sorted);
        hosts.insertSeparator(hosts.count());
-       hosts.addItem(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_received.png"), tr("Listen for Incoming Connections"));
+       hosts.addItem(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_received.png"), LISTEN_FOR_INCOMING_CONNECTIONS_STRING);
        hosts.setEditable(true);
 #ifdef Q_WS_MAEMO_5
        hosts.lineEdit()->setInputMethodHints(Qt::ImhNoAutoUppercase); //somehow this doesn't work that well here
 #endif
        connect(&hosts, SIGNAL(editTextChanged(QString)),
                this, SLOT(hostnameUpdated(QString)));
-       connect(&hosts, SIGNAL(currentIndexChanged(int)),
-               this, SLOT(indexChanged(int)));
        layout.addWidget(&hosts);
 
 #ifdef Q_WS_MAEMO_5
@@ -77,7 +79,6 @@ ConnectDialog::ConnectDialog(QWidget *parent):
        hostnameUpdated(hosts.lineEdit()->text()); //get saved quality for last host, or 2
 #endif
 
-       QPushButton *done = new QPushButton(tr("Connect"));
        done->setMaximumWidth(110);
        connect(done, SIGNAL(clicked()),
                this, SLOT(accept()));
@@ -89,22 +90,26 @@ ConnectDialog::ConnectDialog(QWidget *parent):
                this, SLOT(deleteLater()));
 }
 
-void ConnectDialog::indexChanged(int index) {
-       if(index == -1)
-               return;
+void ConnectDialog::hostnameUpdated(QString newtext)
+{
+       const int cursorpos = hosts.lineEdit()->cursorPosition();
 
-       //disallow editing for special entries (icon set)
-       hosts.setEditable(hosts.itemIcon(index).isNull());
-}
+       const bool normal_entry = hosts.itemIcon(hosts.currentIndex()).isNull();
+       done->setText(normal_entry ? tr("Connect") : tr("Listen"));
 
+       //unselect 'listen ...' entry if edited
+       if(!normal_entry) {
+               if(newtext != LISTEN_FOR_INCOMING_CONNECTIONS_STRING) {
+                       hosts.setCurrentIndex(-1);
+               } else { 
+                       return;
+               }
+       }
 
-void ConnectDialog::hostnameUpdated(QString newtext)
-{
-       //clean up hostname
+       //clean up hostname (we don't want / or \ in saved hostnames)
        newtext.remove(QChar('/'));
        newtext.remove(QChar('\\'));
-       int cursorpos = hosts.lineEdit()->cursorPosition();
-       hosts.lineEdit()->setText(newtext.toLower());
+       hosts.lineEdit()->setText(newtext);
        hosts.lineEdit()->setCursorPosition(cursorpos);
 
 #ifdef Q_WS_MAEMO_5
@@ -125,15 +130,38 @@ void ConnectDialog::accept()
        if(selected_host.isEmpty()) {
                return;
        }
+
+#ifdef Q_WS_MAEMO_5
+       int quality = quality_selector->currentIndex() + 1;
+#else
+       int quality = 2;
+#endif
+
+       QSettings settings;
        if(!hosts.itemIcon(hosts.currentIndex()).isNull()) {
-               emit connectToHost("", 2, 5900); //TODO: quality and port from user input
+               int listen_port = settings.value("listen_port", LISTEN_PORT_OFFSET).toInt();
+
+#if QT_VERSION >= 0x040500
+               //ask user if listen_port is correct
+               bool ok;
+               listen_port = QInputDialog::getInt(this,
+                       tr("Listen for Incoming Connections"),
+                       tr("Listen on Port:"),
+                       listen_port, 1, 65535, //value, min, max
+                       1, &ok);
+#else
+               bool ok = true;
+#endif
+               if(ok) {
+                       settings.setValue("listen_port", listen_port);
+                       emit connectToHost("", quality, listen_port);
+               }
                return;
        }
 
-       QSettings settings;
        settings.beginGroup("hosts");
-       bool new_item = !hostnames_sorted.contains(selected_host);
-       bool used_old_host = !new_item and hosts.currentIndex() > 0;
+       const bool new_item = !hostnames_sorted.contains(selected_host);
+       const bool used_old_host = !new_item and hosts.currentIndex() > 0;
        //if both are false, we don't need to mess with positions
 
        if(new_item or used_old_host) {
@@ -150,10 +178,7 @@ void ConnectDialog::accept()
        }
 
 #ifdef Q_WS_MAEMO_5
-       int quality = quality_selector->currentIndex() + 1;
        settings.setValue(QString("%1/quality").arg(selected_host), quality);
-#else
-       int quality = 2;
 #endif
 
        settings.endGroup();