strawberry-audio-player-win.../3rdparty/taglib/tagunion.cpp

252 lines
7.3 KiB
C++
Raw Normal View History

2020-06-26 23:30:30 +02:00
/***************************************************************************
2018-05-10 15:29:28 +02:00
copyright : (C) 2002 - 2008 by Scott Wheeler
email : wheeler@kde.org
***************************************************************************/
/***************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License version *
* 2.1 as published by the Free Software Foundation. *
* *
* This library is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
* 02110-1301 USA *
* *
* Alternatively, this file is available under the Mozilla Public *
* License Version 1.1. You may obtain a copy of the License at *
* http://www.mozilla.org/MPL/ *
***************************************************************************/
2020-06-26 23:30:30 +02:00
#include <memory>
2020-06-27 00:06:02 +02:00
#include "tagunion.h"
#include "tstringlist.h"
#include "tpropertymap.h"
#include "tpicturemap.h"
2020-06-26 23:30:30 +02:00
#define stringUnion(method) \
for (size_t j = 0; j < COUNT; ++j) { \
String val = d->tags[j] ? d->tags[j]->method() : String(); \
if (!val.isEmpty()) \
return val; \
} \
2020-06-13 19:02:42 +02:00
return String();
2020-06-26 23:30:30 +02:00
#define numberUnion(method) \
for (size_t j = 0; j < COUNT; ++j) { \
unsigned int val = d->tags[j] ? d->tags[j]->method() : 0; \
if (val > 0) \
return val; \
} \
return 0;
#define pictureMapUnion(method) \
for (size_t j = 0; j < COUNT; ++j) { \
PictureMap val = d->tags[j] ? d->tags[j]->method() : PictureMap(); \
if (!val.isEmpty()) \
return val; \
} \
return PictureMap();
#define setUnion(method, value) \
for (size_t j = 0; j < COUNT; ++j) { \
if (d->tags[j]) \
d->tags[j]->set##method(value); \
2018-05-10 15:29:28 +02:00
}
2020-06-26 23:30:30 +02:00
namespace Strawberry_TagLib {
namespace TagLib {
template<size_t COUNT>
class TagUnion<COUNT>::TagUnionPrivate {
public:
std::unique_ptr<Tag> tags[COUNT];
2018-05-10 15:29:28 +02:00
};
2020-06-26 23:30:30 +02:00
template<size_t COUNT>
TagUnion<COUNT>::TagUnion() : d(new TagUnionPrivate()) {}
2018-05-10 15:29:28 +02:00
2020-06-26 23:30:30 +02:00
template<size_t COUNT>
TagUnion<COUNT>::~TagUnion() {
2018-05-10 15:29:28 +02:00
delete d;
}
2020-06-26 23:30:30 +02:00
template<size_t COUNT>
Tag *TagUnion<COUNT>::operator[](size_t index) const {
2018-05-10 15:29:28 +02:00
return tag(index);
}
2020-06-26 23:30:30 +02:00
template<size_t COUNT>
Tag *TagUnion<COUNT>::tag(size_t index) const {
return d->tags[index].get();
2018-05-10 15:29:28 +02:00
}
2020-06-26 23:30:30 +02:00
template<size_t COUNT>
void TagUnion<COUNT>::set(size_t index, Tag *tag) {
d->tags[index].reset(tag);
2018-05-10 15:29:28 +02:00
}
2020-06-26 23:30:30 +02:00
template<size_t COUNT>
PropertyMap TagUnion<COUNT>::properties() const {
2018-05-10 15:29:28 +02:00
2020-06-26 23:30:30 +02:00
for (size_t i = 0; i < COUNT; ++i) {
if (d->tags[i] && !d->tags[i]->isEmpty())
return d->tags[i]->properties();
2018-05-10 15:29:28 +02:00
}
return PropertyMap();
2018-05-10 15:29:28 +02:00
}
2020-06-26 23:30:30 +02:00
template<size_t COUNT>
void TagUnion<COUNT>::removeUnsupportedProperties(const StringList &unsupported) {
2020-06-26 23:30:30 +02:00
for (size_t i = 0; i < COUNT; ++i) {
if (d->tags[i])
d->tags[i]->removeUnsupportedProperties(unsupported);
}
2018-05-10 15:29:28 +02:00
2020-06-26 23:30:30 +02:00
}
2018-05-10 15:29:28 +02:00
2020-06-26 23:30:30 +02:00
template<size_t COUNT>
PropertyMap TagUnion<COUNT>::setProperties(const PropertyMap &properties) {
2018-05-10 15:29:28 +02:00
2020-06-26 23:30:30 +02:00
// Record unassigned properties for each tag in the union
std::vector<PropertyMap> returnCandidates;
for (size_t i = 0; i < COUNT; ++i) {
if (d->tags[i])
returnCandidates.insert(returnCandidates.end(), d->tags[i]->setProperties(properties));
}
2018-05-10 15:29:28 +02:00
2020-06-26 23:30:30 +02:00
if (!returnCandidates.empty()) {
// Only one tag present, return its unassigned properties
if (returnCandidates.size() == 1) {
return returnCandidates.front();
}
2018-05-10 15:29:28 +02:00
2020-06-26 23:30:30 +02:00
// Multiple tags in union:
// if a property has been assigned in any member tag
// remove it from ignored properties to return
PropertyMap propertiesCopy(properties);
for (std::vector<PropertyMap>::iterator i = returnCandidates.begin(); i != returnCandidates.end(); i++) {
for (PropertyMap::Iterator j = propertiesCopy.begin(); j != propertiesCopy.end();) {
if (!i->contains(j->first)) {
j = propertiesCopy.erase(j->first).begin();
}
else {
j++;
}
}
2018-05-10 15:29:28 +02:00
}
}
2020-06-26 23:30:30 +02:00
// No assignments made by union member tags.
// Return input (this should not happen)
return properties;
2018-05-10 15:29:28 +02:00
}
2020-06-26 23:30:30 +02:00
template<size_t COUNT>
String TagUnion<COUNT>::title() const {
2018-05-10 15:29:28 +02:00
stringUnion(title);
}
2020-06-26 23:30:30 +02:00
template<size_t COUNT>
String TagUnion<COUNT>::artist() const {
2018-05-10 15:29:28 +02:00
stringUnion(artist);
}
2020-06-26 23:30:30 +02:00
template<size_t COUNT>
String TagUnion<COUNT>::album() const {
2018-05-10 15:29:28 +02:00
stringUnion(album);
}
2020-06-26 23:30:30 +02:00
template<size_t COUNT>
String TagUnion<COUNT>::comment() const {
2018-05-10 15:29:28 +02:00
stringUnion(comment);
}
2020-06-26 23:30:30 +02:00
template<size_t COUNT>
String TagUnion<COUNT>::genre() const {
2018-05-10 15:29:28 +02:00
stringUnion(genre);
}
2020-06-26 23:30:30 +02:00
template<size_t COUNT>
unsigned int TagUnion<COUNT>::year() const {
2018-05-10 15:29:28 +02:00
numberUnion(year);
}
2020-06-26 23:30:30 +02:00
template<size_t COUNT>
unsigned int TagUnion<COUNT>::track() const {
2018-05-10 15:29:28 +02:00
numberUnion(track);
}
2020-06-26 23:30:30 +02:00
template<size_t COUNT>
void TagUnion<COUNT>::setTitle(const String &s) {
2018-05-10 15:29:28 +02:00
setUnion(Title, s);
}
2020-06-26 23:30:30 +02:00
template<size_t COUNT>
void TagUnion<COUNT>::setArtist(const String &s) {
2018-05-10 15:29:28 +02:00
setUnion(Artist, s);
}
2020-06-26 23:30:30 +02:00
template<size_t COUNT>
Strawberry_TagLib::TagLib::PictureMap TagUnion<COUNT>::pictures() const {
pictureMapUnion(pictures);
}
template<size_t COUNT>
void TagUnion<COUNT>::setAlbum(const String &s) {
2018-05-10 15:29:28 +02:00
setUnion(Album, s);
}
2020-06-26 23:30:30 +02:00
template<size_t COUNT>
void TagUnion<COUNT>::setComment(const String &s) {
2018-05-10 15:29:28 +02:00
setUnion(Comment, s);
}
2020-06-26 23:30:30 +02:00
template<size_t COUNT>
void TagUnion<COUNT>::setGenre(const String &s) {
2018-05-10 15:29:28 +02:00
setUnion(Genre, s);
}
2020-06-26 23:30:30 +02:00
template<size_t COUNT>
void TagUnion<COUNT>::setYear(unsigned int i) {
2018-05-10 15:29:28 +02:00
setUnion(Year, i);
}
2020-06-26 23:30:30 +02:00
template<size_t COUNT>
void TagUnion<COUNT>::setTrack(unsigned int i) {
2018-05-10 15:29:28 +02:00
setUnion(Track, i);
}
2020-06-26 23:30:30 +02:00
template<size_t COUNT>
void TagUnion<COUNT>::setPictures(const PictureMap &l) {
setUnion(Pictures, l);
}
template<size_t COUNT>
bool TagUnion<COUNT>::isEmpty() const {
2020-06-26 23:30:30 +02:00
for (size_t i = 0; i < COUNT; ++i) {
if (d->tags[i] && !d->tags[i]->isEmpty())
return false;
}
2018-05-10 15:29:28 +02:00
return true;
2018-05-10 15:29:28 +02:00
}
2020-06-26 23:30:30 +02:00
// All the versions of TagUnion should be explicitly instantiated here.
template class TagUnion<2>;
template class TagUnion<3>;
} // namespace TagLib
} // namepsace Strawberry_TagLib