defuze.me  Client
remotecontrol.cpp
00001 /**************************************************************************
00002 ** defuze.me Epitech Innovative Project
00003 **
00004 ** Copyright 2010-2011
00005 **   Athena Calmettes - Jocelyn De La Rosa - Francois Gaillard
00006 **   Adrien Jarthon - Alexandre Moore - Luc Peres - Arnaud Sellier
00007 **
00008 ** All rights reserved.
00009 **************************************************************************/
00010 
00011 #include "remotecontrol.hpp"
00012 #include "queuetrack.hpp"
00013 
00014 using namespace Remote;
00015 
00016 RemoteControl::RemoteControl()
00017 {
00018 }
00019 
00020 RemoteControl::~RemoteControl()
00021 {
00022 }
00023 
00024 void    RemoteControl::init()
00025 {
00026     play_queue = plugins->cast<Queue::PlayQueue>("queue");
00027     player = plugins->cast<Player::MainPlayer>("player");
00028     connect(cores->net(), SIGNAL(newRemoteClient(RemoteSock*)), SLOT(newRemoteClient(RemoteSock*)));
00029     connect(play_queue, SIGNAL(popQueue()), SLOT(popQueue()));
00030     connect(play_queue, SIGNAL(removeQueueElem(Queue::Queueable*)), SLOT(removeQueueElem(Queue::Queueable*)));
00031     connect(play_queue, SIGNAL(addQueueElem(Queue::Queueable*)), SLOT(addQueueElem(Queue::Queueable*)));
00032     connect(player, SIGNAL(played()), SLOT(playStatusChanged()));
00033     connect(player, SIGNAL(paused()), SLOT(playStatusChanged()));
00034     connect(player, SIGNAL(stopped()), SLOT(playStatusChanged()));
00035 }
00036 
00037 void        RemoteControl::newRemoteClient(RemoteSock* sock)
00038 {
00039     clients.insert(sock);
00040     connect(sock, SIGNAL(receiveEvent(const RemoteEvent&)), SLOT(receiveEvent(const RemoteEvent&)));
00041     connect(sock, SIGNAL(destroyed()), SLOT(removeRemoteClient()));
00042     sendPlayQueue(sock);
00043     sendPlayState(sock);
00044 }
00045 
00046 void        RemoteControl::removeRemoteClient()
00047 {
00048     clients.remove(static_cast<RemoteSock*>(sender()));
00049 }
00050 
00051 void        RemoteControl::sendToAll(const QString& event, const QVariantMap& data)
00052 {
00053     QSet<RemoteSock*>::const_iterator   it;
00054     for (it = clients.begin(); it != clients.end(); it++)
00055     {
00056         RemoteSock  *client = (*it);
00057         RemoteEvent &evt = client->newEvent(event);
00058 
00059         evt.setData(data);
00060         evt.send();
00061     }
00062 }
00063 
00064 void        RemoteControl::receiveEvent(const RemoteEvent &packet)
00065 {
00066     if (packet.getEvent() == "removeQueueElem")
00067         removeQueueElem(packet);
00068     else if (packet.getEvent() == "addQueueElem")
00069         addQueueElem(packet);
00070     else if (packet.getEvent() == "moveQueueElem")
00071         moveQueueElem(packet);
00072     else if (packet.getEvent() == "play")
00073         play(packet);
00074     else if (packet.getEvent() == "pause")
00075         pause(packet);
00076     else if (packet.getEvent() == "stop")
00077         stop(packet);
00078     else if (packet.getEvent() == "next")
00079         next(packet);
00080     else
00081     {
00082         qDebug() << "Unknow event received: " << packet.getEvent();
00083     }
00084 }
00085 
00086 void        RemoteControl::popQueue()
00087 {
00088     sendToAll("popQueue");
00089 }
00090 
00091 void        RemoteControl::removeQueueElem(Queue::Queueable* elem)
00092 {
00093     sendToAll("removeQueueElem", elem->getNetData());
00094 }
00095 
00096 void        RemoteControl::removeQueueElem(const RemoteEvent &packet)
00097 {
00098     unsigned int        position = packet["position"].toInt();
00099 
00100     if (position >= play_queue->getQueue().size())
00101         return packet.autoReply(RemoteEvent::ERROR);
00102     play_queue->remove(position);
00103     play_queue->emitAltered();
00104     packet.autoReply(RemoteEvent::OK);
00105 }
00106 
00107 void        RemoteControl::addQueueElem(Queue::Queueable* elem)
00108 {
00109     sendToAll("newQueueElem", elem->getNetData());
00110 }
00111 
00112 void        RemoteControl::addQueueElem(const RemoteEvent &packet)
00113 {
00114     unsigned int        position = packet["position"].toInt();
00115     int                 track_id = packet["content"].toMap()["id"].toInt();
00116     Library::AudioTrack *track = Library::AudioTrack::getTrack(track_id);
00117     if (!track)
00118         return packet.autoReply(RemoteEvent::ERROR);
00119     if (position >= play_queue->getQueue().size())
00120         return packet.autoReply(RemoteEvent::ERROR);
00121 
00122     play_queue->add(new Queue::QueueTrack(*track), position);
00123     play_queue->emitAltered();
00124     packet.autoReply(RemoteEvent::OK);
00125 }
00126 
00127 void        RemoteControl::moveQueueElem(const RemoteEvent &packet)
00128 {
00129     unsigned int        oldPosition = packet["oldPosition"].toInt();
00130     unsigned int        newPosition = packet["position"].toInt();
00131     if (oldPosition >= play_queue->getQueue().size())
00132         return packet.autoReply(RemoteEvent::ERROR);
00133     Queue::Queueable*   elem = play_queue->getQueue()[oldPosition];
00134     Library::AudioTrack *track = elem->toQueueTrack()->getTrack();
00135     if (!track)
00136         return packet.autoReply(RemoteEvent::ERROR);
00137     if (newPosition >= play_queue->getQueue().size())
00138         return packet.autoReply(RemoteEvent::ERROR);
00139 
00140     if (newPosition == oldPosition)
00141         return packet.autoReply(RemoteEvent::NO_CHANGES);
00142 
00143     play_queue->remove(oldPosition);
00144     play_queue->add(new Queue::QueueTrack(*track), newPosition);
00145     play_queue->emitAltered();
00146     packet.autoReply(RemoteEvent::OK);
00147 }
00148 
00149 void        RemoteControl::play(const RemoteEvent &packet)
00150 {
00151     if (player->play())
00152         packet.autoReply(RemoteEvent::OK);
00153     else
00154         packet.autoReply(RemoteEvent::NO_CHANGES);
00155 }
00156 
00157 void        RemoteControl::pause(const RemoteEvent &packet)
00158 {
00159     if (player->pause())
00160         packet.autoReply(RemoteEvent::OK);
00161     else
00162         packet.autoReply(RemoteEvent::NO_CHANGES);
00163 }
00164 
00165 void        RemoteControl::stop(const RemoteEvent &packet)
00166 {
00167     if (player->stop())
00168         packet.autoReply(RemoteEvent::OK);
00169     else
00170         packet.autoReply(RemoteEvent::NO_CHANGES);
00171 }
00172 
00173 void        RemoteControl::next(const RemoteEvent &packet)
00174 {
00175     if (player->crossfadeNext())
00176         packet.autoReply(RemoteEvent::OK);
00177     else
00178         packet.autoReply(RemoteEvent::NO_CHANGES);
00179 }
00180 
00181 void        RemoteControl::playStatusChanged()
00182 {
00183     QString     state = player->isPlaying() ? "play" : "pause";
00184     sendToAll(state);
00185 }
00186 
00187 void        RemoteControl::sendPlayQueue(RemoteSock* client)
00188 {
00189     Queue::QueueableDeque::const_iterator   it;
00190     for (it = play_queue->getQueue().begin(); it != play_queue->getQueue().end(); it++)
00191     {
00192         Queue::Queueable    *elem = (*it);
00193         RemoteEvent         &evt = client->newEvent("newQueueElem");
00194 
00195         evt.setData(elem->getNetData());
00196         evt.send();
00197     }
00198 }
00199 
00200 void        RemoteControl::sendPlayState(RemoteSock* client)
00201 {
00202     QString     state = player->isPlaying() ? "play" : "pause";
00203     RemoteEvent &evt = client->newEvent(state);
00204     evt.send();
00205 }