fix history
[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 #ifdef Q_WS_MAEMO_5
22 #include <QMaemo5ValueButton>
23 #include <QMaemo5ListPickSelector>
24 #endif
25
26 #include "connectdialog.h"
27
28 #include <iostream>
29
30
31 ConnectDialog::ConnectDialog(QWidget *parent):
32         QDialog(parent)
33 {
34         setWindowTitle(tr("Connect to VNC Server"));
35         QSettings settings;
36
37         //read history
38         settings.beginGroup("hosts");
39         QStringList hostnames = settings.childGroups();
40         QMap<int, QString> hosts_map; //use position as key
41         foreach(QString hostname, hostnames) {
42                 if(!settings.contains(hostname + "/position")) {
43                         continue; //can happen when host was given as a command line argument, don't show those
44                 }
45
46                 int position = settings.value(hostname + "/position").toInt();
47                 hosts_map.insert(position, hostname);
48         }
49         hostnames_sorted = hosts_map.values(); //sorted by ascending position
50         settings.endGroup();
51
52         //set up combobox
53         hosts.addItems(hostnames_sorted);
54         hosts.setEditable(true);
55 #ifdef Q_WS_MAEMO_5
56         hosts.lineEdit()->setInputMethodHints(Qt::ImhNoAutoUppercase); //somehow this doesn't work that well here
57 #endif
58         connect(&hosts, SIGNAL(editTextChanged(QString)),
59                 this, SLOT(hostnameUpdated(QString)));
60         layout.addWidget(&hosts);
61
62 #ifdef Q_WS_MAEMO_5
63         QMaemo5ValueButton *quality = new QMaemo5ValueButton(tr("Quality"), this);
64         quality_selector = new QMaemo5ListPickSelector(this);
65         QStandardItemModel *model = new QStandardItemModel(0, 1, this);
66         model->appendRow(new QStandardItem(tr("High\t\t(LAN)"))); //1
67         model->appendRow(new QStandardItem(tr("Medium\t(DSL)"))); //2
68         model->appendRow(new QStandardItem(tr("Low\t\t(ISDN)"))); //3
69         quality_selector->setModel(model);
70         quality->setPickSelector(quality_selector);
71         quality->setValueLayout(QMaemo5ValueButton::ValueUnderText);
72         quality->setMaximumWidth(120);
73         layout.addWidget(quality);
74
75         hostnameUpdated(hosts.lineEdit()->text()); //get saved quality for last host, or 2
76 #endif
77
78         QPushButton *done = new QPushButton(tr("Connect"));
79         done->setMaximumWidth(110);
80         connect(done, SIGNAL(clicked()),
81                 this, SLOT(accept()));
82         layout.addWidget(done);
83
84         setLayout(&layout);
85 }
86
87 void ConnectDialog::hostnameUpdated(QString newtext)
88 {
89         //clean up hostname
90         newtext.remove(QChar('/'));
91         newtext.remove(QChar('\\'));
92         int cursorpos = hosts.lineEdit()->cursorPosition();
93         hosts.lineEdit()->setText(newtext.toLower());
94         hosts.lineEdit()->setCursorPosition(cursorpos);
95
96 #ifdef Q_WS_MAEMO_5
97         //saved quality setting available?
98         QSettings settings;
99         int quality = settings.value(QString("hosts/%1/quality").arg(hosts.lineEdit()->text()), 2).toInt();
100         if(quality < 1 or quality > 3)
101                 quality = 2;
102         quality_selector->setCurrentIndex(quality-1);
103 #endif
104 }
105
106 void ConnectDialog::accept()
107 {
108         QDialog::accept();
109
110         QString selected_host = hosts.currentText();
111         if(selected_host.isEmpty()) {
112                 deleteLater();
113                 return;
114         }
115
116         QSettings settings;
117         settings.beginGroup("hosts");
118         bool new_item = !hostnames_sorted.contains(selected_host);
119         bool used_old_host = !new_item and hosts.currentIndex() > 0;
120         //if both are false, we don't need to mess with positions
121
122         if(new_item or used_old_host) {
123                 //put selected_host at the top
124                 settings.setValue(QString("%1/position").arg(selected_host), 0);
125
126                 //don't create duplicates
127                 if(used_old_host)
128                         hostnames_sorted.removeAll(selected_host);
129
130                 //now rebuild list for positions >= 1
131                 for(int i = 0; i < hostnames_sorted.size(); i++)
132                         settings.setValue(QString("%1/position").arg(hostnames_sorted.at(i)), i+1);
133         }
134
135 #ifdef Q_WS_MAEMO_5
136         int quality = quality_selector->currentIndex() + 1;
137         settings.setValue(QString("%1/quality").arg(selected_host), quality);
138 #else
139         int quality = 2;
140 #endif
141
142         settings.endGroup();
143         settings.sync();
144
145         emit connectToHost(QString("vnc://%1").arg(selected_host), quality);
146         deleteLater();
147 }