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/>.
25 #include <QApplication>
31 #include <QDesktopServices>
35 #include <QPluginLoader>
37 #include "qtmadsservice.h"
38 #include "qtmadsserviceinterface.h"
40 QtmadsAd::QtmadsAd(QString serviceName, AdType defaultType, quint32 id, AdFit fitting)
41 : adType(anyAd), adFit(fitting)
43 qDebug() << "QtmadsAd()";
44 this->loadPlugin(serviceName);
45 this->service->initializeService(serviceName, id, defaultType);
47 layout = new QBoxLayout(QBoxLayout::TopToBottom, this);
49 label = new QLabel(this);
51 htmlView = new QWebView(this);
63 void QtmadsAd::startAd(quint32 adChangeIntervalInSecs)
67 if(0 != adChangeIntervalInSecs){
69 adTimer = new QTimer(this);
70 connect(adTimer, SIGNAL(timeout()), this, SLOT(getNewAd()));
74 adTimer->start(adChangeIntervalInSecs*1000);
78 void QtmadsAd::getNewAd()
80 qDebug() << "getNewAd()";
81 connect(service, SIGNAL(adRequestReady(QHash<QString, QVariant> &)),this, SLOT(adRequestSucceeded(QHash<QString, QVariant> &)));
82 connect(service, SIGNAL(adRequestFailed()),this, SLOT(adRequestFailed()));
84 connect(this, SIGNAL(newAdReady()),this, SLOT(readyToShow()));
85 connect(this, SIGNAL(newAdFailed()),this, SLOT(showFailed()));
87 QHash<QString, QVariant> adParams;
88 this->getHash(adParams);
89 this->service->getAd(adParams);
92 void QtmadsAd::adRequestSucceeded(QHash<QString, QVariant> &ad)
94 qDebug() << "adRequestSucceeded()";
95 disconnect(service, SIGNAL(adRequestReady(QHash<QString, QVariant> &)),this, SLOT(adRequestSucceeded(QHash<QString, QVariant> &)));
96 disconnect(service, SIGNAL(adRequestFailed()),this, SLOT(adRequestFailed()));
103 QHash<QString, QVariant>::iterator iter;
105 // get ad type before other
106 for(iter = ad.begin(); iter != ad.end(); iter++){
107 if(0 == iter.key().compare(TAG_ADTYPE)){
108 this->adType = this->service->getAdTypeFromString(iter.value().toString());
113 for(iter = ad.begin(); iter != ad.end(); iter++){
114 //qDebug() << "key: " << iter.key() << " value: " << iter.value().toString();
115 if(0 == iter.key().compare(TAG_TEXT)){
116 this->text = iter.value().toString();
118 }else if(0 == iter.key().compare(TAG_CONTENT_URL)){
119 if(this->adType == imageAd){
120 connect(service, SIGNAL(imageRequestReady(QImage *)),this, SLOT(imageRequestSucceeded(QImage *)));
121 connect(service, SIGNAL(imageRequestFailed()),this, SLOT(imageRequestFailed()));
122 this->service->getRemoteImage(iter.value().toString());
125 if(this->adType == videoAd || this->adType == audioAd ){
126 this->mediaContentUrl = iter.value().toString();
129 }else if(0 == iter.key().compare(TAG_URL)){
130 adUrl.setUrl(iter.value().toString());
132 }else if(0 == iter.key().compare(TAG_HTML)){
133 this->htmlContent = iter.value().toString();
135 }else if(0 == iter.key().compare(TAG_ADTYPE)){
138 qDebug() << "Unknown tag.";
147 void QtmadsAd::adRequestFailed()
149 disconnect(service, SIGNAL(adRequestReady(QHash<QString, QVariant> &)),this, SLOT(adRequestSucceeded(QHash<QString, QVariant> &)));
150 disconnect(service, SIGNAL(adRequestFailed()),this, SLOT(adRequestFailed()));
156 void QtmadsAd::imageRequestSucceeded(QImage *img)
158 disconnect(service, SIGNAL(imageRequestReady(QImage *)),this, SLOT(imageRequestSucceeded(QImage *)));
159 disconnect(service, SIGNAL(imageRequestFailed()),this, SLOT(imageRequestFailed()));
161 if(this->image != 0){
170 void QtmadsAd::imageRequestFailed()
176 void QtmadsAd::getHash(QHash<QString, QVariant> &adParams)
178 adParams.insert("adsize", QVariant(this->sizeHint()));
182 void QtmadsAd::readyToShow()
184 qDebug() << "readyToShow()";
185 switch(this->adType){
189 this->label->setFont(this->font());
190 this->label->setAlignment(textAlignment);
191 this->label->setText(this->text);
193 if(this->adFit == fitAdToWidget){
194 //TODO: fit text to ad
195 /*QFontMetrics metrics (label->font());
196 int pixelWidth = metrics.width(label->text());
197 int pixelHeight = metrics.height();
199 label->font().setPixelSize();*/
200 }/*TODO: else if(this->adFit == adToFit){
203 this->layout->addWidget(label);
210 if(this->adFit == fitAdToWidget){
211 pic = QPixmap::fromImage(*image).scaled(this->size());
213 pic = QPixmap::fromImage(*image);
214 }/*TODO: else if(this->adFit == fitWidgetToAd){
217 label->setPixmap(pic);
218 this->layout->addWidget(label);
225 /*TODO: if(this->adFit == fitToAd){
227 }else if(this->adFit == adToFit){
229 htmlView->setHtml(this->htmlContent);
230 htmlView->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum));
231 htmlView->setMinimumSize(QSize(216, 36));
233 //TODO: opening url inplace webpage
234 htmlView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
236 connect(htmlView->page(), SIGNAL(linkClicked(const QUrl &)),this, SLOT(adClicked(const QUrl &)));
237 this->layout->addWidget(htmlView);
249 if(this->adType == audioAd){
250 player = Phonon::createPlayer(Phonon::MusicCategory,
251 Phonon::MediaSource(this->mediaContentUrl));
253 player = Phonon::createPlayer(Phonon::VideoCategory,
254 Phonon::MediaSource(this->mediaContentUrl));
258 connect(player, SIGNAL(stateChanged(Phonon::State newstate, Phonon::State oldstate)),
259 this, SLOT(phononPlayerStateChanged(Phonon::State newstate, Phonon::State oldstate)));
260 connect(player, SIGNAL(finished ()),
261 this, SLOT(phononPlayerStateChanged()));
265 qDebug() << "media play started!";
270 disconnect(this, SIGNAL(newAdReady()),this, SLOT(readyToShow()));
271 disconnect(this, SIGNAL(newAdFailed()),this, SLOT(showFailed()));
274 void QtmadsAd::showFailed()
276 disconnect(this, SIGNAL(newAdReady()),this, SLOT(readyToShow()));
277 disconnect(this, SIGNAL(newAdFailed()),this, SLOT(showFailed()));
279 //TODO: emit signal for not able to show
283 void QtmadsAd::phononPlayerStateChanged(Phonon::State newstate, Phonon::State oldstate)
286 if(newstate == Phonon::StoppedState || newstate == Phonon::ErrorState){
287 disconnect(player, SIGNAL(stateChanged(Phonon::State newstate, Phonon::State oldstate)),
288 this, SLOT(phononState(Phonon::State newstate, Phonon::State oldstate)));
289 disconnect(player, SIGNAL(finished ()), this, SLOT(phononState()));
297 void QtmadsAd::setAsTextAd(const QFont &font, bool isOnlyTextAd, Qt::Alignment alignment)
300 this->textAlignment = alignment;
303 this->service->setDefaultAdType(txtAd);
306 if(this->service->getDefaultAdType() != txtAd)
307 this->service->setDefaultAdType(anyAd);
311 void QtmadsAd::setAsImageAd(bool isOnlyImageAd)
314 this->service->setDefaultAdType(imageAd);
317 if(this->service->getDefaultAdType() != imageAd)
318 this->service->setDefaultAdType(anyAd);
322 void QtmadsAd::setAsBannerAd(bool isOnlyBannerAd)
325 this->service->setDefaultAdType(imageBannerAd);
328 if(this->service->getDefaultAdType() != imageBannerAd)
329 this->service->setDefaultAdType(anyAd);
334 void QtmadsAd::setAsTextBannerAd(bool isOnlyTextBannerAd)
336 if(isOnlyTextBannerAd){
337 this->service->setDefaultAdType(txtBannerAd);
340 if(this->service->getDefaultAdType() != txtBannerAd)
341 this->service->setDefaultAdType(anyAd);
345 void QtmadsAd::setAsAudioAd(bool isOnlyAudioAd)
348 this->service->setDefaultAdType(audioAd);
351 if(this->service->getDefaultAdType() != audioAd)
352 this->service->setDefaultAdType(anyAd);
356 void QtmadsAd::setAsVideoAd()
358 this->service->setDefaultAdType(videoAd);
361 void QtmadsAd::adClicked(const QUrl &url)
364 if(!adUrl.isValid()){
365 qDebug() << "Browser opened to urli: " << url.toString();
366 QDesktopServices::openUrl(url);
369 qDebug() << "Browser opened to urli: " << adUrl.toString();
370 QDesktopServices::openUrl(adUrl);
374 void QtmadsAd::clear()
376 qDebug() << "clear()";
386 disconnect(htmlView->page(), SIGNAL(linkClicked(const QUrl &)),this, SLOT(adClicked(const QUrl &)));
388 //TODO: remove widgets from layout?
393 disconnect(player, SIGNAL(stateChanged(Phonon::State newstate, Phonon::State oldstate)),
394 this, SLOT(phononState(Phonon::State newstate, Phonon::State oldstate)));
395 disconnect(player, SIGNAL(finished ()), this, SLOT(phononState()));
402 bool QtmadsAd::loadPlugin(QString serviceName)
404 bool pluginFound = false;
405 QDir pluginsDir(qApp->applicationDirPath());
406 #if defined(Q_OS_WIN)
407 if (pluginsDir.dirName().toLower() == "debug" || pluginsDir.dirName().toLower() == "release")
409 #elif defined(Q_OS_MAC)
410 if (pluginsDir.dirName() == "MacOS") {
416 pluginsDir.cd(PLUGINS_PATH);
417 foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
418 qDebug() << "path: " << pluginsDir.absoluteFilePath(fileName);
419 if(fileName.contains(serviceName) && !pluginFound){
420 QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
421 QObject *plugin = pluginLoader.instance();
423 QtmadsServiceInterface *plugLoader = qobject_cast<QtmadsServiceInterface *>(plugin);
424 this->service = plugLoader->loadService();
428 qDebug() << "Plugin load failed: " << pluginLoader.errorString();
433 qDebug() << "Plugin load failed: " << pluginLoader.errorString();
436 qDebug() << "not service";