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