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/>.
23 #include "qtmadsadwidget.h"
25 #include <QApplication>
28 #include <QDesktopServices>
31 #include <QPluginLoader>
32 #include <QMessageBox>
33 #include <QAbstractButton>
35 #include "qtmadsservice.h"
36 #include "qtmadsserviceloaderinterface.h"
38 QtmadsAdWidget::QtmadsAdWidget(QString serviceName, AdType defaultType, quint32 groupId, AdFit fitting, QWidget *parent)
39 : type(defaultType), adFit(fitting), QWidget(parent)
41 this->loadServicePlugin(serviceName);
42 this->service->initializeService(serviceName, groupId, defaultType);
48 QtmadsAdWidget::~QtmadsAdWidget()
54 void QtmadsAdWidget::startAd(quint32 adChangeIntervalInSecs)
58 if(0 != adChangeIntervalInSecs){
60 adTimer = new QTimer(this);
62 if(adTimer->isSingleShot()){
63 disconnect(adTimer, SIGNAL(timeout()), this, SLOT(singleShotEnded()));
65 disconnect(adTimer, SIGNAL(timeout()), this, SLOT(getNewAd()));
69 connect(adTimer, SIGNAL(timeout()), this, SLOT(getNewAd()));
70 adTimer->setSingleShot(false);
71 adTimer->start(adChangeIntervalInSecs*1000);
75 void QtmadsAdWidget::startSingleShotAd(quint32 durationInSecs)
79 if(0 != durationInSecs){
81 adTimer = new QTimer(this);
83 if(adTimer->isSingleShot()){
84 disconnect(adTimer, SIGNAL(timeout()), this, SLOT(singleShotEnded()));
86 disconnect(adTimer, SIGNAL(timeout()), this, SLOT(getNewAd()));
90 connect(adTimer, SIGNAL(timeout()), this, SLOT(singleShotEnded()));
91 adTimer->setSingleShot(true);
92 adTimer->start(durationInSecs*1000);
96 void QtmadsAdWidget::setAdType(AdType adType)
101 AdType QtmadsAdWidget::adType()
106 void QtmadsAdWidget::getNewAd()
108 qDebug() << "getNewAd()";
109 connect(service, SIGNAL(adRequestReady(QHash<QString, QVariant> &)),this, SLOT(adRequestSucceeded(QHash<QString, QVariant> &)));
110 connect(service, SIGNAL(adRequestFailed()),this, SLOT(adRequestFailed()));
112 QHash<QString, QVariant> adParams;
113 this->getAdAsHash(adParams);
114 this->service->getAd(adParams);
117 void QtmadsAdWidget::adRequestSucceeded(QHash<QString, QVariant> &ad)
119 qDebug() << "adRequestSucceeded()";
120 disconnect(service, SIGNAL(adRequestReady(QHash<QString, QVariant> &)),this, SLOT(adRequestSucceeded(QHash<QString, QVariant> &)));
121 disconnect(service, SIGNAL(adRequestFailed()),this, SLOT(adRequestFailed()));
123 ParseState adReady = parseWaiting;
128 if(adReady != parseFailed){
129 adReady = this->parseFromParameters(ad);
132 if(adReady == parseReady){
135 }else if(adReady == parseFailed){
140 void QtmadsAdWidget::adRequestFailed()
142 disconnect(service, SIGNAL(adRequestReady(QHash<QString, QVariant> &)),this, SLOT(adRequestSucceeded(QHash<QString, QVariant> &)));
143 disconnect(service, SIGNAL(adRequestFailed()),this, SLOT(adRequestFailed()));
148 void QtmadsAdWidget::adClicked(const QUrl &url)
151 qDebug() << "Browser opened to url: " << url.toString();
153 msgBox = new QMessageBox(QMessageBox::NoIcon, tr("Opening ad"),
154 tr("Opening ad to browser..."), QMessageBox::Cancel, this);
156 // just fixed wait time for now..
160 QDesktopServices::openUrl(url);
164 void QtmadsAdWidget::clearAd()
170 void QtmadsAdWidget::singleShotEnded()
172 qDebug() << "singleShotEnded()";
181 void QtmadsAdWidget::timerEvent(QTimerEvent *event)
183 qDebug() << "timerEvent()";
184 this->killTimer(event->timerId());
189 bool QtmadsAdWidget::loadServicePlugin(QString serviceName)
191 bool pluginFound = false;
192 QDir pluginsDir(qApp->applicationDirPath());
193 #if defined(Q_OS_WIN)
194 if (pluginsDir.dirName().toLower() == "debug" || pluginsDir.dirName().toLower() == "release")
196 #elif defined(Q_OS_MAC)
197 if (pluginsDir.dirName() == "MacOS") {
203 pluginsDir.cd(PLUGINS_PATH);
204 foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
205 qDebug() << "path: " << pluginsDir.absoluteFilePath(fileName);
206 if(fileName.contains(serviceName) && !pluginFound){
207 QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
208 QObject *plugin = pluginLoader.instance();
210 QtmadsServiceLoaderInterface *plugLoader = qobject_cast<QtmadsServiceLoaderInterface *>(plugin);
211 this->service = plugLoader->loadService();
214 qDebug() << "Plugin loaded";
216 qDebug() << "Plugin load failed: " << pluginLoader.errorString();
221 qDebug() << "Plugin load failed: " << pluginLoader.errorString();
224 qDebug() << "not service";