cursor rectangle was 1px too big
[presencevnc] / src / preferences.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 "preferences.h"
27
28 #include <iostream>
29
30 void migrateConfiguration()
31 {
32         QSettings settings;
33         int config_ver = settings.value("config_version", 0).toInt();
34         const int current_ver = 2;
35         if(config_ver == current_ver) //config file up-to-date
36                 return;
37         if(config_ver > current_ver) {
38                 std::cout << "Warning: Config file was created by a newer version of Presence VNC. This may break things.\n";
39                 return;
40         }
41         
42         if(settings.allKeys().isEmpty()) { //no config file
43                 settings.setValue("config_version", current_ver);
44                 settings.sync();
45                 return;
46         }
47
48         std::cout << "Migrating from configuration ver. " << config_ver << "\n";
49
50         if(config_ver == 0) {
51                 int left_zoom = settings.value("left_zoom", 0).toInt();
52                 int right_zoom = settings.value("left_zoom", 0).toInt();
53                 if(left_zoom >= 2)
54                         settings.setValue("left_zoom", left_zoom+1);
55                 if(right_zoom >= 2)
56                         settings.setValue("left_zoom", right_zoom+1);
57                 config_ver = 1;
58         }
59         if(config_ver == 1) {
60                 QString last_hostname = settings.value("last_hostname", "").toString();
61                 settings.remove("last_hostname");
62                 if(!last_hostname.isEmpty()) {
63                         //make sure hostname is sane
64                         last_hostname.remove(QChar('/'));
65                         last_hostname.remove(QChar('\\'));
66                         last_hostname = last_hostname.toLower();
67
68                         settings.setValue(QString("hosts/%1/position").arg(last_hostname), 0);
69                 }
70                 
71                 config_ver = 2;
72         }
73         Q_ASSERT(config_ver == current_ver);
74         settings.setValue("config_version", config_ver);
75         settings.sync();
76 }
77
78
79 Preferences::Preferences(QWidget *parent):
80         QDialog(parent)
81 {
82         setWindowTitle(tr("Preferences"));
83
84         QHBoxLayout *layout1 = new QHBoxLayout();
85         QVBoxLayout *layout2 = new QVBoxLayout();
86
87 #ifdef Q_WS_MAEMO_5
88         QMaemo5ValueButton *rotation = new QMaemo5ValueButton(tr("Screen Rotation"), this);
89         rotation_selector = new QMaemo5ListPickSelector(this);
90         QStandardItemModel *model = new QStandardItemModel(0, 1, this);
91         model->appendRow(new QStandardItem(tr("Automatic")));
92         model->appendRow(new QStandardItem(tr("Landscape")));
93         model->appendRow(new QStandardItem(tr("Portrait")));
94         rotation_selector->setModel(model);
95         rotation_selector->setCurrentIndex(settings.value("screen_rotation", 0).toInt());
96         rotation->setPickSelector(rotation_selector);
97         rotation->setValueLayout(QMaemo5ValueButton::ValueBesideText);
98         layout2->addWidget(rotation);
99
100         QMaemo5ValueButton *leftzoom = new QMaemo5ValueButton(tr("Left Zoom Button"), this);
101         leftzoom_selector = new QMaemo5ListPickSelector(this);
102         QStandardItemModel *key_model = new QStandardItemModel(0, 1, this);
103         key_model->insertRow(0, new QStandardItem(tr("Left Click")));
104         key_model->insertRow(1, new QStandardItem(tr("Right Click")));
105         key_model->insertRow(2, new QStandardItem(tr("Middle Click")));
106         key_model->insertRow(3, new QStandardItem(tr("Wheel Up")));
107         key_model->insertRow(4, new QStandardItem(tr("Wheel Down")));
108         key_model->insertRow(5, new QStandardItem(tr("Page Up")));
109         key_model->insertRow(6, new QStandardItem(tr("Page Down")));
110         leftzoom_selector->setModel(key_model);
111         leftzoom_selector->setCurrentIndex(settings.value("left_zoom", 0).toInt());
112         leftzoom->setPickSelector(leftzoom_selector);
113         leftzoom->setValueLayout(QMaemo5ValueButton::ValueBesideText);
114         layout2->addWidget(leftzoom);
115
116         QMaemo5ValueButton *rightzoom = new QMaemo5ValueButton(tr("Right Zoom Button"), this);
117         rightzoom_selector = new QMaemo5ListPickSelector(this);
118         rightzoom_selector->setModel(key_model);
119         rightzoom_selector->setCurrentIndex(settings.value("right_zoom", 1).toInt());
120         rightzoom->setPickSelector(rightzoom_selector);
121         rightzoom->setValueLayout(QMaemo5ValueButton::ValueBesideText);
122         layout2->addWidget(rightzoom);
123
124         //useful if one only wants to click using the zoom buttons exclusively
125         disable_tapping = new QCheckBox(tr("Disable Tapping"), this);
126         disable_tapping->setChecked(settings.value("disable_tapping", false).toBool());
127         layout2->addWidget(disable_tapping);
128 #endif
129
130         always_show_local_cursor = new QCheckBox(tr("Always show local cursor"), this);
131         always_show_local_cursor->setChecked(settings.value("always_show_local_cursor", false).toBool());
132         layout2->addWidget(always_show_local_cursor);
133
134         QPushButton *ok = new QPushButton(tr("Done"));
135         ok->setMaximumWidth(100);
136
137         layout1->addLayout(layout2);
138         layout1->addWidget(ok);
139
140         setLayout(layout1);
141
142         connect(ok, SIGNAL(clicked()),
143                 this, SLOT(accept()));
144         connect(this, SIGNAL(accepted()),
145                 this, SLOT(save()));
146 }
147
148 void Preferences::save()
149 {
150 #ifdef Q_WS_MAEMO_5
151         settings.setValue("screen_rotation", rotation_selector->currentIndex());
152         settings.setValue("left_zoom", leftzoom_selector->currentIndex());
153         settings.setValue("right_zoom", rightzoom_selector->currentIndex());
154         settings.setValue("disable_tapping", disable_tapping->isChecked());
155 #endif
156         settings.setValue("always_show_local_cursor", always_show_local_cursor->isChecked());
157
158         settings.sync();
159 }