change history format; fix screen tearing
[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                 int position = settings.value(hostname + "/position").toInt();
38                 if(position < 0)
39                         position = 0;
40                 else if(position >= hostnames.size())
41                         position = hostnames.size()-1;
42
43                 hostnames_sorted.replace(position, hostname);
44         }
45         settings.endGroup();
46
47         //set up combobox
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);
54
55         QPushButton *done = new QPushButton(tr("Done"));
56         done->setMaximumWidth(100);
57         connect(done, SIGNAL(clicked()),
58                 this, SLOT(accept()));
59         layout.addWidget(done);
60
61         setLayout(&layout);
62 }
63
64 void ConnectDialog::cleanHostname(QString newtext)
65 {
66         newtext.remove(QChar('/'));
67         newtext.remove(QChar('\\'));
68         hosts.lineEdit()->setText(newtext.toLower());
69 }
70
71 void ConnectDialog::accept()
72 {
73         QDialog::accept();
74
75         if(hosts.currentText().isEmpty()) {
76                 deleteLater();
77                 return;
78         }
79
80         //save url?
81         QSettings settings;
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;
86         if(new_item) {
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();
91         }
92
93         if(new_item or used_old_host) {
94                 std::cout << "rearranging history,  last index " << rearrange_up_to_pos << "\n";
95
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);
101                 }
102                 //position 0 is now free
103
104                 //move selected host to front
105                 settings.setValue(QString("%1/position").arg(hosts.currentText()), 0);
106         }
107         settings.endGroup();
108
109         emit connectToHost(QString("vnc://") + hosts.currentText());
110         deleteLater();
111 }