summaryrefslogtreecommitdiffstats
path: root/weather/src/xoapweatherresponse.cpp
blob: 5804bc94ecc2bb02d8716c190006bef37ce144bc (plain)
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
#include "xoapweatherresponse.h"

#include <QDebug>

static inline int strToInt(const QString &str, int defaultValue)
{
    bool ok;
    int result = str.toInt(&ok);
    return ok ? result : defaultValue;
}

static inline QString getNodeContentText(QDomNode node)
{
    if (node.childNodes().count() == 0 || !node.childNodes().at(0).isText())
        return QString();
    return node.childNodes().at(0).nodeValue();
}

void XoapWeatherResponse::Item::read(QDomElement element)
{
    m_id = element.attribute("id");
    m_type = strToInt(element.attribute("type"), 0);
    m_texts.clear();
    m_texts << getNodeContentText(element).split(",", QString::SkipEmptyParts);
}

XoapWeatherResponse::XoapWeatherResponse()
{
}

void XoapWeatherResponse::read(QDomElement element)
{
    m_version = element.attribute("ver");
    for (int i = 0; i < element.childNodes().count(); ++i) {
        if (element.childNodes().at(i).isElement()) {
            Item item;
            item.read(element.childNodes().at(i).toElement());
            m_list.append(item);
        }
    }
}

void XoapWeatherResponse::print()
{
    /*
    qDebug() << m_version << m_list.count() << " items";
    for (int i = 0; i < m_list.count(); ++i)
        qDebug() << "item " << i << " : " << m_list[i].id() << m_list[i].type() << m_list[i].texts();
        */
}