7708be8024762cbcbf8165709f8b13ddf2fa8f31
[googlelatitude] / src / latitude.cpp
1 #include "latitude.h"
2
3 LatitudeGUI::LatitudeGUI(QMainWindow *parent) : QMainWindow(parent) {
4     setting = new QSettings();
5     gps = new GpsMaemo5(this);
6     glatitude = new GoogleLatitude(this);
7
8     connect(gps, SIGNAL(fix()), this, SLOT(set()));
9     connect(glatitude, SIGNAL(glat_ok()), this, SLOT(latitude_ok()));
10     connect(glatitude, SIGNAL(glat_error()), this, SLOT(latitude_error()));
11
12     set_config();
13     if ( 0 ) gps->config(15, 3, 0);
14
15     gps->refresh();
16
17 #ifdef Q_WS_MAEMO_5
18     setAttribute(Qt::WA_Maemo5AutoOrientation, true);
19 #endif
20     setWindowTitle(tr("Latitude & Buzz"));
21
22     maps = new QWebView();
23     connect(maps, SIGNAL(loadFinished(bool)), this, SLOT(maps_login()));
24
25     maps->settings()->setAttribute(QWebSettings::PluginsEnabled, true);
26     maps->load(QUrl::fromEncoded("https://www.google.com/accounts/ServiceLogin?service=friendview&continue=http://www.google.com/maps/m?mode=latitude"));
27
28     setCentralWidget(maps);
29
30     menuBar()->addAction(tr("&Latitude"), this, SLOT(mode_latitude()));
31     menuBar()->addAction(tr("&Buzz"), this, SLOT(mode_buzz()));
32     menuBar()->addAction(tr("&Config"), this, SLOT(config()));
33
34     demonio = new QAction(this);
35     if ( system("killall -0 GoogleLatitudeDaemon 2> /dev/null" ) ) {
36         demonio->setText(tr("&Start Daemon"));
37     } else {
38         demonio->setText(tr("&Stop Daemon"));
39     }
40     connect(demonio, SIGNAL(triggered()), this, SLOT(mode_demonio()));
41     menuBar()->addAction(demonio);
42 }
43
44 void LatitudeGUI::set() {
45     qDebug() << "LatitudeGUI: set";
46     set_config();
47     glatitude->update(gps->get_lat(),
48                       gps->get_lon(),
49                       gps->get_acc());
50 }
51
52 void LatitudeGUI::set_config() {
53     qDebug() << "LatitudeGUI: set_config";
54     glatitude->set_login(setting->value("user","my_username").toString(),
55                          setting->value("pass","my_password").toString());
56
57     gps->config(setting->value("interval",1800).toInt(),
58                 setting->value("wait",30).toInt(),
59                 setting->value("usegps",false).toBool());
60 }
61
62 void LatitudeGUI::latitude_ok() {
63     qDebug() << "LatitudeGUI: ok";
64 }
65
66 void LatitudeGUI::latitude_error() {
67 #ifdef Q_WS_MAEMO_5
68     QString error_message = tr("<b>Error in Authentification!</b><br><br>") +
69                             tr("Plese verify your login details<br>");
70     QMaemo5InformationBox::information(this, error_message,
71                                        QMaemo5InformationBox::NoTimeout);
72 #endif
73     qDebug() << "LatitudeGUI: no auth";
74     config();
75 }
76
77 void LatitudeGUI::maps_login() {
78     if ( maps->url() == QUrl::fromEncoded("https://www.google.com/accounts/ServiceLogin?service=friendview&continue=http://www.google.com/maps/m?mode=latitude") ) {
79         maps->page()->mainFrame()->evaluateJavaScript(
80                 QString("document.getElementById('Email').value = \"%1\";").arg(
81                         setting->value("user").toString()));
82         maps->page()->mainFrame()->evaluateJavaScript(
83                 QString("document.getElementById('Passwd').value = \"%1\";").arg(
84                         setting->value("pass").toString()));
85         maps->page()->mainFrame()->evaluateJavaScript("document.getElementById('gaia_loginform').submit();");
86     }
87     if ( maps->url() == QUrl::fromEncoded("https://www.google.com/accounts/ServiceLoginAuth") ) {
88         latitude_error();
89     }
90 }
91
92 void LatitudeGUI::mode_buzz() {
93     maps->load(QUrl::fromEncoded("http://www.google.com/maps/m?l-view=map&l-lci=m,com.google.latitudepublicupdates&ac=f,s,l"));
94 }
95
96 void LatitudeGUI::mode_latitude() {
97     maps->load(QUrl::fromEncoded("https://www.google.com/accounts/ServiceLogin?service=friendview&continue=http://www.google.com/maps/m?mode=latitude"));
98 }
99
100 void LatitudeGUI::mode_demonio() {
101     if ( ! system("killall -0 GoogleLatitudeDaemon 2> /dev/null" ) ) {
102         system("killall GoogleLatitudeDaemon 2> /dev/null");
103         demonio->setText(tr("&Start Daemon"));
104     } else {
105         QProcess *cli = new QProcess(this);
106         cli->start(QCoreApplication::applicationDirPath()+QDir::separator()+"GoogleLatitudeDaemon");
107         qDebug() << "LatitudeGUI: demonio" << QCoreApplication::applicationDirPath()+"/GoogleLatitudeDaemon";
108         if ( ! system("killall -0 GoogleLatitudeDaemon 2> /dev/null" ) ) {
109             demonio->setText(tr("&Stop Daemon"));
110         }
111     }
112 }
113
114 void LatitudeGUI::config() {
115     gps->forcestop();
116
117     QDialog *dialoglogin = new QDialog(this);
118
119     QLineEdit *login_user = new QLineEdit(setting->value("user","my_username").toString());
120     QLineEdit *login_pass = new QLineEdit(setting->value("pass","my_password").toString());
121     login_pass->setEchoMode(QLineEdit::Password);
122
123     QLineEdit *gps_interval = new QLineEdit(setting->value("interval",1800).toString());
124     QLineEdit *gps_wait = new QLineEdit(setting->value("wait",30).toString());
125     QCheckBox *gps_use = new QCheckBox();
126     if ( setting->value("usegps",false).toBool() ) {
127         gps_use->setCheckState(Qt::Checked);
128     } else {
129         gps_use->setCheckState(Qt::Unchecked);
130     }
131
132     QCheckBox *daemon_use = new QCheckBox();
133     if ( setting->value("daemon",false).toBool() ) {
134         daemon_use->setCheckState(Qt::Checked);
135     } else {
136         daemon_use->setCheckState(Qt::Unchecked);
137     }
138
139     QFormLayout *layout_config = new QFormLayout();
140     layout_config->addRow(tr("&Username"), login_user);
141     layout_config->addRow(tr("&Password"), login_pass);
142     layout_config->addRow(tr("&Interval for Updates"), gps_interval);
143     layout_config->addRow(tr("&Wait for a Fix"), gps_wait);
144     layout_config->addRow(tr("&Use Gps"), gps_use);
145     layout_config->addRow(tr("&Daemon at Boot"), daemon_use);
146
147     connect(login_user, SIGNAL(textEdited(QString)), this, SLOT(save_user(QString)));
148     connect(login_user, SIGNAL(returnPressed()), login_pass, SLOT(setFocus()));
149     connect(login_pass, SIGNAL(textEdited(QString)), this, SLOT(save_pass(QString)));
150     connect(login_pass, SIGNAL(returnPressed()), dialoglogin, SLOT(accept()));
151
152     connect(gps_interval, SIGNAL(textEdited(QString)), this, SLOT(save_interval(QString)));
153     connect(gps_wait, SIGNAL(textEdited(QString)), this, SLOT(save_wait(QString)));
154     connect(gps_use, SIGNAL(stateChanged(int)), this, SLOT(save_gps(int)));
155     connect(daemon_use, SIGNAL(stateChanged(int)), this, SLOT(save_daemon(int)));
156
157     dialoglogin->setLayout(layout_config);
158     dialoglogin->exec();
159
160     if ( setting->value("interval",1800).toInt() < 300 )
161         setting->setValue("interval", 300);
162     if ( setting->value("interval",1800).toInt() > 3600 )
163         setting->setValue("interval", 3600);
164
165     if ( setting->value("usegps",false).toBool() ) {
166         if ( setting->value("wait",30).toInt() < 15 )
167             setting->setValue("wait", 15);
168     } else {
169         if ( setting->value("wait",30).toInt() < 5 )
170             setting->setValue("wait", 5);
171     }
172     if ( setting->value("wait",30).toInt() > 120 )
173         setting->setValue("wait", 120);
174
175     set_config();
176     glatitude->reset();
177     gps->refresh();
178
179     mode_latitude();
180 }
181