21 #include <QNetworkReply>
28 m_networkAccessManager()
30 connect( &m_networkAccessManager, SIGNAL(finished(QNetworkReply*)),
31 this, SLOT(slotRequestFinished(QNetworkReply*)), Qt::DirectConnection );
38 void HostipRunner::slotNoResults()
43 void HostipRunner::search(
const QString &searchTerm,
const GeoDataLatLonAltBox & )
45 if( !searchTerm.contains(
'.') ) {
54 timer.setSingleShot(
true );
55 timer.setInterval( 15000 );
57 connect( &timer, SIGNAL(timeout()),
58 &eventLoop, SLOT(quit()));
60 &eventLoop, SLOT(quit()) );
63 QHostInfo ::lookupHost( searchTerm,
this, SLOT(slotLookupFinished(QHostInfo)));
70 void HostipRunner::slotLookupFinished(
const QHostInfo &info)
72 if ( !info.addresses().isEmpty() ) {
74 QString hostAddress = info.addresses().first().toString();
75 QString query = QString(
"http://api.hostip.info/get_html.php?ip=%1&position=true" ).arg( hostAddress );
76 m_request.setUrl( QUrl( query ) );
79 QTimer::singleShot( 0,
this, SLOT(
get()) );
85 void HostipRunner::get()
87 QNetworkReply *reply = m_networkAccessManager.get( m_request );
88 connect( reply, SIGNAL(error(QNetworkReply::NetworkError)),
89 this, SLOT(slotNoResults()), Qt::DirectConnection );
92 void HostipRunner::slotRequestFinished( QNetworkReply* reply )
94 double lon(0.0), lat(0.0);
95 for ( QString line = reply->readLine(); !line.isEmpty(); line = reply->readLine() ) {
96 QString lonInd =
"Longitude: ";
97 if ( line.startsWith(lonInd) ) {
98 lon = line.mid( lonInd.length() ).toDouble();
101 QString latInd =
"Latitude: ";
102 if (line.startsWith( latInd) ) {
103 lat = line.mid( latInd.length() ).toDouble();
107 QVector<GeoDataPlacemark*> placemarks;
109 if (lon != 0.0 && lat != 0.0) {
110 GeoDataPlacemark *placemark =
new GeoDataPlacemark;
112 placemark->setName( m_hostInfo.hostName() );
114 QString description(
"%1 (%2)");
115 placemark->setDescription( description.
116 arg( m_hostInfo.hostName() ).
117 arg( m_hostInfo.addresses().first().toString() ) );
119 placemark->setCoordinate( lon *
DEG2RAD, lat * DEG2RAD );
121 placemarks << placemark;
129 #include "HostipRunner.moc"
void searchFinished(QVector< GeoDataPlacemark * > result)
This is emitted to indicate that the runner has finished the placemark search.
HostipRunner(QObject *parent=0)