Changed the old code to work as a daemon
[googlelatitude] / src / daemon / latitude.cpp
diff --git a/src/daemon/latitude.cpp b/src/daemon/latitude.cpp
new file mode 100644 (file)
index 0000000..dd1c6d7
--- /dev/null
@@ -0,0 +1,161 @@
+#include "latitude.h"
+#include "qwebviewselectionsuppressor.h"
+#include "../common/dbusclient.h"
+#include "customuseragentpage.h"
+
+#include <QTimer>
+
+LatitudeGUI::LatitudeGUI(QSettings *set, QWidget *parent) : QMainWindow(parent)
+{
+#ifdef Q_WS_MAEMO_5
+    setAttribute(Qt::WA_Maemo5AutoOrientation, true);
+    DBusClient* dbcli = new DBusClient(this);
+#endif
+    loginOk = false;
+    settings = set;
+    loadSettings();
+
+    // GUI
+    url = QUrl::fromEncoded("https://www.google.com/accounts/ServiceLogin?service=friendview&continue=http://www.google.com/maps/m?mode=latitude");
+    maps = new QWebView();
+    maps->setPage(new customUAPage(this));
+    maps->settings()->enablePersistentStorage();
+    maps->setZoomFactor(1.4);
+    maps->settings()->setAttribute(QWebSettings::PluginsEnabled, true);
+    maps->setContextMenuPolicy(Qt::NoContextMenu);
+    connect(maps, SIGNAL(loadFinished(bool)), this, SLOT(maps_login()));
+    maps->load(url);
+    new QWebViewSelectionSuppressor(maps);
+    menuBar()->addAction(tr("&Latitude"), this, SLOT(mode_latitude()));
+    menuBar()->addAction(tr("&Buzz"), this, SLOT(mode_buzz()));
+
+    // updater
+    glatitude = new GoogleLatitude(this);
+#ifdef Q_WS_MAEMO_5
+    gps = new GpsMaemo5(location_interval, location_method, this);
+    enableServices();
+
+    connect(gps, SIGNAL(fix()), this, SLOT(set()));
+    connect(dbcli, SIGNAL(sigNetworkConnected()),
+            this, SLOT(enableServices()));
+    connect(dbcli, SIGNAL(sigNetworkDisconnected()),
+            this, SLOT(disableServices()));
+
+#endif
+    connect(glatitude, SIGNAL(OK()), this, SLOT(latitude_OK()));
+    connect(glatitude, SIGNAL(ERROR()), this, SLOT(latitude_ERROR()));
+
+    // show it
+    setCentralWidget(maps);
+}
+
+#ifdef Q_WS_MAEMO_5
+void LatitudeGUI::enableServices() {
+    if (!DBusClient::networkConnected())
+        return;
+
+    gps->enable();
+    glatitude->enableUpdates();
+}
+void LatitudeGUI::disableServices() {
+    gps->disable();
+    glatitude->disableUpdates();
+}
+#endif
+
+void LatitudeGUI::set() {
+#ifdef Q_WS_MAEMO_5
+    //QMaemo5InformationBox::information(this, "new position...", 1000);
+#else
+    qDebug() << "new position...";
+#endif
+    glatitude->login(settings->value("user","my_username").toString(),
+                     settings->value("pass","my_password").toString());
+    glatitude->set(gps->get_lat(),
+                   gps->get_lon(),
+                   gps->get_acc());
+    glatitude->disableUpdates();
+#ifdef Q_WS_MAEMO_5
+    QTimer::singleShot(location_interval*100, glatitude, SLOT(enableUpdates()));
+#endif
+}
+
+void LatitudeGUI::latitude_OK() {    
+    loginOk = true; //We could login at least once!
+    //TODO: REMOVE THIS
+    qDebug() << maps->url();
+#ifdef Q_WS_MAEMO_5
+    //QMaemo5InformationBox::information(this, "...location Sent!", 1000);
+#else
+    qDebug() << "...location Sent!";
+#endif
+}
+
+void LatitudeGUI::latitude_ERROR() {
+    //Errors in authentication may occur frequently due to transmition or
+    //reception problems in low signal situations, so we will only alert the
+    //user if we can't login at least once
+    if (loginOk)
+        return;
+
+#ifdef Q_WS_MAEMO_5
+    disableServices();
+    QString error_message = tr("<b>Error in Authentification!</b><br><br> ") +
+                            tr("Plese verify your login details<br>");
+    QMaemo5InformationBox::information(this, error_message,
+                                       QMaemo5InformationBox::NoTimeout);
+    system("/usr/bin/googlelatitude-settings");
+    enableServices();
+#else
+    qDebug() << "Error in Authentification!";
+#endif
+}
+
+void LatitudeGUI::loadSettings()
+{
+    settings->sync();
+#ifdef Q_WS_MAEMO_5
+    location_method = (LocationGPSDControlMethod) settings->value("location", LOCATION_METHOD_GNSS).toInt();
+    location_interval = (LocationGPSDControlInterval) settings->value("interval", LOCATION_INTERVAL_20S).toUInt();
+#endif
+}
+
+void LatitudeGUI::maps_login() {
+    if ( maps->url() == url ) {
+        maps->page()->mainFrame()->evaluateJavaScript(
+                QString("document.getElementById('Email').value = \"%1\";").arg(
+                        settings->value("user").toString()));
+        maps->page()->mainFrame()->evaluateJavaScript(
+                QString("document.getElementById('Passwd').value = \"%1\";").arg(
+                        settings->value("pass").toString()));
+        maps->page()->mainFrame()->evaluateJavaScript("document.getElementById('gaia_loginform').submit();");
+    }
+}
+
+void LatitudeGUI::mode_buzz() {
+    maps->load(QUrl::fromEncoded("http://www.google.com/maps/m?l-view=map&l-lci=m,com.google.latitudepublicupdates&ac=f,s,l"));
+}
+
+void LatitudeGUI::mode_latitude() {
+    maps->load(QUrl::fromEncoded("https://www.google.com/accounts/ServiceLogin?service=friendview&continue=http://www.google.com/maps/m?mode=latitude"));
+}
+
+void LatitudeGUI::reparseConfiguration()
+{
+#ifdef Q_WS_MAEMO_5
+    disableServices();
+    loadSettings();
+    gps->updateGPSConfig(location_interval, location_method);
+    enableServices();
+    QMaemo5InformationBox::information(this,
+                                       tr("Google Latitude daemon updated"),
+                                       2000);
+#endif
+}
+
+void LatitudeGUI::save_user(QString _user) {
+    settings->setValue("user", _user);
+}
+void LatitudeGUI::save_pass(QString _pass) {
+    settings->setValue("pass", _pass);
+}