fix the maximal unstable
[googlelatitude] / src / latitude.cpp
1 #include "latitude.h"
2
3 LatitudeGUI::LatitudeGUI(QMainWindow *parent) : QMainWindow(parent) {
4 #ifdef Q_WS_MAEMO_5
5     setAttribute(Qt::WA_Maemo5AutoOrientation, true);
6 #endif
7     setWindowTitle(tr("Latitude & Buzz"));
8     setting = new QSettings();
9
10     // GUI
11     url = QUrl::fromEncoded("https://www.google.com/accounts/ServiceLogin?service=friendview&continue=http://www.google.com/maps/m?mode=latitude");
12     maps = new QWebView();
13     maps->settings()->setAttribute(QWebSettings::PluginsEnabled, true);
14     connect(maps, SIGNAL(loadFinished(bool)), this, SLOT(maps_login()));
15     maps->load(url);
16     menuBar()->addAction(tr("&Latitude"), this, SLOT(mode_latitude()));
17     menuBar()->addAction(tr("&Buzz"), this, SLOT(mode_buzz()));
18     menuBar()->addAction(tr("&Config"), this, SLOT(config()));
19
20     demonio = new QAction(this);
21     if ( system("killall -0 GoogleLatitudeDaemon 2> /dev/null" ) ) {
22         demonio->setText(tr("&Start Daemon"));
23     } else {
24         demonio->setText(tr("&Stop Daemon"));
25     }
26     connect(demonio, SIGNAL(triggered()), this, SLOT(mode_demonio()));
27     menuBar()->addAction(demonio);
28
29     // updater
30     glatitude = new GoogleLatitude(this);
31     gps = new GpsMaemo5(this);
32     connect(gps, SIGNAL(fix()), this, SLOT(set()));
33     connect(glatitude, SIGNAL(OK()), this, SLOT(latitude_OK()));
34     connect(glatitude, SIGNAL(ERROR()), this, SLOT(latitude_ERROR()));
35
36     // show it
37     setCentralWidget(maps);
38 }
39
40 void LatitudeGUI::config() {
41     // login input
42     QDialog *dialoglogin = new QDialog(this);
43     QLineEdit *login_user = new QLineEdit(setting->value("user","my_username").toString());
44     QLineEdit *login_pass = new QLineEdit(setting->value("pass","my_password").toString());
45     login_pass->setEchoMode(QLineEdit::Password);
46     QLineEdit *login_freq = new QLineEdit(setting->value("freq","120").toString());
47     QFormLayout *layout_login = new QFormLayout();
48     layout_login->addRow(tr("&Username"), login_user);
49     layout_login->addRow(tr("&Password"), login_pass);
50     layout_login->addRow(tr("&Interval"), login_freq);
51     connect(login_user, SIGNAL(textEdited(QString)), this, SLOT(save_user(QString)));
52     connect(login_pass, SIGNAL(textEdited(QString)), this, SLOT(save_pass(QString)));
53     connect(login_freq, SIGNAL(textEdited(QString)), this, SLOT(save_freq(QString)));
54     connect(login_user, SIGNAL(returnPressed()), login_pass, SLOT(setFocus()));
55     connect(login_pass, SIGNAL(returnPressed()), login_freq, SLOT(setFocus()));
56     connect(login_freq, SIGNAL(returnPressed()), dialoglogin, SLOT(accept()));
57     dialoglogin->setLayout(layout_login);
58     dialoglogin->exec();
59
60     set();
61     mode_latitude();
62     glatitude->reset();
63 }
64
65
66 void LatitudeGUI::set() {
67     qDebug() << "LatitudeGUI: set";
68     glatitude->login(setting->value("user","my_username").toString(),
69                      setting->value("pass","my_password").toString());
70     glatitude->freq(setting->value("freq","120").toInt());
71     glatitude->set(gps->get_lat(),
72                    gps->get_lon(),
73                    gps->get_acc());
74 }
75
76 void LatitudeGUI::latitude_OK() {
77     qDebug() << "LatitudeGUI: send";
78 }
79
80 void LatitudeGUI::latitude_ERROR() {
81 #ifdef Q_WS_MAEMO_5
82     QString error_message = tr("<b>Error in Authentification!</b><br><br>") +
83                             tr("Plese verify your login details<br>");
84     QMaemo5InformationBox::information(this, error_message,
85                                        QMaemo5InformationBox::NoTimeout);
86 #endif
87     qDebug() << "LatitudeGUI: no auth";
88     config();
89 }
90
91 void LatitudeGUI::save_user(QString _user) {
92     setting->setValue("user", _user);
93 }
94 void LatitudeGUI::save_pass(QString _pass) {
95     setting->setValue("pass", _pass);
96 }
97 void LatitudeGUI::save_freq(QString _freq) {
98     setting->setValue("freq", _freq);
99 }
100 void LatitudeGUI::mode_buzz() {
101     maps->load(QUrl::fromEncoded("http://www.google.com/maps/m?l-view=map&l-lci=m,com.google.latitudepublicupdates&ac=f,s,l"));
102 }
103
104 void LatitudeGUI::mode_latitude() {
105     maps->load(QUrl::fromEncoded("https://www.google.com/accounts/ServiceLogin?service=friendview&continue=http://www.google.com/maps/m?mode=latitude"));
106 }
107
108 void LatitudeGUI::maps_login() {
109     if ( maps->url() == url ) {
110         maps->page()->mainFrame()->evaluateJavaScript(
111                 QString("document.getElementById('Email').value = \"%1\";").arg(
112                         setting->value("user").toString()));
113         maps->page()->mainFrame()->evaluateJavaScript(
114                 QString("document.getElementById('Passwd').value = \"%1\";").arg(
115                         setting->value("pass").toString()));
116         maps->page()->mainFrame()->evaluateJavaScript("document.getElementById('gaia_loginform').submit();");
117     }
118 }
119
120 void LatitudeGUI::mode_demonio() {
121     if ( ! system("killall -0 GoogleLatitudeDaemon 2> /dev/null" ) ) {
122         system("killall GoogleLatitudeDaemon 2> /dev/null");
123         demonio->setText(tr("&Start Daemon"));
124     } else {
125         QProcess *cli = new QProcess(this);
126         cli->start(QCoreApplication::applicationDirPath()+QDir::separator()+"GoogleLatitudeDaemon");
127         qDebug() << "LatitudeGUI: demonio" << QCoreApplication::applicationDirPath()+"/GoogleLatitudeDaemon";
128         if ( ! system("killall -0 GoogleLatitudeDaemon 2> /dev/null" ) ) {
129             demonio->setText(tr("&Stop Daemon"));
130         }
131     }
132 }