3 Copyright (C) 2010 Christian Pulvermacher
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "connectdialog.h"
21 #include "rfb/rfbclient.h"
26 #include <QMaemo5ValueButton>
27 #include <QMaemo5ListPickSelector>
31 const QString LISTEN_FOR_INCOMING_CONNECTIONS_STRING = QObject::tr("Listen for incoming connections");
33 ConnectDialog::ConnectDialog(QWidget *parent):
37 setWindowTitle(tr("Connect to VNC Server"));
41 settings.beginGroup("hosts");
42 QStringList hostnames = settings.childGroups();
43 QMap<int, QString> hosts_map; //use position as key
44 foreach(QString hostname, hostnames) {
45 if(!settings.contains(hostname + "/position")) {
46 continue; //can happen when host was given as a command line argument, don't show those
49 int position = settings.value(hostname + "/position").toInt();
50 hosts_map.insert(position, hostname);
52 hostnames_sorted = hosts_map.values(); //sorted by ascending position
56 hosts.addItems(hostnames_sorted);
57 hosts.insertSeparator(hosts.count());
58 hosts.addItem(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_received.png"), LISTEN_FOR_INCOMING_CONNECTIONS_STRING);
59 hosts.setEditable(true);
61 hosts.lineEdit()->setInputMethodHints(Qt::ImhNoAutoUppercase); //somehow this doesn't work that well here
63 connect(&hosts, SIGNAL(editTextChanged(QString)),
64 this, SLOT(hostnameUpdated(QString)));
65 layout.addWidget(&hosts);
68 QMaemo5ValueButton *quality = new QMaemo5ValueButton(tr("Quality"), this);
69 quality_selector = new QMaemo5ListPickSelector(this);
70 QStandardItemModel *model = new QStandardItemModel(0, 1, this);
71 model->appendRow(new QStandardItem(tr("High\t\t(LAN)"))); //1
72 model->appendRow(new QStandardItem(tr("Medium\t(DSL)"))); //2
73 model->appendRow(new QStandardItem(tr("Low\t\t(ISDN)"))); //3
74 quality_selector->setModel(model);
75 quality->setPickSelector(quality_selector);
76 quality->setValueLayout(QMaemo5ValueButton::ValueUnderText);
77 quality->setMaximumWidth(120);
78 layout.addWidget(quality);
80 //combobox numbering starts from 0, so currentIndex() == quality-1
81 quality_combobox.addItem(tr("High quality (LAN)")); //1
82 quality_combobox.addItem(tr("Medium quality (DSL)")); //2
83 quality_combobox.addItem(tr("Low quality (ISDN)")); //3
84 //quality_combobox.setMaximumWidth(120);
85 layout.addWidget(&quality_combobox);
88 hostnameUpdated(hosts.lineEdit()->text()); //get saved quality for last host, or 2
90 done->setText(tr("Connect"));
91 done->setMaximumWidth(110);
92 connect(done, SIGNAL(clicked()),
93 this, SLOT(accept()));
94 layout.addWidget(done);
98 connect(this, SIGNAL(finished(int)),
99 this, SLOT(deleteLater()));
102 void ConnectDialog::hostnameUpdated(QString newtext)
104 const int cursorpos = hosts.lineEdit()->cursorPosition();
106 const bool normal_entry = hosts.itemIcon(hosts.currentIndex()).isNull();
107 done->setText(normal_entry ? tr("Connect") : tr("Listen"));
109 //unselect 'listen ...' entry if edited
111 if(newtext != LISTEN_FOR_INCOMING_CONNECTIONS_STRING) {
112 hosts.setCurrentIndex(-1);
118 //clean up hostname (we don't want / or \ in saved hostnames)
119 newtext.remove(QChar('/'));
120 newtext.remove(QChar('\\'));
121 hosts.lineEdit()->setText(newtext);
122 hosts.lineEdit()->setCursorPosition(cursorpos);
124 //saved quality setting available?
126 int quality = settings.value(QString("hosts/%1/quality").arg(hosts.lineEdit()->text()), 2).toInt();
127 if(quality < 1 or quality > 3)
130 quality_selector->setCurrentIndex(quality-1);
132 quality_combobox.setCurrentIndex(quality-1);
136 void ConnectDialog::accept()
140 QString selected_host = hosts.currentText();
141 if(selected_host.isEmpty()) {
146 int quality = quality_selector->currentIndex() + 1;
148 int quality = quality_combobox.currentIndex() + 1;
152 if(!hosts.itemIcon(hosts.currentIndex()).isNull()) {
153 int listen_port = settings.value("listen_port", LISTEN_PORT_OFFSET).toInt();
155 #if QT_VERSION >= 0x040500
156 //ask user if listen_port is correct
158 listen_port = QInputDialog::getInt(this,
159 tr("Listen for Incoming Connections"),
160 tr("Listen on Port:"),
161 listen_port, 1, 65535, //value, min, max
167 settings.setValue("listen_port", listen_port);
168 emit connectToHost("", quality, listen_port);
173 settings.beginGroup("hosts");
174 const bool new_item = !hostnames_sorted.contains(selected_host);
175 const bool used_old_host = !new_item and hosts.currentIndex() > 0;
176 //if both are false, we don't need to mess with positions
178 if(new_item or used_old_host) {
179 //put selected_host at the top
180 settings.setValue(QString("%1/position").arg(selected_host), 0);
182 //don't create duplicates
184 hostnames_sorted.removeAll(selected_host);
186 //now rebuild list for positions >= 1
187 for(int i = 0; i < hostnames_sorted.size(); i++)
188 settings.setValue(QString("%1/position").arg(hostnames_sorted.at(i)), i+1);
192 settings.setValue(QString("%1/quality").arg(selected_host), quality);
198 emit connectToHost(QString("vnc://%1").arg(selected_host), quality, 0);