Add '--quiet' comand line option to suppress some output
[ncmpcpp.git] / src / mutable_song.cpp
blob9a4c8100588acd7a4cb7ef8eb6df22a0192df64a
1 /***************************************************************************
2 * Copyright (C) 2008-2017 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 "statusbar.h"
22 #include <boost/algorithm/string/split.hpp>
23 #include "mutable_song.h"
25 namespace MPD {
27 std::string MutableSong::getArtist(unsigned idx) const
29 return getTag(MPD_TAG_ARTIST, [this, idx](){ return Song::getArtist(idx); }, idx);
32 std::string MutableSong::getTitle(unsigned idx) const
34 return getTag(MPD_TAG_TITLE, [this, idx](){ return Song::getTitle(idx); }, idx);
37 std::string MutableSong::getAlbum(unsigned idx) const
39 return getTag(MPD_TAG_ALBUM, [this, idx](){ return Song::getAlbum(idx); }, idx);
42 std::string MutableSong::getAlbumArtist(unsigned idx) const
44 return getTag(MPD_TAG_ALBUM_ARTIST, [this, idx](){ return Song::getAlbumArtist(idx); }, idx);
47 std::string MutableSong::getTrack(unsigned idx) const
49 std::string track = getTag(MPD_TAG_TRACK, [this, idx](){ return Song::getTrack(idx); }, idx);
50 if ((track.length() == 1 && track[0] != '0')
51 || (track.length() > 3 && track[1] == '/'))
52 return "0"+track;
53 else
54 return track;
57 std::string MutableSong::getDate(unsigned idx) const
59 return getTag(MPD_TAG_DATE, [this, idx](){ return Song::getDate(idx); }, idx);
62 std::string MutableSong::getGenre(unsigned idx) const
64 return getTag(MPD_TAG_GENRE, [this, idx](){ return Song::getGenre(idx); }, idx);
67 std::string MutableSong::getComposer(unsigned idx) const
69 return getTag(MPD_TAG_COMPOSER, [this, idx](){ return Song::getComposer(idx); }, idx);
72 std::string MutableSong::getPerformer(unsigned idx) const
74 return getTag(MPD_TAG_PERFORMER, [this, idx](){ return Song::getPerformer(idx); }, idx);
77 std::string MutableSong::getDisc(unsigned idx) const
79 return getTag(MPD_TAG_DISC, [this, idx](){ return Song::getDisc(idx); }, idx);
82 std::string MutableSong::getComment(unsigned idx) const
84 return getTag(MPD_TAG_COMMENT, [this, idx](){ return Song::getComment(idx); }, idx);
87 void MutableSong::setArtist(const std::string &value, unsigned idx)
89 replaceTag(MPD_TAG_ARTIST, Song::getArtist(idx), value, idx);
92 void MutableSong::setTitle(const std::string &value, unsigned idx)
94 replaceTag(MPD_TAG_TITLE, Song::getTitle(idx), value, idx);
97 void MutableSong::setAlbum(const std::string &value, unsigned idx)
99 replaceTag(MPD_TAG_ALBUM, Song::getAlbum(idx), value, idx);
102 void MutableSong::setAlbumArtist(const std::string &value, unsigned idx)
104 replaceTag(MPD_TAG_ALBUM_ARTIST, Song::getAlbumArtist(idx), value, idx);
107 void MutableSong::setTrack(const std::string &value, unsigned idx)
109 replaceTag(MPD_TAG_TRACK, Song::getTrack(idx), value, idx);
112 void MutableSong::setDate(const std::string &value, unsigned idx)
114 replaceTag(MPD_TAG_DATE, Song::getDate(idx), value, idx);
117 void MutableSong::setGenre(const std::string &value, unsigned idx)
119 replaceTag(MPD_TAG_GENRE, Song::getGenre(idx), value, idx);
122 void MutableSong::setComposer(const std::string &value, unsigned idx)
124 replaceTag(MPD_TAG_COMPOSER, Song::getComposer(idx), value, idx);
127 void MutableSong::setPerformer(const std::string &value, unsigned idx)
129 replaceTag(MPD_TAG_PERFORMER, Song::getPerformer(idx), value, idx);
132 void MutableSong::setDisc(const std::string &value, unsigned idx)
134 replaceTag(MPD_TAG_DISC, Song::getDisc(idx), value, idx);
137 void MutableSong::setComment(const std::string &value, unsigned idx)
139 replaceTag(MPD_TAG_COMMENT, Song::getComment(idx), value, idx);
142 const std::string &MutableSong::getNewName() const
144 return m_name;
147 void MutableSong::setNewName(const std::string &value)
149 if (getName() == value)
150 m_name.clear();
151 else
152 m_name = value;
155 unsigned MutableSong::getDuration() const
157 if (m_duration > 0)
158 return m_duration;
159 else
160 return Song::getDuration();
163 time_t MutableSong::getMTime() const
165 if (m_mtime > 0)
166 return m_mtime;
167 else
168 return Song::getMTime();
171 void MutableSong::setDuration(unsigned int duration)
173 m_duration = duration;
176 void MutableSong::setMTime(time_t mtime)
178 m_mtime = mtime;
181 void MutableSong::setTags(SetFunction set, const std::string &value)
183 std::vector<std::string> tags;
184 boost::iter_split(tags, value, boost::first_finder(Song::TagsSeparator));
185 size_t i = 0;
186 for (; i < tags.size(); ++i)
187 (this->*set)(tags[i], i);
188 // set next tag to be empty, so tags with bigger indexes won't be read
189 (this->*set)("", i);
192 bool MutableSong::isModified() const
194 return !m_name.empty() || !m_tags.empty();
197 void MutableSong::clearModifications()
199 m_name.clear();
200 m_tags.clear();
203 void MutableSong::replaceTag(mpd_tag_type tag_type, std::string orig_value, const std::string &value, unsigned idx)
205 Tag tag(tag_type, idx);
206 if (value == orig_value)
208 auto it = m_tags.find(tag);
209 if (it != m_tags.end())
210 m_tags.erase(it);
212 else
213 m_tags[tag] = value;