⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 yawpwserv.cpp

📁 KDE4.1 weather plasmoid
💻 CPP
字号:
//// C++ Implementation: yawp//// Description:////// Author: Ruan <ruans@kr8.co.za>, (C) 2008//// Copyright: Do what you want, whenever you want.////#include "yawpwserv.h"#include <QDomDocument>#include <QRegExp>#include <QFile>#include <QDir>#include <QMetaType>#include <kio/netaccess.h>//#include "curl/curl.h"#include <kio/job.h>//-----------------------------------------------------------------------------YaWPWeatherService::YaWPWeatherService(const QString &name) {    m_name = name;    m_days.append(YaWPDay());    m_days.append(YaWPDay());    m_days.append(YaWPDay());    m_days.append(YaWPDay());}//-----------------------------------------------------------------------------YaWPGoogleWeatherService::YaWPGoogleWeatherService() : YaWPWeatherService("google") {}//-----------------------------------------------------------------------------YaWPGoogleWeatherService::~YaWPGoogleWeatherService() {}//-----------------------------------------------------------------------------void YaWPGoogleWeatherService::update(const QString &country, const QString &city) {    m_country = country;    m_city = city;    QString urlFormat;    QString fileName;    QMap<QString, QString> metaData;    m_xmldata.clear();        urlFormat = QString("http://www.google.com/ig/api?hl=en&weather=%1,%2").arg(city).arg(country);    urlFormat = urlFormat.replace(" ", "%20");    KUrl url(urlFormat);        /*NOTE This seams like a bug ? Doing a workarround*/    qRegisterMetaType<KIO::filesize_t>("KIO::filesize_t");    qRegisterMetaType<KIO::MetaData>("KIO::MetaData");    KIO::Job *job = KIO::get( url, KIO::NoReload, KIO::HideProgressInfo );    metaData.insert( "PropagateHttpHeader", "true" );    connect(job, SIGNAL(data( KIO::Job*, const QByteArray&)), this, SLOT( slotData( KIO::Job *, const QByteArray & ) ) );    connect(job, SIGNAL(result(KJob *)), this, SLOT( slotResult(KJob*)));                 job->start();}//-----------------------------------------------------------------------------void YaWPGoogleWeatherService::slotData( KIO::Job *job, const QByteArray &data) {    Q_UNUSED(job);    m_xmldata += data;}//-----------------------------------------------------------------------------void YaWPGoogleWeatherService::slotResult( KJob *job) {    Q_UNUSED(job);    int code = 0;    if(job->error() == 0) {        if(parseInfo(m_xmldata, m_days))            code = SUCCESS;        else            code = DATA_FAILED;    } else {        code = CONNECTION_FAILED;    }    emit completed(this, code);}//-----------------------------------------------------------------------------//Called by updateInfobool YaWPGoogleWeatherService::parseInfo(const QByteArray &data, QVector<YaWPDay> &days) {        bool success = false;    int units = YaWPDay::IMPERIAL;        QDomDocument doc("weatherdoc");    doc.setContent(data);        QDomNodeList nodeList = doc.elementsByTagName("forecast_information");    for(int i = 0; i < nodeList.size(); i++)    {        QDomNode node = nodeList.at(i);        if(node.nodeName() == "forecast_information")        {            node = node.firstChild();            while(!node.isNull()) {                QDomElement element = node.toElement();                if(!element.isNull()) {                    if( element.tagName() == "unit_system") {                        QDomAttr attribute = element.attributeNode("data");                        if(!attribute.isNull())                        {                            if(attribute.value() == "US")                                units = YaWPDay::IMPERIAL;                            else                                units = YaWPDay::METRIC;                        }                    }                }                node = node.nextSibling();            }        }    }    nodeList = doc.elementsByTagName("current_conditions");    for(int i = 0; i < nodeList.size(); i++)    {        QDomNode node = nodeList.at(i);        if(node.nodeName() == "current_conditions")        {            node = node.firstChild();            while(!node.isNull()) {                QDomElement element = node.toElement();                if(!element.isNull()) {                    //SUCCESS                    success = true;                    days[0].setUnits(units);                    if( element.tagName() == "condition") {                        QDomAttr attribute = element.attributeNode("data");                        if(!attribute.isNull()) {                            days[i].setDescription(attribute.value());                            days[i].setEmblem(attribute.value().toLower().replace(" ","-"));                        }                    }                    if( element.tagName() == "temp_f") {                        QDomAttr attribute = element.attributeNode("data");                        if(!attribute.isNull())                            days[0].setCurrent(YaWPDay::convertDegrees(YaWPDay::IMPERIAL, units, attribute.value().toInt()));                    }                    if( element.tagName() == "temp_c") {                        QDomAttr attribute = element.attributeNode("data");                        if(!attribute.isNull())                            days[0].setCurrent(YaWPDay::convertDegrees(YaWPDay::METRIC, units, attribute.value().toInt()));                    }                    if( element.tagName() == "humidity") {                        QDomAttr attribute = element.attributeNode("data");                        if(!attribute.isNull()) {                            QRegExp regx(".*([0-9]*).*");                            if(regx.indexIn(attribute.value()) > -1)                                days[0].setHumidity(regx.cap(1).toInt());                        }                    }                    if( element.tagName() == "wind_condition") {                        QDomAttr attribute = element.attributeNode("data");                        if(!attribute.isNull()) {                            QRegExp regx("(.*:\\s+)([A-Z]*)(\\s+\\w+\\s+)([0-9]*)(.*)");                            if(regx.indexIn(attribute.value()) > -1) {                                days[0].setWindSpeed(regx.cap(4).toInt());                                days[0].setWindDirection(regx.cap(2));                            }                        }                    }                }                node = node.nextSibling();            }        }    }    nodeList = doc.elementsByTagName("forecast_conditions");    for(int i = 0; i < nodeList.size() && i < 4; i++)    {        QDomNode node = nodeList.at(i);        if(node.nodeName() == "forecast_conditions")        {            node = node.firstChild();            while(!node.isNull()) {                QDomElement element = node.toElement();                if(!element.isNull()) {                    days[i].setUnits(units);                    if( element.tagName() == "low") {                        QDomAttr attribute = element.attributeNode("data");                        if(!attribute.isNull())                            days[i].setLow(attribute.value().toInt());                    }                    if( element.tagName() == "high") {                        QDomAttr attribute = element.attributeNode("data");                        if(!attribute.isNull())                            days[i].setHigh(attribute.value().toInt());                    }                    if( element.tagName() == "condition") {                        QDomAttr attribute = element.attributeNode("data");                        if(!attribute.isNull()) {                            days[i].setDescription(attribute.value());                            days[i].setEmblem(attribute.value().toLower().replace(" ","-"));                        }                    }                    if( element.tagName() == "day_of_week") {                        QDomAttr attribute = element.attributeNode("data");                        if(!attribute.isNull()) {                            days[i].setDate(attribute.value());                        }                    }                }                node = node.nextSibling();            }        }    }    return success;}#include "moc_yawpwserv.cpp"

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -