Update web page
[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
26 const QString LISTEN_FOR_INCOMING_CONNECTIONS_STRING = QObject::tr("Listen for incoming connections");
27
28 ConnectDialog::ConnectDialog(QWidget *parent):
29     QDialog(parent)
30 {
31     setWindowTitle(tr("Connect to VNC Server"));
32     QSettings settings;
33
34     //read history
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
41         }
42
43         int position = settings.value(hostname + "/position").toInt();
44         hosts_map.insert(position, hostname);
45     }
46     hostnames_sorted = hosts_map.values(); //sorted by ascending position
47     settings.endGroup();
48
49     QHBoxLayout *layout = new QHBoxLayout(this);
50     QGridLayout *grid_layout = new QGridLayout();
51     layout->addLayout(grid_layout);
52
53     //set up combobox
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);
58 #ifdef Q_WS_MAEMO_5
59     hosts.lineEdit()->setInputMethodHints(Qt::ImhNoAutoUppercase); //somehow this doesn't work that well here
60 #endif
61     connect(&hosts, SIGNAL(editTextChanged(QString)),
62             this, SLOT(hostnameUpdated(QString)));
63     grid_layout->addWidget(&hosts, 0, 0, 1, 2);
64
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);
71
72     viewonly.setText(tr("View only"));
73     grid_layout->addWidget(&viewonly, 2, 0, 1, 2);
74
75     done.setMaximumWidth(110);
76     connect(&done, SIGNAL(clicked()),
77             this, SLOT(accept()));
78     layout->addWidget(&done);
79
80     //get settings for last host
81     hostnameUpdated(hosts.lineEdit()->text());
82
83     connect(this, SIGNAL(finished(int)),
84             this, SLOT(deleteLater()));
85 }
86
87 void ConnectDialog::hostnameUpdated(QString newtext)
88 {
89     const int cursorpos = hosts.lineEdit()->cursorPosition();
90
91     const bool normal_entry = hosts.itemIcon(hosts.currentIndex()).isNull();
92     done.setText(normal_entry ? tr("Connect") : tr("Listen"));
93
94     //unselect 'listen ...' entry if edited
95     if(!normal_entry) {
96         if(newtext != LISTEN_FOR_INCOMING_CONNECTIONS_STRING) {
97             hosts.setCurrentIndex(-1);
98         } else {
99             return;
100         }
101     }
102
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);
108
109     //saved quality setting available?
110     QSettings settings;
111     int quality = settings.value(QString("hosts/%1/quality").arg(hosts.lineEdit()->text()), 2).toInt();
112     if(quality < 1 or quality > 3)
113         quality = 2;
114     quality_combobox.setCurrentIndex(quality-1);
115
116     //saved viewonly setting?
117     viewonly.setChecked(settings.value(QString("hosts/%1/viewonly").arg(hosts.lineEdit()->text()), false).toBool());
118 }
119
120 void ConnectDialog::accept()
121 {
122     QDialog::accept();
123
124     QString selected_host = hosts.currentText();
125     if(selected_host.isEmpty()) {
126         return;
127     }
128
129     int quality = quality_combobox.currentIndex() + 1;
130
131     QSettings settings;
132     if(!hosts.itemIcon(hosts.currentIndex()).isNull()) {
133         //listen mode selected
134         int listen_port = settings.value("listen_port", LISTEN_PORT_OFFSET).toInt();
135
136         //ask user if listen_port is correct
137         bool ok;
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
142                                            1, &ok);
143
144         if(ok) {
145             settings.setValue("listen_port", listen_port);
146             emit connectToHost("", quality, listen_port, viewonly.isChecked());
147         }
148         return;
149     }
150
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
155
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);
159
160         //don't create duplicates
161         if(used_old_host)
162             hostnames_sorted.removeAll(selected_host);
163
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);
167     }
168
169     settings.setValue(QString("%1/quality").arg(selected_host), quality);
170     settings.setValue(QString("%1/viewonly").arg(selected_host), viewonly.isChecked());
171
172     settings.endGroup();
173     settings.sync();
174
175     //listen_port = 0 is ignored
176     emit connectToHost(QString("vnc://%1").arg(selected_host), quality, 0, viewonly.isChecked());
177 }