browser: read tags from local songs
[ncmpcpp.git] / src / mutable_song.cpp
blobebe72c097650f70181c73971be225ec05498a04e
1 /***************************************************************************
2 * Copyright (C) 2008-2012 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "mutable_song.h"
22 #include "utility/string.h"
24 namespace MPD {//
26 std::string MutableSong::getArtist(unsigned idx) const
28 return getTag(MPD_TAG_ARTIST, [this, idx](){ return Song::getArtist(idx); }, idx);
31 std::string MutableSong::getTitle(unsigned idx) const
33 return getTag(MPD_TAG_TITLE, [this, idx](){ return Song::getTitle(idx); }, idx);
36 std::string MutableSong::getAlbum(unsigned idx) const
38 return getTag(MPD_TAG_ALBUM, [this, idx](){ return Song::getAlbum(idx); }, idx);
41 std::string MutableSong::getAlbumArtist(unsigned idx) const
43 return getTag(MPD_TAG_ALBUM_ARTIST, [this, idx](){ return Song::getAlbumArtist(idx); }, idx);
46 std::string MutableSong::getTrack(unsigned idx) const
48 std::string track = getTag(MPD_TAG_TRACK, [this, idx](){ return Song::getTrack(idx); }, idx);
49 if ((track.length() == 1 && track[0] != '0')
50 || (track.length() > 3 && track[1] == '/'))
51 return "0"+track;
52 else
53 return track;
56 std::string MutableSong::getDate(unsigned idx) const
58 return getTag(MPD_TAG_DATE, [this, idx](){ return Song::getDate(idx); }, idx);
61 std::string MutableSong::getGenre(unsigned idx) const
63 return getTag(MPD_TAG_GENRE, [this, idx](){ return Song::getGenre(idx); }, idx);
66 std::string MutableSong::getComposer(unsigned idx) const
68 return getTag(MPD_TAG_COMPOSER, [this, idx](){ return Song::getComposer(idx); }, idx);
71 std::string MutableSong::getPerformer(unsigned idx) const
73 return getTag(MPD_TAG_PERFORMER, [this, idx](){ return Song::getPerformer(idx); }, idx);
76 std::string MutableSong::getDisc(unsigned idx) const
78 return getTag(MPD_TAG_DISC, [this, idx](){ return Song::getDisc(idx); }, idx);
81 std::string MutableSong::getComment(unsigned idx) const
83 return getTag(MPD_TAG_COMMENT, [this, idx](){ return Song::getComment(idx); }, idx);
86 void MutableSong::setArtist(const std::string &value, unsigned idx)
88 replaceTag(MPD_TAG_ARTIST, Song::getArtist(idx), value, idx);
91 void MutableSong::setTitle(const std::string &value, unsigned idx)
93 replaceTag(MPD_TAG_TITLE, Song::getTitle(idx), value, idx);
96 void MutableSong::setAlbum(const std::string &value, unsigned idx)
98 replaceTag(MPD_TAG_ALBUM, Song::getAlbum(idx), value, idx);
101 void MutableSong::setAlbumArtist(const std::string &value, unsigned idx)
103 replaceTag(MPD_TAG_ALBUM_ARTIST, Song::getAlbumArtist(idx), value, idx);
106 void MutableSong::setTrack(const std::string &value, unsigned idx)
108 replaceTag(MPD_TAG_TRACK, Song::getTrack(idx), value, idx);
111 void MutableSong::setDate(const std::string &value, unsigned idx)
113 replaceTag(MPD_TAG_DATE, Song::getDate(idx), value, idx);
116 void MutableSong::setGenre(const std::string &value, unsigned idx)
118 replaceTag(MPD_TAG_GENRE, Song::getGenre(idx), value, idx);
121 void MutableSong::setComposer(const std::string &value, unsigned idx)
123 replaceTag(MPD_TAG_COMPOSER, Song::getComposer(idx), value, idx);
126 void MutableSong::setPerformer(const std::string &value, unsigned idx)
128 replaceTag(MPD_TAG_PERFORMER, Song::getPerformer(idx), value, idx);
131 void MutableSong::setDisc(const std::string &value, unsigned idx)
133 replaceTag(MPD_TAG_DISC, Song::getDisc(idx), value, idx);
136 void MutableSong::setComment(const std::string &value, unsigned idx)
138 replaceTag(MPD_TAG_COMMENT, Song::getComment(idx), value, idx);
141 const std::string &MutableSong::getNewURI() const
143 return m_uri;
146 void MutableSong::setNewURI(const std::string &value)
148 std::string orig_uri = getURI();
149 if (orig_uri == value)
150 m_uri.clear();
151 else
152 m_uri = value;
155 unsigned MutableSong::getDuration() const
157 if (m_duration > 0)
158 return m_duration;
159 else
160 return Song::getDuration();
164 void MutableSong::setDuration(unsigned int duration)
166 m_duration = duration;
169 void MutableSong::setTag(SetFunction set, const std::string &value, const std::string &delimiter)
171 auto tags = split(value, delimiter);
172 for (size_t i = 0; i < tags.size(); ++i)
173 (this->*set)(tags[i], i);
176 bool MutableSong::isModified() const
178 return !m_uri.empty() && !m_tags.empty();
181 void MutableSong::clearModifications()
183 m_uri.clear();
184 m_tags.clear();
187 std::string MutableSong::getTag(mpd_tag_type tag_type, std::function<std::string()> orig_value, unsigned idx) const
189 auto it = m_tags.find(Tag(tag_type, idx));
190 std::string result;
191 if (it == m_tags.end())
192 result = orig_value();
193 else
194 result = it->second;
195 return result;
198 void MutableSong::replaceTag(mpd_tag_type tag_type, std::string &&orig_value, const std::string &value, unsigned idx)
200 Tag tag(tag_type, idx);
201 if (value == orig_value)
203 auto it = m_tags.find(tag);
204 if (it != m_tags.end())
205 m_tags.erase(it);
207 else
208 m_tags[tag] = value;