kdereview/plasma/dataengines/geolocation

Andrew Coles andrew_coles at yahoo.co.uk
Fri May 1 11:01:40 CEST 2009


SVN commit 961956 by coles:

After chatting to the iplocationtools.com guys, I've managed to get them to make a change to their API so that calling without an IP, rather than failing, returns the visitor's location information.  As such, if we now just go to:

http://iplocationtools.com/ip_query.php

...we get IP geolocation straight away, without needing the 'whatismyip.org' step first to find our world-visible IP.  Three plusses:

- whatismyip was sometimes unreliable
- the whole process is a heck of a lot quicker due to it only being one network job
- the code is much much simpler :) - the diff is only big due to the sheer number of lines removed

Enjoy,

Andrew

CCMAIL: sebas at kde.org
CCMAIL: plasma-devel at kde.org



 M  +11 -51    location_ip.cpp  
 M  +0 -3      location_ip.h  


--- trunk/kdereview/plasma/dataengines/geolocation/location_ip.cpp #961955:961956
@@ -29,7 +29,6 @@
 
 public:
     QString m_data;
-    QString m_ip;
     QXmlStreamReader m_xmlReader;
 
     void populateDataEngineData(Plasma::DataEngine::Data & outd) {
@@ -87,15 +86,18 @@
 
 bool Ip::updateLocation()
 {
-//    kDebug() << "Getting IP data from http://www.whatismyip.org/";
-    KIO::TransferJob *job = KIO::get(KUrl("http://www.whatismyip.org/"),
-                             KIO::NoReload, KIO::HideProgressInfo);
+    d->m_xmlReader.clear();
+    d->m_data.clear();
 
-    if (job) {
-        connect(job, SIGNAL(data(KIO::Job *, const QByteArray &)), this,
-                SLOT(IPreadData(KIO::Job *, const QByteArray &)));
-        connect(job, SIGNAL(result(KJob *)), this, SLOT(IPresult(KJob *)));
-        return true;
+    KIO::TransferJob *datajob = KIO::get(KUrl("http://iplocationtools.com/ip_query.php"),
+					  KIO::NoReload, KIO::HideProgressInfo);
+
+    if (datajob) {
+	kDebug() << "Fetching http://iplocationtools.com/ip_query.php";
+	connect(datajob, SIGNAL(data(KIO::Job *, const QByteArray &)), this,
+			  SLOT(readData(KIO::Job *, const QByteArray &)));
+	connect(datajob, SIGNAL(result(KJob *)), this, SLOT(result(KJob *)));
+	return true;
     }
 
     kDebug() << "Could not create job";
@@ -104,48 +106,6 @@
 
 }
 
-void Ip::IPreadData(KIO::Job* job, const QByteArray& data)
-{
-    Q_UNUSED(job)
-    d->m_data += QString(data);
-}
-
-void Ip::IPresult(KJob *job)
-{
-    if(job && !job->error()) {
-        QStringList lines = d->m_data.split('\n');
-        if (lines.count() == 1) {
-
-//            kDebug() << "Got IP: " + lines[0];
-            d->m_xmlReader.clear();
-
-            d->m_ip = lines[0];
-            KIO::TransferJob *datajob = KIO::get(KUrl("http://iplocationtools.com/ip_query.php?ip="+d->m_ip),
-                                                  KIO::NoReload, KIO::HideProgressInfo);
-
-            d->m_data.clear();
-
-            if (datajob) {
-                kDebug() << "Fetching http://iplocationtools.com/ip_query.php?ip="+d->m_ip;
-                connect(datajob, SIGNAL(data(KIO::Job *, const QByteArray &)), this,
-                                 SLOT(readData(KIO::Job *, const QByteArray &)));
-                connect(datajob, SIGNAL(result(KJob *)), this, SLOT(result(KJob *)));
-                return;
-            }
-//            kDebug() << "Could not spawn job to fetch http://iplocationtools.com/ip_query.php?ip="+d->m_ip;
-
-        } else {
-//            kDebug() << "Too many lines returned from IP source";
-        }
-    } else {
-//        if (job) kDebug() << "Error was " + job->errorString();
-    }
-//    kDebug() << "Error some point prior to fetching IP location information";
-    d->m_data.clear();
-    Plasma::DataEngine::Data outd;
-    emit dataReady(pluginName(), outd);
-}
-
 void Ip::readData(KIO::Job* job, const QByteArray& data)
 {
     Q_UNUSED(job)
--- trunk/kdereview/plasma/dataengines/geolocation/location_ip.h #961955:961956
@@ -35,9 +35,6 @@
     virtual bool updateLocation();
 
 protected slots:
-    void IPreadData(KIO::Job* job, const QByteArray& data);
-    void IPresult(KJob* job);
-
     void readData(KIO::Job *, const QByteArray& data);
     void result(KJob* job);
 


More information about the Plasma-devel mailing list