fix memory leak
[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
29 ConnectDialog::ConnectDialog(QWidget *parent):
30         QDialog(parent)
31 {
32         setWindowTitle(tr("Connect to VNC Server"));
33         QSettings settings;
34
35         //read history
36         settings.beginGroup("hosts");
37         QStringList hostnames = settings.childGroups();
38         QMap<int, QString> hosts_map; //use position as key
39         foreach(QString hostname, hostnames) {
40                 if(!settings.contains(hostname + "/position")) {
41                         continue; //can happen when host was given as a command line argument, don't show those
42                 }
43
44                 int position = settings.value(hostname + "/position").toInt();
45                 hosts_map.insert(position, hostname);
46         }
47         hostnames_sorted = hosts_map.values(); //sorted by ascending position
48         settings.endGroup();
49
50         //set up combobox
51         hosts.addItems(hostnames_sorted);
52         hosts.insertSeparator(hosts.count());
53         hosts.addItem(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_received.png"), tr("Listen for Incoming Connections"));
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         connect(&hosts, SIGNAL(currentIndexChanged(int)),
61                 this, SLOT(indexChanged(int)));
62         layout.addWidget(&hosts);
63
64 #ifdef Q_WS_MAEMO_5
65         QMaemo5ValueButton *quality = new QMaemo5ValueButton(tr("Quality"), this);
66         quality_selector = new QMaemo5ListPickSelector(this);
67         QStandardItemModel *model = new QStandardItemModel(0, 1, this);
68         model->appendRow(new QStandardItem(tr("High\t\t(LAN)"))); //1
69         model->appendRow(new QStandardItem(tr("Medium\t(DSL)"))); //2
70         model->appendRow(new QStandardItem(tr("Low\t\t(ISDN)"))); //3
71         quality_selector->setModel(model);
72         quality->setPickSelector(quality_selector);
73         quality->setValueLayout(QMaemo5ValueButton::ValueUnderText);
74         quality->setMaximumWidth(120);
75         layout.addWidget(quality);
76
77         hostnameUpdated(hosts.lineEdit()->text()); //get saved quality for last host, or 2
78 #endif
79
80         QPushButton *done = new QPushButton(tr("Connect"));
81         done->setMaximumWidth(110);
82         connect(done, SIGNAL(clicked()),
83                 this, SLOT(accept()));
84         layout.addWidget(done);
85
86         setLayout(&layout);
87
88         connect(this, SIGNAL(finished(int)),
89                 this, SLOT(deleteLater()));
90 }
91
92 void ConnectDialog::indexChanged(int index) {
93         if(index == -1)
94                 return;
95
96         //disallow editing for special entries (icon set)
97         hosts.setEditable(hosts.itemIcon(index).isNull());
98 }
99
100
101 void ConnectDialog::hostnameUpdated(QString newtext)
102 {
103         //clean up hostname
104         newtext.remove(QChar('/'));
105         newtext.remove(QChar('\\'));
106         int cursorpos = hosts.lineEdit()->cursorPosition();
107         hosts.lineEdit()->setText(newtext.toLower());
108         hosts.lineEdit()->setCursorPosition(cursorpos);
109
110 #ifdef Q_WS_MAEMO_5
111         //saved quality setting available?
112         QSettings settings;
113         int quality = settings.value(QString("hosts/%1/quality").arg(hosts.lineEdit()->text()), 2).toInt();
114         if(quality < 1 or quality > 3)
115                 quality = 2;
116         quality_selector->setCurrentIndex(quality-1);
117 #endif
118 }
119
120 void ConnectDialog::accept()
121 {
122         QDialog::accept();
123
124         QString selected_host = hosts.currentText();
125         if(selected_host.isEmpty()) {
126                 return;
127         }
128         if(!hosts.itemIcon(hosts.currentIndex()).isNull()) {
129                 emit connectToHost("", 2, 5900); //TODO: quality and port from user input
130                 return;
131         }
132
133         QSettings settings;
134         settings.beginGroup("hosts");
135         bool new_item = !hostnames_sorted.contains(selected_host);
136         bool used_old_host = !new_item and hosts.currentIndex() > 0;
137         //if both are false, we don't need to mess with positions
138
139         if(new_item or used_old_host) {
140                 //put selected_host at the top
141                 settings.setValue(QString("%1/position").arg(selected_host), 0);
142
143                 //don't create duplicates
144                 if(used_old_host)
145                         hostnames_sorted.removeAll(selected_host);
146
147                 //now rebuild list for positions >= 1
148                 for(int i = 0; i < hostnames_sorted.size(); i++)
149                         settings.setValue(QString("%1/position").arg(hostnames_sorted.at(i)), i+1);
150         }
151
152 #ifdef Q_WS_MAEMO_5
153         int quality = quality_selector->currentIndex() + 1;
154         settings.setValue(QString("%1/quality").arg(selected_host), quality);
155 #else
156         int quality = 2;
157 #endif
158
159         settings.endGroup();
160         settings.sync();
161
162         emit connectToHost(QString("vnc://%1").arg(selected_host), quality, 0);
163 }