Merge branch 'libmpdclient2'

Conflicts:
	src/browser.cpp
	src/ncmpcpp.cpp
This commit is contained in:
Andrzej Rybczak
2009-10-04 16:23:42 +02:00
24 changed files with 579 additions and 3648 deletions

View File

@@ -32,35 +32,52 @@
#include "error.h"
#include "song.h"
MPD::Song::Song(mpd_Song *s, bool copy_ptr) : itsSong(s ? s : mpd_newSong()),
namespace
{
unsigned calc_hash(const char *p)
{
unsigned hash = 5381;
while (*p)
hash = (hash << 5) + hash + *p++;
return hash;
}
}
MPD::Song::Song(mpd_song *s, bool copy_ptr) : itsSong(s),
itsFile(0),
itsTags(0),
itsSlash(std::string::npos),
itsHash(0),
copyPtr(copy_ptr),
isLocalised(0)
{
if (itsSong->file)
if (itsSong)
SetHashAndSlash();
}
MPD::Song::Song(const Song &s) : itsSong(0),
MPD::Song::Song(const Song &s) : itsSong(s.copyPtr ? s.itsSong : mpd_song_dup(s.itsSong)),
itsFile(s.itsFile ? strdup(s.itsFile) : 0),
itsTags(s.itsTags ? new TagMap(*s.itsTags) : 0),
itsNewName(s.itsNewName),
itsSlash(s.itsSlash),
itsHash(s.itsHash),
copyPtr(s.copyPtr),
isLocalised(s.isLocalised)
{
itsSong = s.copyPtr ? s.itsSong : mpd_songDup(s.itsSong);
}
MPD::Song::~Song()
{
if (itsSong)
mpd_freeSong(itsSong);
mpd_song_free(itsSong);
delete [] itsFile;
delete itsTags;
}
std::string MPD::Song::GetLength() const
{
return itsSong->time <= 0 ? "-:--" : ShowTime(itsSong->time);
unsigned len = mpd_song_get_duration(itsSong);
return !len ? "-:--" : ShowTime(len);
}
void MPD::Song::Localize()
@@ -68,19 +85,28 @@ void MPD::Song::Localize()
# ifdef HAVE_ICONV_H
if (isLocalised)
return;
str_pool_utf_to_locale(itsSong->file);
SetHashAndSlash();
str_pool_utf_to_locale(itsSong->artist);
str_pool_utf_to_locale(itsSong->title);
str_pool_utf_to_locale(itsSong->album);
str_pool_utf_to_locale(itsSong->track);
str_pool_utf_to_locale(itsSong->name);
str_pool_utf_to_locale(itsSong->date);
str_pool_utf_to_locale(itsSong->genre);
str_pool_utf_to_locale(itsSong->composer);
str_pool_utf_to_locale(itsSong->performer);
str_pool_utf_to_locale(itsSong->disc);
str_pool_utf_to_locale(itsSong->comment);
const char *tag, *conv_tag;
conv_tag = tag = mpd_song_get_uri(itsSong);
utf_to_locale(conv_tag, 0);
if (tag != conv_tag) // file has been converted
{
itsFile = conv_tag;
SetHashAndSlash();
}
for (unsigned t = MPD_TAG_ARTIST; t <= MPD_TAG_DISC; ++t)
{
unsigned pos = 0;
for (; (tag = mpd_song_get_tag(itsSong, mpd_tag_type(t), pos)); ++pos)
{
conv_tag = tag;
utf_to_locale(conv_tag, 0);
if (tag != conv_tag) // tag has been converted
{
SetTag(mpd_tag_type(t), pos, conv_tag);
delete [] conv_tag;
}
}
}
isLocalised = 1;
# endif // HAVE_ICONV_H
}
@@ -88,8 +114,15 @@ void MPD::Song::Localize()
void MPD::Song::Clear()
{
if (itsSong)
mpd_freeSong(itsSong);
itsSong = mpd_newSong();
mpd_song_free(itsSong);
itsSong = 0;
delete [] itsFile;
itsFile = 0;
delete itsTags;
itsTags = 0;
itsNewName.clear();
itsSlash = std::string::npos;
itsHash = 0;
@@ -99,210 +132,172 @@ void MPD::Song::Clear()
bool MPD::Song::Empty() const
{
return !itsSong || (!itsSong->file && !itsSong->title && !itsSong->artist && !itsSong->album && !itsSong->date && !itsSong->track && !itsSong->genre && !itsSong->composer && !itsSong->performer && !itsSong->disc && !itsSong->comment);
return !itsSong;
}
bool MPD::Song::isFromDB() const
{
return (itsSong->file && itsSong->file[0] != '/') || itsSlash == std::string::npos;
return (MyFilename()[0] != '/') || itsSlash == std::string::npos;
}
bool MPD::Song::isStream() const
{
return !strncmp(itsSong->file, "http://", 7);
return !strncmp(MyFilename(), "http://", 7);
}
std::string MPD::Song::GetFile() const
{
return !itsSong->file ? "" : itsSong->file;
return MyFilename();
}
std::string MPD::Song::GetName() const
{
if (itsSong->name)
return itsSong->name;
else if (!itsSong->file)
return "";
std::string name = GetTag(MPD_TAG_NAME, 0);
if (!name.empty())
return name;
else if (itsSlash != std::string::npos)
return itsSong->file+itsSlash+1;
return MyFilename()+itsSlash+1;
else
return itsSong->file;
return MyFilename();
}
std::string MPD::Song::GetDirectory() const
{
if (!itsSong->file || isStream())
if (isStream())
return "";
else if (itsSlash == std::string::npos)
return "/";
else
return std::string(itsSong->file, itsSlash);
return std::string(MyFilename(), itsSlash);
}
std::string MPD::Song::GetArtist() const
{
return !itsSong->artist ? "" : itsSong->artist;
return GetTag(MPD_TAG_ARTIST, 0);
}
std::string MPD::Song::GetTitle() const
{
return !itsSong->title ? "" : itsSong->title;
return GetTag(MPD_TAG_TITLE, 0);
}
std::string MPD::Song::GetAlbum() const
{
return !itsSong->album ? "" : itsSong->album;
return GetTag(MPD_TAG_ALBUM, 0);
}
std::string MPD::Song::GetTrack() const
{
if (!itsSong->track)
return "";
else if (itsSong->track[0] != '0' && !itsSong->track[1])
return "0"+std::string(itsSong->track);
else
return itsSong->track;
std::string track = GetTag(MPD_TAG_TRACK, 0);
return track.length() == 1 && track[0] != '0' ? "0"+track : track;
}
std::string MPD::Song::GetTrackNumber() const
{
if (!itsSong->track)
return "";
const char *slash = strrchr(itsSong->track, '/');
if (slash)
std::string track = GetTag(MPD_TAG_TRACK, 0);
size_t slash = track.find('/');
if (slash != std::string::npos)
{
std::string result(itsSong->track, slash-itsSong->track);
return result[0] != '0' && result.length() == 1 ? "0"+result : result;
track = track.substr(slash+1);
return track.length() == 1 && track[0] != '0' ? "0"+track : track;
}
else
return GetTrack();
return track;
}
std::string MPD::Song::GetDate() const
{
return !itsSong->date ? "" : itsSong->date;
return GetTag(MPD_TAG_DATE, 0);
}
std::string MPD::Song::GetGenre() const
{
return !itsSong->genre ? "" : itsSong->genre;
return GetTag(MPD_TAG_GENRE, 0);
}
std::string MPD::Song::GetComposer() const
{
return !itsSong->composer ? "" : itsSong->composer;
return GetTag(MPD_TAG_COMPOSER, 0);
}
std::string MPD::Song::GetPerformer() const
{
return !itsSong->performer ? "" : itsSong->performer;
return GetTag(MPD_TAG_PERFORMER, 0);
}
std::string MPD::Song::GetDisc() const
{
return !itsSong->disc ? "" : itsSong->disc;
return GetTag(MPD_TAG_DISC, 0);
}
std::string MPD::Song::GetComment() const
{
return !itsSong->comment ? "" : itsSong->comment;
}
void MPD::Song::SetFile(const std::string &str)
{
if (itsSong->file)
str_pool_put(itsSong->file);
itsSong->file = str.empty() ? 0 : str_pool_get(str.c_str());
SetHashAndSlash();
return GetTag(MPD_TAG_COMMENT, 0);
}
void MPD::Song::SetArtist(const std::string &str)
{
if (itsSong->artist)
str_pool_put(itsSong->artist);
itsSong->artist = str.empty() ? 0 : str_pool_get(str.c_str());
SetTag(MPD_TAG_ARTIST, 0, str);
}
void MPD::Song::SetTitle(const std::string &str)
{
if (itsSong->title)
str_pool_put(itsSong->title);
itsSong->title = str.empty() ? 0 : str_pool_get(str.c_str());
SetTag(MPD_TAG_TITLE, 0, str);
}
void MPD::Song::SetAlbum(const std::string &str)
{
if (itsSong->album)
str_pool_put(itsSong->album);
itsSong->album = str.empty() ? 0 : str_pool_get(str.c_str());
SetTag(MPD_TAG_ALBUM, 0, str);
}
void MPD::Song::SetTrack(const std::string &str)
{
if (itsSong->track)
str_pool_put(itsSong->track);
itsSong->track = str.empty() ? 0 : str_pool_get(str.c_str());
SetTag(MPD_TAG_TRACK, 0, str);
}
void MPD::Song::SetTrack(int track)
void MPD::Song::SetTrack(unsigned track)
{
if (itsSong->track)
str_pool_put(itsSong->track);
itsSong->track = str_pool_get(IntoStr(track).c_str());
SetTag(MPD_TAG_ARTIST, 0, IntoStr(track));
}
void MPD::Song::SetDate(const std::string &str)
{
if (itsSong->date)
str_pool_put(itsSong->date);
itsSong->date = str.empty() ? 0 : str_pool_get(str.c_str());
SetTag(MPD_TAG_DATE, 0, str);
}
void MPD::Song::SetDate(int year)
void MPD::Song::SetDate(unsigned year)
{
if (itsSong->date)
str_pool_put(itsSong->date);
itsSong->date = str_pool_get(IntoStr(year).c_str());
SetTag(MPD_TAG_TRACK, 0, IntoStr(year));
}
void MPD::Song::SetGenre(const std::string &str)
{
if (itsSong->genre)
str_pool_put(itsSong->genre);
itsSong->genre = str.empty() ? 0 : str_pool_get(str.c_str());
SetTag(MPD_TAG_GENRE, 0, str);
}
void MPD::Song::SetComposer(const std::string &str)
{
if (itsSong->composer)
str_pool_put(itsSong->composer);
itsSong->composer = str.empty() ? 0 : str_pool_get(str.c_str());
SetTag(MPD_TAG_COMPOSER, 0, str);
}
void MPD::Song::SetPerformer(const std::string &str)
{
if (itsSong->performer)
str_pool_put(itsSong->performer);
itsSong->performer = str.empty() ? 0 : str_pool_get(str.c_str());
SetTag(MPD_TAG_PERFORMER, 0, str);
}
void MPD::Song::SetDisc(const std::string &str)
{
if (itsSong->disc)
str_pool_put(itsSong->disc);
itsSong->disc = str.empty() ? 0 : str_pool_get(str.c_str());
SetTag(MPD_TAG_DISC, 0, str);
}
void MPD::Song::SetComment(const std::string &str)
{
if (itsSong->comment)
str_pool_put(itsSong->comment);
itsSong->comment = str.empty() ? 0 : str_pool_get(str.c_str());
SetTag(MPD_TAG_COMMENT, 0, str);
}
void MPD::Song::SetPosition(int pos)
void MPD::Song::SetPosition(unsigned pos)
{
itsSong->pos = pos;
mpd_song_set_pos(itsSong, pos);
}
std::string MPD::Song::ParseFormat(std::string::const_iterator &it, const char *escape_chars) const
@@ -438,8 +433,12 @@ MPD::Song &MPD::Song::operator=(const MPD::Song &s)
if (this == &s)
return *this;
if (itsSong)
mpd_freeSong(itsSong);
itsSong = s.copyPtr ? s.itsSong : mpd_songDup(s.itsSong);
mpd_song_free(itsSong);
delete [] itsFile;
delete itsTags;
itsSong = s.copyPtr ? s.itsSong : (s.itsSong ? mpd_song_dup(s.itsSong) : 0);
itsFile = s.itsFile ? strdup(s.itsFile) : 0;
itsTags = s.itsTags ? new TagMap(*s.itsTags) : 0;
itsNewName = s.itsNewName;
itsSlash = s.itsSlash;
itsHash = s.itsHash;
@@ -488,13 +487,39 @@ void MPD::Song::ValidateFormat(const std::string &type, const std::string &s)
void MPD::Song::SetHashAndSlash()
{
if (!itsSong->file)
return;
const char *filename = MyFilename();
if (!isStream())
{
const char *tmp = strrchr(itsSong->file, '/');
itsSlash = tmp ? tmp-itsSong->file : std::string::npos;
const char *tmp = strrchr(filename, '/');
itsSlash = tmp ? tmp-filename : std::string::npos;
}
itsHash = calc_hash(itsSong->file);
if (!itsFile)
itsHash = calc_hash(filename);
}
const char *MPD::Song::MyFilename() const
{
return itsFile ? itsFile : mpd_song_get_uri(itsSong);
}
void MPD::Song::SetTag(mpd_tag_type type, unsigned pos, const std::string &value)
{
if (value.empty())
return;
if (!itsTags)
itsTags = new TagMap;
(*itsTags)[std::make_pair(type, pos)] = value;
}
std::string MPD::Song::GetTag(mpd_tag_type type, unsigned pos) const
{
if (itsTags)
{
TagMap::const_iterator it = itsTags->find(std::make_pair(type, pos));
if (it != itsTags->end())
return it->second;
}
const char *tag = mpd_song_get_tag(itsSong, type, pos);
return tag ? tag : "";
}