disconnect now closes listening connections
[presencevnc] / src / connectdialog.cpp
1 /*
2     Presence VNC
3     Copyright (C) 2010 Christian Pulvermacher
4
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.
9
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.
14
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.
18 */
19 #include <QtGui>
20
21 #ifdef Q_WS_MAEMO_5
22 #include <QMaemo5ValueButton>
23 #include <QMaemo5ListPickSelector>
24 #endif
25
26 #include "connectdialog.h"
27 #include "rfb/rfbclient.h"
28
29
30 ConnectDialog::ConnectDialog(QWidget *parent):
31         QDialog(parent)
32 {
33         setWindowTitle(tr("Connect to VNC Server"));
34         QSettings settings;
35
36         //read history
37         settings.beginGroup("hosts");
38         QStringList hostnames = settings.childGroups();
39         QMap<int, QString> hosts_map; //use position as key
40         foreach(QString hostname, hostnames) {
41                 if(!settings.contains(hostname + "/position")) {
42                         continue; //can happen when host was given as a command line argument, don't show those
43                 }
44
45                 int position = settings.value(hostname + "/position").toInt();
46                 hosts_map.insert(position, hostname);
47         }
48         hostnames_sorted = hosts_map.values(); //sorted by ascending position
49         settings.endGroup();
50
51         //set up combobox
52         hosts.addItems(hostnames_sorted);
53         hosts.insertSeparator(hosts.count());
54         hosts.addItem(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_received.png"), tr("Listen for Incoming Connections"));
55         hosts.setEditable(true);
56 #ifdef Q_WS_MAEMO_5
57         hosts.lineEdit()->setInputMethodHints(Qt::ImhNoAutoUppercase); //somehow this doesn't work that well here
58 #endif
59         connect(&hosts, SIGNAL(editTextChanged(QString)),
60                 this, SLOT(hostnameUpdated(QString)));
61         connect(&hosts, SIGNAL(currentIndexChanged(int)),
62                 this, SLOT(indexChanged(int)));
63         layout.addWidget(&hosts);
64
65 #ifdef Q_WS_MAEMO_5
66         QMaemo5ValueButton *quality = new QMaemo5ValueButton(tr("Quality"), this);
67         quality_selector = new QMaemo5ListPickSelector(this);
68         QStandardItemModel *model = new QStandardItemModel(0, 1, this);
69         model->appendRow(new QStandardItem(tr("High\t\t(LAN)"))); //1
70         model->appendRow(new QStandardItem(tr("Medium\t(DSL)"))); //2
71         model->appendRow(new QStandardItem(tr("Low\t\t(ISDN)"))); //3
72         quality_selector->setModel(model);
73         quality->setPickSelector(quality_selector);
74         quality->setValueLayout(QMaemo5ValueButton::ValueUnderText);
75         quality->setMaximumWidth(120);
76         layout.addWidget(quality);
77
78         hostnameUpdated(hosts.lineEdit()->text()); //get saved quality for last host, or 2
79 #endif
80
81         QPushButton *done = new QPushButton(tr("Connect"));
82         done->setMaximumWidth(110);
83         connect(done, SIGNAL(clicked()),
84                 this, SLOT(accept()));
85         layout.addWidget(done);
86
87         setLayout(&layout);
88
89         connect(this, SIGNAL(finished(int)),
90                 this, SLOT(deleteLater()));
91 }
92
93 void ConnectDialog::indexChanged(int index) {
94         if(index == -1)
95                 return;
96
97         //disallow editing for special entries (icon set)
98         hosts.setEditable(hosts.itemIcon(index).isNull());
99 }
100
101
102 void ConnectDialog::hostnameUpdated(QString newtext)
103 {
104         //clean up hostname
105         newtext.remove(QChar('/'));
106         newtext.remove(QChar('\\'));
107         int cursorpos = hosts.lineEdit()->cursorPosition();
108         hosts.lineEdit()->setText(newtext.toLower());
109         hosts.lineEdit()->setCursorPosition(cursorpos);
110
111 #ifdef Q_WS_MAEMO_5
112         //saved quality setting available?
113         QSettings settings;
114         int quality = settings.value(QString("hosts/%1/quality").arg(hosts.lineEdit()->text()), 2).toInt();
115         if(quality < 1 or quality > 3)
116                 quality = 2;
117         quality_selector->setCurrentIndex(quality-1);
118 #endif
119 }
120
121 void ConnectDialog::accept()
122 {
123         QDialog::accept();
124
125         QString selected_host = hosts.currentText();
126         if(selected_host.isEmpty()) {
127                 return;
128         }
129
130 #ifdef Q_WS_MAEMO_5
131         int quality = quality_selector->currentIndex() + 1;
132 #else
133         int quality = 2;
134 #endif
135
136         QSettings settings;
137         if(!hosts.itemIcon(hosts.currentIndex()).isNull()) {
138                 int listen_port = settings.value("listen_port", LISTEN_PORT_OFFSET).toInt();
139
140 #if QT_VERSION >= 0x040500
141                 //ask user if listen_port is correct
142                 bool ok;
143                 listen_port = QInputDialog::getInt(this,
144                         tr("Listen for Incoming Connections"),
145                         tr("Listen on Port:"),
146                         listen_port, 1, 65535, //value, min, max
147                         1, &ok);
148 #else
149                 bool ok = true;
150 #endif
151                 if(ok) {
152                         settings.setValue("listen_port", listen_port);
153                         emit connectToHost("", quality, listen_port);
154                 }
155                 return;
156         }
157
158         settings.beginGroup("hosts");
159         bool new_item = !hostnames_sorted.contains(selected_host);
160         bool used_old_host = !new_item and hosts.currentIndex() > 0;
161         //if both are false, we don't need to mess with positions
162
163         if(new_item or used_old_host) {
164                 //put selected_host at the top
165                 settings.setValue(QString("%1/position").arg(selected_host), 0);
166
167                 //don't create duplicates
168                 if(used_old_host)
169                         hostnames_sorted.removeAll(selected_host);
170
171                 //now rebuild list for positions >= 1
172                 for(int i = 0; i < hostnames_sorted.size(); i++)
173                         settings.setValue(QString("%1/position").arg(hostnames_sorted.at(i)), i+1);
174         }
175
176 #ifdef Q_WS_MAEMO_5
177         settings.setValue(QString("%1/quality").arg(selected_host), quality);
178 #endif
179
180         settings.endGroup();
181         settings.sync();
182
183         emit connectToHost(QString("vnc://%1").arg(selected_host), quality, 0);
184 }