Splashscreen ad support. Wait dialog for ad click/browser start.
[qtmads] / src / qtmadslabelad.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 "qtmadslabelad.h"
24
25 #include <QPixmap>
26 #include <QUrl>
27 #include <QDebug>
28
29 #include "qtmadsservice.h"
30
31 QtmadsLabelAd::QtmadsLabelAd(QString serviceName, quint32 adGroupId, AdFit fitting, QWidget *parent)
32     : QtmadsAdWidget(serviceName, imageAd, adGroupId, fitting, parent)
33 {
34     label = new QLabel(this);
35     QHBoxLayout *layout = new QHBoxLayout(this);
36     layout->addWidget(label);
37     this->setLayout(layout);
38 }
39
40 QtmadsLabelAd::~QtmadsLabelAd()
41 {
42     this->clearAd();
43 }
44
45 ParseState QtmadsLabelAd::parseFromParameters(QHash<QString, QVariant> &ad)
46 {
47     ParseState parseSuccess = parseReady;
48     bool textOrImageFound = false;
49
50     QHash<QString, QVariant>::iterator iter;
51
52     for(iter = ad.begin(); iter != ad.end() && parseSuccess != parseFailed; iter++){
53         //qDebug() << "key: " << iter.key() << " value: " << iter.value().toString();
54         if(0 == iter.key().compare(TAG_TEXT)){
55             label->setText(iter.value().toString());
56             if(this->adFit == fitAdToWidget){
57                 //TODO: fit text to ad
58                 /*QFontMetrics metrics (label->font());
59                 int pixelWidth = metrics.width(label->text());
60                 int pixelHeight = metrics.height();
61                 if()
62                 label->font().setPixelSize();*/
63             }/*TODO: else if(this->adFit == adToFit){
64             }*/
65             textOrImageFound = true;
66
67         }else if(0 == iter.key().compare(TAG_CONTENT_URL)){
68             connect(service, SIGNAL(imageRequestReady(const QImage *)),this, SLOT(imageRequestSucceeded(const QImage *)));
69             connect(service, SIGNAL(imageRequestFailed()),this, SLOT(imageRequestFailed()));
70             this->service->getRemoteImage(iter.value().toString());
71             parseSuccess = parseWaiting;
72             textOrImageFound = true;
73
74         }else if(0 == iter.key().compare(TAG_ADTYPE)){
75             this->setAdType(this->service->getAdTypeFromString(iter.value().toString()));
76
77         }else{
78             qDebug() << "Unknown tag.";
79         }
80     }
81
82     // either text or image should be found from ad
83     if(!textOrImageFound)
84         parseSuccess = parseFailed;
85
86     return parseSuccess;
87 }
88
89 void QtmadsLabelAd::imageRequestSucceeded(const QImage *image)
90 {
91     disconnect(service, SIGNAL(imageRequestReady(const QImage *)),this, SLOT(imageRequestSucceeded(const QImage *)));
92     disconnect(service, SIGNAL(imageRequestFailed()),this, SLOT(imageRequestFailed()));
93
94     if(image){
95         QPixmap pic;
96         if(this->adFit == fitAdToWidget){
97             pic = QPixmap::fromImage(*image).scaled(this->sizeHint());
98         }/*TODO: else if(this->adFit == fitWidgetToAd){
99         }*/else{
100             pic = QPixmap::fromImage(*image);
101         }
102         label->setPixmap(pic);
103
104         emit newAdReady();
105     }else{
106         emit newAdFailed();
107     }
108 }
109
110 void QtmadsLabelAd::imageRequestFailed()
111 {
112     emit newAdFailed();
113 }
114
115 void QtmadsLabelAd::getAdAsHash(QHash<QString, QVariant> &adParams)
116 {
117     //adParams.insert("adsize", QVariant(this->sizeHint()));
118 }
119
120 void QtmadsLabelAd::setAsTextAd(bool isOnlyTextAd)
121 {
122     if(isOnlyTextAd){
123         this->service->setDefaultAdType(txtAd);
124     }else{
125         //TODO: HASH it
126         if(this->service->defaultAdType() != txtAd)
127             this->service->setDefaultAdType(anyAd);
128     }
129 }
130
131 void QtmadsLabelAd::setAsImageAd(bool isOnlyImageAd)
132 {
133     if(isOnlyImageAd){
134         this->service->setDefaultAdType(imageAd);
135     }else{
136         //TODO: HASH it
137         if(this->service->defaultAdType() != imageAd)
138             this->service->setDefaultAdType(anyAd);
139     }
140 }
141
142 void QtmadsLabelAd::clearAd()
143 {
144     this->label->clear();
145 }