some MPD::Song improvements and syntax simplifications

This commit is contained in:
Andrzej Rybczak
2009-07-05 14:13:18 +02:00
parent df051bf674
commit f60cc71b91
11 changed files with 65 additions and 62 deletions

View File

@@ -111,7 +111,7 @@ void Browser::EnterPressed()
if (Config.ncmpc_like_songs_adding && w->isBold()) if (Config.ncmpc_like_songs_adding && w->isBold())
{ {
bool found = 0; bool found = 0;
long long hash = w->Current().song->GetHash(); unsigned hash = w->Current().song->GetHash();
for (size_t i = 0; i < myPlaylist->Main()->Size(); ++i) for (size_t i = 0; i < myPlaylist->Main()->Size(); ++i)
{ {
if (myPlaylist->Main()->at(i).GetHash() == hash) if (myPlaylist->Main()->at(i).GetHash() == hash)
@@ -225,7 +225,7 @@ void Browser::SpacePressed()
if (Config.ncmpc_like_songs_adding && w->isBold()) if (Config.ncmpc_like_songs_adding && w->isBold())
{ {
Playlist::BlockUpdate = 1; Playlist::BlockUpdate = 1;
long long hash = w->Current().song->GetHash(); unsigned hash = w->Current().song->GetHash();
Mpd.StartCommandsList(); Mpd.StartCommandsList();
for (size_t i = 0; i < myPlaylist->Main()->Size(); ++i) for (size_t i = 0; i < myPlaylist->Main()->Size(); ++i)
{ {
@@ -448,7 +448,7 @@ void Browser::LocateSong(const MPD::Song &s)
if (s.GetDirectory().empty()) if (s.GetDirectory().empty())
return; return;
Config.local_browser = !s.IsFromDB(); Config.local_browser = !s.isFromDB();
SwitchTo(); SwitchTo();

View File

@@ -335,7 +335,7 @@ void Info::PrepareSong(MPD::Song &s)
{ {
# ifdef HAVE_TAGLIB_H # ifdef HAVE_TAGLIB_H
std::string path_to_file; std::string path_to_file;
if (s.IsFromDB()) if (s.isFromDB())
path_to_file += Config.mpd_music_dir; path_to_file += Config.mpd_music_dir;
path_to_file += s.GetFile(); path_to_file += s.GetFile();
TagLib::FileRef f(path_to_file.c_str()); TagLib::FileRef f(path_to_file.c_str());

View File

@@ -523,7 +523,7 @@ void MediaLibrary::AddToPlaylist(bool add_n_play)
BlockItemListUpdate = 1; BlockItemListUpdate = 1;
if (Config.ncmpc_like_songs_adding && Songs->isBold()) if (Config.ncmpc_like_songs_adding && Songs->isBold())
{ {
long long hash = Songs->Current().GetHash(); unsigned hash = Songs->Current().GetHash();
if (add_n_play) if (add_n_play)
{ {
for (size_t i = 0; i < myPlaylist->Main()->Size(); ++i) for (size_t i = 0; i < myPlaylist->Main()->Size(); ++i)

View File

@@ -560,7 +560,7 @@ int Connection::AddSong(const std::string &path)
int Connection::AddSong(const Song &s) int Connection::AddSong(const Song &s)
{ {
return !s.Empty() ? (AddSong((!s.IsFromDB() ? "file://" : "") + (s.Localized() ? locale_to_utf_cpy(s.GetFile()) : s.GetFile()))) : -1; return !s.Empty() ? (AddSong((!s.isFromDB() ? "file://" : "") + (s.Localized() ? locale_to_utf_cpy(s.GetFile()) : s.GetFile()))) : -1;
} }
bool Connection::AddRandomSongs(size_t number) bool Connection::AddRandomSongs(size_t number)

View File

@@ -224,7 +224,7 @@ void PlaylistEditor::AddToPlaylist(bool add_n_play)
BlockItemListUpdate = 1; BlockItemListUpdate = 1;
if (Config.ncmpc_like_songs_adding && Content->isBold()) if (Config.ncmpc_like_songs_adding && Content->isBold())
{ {
long long hash = Content->Current().GetHash(); unsigned hash = Content->Current().GetHash();
if (add_n_play) if (add_n_play)
{ {
for (size_t i = 0; i < myPlaylist->Main()->Size(); ++i) for (size_t i = 0; i < myPlaylist->Main()->Size(); ++i)

View File

@@ -228,7 +228,7 @@ void SearchEngine::EnterPressed()
BlockItemListUpdate = 1; BlockItemListUpdate = 1;
if (Config.ncmpc_like_songs_adding && w->isBold()) if (Config.ncmpc_like_songs_adding && w->isBold())
{ {
long long hash = w->Current().second->GetHash(); unsigned hash = w->Current().second->GetHash();
for (size_t i = 0; i < myPlaylist->Main()->Size(); ++i) for (size_t i = 0; i < myPlaylist->Main()->Size(); ++i)
{ {
if (myPlaylist->Main()->at(i).GetHash() == hash) if (myPlaylist->Main()->at(i).GetHash() == hash)
@@ -271,7 +271,7 @@ void SearchEngine::SpacePressed()
if (Config.ncmpc_like_songs_adding && w->isBold()) if (Config.ncmpc_like_songs_adding && w->isBold())
{ {
Playlist::BlockUpdate = 1; Playlist::BlockUpdate = 1;
long long hash = w->Current().second->GetHash(); unsigned hash = w->Current().second->GetHash();
Mpd.StartCommandsList(); Mpd.StartCommandsList();
for (size_t i = 0; i < myPlaylist->Main()->Size(); ++i) for (size_t i = 0; i < myPlaylist->Main()->Size(); ++i)
{ {

View File

@@ -34,34 +34,18 @@ MPD::Song::Song(mpd_Song *s, bool copy_ptr) : itsSong(s ? s : mpd_newSong()),
itsSlash(std::string::npos), itsSlash(std::string::npos),
itsHash(0), itsHash(0),
copyPtr(copy_ptr), copyPtr(copy_ptr),
isStream(0),
isLocalised(0) isLocalised(0)
{ {
size_t file_len = itsSong->file ? strlen(itsSong->file) : 0;
if (itsSong->file) if (itsSong->file)
{ SetHashAndSlash();
CountLastSlashPosition();
if (strncmp(itsSong->file, "http://", 7) == 0)
isStream = 1;
}
// generate pseudo-hash
for (size_t i = 0; i < file_len; ++i)
{
itsHash += itsSong->file[i];
if (i%3)
itsHash *= itsSong->file[i];
}
} }
MPD::Song::Song(const Song &s) : itsSong(0), MPD::Song::Song(const Song &s) : itsSong(0),
itsNewName(s.itsNewName), itsNewName(s.itsNewName),
itsSlash(s.itsSlash), itsSlash(s.itsSlash),
itsHash(s.itsHash), itsHash(s.itsHash),
copyPtr(s.copyPtr), copyPtr(s.copyPtr),
isStream(s.isStream), isLocalised(s.isLocalised)
isLocalised(s.isLocalised)
{ {
itsSong = s.copyPtr ? s.itsSong : mpd_songDup(s.itsSong); itsSong = s.copyPtr ? s.itsSong : mpd_songDup(s.itsSong);
} }
@@ -74,9 +58,7 @@ MPD::Song::~Song()
std::string MPD::Song::GetLength() const std::string MPD::Song::GetLength() const
{ {
if (itsSong->time <= 0) return itsSong->time <= 0 ? "-:--" : ShowTime(itsSong->time);
return "-:--";
return ShowTime(itsSong->time);
} }
void MPD::Song::Localize() void MPD::Song::Localize()
@@ -85,7 +67,7 @@ void MPD::Song::Localize()
if (isLocalised) if (isLocalised)
return; return;
str_pool_utf_to_locale(itsSong->file); str_pool_utf_to_locale(itsSong->file);
CountLastSlashPosition(); SetHashAndSlash();
str_pool_utf_to_locale(itsSong->artist); str_pool_utf_to_locale(itsSong->artist);
str_pool_utf_to_locale(itsSong->title); str_pool_utf_to_locale(itsSong->title);
str_pool_utf_to_locale(itsSong->album); str_pool_utf_to_locale(itsSong->album);
@@ -107,9 +89,8 @@ void MPD::Song::Clear()
mpd_freeSong(itsSong); mpd_freeSong(itsSong);
itsSong = mpd_newSong(); itsSong = mpd_newSong();
itsNewName.clear(); itsNewName.clear();
itsSlash = 0; itsSlash = std::string::npos;
itsHash = 0; itsHash = 0;
isStream = 0;
isLocalised = 0; isLocalised = 0;
copyPtr = 0; copyPtr = 0;
} }
@@ -119,10 +100,14 @@ 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 || (!itsSong->file && !itsSong->title && !itsSong->artist && !itsSong->album && !itsSong->date && !itsSong->track && !itsSong->genre && !itsSong->composer && !itsSong->performer && !itsSong->disc && !itsSong->comment);
} }
bool MPD::Song::IsFromDB() const bool MPD::Song::isFromDB() const
{ {
const std::string &dir = GetDirectory(); return (itsSong->file && itsSong->file[0] != '/') || itsSlash == std::string::npos;
return dir[0] != '/' || dir == "/"; }
bool MPD::Song::isStream() const
{
return !strncmp(itsSong->file, "http://", 7);
} }
std::string MPD::Song::GetFile() const std::string MPD::Song::GetFile() const
@@ -132,12 +117,24 @@ std::string MPD::Song::GetFile() const
std::string MPD::Song::GetName() const std::string MPD::Song::GetName() const
{ {
return !itsSong->file ? "" : (itsSlash != std::string::npos && !isStream ? itsSong->file+itsSlash+1 : (isStream && itsSong->name ? itsSong->name : itsSong->file)); if (itsSong->name)
return itsSong->name;
else if (!itsSong->file)
return "";
else if (itsSlash != std::string::npos)
return itsSong->file+itsSlash+1;
else
return itsSong->file;
} }
std::string MPD::Song::GetDirectory() const std::string MPD::Song::GetDirectory() const
{ {
return !itsSong->file || isStream ? "" : itsSlash != std::string::npos ? std::string(itsSong->file).substr(0, itsSlash) : "/"; if (!itsSong->file || isStream())
return "";
else if (itsSlash == std::string::npos)
return "/";
else
return std::string(itsSong->file, itsSlash);
} }
std::string MPD::Song::GetArtist() const std::string MPD::Song::GetArtist() const
@@ -157,7 +154,12 @@ std::string MPD::Song::GetAlbum() const
std::string MPD::Song::GetTrack() const std::string MPD::Song::GetTrack() const
{ {
return !itsSong->track ? "" : (StrToInt(itsSong->track) < 10 && itsSong->track[0] != '0' ? "0"+std::string(itsSong->track) : itsSong->track); 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 MPD::Song::GetYear() const std::string MPD::Song::GetYear() const
@@ -195,7 +197,7 @@ void MPD::Song::SetFile(const std::string &str)
if (itsSong->file) if (itsSong->file)
str_pool_put(itsSong->file); str_pool_put(itsSong->file);
itsSong->file = str.empty() ? 0 : str_pool_get(str.c_str()); itsSong->file = str.empty() ? 0 : str_pool_get(str.c_str());
CountLastSlashPosition(); SetHashAndSlash();
} }
void MPD::Song::SetArtist(const std::string &str) void MPD::Song::SetArtist(const std::string &str)
@@ -449,7 +451,7 @@ std::string MPD::Song::toString(const std::string &format) const
return result; return result;
} }
MPD::Song & MPD::Song::operator=(const MPD::Song &s) MPD::Song &MPD::Song::operator=(const MPD::Song &s)
{ {
if (this == &s) if (this == &s)
return *this; return *this;
@@ -460,7 +462,6 @@ MPD::Song & MPD::Song::operator=(const MPD::Song &s)
itsSlash = s.itsSlash; itsSlash = s.itsSlash;
itsHash = s.itsHash; itsHash = s.itsHash;
copyPtr = s.copyPtr; copyPtr = s.copyPtr;
isStream = s.isStream;
isLocalised = s.isLocalised; isLocalised = s.isLocalised;
return *this; return *this;
} }
@@ -540,11 +541,12 @@ std::string MPD::Song::ShowTime(int length)
return ss.str(); return ss.str();
} }
void MPD::Song::CountLastSlashPosition() void MPD::Song::SetHashAndSlash()
{ {
if (!itsSong->file) if (!itsSong->file)
return; return;
char *tmp = strrchr(itsSong->file, '/'); char *tmp = strrchr(itsSong->file, '/');
itsSlash = tmp ? tmp-itsSong->file : std::string::npos; itsSlash = tmp && *(tmp-1) != '/' /* no http:// */ ? tmp-itsSong->file : std::string::npos;
itsHash = calc_hash(itsSong->file);
} }

View File

@@ -34,7 +34,7 @@ namespace MPD
typedef void (Song::*SetFunction)(const std::string &); typedef void (Song::*SetFunction)(const std::string &);
typedef std::string (Song::*GetFunction)() const; typedef std::string (Song::*GetFunction)() const;
Song() : itsSlash(std::string::npos), itsHash(0), copyPtr(0), isStream(0), isLocalised(0) { itsSong = mpd_newSong(); } Song() : itsSlash(std::string::npos), itsHash(0), copyPtr(0), isLocalised(0) { itsSong = mpd_newSong(); }
Song(mpd_Song *, bool = 0); Song(mpd_Song *, bool = 0);
Song(const Song &); Song(const Song &);
~Song(); ~Song();
@@ -54,7 +54,7 @@ namespace MPD
std::string GetComment() const; std::string GetComment() const;
std::string GetLength() const; std::string GetLength() const;
long long GetHash() const { return itsHash; } unsigned GetHash() const { return itsHash; }
int GetTotalLength() const { return itsSong->time < 0 ? 0 : itsSong->time; } int GetTotalLength() const { return itsSong->time < 0 ? 0 : itsSong->time; }
int GetPosition() const { return itsSong->pos; } int GetPosition() const { return itsSong->pos; }
int GetID() const { return itsSong->id; } int GetID() const { return itsSong->id; }
@@ -85,25 +85,24 @@ namespace MPD
void Localize(); void Localize();
void Clear(); void Clear();
bool Empty() const; bool Empty() const;
bool IsFromDB() const; bool isFromDB() const;
bool IsStream() const { return isStream; } bool isStream() const;
bool Localized() const { return isLocalised; } bool Localized() const { return isLocalised; }
Song & operator=(const Song &); Song &operator=(const Song &);
bool operator==(const Song &) const; bool operator==(const Song &) const;
bool operator!=(const Song &) const; bool operator!=(const Song &) const;
bool operator<(const Song &rhs) const; bool operator<(const Song &rhs) const;
static std::string ShowTime(int); static std::string ShowTime(int);
private: private:
void CountLastSlashPosition(); void SetHashAndSlash();
mpd_Song *itsSong; mpd_Song *itsSong;
std::string itsNewName; std::string itsNewName;
size_t itsSlash; size_t itsSlash;
long long itsHash; unsigned itsHash;
bool copyPtr; bool copyPtr;
bool isStream;
bool isLocalised; bool isLocalised;
}; };
} }

View File

@@ -33,7 +33,7 @@ struct slot {
struct slot *slots[NUM_SLOTS]; struct slot *slots[NUM_SLOTS];
static inline unsigned inline unsigned
calc_hash(const char *p) calc_hash(const char *p)
{ {
unsigned hash = 5381; unsigned hash = 5381;

View File

@@ -23,6 +23,8 @@
extern "C" { extern "C" {
#endif #endif
unsigned calc_hash(const char *p);
char *str_pool_get(const char *value); char *str_pool_get(const char *value);
char *str_pool_dup(const char *value); char *str_pool_dup(const char *value);

View File

@@ -63,7 +63,7 @@ void TinyTagEditor::Resize()
void TinyTagEditor::SwitchTo() void TinyTagEditor::SwitchTo()
{ {
if (itsEdited.IsStream()) if (itsEdited.isStream())
{ {
ShowMessage("Cannot edit streams!"); ShowMessage("Cannot edit streams!");
} }
@@ -78,7 +78,7 @@ void TinyTagEditor::SwitchTo()
else else
{ {
string message = "Cannot read file '"; string message = "Cannot read file '";
if (itsEdited.IsFromDB()) if (itsEdited.isFromDB())
message += Config.mpd_music_dir; message += Config.mpd_music_dir;
message += itsEdited.GetFile(); message += itsEdited.GetFile();
message += "'!"; message += "'!";
@@ -191,7 +191,7 @@ void TinyTagEditor::EnterPressed()
if (TagEditor::WriteTags(s)) if (TagEditor::WriteTags(s))
{ {
ShowMessage("Tags updated!"); ShowMessage("Tags updated!");
if (s.IsFromDB()) if (s.isFromDB())
{ {
Mpd.UpdateDirectory(locale_to_utf_cpy(s.GetDirectory())); Mpd.UpdateDirectory(locale_to_utf_cpy(s.GetDirectory()));
if (myOldScreen == mySearcher) if (myOldScreen == mySearcher)
@@ -245,7 +245,7 @@ bool TinyTagEditor::GetTags()
Song &s = itsEdited; Song &s = itsEdited;
string path_to_file; string path_to_file;
if (s.IsFromDB()) if (s.isFromDB())
path_to_file += Config.mpd_music_dir; path_to_file += Config.mpd_music_dir;
path_to_file += s.GetFile(); path_to_file += s.GetFile();
locale_to_utf(path_to_file); locale_to_utf(path_to_file);
@@ -918,7 +918,7 @@ bool TagEditor::WriteTags(Song &s)
{ {
using namespace TagLib; using namespace TagLib;
string path_to_file; string path_to_file;
bool file_is_from_db = s.IsFromDB(); bool file_is_from_db = s.isFromDB();
if (file_is_from_db) if (file_is_from_db)
path_to_file += Config.mpd_music_dir; path_to_file += Config.mpd_music_dir;
path_to_file += s.GetFile(); path_to_file += s.GetFile();