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