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

196 lines
6.0 KiB
C++
Raw Normal View History

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/ *
***************************************************************************/
#include "tag.h"
#include "tstringlist.h"
2020-06-26 23:30:30 +02:00
#include "tpicturemap.h"
2018-05-10 15:29:28 +02:00
#include "tpropertymap.h"
using namespace Strawberry_TagLib::TagLib;
2018-05-10 15:29:28 +02:00
class Tag::TagPrivate {};
2018-05-10 15:29:28 +02:00
Tag::Tag() : d(nullptr) {}
2018-05-10 15:29:28 +02:00
Tag::~Tag() {}
2018-05-10 15:29:28 +02:00
2020-06-13 19:02:42 +02:00
bool Tag::isEmpty() const {
2018-05-10 15:29:28 +02:00
return (title().isEmpty() &&
2020-06-13 19:02:42 +02:00
artist().isEmpty() &&
album().isEmpty() &&
comment().isEmpty() &&
genre().isEmpty() &&
year() == 0 &&
2020-06-26 23:30:30 +02:00
track() == 0 &&
pictures().isEmpty());
2018-05-10 15:29:28 +02:00
}
2020-06-13 19:02:42 +02:00
PropertyMap Tag::properties() const {
2018-05-10 15:29:28 +02:00
PropertyMap map;
2019-04-08 18:46:11 +02:00
if (!(title().isEmpty()))
2018-05-10 15:29:28 +02:00
map["TITLE"].append(title());
2019-04-08 18:46:11 +02:00
if (!(artist().isEmpty()))
2018-05-10 15:29:28 +02:00
map["ARTIST"].append(artist());
2019-04-08 18:46:11 +02:00
if (!(album().isEmpty()))
2018-05-10 15:29:28 +02:00
map["ALBUM"].append(album());
2019-04-08 18:46:11 +02:00
if (!(comment().isEmpty()))
2018-05-10 15:29:28 +02:00
map["COMMENT"].append(comment());
2019-04-08 18:46:11 +02:00
if (!(genre().isEmpty()))
2018-05-10 15:29:28 +02:00
map["GENRE"].append(genre());
2019-04-08 18:46:11 +02:00
if (!(year() == 0))
2018-05-10 15:29:28 +02:00
map["DATE"].append(String::number(year()));
2019-04-08 18:46:11 +02:00
if (!(track() == 0))
2018-05-10 15:29:28 +02:00
map["TRACKNUMBER"].append(String::number(track()));
return map;
}
void Tag::removeUnsupportedProperties(const StringList &) {}
2020-06-13 19:02:42 +02:00
PropertyMap Tag::setProperties(const PropertyMap &origProps) {
2018-05-10 15:29:28 +02:00
PropertyMap properties(origProps);
properties.removeEmpty();
StringList oneValueSet;
// can this be simplified by using some preprocessor defines / function pointers?
2019-04-08 18:46:11 +02:00
if (properties.contains("TITLE")) {
2018-05-10 15:29:28 +02:00
setTitle(properties["TITLE"].front());
oneValueSet.append("TITLE");
2020-06-13 19:02:42 +02:00
}
else
2018-05-10 15:29:28 +02:00
setTitle(String());
2019-04-08 18:46:11 +02:00
if (properties.contains("ARTIST")) {
2018-05-10 15:29:28 +02:00
setArtist(properties["ARTIST"].front());
oneValueSet.append("ARTIST");
2020-06-13 19:02:42 +02:00
}
else
2018-05-10 15:29:28 +02:00
setArtist(String());
2019-04-08 18:46:11 +02:00
if (properties.contains("ALBUM")) {
2018-05-10 15:29:28 +02:00
setAlbum(properties["ALBUM"].front());
oneValueSet.append("ALBUM");
2020-06-13 19:02:42 +02:00
}
else
2018-05-10 15:29:28 +02:00
setAlbum(String());
2019-04-08 18:46:11 +02:00
if (properties.contains("COMMENT")) {
2018-05-10 15:29:28 +02:00
setComment(properties["COMMENT"].front());
oneValueSet.append("COMMENT");
2020-06-13 19:02:42 +02:00
}
else
2018-05-10 15:29:28 +02:00
setComment(String());
2019-04-08 18:46:11 +02:00
if (properties.contains("GENRE")) {
2018-05-10 15:29:28 +02:00
setGenre(properties["GENRE"].front());
oneValueSet.append("GENRE");
2020-06-13 19:02:42 +02:00
}
else
2018-05-10 15:29:28 +02:00
setGenre(String());
2019-04-08 18:46:11 +02:00
if (properties.contains("DATE")) {
2018-05-10 15:29:28 +02:00
bool ok;
int date = properties["DATE"].front().toInt(&ok);
2019-04-08 18:46:11 +02:00
if (ok) {
2018-05-10 15:29:28 +02:00
setYear(date);
oneValueSet.append("DATE");
2020-06-13 19:02:42 +02:00
}
else
2018-05-10 15:29:28 +02:00
setYear(0);
}
else
setYear(0);
2019-04-08 18:46:11 +02:00
if (properties.contains("TRACKNUMBER")) {
2018-05-10 15:29:28 +02:00
bool ok;
int track = properties["TRACKNUMBER"].front().toInt(&ok);
2019-04-08 18:46:11 +02:00
if (ok) {
2018-05-10 15:29:28 +02:00
setTrack(track);
oneValueSet.append("TRACKNUMBER");
2020-06-13 19:02:42 +02:00
}
else
2018-05-10 15:29:28 +02:00
setTrack(0);
}
else
setTrack(0);
// for each tag that has been set above, remove the first entry in the corresponding
// value list. The others will be returned as unsupported by this format.
2020-06-13 19:02:42 +02:00
for (StringList::ConstIterator it = oneValueSet.begin(); it != oneValueSet.end(); ++it) {
2019-04-08 18:46:11 +02:00
if (properties[*it].size() == 1)
2018-05-10 15:29:28 +02:00
properties.erase(*it);
else
2020-06-13 19:02:42 +02:00
properties[*it].erase(properties[*it].begin());
2018-05-10 15:29:28 +02:00
}
return properties;
2018-05-10 15:29:28 +02:00
}
void Tag::duplicate(const Tag *source, Tag *target, bool overwrite) { // static
2019-04-08 18:46:11 +02:00
if (overwrite) {
2018-05-10 15:29:28 +02:00
target->setTitle(source->title());
target->setArtist(source->artist());
target->setAlbum(source->album());
target->setComment(source->comment());
target->setGenre(source->genre());
target->setYear(source->year());
target->setTrack(source->track());
2020-06-26 23:30:30 +02:00
target->setPictures(source->pictures());
2018-05-10 15:29:28 +02:00
}
else {
2019-04-08 18:46:11 +02:00
if (target->title().isEmpty())
2018-05-10 15:29:28 +02:00
target->setTitle(source->title());
2019-04-08 18:46:11 +02:00
if (target->artist().isEmpty())
2018-05-10 15:29:28 +02:00
target->setArtist(source->artist());
2019-04-08 18:46:11 +02:00
if (target->album().isEmpty())
2018-05-10 15:29:28 +02:00
target->setAlbum(source->album());
2019-04-08 18:46:11 +02:00
if (target->comment().isEmpty())
2018-05-10 15:29:28 +02:00
target->setComment(source->comment());
2019-04-08 18:46:11 +02:00
if (target->genre().isEmpty())
2018-05-10 15:29:28 +02:00
target->setGenre(source->genre());
2020-06-26 23:30:30 +02:00
if (target->year() <= 0)
2018-05-10 15:29:28 +02:00
target->setYear(source->year());
2020-06-26 23:30:30 +02:00
if (target->track() <= 0)
2018-05-10 15:29:28 +02:00
target->setTrack(source->track());
2020-06-26 23:30:30 +02:00
if (target->pictures().isEmpty())
target->setPictures(source->pictures());
2018-05-10 15:29:28 +02:00
}
2018-05-10 15:29:28 +02:00
}
2020-06-26 23:30:30 +02:00
String Tag::toString() const {
StringList desc;
desc.append("title=" + title());
desc.append("artist=" + artist());
desc.append("album=" + album());
desc.append("comment=" + comment());
desc.append("genre=" + genre());
desc.append("year=" + String::number(year()));
desc.append("track=" + String::number(track()));
return desc.toString("\n");
}