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.
21 #include "connectdialog.h"
26 ConnectDialog::ConnectDialog(QWidget *parent):
29 setWindowTitle(tr("Connect to VNC Server"));
33 settings.beginGroup("hosts");
34 QStringList hostnames = settings.childGroups();
35 QStringList hostnames_sorted = hostnames;
36 foreach(QString hostname, hostnames) {
37 if(!settings.contains(hostname + "/position")) {
38 //can happen when host was given as a command line argument, don't show those
39 hostnames_sorted.removeAll(hostname);
43 int position = settings.value(hostname + "/position").toInt();
46 else if(position >= hostnames_sorted.size())
47 position = hostnames_sorted.size()-1;
49 hostnames_sorted.replace(position, hostname);
54 hosts.addItems(hostnames_sorted);
55 hosts.setEditable(true);
56 hosts.lineEdit()->setInputMethodHints(Qt::ImhLowercaseOnly); //doesn't work, but I tried.
57 connect(&hosts, SIGNAL(editTextChanged(QString)),
58 this, SLOT(cleanHostname(QString)));
59 layout.addWidget(&hosts);
61 QPushButton *done = new QPushButton(tr("Done"));
62 done->setMaximumWidth(100);
63 connect(done, SIGNAL(clicked()),
64 this, SLOT(accept()));
65 layout.addWidget(done);
70 void ConnectDialog::cleanHostname(QString newtext)
72 newtext.remove(QChar('/'));
73 newtext.remove(QChar('\\'));
74 hosts.lineEdit()->setText(newtext.toLower());
77 void ConnectDialog::accept()
81 if(hosts.currentText().isEmpty()) {
88 settings.beginGroup("hosts");
89 bool new_item = hosts.itemText(hosts.currentIndex()) != hosts.currentText();
90 bool used_old_host = !new_item and hosts.currentIndex() > 0;
91 int rearrange_up_to_pos;
93 std::cout << "adding new item to history\n";
94 rearrange_up_to_pos = hosts.count(); //use free index
95 } else if(used_old_host) {
96 rearrange_up_to_pos = hosts.currentIndex();
99 if(new_item or used_old_host) {
100 std::cout << "rearranging history, last index " << rearrange_up_to_pos << "\n";
102 QStringList hostnames = settings.childGroups();
103 foreach(QString hostname, hostnames) {
104 if(!settings.contains(hostname + "/position"))
105 continue; //ignore entries without position
107 int position = settings.value(hostname + "/position").toInt();
108 if(position < rearrange_up_to_pos)
109 settings.setValue(hostname + "/position", position+1);
111 //position 0 is now free
113 //move selected host to front
114 settings.setValue(QString("%1/position").arg(hosts.currentText()), 0);
118 emit connectToHost(QString("vnc://") + hosts.currentText());