File indexing completed on 2024-04-28 15:11:24

0001 
0002 #include "skyobjdescription.h"
0003 
0004 #include <QString>
0005 #include <QUrl>
0006 #include <QDebug>
0007 
0008 SkyObjDescription::SkyObjDescription(const QString so_Name, const QString so_Type)
0009     : soName(so_Name), soType(so_Type), m_description(""), m_DownloadedData("")
0010 {
0011     QUrl wikiUrl("http://en.wikipedia.org/w/api.php?action=opensearch&search=" + soName.replace(' ', '_').toLower() +
0012                  '_' + soType.toLower() + "&format=xml&limit=1.xml");
0013 
0014     QNetworkRequest request(wikiUrl);
0015 
0016     manager = new QNetworkAccessManager(this);
0017     connect(manager, SIGNAL(finished(QNetworkReply*)), SLOT(fileDownloaded(QNetworkReply*)));
0018     manager->get(request);
0019 }
0020 
0021 void SkyObjDescription::fileDownloaded(QNetworkReply *reply)
0022 {
0023     m_DownloadedData = reply->readAll();
0024 
0025     if (!m_DownloadedData.isEmpty())
0026     {
0027         QString data(m_DownloadedData);
0028 
0029         const QString descOpeing("<Description xml:space=\"preserve\">");
0030         const QString descClosing("</Description>");
0031         // retrieving description from received data
0032         if (data.contains("description", Qt::CaseInsensitive))
0033         {
0034             int startIndex = data.lastIndexOf(descOpeing) + descOpeing.length();
0035             int endIndex   = data.lastIndexOf(descClosing);
0036             m_description  = data.mid(startIndex, endIndex - startIndex);
0037         }
0038 
0039         const QString urlOpening("<Url xml:space=\"preserve\">");
0040         const QString urlClosing("</Url>");
0041         // retrieving link of wikipedia page from received data
0042         if (data.contains(urlOpening, Qt::CaseInsensitive))
0043         {
0044             int startIndex = data.lastIndexOf(urlOpening) + urlOpening.length();
0045             int endIndex   = data.lastIndexOf(urlClosing);
0046             m_url          = data.mid(startIndex, endIndex - startIndex);
0047         }
0048     }
0049     reply->deleteLater();
0050     emit downloaded();
0051 }