a6ebcae89f77be98b0afd221e7a2155f55db5b48
[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 #include "connectdialog.h"
22
23 #include <iostream>
24
25
26 ConnectDialog::ConnectDialog(QWidget *parent):
27         QDialog(parent)
28 {
29         setWindowTitle(tr("Connect to VNC Server"));
30         QSettings settings;
31
32         //read history
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);
40                         continue;
41                 }
42
43                 int position = settings.value(hostname + "/position").toInt();
44                 if(position < 0)
45                         position = 0;
46                 else if(position >= hostnames_sorted.size())
47                         position = hostnames_sorted.size()-1;
48
49                 hostnames_sorted.replace(position, hostname);
50         }
51         settings.endGroup();
52
53         //set up combobox
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);
60
61         QPushButton *done = new QPushButton(tr("Done"));
62         done->setMaximumWidth(100);
63         connect(done, SIGNAL(clicked()),
64                 this, SLOT(accept()));
65         layout.addWidget(done);
66
67         setLayout(&layout);
68 }
69
70 void ConnectDialog::cleanHostname(QString newtext)
71 {
72         newtext.remove(QChar('/'));
73         newtext.remove(QChar('\\'));
74         hosts.lineEdit()->setText(newtext.toLower());
75 }
76
77 void ConnectDialog::accept()
78 {
79         QDialog::accept();
80
81         if(hosts.currentText().isEmpty()) {
82                 deleteLater();
83                 return;
84         }
85
86         //save url?
87         QSettings settings;
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;
92         if(new_item) {
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();
97         }
98
99         if(new_item or used_old_host) {
100                 std::cout << "rearranging history,  last index " << rearrange_up_to_pos << "\n";
101
102                 QStringList hostnames = settings.childGroups();
103                 foreach(QString hostname, hostnames) {
104                         if(!settings.contains(hostname + "/position"))
105                                 continue; //ignore entries without position
106
107                         int position = settings.value(hostname + "/position").toInt();
108                         if(position < rearrange_up_to_pos)
109                                 settings.setValue(hostname + "/position", position+1);
110                 }
111                 //position 0 is now free
112
113                 //move selected host to front
114                 settings.setValue(QString("%1/position").arg(hosts.currentText()), 0);
115         }
116         settings.endGroup();
117
118         emit connectToHost(QString("vnc://") + hosts.currentText());
119         deleteLater();
120 }