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