55c7bf52cd5ab58fe6f3dd8e76b4312267aeeaca
[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         QStringList hostnames_sorted = hostnames;
41         foreach(QString hostname, hostnames) {
42                 if(!settings.contains(hostname + "/position")) {
43                         //can happen when host was given as a command line argument, don't show those
44                         hostnames_sorted.removeAll(hostname);
45                         continue;
46                 }
47
48                 int position = settings.value(hostname + "/position").toInt();
49                 if(position < 0)
50                         position = 0;
51                 else if(position >= hostnames_sorted.size())
52                         position = hostnames_sorted.size()-1;
53
54                 hostnames_sorted.replace(position, hostname);
55         }
56         settings.endGroup();
57
58         //set up combobox
59         hosts.addItems(hostnames_sorted);
60         hosts.setEditable(true);
61 #ifdef Q_WS_MAEMO_5
62         hosts.lineEdit()->setInputMethodHints(Qt::ImhNoAutoUppercase); //somehow this doesn't work that well here
63 #endif
64         connect(&hosts, SIGNAL(editTextChanged(QString)),
65                 this, SLOT(hostnameUpdated(QString)));
66         layout.addWidget(&hosts);
67
68 #ifdef Q_WS_MAEMO_5
69         QMaemo5ValueButton *quality = new QMaemo5ValueButton(tr("Quality"), this);
70         quality_selector = new QMaemo5ListPickSelector(this);
71         QStandardItemModel *model = new QStandardItemModel(0, 1, this);
72         model->appendRow(new QStandardItem(tr("High\t\t(LAN)"))); //1
73         model->appendRow(new QStandardItem(tr("Medium\t(DSL)"))); //2
74         model->appendRow(new QStandardItem(tr("Low\t\t(ISDN)"))); //3
75         quality_selector->setModel(model);
76         quality->setPickSelector(quality_selector);
77         quality->setValueLayout(QMaemo5ValueButton::ValueUnderText);
78         quality->setMaximumWidth(120);
79         layout.addWidget(quality);
80
81         hostnameUpdated(hosts.lineEdit()->text()); //get saved quality for last host, or 2
82 #endif
83
84         QPushButton *done = new QPushButton(tr("Connect"));
85         done->setMaximumWidth(110);
86         connect(done, SIGNAL(clicked()),
87                 this, SLOT(accept()));
88         layout.addWidget(done);
89
90         setLayout(&layout);
91 }
92
93 void ConnectDialog::hostnameUpdated(QString newtext)
94 {
95         //clean up hostname
96         newtext.remove(QChar('/'));
97         newtext.remove(QChar('\\'));
98         int cursorpos = hosts.lineEdit()->cursorPosition();
99         hosts.lineEdit()->setText(newtext.toLower());
100         hosts.lineEdit()->setCursorPosition(cursorpos);
101
102 #ifdef Q_WS_MAEMO_5
103         //saved quality setting available?
104         QSettings settings;
105         int quality = settings.value(QString("hosts/%1/quality").arg(hosts.lineEdit()->text()), 2).toInt();
106         if(quality < 1 or quality > 3)
107                 quality = 2;
108         quality_selector->setCurrentIndex(quality-1);
109 #endif
110 }
111
112 void ConnectDialog::accept()
113 {
114         QDialog::accept();
115
116         if(hosts.currentText().isEmpty()) {
117                 deleteLater();
118                 return;
119         }
120
121         //save url?
122         QSettings settings;
123         settings.beginGroup("hosts");
124         bool new_item = !settings.contains(hosts.currentText());
125         bool used_old_host = !new_item and hosts.currentIndex() > 0;
126         int rearrange_up_to_pos;
127         if(new_item) {
128                 std::cout << "adding new item to history\n";
129                 rearrange_up_to_pos = hosts.count(); //use free index
130         } else if(used_old_host) {
131                 rearrange_up_to_pos = hosts.currentIndex();
132         }
133
134         if(new_item or used_old_host) {
135                 std::cout << "rearranging history,  last index " << rearrange_up_to_pos << "\n";
136
137                 QStringList hostnames = settings.childGroups();
138                 foreach(QString hostname, hostnames) {
139                         if(!settings.contains(hostname + "/position"))
140                                 continue; //ignore entries without position
141
142                         int position = settings.value(hostname + "/position").toInt();
143                         if(position < rearrange_up_to_pos)
144                                 settings.setValue(hostname + "/position", position+1);
145                 }
146                 //position 0 is now free
147
148                 //move selected host to front
149                 settings.setValue(QString("%1/position").arg(hosts.currentText()), 0);
150         }
151 #ifdef Q_WS_MAEMO_5
152         int quality = quality_selector->currentIndex() + 1;
153         settings.setValue(QString("%1/quality").arg(hosts.currentText()), quality);
154 #else
155         int quality = 2;
156 #endif
157
158         settings.endGroup();
159         settings.sync();
160
161         emit connectToHost(QString("vnc://%1").arg(hosts.currentText()), quality);
162         deleteLater();
163 }