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
21void 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}
SkyObjDescription(const QString soName, const QString soType)
Constructor sends request to network for data from wikipedia API and starts downloading data from QUr...
bool isEmpty() const const
QByteArray readAll()
QNetworkReply * get(const QNetworkRequest &request)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
QString mid(qsizetype position, qsizetype n) const const
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
QString toLower() const const
CaseInsensitive
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:57:49 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.