cell/gps only on maemo5
[googlelatitude] / src / latitude.cpp
1 #include "latitude.h"
2
3 LatitudeGUI::LatitudeGUI(QMainWindow *parent) : QMainWindow(parent) {
4     glatitude = new GoogleLatitude(this);
5     gps = new GpsMaemo5(this);
6     setting = new QSettings();
7     urllogin = QUrl::fromEncoded("https://www.google.com/accounts/ServiceLogin?service=friendview&continue=http://www.google.com/maps/m?mode=latitude");
8
9     show_lat();
10
11     connect(glatitude, SIGNAL(getOK()), this, SLOT(get_loc()));
12     connect(gps, SIGNAL(getOK()), this, SLOT(get_maemo5()));
13     connect(gps, SIGNAL(getOK_acwp()), this, SLOT(get_acwp()));
14     connect(gps, SIGNAL(getOK_agnss()), this, SLOT(get_agnss()));
15     connect(glatitude, SIGNAL(setOK()), this, SLOT(set_OK()));
16     connect(glatitude, SIGNAL(setERROR()), this, SLOT(set_ERROR()));
17     connect(this, SIGNAL(newpos()), this, SLOT(set()));
18
19 }
20
21 void LatitudeGUI::get_loc() {
22     location_lat->setText(QString::number(glatitude->get_lat()));
23     location_lon->setText(QString::number(glatitude->get_lon()));
24     location_acc->setText(QString::number(glatitude->get_acc()));
25     status->setText(tr("Using google.com/loc, ip-based" ));
26     emit newpos();
27 }
28
29 void LatitudeGUI::get_maemo5() {
30     location_lat->setText(QString::number(gps->get_lat()));
31     location_lon->setText(QString::number(gps->get_lon()));
32     location_acc->setText(QString::number(gps->get_acc()));
33     status->setText(tr("Using liblocation "));
34     emit newpos();
35 }
36
37 void LatitudeGUI::get_acwp() {
38     location_lat->setText(QString::number(gps->get_lat()));
39     location_lon->setText(QString::number(gps->get_lon()));
40     location_acc->setText(QString::number(gps->get_acc()));
41     status->setText(tr("Using acwp, cell-based "));
42     emit newpos();
43 }
44
45 void LatitudeGUI::get_agnss() {
46     location_lat->setText(QString::number(gps->get_lat()));
47     location_lon->setText(QString::number(gps->get_lon()));
48     location_acc->setText(QString::number(gps->get_acc()));
49     status->setText(tr("Using agnss, gps-based "));
50     emit newpos();
51 }
52
53 void LatitudeGUI::set() {
54     glatitude->login(login_user->text(),
55                      login_pass->text());
56     glatitude->set(location_lat->text().toDouble(),
57                    location_lon->text().toDouble(),
58                    location_acc->text().toDouble());
59 }
60
61 void LatitudeGUI::set_OK() {
62     status->setText(tr("Updated Location !"));
63 }
64
65 void LatitudeGUI::set_ERROR() {
66     status->setText(tr("Error in Authentification !"));
67 }
68
69 void LatitudeGUI::save() {
70     setting->setValue("user", login_user->text());
71     setting->setValue("pass", login_pass->text());
72 }
73
74 void LatitudeGUI::show_map() {
75     // no scrash and stop gps
76     location_lat = new QLineEdit(QString::number(0));
77     location_lon = new QLineEdit(QString::number(0));
78     location_acc = new QLineEdit(QString::number(0));
79     status = new QLineEdit("Ready");
80     login_user = new QLineEdit(setting->value("user","my_username").toString());
81     login_pass = new QLineEdit(setting->value("pass","my_password").toString());
82     gps->stop();
83
84     // webkit magic
85     maps = new QWebView();
86     connect(maps, SIGNAL(loadFinished(bool)), this, SLOT(maps_login()));
87     maps->load(urllogin);
88
89     // set widget
90     setCentralWidget(maps);
91     setWindowTitle(tr("Google Maps"));
92
93     // menu
94     menuBar()->clear();
95     menuBar()->addAction(tr("&Latitude") ,this, SLOT(show_lat()));
96 }
97
98 void LatitudeGUI::show_lat() {
99     // login input
100     login_user = new QLineEdit(setting->value("user","my_username").toString());
101     login_pass = new QLineEdit(setting->value("pass","my_password").toString());
102     login_pass->setEchoMode(QLineEdit::Password);
103     QPushButton *button_update = new QPushButton(tr("Manual &Update"));
104     // login layout
105     QFormLayout *layout_login = new QFormLayout();
106     layout_login->addRow(tr("&Username"), login_user);
107     layout_login->addRow(tr("&Password"), login_pass);
108     layout_login->addRow(button_update);
109     // login connect
110     connect(login_user, SIGNAL(editingFinished()), this, SLOT(save()));
111     connect(login_pass, SIGNAL(editingFinished()), this, SLOT(save()));
112     connect(login_user, SIGNAL(returnPressed()), login_pass, SLOT(setFocus()));
113     connect(login_pass, SIGNAL(returnPressed()), this, SLOT(set()));
114     connect(button_update, SIGNAL(clicked()), this, SLOT(set()));
115
116     // location input
117     location_lat = new QLineEdit(QString::number(0));
118     location_lon = new QLineEdit(QString::number(0));
119     location_acc = new QLineEdit(QString::number(0));
120     // location layout
121     QFormLayout *layout_location = new QFormLayout();
122     layout_location->addRow(tr("lat :"), location_lat);
123     layout_location->addRow(tr("lon :"), location_lon);
124     layout_location->addRow(tr("acc :"), location_acc);
125
126     // source button
127     QPushButton *source_loc = new QPushButton(tr("Update with &IP"));
128     QPushButton *source_cell = new QPushButton(tr("Update with &Cell"));
129     QPushButton *source_gps = new QPushButton(tr("Update with &Gps"));
130     // source layout
131     QHBoxLayout *layout_source = new QHBoxLayout();
132     layout_source->addWidget(source_loc);
133     layout_source->addWidget(source_cell);
134     layout_source->addWidget(source_gps);
135     // source connect
136     connect(source_loc, SIGNAL(clicked()), glatitude, SLOT(get()));
137     connect(source_cell, SIGNAL(clicked()), gps, SLOT(get_acwp()));
138     connect(source_gps, SIGNAL(clicked()), gps, SLOT(get_agnss()));
139
140     // main layout
141     location = new QWidget();
142     QHBoxLayout *layout_form = new QHBoxLayout();
143     layout_form->addLayout(layout_login);
144     layout_form->addLayout(layout_location);
145     QVBoxLayout *layout = new QVBoxLayout();
146     status = new QLineEdit("Ready");
147     status->setReadOnly(true);
148     status->setDisabled(true);
149     layout->addLayout(layout_form);
150     layout->addWidget(status);
151     layout->addLayout(layout_source);
152     location->setLayout(layout);
153
154     // gps enable only on maemo5
155 #ifndef Q_WS_MAEMO_5
156     source_cell->setDisabled(1);
157     source_gps->setDisabled(1);
158 #endif
159
160     // set widget
161     setCentralWidget(location);
162     setWindowTitle(tr("Google Latitude Updater"));
163
164     // menu
165     menuBar()->clear();
166     menuBar()->addAction(tr("&Maps") ,this, SLOT(show_map()));
167 }
168
169 void LatitudeGUI::maps_login() {
170     if ( maps->url() == urllogin ) {
171         maps->page()->mainFrame()->evaluateJavaScript(
172                 QString("document.getElementById('Email').value = \"%1\";").arg(
173                         setting->value("user").toString()));
174         maps->page()->mainFrame()->evaluateJavaScript(
175                 QString("document.getElementById('Passwd').value = \"%1\";").arg(
176                         setting->value("pass").toString()));
177         maps->page()->mainFrame()->evaluateJavaScript("document.getElementById('gaia_loginform').submit();");
178     }
179 }