move code responsible for screen resize to SIGWINCH handler
[ncmpcpp.git] / src / song.cpp
blob5440144341e55e37395f08997f40a3eb7b8a8b7e
1 /***************************************************************************
2 * Copyright (C) 2008-2009 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 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
25 #include <cstring>
26 #include <iomanip>
27 #include <sstream>
28 #include <stdexcept>
30 #include "charset.h"
31 #include "conv.h"
32 #include "error.h"
33 #include "song.h"
35 namespace
37 unsigned calc_hash(const char *p)
39 unsigned hash = 5381;
40 while (*p)
41 hash = (hash << 5) + hash + *p++;
42 return hash;
46 MPD::Song::Song(mpd_song *s, bool copy_ptr) : itsSong(s),
47 itsFile(0),
48 itsTags(0),
49 itsSlash(std::string::npos),
50 itsHash(0),
51 copyPtr(copy_ptr),
52 isLocalised(0)
54 if (itsSong)
55 SetHashAndSlash();
58 MPD::Song::Song(const Song &s) : itsSong(s.copyPtr ? s.itsSong : mpd_song_dup(s.itsSong)),
59 itsFile(s.itsFile ? strdup(s.itsFile) : 0),
60 itsTags(s.itsTags ? new TagMap(*s.itsTags) : 0),
61 itsNewName(s.itsNewName),
62 itsSlash(s.itsSlash),
63 itsHash(s.itsHash),
64 copyPtr(s.copyPtr),
65 isLocalised(s.isLocalised)
69 MPD::Song::~Song()
71 if (itsSong)
72 mpd_song_free(itsSong);
73 delete [] itsFile;
74 delete itsTags;
77 std::string MPD::Song::GetLength(unsigned pos) const
79 if (pos > 0)
80 return "";
81 unsigned len = mpd_song_get_duration(itsSong);
82 return !len ? "-:--" : ShowTime(len);
85 void MPD::Song::Localize()
87 # ifdef HAVE_ICONV_H
88 if (isLocalised)
89 return;
90 const char *tag, *conv_tag;
91 conv_tag = tag = mpd_song_get_uri(itsSong);
92 utf_to_locale(conv_tag, 0);
93 if (tag != conv_tag) // file has been converted
95 itsFile = conv_tag;
96 SetHashAndSlash();
98 for (unsigned t = MPD_TAG_ARTIST; t <= MPD_TAG_DISC; ++t)
100 unsigned pos = 0;
101 for (; (tag = mpd_song_get_tag(itsSong, mpd_tag_type(t), pos)); ++pos)
103 conv_tag = tag;
104 utf_to_locale(conv_tag, 0);
105 if (tag != conv_tag) // tag has been converted
107 SetTag(mpd_tag_type(t), pos, conv_tag);
108 delete [] conv_tag;
112 isLocalised = 1;
113 # endif // HAVE_ICONV_H
116 void MPD::Song::Clear()
118 if (itsSong)
119 mpd_song_free(itsSong);
120 itsSong = 0;
122 delete [] itsFile;
123 itsFile = 0;
125 delete itsTags;
126 itsTags = 0;
128 itsNewName.clear();
129 itsSlash = std::string::npos;
130 itsHash = 0;
131 isLocalised = 0;
132 copyPtr = 0;
135 bool MPD::Song::Empty() const
137 return !itsSong;
140 bool MPD::Song::isFromDB() const
142 return (MyFilename()[0] != '/') || itsSlash == std::string::npos;
145 bool MPD::Song::isStream() const
147 return !strncmp(MyFilename(), "http://", 7);
150 std::string MPD::Song::GetFile(unsigned pos) const
152 return pos > 0 ? "" : MyFilename();
155 std::string MPD::Song::GetName(unsigned pos) const
157 if (pos > 0)
158 return "";
159 std::string name = GetTag(MPD_TAG_NAME, 0);
160 if (!name.empty())
161 return name;
162 else if (itsSlash != std::string::npos)
163 return MyFilename()+itsSlash+1;
164 else
165 return MyFilename();
168 std::string MPD::Song::GetDirectory(unsigned pos) const
170 if (pos > 0 || isStream())
171 return "";
172 else if (itsSlash == std::string::npos)
173 return "/";
174 else
175 return std::string(MyFilename(), itsSlash);
178 std::string MPD::Song::GetArtist(unsigned pos) const
180 return GetTag(MPD_TAG_ARTIST, pos);
183 std::string MPD::Song::GetTitle(unsigned pos) const
185 return GetTag(MPD_TAG_TITLE, pos);
188 std::string MPD::Song::GetAlbum(unsigned pos) const
190 return GetTag(MPD_TAG_ALBUM, pos);
193 std::string MPD::Song::GetTrack(unsigned pos) const
195 std::string track = GetTag(MPD_TAG_TRACK, pos);
196 return (track.length() == 1 && track[0] != '0') || (track.length() > 3 && track[1] == '/') ? "0"+track : track;
199 std::string MPD::Song::GetTrackNumber(unsigned pos) const
201 std::string track = GetTag(MPD_TAG_TRACK, pos);
202 size_t slash = track.find('/');
203 if (slash != std::string::npos)
204 track.resize(slash);
205 return track.length() == 1 && track[0] != '0' ? "0"+track : track;
208 std::string MPD::Song::GetDate(unsigned pos) const
210 return GetTag(MPD_TAG_DATE, pos);
213 std::string MPD::Song::GetGenre(unsigned pos) const
215 return GetTag(MPD_TAG_GENRE, pos);
218 std::string MPD::Song::GetComposer(unsigned pos) const
220 return GetTag(MPD_TAG_COMPOSER, pos);
223 std::string MPD::Song::GetPerformer(unsigned pos) const
225 return GetTag(MPD_TAG_PERFORMER, pos);
228 std::string MPD::Song::GetDisc(unsigned pos) const
230 return GetTag(MPD_TAG_DISC, pos);
233 std::string MPD::Song::GetComment(unsigned pos) const
235 return GetTag(MPD_TAG_COMMENT, pos);
238 std::string MPD::Song::GetTags(GetFunction f) const
240 unsigned pos = 0;
241 std::string result;
242 for (std::string tag; !(tag = (this->*f)(pos)).empty(); ++pos)
244 if (!result.empty())
245 result += ", ";
246 result += tag;
248 return result;
251 void MPD::Song::SetArtist(const std::string &str, unsigned pos)
253 SetTag(MPD_TAG_ARTIST, pos, str);
256 void MPD::Song::SetTitle(const std::string &str, unsigned pos)
258 SetTag(MPD_TAG_TITLE, pos, str);
261 void MPD::Song::SetAlbum(const std::string &str, unsigned pos)
263 SetTag(MPD_TAG_ALBUM, pos, str);
266 void MPD::Song::SetTrack(const std::string &str, unsigned pos)
268 SetTag(MPD_TAG_TRACK, pos, str);
271 void MPD::Song::SetTrack(unsigned track, unsigned pos)
273 SetTag(MPD_TAG_TRACK, pos, IntoStr(track));
276 void MPD::Song::SetDate(const std::string &str, unsigned pos)
278 SetTag(MPD_TAG_DATE, pos, str);
281 void MPD::Song::SetDate(unsigned year, unsigned pos)
283 SetTag(MPD_TAG_DATE, pos, IntoStr(year));
286 void MPD::Song::SetGenre(const std::string &str, unsigned pos)
288 SetTag(MPD_TAG_GENRE, pos, str);
291 void MPD::Song::SetComposer(const std::string &str, unsigned pos)
293 SetTag(MPD_TAG_COMPOSER, pos, str);
296 void MPD::Song::SetPerformer(const std::string &str, unsigned pos)
298 SetTag(MPD_TAG_PERFORMER, pos, str);
301 void MPD::Song::SetDisc(const std::string &str, unsigned pos)
303 SetTag(MPD_TAG_DISC, pos, str);
306 void MPD::Song::SetComment(const std::string &str, unsigned pos)
308 SetTag(MPD_TAG_COMMENT, pos, str);
311 void MPD::Song::SetPosition(unsigned pos)
313 mpd_song_set_pos(itsSong, pos);
316 void MPD::Song::SetTags(SetFunction f, const std::string &value)
318 unsigned pos = 0;
319 // tag editor can save multiple instances of performer and composer
320 // tag, so we need to split them and allow it to read them separately.
321 if (f == &Song::SetComposer || f == &Song::SetPerformer)
323 for (size_t i = 0; i != std::string::npos; i = value.find(",", i))
325 if (i)
326 ++i;
327 while (value[i] == ' ')
328 ++i;
329 size_t j = value.find(",", i);
330 (this->*f)(value.substr(i, j-i), pos++);
333 else
334 (this->*f)(value, pos++);
335 // there should be empty tag at the end since if we are
336 // reading them, original tag from mpd_song at the position
337 // after the last one locally set can be non-empty and in this
338 // case GetTags() would read it, which is undesirable.
339 (this->*f)("", pos);
342 std::string MPD::Song::ParseFormat(std::string::const_iterator &it, const char *escape_chars) const
344 std::string result;
345 bool has_some_tags = 0;
346 MPD::Song::GetFunction get = 0;
347 while (*++it != '}')
349 while (*it == '{')
351 std::string tags = ParseFormat(it, escape_chars);
352 if (!tags.empty())
354 has_some_tags = 1;
355 result += tags;
358 if (*it == '}')
359 break;
361 if (*it == '%')
363 switch (*++it)
365 case 'l':
366 get = &MPD::Song::GetLength;
367 break;
368 case 'D':
369 get = &MPD::Song::GetDirectory;
370 break;
371 case 'f':
372 get = &MPD::Song::GetName;
373 break;
374 case 'a':
375 get = &MPD::Song::GetArtist;
376 break;
377 case 'b':
378 get = &MPD::Song::GetAlbum;
379 break;
380 case 'y':
381 get = &MPD::Song::GetDate;
382 break;
383 case 'n':
384 get = &MPD::Song::GetTrackNumber;
385 break;
386 case 'N':
387 get = &MPD::Song::GetTrack;
388 break;
389 case 'g':
390 get = &MPD::Song::GetGenre;
391 break;
392 case 'c':
393 get = &MPD::Song::GetComposer;
394 break;
395 case 'p':
396 get = &MPD::Song::GetPerformer;
397 break;
398 case 'd':
399 get = &MPD::Song::GetDisc;
400 break;
401 case 'C':
402 get = &MPD::Song::GetComment;
403 break;
404 case 't':
405 get = &MPD::Song::GetTitle;
406 break;
407 case '%':
408 result += *it; // no break here
409 default:
410 get = 0;
411 break;
413 if (get)
415 std::string tag = GetTags(get);
416 if (escape_chars) // prepend format escape character to all given chars to escape
417 for (const char *ch = escape_chars; *ch; ++ch)
418 for (size_t i = 0; (i = tag.find(*ch, i)) != std::string::npos; i += 2)
419 tag.replace(i, 1, std::string(1, FormatEscapeCharacter) + ch);
420 if (!tag.empty() && (get != &MPD::Song::GetLength || GetTotalLength()))
422 has_some_tags = 1;
423 result += tag;
425 else
426 break;
429 else
430 result += *it;
432 int brace_counter = 0;
433 if (*it != '}' || !has_some_tags)
435 for (; *it != '}' || brace_counter; ++it)
437 if (*it == '{')
438 ++brace_counter;
439 else if (*it == '}')
440 --brace_counter;
442 if (*++it == '|')
443 return ParseFormat(++it, escape_chars);
444 else
445 return "";
447 else
449 if (*(it+1) == '|')
451 for (; *it != '}' || *(it+1) == '|' || brace_counter; ++it)
453 if (*it == '{')
454 ++brace_counter;
455 else if (*it == '}')
456 --brace_counter;
459 ++it;
460 return result;
464 std::string MPD::Song::toString(const std::string &format, const char *escape_chars) const
466 std::string::const_iterator it = format.begin();
467 return ParseFormat(it, escape_chars);
470 MPD::Song &MPD::Song::operator=(const MPD::Song &s)
472 if (this == &s)
473 return *this;
474 if (itsSong)
475 mpd_song_free(itsSong);
476 delete [] itsFile;
477 delete itsTags;
478 itsSong = s.copyPtr ? s.itsSong : (s.itsSong ? mpd_song_dup(s.itsSong) : 0);
479 itsFile = s.itsFile ? strdup(s.itsFile) : 0;
480 itsTags = s.itsTags ? new TagMap(*s.itsTags) : 0;
481 itsNewName = s.itsNewName;
482 itsSlash = s.itsSlash;
483 itsHash = s.itsHash;
484 copyPtr = s.copyPtr;
485 isLocalised = s.isLocalised;
486 return *this;
489 std::string MPD::Song::ShowTime(int length)
491 std::ostringstream ss;
493 int hours = length/3600;
494 length -= hours*3600;
495 int minutes = length/60;
496 length -= minutes*60;
497 int seconds = length;
499 if (hours > 0)
501 ss << hours << ":"
502 << std::setw(2) << std::setfill('0') << minutes << ":"
503 << std::setw(2) << std::setfill('0') << seconds;
505 else
507 ss << minutes << ":"
508 << std::setw(2) << std::setfill('0') << seconds;
510 return ss.str();
513 void MPD::Song::ValidateFormat(const std::string &type, const std::string &s)
515 int braces = 0;
516 for (std::string::const_iterator it = s.begin(); it != s.end(); ++it)
518 if (*it == '{')
519 ++braces;
520 else if (*it == '}')
521 --braces;
523 if (braces)
524 FatalError(type + ": number of opening and closing braces does not equal!");
527 void MPD::Song::SetHashAndSlash()
529 const char *filename = MyFilename();
530 if (!isStream())
532 const char *tmp = strrchr(filename, '/');
533 itsSlash = tmp ? tmp-filename : std::string::npos;
535 if (!itsFile)
536 itsHash = calc_hash(filename);
539 const char *MPD::Song::MyFilename() const
541 return itsFile ? itsFile : mpd_song_get_uri(itsSong);
544 void MPD::Song::SetTag(mpd_tag_type type, unsigned pos, const std::string &value)
546 if (!itsTags)
547 itsTags = new TagMap;
548 (*itsTags)[std::make_pair(type, pos)] = value;
551 std::string MPD::Song::GetTag(mpd_tag_type type, unsigned pos) const
553 if (itsTags)
555 TagMap::const_iterator it = itsTags->find(std::make_pair(type, pos));
556 if (it != itsTags->end())
557 return it->second;
559 const char *tag = mpd_song_get_tag(itsSong, type, pos);
560 return tag ? tag : "";