Merge branch 'master' of https://vcs.maemo.org/git/situare
[situare] / src / ui / settingsdialog.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5       Katri Kaikkonen - katri.kaikkonen@ixonos.com
6
7    Situare is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License
9    version 2 as published by the Free Software Foundation.
10
11    Situare is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with Situare; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19    USA.
20 */
21
22 #ifdef Q_WS_MAEMO_5
23 #include <QMaemo5TimePickSelector>
24 #include <QMaemo5ValueButton>
25 #endif
26
27 #include <QtGui>
28 #include <QDebug>
29 #include <QTime>
30 #include "common.h"
31 #include "settingsdialog.h"
32
33 const int LIST_MINUTES_STEP = 5;
34 const int LIST_MINUTES_MAX = 60;
35 const int LIST_HOURS_MAX = 1;
36
37 SettingsDialog::SettingsDialog(QWidget *parent)
38     : QDialog(parent)
39 {
40     qDebug() << __PRETTY_FUNCTION__;
41     setWindowTitle(tr("Settings"));
42
43     QScrollArea *scrollArea = new QScrollArea(this);
44     QGridLayout *gridLayout = new QGridLayout(this);
45     QGroupBox *groupBox = new QGroupBox(scrollArea);
46
47     m_automaticLocationUpdate = new QCheckBox(tr("Use automatic location update"));
48
49     QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical);
50     QPushButton *saveButton = buttonBox->addButton(QDialogButtonBox::Save);
51     QPushButton *cancelButton = buttonBox->addButton(QDialogButtonBox::Cancel);
52
53 #ifdef Q_WS_MAEMO_5
54     m_automaticLocationUpdateIntervalButton = new QMaemo5ValueButton(tr("Update interval"), this);
55     m_automaticLocationUpdateIntervalButton->setDisabled(true);
56     m_timePick = new QMaemo5ListPickSelector;
57     m_automaticLocationUpdateIntervalButton->setPickSelector(m_timePick);
58     m_automaticLocationUpdateIntervalButton->setValueLayout(QMaemo5ValueButton::ValueBesideText);
59     QStandardItemModel *updateIntervalListModel = new QStandardItemModel(0, 1, this);
60     populateUpdateIntervalList(updateIntervalListModel);
61     m_timePick->setModel(updateIntervalListModel);
62     m_automaticLocationUpdateIntervalButton->setValueText(
63             updateIntervalListModel->item(0, 0)->text());
64     Q_UNUSED(cancelButton);
65 #else
66     m_automaticLocationUpdateInterval = new QTimeEdit();
67     m_automaticLocationUpdateInterval->setTimeRange(QTime(0, LIST_MINUTES_STEP),
68                                                     QTime(LIST_HOURS_MAX, 0));
69     m_automaticLocationUpdateInterval->setDisplayFormat("hh:mm");
70     m_automaticLocationUpdateInterval->setDisabled(true);
71
72     connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
73 #endif
74
75     connect(m_automaticLocationUpdate, SIGNAL(toggled(bool)),
76             this, SLOT(toggleAutomaticLocationUpdate(bool)));
77     connect(saveButton, SIGNAL(clicked()), this, SLOT(saveValues()));
78
79     QFormLayout *form = new QFormLayout();
80     form->setRowWrapPolicy(QFormLayout::WrapAllRows);
81     form->addWidget(m_automaticLocationUpdate);
82
83 #ifdef Q_WS_MAEMO_5
84     form->addWidget(m_automaticLocationUpdateIntervalButton);
85 #else
86     form->addRow(tr("Update interval"), m_automaticLocationUpdateInterval);
87 #endif
88
89     groupBox->setLayout(form);
90     scrollArea->setWidget(groupBox);
91     scrollArea->setWidgetResizable(true);
92     gridLayout->addWidget(scrollArea, 0, 0, 2, 1);
93     gridLayout->addWidget(buttonBox, 0, 1, 1, 1);
94     setLayout(gridLayout);
95
96     scrollArea->show();
97
98     readSettings();
99 }
100
101 void SettingsDialog::emitAutomaticLocationUpdateSettings()
102 {
103     qDebug() << __PRETTY_FUNCTION__;
104
105     if (m_automaticLocationUpdate->isChecked()) {
106         QTime emptyTime = QTime();
107         emit enableAutomaticLocationUpdate(true, emptyTime.msecsTo(time()));
108     }
109     else {
110         emit enableAutomaticLocationUpdate(false);
111     }
112 }
113
114 void SettingsDialog::enableSituareSettings(bool enabled)
115 {
116     qDebug() << __PRETTY_FUNCTION__;
117
118     m_automaticLocationUpdate->setEnabled(enabled);
119
120     if (enabled)
121         toggleAutomaticLocationUpdate(m_automaticLocationUpdate->isChecked());
122     else
123         toggleAutomaticLocationUpdate(false);
124 }
125
126 void SettingsDialog::populateUpdateIntervalList(QStandardItemModel *model)
127 {
128     qDebug() << __PRETTY_FUNCTION__;
129
130     for (int i = LIST_MINUTES_STEP; i <= LIST_MINUTES_MAX; i+=LIST_MINUTES_STEP) {
131         QStandardItem *item = new QStandardItem(QString(tr("%1 min")).arg(i));
132         item->setTextAlignment(Qt::AlignCenter);
133         item->setEditable(false);
134         model->appendRow(item);
135     }
136 }
137
138 void SettingsDialog::readSettings()
139 {
140     qDebug() << __PRETTY_FUNCTION__;
141
142     QSettings settings(DIRECTORY_NAME, FILE_NAME);
143     bool automaticUpdateEnabled = settings.value(SETTINGS_AUTOMATIC_UPDATE_ENABLED, false).toBool();
144     QString automaticUpdateInterval = settings.value(SETTINGS_AUTOMATIC_UPDATE_INTERVAL, "")
145                                       .toString();
146
147     m_automaticLocationUpdate->setChecked(automaticUpdateEnabled);
148
149     if (!automaticUpdateInterval.isEmpty())
150         setTime(QTime::fromString(automaticUpdateInterval, "hh:mm:ss"));
151     else
152         setTime(QTime(0, LIST_MINUTES_STEP));
153 }
154
155 void SettingsDialog::saveValues()
156 {
157     qDebug() << __PRETTY_FUNCTION__;
158
159     QSettings settings(DIRECTORY_NAME, FILE_NAME);
160     settings.setValue(SETTINGS_AUTOMATIC_UPDATE_ENABLED, m_automaticLocationUpdate->isChecked());
161     settings.setValue(SETTINGS_AUTOMATIC_UPDATE_INTERVAL, time());
162
163     emitAutomaticLocationUpdateSettings();
164
165     accept();
166 }
167
168 void SettingsDialog::setAutomaticLocationUpdateSettings(bool checked)
169 {
170     qDebug() << __PRETTY_FUNCTION__;
171
172     m_automaticLocationUpdate->setChecked(checked);
173
174     QSettings settings(DIRECTORY_NAME, FILE_NAME);
175     settings.setValue(SETTINGS_AUTOMATIC_UPDATE_ENABLED, m_automaticLocationUpdate->isChecked());
176 }
177
178 void SettingsDialog::setTime(const QTime &time)
179 {
180     qDebug() << __PRETTY_FUNCTION__;
181
182 #ifdef Q_WS_MAEMO_5
183         // Convert time to index in list
184         int index = time.minute()/LIST_MINUTES_STEP - 1;
185
186         if (index < 0)
187             index = 0;
188         if (index >= m_timePick->model()->rowCount())
189             index = m_timePick->model()->rowCount() - 1;
190
191         m_timePick->setCurrentIndex(index);
192 #else
193         m_automaticLocationUpdateInterval->setTime(time);
194 #endif
195 }
196
197 QTime SettingsDialog::time()
198 {
199     qDebug() << __PRETTY_FUNCTION__;
200
201     QTime time = QTime();
202
203 #ifdef Q_WS_MAEMO_5
204     time = time.addSecs((m_timePick->currentIndex()+1)*LIST_MINUTES_STEP*60);
205 #else
206     time = m_automaticLocationUpdateInterval->time();
207 #endif
208
209     if (time < QTime(0, LIST_MINUTES_STEP))
210         time = QTime(0, LIST_MINUTES_STEP);
211     if (time > QTime(LIST_HOURS_MAX, 0))
212         time = QTime(LIST_HOURS_MAX, 0);
213
214     return time;
215 }
216
217 void SettingsDialog::toggleAutomaticLocationUpdate(bool enabled)
218 {
219     qDebug() << __PRETTY_FUNCTION__;
220
221 #ifdef Q_WS_MAEMO_5
222     m_automaticLocationUpdateIntervalButton->setEnabled(enabled);
223 #else
224     m_automaticLocationUpdateInterval->setEnabled(enabled);
225 #endif
226 }