X-Git-Url: http://vcs.maemo.org/git/?a=blobdiff_plain;f=src%2Fqtmadsservice.cpp;fp=src%2Fqtmadsservice.cpp;h=47fb5d7c219c0651d847372c90524d37714e6d87;hb=e494cd05f1809789fd03d888c5592753d04f2031;hp=0000000000000000000000000000000000000000;hpb=4c87a3c868d40870d0eed21fb6e01c014acd3062;p=qtmads diff --git a/src/qtmadsservice.cpp b/src/qtmadsservice.cpp new file mode 100644 index 0000000..47fb5d7 --- /dev/null +++ b/src/qtmadsservice.cpp @@ -0,0 +1,320 @@ +/* + * Copyright (c) 2009 Eetu Lehmusvuo. + * + * This file is part of QtMAds. + * + * QtMAds is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * QtMAds is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with QtMAds. If not, see . + * + */ + +#include "qtmadsservice.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +QtmadsService::QtmadsService() +{ +} + +QtmadsService::~QtmadsService() +{ + http->close(); + delete http; +} + +bool QtmadsService::initializeService(QString service, quint32 adGroup, AdType defaultAdType) +{ + this->serviceName = service; + this->adGroupId = adGroup; + this->defAdType = defaultAdType; + + bool parseSuccess = true; + // ../plugins/SERVICE_NAME_adconfigure.xml + QFile file(CONF_PATH + this->serviceName + CONF_FILE_POSTFIX ); + parseSuccess = file.exists(); + + /*if(!file.exists()){ + // ./default_adconfigure.xml + file.setFileName("." + CONF_FILE); + parseSuccess = file.exists(); + }*/ + + if(parseSuccess){ + file.open(QIODevice::ReadOnly); + this->parseXmlConfFile(file); + } + + if(parseSuccess){ + initialized = true; + }else{ + qDebug() << "Ad service configure failed. " << CONF_PATH + this->serviceName + CONF_FILE_POSTFIX ; + initialized = false; + //TODO: parse failed + } + + return isInitialized(); +} + +bool QtmadsService::isInitialized() +{ + return initialized; +} + +void QtmadsService::getAd(QHash &adParameters) +{ + //qDebug() << "QtmadsService::getAd()"; + if(isInitialized()){ + QList > parameters; + this->parseUrlParameters(adParameters, parameters); + + QUrl url(this->serviceUrl); + url.setQueryItems(parameters); + QByteArray path = QUrl::toPercentEncoding(url.path(), "!$&'()*+,;=:@/?"); + if (path.isEmpty()) + path = "/"; + path.append(QUrl::toPercentEncoding("?", "!$&'()*+,;=:@/?")); + path.append(url.encodedQuery()); + + http = new QHttp(this); + connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(adRequestFinished(int, bool))); + http->setHost(url.host()); + //qDebug() << "Url: " << url.toString(); + adTrasactionId = http->get(path); + }else{ + emit adRequestFailed(); + } +} + + +void QtmadsService::adRequestFinished(int transactionId, bool error) +{ + //qDebug() << "adRequestFinished()"; + if(adTrasactionId != transactionId){ + return; + } + adTrasactionId = -1; + + if(!error){ + QByteArray data = http->readAll(); + + qDebug() << "Data: " << data; + + QHash adParams;// = new QHash(); + this->parseReceivedAd(data, adParams); + QHash::iterator i; + + + /*for(i = adParams.begin(); i != adParams.end(); i++){ + qDebug() << "key: " << i.key() << " value: " << i.value().toString(); + }*/ + + emit adRequestReady(adParams); + }else{ + qDebug() << "adRequestFailed()"; + emit adRequestFailed(); + } + + disconnect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(adRequestFinished(int, bool))); +} + +void QtmadsService::getRemoteImage(QString urlString) +{ + QUrl url(urlString); + QByteArray path = QUrl::toPercentEncoding(url.path(), "!$&'()*+,;=:@/?"); + if (path.isEmpty()) + path = "/"; + + connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(imageRequestFinished(int, bool))); + http->setHost(url.host()); + imageTrasactionId = http->get(path); +} + + +void QtmadsService::imageRequestFinished(int transactionId, bool error) +{ + if(imageTrasactionId != transactionId){ + return; + } + imageTrasactionId = -1; + + if(!error){ + QImage *image = new QImage(); + if(image->loadFromData(http->readAll())){ + emit imageRequestReady(image); + delete image; + }else{ + delete image; + emit imageRequestFailed(); + } + } + + disconnect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(imageRequestFinished(int, bool))); +} + + +void QtmadsService::setDefaultAdType(AdType type) +{ + this->defAdType = type; +} + +AdType QtmadsService::defaultAdType() +{ + return this->defAdType; +} + +bool QtmadsService::parseXmlConfFile(QFile &file) +{ + qDebug() << "QtmadsService::parseXmlConfFile()"; + bool success = false; + QXmlStreamReader xmlReader(&file); + + while (!xmlReader.atEnd()) { + // find right servicename tag + if(QXmlStreamReader::StartElement == xmlReader.readNext() + && 0 == xmlReader.name().compare(TAG_SERVICE) + && 0 == xmlReader.attributes().value("name").compare(this->serviceName)){ + this->serviceUrl.clear(); + this->serviceUrl.append(xmlReader.attributes().value("url")); + + // until + while (!(QXmlStreamReader::EndElement == xmlReader.readNext() + && 0 == xmlReader.name().compare(TAG_SERVICE))) { + success = true; + + if(QXmlStreamReader::StartElement == xmlReader.tokenType()){ + if(0 == xmlReader.name().compare(TAG_ACCOUNTID)){ + parameterTags.insert( + QString(TAG_ACCOUNTID),xmlReader.attributes().value(TAG_KEY).toString()); + this->accountId.clear(); + this->accountId.append(xmlReader.attributes().value(TAG_VALUE)); + }else if(0 == xmlReader.name().compare(TAG_PASSWORD)){ + parameterTags.insert( + QString(TAG_PASSWORD),xmlReader.attributes().value(TAG_KEY).toString()); + this->password.clear(); + this->password.append(xmlReader.attributes().value(TAG_VALUE)); + }else if(0 == xmlReader.name().compare(TAG_CAMPAIGN)){ + + if(0 == xmlReader.attributes().value("id").compare(QString("%1").arg(this->adGroupId))){ + // until + while(!(QXmlStreamReader::EndElement == xmlReader.readNext() + && 0 == xmlReader.name().compare(TAG_CAMPAIGN))){ + if(QXmlStreamReader::StartElement == xmlReader.tokenType()){ + /*qDebug() << "name: " << xmlReader.name() + << "key: " << xmlReader.attributes().value(TAG_KEY).toString() + << "value: " << xmlReader.attributes().value(TAG_VALUE).toString();*/ + if(0 == xmlReader.name().compare(TAG_ADTYPE)){ + parameterTags.insert( + QString(TAG_ADTYPE),xmlReader.attributes().value(TAG_KEY).toString()); + this->defAdType = this->getAdTypeFromString(xmlReader.attributes().value(TAG_VALUE).toString()); + + }else if(0 == xmlReader.name().compare(TAG_LANGUAGE)){ + parameterTags.insert( + QString(TAG_LANGUAGE),xmlReader.attributes().value(TAG_KEY).toString()); + this->language = xmlReader.attributes().value(TAG_VALUE).toString(); + + }else if(0 == xmlReader.name().compare(TAG_TRANSACTIONID)){ + parameterTags.insert( + QString(TAG_TRANSACTIONID),xmlReader.attributes().value(TAG_KEY).toString()); + this->transactionId = xmlReader.attributes().value(TAG_VALUE).toString(); + + }else{ + qDebug() << "Unknown element: " << xmlReader.tokenString(); + if(xmlReader.atEnd()){ + // TODO: throw exception? + qDebug() << "Unexpected end of document!"; + return false; + } + + } + } + } + } + }else{ + qDebug() << "Unknown element: " << xmlReader.tokenString(); + if(xmlReader.atEnd()){ + // TODO: throw exception? + qDebug() << "Unexpected end of document!"; + return false; + } + } + } + } + } + } + + /*while (!xmlReader.atEnd()) { + qDebug() << "Type: "<< xmlReader.readNext(); + qDebug() << " Tokenstring: " << xmlReader.tokenString(); + qDebug() << " name: " << xmlReader.name(); //<< " key: " << xmlReader.attributes().value("key") << " value: " << xmlReader.attributes().value("value"); + qDebug() << " "; + }*/ + + return success; +} + +AdType QtmadsService::getAdTypeFromString(QString adStr) +{ + AdType adType = anyAd; + + if(0 == adStr.compare("txtAd")){ + adType = txtAd; + }else if(0 == adStr.compare("imageAd")){ + adType = imageAd; + }else if(0 == adStr.compare("txtBannerAd")){ + adType = txtBannerAd; + }else if(0 == adStr.compare("imageBannerAd")){ + adType = imageBannerAd; + }else if(0 == adStr.compare("audioAd")){ + adType = audioAd; + }else if(0 == adStr.compare("videoAd")){ + adType = videoAd; + } + return adType; +} + +QString QtmadsService::getStringFromAdType(AdType adType) +{ + QString adStr; + switch(adType){ + default: + case anyAd: + adStr.append("anyAd"); + break; + case txtAd: + adStr.append("txtAd"); + break; + case imageAd: + adStr.append("imageAd"); + break; + case txtBannerAd: + adStr.append("txtBannerAd"); + break; + case imageBannerAd: + adStr.append("imageBannerAd"); + break; + case audioAd: + adStr.append("audioAd"); + break; + case videoAd: + adStr.append("videoAd"); + break; + } + return adStr; +}