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 QFile file(CONF_PATH + this->serviceName + CONF_FILE_POSTFIX );
51 parseSuccess = file.exists();
54 // ./default_adconfigure.xml
55 file.setFileName("." + CONF_FILE);
56 parseSuccess = file.exists();
60 file.open(QIODevice::ReadOnly);
61 this->parseXmlConfFile(file);
67 qDebug() << "Ad service configure failed. " << CONF_PATH + this->serviceName + CONF_FILE_POSTFIX ;
72 return isInitialized();
75 bool QtmadsService::isInitialized()
80 void QtmadsService::getAd(QHash<QString, QVariant> &adParameters)
82 //qDebug() << "QtmadsService::getAd()";
84 QList<QPair<QString,QString> > parameters;
85 this->parseUrlParameters(adParameters, parameters);
87 QUrl url(this->serviceUrl);
88 url.setQueryItems(parameters);
89 QByteArray path = QUrl::toPercentEncoding(url.path(), "!$&'()*+,;=:@/?");
92 path.append(QUrl::toPercentEncoding("?", "!$&'()*+,;=:@/?"));
93 path.append(url.encodedQuery());
95 http = new QHttp(this);
96 connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(adRequestFinished(int, bool)));
97 http->setHost(url.host());
98 //qDebug() << "Url: " << url.toString();
99 adTrasactionId = http->get(path);
101 emit adRequestFailed();
106 void QtmadsService::adRequestFinished(int transactionId, bool error)
108 //qDebug() << "adRequestFinished()";
109 if(adTrasactionId != transactionId){
115 QByteArray data = http->readAll();
117 qDebug() << "Data: " << data;
119 QHash<QString, QVariant> adParams;// = new QHash<QString, QVariant>();
120 this->parseReceivedAd(data, adParams);
121 QHash<QString, QVariant>::iterator i;
124 /*for(i = adParams.begin(); i != adParams.end(); i++){
125 qDebug() << "key: " << i.key() << " value: " << i.value().toString();
128 emit adRequestReady(adParams);
130 qDebug() << "adRequestFailed()";
131 emit adRequestFailed();
134 disconnect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(adRequestFinished(int, bool)));
137 void QtmadsService::getRemoteImage(QString urlString)
140 QByteArray path = QUrl::toPercentEncoding(url.path(), "!$&'()*+,;=:@/?");
144 connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(imageRequestFinished(int, bool)));
145 http->setHost(url.host());
146 imageTrasactionId = http->get(path);
150 void QtmadsService::imageRequestFinished(int transactionId, bool error)
152 if(imageTrasactionId != transactionId){
155 imageTrasactionId = -1;
158 QImage *image = new QImage();
159 if(image->loadFromData(http->readAll())){
160 emit imageRequestReady(image);
164 emit imageRequestFailed();
168 disconnect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(imageRequestFinished(int, bool)));
172 void QtmadsService::setDefaultAdType(AdType type)
174 this->defAdType = type;
177 AdType QtmadsService::defaultAdType()
179 return this->defAdType;
182 bool QtmadsService::parseXmlConfFile(QFile &file)
184 qDebug() << "QtmadsService::parseXmlConfFile()";
185 bool success = false;
186 QXmlStreamReader xmlReader(&file);
188 while (!xmlReader.atEnd()) {
189 // find right servicename tag
190 if(QXmlStreamReader::StartElement == xmlReader.readNext()
191 && 0 == xmlReader.name().compare(TAG_SERVICE)
192 && 0 == xmlReader.attributes().value("name").compare(this->serviceName)){
193 this->serviceUrl.clear();
194 this->serviceUrl.append(xmlReader.attributes().value("url"));
197 while (!(QXmlStreamReader::EndElement == xmlReader.readNext()
198 && 0 == xmlReader.name().compare(TAG_SERVICE))) {
201 if(QXmlStreamReader::StartElement == xmlReader.tokenType()){
202 if(0 == xmlReader.name().compare(TAG_ACCOUNTID)){
203 parameterTags.insert(
204 QString(TAG_ACCOUNTID),xmlReader.attributes().value(TAG_KEY).toString());
205 this->accountId.clear();
206 this->accountId.append(xmlReader.attributes().value(TAG_VALUE));
207 }else if(0 == xmlReader.name().compare(TAG_PASSWORD)){
208 parameterTags.insert(
209 QString(TAG_PASSWORD),xmlReader.attributes().value(TAG_KEY).toString());
210 this->password.clear();
211 this->password.append(xmlReader.attributes().value(TAG_VALUE));
212 }else if(0 == xmlReader.name().compare(TAG_CAMPAIGN)){
214 if(0 == xmlReader.attributes().value("id").compare(QString("%1").arg(this->adGroupId))){
216 while(!(QXmlStreamReader::EndElement == xmlReader.readNext()
217 && 0 == xmlReader.name().compare(TAG_CAMPAIGN))){
218 if(QXmlStreamReader::StartElement == xmlReader.tokenType()){
219 /*qDebug() << "name: " << xmlReader.name()
220 << "key: " << xmlReader.attributes().value(TAG_KEY).toString()
221 << "value: " << xmlReader.attributes().value(TAG_VALUE).toString();*/
222 if(0 == xmlReader.name().compare(TAG_ADTYPE)){
223 parameterTags.insert(
224 QString(TAG_ADTYPE),xmlReader.attributes().value(TAG_KEY).toString());
225 this->defAdType = this->getAdTypeFromString(xmlReader.attributes().value(TAG_VALUE).toString());
227 }else if(0 == xmlReader.name().compare(TAG_LANGUAGE)){
228 parameterTags.insert(
229 QString(TAG_LANGUAGE),xmlReader.attributes().value(TAG_KEY).toString());
230 this->language = xmlReader.attributes().value(TAG_VALUE).toString();
232 }else if(0 == xmlReader.name().compare(TAG_TRANSACTIONID)){
233 parameterTags.insert(
234 QString(TAG_TRANSACTIONID),xmlReader.attributes().value(TAG_KEY).toString());
235 this->transactionId = xmlReader.attributes().value(TAG_VALUE).toString();
238 qDebug() << "Unknown element: " << xmlReader.tokenString();
239 if(xmlReader.atEnd()){
240 // TODO: throw exception?
241 qDebug() << "Unexpected end of document!";
250 qDebug() << "Unknown element: " << xmlReader.tokenString();
251 if(xmlReader.atEnd()){
252 // TODO: throw exception?
253 qDebug() << "Unexpected end of document!";
262 /*while (!xmlReader.atEnd()) {
263 qDebug() << "Type: "<< xmlReader.readNext();
264 qDebug() << " Tokenstring: " << xmlReader.tokenString();
265 qDebug() << " name: " << xmlReader.name(); //<< " key: " << xmlReader.attributes().value("key") << " value: " << xmlReader.attributes().value("value");
272 AdType QtmadsService::getAdTypeFromString(QString adStr)
274 AdType adType = anyAd;
276 if(0 == adStr.compare("txtAd")){
278 }else if(0 == adStr.compare("imageAd")){
280 }else if(0 == adStr.compare("txtBannerAd")){
281 adType = txtBannerAd;
282 }else if(0 == adStr.compare("imageBannerAd")){
283 adType = imageBannerAd;
284 }else if(0 == adStr.compare("audioAd")){
286 }else if(0 == adStr.compare("videoAd")){
292 QString QtmadsService::getStringFromAdType(AdType adType)
298 adStr.append("anyAd");
301 adStr.append("txtAd");
304 adStr.append("imageAd");
307 adStr.append("txtBannerAd");
310 adStr.append("imageBannerAd");
313 adStr.append("audioAd");
316 adStr.append("videoAd");