Changed the old code to work as a daemon
[googlelatitude] / src / settings / settings.cpp
1 #include "settings.h"
2 #include "ui_settings.h"
3 #include "../common/dbusclient.h"
4
5 #include <QProcess>
6 #include <QDebug>
7
8 Settings::Settings(QWidget *parent) :
9     QWidget(parent),
10     ui(new Ui::Settings)
11 {
12     intervals << tr("1 second") << tr("2 seconds") << tr("5 seconds") <<
13                  tr("10 seconds") << tr("20 seconds") << tr("30 seconds") <<
14                  tr("60 seconds") << tr("120 seconds");
15
16     ui->setupUi(this);
17     addUIElements();   //Add Maemo 5 Specific widgets
18
19     settings = new QSettings();
20     loadSettings();
21
22     //Make the window a modal panel
23     setWindowFlags(Qt::Dialog);
24     setWindowModality(Qt::ApplicationModal);
25
26     connect(ui->login_user, SIGNAL(returnPressed()), ui->login_pass, SLOT(setFocus()));
27     connect(ui->login_pass, SIGNAL(returnPressed()), SLOT(close()));
28     connect(ui->ok_button, SIGNAL(clicked()), SLOT(close()));
29
30 }
31
32 Settings::~Settings()
33 {
34 #ifdef Q_WS_MAEMO_5
35     delete interval_button;
36     delete interval_selector;
37     delete interval_model;
38 #endif
39     delete ui;
40     delete settings;
41 }
42
43 void Settings::addUIElements()
44 {
45     QVBoxLayout* ui_layout = (QVBoxLayout*)ui->scrollArea->widget()->layout();
46 #ifdef Q_WS_MAEMO_5
47     interval_button = new QMaemo5ValueButton("Update Interval",ui->scrollArea->widget());
48     interval_button->setValueLayout(QMaemo5ValueButton::ValueBesideText);
49     interval_selector = new QMaemo5ListPickSelector;
50     interval_model = new QStandardItemModel(0, 1);
51     populateListModel(interval_model);
52     interval_selector->setModel(interval_model);
53     interval_button->setPickSelector(interval_selector);
54     ui_layout->insertWidget(4, interval_button);
55 #endif
56 }
57
58 void Settings::closeEvent(QCloseEvent *event)
59 {
60     storeSettings();
61     settings->sync();
62 #ifdef Q_WS_MAEMO_5
63     updateDaemon();
64 #endif
65     event->accept();
66 }
67
68 void Settings::loadSettings()
69 {
70     unsigned short location;
71     unsigned short interval;
72
73     ui->connectIfNecessary->setChecked( settings->value("autoconnect",true).toBool() );
74     ui->launchOnPowerOn->setChecked( settings->value("autolaunch",false).toBool() );
75 #ifdef Q_WS_MAEMO_5
76     location = settings->value("location", LOCATION_METHOD_GNSS).toInt();
77 #endif
78     ui->login_user->setText(settings->value("user","my_username").toString());
79     ui->login_pass->setText(settings->value("pass","my_password").toString());
80 #ifdef Q_WS_MAEMO_5
81     switch(settings->value("interval", LOCATION_INTERVAL_20S).toInt()) {
82         case LOCATION_INTERVAL_1S:
83             interval = 0;
84             break;
85         case LOCATION_INTERVAL_2S:
86             interval = 1;
87             break;
88         case LOCATION_INTERVAL_5S:
89             interval = 2;
90             break;
91         case LOCATION_INTERVAL_10S:
92             interval = 3;
93             break;
94         case LOCATION_INTERVAL_20S:
95             interval = 4;
96             break;
97         case LOCATION_INTERVAL_30S:
98             interval = 5;
99             break;
100         case LOCATION_INTERVAL_60S:
101             interval = 6;
102             break;
103         case LOCATION_INTERVAL_120S:
104             interval = 7;
105             break;
106     }
107     interval_selector->setCurrentIndex(interval);
108
109
110     if (DBusClient::daemonRunning())
111         ui->status_on->setChecked(true);
112     else
113         ui->status_off->setChecked(true);
114
115
116     switch (location) {
117         case LOCATION_METHOD_GNSS:
118             ui->location_gps->setChecked(true);
119             break;
120         case LOCATION_METHOD_AGNSS | LOCATION_METHOD_ACWP:
121             ui->location_gps_network->setChecked(true);
122             break;
123         case LOCATION_METHOD_ACWP:
124             ui->location_network->setChecked(true);
125             break;
126     }
127 #endif
128 }
129
130 void Settings::populateListModel(QStandardItemModel *model)
131 {
132     for(int i = 0; i < intervals.size(); ++i) {
133         QStandardItem *item = new QStandardItem(intervals.at(i));
134         item->setTextAlignment(Qt::AlignCenter); // the Maemo 5 design spec recommends this.
135         item->setEditable(false); // prevent editing of the item
136         model->appendRow(item);
137     }
138 }
139
140 void Settings::storeSettings()
141 {
142     unsigned short status;
143     unsigned short location;
144     unsigned short interval;
145
146     settings->setValue("autoconnect", ui->connectIfNecessary->checkState());
147     settings->setValue("autolaunch", ui->launchOnPowerOn->checkState());
148     settings->setValue("user", ui->login_user->text());
149     settings->setValue("pass", ui->login_pass->text());
150 #ifdef Q_WS_MAEMO_5
151     switch(interval_selector->currentIndex()) {
152         case 0:
153             interval = LOCATION_INTERVAL_1S;
154             break;
155         case 1:
156             interval = LOCATION_INTERVAL_2S;
157             break;
158         case 2:
159             interval = LOCATION_INTERVAL_5S;
160             break;
161         case 3:
162             interval = LOCATION_INTERVAL_10S;
163             break;
164         case 4:
165             interval = LOCATION_INTERVAL_20S;
166             break;
167         case 5:
168             interval = LOCATION_INTERVAL_30S;
169             break;
170         case 6:
171             interval = LOCATION_INTERVAL_60S;
172             break;
173         case 7:
174             interval = LOCATION_INTERVAL_120S;
175             break;
176     }
177     settings->setValue("interval", interval);
178
179     if (ui->location_gps->isChecked())
180         location = LOCATION_METHOD_GNSS;
181     else if (ui->location_gps_network->isChecked())
182         location = LOCATION_METHOD_AGNSS | LOCATION_METHOD_ACWP;
183     else if (ui->location_network->isChecked())
184         location = LOCATION_METHOD_ACWP;
185     settings->setValue("location", location);
186 #endif
187
188     settings->setValue("status", status);
189 }
190
191 #ifdef Q_WS_MAEMO_5
192 void Settings::updateDaemon()
193 {
194     if (ui->status_on->isChecked()) {
195         if (DBusClient::daemonRunning())
196             DBusClient::reloadDaemonConfig();
197         else
198             (new QProcess())->start(QString("/opt/linfati.com/googlelatitude-daemon"));
199     }
200     else if (ui->status_off->isChecked())
201         DBusClient::quitDaemon();
202     //else if (ui->status_hide->isChecked())
203     //    status = STATUS_HIDE;
204 }
205 #endif
206
207 void Settings::changeEvent(QEvent *e)
208 {
209     QWidget::changeEvent(e);
210     switch (e->type()) {
211     case QEvent::LanguageChange:
212         ui->retranslateUi(this);
213         break;
214     default:
215         break;
216     }
217 }