Changed the old code to work as a daemon
[googlelatitude] / src / daemon / latitude.cpp
1 #include "latitude.h"
2 #include "qwebviewselectionsuppressor.h"
3 #include "../common/dbusclient.h"
4 #include "customuseragentpage.h"
5
6 #include <QTimer>
7
8 LatitudeGUI::LatitudeGUI(QSettings *set, QWidget *parent) : QMainWindow(parent)
9 {
10 #ifdef Q_WS_MAEMO_5
11     setAttribute(Qt::WA_Maemo5AutoOrientation, true);
12     DBusClient* dbcli = new DBusClient(this);
13 #endif
14     loginOk = false;
15     settings = set;
16     loadSettings();
17
18     // GUI
19     url = QUrl::fromEncoded("https://www.google.com/accounts/ServiceLogin?service=friendview&continue=http://www.google.com/maps/m?mode=latitude");
20     maps = new QWebView();
21     maps->setPage(new customUAPage(this));
22     maps->settings()->enablePersistentStorage();
23     maps->setZoomFactor(1.4);
24     maps->settings()->setAttribute(QWebSettings::PluginsEnabled, true);
25     maps->setContextMenuPolicy(Qt::NoContextMenu);
26     connect(maps, SIGNAL(loadFinished(bool)), this, SLOT(maps_login()));
27     maps->load(url);
28     new QWebViewSelectionSuppressor(maps);
29     menuBar()->addAction(tr("&Latitude"), this, SLOT(mode_latitude()));
30     menuBar()->addAction(tr("&Buzz"), this, SLOT(mode_buzz()));
31
32     // updater
33     glatitude = new GoogleLatitude(this);
34 #ifdef Q_WS_MAEMO_5
35     gps = new GpsMaemo5(location_interval, location_method, this);
36     enableServices();
37
38     connect(gps, SIGNAL(fix()), this, SLOT(set()));
39     connect(dbcli, SIGNAL(sigNetworkConnected()),
40             this, SLOT(enableServices()));
41     connect(dbcli, SIGNAL(sigNetworkDisconnected()),
42             this, SLOT(disableServices()));
43
44 #endif
45     connect(glatitude, SIGNAL(OK()), this, SLOT(latitude_OK()));
46     connect(glatitude, SIGNAL(ERROR()), this, SLOT(latitude_ERROR()));
47
48     // show it
49     setCentralWidget(maps);
50 }
51
52 #ifdef Q_WS_MAEMO_5
53 void LatitudeGUI::enableServices() {
54     if (!DBusClient::networkConnected())
55         return;
56
57     gps->enable();
58     glatitude->enableUpdates();
59 }
60 void LatitudeGUI::disableServices() {
61     gps->disable();
62     glatitude->disableUpdates();
63 }
64 #endif
65
66 void LatitudeGUI::set() {
67 #ifdef Q_WS_MAEMO_5
68     //QMaemo5InformationBox::information(this, "new position...", 1000);
69 #else
70     qDebug() << "new position...";
71 #endif
72     glatitude->login(settings->value("user","my_username").toString(),
73                      settings->value("pass","my_password").toString());
74     glatitude->set(gps->get_lat(),
75                    gps->get_lon(),
76                    gps->get_acc());
77     glatitude->disableUpdates();
78 #ifdef Q_WS_MAEMO_5
79     QTimer::singleShot(location_interval*100, glatitude, SLOT(enableUpdates()));
80 #endif
81 }
82
83 void LatitudeGUI::latitude_OK() {    
84     loginOk = true; //We could login at least once!
85     //TODO: REMOVE THIS
86     qDebug() << maps->url();
87 #ifdef Q_WS_MAEMO_5
88     //QMaemo5InformationBox::information(this, "...location Sent!", 1000);
89 #else
90     qDebug() << "...location Sent!";
91 #endif
92 }
93
94 void LatitudeGUI::latitude_ERROR() {
95     //Errors in authentication may occur frequently due to transmition or
96     //reception problems in low signal situations, so we will only alert the
97     //user if we can't login at least once
98     if (loginOk)
99         return;
100
101 #ifdef Q_WS_MAEMO_5
102     disableServices();
103     QString error_message = tr("<b>Error in Authentification!</b><br><br> ") +
104                             tr("Plese verify your login details<br>");
105     QMaemo5InformationBox::information(this, error_message,
106                                        QMaemo5InformationBox::NoTimeout);
107     system("/usr/bin/googlelatitude-settings");
108     enableServices();
109 #else
110     qDebug() << "Error in Authentification!";
111 #endif
112 }
113
114 void LatitudeGUI::loadSettings()
115 {
116     settings->sync();
117 #ifdef Q_WS_MAEMO_5
118     location_method = (LocationGPSDControlMethod) settings->value("location", LOCATION_METHOD_GNSS).toInt();
119     location_interval = (LocationGPSDControlInterval) settings->value("interval", LOCATION_INTERVAL_20S).toUInt();
120 #endif
121 }
122
123 void LatitudeGUI::maps_login() {
124     if ( maps->url() == url ) {
125         maps->page()->mainFrame()->evaluateJavaScript(
126                 QString("document.getElementById('Email').value = \"%1\";").arg(
127                         settings->value("user").toString()));
128         maps->page()->mainFrame()->evaluateJavaScript(
129                 QString("document.getElementById('Passwd').value = \"%1\";").arg(
130                         settings->value("pass").toString()));
131         maps->page()->mainFrame()->evaluateJavaScript("document.getElementById('gaia_loginform').submit();");
132     }
133 }
134
135 void LatitudeGUI::mode_buzz() {
136     maps->load(QUrl::fromEncoded("http://www.google.com/maps/m?l-view=map&l-lci=m,com.google.latitudepublicupdates&ac=f,s,l"));
137 }
138
139 void LatitudeGUI::mode_latitude() {
140     maps->load(QUrl::fromEncoded("https://www.google.com/accounts/ServiceLogin?service=friendview&continue=http://www.google.com/maps/m?mode=latitude"));
141 }
142
143 void LatitudeGUI::reparseConfiguration()
144 {
145 #ifdef Q_WS_MAEMO_5
146     disableServices();
147     loadSettings();
148     gps->updateGPSConfig(location_interval, location_method);
149     enableServices();
150     QMaemo5InformationBox::information(this,
151                                        tr("Google Latitude daemon updated"),
152                                        2000);
153 #endif
154 }
155
156 void LatitudeGUI::save_user(QString _user) {
157     settings->setValue("user", _user);
158 }
159 void LatitudeGUI::save_pass(QString _pass) {
160     settings->setValue("pass", _pass);
161 }