First version. ADHERE service support with label, banner and media ads.
[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
33 #include "qtmadsservice.h"
34 #include "qtmadsserviceinterface.h"
35
36 QtmadsAdWidget::QtmadsAdWidget(QString serviceName, AdType defaultType, quint32 groupId, AdFit fitting, QWidget *parent)
37     : type(defaultType), adFit(fitting), QWidget(parent)
38 {
39         this->loadPlugin(serviceName);
40         this->service->initializeService(serviceName, groupId, defaultType);
41
42     adTimer = 0;
43 }
44
45 QtmadsAdWidget::~QtmadsAdWidget()
46 {
47     this->clearAd();
48 }
49
50 void QtmadsAdWidget::startAd(quint32 adChangeIntervalInSecs)
51 {
52     getNewAd();
53
54     if(0 != adChangeIntervalInSecs){
55         if(adTimer == 0){
56             adTimer = new QTimer(this);
57             connect(adTimer, SIGNAL(timeout()), this, SLOT(getNewAd()));
58         }else{
59             adTimer->stop();
60         }
61         adTimer->start(adChangeIntervalInSecs*1000);
62     }
63 }
64
65 void QtmadsAdWidget::setAdType(AdType adType)
66 {
67     this->type = adType;
68 }
69
70 AdType QtmadsAdWidget::adType()
71 {
72     return this->type;
73 }
74
75 void QtmadsAdWidget::getNewAd()
76 {
77     qDebug() << "getNewAd()";
78     connect(service, SIGNAL(adRequestReady(QHash<QString, QVariant> &)),this, SLOT(adRequestSucceeded(QHash<QString, QVariant> &)));
79         connect(service, SIGNAL(adRequestFailed()),this, SLOT(adRequestFailed()));
80
81         QHash<QString, QVariant> adParams;
82         this->getAdAsHash(adParams);
83         this->service->getAd(adParams);
84 }
85
86 void QtmadsAdWidget::adRequestSucceeded(QHash<QString, QVariant> &ad)
87 {
88     qDebug() << "adRequestSucceeded()";
89     disconnect(service, SIGNAL(adRequestReady(QHash<QString, QVariant> &)),this, SLOT(adRequestSucceeded(QHash<QString, QVariant> &)));
90     disconnect(service, SIGNAL(adRequestFailed()),this, SLOT(adRequestFailed()));
91
92     ParseState adReady = parseWaiting;
93
94     // clear previous ad
95     this->clearAd();
96
97     if(adReady != parseFailed){
98         adReady = this->parseFromParameters(ad);
99     }
100
101     if(adReady == parseReady){
102         emit newAdReady();
103         show();
104     }else if(adReady == parseFailed){
105         emit newAdFailed();
106     }
107 }
108
109 void QtmadsAdWidget::adRequestFailed()
110 {
111     disconnect(service, SIGNAL(adRequestReady(QHash<QString, QVariant> &)),this, SLOT(adRequestSucceeded(QHash<QString, QVariant> &)));
112     disconnect(service, SIGNAL(adRequestFailed()),this, SLOT(adRequestFailed()));
113
114         emit newAdFailed();
115 }
116
117 void QtmadsAdWidget::adClicked(const QUrl &url)
118 {
119     if(url.isValid()){
120         qDebug() << "Browser opened to urli: " << url.toString();
121         QDesktopServices::openUrl(url);
122     }
123 }
124
125 void QtmadsAdWidget::clearAd()
126 {
127     adID = -1;
128     type = noAd;
129 }
130
131 bool QtmadsAdWidget::loadPlugin(QString serviceName)
132 {
133     bool pluginFound = false;
134     QDir pluginsDir(qApp->applicationDirPath());
135 #if defined(Q_OS_WIN)
136     if (pluginsDir.dirName().toLower() == "debug" || pluginsDir.dirName().toLower() == "release")
137         pluginsDir.cdUp();
138 #elif defined(Q_OS_MAC)
139     if (pluginsDir.dirName() == "MacOS") {
140         pluginsDir.cdUp();
141         pluginsDir.cdUp();
142         pluginsDir.cdUp();
143     }
144 #endif
145     pluginsDir.cd(PLUGINS_PATH);
146     foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
147         qDebug() << "path: " << pluginsDir.absoluteFilePath(fileName);
148         if(fileName.contains(serviceName) && !pluginFound){
149             QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
150             QObject *plugin = pluginLoader.instance();
151             if (plugin) {
152                 QtmadsServiceInterface *plugLoader = qobject_cast<QtmadsServiceInterface *>(plugin);
153                 this->service = plugLoader->loadService();
154                 if (this->service)
155                     pluginFound = true;
156                 else{
157                     qDebug() << "Plugin load failed: " << pluginLoader.errorString();
158                     }
159                 plugLoader = 0;
160                 delete plugin;
161             }else{
162                 qDebug() << "Plugin load failed: " << pluginLoader.errorString();
163             }
164         }else{
165             qDebug() << "not service";
166         }
167     }
168
169     return pluginFound;
170 }