just unselect 'listen ...' entry when edited by user
[presencevnc] / src / connectdialog.cpp
index 55c7bf5..e3f52de 100644 (file)
 #endif
 
 #include "connectdialog.h"
+#include "rfb/rfbclient.h"
 
-#include <iostream>
 
+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;
@@ -37,26 +39,22 @@ ConnectDialog::ConnectDialog(QWidget *parent):
        //read history
        settings.beginGroup("hosts");
        QStringList hostnames = settings.childGroups();
-       QStringList hostnames_sorted = hostnames;
+       QMap<int, QString> hosts_map; //use position as key
        foreach(QString hostname, hostnames) {
                if(!settings.contains(hostname + "/position")) {
-                       //can happen when host was given as a command line argument, don't show those
-                       hostnames_sorted.removeAll(hostname);
-                       continue;
+                       continue; //can happen when host was given as a command line argument, don't show those
                }
 
                int position = settings.value(hostname + "/position").toInt();
-               if(position < 0)
-                       position = 0;
-               else if(position >= hostnames_sorted.size())
-                       position = hostnames_sorted.size()-1;
-
-               hostnames_sorted.replace(position, hostname);
+               hosts_map.insert(position, hostname);
        }
+       hostnames_sorted = hosts_map.values(); //sorted by ascending position
        settings.endGroup();
 
        //set up combobox
        hosts.addItems(hostnames_sorted);
+       hosts.insertSeparator(hosts.count());
+       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
@@ -81,22 +79,37 @@ 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()));
        layout.addWidget(done);
 
        setLayout(&layout);
+
+       connect(this, SIGNAL(finished(int)),
+               this, SLOT(deleteLater()));
 }
 
 void ConnectDialog::hostnameUpdated(QString newtext)
 {
-       //clean up hostname
+       const int cursorpos = hosts.lineEdit()->cursorPosition();
+
+       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;
+               }
+       }
+
+       //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
@@ -113,51 +126,63 @@ void ConnectDialog::accept()
 {
        QDialog::accept();
 
-       if(hosts.currentText().isEmpty()) {
-               deleteLater();
+       QString selected_host = hosts.currentText();
+       if(selected_host.isEmpty()) {
                return;
        }
 
-       //save url?
+#ifdef Q_WS_MAEMO_5
+       int quality = quality_selector->currentIndex() + 1;
+#else
+       int quality = 2;
+#endif
+
        QSettings settings;
-       settings.beginGroup("hosts");
-       bool new_item = !settings.contains(hosts.currentText());
-       bool used_old_host = !new_item and hosts.currentIndex() > 0;
-       int rearrange_up_to_pos;
-       if(new_item) {
-               std::cout << "adding new item to history\n";
-               rearrange_up_to_pos = hosts.count(); //use free index
-       } else if(used_old_host) {
-               rearrange_up_to_pos = hosts.currentIndex();
+       if(!hosts.itemIcon(hosts.currentIndex()).isNull()) {
+               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;
        }
 
-       if(new_item or used_old_host) {
-               std::cout << "rearranging history,  last index " << rearrange_up_to_pos << "\n";
+       settings.beginGroup("hosts");
+       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
 
-               QStringList hostnames = settings.childGroups();
-               foreach(QString hostname, hostnames) {
-                       if(!settings.contains(hostname + "/position"))
-                               continue; //ignore entries without position
+       if(new_item or used_old_host) {
+               //put selected_host at the top
+               settings.setValue(QString("%1/position").arg(selected_host), 0);
 
-                       int position = settings.value(hostname + "/position").toInt();
-                       if(position < rearrange_up_to_pos)
-                               settings.setValue(hostname + "/position", position+1);
-               }
-               //position 0 is now free
+               //don't create duplicates
+               if(used_old_host)
+                       hostnames_sorted.removeAll(selected_host);
 
-               //move selected host to front
-               settings.setValue(QString("%1/position").arg(hosts.currentText()), 0);
+               //now rebuild list for positions >= 1
+               for(int i = 0; i < hostnames_sorted.size(); i++)
+                       settings.setValue(QString("%1/position").arg(hostnames_sorted.at(i)), i+1);
        }
+
 #ifdef Q_WS_MAEMO_5
-       int quality = quality_selector->currentIndex() + 1;
-       settings.setValue(QString("%1/quality").arg(hosts.currentText()), quality);
-#else
-       int quality = 2;
+       settings.setValue(QString("%1/quality").arg(selected_host), quality);
 #endif
 
        settings.endGroup();
        settings.sync();
 
-       emit connectToHost(QString("vnc://%1").arg(hosts.currentText()), quality);
-       deleteLater();
+       emit connectToHost(QString("vnc://%1").arg(selected_host), quality, 0);
 }