2
* Copyright (C) 2009 Petri Damstén <damu@iki.fi>
3
* - Original Implementation.
4
* 2009 Andrew Coles <andrew.coles@yahoo.co.uk>
5
* - Extension to iplocationtools engine.
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU General Public License as
9
* published by the Free Software Foundation; either version 2 of
10
* the License, or (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21
#include "location_ip.h"
25
#include <KIO/TransferJob>
27
#include <QtXml/QXmlStreamReader>
29
class Ip::Private : public QObject {
32
QXmlStreamReader m_xmlReader;
34
void populateDataEngineData(Plasma::DataEngine::Data & outd)
36
QString country, countryCode, city, latitude, longitude;
37
while (!m_xmlReader.atEnd()) {
38
m_xmlReader.readNext();
39
if (m_xmlReader.isEndElement() && m_xmlReader.name() == "Response") {
43
if (m_xmlReader.isStartElement()) {
44
if (m_xmlReader.name() == "Status") {
45
QString tmp = m_xmlReader.readElementText();
46
if (tmp != "OK") return;
47
} else if (m_xmlReader.name() == "CountryCode") {
48
countryCode = m_xmlReader.readElementText();
49
} else if (m_xmlReader.name() == "CountryName") {
50
country = m_xmlReader.readElementText();
51
} else if (m_xmlReader.name() == "City") {
52
city = m_xmlReader.readElementText();
53
} else if (m_xmlReader.name() == "Latitude") {
54
latitude = m_xmlReader.readElementText();
55
} else if (m_xmlReader.name() == "Longitude") {
56
longitude = m_xmlReader.readElementText();
57
} else { // for fields such as 'IP'...
58
m_xmlReader.readElementText();
63
// ordering of first three to preserve backwards compatibility
65
outd["accuracy"] = 40000;
66
outd["country"] = country;
67
outd["country code"] = countryCode;
69
outd["latitude"] = latitude;
70
outd["longitude"] = longitude;
76
Ip::Ip(QObject* parent, const QVariantList& args)
77
: GeolocationProvider(parent, args), d(new Private())
79
setUpdateTriggers(SourceEvent | NetworkConnected);
89
d->m_xmlReader.clear();
91
KIO::TransferJob *datajob = KIO::get(KUrl("http://ipinfodb.com/ip_query.php"),
92
KIO::NoReload, KIO::HideProgressInfo);
95
kDebug() << "Fetching http://iplocationtools.com/ip_query.php";
96
connect(datajob, SIGNAL(data(KIO::Job *, const QByteArray &)), this,
97
SLOT(readData(KIO::Job *, const QByteArray &)));
98
connect(datajob, SIGNAL(result(KJob *)), this, SLOT(result(KJob *)));
100
kDebug() << "Could not create job";
104
void Ip::readData(KIO::Job* job, const QByteArray& data)
108
if (data.isEmpty()) {
112
d->m_xmlReader.addData(data);
115
void Ip::result(KJob* job)
117
Plasma::DataEngine::Data outd;
119
if(job && !job->error()) {
121
while (!d->m_xmlReader.atEnd()) {
122
d->m_xmlReader.readNext();
124
if (d->m_xmlReader.isStartElement()) {
125
if (d->m_xmlReader.name() == "Response") {
126
d->populateDataEngineData(outd);
131
// kDebug() << "Done reading XML";
139
K_EXPORT_PLASMA_GEOLOCATIONPROVIDER(ip, Ip)
141
#include "location_ip.moc"