~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/generic/dataengines/geolocation/location_ip.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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.
 
6
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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/>.
 
19
 */
 
20
 
 
21
#include "location_ip.h"
 
22
#include <KDebug>
 
23
#include <KJob>
 
24
#include <KIO/Job>
 
25
#include <KIO/TransferJob>
 
26
 
 
27
#include <QtXml/QXmlStreamReader>
 
28
 
 
29
class Ip::Private : public QObject {
 
30
 
 
31
public:
 
32
    QXmlStreamReader m_xmlReader;
 
33
 
 
34
    void populateDataEngineData(Plasma::DataEngine::Data & outd)
 
35
    {
 
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") {
 
40
                break;
 
41
            }
 
42
 
 
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();
 
59
                }
 
60
            }
 
61
        }
 
62
 
 
63
        // ordering of first three to preserve backwards compatibility
 
64
 
 
65
        outd["accuracy"] = 40000;
 
66
        outd["country"] = country;
 
67
        outd["country code"] = countryCode;
 
68
        outd["city"] = city;
 
69
        outd["latitude"] = latitude;
 
70
        outd["longitude"] = longitude;
 
71
 
 
72
    }
 
73
 
 
74
};
 
75
 
 
76
Ip::Ip(QObject* parent, const QVariantList& args)
 
77
    : GeolocationProvider(parent, args), d(new Private())
 
78
{
 
79
    setUpdateTriggers(SourceEvent | NetworkConnected);
 
80
}
 
81
 
 
82
Ip::~Ip()
 
83
{
 
84
    delete d;
 
85
}
 
86
 
 
87
void Ip::update()
 
88
{
 
89
    d->m_xmlReader.clear();
 
90
 
 
91
    KIO::TransferJob *datajob = KIO::get(KUrl("http://ipinfodb.com/ip_query.php"),
 
92
                                         KIO::NoReload, KIO::HideProgressInfo);
 
93
 
 
94
    if (datajob) {
 
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 *)));
 
99
    } else {
 
100
        kDebug() << "Could not create job";
 
101
    }
 
102
}
 
103
 
 
104
void Ip::readData(KIO::Job* job, const QByteArray& data)
 
105
{
 
106
    Q_UNUSED(job)
 
107
 
 
108
    if (data.isEmpty()) {
 
109
        return;
 
110
    }
 
111
 
 
112
    d->m_xmlReader.addData(data);
 
113
}
 
114
 
 
115
void Ip::result(KJob* job)
 
116
{
 
117
    Plasma::DataEngine::Data outd;
 
118
 
 
119
    if(job && !job->error()) {
 
120
 
 
121
        while (!d->m_xmlReader.atEnd()) {
 
122
            d->m_xmlReader.readNext();
 
123
 
 
124
            if (d->m_xmlReader.isStartElement()) {
 
125
                if (d->m_xmlReader.name() == "Response") {
 
126
                    d->populateDataEngineData(outd);
 
127
                    break;
 
128
                }
 
129
            }
 
130
        }
 
131
//        kDebug() << "Done reading XML";
 
132
    } else {
 
133
        kDebug() << "error";
 
134
    }
 
135
 
 
136
    setData(outd);
 
137
}
 
138
 
 
139
K_EXPORT_PLASMA_GEOLOCATIONPROVIDER(ip, Ip)
 
140
 
 
141
#include "location_ip.moc"