• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

marble

  • sources
  • kde-4.12
  • kdeedu
  • marble
  • src
  • plugins
  • runner
  • hostip
HostipRunner.cpp
Go to the documentation of this file.
1 //
2 // This file is part of the Marble Virtual Globe.
3 //
4 // This program is free software licensed under the GNU LGPL. You can
5 // find a copy of this license in LICENSE.txt in the top directory of
6 // the source code.
7 //
8 // Copyright 2010 Dennis Nienhüser <earthwings@gentoo.org>
9 
10 #include "HostipRunner.h"
11 
12 #include "MarbleDebug.h"
13 #include "GeoDataDocument.h"
14 #include "GeoDataFeature.h"
15 #include "GeoDataPlacemark.h"
16 
17 #include <QString>
18 #include <QTimer>
19 #include <QVector>
20 #include <QUrl>
21 #include <QNetworkReply>
22 
23 namespace Marble
24 {
25 
26 HostipRunner::HostipRunner( QObject *parent ) :
27  SearchRunner( parent ),
28  m_networkAccessManager()
29 {
30  connect( &m_networkAccessManager, SIGNAL(finished(QNetworkReply*)),
31  this, SLOT(slotRequestFinished(QNetworkReply*)), Qt::DirectConnection );
32 }
33 
34 HostipRunner::~HostipRunner()
35 {
36 }
37 
38 void HostipRunner::slotNoResults()
39 {
40  emit searchFinished( QVector<GeoDataPlacemark*>() );
41 }
42 
43 void HostipRunner::search( const QString &searchTerm, const GeoDataLatLonAltBox & )
44 {
45  if( !searchTerm.contains('.') ) {
46  // Simple IP/hostname heuristic to avoid requests not needed:
47  // String must contain at least one dot.
48  slotNoResults();
49  }
50  else {
51  QEventLoop eventLoop;
52 
53  QTimer timer;
54  timer.setSingleShot( true );
55  timer.setInterval( 15000 );
56 
57  connect( &timer, SIGNAL(timeout()),
58  &eventLoop, SLOT(quit()));
59  connect( this, SIGNAL(searchFinished(QVector<GeoDataPlacemark*>)),
60  &eventLoop, SLOT(quit()) );
61 
62  // Lookup the IP address for a hostname, or the hostname if an IP address was given
63  QHostInfo ::lookupHost( searchTerm, this, SLOT(slotLookupFinished(QHostInfo)));
64  timer.start();
65 
66  eventLoop.exec();
67  }
68 }
69 
70 void HostipRunner::slotLookupFinished(const QHostInfo &info)
71 {
72  if ( !info.addresses().isEmpty() ) {
73  m_hostInfo = info;
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 ) );
77 
78  // @todo FIXME Must currently be done in the main thread, see bug 257376
79  QTimer::singleShot( 0, this, SLOT(get()) );
80  }
81  else
82  slotNoResults();
83 }
84 
85 void HostipRunner::get()
86 {
87  QNetworkReply *reply = m_networkAccessManager.get( m_request );
88  connect( reply, SIGNAL(error(QNetworkReply::NetworkError)),
89  this, SLOT(slotNoResults()), Qt::DirectConnection );
90 }
91 
92 void HostipRunner::slotRequestFinished( QNetworkReply* reply )
93 {
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();
99  }
100 
101  QString latInd = "Latitude: ";
102  if (line.startsWith( latInd) ) {
103  lat = line.mid( latInd.length() ).toDouble();
104  }
105  }
106 
107  QVector<GeoDataPlacemark*> placemarks;
108 
109  if (lon != 0.0 && lat != 0.0) {
110  GeoDataPlacemark *placemark = new GeoDataPlacemark;
111 
112  placemark->setName( m_hostInfo.hostName() );
113 
114  QString description("%1 (%2)");
115  placemark->setDescription( description.
116  arg( m_hostInfo.hostName() ).
117  arg( m_hostInfo.addresses().first().toString() ) );
118 
119  placemark->setCoordinate( lon * DEG2RAD, lat * DEG2RAD );
120  placemark->setVisualCategory( GeoDataFeature::Coordinate );
121  placemarks << placemark;
122  }
123 
124  emit searchFinished( placemarks );
125 }
126 
127 } // namespace Marble
128 
129 #include "HostipRunner.moc"
GeoDataDocument.h
Marble::SearchRunner::searchFinished
void searchFinished(QVector< GeoDataPlacemark * > result)
This is emitted to indicate that the runner has finished the placemark search.
Marble::GeoDataFeature::Coordinate
Definition: GeoDataFeature.h:123
QObject
MarbleDebug.h
GeoDataFeature.h
HostipRunner.h
GeoDataPlacemark.h
Marble::DEG2RAD
const qreal DEG2RAD
Definition: MarbleGlobal.h:200
Marble::HostipRunner::HostipRunner
HostipRunner(QObject *parent=0)
Definition: HostipRunner.cpp:26
Marble::HostipRunner::~HostipRunner
~HostipRunner()
Definition: HostipRunner.cpp:34
Marble::SearchRunner
Definition: SearchRunner.h:30
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:38:50 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

marble

Skip menu "marble"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal