WeatherService.cpp Example File
soapxml/src/WeatherService.cpp
#include "WeatherService.hpp"
WeatherService::WeatherService(QObject* parent)
: QObject(parent)
, m_succeeded(false)
, m_active(false)
{
bool ok = connect(&m_soap, SIGNAL(responseReady()), SLOT(onServiceResponse()));
Q_ASSERT(ok);
Q_UNUSED(ok);
}
QString WeatherService::temperature() const
{
return m_temperature;
}
QString WeatherService::description() const
{
return m_description;
}
bool WeatherService::succeeded() const
{
return m_succeeded;
}
QString WeatherService::error() const
{
return m_error;
}
bool WeatherService::active() const
{
return m_active;
}
void WeatherService::requestWeatherInformation(const QString &zipCode)
{
if (m_active)
return;
m_active = true;
emit activeChanged();
m_succeeded = false;
m_soap.setAction("http://ws.cdyne.com/WeatherWS/GetCityWeatherByZIP");
m_soap.setHost("wsf.cdyne.com");
QtSoapMessage request;
request.setMethod(QtSoapQName("GetCityWeatherByZIP", "http://ws.cdyne.com/WeatherWS/"));
request.addMethodArgument("ZIP", "http://ws.cdyne.com/WeatherWS/", zipCode);
m_soap.submitRequest(request, "/WeatherWS/Weather.asmx");
}
void WeatherService::reset()
{
m_temperature.clear();
m_description.clear();
emit temperatureChanged();
emit descriptionChanged();
}
void WeatherService::onServiceResponse()
{
const QtSoapMessage& response = m_soap.getResponse();
if (response.isFault()) {
m_error = tr("Query failed: %1").arg(response.faultString().value().toString());
emit statusChanged();
m_active = false;
emit activeChanged();
emit complete();
return;
}
const QtSoapType& responseValue = response.returnValue();
if (!responseValue.isValid()) {
m_error = tr("Query failed: invalid return value");
emit statusChanged();
m_active = false;
emit activeChanged();
emit complete();
return;
}
if (QString::compare("true", responseValue["Success"].toString(), Qt::CaseInsensitive) == 0) {
m_succeeded = true;
m_error.clear();
const int fahrenheit = responseValue["Temperature"].toInt();
const int celsius = (fahrenheit - 32)*(5.0 / 9.0);
m_temperature = QString::fromLatin1("%1 F / %2%3C").arg(fahrenheit).arg(celsius).arg(QChar(0x00B0));
m_description = responseValue["Description"].toString();
emit temperatureChanged();
emit descriptionChanged();
} else {
m_succeeded = false;
m_error = responseValue["ResponseText"].toString();
}
emit statusChanged();
m_active = false;
emit activeChanged();
emit complete();
}