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 int position = settings.value(hostname + "/position").toInt();
40 else if(position >= hostnames.size())
41 position = hostnames.size()-1;
43 hostnames_sorted.replace(position, hostname);
48 hosts.addItems(hostnames_sorted);
49 hosts.setEditable(true);
50 hosts.lineEdit()->setInputMethodHints(Qt::ImhLowercaseOnly); //doesn't work, but I tried.
51 connect(&hosts, SIGNAL(editTextChanged(QString)),
52 this, SLOT(cleanHostname(QString)));
53 layout.addWidget(&hosts);
55 QPushButton *done = new QPushButton(tr("Done"));
56 done->setMaximumWidth(100);
57 connect(done, SIGNAL(clicked()),
58 this, SLOT(accept()));
59 layout.addWidget(done);
64 void ConnectDialog::cleanHostname(QString newtext)
66 newtext.remove(QChar('/'));
67 newtext.remove(QChar('\\'));
68 hosts.lineEdit()->setText(newtext.toLower());
71 void ConnectDialog::accept()
75 if(hosts.currentText().isEmpty()) {
82 settings.beginGroup("hosts");
83 bool new_item = hosts.itemText(hosts.currentIndex()) != hosts.currentText();
84 bool used_old_host = !new_item and hosts.currentIndex() > 0;
85 int rearrange_up_to_pos;
87 std::cout << "adding new item to history\n";
88 rearrange_up_to_pos = hosts.count(); //use free index
89 } else if(used_old_host) {
90 rearrange_up_to_pos = hosts.currentIndex();
93 if(new_item or used_old_host) {
94 std::cout << "rearranging history, last index " << rearrange_up_to_pos << "\n";
96 QStringList hostnames = settings.childGroups();
97 foreach(QString hostname, hostnames) {
98 int position = settings.value(hostname + "/position").toInt();
99 if(position < rearrange_up_to_pos)
100 settings.setValue(hostname + "/position", position+1);
102 //position 0 is now free
104 //move selected host to front
105 settings.setValue(QString("%1/position").arg(hosts.currentText()), 0);
109 emit connectToHost(QString("vnc://") + hosts.currentText());