Implemented NetworkCookieJar::clearCookiesSetting()
[situare] / src / ui / settingsdialog.cpp
index aaf1471..570363a 100644 (file)
    USA.
 */
 
-#include <QtGui>
+#ifdef Q_WS_MAEMO_5
+#include <QMaemo5TimePickSelector>
+#include <QMaemo5ValueButton>
+#endif
+
+#include <QCheckBox>
 #include <QDebug>
+#include <QDialogButtonBox>
+#include <QFormLayout>
+#include <QGridLayout>
+#include <QGroupBox>
+#include <QPushButton>
+#include <QScrollArea>
+#include <QSettings>
 #include <QTime>
+
 #include "common.h"
+
 #include "settingsdialog.h"
 
-const QString SETTINGS_AUTOMATIC_UPDATE_INTERVAL = "SETTINGS_AUTOMATIC_UPDATE_INTERVAL";
-const int MINIMUM_UPDATE_INTERVAL_SECS = 30;
-const int MAXIMUM_UPDATE_INTERVAL_HOURS = 1;
+const int LIST_MINUTES_STEP = 5;
+const int LIST_MINUTES_MAX = 60;
+const int LIST_HOURS_MAX = 1;
 
 SettingsDialog::SettingsDialog(QWidget *parent)
-    : QDialog(parent),
-      m_automaticLocationUpdateOldValue(false),
-      m_automaticLocationUpdateIntervalOldValue(QTime(0, 0, MINIMUM_UPDATE_INTERVAL_SECS))
+    : QDialog(parent)
 {
     qDebug() << __PRETTY_FUNCTION__;
     setWindowTitle(tr("Settings"));
+    setAttribute(Qt::WA_DeleteOnClose, true);
 
     QScrollArea *scrollArea = new QScrollArea(this);
     QGridLayout *gridLayout = new QGridLayout(this);
@@ -43,26 +56,45 @@ SettingsDialog::SettingsDialog(QWidget *parent)
 
     m_automaticLocationUpdate = new QCheckBox(tr("Use automatic location update"));
 
+    QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical);
+    m_saveButton = buttonBox->addButton(QDialogButtonBox::Save);
+    QPushButton *cancelButton = buttonBox->addButton(QDialogButtonBox::Cancel);
+
+#ifdef Q_WS_MAEMO_5
+    m_automaticLocationUpdateIntervalButton = new QMaemo5ValueButton(tr("Update interval"), this);
+    m_automaticLocationUpdateIntervalButton->setDisabled(true);
+    m_timePick = new QMaemo5ListPickSelector;
+    m_automaticLocationUpdateIntervalButton->setPickSelector(m_timePick);
+    m_automaticLocationUpdateIntervalButton->setValueLayout(QMaemo5ValueButton::ValueBesideText);
+    QStandardItemModel *updateIntervalListModel = new QStandardItemModel(0, 1, this);
+    populateUpdateIntervalList(updateIntervalListModel);
+    m_timePick->setModel(updateIntervalListModel);
+    m_automaticLocationUpdateIntervalButton->setValueText(
+            updateIntervalListModel->item(0, 0)->text());
+    Q_UNUSED(cancelButton);
+#else
     m_automaticLocationUpdateInterval = new QTimeEdit();
-    m_automaticLocationUpdateInterval->setTimeRange(QTime(0, 0, MINIMUM_UPDATE_INTERVAL_SECS),
-                                                    QTime(MAXIMUM_UPDATE_INTERVAL_HOURS, 0));
-    m_automaticLocationUpdateInterval->setDisplayFormat("hh:mm:ss");
+    m_automaticLocationUpdateInterval->setTimeRange(QTime(0, LIST_MINUTES_STEP),
+                                                    QTime(LIST_HOURS_MAX, 0));
+    m_automaticLocationUpdateInterval->setDisplayFormat("hh:mm");
     m_automaticLocationUpdateInterval->setDisabled(true);
 
-    QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical);
-    QPushButton *saveButton = buttonBox->addButton(QDialogButtonBox::Save);
-    QPushButton *cancelButton = buttonBox->addButton(QDialogButtonBox::Cancel);
+    connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
+#endif
 
     connect(m_automaticLocationUpdate, SIGNAL(toggled(bool)),
             this, SLOT(toggleAutomaticLocationUpdate(bool)));
-    connect(saveButton, SIGNAL(clicked()), this, SLOT(saveValues()));
-    connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
-    connect(this, SIGNAL(rejected()), this, SLOT(rejectValues()));
+    connect(m_saveButton, SIGNAL(clicked()), this, SLOT(saveValues()));
 
     QFormLayout *form = new QFormLayout();
     form->setRowWrapPolicy(QFormLayout::WrapAllRows);
     form->addWidget(m_automaticLocationUpdate);
+
+#ifdef Q_WS_MAEMO_5
+    form->addWidget(m_automaticLocationUpdateIntervalButton);
+#else
     form->addRow(tr("Update interval"), m_automaticLocationUpdateInterval);
+#endif
 
     groupBox->setLayout(form);
     scrollArea->setWidget(groupBox);
@@ -73,60 +105,108 @@ SettingsDialog::SettingsDialog(QWidget *parent)
 
     scrollArea->show();
 
-    QSettings settings(DIRECTORY_NAME, FILE_NAME);
-    QString automaticUpdateInterval = settings.value(SETTINGS_AUTOMATIC_UPDATE_INTERVAL, "")
-                                      .toString();
-    if (!automaticUpdateInterval.isEmpty()) {
-        m_automaticLocationUpdateInterval->setTime(QTime::fromString(automaticUpdateInterval));
-        m_automaticLocationUpdateIntervalOldValue = m_automaticLocationUpdateInterval->time();
-    }
+    readSettings();
 }
 
-SettingsDialog::~SettingsDialog()
+void SettingsDialog::enableSituareSettings(bool enabled)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    QSettings settings(DIRECTORY_NAME, FILE_NAME);
-    settings.setValue(SETTINGS_AUTOMATIC_UPDATE_INTERVAL,
-                      m_automaticLocationUpdateInterval->time());
+    m_saveButton->setEnabled(enabled);
+    m_automaticLocationUpdate->setEnabled(enabled);
+
+    if (enabled)
+        toggleAutomaticLocationUpdate(m_automaticLocationUpdate->isChecked());
+    else
+        toggleAutomaticLocationUpdate(false);
 }
 
-void SettingsDialog::rejectValues()
+void SettingsDialog::populateUpdateIntervalList(QStandardItemModel *model)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_automaticLocationUpdate->setChecked(m_automaticLocationUpdateOldValue);
-    m_automaticLocationUpdateInterval->setTime(m_automaticLocationUpdateIntervalOldValue);
+    for (int i = LIST_MINUTES_STEP; i <= LIST_MINUTES_MAX; i+=LIST_MINUTES_STEP) {
+        QStandardItem *item = new QStandardItem(QString(tr("%1 min")).arg(i));
+        item->setTextAlignment(Qt::AlignCenter);
+        item->setEditable(false);
+        model->appendRow(item);
+    }
+}
+
+void SettingsDialog::readSettings()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    QSettings settings(SETTINGS_ORGANIZATION_NAME, SETTINGS_APPLICATION_NAME);
+    bool automaticUpdateEnabled = settings.value(SETTINGS_AUTOMATIC_UPDATE_ENABLED, false).toBool();
+    QTime automaticUpdateInterval = settings.value(SETTINGS_AUTOMATIC_UPDATE_INTERVAL, QTime())
+                                      .toTime();
+
+    m_automaticLocationUpdate->setChecked(automaticUpdateEnabled);
+
+    if (automaticUpdateInterval.isValid())
+        setTime(automaticUpdateInterval);
+    else
+        setTime(QTime(0, LIST_MINUTES_STEP));
 }
 
 void SettingsDialog::saveValues()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_automaticLocationUpdateOldValue = m_automaticLocationUpdate->isChecked();
-    m_automaticLocationUpdateIntervalOldValue = m_automaticLocationUpdateInterval->time();
+    QSettings settings(SETTINGS_ORGANIZATION_NAME, SETTINGS_APPLICATION_NAME);
+    settings.setValue(SETTINGS_AUTOMATIC_UPDATE_ENABLED, m_automaticLocationUpdate->isChecked());
+    settings.setValue(SETTINGS_AUTOMATIC_UPDATE_INTERVAL, time());
 
-    if (m_automaticLocationUpdate->isChecked()) {
-        QTime time = QTime();
-        emit enableAutomaticLocationUpdate(true, time.msecsTo(
-                m_automaticLocationUpdateInterval->time()));
-    }
-    else {
-        emit enableAutomaticLocationUpdate(false);
-    }
     accept();
 }
 
-void SettingsDialog::enableSituareSettings(bool enabled)
+void SettingsDialog::setTime(const QTime &time)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_automaticLocationUpdate->setEnabled(enabled);
+#ifdef Q_WS_MAEMO_5
+        // Convert time to index in list
+        int index = time.minute()/LIST_MINUTES_STEP - 1;
+
+        if (index < 0)
+            index = 0;
+        if (index >= m_timePick->model()->rowCount())
+            index = m_timePick->model()->rowCount() - 1;
+
+        m_timePick->setCurrentIndex(index);
+#else
+        m_automaticLocationUpdateInterval->setTime(time);
+#endif
+}
+
+QTime SettingsDialog::time()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    QTime time;
+
+#ifdef Q_WS_MAEMO_5
+    time = time.addSecs((m_timePick->currentIndex()+1)*LIST_MINUTES_STEP*60);
+#else
+    time = m_automaticLocationUpdateInterval->time();
+#endif
+
+    if (time < QTime(0, LIST_MINUTES_STEP))
+        time = QTime(0, LIST_MINUTES_STEP);
+    if (time > QTime(LIST_HOURS_MAX, 0))
+        time = QTime(LIST_HOURS_MAX, 0);
+
+    return time;
 }
 
-void SettingsDialog::toggleAutomaticLocationUpdate(bool value)
+void SettingsDialog::toggleAutomaticLocationUpdate(bool enabled)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_automaticLocationUpdateInterval->setEnabled(value);
+#ifdef Q_WS_MAEMO_5
+    m_automaticLocationUpdateIntervalButton->setEnabled(enabled);
+#else
+    m_automaticLocationUpdateInterval->setEnabled(enabled);
+#endif
 }