First version. ADHERE service support with label, banner and media ads.
[qtmads] / src / qtmadsmediaad.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 "qtmadsmediaad.h"
24
25 #include <QPixmap>
26 #include <QUrl>
27 #include <QDebug>
28
29 #include "qtmadsservice.h"
30
31 QtmadsMediaAd::QtmadsMediaAd(QString serviceName, quint32 adGroupId, AdFit fitting, QWidget *parent)
32     : QtmadsAdWidget(serviceName, imageAd, adGroupId, fitting, parent)
33 {
34     this->player = 0;
35     QHBoxLayout *layout = new QHBoxLayout(this);
36     this->setLayout(layout);
37 }
38
39 QtmadsMediaAd::~QtmadsMediaAd()
40 {
41     this->clearAd();
42
43     if(player){
44         delete player;
45         player = 0;
46     }
47 }
48
49 ParseState QtmadsMediaAd::parseFromParameters(QHash<QString, QVariant> &ad)
50 {
51     ParseState parseSuccess = parseReady;
52     bool textOrImageFound = false;
53
54     QHash<QString, QVariant>::iterator iter;
55
56     for(iter = ad.begin(); iter != ad.end() && parseSuccess != parseFailed; iter++){
57         //qDebug() << "key: " << iter.key() << " value: " << iter.value().toString();
58         if(0 == iter.key().compare(TAG_CONTENT_URL)){
59             this->mediaContentUrl = iter.value().toString();
60             if(!player){
61                 if(this->adType() == audioAd){
62                     player = Phonon::createPlayer(Phonon::MusicCategory,
63                                              Phonon::MediaSource(this->mediaContentUrl));
64
65                     Phonon::AudioOutput *audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, player);
66                     Phonon::createPath(player, audioOutput);
67                 }else{
68                     player = Phonon::createPlayer(Phonon::VideoCategory,
69                                              Phonon::MediaSource(this->mediaContentUrl));
70                     this->videoWidget = new Phonon::VideoWidget(this);
71                     Phonon::createPath(player, videoWidget);
72
73                     Phonon::AudioOutput *audioOutput = new Phonon::AudioOutput(Phonon::VideoCategory, this);
74                     Phonon::createPath(player, audioOutput);
75
76                     this->layout()->addWidget(videoWidget);
77                 }
78             }else{
79                 player->setCurrentSource(Phonon::MediaSource(this->mediaContentUrl));
80             }
81
82             connect(player, SIGNAL(stateChanged(Phonon::State newstate, Phonon::State oldstate)),
83                     this, SLOT(phononPlayerStateChanged(Phonon::State newstate, Phonon::State oldstate)));
84             connect(player, SIGNAL(finished ()),
85                     this, SLOT(phononPlayerStateChanged()));
86             player->play();
87
88             qDebug() << "media play started!";
89
90         }else if(0 == iter.key().compare(TAG_ADTYPE)){
91             this->setAdType(this->service->getAdTypeFromString(iter.value().toString()));
92
93         }else{
94             qDebug() << "Unknown tag.";
95         }
96     }
97
98     // either text or image should be found from ad
99     if(!textOrImageFound)
100         parseSuccess = parseFailed;
101
102     return parseSuccess;
103 }
104
105 void QtmadsMediaAd::getAdAsHash(QHash<QString, QVariant> &adParams)
106 {
107     //adParams.insert("adsize", QVariant(this->sizeHint()));
108 }
109
110 void QtmadsMediaAd::setAsAudioAd(bool isOnlyAudioAd)
111 {
112     if(this->service->defaultAdType() == videoAd){
113         // can't be video and audio ad..
114         this->clearAd();
115         if(player){
116             this->layout()->removeWidget(videoWidget);
117             delete player;
118             player = 0;
119         }
120     }
121
122     if(isOnlyAudioAd){
123         this->service->setDefaultAdType(audioAd);
124     }else{
125         //TODO: HASH it
126         if(this->service->defaultAdType() != audioAd)
127             this->service->setDefaultAdType(anyAd);
128     }
129 }
130
131 void QtmadsMediaAd::setAsVideoAd()
132 {
133     if(this->service->defaultAdType() == audioAd){
134         // clear audio ad first..
135         this->clearAd();
136         if(player){
137             delete player;
138             player = 0;
139         }
140     }
141     this->service->setDefaultAdType(videoAd);
142 }
143
144 void QtmadsMediaAd::clearAd()
145 {
146     this->mediaContentUrl.clear();
147
148     if(this->player){
149         disconnect(player, SIGNAL(stateChanged(Phonon::State newstate, Phonon::State oldstate)),
150                 this, SLOT(phononState(Phonon::State newstate, Phonon::State oldstate)));
151         disconnect(player, SIGNAL(finished ()), this, SLOT(phononState()));
152         player->stop();
153     }
154 }