parent
ddeec3aea0
commit
4cb0e2232a
22 changed files with 609 additions and 247 deletions
@ -0,0 +1,197 @@ |
||||
/***************************************************************************
|
||||
* Copyright (C) 2008-2012 by Andrzej Rybczak * |
||||
* electricityispower@gmail.com * |
||||
* * |
||||
* This program is free software; you can redistribute it and/or modify * |
||||
* it under the terms of the GNU General Public License as published by * |
||||
* the Free Software Foundation; either version 2 of the License, or * |
||||
* (at your option) any later version. * |
||||
* * |
||||
* This program 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 General Public License for more details. * |
||||
* * |
||||
* You should have received a copy of the GNU General Public License * |
||||
* along with this program; if not, write to the * |
||||
* Free Software Foundation, Inc., * |
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * |
||||
***************************************************************************/ |
||||
|
||||
#include "mutable_song.h" |
||||
#include "string_utilities.h" |
||||
|
||||
namespace MPD {//
|
||||
|
||||
std::string MutableSong::getArtist(unsigned idx) const |
||||
{ |
||||
return getTag(MPD_TAG_ARTIST, [this, idx](){ return Song::getArtist(idx); }, idx); |
||||
} |
||||
|
||||
std::string MutableSong::getTitle(unsigned idx) const |
||||
{ |
||||
return getTag(MPD_TAG_TITLE, [this, idx](){ return Song::getTitle(idx); }, idx); |
||||
} |
||||
|
||||
std::string MutableSong::getAlbum(unsigned idx) const |
||||
{ |
||||
return getTag(MPD_TAG_ALBUM, [this, idx](){ return Song::getAlbum(idx); }, idx); |
||||
} |
||||
|
||||
std::string MutableSong::getAlbumArtist(unsigned idx) const |
||||
{ |
||||
return getTag(MPD_TAG_ALBUM_ARTIST, [this, idx](){ return Song::getAlbumArtist(idx); }, idx); |
||||
} |
||||
|
||||
std::string MutableSong::getTrack(unsigned idx) const |
||||
{ |
||||
std::string track = getTag(MPD_TAG_TRACK, [this, idx](){ return Song::getTrack(idx); }, idx); |
||||
if ((track.length() == 1 && track[0] != '0') |
||||
|| (track.length() > 3 && track[1] == '/')) |
||||
return "0"+track; |
||||
else |
||||
return track; |
||||
} |
||||
|
||||
std::string MutableSong::getDate(unsigned idx) const |
||||
{ |
||||
return getTag(MPD_TAG_DATE, [this, idx](){ return Song::getDate(idx); }, idx); |
||||
} |
||||
|
||||
std::string MutableSong::getGenre(unsigned idx) const |
||||
{ |
||||
return getTag(MPD_TAG_GENRE, [this, idx](){ return Song::getGenre(idx); }, idx); |
||||
} |
||||
|
||||
std::string MutableSong::getComposer(unsigned idx) const |
||||
{ |
||||
return getTag(MPD_TAG_COMPOSER, [this, idx](){ return Song::getComposer(idx); }, idx); |
||||
} |
||||
|
||||
std::string MutableSong::getPerformer(unsigned idx) const |
||||
{ |
||||
return getTag(MPD_TAG_PERFORMER, [this, idx](){ return Song::getPerformer(idx); }, idx); |
||||
} |
||||
|
||||
std::string MutableSong::getDisc(unsigned idx) const |
||||
{ |
||||
return getTag(MPD_TAG_DISC, [this, idx](){ return Song::getDisc(idx); }, idx); |
||||
} |
||||
|
||||
std::string MutableSong::getComment(unsigned idx) const |
||||
{ |
||||
return getTag(MPD_TAG_COMMENT, [this, idx](){ return Song::getComment(idx); }, idx); |
||||
} |
||||
|
||||
void MutableSong::setArtist(const std::string &value, unsigned idx) |
||||
{ |
||||
replaceTag(MPD_TAG_ARTIST, Song::getArtist(idx), value, idx); |
||||
} |
||||
|
||||
void MutableSong::setTitle(const std::string &value, unsigned idx) |
||||
{ |
||||
replaceTag(MPD_TAG_TITLE, Song::getTitle(idx), value, idx); |
||||
} |
||||
|
||||
void MutableSong::setAlbum(const std::string &value, unsigned idx) |
||||
{ |
||||
replaceTag(MPD_TAG_ALBUM, Song::getAlbum(idx), value, idx); |
||||
} |
||||
|
||||
void MutableSong::setAlbumArtist(const std::string &value, unsigned idx) |
||||
{ |
||||
replaceTag(MPD_TAG_ALBUM_ARTIST, Song::getAlbumArtist(idx), value, idx); |
||||
} |
||||
|
||||
void MutableSong::setTrack(const std::string &value, unsigned idx) |
||||
{ |
||||
replaceTag(MPD_TAG_TRACK, Song::getTrack(idx), value, idx); |
||||
} |
||||
|
||||
void MutableSong::setDate(const std::string &value, unsigned idx) |
||||
{ |
||||
replaceTag(MPD_TAG_DATE, Song::getDate(idx), value, idx); |
||||
} |
||||
|
||||
void MutableSong::setGenre(const std::string &value, unsigned idx) |
||||
{ |
||||
replaceTag(MPD_TAG_GENRE, Song::getGenre(idx), value, idx); |
||||
} |
||||
|
||||
void MutableSong::setComposer(const std::string &value, unsigned idx) |
||||
{ |
||||
replaceTag(MPD_TAG_COMPOSER, Song::getComposer(idx), value, idx); |
||||
} |
||||
|
||||
void MutableSong::setPerformer(const std::string &value, unsigned idx) |
||||
{ |
||||
replaceTag(MPD_TAG_PERFORMER, Song::getPerformer(idx), value, idx); |
||||
} |
||||
|
||||
void MutableSong::setDisc(const std::string &value, unsigned idx) |
||||
{ |
||||
replaceTag(MPD_TAG_DISC, Song::getDisc(idx), value, idx); |
||||
} |
||||
|
||||
void MutableSong::setComment(const std::string &value, unsigned idx) |
||||
{ |
||||
replaceTag(MPD_TAG_COMMENT, Song::getComment(idx), value, idx); |
||||
} |
||||
|
||||
const std::string &MutableSong::getNewURI() const |
||||
{ |
||||
return m_uri; |
||||
} |
||||
|
||||
void MutableSong::setNewURI(const std::string &value) |
||||
{ |
||||
std::string orig_uri = getURI(); |
||||
if (orig_uri == value) |
||||
m_uri.clear(); |
||||
else |
||||
m_uri = value; |
||||
} |
||||
|
||||
void MutableSong::setTag(SetFunction set, const std::string &value, const std::string &delimiter) |
||||
{ |
||||
auto tags = split(value, delimiter); |
||||
for (size_t i = 0; i < tags.size(); ++i) |
||||
(this->*set)(tags[i], i); |
||||
} |
||||
|
||||
bool MutableSong::isModified() const |
||||
{ |
||||
return !m_uri.empty() && !m_tags.empty(); |
||||
} |
||||
|
||||
void MutableSong::clearModifications() |
||||
{ |
||||
m_uri.clear(); |
||||
m_tags.clear(); |
||||
} |
||||
|
||||
std::string MutableSong::getTag(mpd_tag_type tag_type, std::function<std::string()> orig_value, unsigned idx) const |
||||
{ |
||||
auto it = m_tags.find(Tag(tag_type, idx)); |
||||
std::string result; |
||||
if (it == m_tags.end()) |
||||
result = orig_value(); |
||||
else |
||||
result = it->second; |
||||
return result; |
||||
} |
||||
|
||||
void MutableSong::replaceTag(mpd_tag_type tag_type, std::string &&orig_value, const std::string &value, unsigned idx) |
||||
{ |
||||
Tag tag(tag_type, idx); |
||||
if (value == orig_value) |
||||
{ |
||||
auto it = m_tags.find(tag); |
||||
if (it != m_tags.end()) |
||||
m_tags.erase(it); |
||||
} |
||||
else |
||||
m_tags[tag] = value; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,97 @@ |
||||
/***************************************************************************
|
||||
* Copyright (C) 2008-2012 by Andrzej Rybczak * |
||||
* electricityispower@gmail.com * |
||||
* * |
||||
* This program is free software; you can redistribute it and/or modify * |
||||
* it under the terms of the GNU General Public License as published by * |
||||
* the Free Software Foundation; either version 2 of the License, or * |
||||
* (at your option) any later version. * |
||||
* * |
||||
* This program 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 General Public License for more details. * |
||||
* * |
||||
* You should have received a copy of the GNU General Public License * |
||||
* along with this program; if not, write to the * |
||||
* Free Software Foundation, Inc., * |
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * |
||||
***************************************************************************/ |
||||
|
||||
#ifndef _EDITABLE_SONG_H |
||||
#define _EDITABLE_SONG_H |
||||
|
||||
#include <map> |
||||
#include "song.h" |
||||
|
||||
namespace MPD {//
|
||||
|
||||
struct MutableSong : public Song |
||||
{ |
||||
typedef void (MutableSong::*SetFunction)(const std::string &, unsigned); |
||||
|
||||
MutableSong() { } |
||||
MutableSong(Song s) : Song(s) { } |
||||
|
||||
virtual std::string getArtist(unsigned idx = 0) const; |
||||
virtual std::string getTitle(unsigned idx = 0) const; |
||||
virtual std::string getAlbum(unsigned idx = 0) const; |
||||
virtual std::string getAlbumArtist(unsigned idx = 0) const; |
||||
virtual std::string getTrack(unsigned idx = 0) const; |
||||
virtual std::string getDate(unsigned idx = 0) const; |
||||
virtual std::string getGenre(unsigned idx = 0) const; |
||||
virtual std::string getComposer(unsigned idx = 0) const; |
||||
virtual std::string getPerformer(unsigned idx = 0) const; |
||||
virtual std::string getDisc(unsigned idx = 0) const; |
||||
virtual std::string getComment(unsigned idx = 0) const; |
||||
|
||||
void setArtist(const std::string &value, unsigned idx = 0); |
||||
void setTitle(const std::string &value, unsigned idx = 0); |
||||
void setAlbum(const std::string &value, unsigned idx = 0); |
||||
void setAlbumArtist(const std::string &value, unsigned idx = 0); |
||||
void setTrack(const std::string &value, unsigned idx = 0); |
||||
void setDate(const std::string &value, unsigned idx = 0); |
||||
void setGenre(const std::string &value, unsigned idx = 0); |
||||
void setComposer(const std::string &value, unsigned idx = 0); |
||||
void setPerformer(const std::string &value, unsigned idx = 0); |
||||
void setDisc(const std::string &value, unsigned idx = 0); |
||||
void setComment(const std::string &value, unsigned idx = 0); |
||||
|
||||
const std::string &getNewURI() const; |
||||
void setNewURI(const std::string &value); |
||||
|
||||
void setTag(SetFunction set, const std::string &value, const std::string &delimiter = ""); |
||||
|
||||
bool isModified() const; |
||||
void clearModifications(); |
||||
|
||||
private: |
||||
struct Tag |
||||
{ |
||||
Tag(mpd_tag_type type_, unsigned idx_) : m_type(type_), m_idx(idx_) { } |
||||
|
||||
mpd_tag_type type() const { return m_type; } |
||||
unsigned idx() const { return m_idx; } |
||||
|
||||
bool operator<(const Tag &t) const |
||||
{ |
||||
if (m_type != t.m_type) |
||||
return m_type < t.m_type; |
||||
return m_idx < t.m_idx; |
||||
} |
||||
|
||||
private: |
||||
mpd_tag_type m_type; |
||||
unsigned m_idx; |
||||
}; |
||||
|
||||
std::string getTag(mpd_tag_type tag_type, std::function<std::string()> orig_value, unsigned idx) const; |
||||
void replaceTag(mpd_tag_type tag_type, std::string &&orig_value, const std::string &value, unsigned idx); |
||||
|
||||
std::string m_uri; |
||||
std::map<Tag, std::string> m_tags; |
||||
}; |
||||
|
||||
} |
||||
|
||||
#endif // _EDITABLE_SONG_H
|
||||
@ -0,0 +1,42 @@ |
||||
/***************************************************************************
|
||||
* Copyright (C) 2008-2012 by Andrzej Rybczak * |
||||
* electricityispower@gmail.com * |
||||
* * |
||||
* This program is free software; you can redistribute it and/or modify * |
||||
* it under the terms of the GNU General Public License as published by * |
||||
* the Free Software Foundation; either version 2 of the License, or * |
||||
* (at your option) any later version. * |
||||
* * |
||||
* This program 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 General Public License for more details. * |
||||
* * |
||||
* You should have received a copy of the GNU General Public License * |
||||
* along with this program; if not, write to the * |
||||
* Free Software Foundation, Inc., * |
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * |
||||
***************************************************************************/ |
||||
|
||||
#include <cassert> |
||||
#include "string_utilities.h" |
||||
|
||||
std::vector<std::string> split(const std::string &s, const std::string &delimiter) |
||||
{ |
||||
if (delimiter.empty()) |
||||
return { s }; |
||||
std::vector<std::string> result; |
||||
size_t i = 0, j = 0; |
||||
while (true) |
||||
{ |
||||
i = j; |
||||
j = s.find(delimiter, i); |
||||
if (j == std::string::npos) |
||||
break; |
||||
else |
||||
result.push_back(s.substr(i, j-i)); |
||||
j += delimiter.length(); |
||||
} |
||||
result.push_back(s.substr(i)); |
||||
return result; |
||||
} |
||||
@ -0,0 +1,29 @@ |
||||
/***************************************************************************
|
||||
* Copyright (C) 2008-2012 by Andrzej Rybczak * |
||||
* electricityispower@gmail.com * |
||||
* * |
||||
* This program is free software; you can redistribute it and/or modify * |
||||
* it under the terms of the GNU General Public License as published by * |
||||
* the Free Software Foundation; either version 2 of the License, or * |
||||
* (at your option) any later version. * |
||||
* * |
||||
* This program 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 General Public License for more details. * |
||||
* * |
||||
* You should have received a copy of the GNU General Public License * |
||||
* along with this program; if not, write to the * |
||||
* Free Software Foundation, Inc., * |
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * |
||||
***************************************************************************/ |
||||
|
||||
#ifndef _STRING_UTILITIES |
||||
#define _STRING_UTILITIES |
||||
|
||||
#include <string> |
||||
#include <vector> |
||||
|
||||
std::vector<std::string> split(const std::string &s, const std::string &delimiter); |
||||
|
||||
#endif // _STRING_UTILITIES
|
||||
Loading…
Reference in new issue