/*************************************************************************** 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/ * ***************************************************************************/ #include #include "tagunion.h" #include "tstringlist.h" #include "tpropertymap.h" #include "tpicturemap.h" #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; \ } \ return String(); #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); \ } namespace Strawberry_TagLib { namespace TagLib { template class TagUnion::TagUnionPrivate { public: std::unique_ptr tags[COUNT]; }; template TagUnion::TagUnion() : d(new TagUnionPrivate()) {} template TagUnion::~TagUnion() { delete d; } template Tag *TagUnion::operator[](size_t index) const { return tag(index); } template Tag *TagUnion::tag(size_t index) const { return d->tags[index].get(); } template void TagUnion::set(size_t index, Tag *tag) { d->tags[index].reset(tag); } template PropertyMap TagUnion::properties() const { for (size_t i = 0; i < COUNT; ++i) { if (d->tags[i] && !d->tags[i]->isEmpty()) return d->tags[i]->properties(); } return PropertyMap(); } template void TagUnion::removeUnsupportedProperties(const StringList &unsupported) { for (size_t i = 0; i < COUNT; ++i) { if (d->tags[i]) d->tags[i]->removeUnsupportedProperties(unsupported); } } template PropertyMap TagUnion::setProperties(const PropertyMap &properties) { // Record unassigned properties for each tag in the union std::vector returnCandidates; for (size_t i = 0; i < COUNT; ++i) { if (d->tags[i]) returnCandidates.insert(returnCandidates.end(), d->tags[i]->setProperties(properties)); } if (!returnCandidates.empty()) { // Only one tag present, return its unassigned properties if (returnCandidates.size() == 1) { return returnCandidates.front(); } // 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::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++; } } } } // No assignments made by union member tags. // Return input (this should not happen) return properties; } template String TagUnion::title() const { stringUnion(title); } template String TagUnion::artist() const { stringUnion(artist); } template String TagUnion::album() const { stringUnion(album); } template String TagUnion::comment() const { stringUnion(comment); } template String TagUnion::genre() const { stringUnion(genre); } template unsigned int TagUnion::year() const { numberUnion(year); } template unsigned int TagUnion::track() const { numberUnion(track); } template void TagUnion::setTitle(const String &s) { setUnion(Title, s); } template void TagUnion::setArtist(const String &s) { setUnion(Artist, s); } template Strawberry_TagLib::TagLib::PictureMap TagUnion::pictures() const { pictureMapUnion(pictures); } template void TagUnion::setAlbum(const String &s) { setUnion(Album, s); } template void TagUnion::setComment(const String &s) { setUnion(Comment, s); } template void TagUnion::setGenre(const String &s) { setUnion(Genre, s); } template void TagUnion::setYear(unsigned int i) { setUnion(Year, i); } template void TagUnion::setTrack(unsigned int i) { setUnion(Track, i); } template void TagUnion::setPictures(const PictureMap &l) { setUnion(Pictures, l); } template bool TagUnion::isEmpty() const { for (size_t i = 0; i < COUNT; ++i) { if (d->tags[i] && !d->tags[i]->isEmpty()) return false; } return true; } // All the versions of TagUnion should be explicitly instantiated here. template class TagUnion<2>; template class TagUnion<3>; } // namespace TagLib } // namepsace Strawberry_TagLib