#include <QtCore>#include <QtGui>#include <QtNetwork>#include "download_xml.h"#include "movie.h"#include "list.h"Download_xml::Download_xml(QString givenAddress, QWidget *parent) : QWidget(parent){//OS_Symbian#ifdef Q_OS_SYMBIAN // Set Internet Access Point QNetworkConfigurationManager manager; const bool canStartIAP = manager.capabilities() & QNetworkConfigurationManager::CanStartAndStopInterfaces; // Is there default access point, use it QNetworkConfiguration cfg = manager.defaultConfiguration(); if (!cfg.isValid() || !canStartIAP) { // Available Access Points not found QMessageBox::warning(this, "Error", "No access point"); return; } m_session = new QNetworkSession(cfg); m_session->open(); m_session->waitForOpened();#endif connect(&http, SIGNAL(readyRead(QHttpResponseHeader)), this, SLOT(readData(QHttpResponseHeader))); connect(&http, SIGNAL(requestFinished(int,bool)), this, SLOT(finished(int,bool))); fetch(givenAddress); //fetch("http://rss.news.yahoo.com/rss/yahoonewsroom"); //parseXml(givenAddress); }void Download_xml::fetch(QString address){ xml.clear(); QUrl url(address); http.setHost(url.host()); connectionId = http.get(url.path());}void Download_xml::readData(const QHttpResponseHeader &resp){ if (resp.statusCode() != 200) http.abort(); else { xml.addData(http.readAll()); }}void Download_xml::finished(int id, bool error){ if (error) { QMessageBox::warning(this, "Error", http.errorString()); } else if (id == connectionId) { parseXml(); }}void Download_xml::parseXml(){ bool movies = false; bool rss = false; bool movie = false; bool item = false; QVector<Movie *> movie_list; while (!xml.atEnd() && movies == false && rss == false) { xml.readNext(); if (xml.isStartElement()) { currentTag = xml.name().toString(); if (currentTag == "movieList") movies = true; if (currentTag == "rss") rss = true; } } while (!xml.atEnd() && movies == true) { xml.readNext(); if (xml.isStartElement()) { currentTag = xml.name().toString(); if (currentTag == "movie") movie = true; } else if (xml.isEndElement() && movies == true && movie == true) { if (xml.name() == "movie") { Movie *one_movie = new Movie; one_movie->setName(titleString); one_movie->setDate(dateString); one_movie->setAbout(aboutString); one_movie->setPicture(pictureString); movie_list.append(one_movie); titleString.clear(); dateString.clear(); aboutString.clear(); pictureString.clear(); movie = false; } } else if (xml.isCharacters() && !xml.isWhitespace() && movies == true && movie == true) { if (currentTag == "title") titleString += xml.text().toString(); else if (currentTag == "globalReleaseDate") dateString += xml.text().toString(); else if (currentTag == "annotation") aboutString += xml.text().toString(); else if (currentTag == "imageType1") pictureString += xml.text().toString(); } } while (!xml.atEnd() && rss == true) { xml.readNext(); if (xml.isStartElement()) { currentTag = xml.name().toString(); if (currentTag == "item") item = true; } else if (xml.isEndElement() && rss == true && item == true) { if (xml.name() == "item") { Movie *one_movie = new Movie; one_movie->setName(titleString); one_movie->setDate(dateString); one_movie->setAbout(aboutString); movie_list.append(one_movie); titleString.clear(); dateString.clear(); aboutString.clear(); item = false; } } else if (xml.isCharacters() && !xml.isWhitespace() && rss == true && item == true) { if (currentTag == "title") titleString += xml.text().toString(); else if (currentTag == "pubDate") dateString += xml.text().toString(); else if (currentTag == "description") aboutString += xml.text().toString(); } } if (xml.error() && xml.error() != QXmlStreamReader::PrematureEndOfDocumentError) { qWarning() << "XML ERROR:" << xml.lineNumber() << ": " << xml.errorString(); http.abort(); } _ml = movie_list;}Download_xml::~Download_xml(){}
#include <QtCore>#include <QtGui>#include <QtNetwork>#include "list.h"#include "custom_widget.h"#include "movie.h"#include "download_xml.h"List::List(QWidget *parent) : QWidget(parent){//List create listWidget = new QListWidget(this); QString str = "http://www.forumcinemas.lv/rss/xml/movies/"; Download_xml *Nado = new Download_xml(str); QVector<Movie *> new_ml = Nado->ml(); for (int i = 0; i<new_ml.size(); i++) { listWidget->insertItem(i, ""); listWidget->setItemWidget(listWidget->item(i), new Custom_Widget (new_ml[i]->name(), new_ml[i]->date(), new_ml[i]->about(), new_ml[i]->picture())); listWidget->item(i)->setSizeHint(QSize (350,470)); listWidget->setSpacing(1); } //QVector<Movie *> new_ml = Nado->parseXml(str); QVBoxLayout *layout = new QVBoxLayout(this); QHBoxLayout *hboxLayout = new QHBoxLayout; hboxLayout->addWidget(listWidget); layout->addLayout(hboxLayout); }
connect(&http, SIGNAL(readyRead(QHttpResponseHeader)), this, SLOT(readData(QHttpResponseHeader))); connect(&http, SIGNAL(requestFinished(int,bool)), this, SLOT(finished(int,bool)));
#ifndef DOWNLOAD_XML_H#define DOWNLOAD_XML_H#include <QHttp>#include <QWidget>#include <QBuffer>#include <QXmlStreamReader>#include "movie.h"#ifdef Q_OS_SYMBIAN// Bearer#include <QNetworkConfigurationManager>#include <QNetworkSession>#include <QPointer>// QtMobility namespaceQTM_USE_NAMESPACE#endifclass Download_xml : public QWidget{ Q_OBJECTpublic: Download_xml(QString givenAddress, QWidget *parent = 0); ~Download_xml(); QVector<Movie *> _ml;public slots: void fetch(QString address); void finished(int id, bool error); void readData(const QHttpResponseHeader &); void parseXml(); QVector<Movie *> ml() { return _ml; }signals: void finish();private: QXmlStreamReader xml; QHttp http; int connectionId; // FOR XML QString currentTag; QString titleString; QString dateString; QString aboutString; QString pictureString;#ifdef Q_OS_SYMBIAN // for bearer management QPointer<QNetworkSession> m_session;#endif};#endif // DOWNLOAD_XML_H