2 * Copyright (c) 2009 Eetu Lehmusvuo.
4 * This file is part of QtMAds.
6 * QtMAds is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * QtMAds is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with QtMAds. If not, see <http://www.gnu.org/licenses/>.
21 #include "qtmadsservice.h"
28 #include <QXmlStreamReader>
30 #include <QPluginLoader>
32 QtmadsService::QtmadsService()
36 QtmadsService::~QtmadsService()
42 bool QtmadsService::initializeService(QString service, quint32 adGroup, AdType defaultAdType)
44 this->serviceName = service;
45 this->adGroupId = adGroup;
46 this->defAdType = defaultAdType;
48 bool parseSuccess = true;
49 // ../plugins/SERVICE_NAME_adconfigure.xml
50 QString path("plugins/confs/");
51 path.append(this->serviceName);
52 path.append("_adconfigure.xml");
54 parseSuccess = file.exists();
57 // ./default_adconfigure.xml
58 file.setFileName("." + CONF_FILE);
59 parseSuccess = file.exists();
63 file.open(QIODevice::ReadOnly);
64 this->parseXmlConfFile(file);
74 return isInitialized();
77 bool QtmadsService::isInitialized()
82 void QtmadsService::getAd(QHash<QString, QVariant> &adParameters)
84 //qDebug() << "QtmadsService::getAd()";
86 QList<QPair<QString,QString> > parameters;
87 this->parseUrlParameters(adParameters, parameters);
89 QUrl url(this->serviceUrl);
90 url.setQueryItems(parameters);
91 QByteArray path = QUrl::toPercentEncoding(url.path(), "!$&'()*+,;=:@/?");
94 path.append(QUrl::toPercentEncoding("?", "!$&'()*+,;=:@/?"));
95 path.append(url.encodedQuery());
97 http = new QHttp(this);
98 connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(adRequestFinished(int, bool)));
99 http->setHost(url.host());
100 //qDebug() << "Url: " << url.toString();
101 adTrasactionId = http->get(path);
103 emit adRequestFailed();
108 void QtmadsService::adRequestFinished(int transactionId, bool error)
110 //qDebug() << "adRequestFinished()";
111 if(adTrasactionId != transactionId){
117 QByteArray data = http->readAll();
119 qDebug() << "Data: " << data;
121 QHash<QString, QVariant> adParams;// = new QHash<QString, QVariant>();
122 this->parseReceivedAd(data, adParams);
123 QHash<QString, QVariant>::iterator i;
126 /*for(i = adParams.begin(); i != adParams.end(); i++){
127 qDebug() << "key: " << i.key() << " value: " << i.value().toString();
130 emit adRequestReady(adParams);
132 qDebug() << "adRequestFailed()";
133 emit adRequestFailed();
136 disconnect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(adRequestFinished(int, bool)));
139 void QtmadsService::getRemoteImage(QString urlString)
142 QByteArray path = QUrl::toPercentEncoding(url.path(), "!$&'()*+,;=:@/?");
146 connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(imageRequestFinished(int, bool)));
147 http->setHost(url.host());
148 imageTrasactionId = http->get(path);
152 void QtmadsService::imageRequestFinished(int transactionId, bool error)
154 if(imageTrasactionId != transactionId){
157 imageTrasactionId = -1;
160 QImage *image = new QImage();
161 if(image->loadFromData(http->readAll())){
162 emit imageRequestReady(image);
166 emit imageRequestFailed();
170 disconnect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(imageRequestFinished(int, bool)));
174 void QtmadsService::setDefaultAdType(AdType type)
176 this->defAdType = type;
179 AdType QtmadsService::defaultAdType()
181 return this->defAdType;
184 bool QtmadsService::parseXmlConfFile(QFile &file)
186 //qDebug() << "QtmadsService::parseXmlConfFile()";
187 //TODO: apply some sanity to if-hell
188 bool success = false;
189 QXmlStreamReader xmlReader(&file);
191 while (!xmlReader.atEnd()) {
192 // find right servicename tag
193 if(QXmlStreamReader::StartElement == xmlReader.readNext()
194 && 0 == xmlReader.name().compare(TAG_SERVICE)
195 && 0 == xmlReader.attributes().value("name").compare(this->serviceName)){
196 this->serviceUrl.clear();
197 this->serviceUrl.append(xmlReader.attributes().value("url"));
200 while (!(QXmlStreamReader::EndElement == xmlReader.readNext()
201 && 0 == xmlReader.name().compare(TAG_SERVICE))) {
204 if(QXmlStreamReader::StartElement == xmlReader.tokenType()){
205 if(0 == xmlReader.name().compare(TAG_ACCOUNTID)){
206 parameterTags.insert(
207 QString(TAG_ACCOUNTID),xmlReader.attributes().value(TAG_KEY).toString());
208 this->accountId.clear();
209 this->accountId.append(xmlReader.attributes().value(TAG_VALUE));
210 }else if(0 == xmlReader.name().compare(TAG_PASSWORD)){
211 parameterTags.insert(
212 QString(TAG_PASSWORD),xmlReader.attributes().value(TAG_KEY).toString());
213 this->password.clear();
214 this->password.append(xmlReader.attributes().value(TAG_VALUE));
215 }else if(0 == xmlReader.name().compare(TAG_CAMPAIGN)){
217 if(0 == xmlReader.attributes().value("id").compare(QString("%1").arg(this->adGroupId))){
219 while(!(QXmlStreamReader::EndElement == xmlReader.readNext()
220 && 0 == xmlReader.name().compare(TAG_CAMPAIGN))){
221 if(QXmlStreamReader::StartElement == xmlReader.tokenType()){
222 /*qDebug() << "name: " << xmlReader.name()
223 << "key: " << xmlReader.attributes().value(TAG_KEY).toString()
224 << "value: " << xmlReader.attributes().value(TAG_VALUE).toString();*/
225 if(0 == xmlReader.name().compare(TAG_ADTYPE)){
226 parameterTags.insert(
227 QString(TAG_ADTYPE),xmlReader.attributes().value(TAG_KEY).toString());
228 this->defAdType = this->getAdTypeFromString(xmlReader.attributes().value(TAG_VALUE).toString());
230 }else if(0 == xmlReader.name().compare(TAG_LANGUAGE)){
231 parameterTags.insert(
232 QString(TAG_LANGUAGE),xmlReader.attributes().value(TAG_KEY).toString());
233 this->language = xmlReader.attributes().value(TAG_VALUE).toString();
235 }else if(0 == xmlReader.name().compare(TAG_TRANSACTIONID)){
236 parameterTags.insert(
237 QString(TAG_TRANSACTIONID),xmlReader.attributes().value(TAG_KEY).toString());
238 this->transactionId = xmlReader.attributes().value(TAG_VALUE).toString();
241 qDebug() << "Unknown element: " << xmlReader.tokenString();
242 if(xmlReader.atEnd()){
243 // TODO: throw exception?
244 qDebug() << "Unexpected end of document!";
253 qDebug() << "Unknown element: " << xmlReader.tokenString();
254 if(xmlReader.atEnd()){
255 // TODO: throw exception?
256 qDebug() << "Unexpected end of document!";
265 /*while (!xmlReader.atEnd()) {
266 qDebug() << "Type: "<< xmlReader.readNext();
267 qDebug() << " Tokenstring: " << xmlReader.tokenString();
268 qDebug() << " name: " << xmlReader.name(); //<< " key: " << xmlReader.attributes().value("key") << " value: " << xmlReader.attributes().value("value");
275 AdType QtmadsService::getAdTypeFromString(QString adStr)
277 AdType adType = anyAd;
279 if(0 == adStr.compare("txtAd")){
281 }else if(0 == adStr.compare("imageAd")){
283 }else if(0 == adStr.compare("txtBannerAd")){
284 adType = txtBannerAd;
285 }else if(0 == adStr.compare("imageBannerAd")){
286 adType = imageBannerAd;
287 }else if(0 == adStr.compare("audioAd")){
289 }else if(0 == adStr.compare("videoAd")){
295 QString QtmadsService::getStringFromAdType(AdType adType)
301 adStr.append("anyAd");
304 adStr.append("txtAd");
307 adStr.append("imageAd");
310 adStr.append("txtBannerAd");
313 adStr.append("imageBannerAd");
316 adStr.append("audioAd");
319 adStr.append("videoAd");