9c21d24dd053f654ab52116c0df856848dffeede
[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
20 #include "connectdialog.h"
21 #include "rfb/rfbclient.h"
22
23 #include <QtGui>
24
25 #ifdef Q_WS_MAEMO_5
26 #include <QMaemo5ValueButton>
27 #include <QMaemo5ListPickSelector>
28 #endif
29
30
31 const QString LISTEN_FOR_INCOMING_CONNECTIONS_STRING = QObject::tr("Listen for incoming connections");
32
33 ConnectDialog::ConnectDialog(QWidget *parent):
34         QDialog(parent),
35         done(new QPushButton)
36 {
37         setWindowTitle(tr("Connect to VNC Server"));
38         QSettings settings;
39
40         //read history
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
47                 }
48
49                 int position = settings.value(hostname + "/position").toInt();
50                 hosts_map.insert(position, hostname);
51         }
52         hostnames_sorted = hosts_map.values(); //sorted by ascending position
53         settings.endGroup();
54
55         //set up combobox
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);
60 #ifdef Q_WS_MAEMO_5
61         hosts.lineEdit()->setInputMethodHints(Qt::ImhNoAutoUppercase); //somehow this doesn't work that well here
62 #endif
63         connect(&hosts, SIGNAL(editTextChanged(QString)),
64                 this, SLOT(hostnameUpdated(QString)));
65         layout.addWidget(&hosts);
66
67 #ifdef Q_WS_MAEMO_5
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);
79 #else
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);
86 #endif
87
88         hostnameUpdated(hosts.lineEdit()->text()); //get saved quality for last host, or 2
89
90         done->setText(tr("Connect"));
91         done->setMaximumWidth(110);
92         connect(done, SIGNAL(clicked()),
93                 this, SLOT(accept()));
94         layout.addWidget(done);
95
96         setLayout(&layout);
97
98         connect(this, SIGNAL(finished(int)),
99                 this, SLOT(deleteLater()));
100 }
101
102 void ConnectDialog::hostnameUpdated(QString newtext)
103 {
104         const int cursorpos = hosts.lineEdit()->cursorPosition();
105
106         const bool normal_entry = hosts.itemIcon(hosts.currentIndex()).isNull();
107         done->setText(normal_entry ? tr("Connect") : tr("Listen"));
108
109         //unselect 'listen ...' entry if edited
110         if(!normal_entry) {
111                 if(newtext != LISTEN_FOR_INCOMING_CONNECTIONS_STRING) {
112                         hosts.setCurrentIndex(-1);
113                 } else { 
114                         return;
115                 }
116         }
117
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);
123
124         //saved quality setting available?
125         QSettings settings;
126         int quality = settings.value(QString("hosts/%1/quality").arg(hosts.lineEdit()->text()), 2).toInt();
127         if(quality < 1 or quality > 3)
128                 quality = 2;
129 #ifdef Q_WS_MAEMO_5
130         quality_selector->setCurrentIndex(quality-1);
131 #else
132         quality_combobox.setCurrentIndex(quality-1);
133 #endif
134 }
135
136 void ConnectDialog::accept()
137 {
138         QDialog::accept();
139
140         QString selected_host = hosts.currentText();
141         if(selected_host.isEmpty()) {
142                 return;
143         }
144
145 #ifdef Q_WS_MAEMO_5
146         int quality = quality_selector->currentIndex() + 1;
147 #else
148         int quality = quality_combobox.currentIndex() + 1;
149 #endif
150
151         QSettings settings;
152         if(!hosts.itemIcon(hosts.currentIndex()).isNull()) {
153                 int listen_port = settings.value("listen_port", LISTEN_PORT_OFFSET).toInt();
154
155 #if QT_VERSION >= 0x040500
156                 //ask user if listen_port is correct
157                 bool ok;
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
162                         1, &ok);
163 #else
164                 bool ok = true;
165 #endif
166                 if(ok) {
167                         settings.setValue("listen_port", listen_port);
168                         emit connectToHost("", quality, listen_port);
169                 }
170                 return;
171         }
172
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
177
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);
181
182                 //don't create duplicates
183                 if(used_old_host)
184                         hostnames_sorted.removeAll(selected_host);
185
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);
189         }
190
191 #ifdef Q_WS_MAEMO_5
192         settings.setValue(QString("%1/quality").arg(selected_host), quality);
193 #endif
194
195         settings.endGroup();
196         settings.sync();
197
198         emit connectToHost(QString("vnc://%1").arg(selected_host), quality, 0);
199 }