Splashscreen ad support. Wait dialog for ad click/browser start.
[qtmads] / src / qtmadsadwidget.cpp
1 /*
2  * Copyright (c) 2009 Eetu Lehmusvuo.
3  *
4  * This file is part of QtMAds.
5  *
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.
10  *
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.
15  *
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/>.
18  *
19  */
20
21 #include <QtGui>
22
23 #include "qtmadsadwidget.h"
24
25 #include <QApplication>
26 #include <QUrl>
27 #include <QDebug>
28 #include <QDesktopServices>
29 #include <QTimer>
30 #include <QDir>
31 #include <QPluginLoader>
32 #include <QMessageBox>
33 #include <QAbstractButton>
34
35 #include "qtmadsservice.h"
36 #include "qtmadsserviceloaderinterface.h"
37
38 QtmadsAdWidget::QtmadsAdWidget(QString serviceName, AdType defaultType, quint32 groupId, AdFit fitting, QWidget *parent)
39     : type(defaultType), adFit(fitting), QWidget(parent)
40 {
41         this->loadServicePlugin(serviceName);
42         this->service->initializeService(serviceName, groupId, defaultType);
43
44     adTimer = 0;
45     msgBox = 0;
46 }
47
48 QtmadsAdWidget::~QtmadsAdWidget()
49 {
50     this->clearAd();
51     emit adStopped();
52 }
53
54 void QtmadsAdWidget::startAd(quint32 adChangeIntervalInSecs)
55 {
56     getNewAd();
57     qDebug() << "ee";
58     if(0 != adChangeIntervalInSecs){
59         if(adTimer == 0){
60             adTimer = new QTimer(this);
61         }else{
62             if(adTimer->isSingleShot()){
63                 disconnect(adTimer, SIGNAL(timeout()), this, SLOT(singleShotEnded()));
64             }else{
65                 disconnect(adTimer, SIGNAL(timeout()), this, SLOT(getNewAd()));
66             }
67             adTimer->stop();
68         }
69         connect(adTimer, SIGNAL(timeout()), this, SLOT(getNewAd()));
70         adTimer->setSingleShot(false);
71         adTimer->start(adChangeIntervalInSecs*1000);
72     }
73 }
74
75 void QtmadsAdWidget::startSingleShotAd(quint32 durationInSecs)
76 {
77     getNewAd();
78
79     if(0 != durationInSecs){
80         if(adTimer == 0){
81             adTimer = new QTimer(this);
82         }else{
83             if(adTimer->isSingleShot()){
84                 disconnect(adTimer, SIGNAL(timeout()), this, SLOT(singleShotEnded()));
85             }else{
86                 disconnect(adTimer, SIGNAL(timeout()), this, SLOT(getNewAd()));
87             }
88             adTimer->stop();
89         }
90         connect(adTimer, SIGNAL(timeout()), this, SLOT(singleShotEnded()));
91         adTimer->setSingleShot(true);
92         adTimer->start(durationInSecs*1000);
93     }
94 }
95
96 void QtmadsAdWidget::setAdType(AdType adType)
97 {
98     this->type = adType;
99 }
100
101 AdType QtmadsAdWidget::adType()
102 {
103     return this->type;
104 }
105
106 void QtmadsAdWidget::getNewAd()
107 {
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()));
111
112         QHash<QString, QVariant> adParams;
113         this->getAdAsHash(adParams);
114         this->service->getAd(adParams);
115 }
116
117 void QtmadsAdWidget::adRequestSucceeded(QHash<QString, QVariant> &ad)
118 {
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()));
122
123     ParseState adReady = parseWaiting;
124
125     // clear previous ad
126     this->clearAd();
127
128     if(adReady != parseFailed){
129         adReady = this->parseFromParameters(ad);
130     }
131
132     if(adReady == parseReady){
133         emit newAdReady();
134         show();
135     }else if(adReady == parseFailed){
136         emit newAdFailed();
137     }
138 }
139
140 void QtmadsAdWidget::adRequestFailed()
141 {
142     disconnect(service, SIGNAL(adRequestReady(QHash<QString, QVariant> &)),this, SLOT(adRequestSucceeded(QHash<QString, QVariant> &)));
143     disconnect(service, SIGNAL(adRequestFailed()),this, SLOT(adRequestFailed()));
144
145         emit newAdFailed();
146 }
147
148 void QtmadsAdWidget::adClicked(const QUrl &url)
149 {
150     if(url.isValid()){
151         qDebug() << "Browser opened to url: " << url.toString();
152         if(msgBox == 0){
153             msgBox = new QMessageBox(QMessageBox::NoIcon, tr("Opening ad"),
154                     tr("Opening ad to browser..."), QMessageBox::Cancel, this);
155        }
156         // just fixed wait time for now..
157         startTimer(10000);
158         msgBox->exec();
159
160         QDesktopServices::openUrl(url);
161     }
162 }
163
164 void QtmadsAdWidget::clearAd()
165 {
166     adID = -1;
167     type = noAd;
168 }
169
170 void QtmadsAdWidget::singleShotEnded()
171 {
172     qDebug() << "singleShotEnded()";
173     adTimer->stop();
174     delete adTimer;
175     adTimer = 0;
176
177     this->clearAd();
178     emit adStopped();
179 }
180
181 void QtmadsAdWidget::timerEvent(QTimerEvent *event)
182 {
183     qDebug() << "timerEvent()";
184     this->killTimer(event->timerId());
185     if(msgBox)
186         msgBox->close();
187 }
188
189 bool QtmadsAdWidget::loadServicePlugin(QString serviceName)
190 {
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")
195         pluginsDir.cdUp();
196 #elif defined(Q_OS_MAC)
197     if (pluginsDir.dirName() == "MacOS") {
198         pluginsDir.cdUp();
199         pluginsDir.cdUp();
200         pluginsDir.cdUp();
201     }
202 #endif
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();
209             if (plugin) {
210                 QtmadsServiceLoaderInterface *plugLoader = qobject_cast<QtmadsServiceLoaderInterface *>(plugin);
211                 this->service = plugLoader->loadService();
212                 if (this->service){
213                     pluginFound = true;
214                     qDebug() << "Plugin loaded";
215                 }else{
216                     qDebug() << "Plugin load failed: " << pluginLoader.errorString();
217                 }
218                 plugLoader = 0;
219                 delete plugin;
220             }else{
221                 qDebug() << "Plugin load failed: " << pluginLoader.errorString();
222             }
223         }else{
224             qDebug() << "not service";
225         }
226     }
227
228     return pluginFound;
229 }