Kstars

skyobjdescription.cpp
1 
2 #include "skyobjdescription.h"
3 
4 #include <QString>
5 #include <QUrl>
6 #include <QDebug>
7 
9  : soName(so_Name), soType(so_Type), m_description(""), m_DownloadedData("")
10 {
11  QUrl wikiUrl("http://en.wikipedia.org/w/api.php?action=opensearch&search=" + soName.replace(' ', '_').toLower() +
12  '_' + soType.toLower() + "&format=xml&limit=1.xml");
13 
14  QNetworkRequest request(wikiUrl);
15 
16  manager = new QNetworkAccessManager(this);
17  connect(manager, SIGNAL(finished(QNetworkReply*)), SLOT(fileDownloaded(QNetworkReply*)));
18  manager->get(request);
19 }
20 
21 void SkyObjDescription::fileDownloaded(QNetworkReply *reply)
22 {
23  m_DownloadedData = reply->readAll();
24 
25  if (!m_DownloadedData.isEmpty())
26  {
27  QString data(m_DownloadedData);
28 
29  const QString descOpeing("<Description xml:space=\"preserve\">");
30  const QString descClosing("</Description>");
31  // retrieving description from received data
32  if (data.contains("description", Qt::CaseInsensitive))
33  {
34  int startIndex = data.lastIndexOf(descOpeing) + descOpeing.length();
35  int endIndex = data.lastIndexOf(descClosing);
36  m_description = data.mid(startIndex, endIndex - startIndex);
37  }
38 
39  const QString urlOpening("<Url xml:space=\"preserve\">");
40  const QString urlClosing("</Url>");
41  // retrieving link of wikipedia page from received data
42  if (data.contains(urlOpening, Qt::CaseInsensitive))
43  {
44  int startIndex = data.lastIndexOf(urlOpening) + urlOpening.length();
45  int endIndex = data.lastIndexOf(urlClosing);
46  m_url = data.mid(startIndex, endIndex - startIndex);
47  }
48  }
49  reply->deleteLater();
50  emit downloaded();
51 }
CaseInsensitive
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QNetworkReply * get(const QNetworkRequest &request)
void deleteLater()
QString & replace(int position, int n, QChar after)
QString toLower() const const
bool isEmpty() const const
SkyObjDescription(const QString soName, const QString soType)
Constructor sends request to network for data from wikipedia API and starts downloading data from QUr...
QByteArray readAll()
QString mid(int position, int n) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Sep 27 2023 04:02:14 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.