1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
/***************************************************************************
* *
* This file is part of the Fotowall project, *
* http://www.enricoros.com/opensource/fotowall *
* *
* Copyright (C) 2009 by Enrico Ros <enrico.ros@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "MetaXmlReader.h"
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QTimer>
#include <QUrl>
#define METAXML_BASE_URL "http://www.enricoros.com/opensource/fotowall/.meta"
#define NETWORK_TIMEOUT 10000
/* pre-0.8.0 used this settings:
#define OLD_CHECK_URL "http://code.google.com/p/fotowall/"
#define OLD_MAGIC_TOKEN "Last version is "
#define OLD_DOWNLOADS_URL "http://code.google.com/p/fotowall/downloads/list"
*/
using namespace MetaXml;
Reader_1::Reader_1(const QByteArray & data)
: QXmlStreamReader(data)
{
read();
}
void Reader_1::read()
{
while (!atEnd()) {
readNext();
if (isEndElement())
break;
if (isStartElement()) {
if (name() == QLatin1String("fotowall-meta"))
continue;
else if (name() == QLatin1String("releases"))
readReleases();
else if (name() == QLatin1String("websites"))
readWebsites();
else
readElementText();
}
}
}
void Reader_1::readReleases()
{
releases.clear();
while (!atEnd()) {
readNext();
if (isEndElement())
break;
if (isStartElement()) {
if (name() == QLatin1String("release"))
releases.append(readRelease());
else
readElementText();
}
}
}
Release Reader_1::readRelease()
{
Release r;
r.name = attributes().value("name").toString();
while (!atEnd()) {
readNext();
if (isEndElement())
break;
if (isStartElement()) {
if (name() == QLatin1String("version"))
r.version = readElementText();
else if (name() == QLatin1String("download-url"))
r.url = readElementText();
else
readElementText();
}
}
return r;
}
void Reader_1::readWebsites()
{
websites.clear();
while (!atEnd()) {
readNext();
if (isEndElement())
break;
if (isStartElement()) {
if (name() == QLatin1String("homepage") || name() == QLatin1String("blog") || name() == QLatin1String("site")) {
Website w;
w.name = attributes().value("name").toString();
w.url = readElementText();
if (name() == QLatin1String("homepage"))
w.type = Website::HomePage;
else if (name() == QLatin1String("blog") || w.name.contains("blog", Qt::CaseInsensitive))
w.type = Website::Blog;
else
w.type = Website::Other;
if (name() == QLatin1String("homepage"))
websites.prepend(w);
else
websites.append(w);
} else
readElementText();
}
}
}
/* Connector */
Q_GLOBAL_STATIC(Connector, connectorInstance);
Connector * Connector::instance()
{
return connectorInstance();
}
Connector::Connector()
: m_nam(new QNetworkAccessManager)
, m_reader(0)
{
QNetworkRequest request(QUrl(METAXML_BASE_URL));
QNetworkReply * reply = m_nam->get(request);
connect(reply, SIGNAL(finished()), this, SLOT(slotGotReply()));
QTimer::singleShot(NETWORK_TIMEOUT, this, SLOT(slotTimeOut()));
}
bool Connector::hasDone() const
{
return !m_nam;
}
bool Connector::isValid() const
{
return m_reader;
}
const Reader_1 * Connector::reader() const
{
return m_reader;
}
void Connector::slotGotReply()
{
// dispose the QNAM
if (!m_nam)
return;
m_nam->deleteLater();
m_nam = 0;
// get the data from the network reply
QNetworkReply * reply = static_cast<QNetworkReply *>(sender());
QByteArray replyData = reply->readAll();
QNetworkReply::NetworkError error = reply->error();
reply->deleteLater();
if (error != QNetworkReply::NoError) {
emit fetchError(tr("Network Error"));
return;
}
// parse the data and notify the completion
delete m_reader;
m_reader = new Reader_1(replyData);
emit fetched();
}
void Connector::slotTimeOut()
{
// dispose the QNAM
if (!m_nam)
return;
m_nam->deleteLater();
m_nam = 0;
// emit the error signal
emit fetchError(tr("Network Timeout"));
}
|