SoundBand/sync/song.cpp

234 lines
4.5 KiB
C++
Raw Normal View History

2017-10-29 14:47:36 +03:00
#include "song.h"
2017-12-25 19:23:24 +03:00
#include <QStringList>
2018-03-25 23:01:44 +03:00
#include <QRegularExpression>
2018-03-29 00:23:53 +03:00
#include <QFile>
2018-03-25 23:01:44 +03:00
2017-11-13 23:05:59 +03:00
namespace syncLib{
2017-12-25 19:23:24 +03:00
static const QStringList ValidSongs = {".mp3", ".wav", ".ogg"};
2017-11-13 23:05:59 +03:00
SongHeader::SongHeader()
2017-10-29 14:47:36 +03:00
{
this->isSelected = false;
2017-11-20 00:37:12 +03:00
this->id = -1;
2017-10-29 14:47:36 +03:00
this->name = "";
this->size = 0;
}
2018-03-25 23:01:44 +03:00
bool SongHeader::getName(QString & name, const QUrl &url) const {
if(url.isLocalFile() && url.isValid()){
name = url.fileName();
name = name.right(name.lastIndexOf(QRegularExpression("[\\\/]")));
return true;
}
return false;
}
2018-04-05 21:54:49 +03:00
bool SongHeader::getSize(int & size, const QUrl &url) const {
if(url.isLocalFile() && url.isValid()){
QFile f(url.toLocalFile());
if(!f.exists()){
return false;
}
size = f.size();
return true;
}
return false;
}
2017-11-13 23:05:59 +03:00
SongHeader& SongHeader::operator =(const SongHeader& right){
2017-10-29 14:47:36 +03:00
this->id = right.id;
this->name = right.name;
this->size = right.size;
return *this;
}
2018-03-25 23:01:44 +03:00
SongHeader& SongHeader::operator =(const QMediaContent& right){
this->id = -1;
if(!getName(name, right.canonicalUrl())){
name.clear();
}
2018-04-05 21:54:49 +03:00
if(!getSize(this->size, right.canonicalUrl())){
this->size = 0;
}
2018-03-25 23:01:44 +03:00
return *this;
}
2018-04-05 21:54:49 +03:00
bool SongHeader::operator ==(const SongHeader& right)const{
2017-10-29 14:47:36 +03:00
return this->name == right.name && this->size == right.size;
}
2018-04-05 21:54:49 +03:00
bool SongHeader::operator ==(const QMediaContent& right)const{
2018-03-25 23:01:44 +03:00
QString name;
if(!getName(name, right.canonicalUrl())){
return false;
}
2018-04-05 21:54:49 +03:00
int size;;
if(!getSize(size, right.canonicalUrl())){
return false;
}
return this->name == name && this->size == size;
2017-11-06 13:20:52 +03:00
}
2017-10-29 14:47:36 +03:00
2017-12-26 12:54:33 +03:00
bool SongHeader::isNameValid() const{
2017-12-25 19:23:24 +03:00
bool CheckSongs = false;
for (QString i: ValidSongs){
CheckSongs = CheckSongs || name.endsWith(i);
}
2017-12-26 12:54:33 +03:00
return CheckSongs;
}
bool SongHeader::isValid() const{
return id > -1 && !name.isEmpty() && size > 0 && isNameValid();
2017-12-14 00:12:27 +03:00
2017-12-02 14:35:39 +03:00
}
SongHeader::~SongHeader(){}
2017-11-13 23:05:59 +03:00
QDataStream& operator << (QDataStream& stream, const SongHeader& song){
2017-11-06 13:20:52 +03:00
stream << song.id;
stream << song.name;
stream << song.size;
return stream;
}
2017-11-13 23:05:59 +03:00
QDataStream& operator >> (QDataStream& stream, SongHeader& song){
2017-11-06 13:20:52 +03:00
stream >> song.id;
stream >> song.name;
stream >> song.size;
return stream;
}
2018-03-18 14:30:48 +03:00
2018-03-29 00:23:53 +03:00
SongStorage::SongStorage():
SongHeader()
{
url.clear();
}
SongStorage::SongStorage(const SongHeader& from)
:SongStorage::SongStorage()
{
this->id = from.id;
this->name = from.name;
this->size = from.size;
}
SongStorage::SongStorage(const QUrl& from)
:SongStorage::SongStorage()
{
if(!from.isValid() || !from.isLocalFile()){
return;
}
2018-04-02 01:03:27 +03:00
QFile f(from.toLocalFile());
this->size = f.size();
f.close();
2018-03-29 00:23:53 +03:00
this->id = -1;
2018-04-02 01:03:27 +03:00
url = from;
2018-03-29 00:23:53 +03:00
if(!getName(name, from)){
name.clear();
}
}
2018-03-29 22:02:03 +03:00
SongStorage::SongStorage(const QMediaContent& from)
:SongStorage::SongStorage(from.canonicalUrl())
{
}
2018-03-29 00:23:53 +03:00
const QUrl& SongStorage::getSource()const{
return url;
}
bool SongStorage::isValid() const{
2018-04-02 01:03:27 +03:00
return SongHeader::isValid() && url.isValid() && QFile(url.toLocalFile()).exists();
2018-03-29 00:23:53 +03:00
}
SongStorage::~SongStorage(){
url.clear();
}
QMediaContent SongStorage::toMedia()const{
return QMediaContent(url);
}
bool SongStorage::toSong(Song&)const{
Song song(*((SongHeader*)this));
QFile f(url.toLocalFile());
if(!f.open(QIODevice::ReadOnly))
return false;
song.source = f.readAll();
f.close();
return song.isValid();
}
QDataStream& operator << (QDataStream& stream,const SongStorage& song){
stream << static_cast<const SongHeader&>(song);
stream << song.url;
return stream;
}
QDataStream& operator >> (QDataStream& stream, SongStorage& song){
stream >> static_cast<SongHeader&>(song);
stream >> song.url;
return stream;
}
2018-03-18 14:30:48 +03:00
Song::Song():
SongHeader()
{
source.clear();
}
2017-11-13 23:05:59 +03:00
Song::Song(const SongHeader& from)
:Song::Song()
2017-10-29 14:47:36 +03:00
{
this->id = from.id;
this->name = from.name;
this->size = from.size;
}
2017-11-13 23:05:59 +03:00
void Song::clear(){
2017-11-06 01:33:16 +03:00
source.clear();
}
2017-12-24 22:04:40 +03:00
const QByteArray& Song::getSource()const{
return source;
}
2017-12-02 14:35:39 +03:00
bool Song::isValid() const{
return SongHeader::isValid() && source.size() == size;
}
Song::~Song(){
source.clear();
}
2017-11-13 23:05:59 +03:00
QDataStream& operator << (QDataStream& stream,const Song& song){
stream << static_cast<const SongHeader&>(song);
2017-11-06 13:20:52 +03:00
stream << song.source;
2017-11-13 23:05:59 +03:00
return stream;
2017-11-06 13:20:52 +03:00
}
2017-11-13 23:05:59 +03:00
QDataStream& operator >> (QDataStream& stream, Song& song){
stream >> static_cast<SongHeader&>(song);
2017-11-06 13:20:52 +03:00
stream >> song.source;
2017-11-13 23:05:59 +03:00
return stream;
}
2017-11-06 13:20:52 +03:00
}