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 const QString LISTEN_FOR_INCOMING_CONNECTIONS_STRING = QObject::tr("Listen for incoming connections");
28 ConnectDialog::ConnectDialog(QWidget *parent):
31 setWindowTitle(tr("Connect to VNC Server"));
35 settings.beginGroup("hosts");
36 QStringList hostnames = settings.childGroups();
37 QMap<int, QString> hosts_map; //use position as key
38 foreach(QString hostname, hostnames) {
39 if(!settings.contains(hostname + "/position")) {
40 continue; //can happen when host was given as a command line argument, don't show those
43 int position = settings.value(hostname + "/position").toInt();
44 hosts_map.insert(position, hostname);
46 hostnames_sorted = hosts_map.values(); //sorted by ascending position
49 QHBoxLayout *layout = new QHBoxLayout(this);
50 QGridLayout *grid_layout = new QGridLayout();
51 layout->addLayout(grid_layout);
54 hosts.addItems(hostnames_sorted);
55 hosts.insertSeparator(hosts.count());
56 hosts.addItem(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_received.png"), LISTEN_FOR_INCOMING_CONNECTIONS_STRING);
57 hosts.setEditable(true);
59 hosts.lineEdit()->setInputMethodHints(Qt::ImhNoAutoUppercase); //somehow this doesn't work that well here
61 connect(&hosts, SIGNAL(editTextChanged(QString)),
62 this, SLOT(hostnameUpdated(QString)));
63 grid_layout->addWidget(&hosts, 0, 0, 1, 2);
65 grid_layout->addWidget(new QLabel(tr("Quality")), 1, 0);
66 //combobox numbering starts from 0, so currentIndex() == quality-1
67 quality_combobox.addItem(tr("High (LAN)")); //1
68 quality_combobox.addItem(tr("Medium (DSL)")); //2
69 quality_combobox.addItem(tr("Low (ISDN)")); //3
70 grid_layout->addWidget(&quality_combobox, 1, 1);
72 viewonly.setText(tr("View only"));
73 grid_layout->addWidget(&viewonly, 2, 0, 1, 2);
75 done.setMaximumWidth(110);
76 connect(&done, SIGNAL(clicked()),
77 this, SLOT(accept()));
78 layout->addWidget(&done);
80 //get settings for last host
81 hostnameUpdated(hosts.lineEdit()->text());
83 connect(this, SIGNAL(finished(int)),
84 this, SLOT(deleteLater()));
87 void ConnectDialog::hostnameUpdated(QString newtext)
89 const int cursorpos = hosts.lineEdit()->cursorPosition();
91 const bool normal_entry = hosts.itemIcon(hosts.currentIndex()).isNull();
92 done.setText(normal_entry ? tr("Connect") : tr("Listen"));
94 //unselect 'listen ...' entry if edited
96 if(newtext != LISTEN_FOR_INCOMING_CONNECTIONS_STRING) {
97 hosts.setCurrentIndex(-1);
103 //clean up hostname (we don't want / or \ in saved hostnames)
104 newtext.remove(QChar('/'));
105 newtext.remove(QChar('\\'));
106 hosts.lineEdit()->setText(newtext);
107 hosts.lineEdit()->setCursorPosition(cursorpos);
109 //saved quality setting available?
111 int quality = settings.value(QString("hosts/%1/quality").arg(hosts.lineEdit()->text()), 2).toInt();
112 if(quality < 1 or quality > 3)
114 quality_combobox.setCurrentIndex(quality-1);
116 //saved viewonly setting?
117 viewonly.setChecked(settings.value(QString("hosts/%1/viewonly").arg(hosts.lineEdit()->text()), false).toBool());
120 void ConnectDialog::accept()
124 QString selected_host = hosts.currentText();
125 if(selected_host.isEmpty()) {
129 int quality = quality_combobox.currentIndex() + 1;
132 if(!hosts.itemIcon(hosts.currentIndex()).isNull()) {
133 //listen mode selected
134 int listen_port = settings.value("listen_port", LISTEN_PORT_OFFSET).toInt();
136 //ask user if listen_port is correct
138 listen_port = QInputDialog::getInt(this,
139 tr("Listen for Incoming Connections"),
140 tr("Listen on Port:"),
141 listen_port, 1, 65535, //value, min, max
145 settings.setValue("listen_port", listen_port);
146 emit connectToHost("", quality, listen_port, viewonly.isChecked());
151 settings.beginGroup("hosts");
152 const bool new_item = !hostnames_sorted.contains(selected_host);
153 const bool used_old_host = !new_item and hosts.currentIndex() > 0;
154 //if both are false, we don't need to mess with positions
156 if(new_item or used_old_host) {
157 //put selected_host at the top
158 settings.setValue(QString("%1/position").arg(selected_host), 0);
160 //don't create duplicates
162 hostnames_sorted.removeAll(selected_host);
164 //now rebuild list for positions >= 1
165 for(int i = 0; i < hostnames_sorted.size(); i++)
166 settings.setValue(QString("%1/position").arg(hostnames_sorted.at(i)), i+1);
169 settings.setValue(QString("%1/quality").arg(selected_host), quality);
170 settings.setValue(QString("%1/viewonly").arg(selected_host), viewonly.isChecked());
175 //listen_port = 0 is ignored
176 emit connectToHost(QString("vnc://%1").arg(selected_host), quality, 0, viewonly.isChecked());