set SIGWINCH handler before initializing ncurses to avoid races
[ncmpcpp.git] / src / song.cpp
blobc7d07ba7ecb752212d4d1ef1fdfbc936f46b94a9
1 /***************************************************************************
2 * Copyright (C) 2008-2014 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 <cassert>
22 #include <cstring>
23 #include <boost/format.hpp>
24 #include <boost/lexical_cast.hpp>
25 #include <iostream>
26 #include <memory>
28 #include "song.h"
29 #include "utility/type_conversions.h"
30 #include "utility/wide_string.h"
31 #include "window.h"
33 namespace {
35 size_t calc_hash(const char* s, unsigned seed = 0)
37 size_t hash = seed;
38 while (*s)
39 hash = hash * 101 + *s++;
40 return hash;
45 namespace MPD {//
47 std::string Song::get(mpd_tag_type type, unsigned idx) const
49 std::string result;
50 const char *tag = mpd_song_get_tag(m_song.get(), type, idx);
51 if (tag)
52 result = tag;
53 return result;
56 Song::Song(mpd_song *s)
58 assert(s);
59 m_song = std::shared_ptr<mpd_song>(s, mpd_song_free);
60 m_hash = calc_hash(mpd_song_get_uri(s));
63 std::string Song::getURI(unsigned idx) const
65 assert(m_song);
66 if (idx > 0)
67 return "";
68 else
69 return mpd_song_get_uri(m_song.get());
72 std::string Song::getName(unsigned idx) const
74 assert(m_song);
75 mpd_song *s = m_song.get();
76 const char *res = mpd_song_get_tag(s, MPD_TAG_NAME, idx);
77 if (!res && idx > 0)
78 return "";
79 const char *uri = mpd_song_get_uri(s);
80 const char *name = strrchr(uri, '/');
81 if (name)
82 return name+1;
83 else
84 return uri;
87 std::string Song::getDirectory(unsigned idx) const
89 assert(m_song);
90 if (idx > 0 || isStream())
91 return "";
92 const char *uri = mpd_song_get_uri(m_song.get());
93 const char *name = strrchr(uri, '/');
94 if (name)
95 return std::string(uri, name-uri);
96 else
97 return "/";
100 std::string Song::getArtist(unsigned idx) const
102 assert(m_song);
103 return get(MPD_TAG_ARTIST, idx);
106 std::string Song::getTitle(unsigned idx) const
108 assert(m_song);
109 return get(MPD_TAG_TITLE, idx);
112 std::string Song::getAlbum(unsigned idx) const
114 assert(m_song);
115 return get(MPD_TAG_ALBUM, idx);
118 std::string Song::getAlbumArtist(unsigned idx) const
120 assert(m_song);
121 return get(MPD_TAG_ALBUM_ARTIST, idx);
124 std::string Song::getTrack(unsigned idx) const
126 assert(m_song);
127 std::string track = get(MPD_TAG_TRACK, idx);
128 if ((track.length() == 1 && track[0] != '0')
129 || (track.length() > 3 && track[1] == '/'))
130 track = "0"+track;
131 return track;
134 std::string Song::getTrackNumber(unsigned idx) const
136 assert(m_song);
137 std::string track = getTrack(idx);
138 size_t slash = track.find('/');
139 if (slash != std::string::npos)
140 track.resize(slash);
141 return track;
144 std::string Song::getDate(unsigned idx) const
146 assert(m_song);
147 return get(MPD_TAG_DATE, idx);
150 std::string Song::getGenre(unsigned idx) const
152 assert(m_song);
153 return get(MPD_TAG_GENRE, idx);
156 std::string Song::getComposer(unsigned idx) const
158 assert(m_song);
159 return get(MPD_TAG_COMPOSER, idx);
162 std::string Song::getPerformer(unsigned idx) const
164 assert(m_song);
165 return get(MPD_TAG_PERFORMER, idx);
168 std::string Song::getDisc(unsigned idx) const
170 assert(m_song);
171 return get(MPD_TAG_DISC, idx);
174 std::string Song::getComment(unsigned idx) const
176 assert(m_song);
177 return get(MPD_TAG_COMMENT, idx);
180 std::string Song::getLength(unsigned idx) const
182 assert(m_song);
183 if (idx > 0)
184 return "";
185 unsigned len = getDuration();
186 if (len > 0)
187 return ShowTime(len);
188 else
189 return "-:--";
192 std::string Song::getPriority(unsigned idx) const
194 assert(m_song);
195 if (idx > 0)
196 return "";
197 return boost::lexical_cast<std::string>(getPrio());
200 std::string MPD::Song::getTags(GetFunction f, const std::string &tags_separator) const
202 assert(m_song);
203 unsigned idx = 0;
204 std::string result;
205 for (std::string tag; !(tag = (this->*f)(idx)).empty(); ++idx)
207 if (!result.empty())
208 result += tags_separator;
209 result += tag;
211 return result;
214 unsigned Song::getDuration() const
216 assert(m_song);
217 return mpd_song_get_duration(m_song.get());
220 unsigned Song::getPosition() const
222 assert(m_song);
223 return mpd_song_get_pos(m_song.get());
226 unsigned Song::getID() const
228 assert(m_song);
229 return mpd_song_get_id(m_song.get());
232 unsigned Song::getPrio() const
234 assert(m_song);
235 return mpd_song_get_prio(m_song.get());
238 time_t Song::getMTime() const
240 assert(m_song);
241 return mpd_song_get_last_modified(m_song.get());
244 bool Song::isFromDatabase() const
246 assert(m_song);
247 const char *uri = mpd_song_get_uri(m_song.get());
248 return uri[0] != '/' || !strrchr(uri, '/');
251 bool Song::isStream() const
253 assert(m_song);
254 return !strncmp(mpd_song_get_uri(m_song.get()), "http://", 7);
257 bool Song::empty() const
259 return m_song.get() == 0;
262 std::string Song::toString(const std::string &fmt, const std::string &tags_separator, const std::string &escape_chars) const
264 assert(m_song);
265 std::string::const_iterator it = fmt.begin();
266 return ParseFormat(it, tags_separator, escape_chars);
269 std::string Song::ShowTime(unsigned length)
271 int hours = length/3600;
272 length -= hours*3600;
273 int minutes = length/60;
274 length -= minutes*60;
275 int seconds = length;
277 std::string result;
278 if (hours > 0)
279 result = (boost::format("%d:%02d:%02d") % hours % minutes % seconds).str();
280 else
281 result = (boost::format("%d:%02d") % minutes % seconds).str();
282 return result;
285 void MPD::Song::validateFormat(const std::string &fmt)
287 int braces = 0;
288 for (std::string::const_iterator it = fmt.begin(); it != fmt.end(); ++it)
290 if (*it == '{')
291 ++braces;
292 else if (*it == '}')
293 --braces;
295 if (braces)
296 throw std::runtime_error("number of opening and closing braces is not equal");
298 for (size_t i = fmt.find('%'); i != std::string::npos; i = fmt.find('%', i))
300 if (isdigit(fmt[++i]))
301 while (isdigit(fmt[++i])) { }
302 if (!charToGetFunction(fmt[i]))
303 throw std::runtime_error(
304 (boost::format("invalid character at position %1%: %2%") % (i+1) % fmt[i]).str()
309 std::string Song::ParseFormat(std::string::const_iterator &it, const std::string &tags_separator,
310 const std::string &escape_chars) const
312 std::string result;
313 bool has_some_tags = 0;
314 MPD::Song::GetFunction get_fun = 0;
315 while (*++it != '}')
317 while (*it == '{')
319 std::string tags = ParseFormat(it, tags_separator, escape_chars);
320 if (!tags.empty())
322 has_some_tags = 1;
323 result += tags;
326 if (*it == '}')
327 break;
329 if (*it == '%')
331 size_t delimiter = 0;
332 if (isdigit(*++it))
334 delimiter = atol(&*it);
335 while (isdigit(*++it)) { }
338 if (*it == '%')
340 result += *it;
341 get_fun = 0;
343 else
344 get_fun = charToGetFunction(*it);
346 if (get_fun)
348 std::string tag = getTags(get_fun, tags_separator);
349 if (!escape_chars.empty()) // prepend format escape character to all given chars to escape
351 for (size_t i = 0; i < escape_chars.length(); ++i)
352 for (size_t j = 0; (j = tag.find(escape_chars[i], j)) != std::string::npos; j += 2)
353 tag.replace(j, 1, std::string(1, FormatEscapeCharacter) + escape_chars[i]);
355 if (!tag.empty() && (get_fun != &MPD::Song::getLength || getDuration() > 0))
357 if (delimiter && tag.size() > delimiter)
359 // Shorten length string by just chopping off the tail
360 if (get_fun == &MPD::Song::getLength)
361 tag = tag.substr(0, delimiter);
362 else
363 tag = ToString(wideShorten(ToWString(tag), delimiter));
365 has_some_tags = 1;
366 result += tag;
368 else
369 break;
372 else
373 result += *it;
375 int brace_counter = 0;
376 if (*it != '}' || !has_some_tags)
378 for (; *it != '}' || brace_counter; ++it)
380 if (*it == '{')
381 ++brace_counter;
382 else if (*it == '}')
383 --brace_counter;
385 if (*++it == '|')
386 return ParseFormat(++it, tags_separator, escape_chars);
387 else
388 return "";
390 else
392 if (*(it+1) == '|')
394 for (; *it != '}' || *(it+1) == '|' || brace_counter; ++it)
396 if (*it == '{')
397 ++brace_counter;
398 else if (*it == '}')
399 --brace_counter;
402 ++it;
403 return result;