listensupport.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 2008-2010  Lukas Sommer < SommerLuk at gmail dot com >
00003 
00004     This program is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU General Public License as
00006     published by the Free Software Foundation; either version 2 of
00007     the License or (at your option) version 3 or any later version
00008     accepted by the membership of KDE e.V. (or its successor approved
00009     by the membership of KDE e.V.), which shall act as a proxy
00010     defined in Section 14 of version 3 of the license.
00011 
00012     This program is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 */
00020 
00021 #include "listensupport.h"
00022 
00023 #include <QTimer>
00024 #include <QCoreApplication>
00025 #include <QEventLoop>
00026 
00027 listenSupport::listenSupport(QObject *parent) : QObject(parent)
00028 {
00029   relayPort = -1;
00030   station = 0;
00031   setIsPlaying(false);
00032   media = new Phonon::MediaObject(this);
00033   audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
00034   createPath(media, audioOutput);
00035   connect(media, SIGNAL(stateChanged(Phonon::State, Phonon::State )),
00036           this, SLOT(processNewState(Phonon::State)));
00037 }
00038 
00039 listenSupport::~listenSupport()
00040 {
00041   delete audioOutput;
00042   delete media;
00043 }
00044 
00045 void listenSupport::setStation(radioStation *newStation)
00046 {
00047   if (newStation != station) {
00048     if (station) {
00049       disconnect(station, 0, this, 0);
00050     };
00051     // change station
00052     station = newStation;
00053     // connect new station
00054     if (station) {
00055       connect(station,
00056               SIGNAL(relayPortChanged(qlonglong, PropertyValue)),
00057               this,
00058               SLOT(setNoStation()));
00059       setRelayPort(0, station->relayPort());
00060       connect(station,
00061               SIGNAL(destroyed(QObject *)),
00062               this,
00063               SLOT(setNoStation()));
00064     } else {
00065       setRelayPort(0, PropertyValue());
00066     };
00067   };
00068 };
00069 
00070 void listenSupport::setNoStation()
00071 {
00072   setStation(0);
00073 }
00074 
00075 void listenSupport::setRelayPort(qlonglong, PropertyValue value)
00076 {
00077   if (value.type == PropertyValue::value) {
00078     const qlonglong newPort = value.internalValue.toLongLong();
00079     if (relayPort != newPort) {
00080       relayPort = newPort;
00081       reloadListening();
00082     };
00083   } else {
00084     if (relayPort >= 0) {
00085       relayPort = -1;
00086       reloadListening();
00087     };
00088   };
00089 }
00090 
00091 void listenSupport::reloadListening()
00092 {
00093   if (relayPort >= 0) {
00094     media->setCurrentSource(QString("http://localhost:%1").arg(relayPort));
00095     media->play();
00096   } else {
00097     media->stop();
00098   };
00099 }
00100 
00101 bool listenSupport::isPlaying() const
00102 {
00103   return internal_isPlaying;
00104 }
00105 
00106 void listenSupport::setIsPlaying(bool newValue)
00107 {
00108   if (newValue != internal_isPlaying) {
00109     internal_isPlaying = newValue;
00110     emit isPlayingChanged(newValue);
00111   };
00112 }
00113 
00114 void listenSupport::processNewState(Phonon::State newstate)
00115 {
00116   setIsPlaying(newstate == Phonon::LoadingState ||
00117                newstate == Phonon::PlayingState ||
00118                newstate == Phonon::BufferingState);
00119 }

doxygen