initial player stuff

This commit is contained in:
Blue 2018-08-05 00:52:16 +03:00 committed by Юрий Губич
parent 4b60ece582
commit 5100d91a72
38 changed files with 1107 additions and 93 deletions

View file

@ -23,6 +23,7 @@ set(SOURCES
)
add_executable(corax ${HEADERS} ${SOURCES})
add_subdirectory(models)
target_link_libraries(corax Qt5::Core)
target_link_libraries(corax Qt5::Network)
@ -31,10 +32,10 @@ target_link_libraries(corax wSocket)
target_link_libraries(corax wDispatcher)
target_link_libraries(corax utils)
target_link_libraries(corax wModel)
target_link_libraries(corax wController)
target_link_libraries(corax wServerUtils)
target_link_libraries(corax wDatabase)
target_link_libraries(corax tag)
target_link_libraries(corax tools)
target_link_libraries(corax coraxModels)
target_link_libraries(corax wServerUtils)
install(TARGETS corax RUNTIME DESTINATION bin)

View file

@ -90,11 +90,11 @@ void Corax::start()
server->listen(8080);
cout << "Registering models..." << endl;
attributes->registerModel(dispatcher, server);
commands->registerModel(dispatcher, server);
attributes->getRegistered(connector);
commands->getRegistered(connector);
for (; beg != end; ++beg) {
beg->second->registerModel(dispatcher, server);
beg->second->getRegistered(connector);
}
cout << "Opening caches..." << endl;
@ -115,11 +115,11 @@ void Corax::stop()
std::map<W::String, ResourceCache*>::iterator end = caches.end();
cout << "Stopping corax..." << endl;
commands->unregisterModel();
attributes->unregisterModel();
commands->getUnregistered();
attributes->getUnregistered();
for (; beg != end; ++beg) {
beg->second->unregisterModel();
beg->second->getUnregistered();
}
server->stop();
@ -169,10 +169,10 @@ void Corax::h_parseDirectory(const W::Event& ev)
if (itr != parsers.end()) {
cout << "directory " << path.toString() << " is already being parsed" << endl;
} else {
const W::Socket& socket = connector->getNodeSocket(W::String(u"Perturabo"));
const W::Socket* socket = connector->getNodeSocket(W::String(u"Perturabo"));
ResourceCache* music = caches.at(W::String(u"music"));
ResourceCache* images = caches.at(W::String(u"images"));
Parser* parser = new Parser(&socket, dispatcher, music, images);
Parser* parser = new Parser(socket, dispatcher, music, images);
parsers.insert(std::make_pair(path, parser));
connect(parser, SIGNAL(serviceMessage(const QString&)), SLOT(onModelServiceMessage(const QString&)));

View file

@ -0,0 +1,21 @@
cmake_minimum_required(VERSION 2.8.12)
project(coraxModels)
find_package(Qt5Core REQUIRED)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(HEADERS
player.h
proxysong.h
)
set(SOURCES
player.cpp
proxysong.cpp
)
add_library(coraxModels STATIC ${HEADERS} ${SOURCES})
target_link_libraries(coraxModels wModel)

121
corax/models/player.cpp Normal file
View file

@ -0,0 +1,121 @@
#include "player.h"
M::Player::Player(const W::Address& address, QObject* parent):
M::Model(address, parent),
controls(),
views(),
playPauseBtn(new M::Button(address + W::Address{u"play"})),
_queueView(new M::List(address + W::Address{u"queueView"})),
_queue(),
current(0)
{
W::Handler* get = W::Handler::create(address + W::Address({u"get"}), this, &M::Player::_h_get);
W::Handler* hqueue = W::Handler::create(address + W::Address({u"queue"}), this, &M::Player::_h_queue);
addHandler(get);
addHandler(hqueue);
playPauseBtn->setLabel(W::String(u"play"));
playPauseBtn->setEnabled(false);
connect(playPauseBtn, SIGNAL(activated()), this, SLOT(onPlayPauseBtn()));
addModel(playPauseBtn);
addModel(_queueView);
controls.insert(std::make_pair(playPause, playPauseBtn->getAddress()));
views.insert(std::make_pair(queue, _queueView->getAddress()));
}
M::Player::~Player()
{
}
void M::Player::set(const W::Object& value)
{
throw 14; //what do you expect here? not implemented, and not sure it ever would be
}
void M::Player::set(W::Object* value)
{
set(*value);
}
M::Model::ModelType M::Player::getType() const
{
return M::Model::player;
}
void M::Player::h_subscribe(const W::Event& ev)
{
M::Model::h_subscribe(ev);
h_get(ev);
}
void M::Player::h_get(const W::Event& ev)
{
W::Vector* ctrls = new W::Vector();
ItemMap::const_iterator citr = controls.begin();
ItemMap::const_iterator cend = controls.end();
for (; citr != cend; ++citr) {
W::Vocabulary* cvc = new W::Vocabulary();
cvc->insert(u"type", new W::Uint64(cend->first));
cvc->insert(u"address", cend->second);
ctrls->push(cvc);
}
W::Vector* vws = new W::Vector();
ItemMap::const_iterator vitr = views.begin();
ItemMap::const_iterator vend = views.end();
for (; vitr != vend; ++vitr) {
W::Vocabulary* vvc = new W::Vocabulary();
vvc->insert(u"type", new W::Uint64(vend->first));
vvc->insert(u"address", vend->second);
vws->push(vvc);
}
W::Vocabulary* res = new W::Vocabulary();
res->insert(u"controls", ctrls);
res->insert(u"views", vws);
response(res, W::Address({u"get"}), ev);
}
void M::Player::onPlayPauseBtn()
{
}
void M::Player::h_queue(const W::Event& ev)
{
const W::Vocabulary& data = static_cast<const W::Vocabulary&>(ev.getData());
const W::Uint64& id = static_cast<const W::Uint64&>(data.at(u"id"));
ProxySong* song = new ProxySong(id, address + W::Address{u"currentPlayback"});
addModel(song);
if (current == 0) {
current = song;
views.insert(std::make_pair(currentPlayback, song->getAddress()));
W::Vocabulary* avc = new W::Vocabulary();
avc->insert(u"type", new W::Uint64(currentPlayback));
avc->insert(u"address", song->getAddress());
W::Vector* add = new W::Vector();
add->push(avc);
W::Vocabulary* res = new W::Vocabulary();
res->insert(u"add", add);
res->insert(u"remove", new W::Vector());
broadcast(res, W::Address{u"viewsChange"});
} else {
_queue.push_back(song);
_queueView->push(song->getAddress());
}
}

60
corax/models/player.h Normal file
View file

@ -0,0 +1,60 @@
#ifndef PLAYER_H
#define PLAYER_H
#include <map>
#include <deque>
#include <wModel/model.h>
#include <wModel/button.h>
#include <wModel/modelstring.h>
#include <wModel/list.h>
#include <wType/vocabulary.h>
#include <wType/vector.h>
#include <wType/address.h>
#include "proxysong.h"
/**
* @todo write docs
*/
namespace M {
class Player : public M::Model {
Q_OBJECT
public:
Player(const W::Address& address, QObject* parent = 0);
~Player();
void set(const W::Object & value) override;
void set(W::Object * value) override;
M::Model::ModelType getType() const override;
enum ItemType {
playPause,
currentPlayback,
queue
};
protected:
void h_subscribe(const W::Event & ev) override;
handler(get);
handler(queue);
private:
typedef std::map<ItemType, const W::Address&> ItemMap;
typedef std::deque<ProxySong*> Queue;
ItemMap controls;
ItemMap views;
M::Button* playPauseBtn;
M::List* _queueView;
Queue _queue;
ProxySong* current;
private slots:
void onPlayPauseBtn();
};
}
#endif // PLAYER_H

135
corax/models/proxysong.cpp Normal file
View file

@ -0,0 +1,135 @@
#include "proxysong.h"
ProxySong::ProxySong(const W::Uint64& p_id, const W::Address& p_address, QObject* parent):
M::Vocabulary(p_address, parent),
songCtrl(new C::Vocabulary(W::Address{u"songs", W::String(p_id.toString())})),
albumCtrl(0),
artistCtrl(0),
fileId(0),
_ready(false)
{
addController(songCtrl, W::String(u"Perturabo"));
connect(songCtrl, SIGNAL(newElement(const W::String&, const W::Object&)), SLOT(onSongNewElement(const W::String&, const W::Object&)));
connect(songCtrl, SIGNAL(removeElement(const W::String&)), SLOT(onSongRemoveElement(const W::String&)));
insert(W::String(u"id"), p_id);
insert(W::String(u"artist"), new W::String(u"undefined"));
insert(W::String(u"album"), new W::String(u"undefined"));
insert(W::String(u"name"), new W::String(u"undefined"));
insert(W::String(u"image"), new W::Uint64(0));
}
bool ProxySong::isReady() const
{
return _ready;
}
ProxySong::~ProxySong()
{
}
void ProxySong::onSongNewElement(const W::String& key, const W::Object& element)
{
if (key == u"name") {
insert(key, element);
} else if (key == u"audio") {
if (_ready) {
_ready = false;
emit notReady();
}
fileId = static_cast<const W::Uint64&>(element);
_ready = true;
emit ready();
} else if (key == u"artist") {
if (artistCtrl != 0) {
removeController(artistCtrl);
disconnect(artistCtrl, SIGNAL(newElement(const W::String&, const W::Object&)), this, SLOT(onArtistNewElement(const W::String&, const W::Object&)));
disconnect(artistCtrl, SIGNAL(removeElement(const W::String&)), this, SLOT(onAtristRemoveElement(const W::String&)));
artistCtrl->deleteLater();
}
const W::Uint64& aid = static_cast<const W::Uint64&>(element);
artistCtrl = new C::Vocabulary(W::Address{u"artists", W::String(aid.toString())});
addController(artistCtrl, W::String(u"Perturabo"));
connect(artistCtrl, SIGNAL(newElement(const W::String&, const W::Object&)), SLOT(onArtistNewElement(const W::String&, const W::Object&)));
connect(artistCtrl, SIGNAL(removeElement(const W::String&)), SLOT(onAtristRemoveElement(const W::String&)));
} else if (key == u"album") {
if (albumCtrl != 0) {
removeController(albumCtrl);
disconnect(albumCtrl, SIGNAL(newElement(const W::String&, const W::Object&)), this, SLOT(onAlbumNewElement(const W::String&, const W::Object&)));
disconnect(albumCtrl, SIGNAL(removeElement(const W::String&)), this, SLOT(onAlbumRemoveElement(const W::String&)));
albumCtrl->deleteLater();
}
const W::Uint64& aid = static_cast<const W::Uint64&>(element);
albumCtrl = new C::Vocabulary(W::Address{u"albums", W::String(aid.toString())});
addController(albumCtrl, W::String(u"Perturabo"));
connect(albumCtrl, SIGNAL(newElement(const W::String&, const W::Object&)), SLOT(onAlbumNewElement(const W::String&, const W::Object&)));
connect(albumCtrl, SIGNAL(removeElement(const W::String&)), SLOT(onAlbumRemoveElement(const W::String&)));
}
}
void ProxySong::onSongRemoveElement(const W::String& key)
{
if (key == u"name") {
insert(key, new W::String(u"undefined"));
} else if (key == u"audio") {
if (_ready) {
_ready = false;
fileId = W::Uint64(0);
emit notReady();
}
} else if (key == u"artist") {
if (artistCtrl != 0) {
removeController(artistCtrl);
disconnect(artistCtrl, SIGNAL(newElement(const W::String&, const W::Object&)), this, SLOT(onArtistNewElement(const W::String&, const W::Object&)));
disconnect(artistCtrl, SIGNAL(removeElement(const W::String&)), this, SLOT(onAtristRemoveElement(const W::String&)));
artistCtrl->deleteLater();
artistCtrl = 0;
}
} else if (key == u"album") {
if (albumCtrl != 0) {
removeController(albumCtrl);
disconnect(albumCtrl, SIGNAL(newElement(const W::String&, const W::Object&)), this, SLOT(onAlbumNewElement(const W::String&, const W::Object&)));
disconnect(albumCtrl, SIGNAL(removeElement(const W::String&)), this, SLOT(onAlbumRemoveElement(const W::String&)));
albumCtrl->deleteLater();
albumCtrl = 0;
}
}
}
void ProxySong::onAlbumNewElement(const W::String& key, const W::Object& element)
{
if (key == u"name") {
insert(W::String(u"album"), element);
} else if (key == u"image") {
insert(key, element);
}
}
void ProxySong::onAlbumRemoveElement(const W::String& key)
{
if (key == u"name") {
insert(W::String(u"album"), new W::String(u"undefined"));
} else if (key == u"image") {
insert(key, new W::Uint64(0));
}
}
void ProxySong::onArtistNewElement(const W::String& key, const W::Object& element)
{
if (key == u"name") {
insert(W::String(u"artist"), element);
}
}
void ProxySong::onArtistRemoveElement(const W::String& key)
{
if (key == u"name") {
insert(W::String(u"artist"), new W::String(u"undefined"));
}
}
const W::Uint64 & ProxySong::getFileId() const
{
return fileId;
}

47
corax/models/proxysong.h Normal file
View file

@ -0,0 +1,47 @@
#ifndef PROXYSONG_H
#define PROXYSONG_H
/**
* @todo write docs
*/
#include <wType/uint64.h>
#include <wType/address.h>
#include <wModel/vocabulary.h>
#include <wController/vocabulary.h>
class ProxySong : public M::Vocabulary {
Q_OBJECT
public:
ProxySong(const W::Uint64& p_id, const W::Address& p_address, QObject* parent = 0);
~ProxySong();
const W::Uint64& getFileId() const;
bool isReady() const;
signals:
void ready();
void notReady();
private:
C::Vocabulary* songCtrl;
C::Vocabulary* albumCtrl;
C::Vocabulary* artistCtrl;
W::Uint64 fileId;
bool _ready;
private slots:
void onSongNewElement(const W::String& key, const W::Object& element);
void onSongRemoveElement(const W::String& key);
void onAlbumNewElement(const W::String& key, const W::Object& element);
void onAlbumRemoveElement(const W::String& key);
void onArtistNewElement(const W::String& key, const W::Object& element);
void onArtistRemoveElement(const W::String& key);
};
#endif // PROXYSONG_H