Added debug prints to SettingsDialog.
[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     setTime(QTime(0, LIST_MINUTES_STEP));
104     readSettings();
105 }
106
107 SettingsDialog::~SettingsDialog()
108 {
109     qDebug() << __PRETTY_FUNCTION__;
110
111     QSettings settings(DIRECTORY_NAME, FILE_NAME);
112     settings.setValue(SETTINGS_AUTOMATIC_UPDATE_ENABLED, m_automaticLocationUpdate->isChecked());
113     settings.setValue(SETTINGS_AUTOMATIC_UPDATE_INTERVAL, time());
114 }
115
116 void SettingsDialog::setAutomaticLocationUpdateSettings(bool checked)
117 {
118     qWarning() << __PRETTY_FUNCTION__;
119
120     m_automaticLocationUpdate->setChecked(checked);
121     m_automaticLocationUpdateOldValue = checked;
122 }
123
124 void SettingsDialog::enableSituareSettings(bool enabled)
125 {
126     qDebug() << __PRETTY_FUNCTION__;
127
128     m_automaticLocationUpdate->setEnabled(enabled);
129 }
130
131 void SettingsDialog::readSettings()
132 {
133     qWarning() << __PRETTY_FUNCTION__;
134
135     QSettings settings(DIRECTORY_NAME, FILE_NAME);
136     bool automaticUpdateEnabled = settings.value(SETTINGS_AUTOMATIC_UPDATE_ENABLED, false).toBool();
137     QString automaticUpdateInterval = settings.value(SETTINGS_AUTOMATIC_UPDATE_INTERVAL, "")
138                                       .toString();
139
140     m_automaticLocationUpdate->setChecked(automaticUpdateEnabled);
141
142     if (!automaticUpdateInterval.isEmpty())
143         setTime(QTime::fromString(automaticUpdateInterval, "hh:mm:ss"));
144 }
145
146 void SettingsDialog::emitAutomaticLocationUpdateSettings()
147 {
148     qWarning() << __PRETTY_FUNCTION__;
149
150     if (m_automaticLocationUpdate->isChecked()) {
151         QTime emptyTime = QTime();
152         qDebug() << emptyTime.msecsTo(time());
153         emit enableAutomaticLocationUpdate(true, emptyTime.msecsTo(time()));
154     }
155     else {
156         emit enableAutomaticLocationUpdate(false);
157     }
158 }
159
160 void SettingsDialog::populateUpdateIntervalList(QStandardItemModel *model)
161 {
162     qDebug() << __PRETTY_FUNCTION__;
163
164     for (int i = LIST_MINUTES_STEP; i <= LIST_MINUTES_MAX; i+=LIST_MINUTES_STEP) {
165         QStandardItem *item = new QStandardItem(QString(tr("%1 min")).arg(i));
166         item->setTextAlignment(Qt::AlignCenter);
167         item->setEditable(false);
168         model->appendRow(item);
169     }
170 }
171
172 void SettingsDialog::rejectValues()
173 {
174     qDebug() << __PRETTY_FUNCTION__;
175
176     m_automaticLocationUpdate->setChecked(m_automaticLocationUpdateOldValue);
177     setTime(m_automaticLocationUpdateIntervalOldValue);
178 }
179
180 void SettingsDialog::saveValues()
181 {
182     qDebug() << __PRETTY_FUNCTION__;
183
184     m_automaticLocationUpdateOldValue = m_automaticLocationUpdate->isChecked();
185     m_automaticLocationUpdateIntervalOldValue = time();
186
187     emitAutomaticLocationUpdateSettings();
188
189     accept();
190 }
191
192 void SettingsDialog::setTime(const QTime &time)
193 {
194     qDebug() << __PRETTY_FUNCTION__;
195
196 #ifdef Q_WS_MAEMO_5
197         int index = time.minute()/LIST_MINUTES_STEP - 1;
198         m_timePick->setCurrentIndex(index);
199 #else
200         m_automaticLocationUpdateInterval->setTime(time);
201 #endif
202 }
203
204 QTime SettingsDialog::time()
205 {
206     qDebug() << __PRETTY_FUNCTION__;
207
208     QTime time = QTime();
209
210 #ifdef Q_WS_MAEMO_5
211     time = time.addSecs((m_timePick->currentIndex()+1)*LIST_MINUTES_STEP*60);
212 #else
213     time = m_automaticLocationUpdateInterval->time();
214 #endif
215
216     return time;
217 }
218
219 void SettingsDialog::toggleAutomaticLocationUpdate(bool enabled)
220 {
221     qDebug() << __PRETTY_FUNCTION__;
222
223 #ifdef Q_WS_MAEMO_5
224     m_automaticLocationUpdateIntervalButton->setEnabled(enabled);
225 #else
226     m_automaticLocationUpdateInterval->setEnabled(enabled);
227 #endif
228 }