21df3a5ea8dc97abd3d3e038715882e52d86fa3e
[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         connect(&hosts, SIGNAL(editTextChanged(QString)),
57                 this, SLOT(cleanHostname(QString)));
58         layout.addWidget(&hosts);
59
60         QPushButton *done = new QPushButton(tr("Done"));
61         done->setMaximumWidth(100);
62         connect(done, SIGNAL(clicked()),
63                 this, SLOT(accept()));
64         layout.addWidget(done);
65
66         setLayout(&layout);
67 }
68
69 void ConnectDialog::cleanHostname(QString newtext)
70 {
71         newtext.remove(QChar('/'));
72         newtext.remove(QChar('\\'));
73         hosts.lineEdit()->setText(newtext.toLower());
74 }
75
76 void ConnectDialog::accept()
77 {
78         QDialog::accept();
79
80         if(hosts.currentText().isEmpty()) {
81                 deleteLater();
82                 return;
83         }
84
85         //save url?
86         QSettings settings;
87         settings.beginGroup("hosts");
88         bool new_item = !settings.contains(hosts.currentText());
89         bool used_old_host = !new_item and hosts.currentIndex() > 0;
90         int rearrange_up_to_pos;
91         if(new_item) {
92                 std::cout << "adding new item to history\n";
93                 rearrange_up_to_pos = hosts.count(); //use free index
94         } else if(used_old_host) {
95                 rearrange_up_to_pos = hosts.currentIndex();
96         }
97
98         if(new_item or used_old_host) {
99                 std::cout << "rearranging history,  last index " << rearrange_up_to_pos << "\n";
100
101                 QStringList hostnames = settings.childGroups();
102                 foreach(QString hostname, hostnames) {
103                         if(!settings.contains(hostname + "/position"))
104                                 continue; //ignore entries without position
105
106                         int position = settings.value(hostname + "/position").toInt();
107                         if(position < rearrange_up_to_pos)
108                                 settings.setValue(hostname + "/position", position+1);
109                 }
110                 //position 0 is now free
111
112                 //move selected host to front
113                 settings.setValue(QString("%1/position").arg(hosts.currentText()), 0);
114         }
115         settings.endGroup();
116         settings.sync();
117
118         emit connectToHost(QString("vnc://") + hosts.currentText());
119         deleteLater();
120 }