Changed the old code to work as a daemon
[googlelatitude] / src / settings / settings.cpp
diff --git a/src/settings/settings.cpp b/src/settings/settings.cpp
new file mode 100644 (file)
index 0000000..55f36db
--- /dev/null
@@ -0,0 +1,217 @@
+#include "settings.h"
+#include "ui_settings.h"
+#include "../common/dbusclient.h"
+
+#include <QProcess>
+#include <QDebug>
+
+Settings::Settings(QWidget *parent) :
+    QWidget(parent),
+    ui(new Ui::Settings)
+{
+    intervals << tr("1 second") << tr("2 seconds") << tr("5 seconds") <<
+                 tr("10 seconds") << tr("20 seconds") << tr("30 seconds") <<
+                 tr("60 seconds") << tr("120 seconds");
+
+    ui->setupUi(this);
+    addUIElements();   //Add Maemo 5 Specific widgets
+
+    settings = new QSettings();
+    loadSettings();
+
+    //Make the window a modal panel
+    setWindowFlags(Qt::Dialog);
+    setWindowModality(Qt::ApplicationModal);
+
+    connect(ui->login_user, SIGNAL(returnPressed()), ui->login_pass, SLOT(setFocus()));
+    connect(ui->login_pass, SIGNAL(returnPressed()), SLOT(close()));
+    connect(ui->ok_button, SIGNAL(clicked()), SLOT(close()));
+
+}
+
+Settings::~Settings()
+{
+#ifdef Q_WS_MAEMO_5
+    delete interval_button;
+    delete interval_selector;
+    delete interval_model;
+#endif
+    delete ui;
+    delete settings;
+}
+
+void Settings::addUIElements()
+{
+    QVBoxLayout* ui_layout = (QVBoxLayout*)ui->scrollArea->widget()->layout();
+#ifdef Q_WS_MAEMO_5
+    interval_button = new QMaemo5ValueButton("Update Interval",ui->scrollArea->widget());
+    interval_button->setValueLayout(QMaemo5ValueButton::ValueBesideText);
+    interval_selector = new QMaemo5ListPickSelector;
+    interval_model = new QStandardItemModel(0, 1);
+    populateListModel(interval_model);
+    interval_selector->setModel(interval_model);
+    interval_button->setPickSelector(interval_selector);
+    ui_layout->insertWidget(4, interval_button);
+#endif
+}
+
+void Settings::closeEvent(QCloseEvent *event)
+{
+    storeSettings();
+    settings->sync();
+#ifdef Q_WS_MAEMO_5
+    updateDaemon();
+#endif
+    event->accept();
+}
+
+void Settings::loadSettings()
+{
+    unsigned short location;
+    unsigned short interval;
+
+    ui->connectIfNecessary->setChecked( settings->value("autoconnect",true).toBool() );
+    ui->launchOnPowerOn->setChecked( settings->value("autolaunch",false).toBool() );
+#ifdef Q_WS_MAEMO_5
+    location = settings->value("location", LOCATION_METHOD_GNSS).toInt();
+#endif
+    ui->login_user->setText(settings->value("user","my_username").toString());
+    ui->login_pass->setText(settings->value("pass","my_password").toString());
+#ifdef Q_WS_MAEMO_5
+    switch(settings->value("interval", LOCATION_INTERVAL_20S).toInt()) {
+        case LOCATION_INTERVAL_1S:
+            interval = 0;
+            break;
+        case LOCATION_INTERVAL_2S:
+            interval = 1;
+            break;
+        case LOCATION_INTERVAL_5S:
+            interval = 2;
+            break;
+        case LOCATION_INTERVAL_10S:
+            interval = 3;
+            break;
+        case LOCATION_INTERVAL_20S:
+            interval = 4;
+            break;
+        case LOCATION_INTERVAL_30S:
+            interval = 5;
+            break;
+        case LOCATION_INTERVAL_60S:
+            interval = 6;
+            break;
+        case LOCATION_INTERVAL_120S:
+            interval = 7;
+            break;
+    }
+    interval_selector->setCurrentIndex(interval);
+
+
+    if (DBusClient::daemonRunning())
+        ui->status_on->setChecked(true);
+    else
+        ui->status_off->setChecked(true);
+
+
+    switch (location) {
+        case LOCATION_METHOD_GNSS:
+            ui->location_gps->setChecked(true);
+            break;
+        case LOCATION_METHOD_AGNSS | LOCATION_METHOD_ACWP:
+            ui->location_gps_network->setChecked(true);
+            break;
+        case LOCATION_METHOD_ACWP:
+            ui->location_network->setChecked(true);
+            break;
+    }
+#endif
+}
+
+void Settings::populateListModel(QStandardItemModel *model)
+{
+    for(int i = 0; i < intervals.size(); ++i) {
+        QStandardItem *item = new QStandardItem(intervals.at(i));
+        item->setTextAlignment(Qt::AlignCenter); // the Maemo 5 design spec recommends this.
+        item->setEditable(false); // prevent editing of the item
+        model->appendRow(item);
+    }
+}
+
+void Settings::storeSettings()
+{
+    unsigned short status;
+    unsigned short location;
+    unsigned short interval;
+
+    settings->setValue("autoconnect", ui->connectIfNecessary->checkState());
+    settings->setValue("autolaunch", ui->launchOnPowerOn->checkState());
+    settings->setValue("user", ui->login_user->text());
+    settings->setValue("pass", ui->login_pass->text());
+#ifdef Q_WS_MAEMO_5
+    switch(interval_selector->currentIndex()) {
+        case 0:
+            interval = LOCATION_INTERVAL_1S;
+            break;
+        case 1:
+            interval = LOCATION_INTERVAL_2S;
+            break;
+        case 2:
+            interval = LOCATION_INTERVAL_5S;
+            break;
+        case 3:
+            interval = LOCATION_INTERVAL_10S;
+            break;
+        case 4:
+            interval = LOCATION_INTERVAL_20S;
+            break;
+        case 5:
+            interval = LOCATION_INTERVAL_30S;
+            break;
+        case 6:
+            interval = LOCATION_INTERVAL_60S;
+            break;
+        case 7:
+            interval = LOCATION_INTERVAL_120S;
+            break;
+    }
+    settings->setValue("interval", interval);
+
+    if (ui->location_gps->isChecked())
+        location = LOCATION_METHOD_GNSS;
+    else if (ui->location_gps_network->isChecked())
+        location = LOCATION_METHOD_AGNSS | LOCATION_METHOD_ACWP;
+    else if (ui->location_network->isChecked())
+        location = LOCATION_METHOD_ACWP;
+    settings->setValue("location", location);
+#endif
+
+    settings->setValue("status", status);
+}
+
+#ifdef Q_WS_MAEMO_5
+void Settings::updateDaemon()
+{
+    if (ui->status_on->isChecked()) {
+        if (DBusClient::daemonRunning())
+            DBusClient::reloadDaemonConfig();
+        else
+            (new QProcess())->start(QString("/opt/linfati.com/googlelatitude-daemon"));
+    }
+    else if (ui->status_off->isChecked())
+        DBusClient::quitDaemon();
+    //else if (ui->status_hide->isChecked())
+    //    status = STATUS_HIDE;
+}
+#endif
+
+void Settings::changeEvent(QEvent *e)
+{
+    QWidget::changeEvent(e);
+    switch (e->type()) {
+    case QEvent::LanguageChange:
+        ui->retranslateUi(this);
+        break;
+    default:
+        break;
+    }
+}