Added confirmation dialog to automatic location update feature.
[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 QString SETTINGS_AUTOMATIC_UPDATE_ENABLED = "SETTINGS_AUTOMATIC_UPDATE_ENABLED";
34 const QString SETTINGS_AUTOMATIC_UPDATE_INTERVAL = "SETTINGS_AUTOMATIC_UPDATE_INTERVAL";
35 const int LIST_MINUTES_STEP = 5;
36 const int LIST_MINUTES_MAX = 60;
37 const int LIST_HOURS_MAX = 1;
38
39 SettingsDialog::SettingsDialog(QWidget *parent)
40     : QDialog(parent),
41       m_automaticLocationUpdateOldValue(false),
42       m_automaticLocationUpdateIntervalOldValue(QTime(0, LIST_MINUTES_STEP))
43 {
44     qDebug() << __PRETTY_FUNCTION__;
45     setWindowTitle(tr("Settings"));
46
47     QScrollArea *scrollArea = new QScrollArea(this);
48     QGridLayout *gridLayout = new QGridLayout(this);
49     QGroupBox *groupBox = new QGroupBox(scrollArea);
50
51     m_automaticLocationUpdate = new QCheckBox(tr("Use automatic location update"));
52
53     QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical);
54     QPushButton *saveButton = buttonBox->addButton(QDialogButtonBox::Save);
55     QPushButton *cancelButton = buttonBox->addButton(QDialogButtonBox::Cancel);
56
57 #ifdef Q_WS_MAEMO_5
58     m_automaticLocationUpdateIntervalButton = new QMaemo5ValueButton(tr("Update interval"), this);
59     m_automaticLocationUpdateIntervalButton->setDisabled(true);
60     m_timePick = new QMaemo5ListPickSelector;
61     m_automaticLocationUpdateIntervalButton->setPickSelector(m_timePick);
62     m_automaticLocationUpdateIntervalButton->setValueLayout(QMaemo5ValueButton::ValueBesideText);
63     QStandardItemModel *updateIntervalListModel = new QStandardItemModel(0, 1, this);
64     populateUpdateIntervalList(updateIntervalListModel);
65     m_timePick->setModel(updateIntervalListModel);
66     m_automaticLocationUpdateIntervalButton->setValueText(
67             updateIntervalListModel->item(0, 0)->text());
68     Q_UNUSED(cancelButton);
69 #else
70     m_automaticLocationUpdateInterval = new QTimeEdit();
71     m_automaticLocationUpdateInterval->setTimeRange(QTime(0, LIST_MINUTES_STEP),
72                                                     QTime(LIST_HOURS_MAX, 0));
73     m_automaticLocationUpdateInterval->setDisplayFormat("hh:mm");
74     m_automaticLocationUpdateInterval->setDisabled(true);
75
76     connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
77 #endif
78
79     connect(m_automaticLocationUpdate, SIGNAL(toggled(bool)),
80             this, SLOT(toggleAutomaticLocationUpdate(bool)));
81     connect(saveButton, SIGNAL(clicked()), this, SLOT(saveValues()));
82     connect(this, SIGNAL(rejected()), this, SLOT(rejectValues()));
83
84     QFormLayout *form = new QFormLayout();
85     form->setRowWrapPolicy(QFormLayout::WrapAllRows);
86     form->addWidget(m_automaticLocationUpdate);
87
88 #ifdef Q_WS_MAEMO_5
89     form->addWidget(m_automaticLocationUpdateIntervalButton);
90 #else
91     form->addRow(tr("Update interval"), m_automaticLocationUpdateInterval);
92 #endif
93
94     groupBox->setLayout(form);
95     scrollArea->setWidget(groupBox);
96     scrollArea->setWidgetResizable(true);
97     gridLayout->addWidget(scrollArea, 0, 0, 2, 1);
98     gridLayout->addWidget(buttonBox, 0, 1, 1, 1);
99     setLayout(gridLayout);
100
101     scrollArea->show();
102
103     readSettings();
104 }
105
106 SettingsDialog::~SettingsDialog()
107 {
108     qDebug() << __PRETTY_FUNCTION__;
109
110     QSettings settings(DIRECTORY_NAME, FILE_NAME);
111     settings.setValue(SETTINGS_AUTOMATIC_UPDATE_ENABLED, m_automaticLocationUpdate->isChecked());
112     settings.setValue(SETTINGS_AUTOMATIC_UPDATE_INTERVAL, time());
113 }
114
115 void SettingsDialog::setAutomaticLocationUpdateSettings(bool checked)
116 {
117     qDebug() << __PRETTY_FUNCTION__;
118
119     m_automaticLocationUpdate->setChecked(checked);
120 }
121
122 void SettingsDialog::enableSituareSettings(bool enabled)
123 {
124     qDebug() << __PRETTY_FUNCTION__;
125
126     m_automaticLocationUpdate->setEnabled(enabled);
127 }
128
129 void SettingsDialog::readSettings()
130 {
131     qWarning() << __PRETTY_FUNCTION__;
132
133     QSettings settings(DIRECTORY_NAME, FILE_NAME);
134     bool automaticUpdateEnabled = settings.value(SETTINGS_AUTOMATIC_UPDATE_ENABLED, false).toBool();
135     QString automaticUpdateInterval = settings.value(SETTINGS_AUTOMATIC_UPDATE_INTERVAL, "")
136                                       .toString();
137
138     m_automaticLocationUpdate->setChecked(automaticUpdateEnabled);
139
140     if (!automaticUpdateInterval.isEmpty()) {
141         qWarning() << automaticUpdateInterval;
142         setTime(QTime::fromString(automaticUpdateInterval, "hh:mm:ss"));
143         qWarning() << time().toString("hh:mm:ss");
144     }
145
146     if (automaticUpdateEnabled)
147         QTimer::singleShot(0, this, SLOT(saveValues()));
148 }
149
150 void SettingsDialog::populateUpdateIntervalList(QStandardItemModel *model)
151 {
152     qDebug() << __PRETTY_FUNCTION__;
153
154     for (int i = LIST_MINUTES_STEP; i <= LIST_MINUTES_MAX; i+=LIST_MINUTES_STEP) {
155         QStandardItem *item = new QStandardItem(QString(tr("%1 min")).arg(i));
156         item->setTextAlignment(Qt::AlignCenter);
157         item->setEditable(false);
158         model->appendRow(item);
159     }
160 }
161
162 void SettingsDialog::rejectValues()
163 {
164     qDebug() << __PRETTY_FUNCTION__;
165
166     m_automaticLocationUpdate->setChecked(m_automaticLocationUpdateOldValue);
167     setTime(m_automaticLocationUpdateIntervalOldValue);
168 }
169
170 void SettingsDialog::saveValues()
171 {
172     qDebug() << __PRETTY_FUNCTION__;
173
174     m_automaticLocationUpdateOldValue = m_automaticLocationUpdate->isChecked();
175     m_automaticLocationUpdateIntervalOldValue = time();
176
177     if (m_automaticLocationUpdate->isChecked()) {
178         QTime emptyTime = QTime();
179         emit enableAutomaticLocationUpdate(true, emptyTime.msecsTo(time()));
180     }
181     else {
182         emit enableAutomaticLocationUpdate(false);
183     }
184     accept();
185 }
186
187 void SettingsDialog::setTime(const QTime &time)
188 {
189     qDebug() << __PRETTY_FUNCTION__;
190
191 #ifdef Q_WS_MAEMO_5
192         int index = time.minute()/LIST_MINUTES_STEP - 1;
193         m_timePick->setCurrentIndex(index);
194 #else
195         m_automaticLocationUpdateInterval->setTime(time);
196 #endif
197 }
198
199 QTime SettingsDialog::time()
200 {
201     qDebug() << __PRETTY_FUNCTION__;
202
203     QTime time = QTime();
204
205 #ifdef Q_WS_MAEMO_5
206     time = time.addSecs((m_timePick->currentIndex()+1)*LIST_MINUTES_STEP*60);
207 #else
208     time = m_automaticLocationUpdateInterval->time();
209 #endif
210
211     return time;
212 }
213
214 void SettingsDialog::toggleAutomaticLocationUpdate(bool enabled)
215 {
216     qDebug() << __PRETTY_FUNCTION__;
217
218 #ifdef Q_WS_MAEMO_5
219     m_automaticLocationUpdateIntervalButton->setEnabled(enabled);
220 #else
221     m_automaticLocationUpdateInterval->setEnabled(enabled);
222 #endif
223 }