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>
|
|
|
|
|
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
|
|
|
{
|
2018-03-06 13:28:42 +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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
this->size = right.canonicalResource().dataSize();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-11-13 23:05:59 +03:00
|
|
|
bool SongHeader::operator ==(const SongHeader& right){
|
2017-10-29 14:47:36 +03:00
|
|
|
return this->name == right.name && this->size == right.size;
|
|
|
|
}
|
|
|
|
|
2018-03-25 23:01:44 +03:00
|
|
|
bool SongHeader::operator ==(const QMediaContent& right){
|
|
|
|
QString name;
|
|
|
|
if(!getName(name, right.canonicalUrl())){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return this->name == name && this->size == right.canonicalResource().dataSize();
|
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
|
|
|
|
|
|
|
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
|
|
|
}
|