some MPD::Song improvements and syntax simplifications
This commit is contained in:
@@ -111,7 +111,7 @@ void Browser::EnterPressed()
|
||||
if (Config.ncmpc_like_songs_adding && w->isBold())
|
||||
{
|
||||
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)
|
||||
{
|
||||
if (myPlaylist->Main()->at(i).GetHash() == hash)
|
||||
@@ -225,7 +225,7 @@ void Browser::SpacePressed()
|
||||
if (Config.ncmpc_like_songs_adding && w->isBold())
|
||||
{
|
||||
Playlist::BlockUpdate = 1;
|
||||
long long hash = w->Current().song->GetHash();
|
||||
unsigned hash = w->Current().song->GetHash();
|
||||
Mpd.StartCommandsList();
|
||||
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())
|
||||
return;
|
||||
|
||||
Config.local_browser = !s.IsFromDB();
|
||||
Config.local_browser = !s.isFromDB();
|
||||
|
||||
SwitchTo();
|
||||
|
||||
|
||||
@@ -335,7 +335,7 @@ void Info::PrepareSong(MPD::Song &s)
|
||||
{
|
||||
# ifdef HAVE_TAGLIB_H
|
||||
std::string path_to_file;
|
||||
if (s.IsFromDB())
|
||||
if (s.isFromDB())
|
||||
path_to_file += Config.mpd_music_dir;
|
||||
path_to_file += s.GetFile();
|
||||
TagLib::FileRef f(path_to_file.c_str());
|
||||
|
||||
@@ -523,7 +523,7 @@ void MediaLibrary::AddToPlaylist(bool add_n_play)
|
||||
BlockItemListUpdate = 1;
|
||||
if (Config.ncmpc_like_songs_adding && Songs->isBold())
|
||||
{
|
||||
long long hash = Songs->Current().GetHash();
|
||||
unsigned hash = Songs->Current().GetHash();
|
||||
if (add_n_play)
|
||||
{
|
||||
for (size_t i = 0; i < myPlaylist->Main()->Size(); ++i)
|
||||
|
||||
@@ -560,7 +560,7 @@ int Connection::AddSong(const std::string &path)
|
||||
|
||||
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)
|
||||
|
||||
@@ -224,7 +224,7 @@ void PlaylistEditor::AddToPlaylist(bool add_n_play)
|
||||
BlockItemListUpdate = 1;
|
||||
if (Config.ncmpc_like_songs_adding && Content->isBold())
|
||||
{
|
||||
long long hash = Content->Current().GetHash();
|
||||
unsigned hash = Content->Current().GetHash();
|
||||
if (add_n_play)
|
||||
{
|
||||
for (size_t i = 0; i < myPlaylist->Main()->Size(); ++i)
|
||||
|
||||
@@ -228,7 +228,7 @@ void SearchEngine::EnterPressed()
|
||||
BlockItemListUpdate = 1;
|
||||
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)
|
||||
{
|
||||
if (myPlaylist->Main()->at(i).GetHash() == hash)
|
||||
@@ -271,7 +271,7 @@ void SearchEngine::SpacePressed()
|
||||
if (Config.ncmpc_like_songs_adding && w->isBold())
|
||||
{
|
||||
Playlist::BlockUpdate = 1;
|
||||
long long hash = w->Current().second->GetHash();
|
||||
unsigned hash = w->Current().second->GetHash();
|
||||
Mpd.StartCommandsList();
|
||||
for (size_t i = 0; i < myPlaylist->Main()->Size(); ++i)
|
||||
{
|
||||
|
||||
80
src/song.cpp
80
src/song.cpp
@@ -34,34 +34,18 @@ MPD::Song::Song(mpd_Song *s, bool copy_ptr) : itsSong(s ? s : mpd_newSong()),
|
||||
itsSlash(std::string::npos),
|
||||
itsHash(0),
|
||||
copyPtr(copy_ptr),
|
||||
isStream(0),
|
||||
isLocalised(0)
|
||||
{
|
||||
size_t file_len = itsSong->file ? strlen(itsSong->file) : 0;
|
||||
|
||||
if (itsSong->file)
|
||||
{
|
||||
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];
|
||||
}
|
||||
SetHashAndSlash();
|
||||
}
|
||||
|
||||
MPD::Song::Song(const Song &s) : itsSong(0),
|
||||
itsNewName(s.itsNewName),
|
||||
itsSlash(s.itsSlash),
|
||||
itsHash(s.itsHash),
|
||||
copyPtr(s.copyPtr),
|
||||
isStream(s.isStream),
|
||||
isLocalised(s.isLocalised)
|
||||
itsNewName(s.itsNewName),
|
||||
itsSlash(s.itsSlash),
|
||||
itsHash(s.itsHash),
|
||||
copyPtr(s.copyPtr),
|
||||
isLocalised(s.isLocalised)
|
||||
{
|
||||
itsSong = s.copyPtr ? s.itsSong : mpd_songDup(s.itsSong);
|
||||
}
|
||||
@@ -74,9 +58,7 @@ MPD::Song::~Song()
|
||||
|
||||
std::string MPD::Song::GetLength() const
|
||||
{
|
||||
if (itsSong->time <= 0)
|
||||
return "-:--";
|
||||
return ShowTime(itsSong->time);
|
||||
return itsSong->time <= 0 ? "-:--" : ShowTime(itsSong->time);
|
||||
}
|
||||
|
||||
void MPD::Song::Localize()
|
||||
@@ -85,7 +67,7 @@ void MPD::Song::Localize()
|
||||
if (isLocalised)
|
||||
return;
|
||||
str_pool_utf_to_locale(itsSong->file);
|
||||
CountLastSlashPosition();
|
||||
SetHashAndSlash();
|
||||
str_pool_utf_to_locale(itsSong->artist);
|
||||
str_pool_utf_to_locale(itsSong->title);
|
||||
str_pool_utf_to_locale(itsSong->album);
|
||||
@@ -107,9 +89,8 @@ void MPD::Song::Clear()
|
||||
mpd_freeSong(itsSong);
|
||||
itsSong = mpd_newSong();
|
||||
itsNewName.clear();
|
||||
itsSlash = 0;
|
||||
itsSlash = std::string::npos;
|
||||
itsHash = 0;
|
||||
isStream = 0;
|
||||
isLocalised = 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);
|
||||
}
|
||||
|
||||
bool MPD::Song::IsFromDB() const
|
||||
bool MPD::Song::isFromDB() const
|
||||
{
|
||||
const std::string &dir = GetDirectory();
|
||||
return dir[0] != '/' || dir == "/";
|
||||
return (itsSong->file && itsSong->file[0] != '/') || itsSlash == std::string::npos;
|
||||
}
|
||||
|
||||
bool MPD::Song::isStream() const
|
||||
{
|
||||
return !strncmp(itsSong->file, "http://", 7);
|
||||
}
|
||||
|
||||
std::string MPD::Song::GetFile() const
|
||||
@@ -132,12 +117,24 @@ std::string MPD::Song::GetFile() 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
|
||||
{
|
||||
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
|
||||
@@ -157,7 +154,12 @@ std::string MPD::Song::GetAlbum() 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
|
||||
@@ -195,7 +197,7 @@ 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());
|
||||
CountLastSlashPosition();
|
||||
SetHashAndSlash();
|
||||
}
|
||||
|
||||
void MPD::Song::SetArtist(const std::string &str)
|
||||
@@ -449,7 +451,7 @@ std::string MPD::Song::toString(const std::string &format) const
|
||||
return result;
|
||||
}
|
||||
|
||||
MPD::Song & MPD::Song::operator=(const MPD::Song &s)
|
||||
MPD::Song &MPD::Song::operator=(const MPD::Song &s)
|
||||
{
|
||||
if (this == &s)
|
||||
return *this;
|
||||
@@ -460,7 +462,6 @@ MPD::Song & MPD::Song::operator=(const MPD::Song &s)
|
||||
itsSlash = s.itsSlash;
|
||||
itsHash = s.itsHash;
|
||||
copyPtr = s.copyPtr;
|
||||
isStream = s.isStream;
|
||||
isLocalised = s.isLocalised;
|
||||
return *this;
|
||||
}
|
||||
@@ -540,11 +541,12 @@ std::string MPD::Song::ShowTime(int length)
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
void MPD::Song::CountLastSlashPosition()
|
||||
void MPD::Song::SetHashAndSlash()
|
||||
{
|
||||
if (!itsSong->file)
|
||||
return;
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
15
src/song.h
15
src/song.h
@@ -34,7 +34,7 @@ namespace MPD
|
||||
typedef void (Song::*SetFunction)(const std::string &);
|
||||
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(const Song &);
|
||||
~Song();
|
||||
@@ -54,7 +54,7 @@ namespace MPD
|
||||
std::string GetComment() 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 GetPosition() const { return itsSong->pos; }
|
||||
int GetID() const { return itsSong->id; }
|
||||
@@ -85,25 +85,24 @@ namespace MPD
|
||||
void Localize();
|
||||
void Clear();
|
||||
bool Empty() const;
|
||||
bool IsFromDB() const;
|
||||
bool IsStream() const { return isStream; }
|
||||
bool isFromDB() const;
|
||||
bool isStream() const;
|
||||
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 &rhs) const;
|
||||
|
||||
static std::string ShowTime(int);
|
||||
private:
|
||||
void CountLastSlashPosition();
|
||||
void SetHashAndSlash();
|
||||
|
||||
mpd_Song *itsSong;
|
||||
std::string itsNewName;
|
||||
size_t itsSlash;
|
||||
long long itsHash;
|
||||
unsigned itsHash;
|
||||
bool copyPtr;
|
||||
bool isStream;
|
||||
bool isLocalised;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ struct slot {
|
||||
|
||||
struct slot *slots[NUM_SLOTS];
|
||||
|
||||
static inline unsigned
|
||||
inline unsigned
|
||||
calc_hash(const char *p)
|
||||
{
|
||||
unsigned hash = 5381;
|
||||
|
||||
@@ -23,6 +23,8 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
unsigned calc_hash(const char *p);
|
||||
|
||||
char *str_pool_get(const char *value);
|
||||
|
||||
char *str_pool_dup(const char *value);
|
||||
|
||||
@@ -63,7 +63,7 @@ void TinyTagEditor::Resize()
|
||||
|
||||
void TinyTagEditor::SwitchTo()
|
||||
{
|
||||
if (itsEdited.IsStream())
|
||||
if (itsEdited.isStream())
|
||||
{
|
||||
ShowMessage("Cannot edit streams!");
|
||||
}
|
||||
@@ -78,7 +78,7 @@ void TinyTagEditor::SwitchTo()
|
||||
else
|
||||
{
|
||||
string message = "Cannot read file '";
|
||||
if (itsEdited.IsFromDB())
|
||||
if (itsEdited.isFromDB())
|
||||
message += Config.mpd_music_dir;
|
||||
message += itsEdited.GetFile();
|
||||
message += "'!";
|
||||
@@ -191,7 +191,7 @@ void TinyTagEditor::EnterPressed()
|
||||
if (TagEditor::WriteTags(s))
|
||||
{
|
||||
ShowMessage("Tags updated!");
|
||||
if (s.IsFromDB())
|
||||
if (s.isFromDB())
|
||||
{
|
||||
Mpd.UpdateDirectory(locale_to_utf_cpy(s.GetDirectory()));
|
||||
if (myOldScreen == mySearcher)
|
||||
@@ -245,7 +245,7 @@ bool TinyTagEditor::GetTags()
|
||||
Song &s = itsEdited;
|
||||
|
||||
string path_to_file;
|
||||
if (s.IsFromDB())
|
||||
if (s.isFromDB())
|
||||
path_to_file += Config.mpd_music_dir;
|
||||
path_to_file += s.GetFile();
|
||||
locale_to_utf(path_to_file);
|
||||
@@ -918,7 +918,7 @@ bool TagEditor::WriteTags(Song &s)
|
||||
{
|
||||
using namespace TagLib;
|
||||
string path_to_file;
|
||||
bool file_is_from_db = s.IsFromDB();
|
||||
bool file_is_from_db = s.isFromDB();
|
||||
if (file_is_from_db)
|
||||
path_to_file += Config.mpd_music_dir;
|
||||
path_to_file += s.GetFile();
|
||||
|
||||
Reference in New Issue
Block a user