--- /dev/null
+/*
+ * Copyright (c) 2009 Eetu Lehmusvuo.
+ *
+ * This file is part of QtMAds.
+ *
+ * QtMAds is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * QtMAds is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with QtMAds. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <QtGui>
+
+#include "qtmadsmediaad.h"
+
+#include <QPixmap>
+#include <QUrl>
+#include <QDebug>
+
+#include "qtmadsservice.h"
+
+QtmadsMediaAd::QtmadsMediaAd(QString serviceName, quint32 adGroupId, AdFit fitting, QWidget *parent)
+ : QtmadsAdWidget(serviceName, imageAd, adGroupId, fitting, parent)
+{
+ this->player = 0;
+ QHBoxLayout *layout = new QHBoxLayout(this);
+ this->setLayout(layout);
+}
+
+QtmadsMediaAd::~QtmadsMediaAd()
+{
+ this->clearAd();
+
+ if(player){
+ delete player;
+ player = 0;
+ }
+}
+
+ParseState QtmadsMediaAd::parseFromParameters(QHash<QString, QVariant> &ad)
+{
+ ParseState parseSuccess = parseReady;
+ bool textOrImageFound = false;
+
+ QHash<QString, QVariant>::iterator iter;
+
+ for(iter = ad.begin(); iter != ad.end() && parseSuccess != parseFailed; iter++){
+ //qDebug() << "key: " << iter.key() << " value: " << iter.value().toString();
+ if(0 == iter.key().compare(TAG_CONTENT_URL)){
+ this->mediaContentUrl = iter.value().toString();
+ if(!player){
+ if(this->adType() == audioAd){
+ player = Phonon::createPlayer(Phonon::MusicCategory,
+ Phonon::MediaSource(this->mediaContentUrl));
+
+ Phonon::AudioOutput *audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, player);
+ Phonon::createPath(player, audioOutput);
+ }else{
+ player = Phonon::createPlayer(Phonon::VideoCategory,
+ Phonon::MediaSource(this->mediaContentUrl));
+ this->videoWidget = new Phonon::VideoWidget(this);
+ Phonon::createPath(player, videoWidget);
+
+ Phonon::AudioOutput *audioOutput = new Phonon::AudioOutput(Phonon::VideoCategory, this);
+ Phonon::createPath(player, audioOutput);
+
+ this->layout()->addWidget(videoWidget);
+ }
+ }else{
+ player->setCurrentSource(Phonon::MediaSource(this->mediaContentUrl));
+ }
+
+ connect(player, SIGNAL(stateChanged(Phonon::State newstate, Phonon::State oldstate)),
+ this, SLOT(phononPlayerStateChanged(Phonon::State newstate, Phonon::State oldstate)));
+ connect(player, SIGNAL(finished ()),
+ this, SLOT(phononPlayerStateChanged()));
+ player->play();
+
+ qDebug() << "media play started!";
+
+ }else if(0 == iter.key().compare(TAG_ADTYPE)){
+ this->setAdType(this->service->getAdTypeFromString(iter.value().toString()));
+
+ }else{
+ qDebug() << "Unknown tag.";
+ }
+ }
+
+ // either text or image should be found from ad
+ if(!textOrImageFound)
+ parseSuccess = parseFailed;
+
+ return parseSuccess;
+}
+
+void QtmadsMediaAd::getAdAsHash(QHash<QString, QVariant> &adParams)
+{
+ //adParams.insert("adsize", QVariant(this->sizeHint()));
+}
+
+void QtmadsMediaAd::setAsAudioAd(bool isOnlyAudioAd)
+{
+ if(this->service->defaultAdType() == videoAd){
+ // can't be video and audio ad..
+ this->clearAd();
+ if(player){
+ this->layout()->removeWidget(videoWidget);
+ delete player;
+ player = 0;
+ }
+ }
+
+ if(isOnlyAudioAd){
+ this->service->setDefaultAdType(audioAd);
+ }else{
+ //TODO: HASH it
+ if(this->service->defaultAdType() != audioAd)
+ this->service->setDefaultAdType(anyAd);
+ }
+}
+
+void QtmadsMediaAd::setAsVideoAd()
+{
+ if(this->service->defaultAdType() == audioAd){
+ // clear audio ad first..
+ this->clearAd();
+ if(player){
+ delete player;
+ player = 0;
+ }
+ }
+ this->service->setDefaultAdType(videoAd);
+}
+
+void QtmadsMediaAd::clearAd()
+{
+ this->mediaContentUrl.clear();
+
+ if(this->player){
+ disconnect(player, SIGNAL(stateChanged(Phonon::State newstate, Phonon::State oldstate)),
+ this, SLOT(phononState(Phonon::State newstate, Phonon::State oldstate)));
+ disconnect(player, SIGNAL(finished ()), this, SLOT(phononState()));
+ player->stop();
+ }
+}