expxmldownloader.cpp
119 lines
| 2.7 KiB
| text/x-c
|
CppLexer
r5 | #include "expxmldownloader.h" | |||
#include <QDebug> | ||||
#include <QFile> | ||||
#include <QQueue> | ||||
#include <QDate> | ||||
#include <QDateTime> | ||||
ExpXmlDownLoader::ExpXmlDownLoader(QObject *parent) : QObject(parent) | ||||
{ | ||||
this->m_doc = NULL; | ||||
} | ||||
ExpXmlDownLoader::~ExpXmlDownLoader() | ||||
{ | ||||
} | ||||
bool ExpXmlDownLoader::parseXml(const QString &fileName) | ||||
{ | ||||
QQueue<QString> nodes; | ||||
nodes<<"RESOURCE"<<"TABLE"<<"DATA"<<"TABLEDATA"; | ||||
if(m_doc!=NULL) | ||||
delete m_doc; | ||||
m_doc = new QDomDocument(fileName); | ||||
QFile file(fileName); | ||||
if (!file.open(QIODevice::ReadOnly)) | ||||
return false; | ||||
if (!m_doc->setContent(&file)) { | ||||
file.close(); | ||||
return false; | ||||
} | ||||
file.close(); | ||||
if(m_doc->isNull()) | ||||
{ | ||||
delete m_doc; | ||||
return false; | ||||
} | ||||
QDomElement docElem = m_doc->documentElement(); | ||||
QDomNode n = docElem.firstChild(); | ||||
while(!n.isNull() && !nodes.empty()) | ||||
{ | ||||
if(n.toElement().tagName()==nodes.head()) | ||||
{ | ||||
nodes.dequeue(); | ||||
n=n.firstChild(); | ||||
} | ||||
else | ||||
{ | ||||
n = n.nextSibling(); | ||||
} | ||||
} | ||||
extractIntervals(n); | ||||
makeDownloadList(); | ||||
return true; | ||||
} | ||||
const QList<QDate> &ExpXmlDownLoader::daysToDownload() | ||||
{ | ||||
return m_FilesToDownload; | ||||
} | ||||
void ExpXmlDownLoader::extractIntervals(QDomNode n) | ||||
{ | ||||
QDomNode inter; | ||||
m_intervals.clear(); | ||||
while(!n.isNull()) | ||||
{ | ||||
if(n.toElement().tagName()=="TR") | ||||
{ | ||||
ExpXmlDownLoaderIntervals interval; | ||||
inter=n.firstChild(); | ||||
if(inter.toElement().tagName()=="TD") | ||||
{ | ||||
interval.start = inter.toElement().text(); | ||||
} | ||||
inter=inter.nextSibling(); | ||||
if(inter.toElement().tagName()=="TD") | ||||
{ | ||||
interval.stop = inter.toElement().text(); | ||||
} | ||||
m_intervals.append(interval); | ||||
} | ||||
n = n.nextSibling(); | ||||
} | ||||
} | ||||
void ExpXmlDownLoader::makeDownloadList() | ||||
{ | ||||
for(int i=0;i<m_intervals.count();i++) | ||||
{ | ||||
QList<QDate> daysToDl; | ||||
QDateTime start,stop; | ||||
start=start.fromString(m_intervals[i].start,Qt::ISODate); | ||||
stop=stop.fromString(m_intervals[i].stop,Qt::ISODate); | ||||
int days=start.daysTo(stop); | ||||
daysToDl.append(start.date()); | ||||
if(days) | ||||
{ | ||||
for(int j=0;j<days;j++) | ||||
{ | ||||
daysToDl.append(start.addDays(j+1).date()); | ||||
} | ||||
} | ||||
addDaysToDownload(daysToDl); | ||||
} | ||||
qDebug()<<m_FilesToDownload.count(); | ||||
} | ||||
void ExpXmlDownLoader::addDaysToDownload(QList<QDate> days) | ||||
{ | ||||
for(int i=0;i<days.count();i++) | ||||
{ | ||||
if(!m_FilesToDownload.contains(days.at(i))) | ||||
{ | ||||
m_FilesToDownload.append(days.at(i)); | ||||
} | ||||
} | ||||
} | ||||