radio/lib/wModel/file/file.cpp

129 lines
3.3 KiB
C++
Raw Permalink Normal View History

2018-08-04 21:46:25 +00:00
#include "file.h"
#include <iostream>
2018-12-17 17:15:58 +00:00
#include "audio.h"
2018-08-04 21:46:25 +00:00
QMimeDatabase M::File::mimeDB;
2018-12-17 17:15:58 +00:00
const std::map<QString, M::Model::ModelType> M::File::mimeMap = {
{"image/jpeg", M::Model::file},
{"audio/mpeg", M::Model::audio}
};
2018-08-04 21:46:25 +00:00
M::File::File(W::Blob* p_file, const W::Address& addr, QObject* parent):
M::Model(addr, parent),
additional(),
file(p_file)
{
W::Handler* get = W::Handler::create(address + W::Address({u"get"}), this, &M::File::_h_get);
W::Handler* getAdditional = W::Handler::create(address + W::Address({u"getAdditional"}), this, &M::File::_h_getAdditional);
W::Handler* getSlice = W::Handler::create(address + W::Address({u"getSlice"}), this, &M::File::_h_getSlice);
2018-08-04 21:46:25 +00:00
addHandler(get);
addHandler(getAdditional);
addHandler(getSlice);
2018-08-04 21:46:25 +00:00
}
M::File::~File()
{
delete file;
}
M::Model::ModelType M::File::getType() const
{
return type;
}
void M::File::initAdditional(const W::String& p_mime)
{
additional.clear();
additional.insert(u"size", new W::Uint64(file->length()));
2018-08-04 21:46:25 +00:00
additional.insert(u"mimeType", p_mime);
}
void M::File::set(const W::Object& value)
{
set(value.copy());
}
void M::File::set(W::Object* value)
{
delete file;
file = static_cast<W::Blob*>(value);
QMimeType mt = mimeDB.mimeTypeForData(file->byteArray());
initAdditional(W::String(mt.name().toStdString()));
W::Vocabulary* vc = static_cast<W::Vocabulary*>(additional.copy());
broadcast(vc, W::Address({u"getAdditional"}));
}
void M::File::h_getAdditional(const W::Event& ev)
{
W::Vocabulary* vc = static_cast<W::Vocabulary*>(additional.copy());
response(vc, W::Address({u"getAdditional"}), ev);
}
void M::File::h_subscribe(const W::Event& ev)
{
M::Model::h_subscribe(ev);
h_getAdditional(ev);
}
void M::File::h_get(const W::Event& ev)
{
W::Vocabulary* vc = new W::Vocabulary();
vc->insert(u"additional", additional.copy());
vc->insert(u"data", file->copy());
response(vc, W::Address({u"get"}), ev);
}
M::File * M::File::create(W::Blob* blob, const W::Address& addr, QObject* parent)
{
M::File* out;
QMimeType mt = mimeDB.mimeTypeForData(blob->byteArray());
2018-12-17 17:15:58 +00:00
const QString& mime = mt.name();
std::map<QString, M::Model::ModelType>::const_iterator itr = mimeMap.find(mime);
M::Model::ModelType modelType = M::Model::file;
if (itr != mimeMap.end()) {
modelType = itr->second;
}
switch (modelType) {
case Model::audio:
out = new Audio(blob, addr, parent);
break;
default:
out = new File(blob, addr, parent);
break;
}
out->initAdditional(W::String(mime.toStdString()));
2018-08-04 21:46:25 +00:00
return out;
}
void M::File::h_getSlice(const W::Event& ev)
{
const W::Vocabulary& vc = static_cast<const W::Vocabulary&>(ev.getData());
const W::Uint64& begin = static_cast<const W::Uint64&>(vc.at(u"begin"));
const W::Uint64& size = static_cast<const W::Uint64&>(vc.at(u"size"));
W::Vocabulary* evc = new W::Vocabulary();
if (begin > file->length() || begin + size > file->length()) {
evc->insert(u"result", new W::Uint64(1));
} else {
evc->insert(u"result", new W::Uint64(0));
evc->insert(u"slice", file->slice(begin, size));
}
response(evc, W::Address{u"getSlice"}, ev);
}