reduce usage of pointers / replace std::pair in SearchEngine

This commit is contained in:
Andrzej Rybczak
2012-08-28 02:44:08 +02:00
parent a270fc8402
commit 397783d701
17 changed files with 237 additions and 241 deletions

View File

@@ -800,10 +800,10 @@ void Delete::Run()
ShowMessage("Deleting directories is disabled by default, see man page for more details"); ShowMessage("Deleting directories is disabled by default, see man page for more details");
return; return;
} }
if (item.type == itDirectory && item.song) // parent dir if (myBrowser->isParentDir(myBrowser->Main()->Choice()))
return; return;
std::string name = item.type == itSong ? item.song->getName() : item.name; std::string name = item.type == itSong ? item.song.getName() : item.name;
std::string question; std::string question;
if (myBrowser->Main()->hasSelected()) if (myBrowser->Main()->hasSelected())
question = "Delete selected items?"; question = "Delete selected items?";
@@ -826,7 +826,7 @@ void Delete::Run()
for (size_t i = 0; i < list.size(); ++i) for (size_t i = 0; i < list.size(); ++i)
{ {
const MPD::Item &it = (*myBrowser->Main())[list[i]]; const MPD::Item &it = (*myBrowser->Main())[list[i]];
name = it.type == itSong ? it.song->getName() : it.name; name = it.type == itSong ? it.song.getName() : it.name;
if (myBrowser->DeleteItem(it)) if (myBrowser->DeleteItem(it))
{ {
const char msg[] = "\"%s\" deleted"; const char msg[] = "\"%s\" deleted";

View File

@@ -128,13 +128,22 @@ void Browser::EnterPressed()
{ {
case itDirectory: case itDirectory:
{ {
GetDirectory(item.name, itsBrowsedDir); if (isParentDir(w->Choice()))
{
size_t slash = itsBrowsedDir.rfind("/");
if (slash != std::string::npos)
GetDirectory(itsBrowsedDir.substr(0, slash), itsBrowsedDir);
else
GetDirectory("/", itsBrowsedDir);
}
else
GetDirectory(item.name, itsBrowsedDir);
RedrawHeader = true; RedrawHeader = true;
break; break;
} }
case itSong: case itSong:
{ {
bool res = myPlaylist->Add(*item.song, w->isBold(), 1); bool res = myPlaylist->Add(item.song, w->isBold(), 1);
w->Bold(w->Choice(), res); w->Bold(w->Choice(), res);
break; break;
} }
@@ -161,7 +170,7 @@ void Browser::SpacePressed()
return; return;
} }
if (itsBrowsedDir != "/" && w->Choice() == 0 /* parent dir */) if (isParentDir(w->Choice()))
return; return;
const MPD::Item &item = w->Current(); const MPD::Item &item = w->Current();
@@ -169,9 +178,6 @@ void Browser::SpacePressed()
{ {
case itDirectory: case itDirectory:
{ {
if (itsBrowsedDir != "/" && !w->Choice())
break; // do not let add parent dir.
bool result; bool result;
# ifndef WIN32 # ifndef WIN32
if (isLocal()) if (isLocal())
@@ -184,7 +190,6 @@ void Browser::SpacePressed()
for (MPD::ItemList::const_iterator it = items.begin(); it != items.end(); ++it) for (MPD::ItemList::const_iterator it = items.begin(); it != items.end(); ++it)
list.push_back(it->song); list.push_back(it->song);
result = myPlaylist->Add(list, 0); result = myPlaylist->Add(list, 0);
FreeSongList(list);
} }
else else
# endif // !WIN32 # endif // !WIN32
@@ -195,7 +200,7 @@ void Browser::SpacePressed()
} }
case itSong: case itSong:
{ {
bool res = myPlaylist->Add(*item.song, w->isBold(), 0); bool res = myPlaylist->Add(item.song, w->isBold(), 0);
w->Bold(w->Choice(), res); w->Bold(w->Choice(), res);
break; break;
} }
@@ -252,7 +257,7 @@ void Browser::MouseButtonPressed(MEVENT me)
MPD::Song *Browser::CurrentSong() MPD::Song *Browser::CurrentSong()
{ {
return !w->Empty() && w->Current().type == itSong ? w->Current().song : 0; return !w->Empty() && w->Current().type == itSong ? &w->Current().song : 0;
} }
void Browser::ReverseSelection() void Browser::ReverseSelection()
@@ -268,7 +273,7 @@ void Browser::GetSelectedSongs(MPD::SongList &v)
w->GetSelected(selected); w->GetSelected(selected);
if (selected.empty()) if (selected.empty())
selected.push_back(w->Choice()); selected.push_back(w->Choice());
for (std::vector<size_t>::const_iterator it = selected.begin(); it != selected.end(); ++it) for (auto it = selected.begin(); it != selected.end(); ++it)
{ {
const MPD::Item &item = w->at(*it); const MPD::Item &item = w->at(*it);
switch (item.type) switch (item.type)
@@ -280,7 +285,7 @@ void Browser::GetSelectedSongs(MPD::SongList &v)
{ {
MPD::ItemList list; MPD::ItemList list;
GetLocalDirectory(list, item.name, 1); GetLocalDirectory(list, item.name, 1);
for (MPD::ItemList::const_iterator j = list.begin(); j != list.end(); ++j) for (auto j = list.begin(); j != list.end(); ++j)
v.push_back(j->song); v.push_back(j->song);
} }
else else
@@ -290,7 +295,7 @@ void Browser::GetSelectedSongs(MPD::SongList &v)
} }
case itSong: case itSong:
{ {
v.push_back(new MPD::Song(*item.song)); v.push_back(item.song);
break; break;
} }
case itPlaylist: case itPlaylist:
@@ -332,7 +337,7 @@ void Browser::LocateSong(const MPD::Song &s)
GetDirectory(s.getDirectory()); GetDirectory(s.getDirectory());
for (size_t i = 0; i < w->Size(); ++i) for (size_t i = 0; i < w->Size(); ++i)
{ {
if ((*w)[i].type == itSong && s.getHash() == (*w)[i].song->getHash()) if ((*w)[i].type == itSong && s.getHash() == (*w)[i].song.getHash())
{ {
w->Highlight(i); w->Highlight(i);
break; break;
@@ -353,20 +358,13 @@ void Browser::GetDirectory(std::string dir, std::string subdir)
locale_to_utf(dir); locale_to_utf(dir);
for (size_t i = 0; i < w->Size(); ++i)
if (w->at(i).type == itSong)
delete w->at(i).song;
w->Clear(); w->Clear();
if (dir != "/") if (dir != "/")
{ {
MPD::Item parent; MPD::Item parent;
size_t slash = dir.rfind("/"); parent.name = "..";
parent.song = reinterpret_cast<MPD::Song *>(1); // in that way we assume that's really parent dir
parent.name = slash != std::string::npos ? dir.substr(0, slash) : "/";
parent.type = itDirectory; parent.type = itDirectory;
utf_to_locale(parent.name);
w->AddOption(parent); w->AddOption(parent);
} }
@@ -377,7 +375,7 @@ void Browser::GetDirectory(std::string dir, std::string subdir)
Mpd.GetDirectory(dir, list); Mpd.GetDirectory(dir, list);
# endif // !WIN32 # endif // !WIN32
if (!isLocal()) // local directory is already sorted if (!isLocal()) // local directory is already sorted
sort(list.begin(), list.end(), CaseInsensitiveSorting()); std::sort(list.begin(), list.end(), CaseInsensitiveSorting());
for (MPD::ItemList::iterator it = list.begin(); it != list.end(); ++it) for (MPD::ItemList::iterator it = list.begin(); it != list.end(); ++it)
{ {
@@ -402,7 +400,7 @@ void Browser::GetDirectory(std::string dir, std::string subdir)
bool bold = 0; bool bold = 0;
for (size_t i = 0; i < myPlaylist->Items->Size(); ++i) for (size_t i = 0; i < myPlaylist->Items->Size(); ++i)
{ {
if (myPlaylist->Items->at(i).getHash() == it->song->getHash()) if (myPlaylist->Items->at(i).getHash() == it->song.getHash())
{ {
bold = 1; bold = 1;
break; break;
@@ -463,7 +461,7 @@ void Browser::GetLocalDirectory(MPD::ItemList &v, const std::string &directory,
{ {
new_item.type = itSong; new_item.type = itSong;
mpd_pair file_pair = { "file", full_path.c_str() }; mpd_pair file_pair = { "file", full_path.c_str() };
new_item.song = new MPD::Song(mpd_song_begin(&file_pair)); new_item.song = MPD::Song(mpd_song_begin(&file_pair));
# ifdef HAVE_TAGLIB_H # ifdef HAVE_TAGLIB_H
if (!recursively) if (!recursively)
TagEditor::ReadTags(*new_item.song); TagEditor::ReadTags(*new_item.song);
@@ -533,17 +531,17 @@ void Browser::ChangeBrowseMode()
bool Browser::DeleteItem(const MPD::Item &item) bool Browser::DeleteItem(const MPD::Item &item)
{ {
// parent dir // parent dir
if (item.type == itDirectory && item.song) if (item.type == itDirectory && item.name == "..")
return false; return false;
// playlist creatd by mpd // playlist created by mpd
if (!isLocal() && item.type == itPlaylist && CurrentDir() == "/") if (!isLocal() && item.type == itPlaylist && CurrentDir() == "/")
return Mpd.DeletePlaylist(locale_to_utf_cpy(item.name)); return Mpd.DeletePlaylist(locale_to_utf_cpy(item.name));
std::string path; std::string path;
if (!isLocal()) if (!isLocal())
path = Config.mpd_music_dir; path = Config.mpd_music_dir;
path += item.type == itSong ? item.song->getURI() : item.name; path += item.type == itSong ? item.song.getURI() : item.name;
if (item.type == itDirectory) if (item.type == itDirectory)
ClearDirectory(path); ClearDirectory(path);
@@ -561,7 +559,7 @@ void Browser::UpdateItemList()
{ {
for (size_t j = 0; j < myPlaylist->Items->Size(); ++j) for (size_t j = 0; j < myPlaylist->Items->Size(); ++j)
{ {
if (myPlaylist->Items->at(j).getHash() == w->at(i).song->getHash()) if (myPlaylist->Items->at(j).getHash() == w->at(i).song.getHash())
{ {
bold = 1; bold = 1;
break; break;
@@ -580,16 +578,14 @@ std::string Browser::ItemToString(const MPD::Item &item, void *)
{ {
case MPD::itDirectory: case MPD::itDirectory:
{ {
if (item.song)
return "[..]";
return "[" + ExtractTopName(item.name) + "]"; return "[" + ExtractTopName(item.name) + "]";
} }
case MPD::itSong: case MPD::itSong:
{ {
if (!Config.columns_in_browser) if (!Config.columns_in_browser)
return item.song->toString(Config.song_list_format_dollar_free); return item.song.toString(Config.song_list_format_dollar_free);
else else
return Playlist::SongInColumnsToString(*item.song, 0); return Playlist::SongInColumnsToString(item.song, 0);
} }
case MPD::itPlaylist: case MPD::itPlaylist:
{ {

View File

@@ -40,7 +40,7 @@ class Browser : public Screen< Menu<MPD::Item> >
virtual bool isTabbable() { return true; } virtual bool isTabbable() { return true; }
virtual MPD::Song *CurrentSong(); virtual MPD::Song *CurrentSong();
virtual MPD::Song *GetSong(size_t pos) { return w->at(pos).type == MPD::itSong ? (*w)[pos].song : 0; } virtual MPD::Song *GetSong(size_t pos) { return w->at(pos).type == MPD::itSong ? &(*w)[pos].song : 0; }
virtual bool allowsSelection() { return true; } virtual bool allowsSelection() { return true; }
virtual void ReverseSelection(); virtual void ReverseSelection();
@@ -65,6 +65,8 @@ class Browser : public Screen< Menu<MPD::Item> >
# endif // !WIN32 # endif // !WIN32
void UpdateItemList(); void UpdateItemList();
bool isParentDir(size_t pos) { return itsBrowsedDir != "/" && pos == 0; }
protected: protected:
virtual void Init(); virtual void Init();
virtual bool isLockable() { return true; } virtual bool isLockable() { return true; }

View File

@@ -360,7 +360,7 @@ void Display::Items(const MPD::Item &item, void *data, Menu<MPD::Item> *menu)
{ {
case MPD::itDirectory: case MPD::itDirectory:
{ {
if (item.song) if (!item.song.empty())
{ {
*menu << "[..]"; *menu << "[..]";
return; return;
@@ -370,9 +370,9 @@ void Display::Items(const MPD::Item &item, void *data, Menu<MPD::Item> *menu)
} }
case MPD::itSong: case MPD::itSong:
if (!Config.columns_in_browser) if (!Config.columns_in_browser)
Display::Songs(*item.song, data, reinterpret_cast<Menu<MPD::Song> *>(menu)); Display::Songs(item.song, data, reinterpret_cast<Menu<MPD::Song> *>(menu));
else else
Display::SongsInColumns(*item.song, data, reinterpret_cast<Menu<MPD::Song> *>(menu)); Display::SongsInColumns(item.song, data, reinterpret_cast<Menu<MPD::Song> *>(menu));
return; return;
case MPD::itPlaylist: case MPD::itPlaylist:
*menu << Config.browser_playlist_prefix << ExtractTopName(item.name); *menu << Config.browser_playlist_prefix << ExtractTopName(item.name);
@@ -382,17 +382,15 @@ void Display::Items(const MPD::Item &item, void *data, Menu<MPD::Item> *menu)
} }
} }
void Display::SearchEngine(const std::pair<Buffer *, MPD::Song *> &pair, void *data, Menu< std::pair<Buffer *, MPD::Song *> > *menu) void Display::SearchEngine(const SEItem &ei, void *data, Menu<SEItem> *menu)
{ {
if (pair.second) if (ei.isSong())
{ {
if (!Config.columns_in_search_engine) if (!Config.columns_in_search_engine)
Display::Songs(*pair.second, data, reinterpret_cast<Menu<MPD::Song> *>(menu)); Display::Songs(ei.song(), data, reinterpret_cast<Menu<MPD::Song> *>(menu));
else else
Display::SongsInColumns(*pair.second, data, reinterpret_cast<Menu<MPD::Song> *>(menu)); Display::SongsInColumns(ei.song(), data, reinterpret_cast<Menu<MPD::Song> *>(menu));
} }
else else
*menu << *pair.first; *menu << ei.buffer();
} }

View File

@@ -25,6 +25,7 @@
#include "menu.h" #include "menu.h"
#include "mpdpp.h" #include "mpdpp.h"
#include "screen.h" #include "screen.h"
#include "search_engine.h"
namespace Display namespace Display
{ {
@@ -52,7 +53,7 @@ namespace Display
void Tags(const MPD::Song &, void *, Menu<MPD::Song> *); void Tags(const MPD::Song &, void *, Menu<MPD::Song> *);
void SearchEngine(const std::pair<Buffer *, MPD::Song *> &, void *, Menu< std::pair<Buffer *, MPD::Song *> > *); void SearchEngine(const SEItem &, void *, Menu<SEItem> *);
void Items(const MPD::Item &, void *, Menu<MPD::Item> *); void Items(const MPD::Item &, void *, Menu<MPD::Item> *);
} }

View File

@@ -317,10 +317,10 @@ bool CaseInsensitiveSorting::operator()(const MPD::Item &a, const MPD::Item &b)
result = operator()(a.song, b.song); result = operator()(a.song, b.song);
break; break;
case smMTime: case smMTime:
result = a.song->getMTime() > b.song->getMTime(); result = a.song.getMTime() > b.song.getMTime();
break; break;
case smCustomFormat: case smCustomFormat:
result = cmp(a.song->toString(Config.browser_sort_format), b.song->toString(Config.browser_sort_format)) < 0; result = cmp(a.song.toString(Config.browser_sort_format), b.song.toString(Config.browser_sort_format)) < 0;
break; break;
} }
break; break;

View File

@@ -55,9 +55,9 @@ class CaseInsensitiveSorting
return cmp(a, b) < 0; return cmp(a, b) < 0;
} }
bool operator()(MPD::Song *a, MPD::Song *b) bool operator()(const MPD::Song &a, const MPD::Song &b)
{ {
return cmp(a->getName(), b->getName()) < 0; return cmp(a.getName(), b.getName()) < 0;
} }
template <typename A, typename B> bool operator()(const std::pair<A, B> &a, const std::pair<A, B> &b) template <typename A, typename B> bool operator()(const std::pair<A, B> &a, const std::pair<A, B> &b)

View File

@@ -337,23 +337,22 @@ void MediaLibrary::Update()
} }
Mpd.CommitSearch(list); Mpd.CommitSearch(list);
sort(list.begin(), list.end(), Albums->Current().Date == AllTracksMarker ? SortAllTracks : SortSongsByTrack); std::sort(list.begin(), list.end(), Albums->Current().Date == AllTracksMarker ? SortAllTracks : SortSongsByTrack);
bool bold = 0; bool bold = 0;
for (MPD::SongList::const_iterator it = list.begin(); it != list.end(); ++it) for (MPD::SongList::const_iterator it = list.begin(); it != list.end(); ++it)
{ {
for (size_t j = 0; j < myPlaylist->Items->Size(); ++j) for (size_t j = 0; j < myPlaylist->Items->Size(); ++j)
{ {
if ((*it)->getHash() == myPlaylist->Items->at(j).getHash()) if (it->getHash() == myPlaylist->Items->at(j).getHash())
{ {
bold = 1; bold = 1;
break; break;
} }
} }
Songs->AddOption(**it, bold); Songs->AddOption(*it, bold);
bold = 0; bold = 0;
} }
FreeSongList(list);
Songs->Window::Clear(); Songs->Window::Clear();
Songs->Refresh(); Songs->Refresh();
} }
@@ -489,16 +488,14 @@ void MediaLibrary::GetSelectedSongs(MPD::SongList &v)
Artists->GetSelected(selected); Artists->GetSelected(selected);
if (selected.empty()) if (selected.empty())
selected.push_back(Artists->Choice()); selected.push_back(Artists->Choice());
for (std::vector<size_t>::const_iterator it = selected.begin(); it != selected.end(); ++it) for (auto it = selected.begin(); it != selected.end(); ++it)
{ {
MPD::SongList list; MPD::SongList list;
Mpd.StartSearch(1); Mpd.StartSearch(1);
Mpd.AddSearch(Config.media_lib_primary_tag, locale_to_utf_cpy(Artists->at(*it))); Mpd.AddSearch(Config.media_lib_primary_tag, locale_to_utf_cpy(Artists->at(*it)));
Mpd.CommitSearch(list); Mpd.CommitSearch(list);
sort(list.begin(), list.end(), SortAllTracks); std::sort(list.begin(), list.end(), SortAllTracks);
for (MPD::SongList::const_iterator sIt = list.begin(); sIt != list.end(); ++sIt) std::copy(list.begin(), list.end(), std::back_inserter(v));
v.push_back(new MPD::Song(**sIt));
FreeSongList(list);
} }
} }
else if (w == Albums && !Albums->Empty()) else if (w == Albums && !Albums->Empty())
@@ -510,11 +507,11 @@ void MediaLibrary::GetSelectedSongs(MPD::SongList &v)
if (v.empty()) if (v.empty())
v.reserve(Songs->Size()); v.reserve(Songs->Size());
for (size_t i = 0; i < Songs->Size(); ++i) for (size_t i = 0; i < Songs->Size(); ++i)
v.push_back(new MPD::Song((*Songs)[i])); v.push_back((*Songs)[i]);
} }
else else
{ {
for (std::vector<size_t>::const_iterator it = selected.begin(); it != selected.end(); ++it) for (auto it = selected.begin(); it != selected.end(); ++it)
{ {
MPD::SongList list; MPD::SongList list;
Mpd.StartSearch(1); Mpd.StartSearch(1);
@@ -524,9 +521,7 @@ void MediaLibrary::GetSelectedSongs(MPD::SongList &v)
Mpd.AddSearch(MPD_TAG_ALBUM, Albums->at(*it).Album); Mpd.AddSearch(MPD_TAG_ALBUM, Albums->at(*it).Album);
Mpd.AddSearch(MPD_TAG_DATE, Albums->at(*it).Date); Mpd.AddSearch(MPD_TAG_DATE, Albums->at(*it).Date);
Mpd.CommitSearch(list); Mpd.CommitSearch(list);
for (MPD::SongList::const_iterator sIt = list.begin(); sIt != list.end(); ++sIt) std::copy(list.begin(), list.end(), std::back_inserter(v));
v.push_back(new MPD::Song(**sIt));
FreeSongList(list);
} }
} }
} }
@@ -535,8 +530,8 @@ void MediaLibrary::GetSelectedSongs(MPD::SongList &v)
Songs->GetSelected(selected); Songs->GetSelected(selected);
if (selected.empty()) if (selected.empty())
selected.push_back(Songs->Choice()); selected.push_back(Songs->Choice());
for (std::vector<size_t>::const_iterator it = selected.begin(); it != selected.end(); ++it) for (auto it = selected.begin(); it != selected.end(); ++it)
v.push_back(new MPD::Song(Songs->at(*it))); v.push_back(Songs->at(*it));
} }
} }
@@ -799,21 +794,21 @@ bool MediaLibrary::SearchConstraintsSorting::operator()(const SearchConstraints
return (result == 0 ? cmp(a.Album, b.Album) : result) < 0; return (result == 0 ? cmp(a.Album, b.Album) : result) < 0;
} }
bool MediaLibrary::SortSongsByTrack(MPD::Song *a, MPD::Song *b) bool MediaLibrary::SortSongsByTrack(const MPD::Song &a, const MPD::Song &b)
{ {
if (a->getDisc() == b->getDisc()) if (a.getDisc() == b.getDisc())
return StrToInt(a->getTrack()) < StrToInt(b->getTrack()); return StrToInt(a.getTrack()) < StrToInt(b.getTrack());
else else
return StrToInt(a->getDisc()) < StrToInt(b->getDisc()); return StrToInt(a.getDisc()) < StrToInt(b.getDisc());
} }
bool MediaLibrary::SortAllTracks(MPD::Song *a, MPD::Song *b) bool MediaLibrary::SortAllTracks(const MPD::Song &a, const MPD::Song &b)
{ {
static MPD::Song::GetFunction gets[] = { &MPD::Song::getDate, &MPD::Song::getAlbum, &MPD::Song::getDisc, 0 }; static MPD::Song::GetFunction gets[] = { &MPD::Song::getDate, &MPD::Song::getAlbum, &MPD::Song::getDisc, 0 };
CaseInsensitiveStringComparison cmp; CaseInsensitiveStringComparison cmp;
for (MPD::Song::GetFunction *get = gets; *get; ++get) for (MPD::Song::GetFunction *get = gets; *get; ++get)
if (int ret = cmp(a->getTags(*get), b->getTags(*get))) if (int ret = cmp(a.getTags(*get), b.getTags(*get)))
return ret < 0; return ret < 0;
return a->getTrack() < b->getTrack(); return a.getTrack() < b.getTrack();
} }

View File

@@ -93,8 +93,8 @@ class MediaLibrary : public Screen<Window>
static void DisplayAlbums(const SearchConstraints &, void *, Menu<SearchConstraints> *); static void DisplayAlbums(const SearchConstraints &, void *, Menu<SearchConstraints> *);
static void DisplayPrimaryTags(const std::string &artist, void *, Menu<std::string> *menu); static void DisplayPrimaryTags(const std::string &artist, void *, Menu<std::string> *menu);
static bool SortSongsByTrack(MPD::Song *, MPD::Song *); static bool SortSongsByTrack(const MPD::Song &, const MPD::Song &);
static bool SortAllTracks(MPD::Song *, MPD::Song *); static bool SortAllTracks(const MPD::Song &, const MPD::Song &);
static bool hasTwoColumns; static bool hasTwoColumns;
static size_t itsLeftColStartX; static size_t itsLeftColStartX;

View File

@@ -631,7 +631,7 @@ void MPD::Connection::GetPlaylistChanges(unsigned version, SongList &v)
GoBusy(); GoBusy();
mpd_send_queue_changes_meta(itsConnection, version); mpd_send_queue_changes_meta(itsConnection, version);
while (mpd_song *s = mpd_recv_song(itsConnection)) while (mpd_song *s = mpd_recv_song(itsConnection))
v.push_back(new Song(s)); v.push_back(Song(s));
mpd_response_finish(itsConnection); mpd_response_finish(itsConnection);
GoIdle(); GoIdle();
} }
@@ -677,7 +677,7 @@ void MPD::Connection::GetPlaylistContent(const std::string &path, SongList &v)
GoBusy(); GoBusy();
mpd_send_list_playlist_meta(itsConnection, path.c_str()); mpd_send_list_playlist_meta(itsConnection, path.c_str());
while (mpd_song *s = mpd_recv_song(itsConnection)) while (mpd_song *s = mpd_recv_song(itsConnection))
v.push_back(new Song(s)); v.push_back(Song(s));
mpd_response_finish(itsConnection); mpd_response_finish(itsConnection);
GoIdle(); GoIdle();
} }
@@ -925,8 +925,8 @@ bool MPD::Connection::AddRandomTag(mpd_tag_type tag, size_t number)
SongList list; SongList list;
CommitSearch(list); CommitSearch(list);
StartCommandsList(); StartCommandsList();
for (SongList::const_iterator j = list.begin(); j != list.end(); ++j) for (auto j = list.begin(); j != list.end(); ++j)
AddSong(**j); AddSong(*j);
CommitCommandsList(); CommitCommandsList();
} }
} }
@@ -1084,7 +1084,6 @@ void MPD::Connection::GetPlaylists(TagList &v)
for (ItemList::const_iterator it = list.begin(); it != list.end(); ++it) for (ItemList::const_iterator it = list.begin(); it != list.end(); ++it)
if (it->type == itPlaylist) if (it->type == itPlaylist)
v.push_back(it->name); v.push_back(it->name);
FreeItemList(list);
} }
void MPD::Connection::GetList(TagList &v, mpd_tag_type type) void MPD::Connection::GetList(TagList &v, mpd_tag_type type)
@@ -1150,7 +1149,7 @@ void MPD::Connection::CommitSearch(SongList &v)
GoBusy(); GoBusy();
mpd_search_commit(itsConnection); mpd_search_commit(itsConnection);
while (mpd_song *s = mpd_recv_song(itsConnection)) while (mpd_song *s = mpd_recv_song(itsConnection))
v.push_back(new Song(s)); v.push_back(Song(s));
mpd_response_finish(itsConnection); mpd_response_finish(itsConnection);
GoIdle(); GoIdle();
} }
@@ -1188,7 +1187,7 @@ void MPD::Connection::GetDirectory(const std::string &path, ItemList &v)
it.type = itDirectory; it.type = itDirectory;
goto WRITE; goto WRITE;
case MPD_ENTITY_TYPE_SONG: case MPD_ENTITY_TYPE_SONG:
it.song = new Song(mpd_song_dup(mpd_entity_get_song(item))); it.song = Song(mpd_song_dup(mpd_entity_get_song(item)));
it.type = itSong; it.type = itSong;
goto WRITE; goto WRITE;
case MPD_ENTITY_TYPE_PLAYLIST: case MPD_ENTITY_TYPE_PLAYLIST:
@@ -1215,7 +1214,7 @@ void MPD::Connection::GetDirectoryRecursive(const std::string &path, SongList &v
GoBusy(); GoBusy();
mpd_send_list_all_meta(itsConnection, path.c_str()); mpd_send_list_all_meta(itsConnection, path.c_str());
while (mpd_song *s = mpd_recv_song(itsConnection)) while (mpd_song *s = mpd_recv_song(itsConnection))
v.push_back(new Song(s)); v.push_back(Song(s));
mpd_response_finish(itsConnection); mpd_response_finish(itsConnection);
GoIdle(); GoIdle();
} }
@@ -1228,7 +1227,7 @@ void MPD::Connection::GetSongs(const std::string &path, SongList &v)
GoBusy(); GoBusy();
mpd_send_list_meta(itsConnection, path.c_str()); mpd_send_list_meta(itsConnection, path.c_str());
while (mpd_song *s = mpd_recv_song(itsConnection)) while (mpd_song *s = mpd_recv_song(itsConnection))
v.push_back(new Song(s)); v.push_back(Song(s));
mpd_response_finish(itsConnection); mpd_response_finish(itsConnection);
GoIdle(); GoIdle();
} }
@@ -1351,18 +1350,3 @@ int MPD::Connection::CheckForErrors()
} }
return error_code; return error_code;
} }
void MPD::FreeSongList(SongList &l)
{
for (SongList::iterator i = l.begin(); i != l.end(); ++i)
delete *i;
l.clear();
}
void MPD::FreeItemList(ItemList &l)
{
for (ItemList::iterator i = l.begin(); i != l.end(); ++i)
delete i->song;
l.clear();
}

View File

@@ -29,20 +29,13 @@
namespace MPD namespace MPD
{ {
namespace Message
{
extern const char *PartOfSongsAdded;
extern const char *FullPlaylist;
}
enum ItemType { itDirectory, itSong, itPlaylist }; enum ItemType { itDirectory, itSong, itPlaylist };
enum PlayerState { psUnknown, psStop, psPlay, psPause }; enum PlayerState { psUnknown, psStop, psPlay, psPause };
enum ReplayGainMode { rgmOff, rgmTrack, rgmAlbum }; enum ReplayGainMode { rgmOff, rgmTrack, rgmAlbum };
struct Item struct Item
{ {
Item() : song(0) { } Song song;
Song *song;
ItemType type; ItemType type;
std::string name; std::string name;
}; };
@@ -69,13 +62,10 @@ namespace MPD
typedef std::pair<std::string, bool> Output; typedef std::pair<std::string, bool> Output;
typedef std::vector<Item> ItemList; typedef std::vector<Item> ItemList;
typedef std::vector<Song *> SongList; typedef std::vector<Song> SongList;
typedef std::vector<std::string> TagList; typedef std::vector<std::string> TagList;
typedef std::vector<Output> OutputList; typedef std::vector<Output> OutputList;
void FreeSongList(SongList &);
void FreeItemList(ItemList &);
class Connection class Connection
{ {
typedef void (*StatusUpdater) (Connection *, StatusChanges, void *); typedef void (*StatusUpdater) (Connection *, StatusChanges, void *);

View File

@@ -185,15 +185,15 @@ void Playlist::EnterPressed()
MPD::SongList playlist; MPD::SongList playlist;
playlist.reserve(end-beginning); playlist.reserve(end-beginning);
for (size_t i = beginning; i < end; ++i) for (size_t i = beginning; i < end; ++i)
playlist.push_back(&(*Items)[i]); playlist.push_back((*Items)[i]);
std::function<void(MPD::SongList::iterator, MPD::SongList::iterator)> iter_swap, quick_sort; std::function<void(MPD::SongList::iterator, MPD::SongList::iterator)> iter_swap, quick_sort;
auto song_cmp = [](MPD::Song *a, MPD::Song *b) { auto song_cmp = [](const MPD::Song &a, const MPD::Song &b) {
CaseInsensitiveStringComparison cmp; CaseInsensitiveStringComparison cmp;
for (size_t i = 0; i < SortOptions; ++i) for (size_t i = 0; i < SortOptions; ++i)
if (int ret = cmp(a->getTags((*SortDialog)[i].second), b->getTags((*SortDialog)[i].second))) if (int ret = cmp(a.getTags((*SortDialog)[i].second), b.getTags((*SortDialog)[i].second)))
return ret < 0; return ret < 0;
return a->getPosition() < b->getPosition(); return a.getPosition() < b.getPosition();
}; };
iter_swap = [&playlist](MPD::SongList::iterator a, MPD::SongList::iterator b) { iter_swap = [&playlist](MPD::SongList::iterator a, MPD::SongList::iterator b) {
std::iter_swap(a, b); std::iter_swap(a, b);
@@ -274,8 +274,8 @@ void Playlist::GetSelectedSongs(MPD::SongList &v)
Items->GetSelected(selected); Items->GetSelected(selected);
if (selected.empty()) if (selected.empty())
selected.push_back(Items->Choice()); selected.push_back(Items->Choice());
for (std::vector<size_t>::const_iterator it = selected.begin(); it != selected.end(); ++it) for (auto it = selected.begin(); it != selected.end(); ++it)
v.push_back(new MPD::Song(Items->at(*it))); v.push_back(Items->at(*it));
} }
void Playlist::ApplyFilter(const std::string &s) void Playlist::ApplyFilter(const std::string &s)
@@ -562,18 +562,16 @@ bool Playlist::Add(const MPD::SongList &l, bool play, int position)
return false; return false;
Mpd.StartCommandsList(); Mpd.StartCommandsList();
MPD::SongList::const_iterator it = l.begin();
if (position < 0) if (position < 0)
{ {
for (; it != l.end(); ++it) for (auto it = l.begin(); it != l.end(); ++it)
if (Mpd.AddSong(**it) < 0) if (Mpd.AddSong(*it) < 0)
break; break;
} }
else else
{ {
MPD::SongList::const_reverse_iterator j = l.rbegin(); for (auto j = l.rbegin(); j != l.rend(); ++j)
for (; j != l.rend(); ++j) if (Mpd.AddSong(*j, position) < 0)
if (Mpd.AddSong(**j, position) < 0)
break; break;
} }
if (!Mpd.CommitCommandsList()) if (!Mpd.CommitCommandsList())

View File

@@ -157,20 +157,19 @@ void PlaylistEditor::Update()
else else
Content->SetTitle(Config.titles_visibility ? "Playlist's content" : ""); Content->SetTitle(Config.titles_visibility ? "Playlist's content" : "");
bool bold = 0; bool bold = 0;
for (MPD::SongList::const_iterator it = list.begin(); it != list.end(); ++it) for (auto it = list.begin(); it != list.end(); ++it)
{ {
for (size_t j = 0; j < myPlaylist->Items->Size(); ++j) for (size_t j = 0; j < myPlaylist->Items->Size(); ++j)
{ {
if ((*it)->getHash() == myPlaylist->Items->at(j).getHash()) if (it->getHash() == myPlaylist->Items->at(j).getHash())
{ {
bold = 1; bold = 1;
break; break;
} }
} }
Content->AddOption(**it, bold); Content->AddOption(*it, bold);
bold = 0; bold = 0;
} }
MPD::FreeSongList(list);
Content->Window::Clear(); Content->Window::Clear();
Content->Display(); Content->Display();
} }
@@ -346,7 +345,6 @@ void PlaylistEditor::AddToPlaylist(bool add_n_play)
else if (w == Content && !Content->Empty()) else if (w == Content && !Content->Empty())
Content->Bold(Content->Choice(), myPlaylist->Add(Content->Current(), Content->isBold(), add_n_play)); Content->Bold(Content->Choice(), myPlaylist->Add(Content->Current(), Content->isBold(), add_n_play));
FreeSongList(list);
if (!add_n_play) if (!add_n_play)
w->Scroll(wDown); w->Scroll(wDown);
} }
@@ -416,8 +414,8 @@ void PlaylistEditor::GetSelectedSongs(MPD::SongList &v)
Content->GetSelected(selected); Content->GetSelected(selected);
if (selected.empty()) if (selected.empty())
selected.push_back(Content->Choice()); selected.push_back(Content->Choice());
for (std::vector<size_t>::const_iterator it = selected.begin(); it != selected.end(); ++it) for (auto it = selected.begin(); it != selected.end(); ++it)
v.push_back(new MPD::Song(Content->at(*it))); v.push_back(Content->at(*it));
} }
void PlaylistEditor::ApplyFilter(const std::string &s) void PlaylistEditor::ApplyFilter(const std::string &s)

View File

@@ -64,7 +64,7 @@ void SearchEngine::Init()
{ {
static Display::ScreenFormat sf = { this, &Config.song_list_format }; static Display::ScreenFormat sf = { this, &Config.song_list_format };
w = new Menu< std::pair<Buffer *, MPD::Song *> >(0, MainStartY, COLS, MainHeight, "", Config.main_color, brNone); w = new Menu<SEItem>(0, MainStartY, COLS, MainHeight, "", Config.main_color, brNone);
w->HighlightColor(Config.main_highlight_color); w->HighlightColor(Config.main_highlight_color);
w->CyclicScrolling(Config.use_cyclic_scrolling); w->CyclicScrolling(Config.use_cyclic_scrolling);
w->CenteredCursor(Config.centered_cursor); w->CenteredCursor(Config.centered_cursor);
@@ -115,7 +115,7 @@ void SearchEngine::SwitchTo()
myScreen = this; myScreen = this;
Global::RedrawHeader = true; Global::RedrawHeader = true;
if (!w->Back().first) if (!w->Back().isSong())
{ {
*w << XY(0, 0) << "Updating list..."; *w << XY(0, 0) << "Updating list...";
UpdateFoundList(); UpdateFoundList();
@@ -131,7 +131,7 @@ void SearchEngine::EnterPressed()
{ {
size_t option = w->Choice(); size_t option = w->Choice();
if (option > ConstraintsNumber && option < SearchButton) if (option > ConstraintsNumber && option < SearchButton)
w->Current().first->Clear(); w->Current().buffer().Clear();
if (option < SearchButton) if (option < SearchButton)
LockStatusbar(); LockStatusbar();
@@ -139,20 +139,20 @@ void SearchEngine::EnterPressed()
{ {
Statusbar() << fmtBold << ConstraintsNames[option] << fmtBoldEnd << ": "; Statusbar() << fmtBold << ConstraintsNames[option] << fmtBoldEnd << ": ";
itsConstraints[option] = Global::wFooter->GetString(itsConstraints[option]); itsConstraints[option] = Global::wFooter->GetString(itsConstraints[option]);
w->Current().first->Clear(); w->Current().buffer().Clear();
*w->Current().first << fmtBold << std::setw(13) << std::left << ConstraintsNames[option] << fmtBoldEnd << ": "; w->Current().buffer() << fmtBold << std::setw(13) << std::left << ConstraintsNames[option] << fmtBoldEnd << ": ";
ShowTag(*w->Current().first, itsConstraints[option]); ShowTag(w->Current().buffer(), itsConstraints[option]);
} }
else if (option == ConstraintsNumber+1) else if (option == ConstraintsNumber+1)
{ {
Config.search_in_db = !Config.search_in_db; Config.search_in_db = !Config.search_in_db;
*w->Current().first << fmtBold << "Search in:" << fmtBoldEnd << ' ' << (Config.search_in_db ? "Database" : "Current playlist"); w->Current().buffer() << fmtBold << "Search in:" << fmtBoldEnd << ' ' << (Config.search_in_db ? "Database" : "Current playlist");
} }
else if (option == ConstraintsNumber+2) else if (option == ConstraintsNumber+2)
{ {
if (!*++SearchMode) if (!*++SearchMode)
SearchMode = &SearchModes[0]; SearchMode = &SearchModes[0];
*w->Current().first << fmtBold << "Search mode:" << fmtBoldEnd << ' ' << *SearchMode; w->Current().buffer() << fmtBold << "Search mode:" << fmtBoldEnd << ' ' << *SearchMode;
} }
else if (option == SearchButton) else if (option == SearchButton)
{ {
@@ -160,16 +160,15 @@ void SearchEngine::EnterPressed()
if (w->Size() > StaticOptions) if (w->Size() > StaticOptions)
Prepare(); Prepare();
Search(); Search();
if (!w->Back().first) if (w->Back().isSong())
{ {
if (Config.columns_in_search_engine) if (Config.columns_in_search_engine)
w->SetTitle(Config.titles_visibility ? Display::Columns(w->GetWidth()) : ""); w->SetTitle(Config.titles_visibility ? Display::Columns(w->GetWidth()) : "");
size_t found = w->Size()-SearchEngine::StaticOptions; size_t found = w->Size()-SearchEngine::StaticOptions;
found += 3; // don't count options inserted below found += 3; // don't count options inserted below
w->InsertSeparator(ResetButton+1); w->InsertSeparator(ResetButton+1);
w->InsertOption(ResetButton+2, std::make_pair(static_cast<Buffer *>(0), static_cast<MPD::Song *>(0)), 1, 1); w->InsertOption(ResetButton+2, SEItem(), 1, 1);
w->at(ResetButton+2).first = new Buffer(); w->at(ResetButton+2).mkBuffer() << Config.color1 << "Search results: " << Config.color2 << "Found " << found << (found > 1 ? " songs" : " song") << clDefault;
*w->at(ResetButton+2).first << Config.color1 << "Search results: " << Config.color2 << "Found " << found << (found > 1 ? " songs" : " song") << clDefault;
w->InsertSeparator(ResetButton+3); w->InsertSeparator(ResetButton+3);
UpdateFoundList(); UpdateFoundList();
ShowMessage("Searching finished"); ShowMessage("Searching finished");
@@ -187,7 +186,10 @@ void SearchEngine::EnterPressed()
Reset(); Reset();
} }
else else
w->Bold(w->Choice(), myPlaylist->Add(*w->Current().second, w->isBold(), 1)); {
bool res = myPlaylist->Add(w->Current().song(), 1, 1);
w->Bold(w->Choice(), res);
}
if (option < SearchButton) if (option < SearchButton)
UnlockStatusbar(); UnlockStatusbar();
@@ -195,7 +197,7 @@ void SearchEngine::EnterPressed()
void SearchEngine::SpacePressed() void SearchEngine::SpacePressed()
{ {
if (w->Current().first) if (!w->Current().isSong())
return; return;
if (Config.space_selects) if (Config.space_selects)
@@ -205,7 +207,8 @@ void SearchEngine::SpacePressed()
return; return;
} }
w->Bold(w->Choice(), myPlaylist->Add(*w->Current().second, w->isBold(), 0)); bool res = myPlaylist->Add(w->Current().song(), 0, 0);
w->Bold(w->Choice(), res);
w->Scroll(wDown); w->Scroll(wDown);
} }
@@ -234,12 +237,12 @@ void SearchEngine::MouseButtonPressed(MEVENT me)
} }
} }
else else
Screen< Menu< std::pair<Buffer *, MPD::Song *> > >::MouseButtonPressed(me); Screen< Menu<SEItem> >::MouseButtonPressed(me);
} }
MPD::Song *SearchEngine::CurrentSong() MPD::Song *SearchEngine::CurrentSong()
{ {
return !w->Empty() ? w->Current().second : 0; return !w->Empty() && w->Current().isSong() ? &w->Current().song() : 0;
} }
void SearchEngine::GetSelectedSongs(MPD::SongList &v) void SearchEngine::GetSelectedSongs(MPD::SongList &v)
@@ -250,8 +253,11 @@ void SearchEngine::GetSelectedSongs(MPD::SongList &v)
w->GetSelected(selected); w->GetSelected(selected);
if (selected.empty() && w->Choice() >= StaticOptions) if (selected.empty() && w->Choice() >= StaticOptions)
selected.push_back(w->Choice()); selected.push_back(w->Choice());
for (std::vector<size_t>::const_iterator it = selected.begin(); it != selected.end(); ++it) for (auto it = selected.begin(); it != selected.end(); ++it)
v.push_back(new MPD::Song(*w->at(*it).second)); {
assert(w->at(*it).isSong());
v.push_back(w->at(*it).song());
}
} }
void SearchEngine::ApplyFilter(const std::string &s) void SearchEngine::ApplyFilter(const std::string &s)
@@ -266,7 +272,7 @@ void SearchEngine::UpdateFoundList()
{ {
for (size_t j = 0; j < myPlaylist->Items->Size(); ++j) for (size_t j = 0; j < myPlaylist->Items->Size(); ++j)
{ {
if (myPlaylist->Items->at(j).getHash() == w->at(i).second->getHash()) if (myPlaylist->Items->at(j).getHash() == w->at(i).song().getHash())
{ {
bold = 1; bold = 1;
break; break;
@@ -283,7 +289,7 @@ void SearchEngine::SelectAlbum()
if (pos < StaticOptions) if (pos < StaticOptions)
return; // not on a song return; // not on a song
std::string album = w->at(pos).second->getAlbum(); std::string album = w->at(pos).song().getAlbum();
// select song under cursor // select song under cursor
w->Select(pos, 1); w->Select(pos, 1);
@@ -291,7 +297,7 @@ void SearchEngine::SelectAlbum()
// go up // go up
while (pos > StaticOptions) while (pos > StaticOptions)
{ {
if (w->at(--pos).second->getAlbum() != album) if (w->at(--pos).song().getAlbum() != album)
break; break;
else else
w->Select(pos, 1); w->Select(pos, 1);
@@ -300,7 +306,7 @@ void SearchEngine::SelectAlbum()
// go down // go down
while (pos < w->Size() - 1) while (pos < w->Size() - 1)
{ {
if (w->at(++pos).second->getAlbum() != album) if (w->at(++pos).song().getAlbum() != album)
break; break;
else else
w->Select(pos, 1); w->Select(pos, 1);
@@ -309,14 +315,6 @@ void SearchEngine::SelectAlbum()
void SearchEngine::Prepare() void SearchEngine::Prepare()
{ {
for (size_t i = 0; i < w->Size(); ++i)
{
if (i == ConstraintsNumber || i == SearchButton-1 || i == ResetButton+1 || i == ResetButton+3) // separators
continue;
delete (*w)[i].first;
delete (*w)[i].second;
}
w->SetTitle(""); w->SetTitle("");
w->Clear(); w->Clear();
w->ResizeList(StaticOptions-3); w->ResizeList(StaticOptions-3);
@@ -324,24 +322,17 @@ void SearchEngine::Prepare()
w->IntoSeparator(ConstraintsNumber); w->IntoSeparator(ConstraintsNumber);
w->IntoSeparator(SearchButton-1); w->IntoSeparator(SearchButton-1);
for (size_t i = 0; i < StaticOptions-3; ++i)
{
if (i == ConstraintsNumber || i == SearchButton-1) // separators
continue;
(*w)[i].first = new Buffer();
}
for (size_t i = 0; i < ConstraintsNumber; ++i) for (size_t i = 0; i < ConstraintsNumber; ++i)
{ {
*(*w)[i].first << fmtBold << std::setw(13) << std::left << ConstraintsNames[i] << fmtBoldEnd << ": "; (*w)[i].mkBuffer() << fmtBold << std::setw(13) << std::left << ConstraintsNames[i] << fmtBoldEnd << ": ";
ShowTag(*(*w)[i].first, itsConstraints[i]); ShowTag((*w)[i].buffer(), itsConstraints[i]);
} }
*w->at(ConstraintsNumber+1).first << fmtBold << "Search in:" << fmtBoldEnd << ' ' << (Config.search_in_db ? "Database" : "Current playlist"); w->at(ConstraintsNumber+1).mkBuffer() << fmtBold << "Search in:" << fmtBoldEnd << ' ' << (Config.search_in_db ? "Database" : "Current playlist");
*w->at(ConstraintsNumber+2).first << fmtBold << "Search mode:" << fmtBoldEnd << ' ' << *SearchMode; w->at(ConstraintsNumber+2).mkBuffer() << fmtBold << "Search mode:" << fmtBoldEnd << ' ' << *SearchMode;
*w->at(SearchButton).first << "Search"; w->at(SearchButton).mkBuffer() << "Search";
*w->at(ResetButton).first << "Reset"; w->at(ResetButton).mkBuffer() << "Reset";
} }
void SearchEngine::Reset() void SearchEngine::Reset()
@@ -394,8 +385,8 @@ void SearchEngine::Search()
Mpd.AddSearch(MPD_TAG_COMMENT, itsConstraints[10]); Mpd.AddSearch(MPD_TAG_COMMENT, itsConstraints[10]);
MPD::SongList results; MPD::SongList results;
Mpd.CommitSearch(results); Mpd.CommitSearch(results);
for (MPD::SongList::const_iterator it = results.begin(); it != results.end(); ++it) for (auto it = results.begin(); it != results.end(); ++it)
w->AddOption(std::make_pair(static_cast<Buffer *>(0), *it)); w->AddOption(*it);
return; return;
} }
@@ -406,7 +397,7 @@ void SearchEngine::Search()
{ {
list.reserve(myPlaylist->Items->Size()); list.reserve(myPlaylist->Items->Size());
for (size_t i = 0; i < myPlaylist->Items->Size(); ++i) for (size_t i = 0; i < myPlaylist->Items->Size(); ++i)
list.push_back(&(*myPlaylist->Items)[i]); list.push_back((*myPlaylist->Items)[i]);
} }
bool any_found = 1; bool any_found = 1;
@@ -422,16 +413,16 @@ void SearchEngine::Search()
if (regcomp(&rx, itsConstraints[0].c_str(), REG_ICASE | Config.regex_type) == 0) if (regcomp(&rx, itsConstraints[0].c_str(), REG_ICASE | Config.regex_type) == 0)
{ {
any_found = any_found =
!regexec(&rx, (*it)->getArtist().c_str(), 0, 0, 0) !regexec(&rx, it->getArtist().c_str(), 0, 0, 0)
|| !regexec(&rx, (*it)->getAlbumArtist().c_str(), 0, 0, 0) || !regexec(&rx, it->getAlbumArtist().c_str(), 0, 0, 0)
|| !regexec(&rx, (*it)->getTitle().c_str(), 0, 0, 0) || !regexec(&rx, it->getTitle().c_str(), 0, 0, 0)
|| !regexec(&rx, (*it)->getAlbum().c_str(), 0, 0, 0) || !regexec(&rx, it->getAlbum().c_str(), 0, 0, 0)
|| !regexec(&rx, (*it)->getName().c_str(), 0, 0, 0) || !regexec(&rx, it->getName().c_str(), 0, 0, 0)
|| !regexec(&rx, (*it)->getComposer().c_str(), 0, 0, 0) || !regexec(&rx, it->getComposer().c_str(), 0, 0, 0)
|| !regexec(&rx, (*it)->getPerformer().c_str(), 0, 0, 0) || !regexec(&rx, it->getPerformer().c_str(), 0, 0, 0)
|| !regexec(&rx, (*it)->getGenre().c_str(), 0, 0, 0) || !regexec(&rx, it->getGenre().c_str(), 0, 0, 0)
|| !regexec(&rx, (*it)->getDate().c_str(), 0, 0, 0) || !regexec(&rx, it->getDate().c_str(), 0, 0, 0)
|| !regexec(&rx, (*it)->getComment().c_str(), 0, 0, 0); || !regexec(&rx, it->getComment().c_str(), 0, 0, 0);
} }
regfree(&rx); regfree(&rx);
} }
@@ -439,61 +430,61 @@ void SearchEngine::Search()
if (found && !itsConstraints[1].empty()) if (found && !itsConstraints[1].empty())
{ {
if (!regcomp(&rx, itsConstraints[1].c_str(), REG_ICASE | Config.regex_type)) if (!regcomp(&rx, itsConstraints[1].c_str(), REG_ICASE | Config.regex_type))
found = !regexec(&rx, (*it)->getArtist().c_str(), 0, 0, 0); found = !regexec(&rx, it->getArtist().c_str(), 0, 0, 0);
regfree(&rx); regfree(&rx);
} }
if (found && !itsConstraints[2].empty()) if (found && !itsConstraints[2].empty())
{ {
if (!regcomp(&rx, itsConstraints[2].c_str(), REG_ICASE | Config.regex_type)) if (!regcomp(&rx, itsConstraints[2].c_str(), REG_ICASE | Config.regex_type))
found = !regexec(&rx, (*it)->getAlbumArtist().c_str(), 0, 0, 0); found = !regexec(&rx, it->getAlbumArtist().c_str(), 0, 0, 0);
regfree(&rx); regfree(&rx);
} }
if (found && !itsConstraints[3].empty()) if (found && !itsConstraints[3].empty())
{ {
if(!regcomp(&rx, itsConstraints[3].c_str(), REG_ICASE | Config.regex_type)) if(!regcomp(&rx, itsConstraints[3].c_str(), REG_ICASE | Config.regex_type))
found = !regexec(&rx, (*it)->getTitle().c_str(), 0, 0, 0); found = !regexec(&rx, it->getTitle().c_str(), 0, 0, 0);
regfree(&rx); regfree(&rx);
} }
if (found && !itsConstraints[4].empty()) if (found && !itsConstraints[4].empty())
{ {
if (!regcomp(&rx, itsConstraints[4].c_str(), REG_ICASE | Config.regex_type)) if (!regcomp(&rx, itsConstraints[4].c_str(), REG_ICASE | Config.regex_type))
found = !regexec(&rx, (*it)->getAlbum().c_str(), 0, 0, 0); found = !regexec(&rx, it->getAlbum().c_str(), 0, 0, 0);
regfree(&rx); regfree(&rx);
} }
if (found && !itsConstraints[5].empty()) if (found && !itsConstraints[5].empty())
{ {
if (!regcomp(&rx, itsConstraints[5].c_str(), REG_ICASE | Config.regex_type)) if (!regcomp(&rx, itsConstraints[5].c_str(), REG_ICASE | Config.regex_type))
found = !regexec(&rx, (*it)->getName().c_str(), 0, 0, 0); found = !regexec(&rx, it->getName().c_str(), 0, 0, 0);
regfree(&rx); regfree(&rx);
} }
if (found && !itsConstraints[6].empty()) if (found && !itsConstraints[6].empty())
{ {
if (!regcomp(&rx, itsConstraints[6].c_str(), REG_ICASE | Config.regex_type)) if (!regcomp(&rx, itsConstraints[6].c_str(), REG_ICASE | Config.regex_type))
found = !regexec(&rx, (*it)->getComposer().c_str(), 0, 0, 0); found = !regexec(&rx, it->getComposer().c_str(), 0, 0, 0);
regfree(&rx); regfree(&rx);
} }
if (found && !itsConstraints[7].empty()) if (found && !itsConstraints[7].empty())
{ {
if (!regcomp(&rx, itsConstraints[7].c_str(), REG_ICASE | Config.regex_type)) if (!regcomp(&rx, itsConstraints[7].c_str(), REG_ICASE | Config.regex_type))
found = !regexec(&rx, (*it)->getPerformer().c_str(), 0, 0, 0); found = !regexec(&rx, it->getPerformer().c_str(), 0, 0, 0);
regfree(&rx); regfree(&rx);
} }
if (found && !itsConstraints[8].empty()) if (found && !itsConstraints[8].empty())
{ {
if (!regcomp(&rx, itsConstraints[8].c_str(), REG_ICASE | Config.regex_type)) if (!regcomp(&rx, itsConstraints[8].c_str(), REG_ICASE | Config.regex_type))
found = !regexec(&rx, (*it)->getGenre().c_str(), 0, 0, 0); found = !regexec(&rx, it->getGenre().c_str(), 0, 0, 0);
regfree(&rx); regfree(&rx);
} }
if (found && !itsConstraints[9].empty()) if (found && !itsConstraints[9].empty())
{ {
if (!regcomp(&rx, itsConstraints[9].c_str(), REG_ICASE | Config.regex_type)) if (!regcomp(&rx, itsConstraints[9].c_str(), REG_ICASE | Config.regex_type))
found = !regexec(&rx, (*it)->getDate().c_str(), 0, 0, 0); found = !regexec(&rx, it->getDate().c_str(), 0, 0, 0);
regfree(&rx); regfree(&rx);
} }
if (found && !itsConstraints[10].empty()) if (found && !itsConstraints[10].empty())
{ {
if (!regcomp(&rx, itsConstraints[10].c_str(), REG_ICASE | Config.regex_type)) if (!regcomp(&rx, itsConstraints[10].c_str(), REG_ICASE | Config.regex_type))
found = !regexec(&rx, (*it)->getComment().c_str(), 0, 0, 0); found = !regexec(&rx, it->getComment().c_str(), 0, 0, 0);
regfree(&rx); regfree(&rx);
} }
} }
@@ -503,60 +494,57 @@ void SearchEngine::Search()
if (!itsConstraints[0].empty()) if (!itsConstraints[0].empty())
any_found = any_found =
!cmp((*it)->getArtist(), itsConstraints[0]) !cmp(it->getArtist(), itsConstraints[0])
|| !cmp((*it)->getAlbumArtist(), itsConstraints[0]) || !cmp(it->getAlbumArtist(), itsConstraints[0])
|| !cmp((*it)->getTitle(), itsConstraints[0]) || !cmp(it->getTitle(), itsConstraints[0])
|| !cmp((*it)->getAlbum(), itsConstraints[0]) || !cmp(it->getAlbum(), itsConstraints[0])
|| !cmp((*it)->getName(), itsConstraints[0]) || !cmp(it->getName(), itsConstraints[0])
|| !cmp((*it)->getComposer(), itsConstraints[0]) || !cmp(it->getComposer(), itsConstraints[0])
|| !cmp((*it)->getPerformer(), itsConstraints[0]) || !cmp(it->getPerformer(), itsConstraints[0])
|| !cmp((*it)->getGenre(), itsConstraints[0]) || !cmp(it->getGenre(), itsConstraints[0])
|| !cmp((*it)->getDate(), itsConstraints[0]) || !cmp(it->getDate(), itsConstraints[0])
|| !cmp((*it)->getComment(), itsConstraints[0]); || !cmp(it->getComment(), itsConstraints[0]);
if (found && !itsConstraints[1].empty()) if (found && !itsConstraints[1].empty())
found = !cmp((*it)->getArtist(), itsConstraints[1]); found = !cmp(it->getArtist(), itsConstraints[1]);
if (found && !itsConstraints[2].empty()) if (found && !itsConstraints[2].empty())
found = !cmp((*it)->getAlbumArtist(), itsConstraints[2]); found = !cmp(it->getAlbumArtist(), itsConstraints[2]);
if (found && !itsConstraints[3].empty()) if (found && !itsConstraints[3].empty())
found = !cmp((*it)->getTitle(), itsConstraints[3]); found = !cmp(it->getTitle(), itsConstraints[3]);
if (found && !itsConstraints[4].empty()) if (found && !itsConstraints[4].empty())
found = !cmp((*it)->getAlbum(), itsConstraints[4]); found = !cmp(it->getAlbum(), itsConstraints[4]);
if (found && !itsConstraints[5].empty()) if (found && !itsConstraints[5].empty())
found = !cmp((*it)->getName(), itsConstraints[5]); found = !cmp(it->getName(), itsConstraints[5]);
if (found && !itsConstraints[6].empty()) if (found && !itsConstraints[6].empty())
found = !cmp((*it)->getComposer(), itsConstraints[6]); found = !cmp(it->getComposer(), itsConstraints[6]);
if (found && !itsConstraints[7].empty()) if (found && !itsConstraints[7].empty())
found = !cmp((*it)->getPerformer(), itsConstraints[7]); found = !cmp(it->getPerformer(), itsConstraints[7]);
if (found && !itsConstraints[8].empty()) if (found && !itsConstraints[8].empty())
found = !cmp((*it)->getGenre(), itsConstraints[8]); found = !cmp(it->getGenre(), itsConstraints[8]);
if (found && !itsConstraints[9].empty()) if (found && !itsConstraints[9].empty())
found = !cmp((*it)->getDate(), itsConstraints[9]); found = !cmp(it->getDate(), itsConstraints[9]);
if (found && !itsConstraints[10].empty()) if (found && !itsConstraints[10].empty())
found = !cmp((*it)->getComment(), itsConstraints[10]); found = !cmp(it->getComment(), itsConstraints[10]);
} }
if (found && any_found) if (found && any_found)
{ {
MPD::Song *ss = Config.search_in_db ? *it : new MPD::Song(**it); w->AddOption(*it);
w->AddOption(std::make_pair(static_cast<Buffer *>(0), ss));
list[it-list.begin()] = 0; list[it-list.begin()] = 0;
} }
found = 1; found = 1;
any_found = 1; any_found = 1;
} }
if (Config.search_in_db) // free song list only if it's database
MPD::FreeSongList(list);
} }
std::string SearchEngine::SearchEngineOptionToString(const std::pair<Buffer *, MPD::Song *> &pair, void *) std::string SearchEngine::SearchEngineOptionToString(const SEItem &ei, void *)
{ {
if (pair.second) if (!ei.isSong())
{ {
if (!Config.columns_in_search_engine) if (!Config.columns_in_search_engine)
return pair.second->toString(Config.song_list_format_dollar_free); return ei.song().toString(Config.song_list_format_dollar_free);
else else
return Playlist::SongInColumnsToString(*pair.second, 0); return Playlist::SongInColumnsToString(ei.song(), 0);
} }
else else
return ""; return "";

View File

@@ -21,10 +21,58 @@
#ifndef _SEARCH_ENGINE_H #ifndef _SEARCH_ENGINE_H
#define _SEARCH_ENGINE_H #define _SEARCH_ENGINE_H
#include <cassert>
#include "mpdpp.h" #include "mpdpp.h"
#include "ncmpcpp.h" #include "ncmpcpp.h"
class SearchEngine : public Screen< Menu< std::pair<Buffer *, MPD::Song *> > > struct SEItem
{
SEItem() : isThisSong(false), itsBuffer(0) { }
SEItem(Buffer *buf) : isThisSong(false), itsBuffer(buf) { }
SEItem(const MPD::Song &s) : isThisSong(true), itsSong(s) { }
SEItem(const SEItem &ei) { *this = ei; }
~SEItem() {
if (!isThisSong)
delete itsBuffer;
}
Buffer &mkBuffer() {
assert(!isThisSong);
delete itsBuffer;
itsBuffer = new Buffer();
return *itsBuffer;
}
bool isSong() const { return isThisSong; }
Buffer &buffer() { assert(!isThisSong && itsBuffer); return *itsBuffer; }
MPD::Song &song() { assert(isThisSong); return itsSong; }
const Buffer &buffer() const { assert(!isThisSong && itsBuffer); return *itsBuffer; }
const MPD::Song &song() const { assert(isThisSong); return itsSong; }
SEItem &operator=(const SEItem &se) {
if (this == &se)
return *this;
isThisSong = se.isThisSong;
if (se.isThisSong)
itsSong = se.itsSong;
else if (se.itsBuffer)
itsBuffer = new Buffer(*se.itsBuffer);
else
itsBuffer = 0;
return *this;
}
private:
bool isThisSong;
Buffer *itsBuffer;
MPD::Song itsSong;
};
class SearchEngine : public Screen< Menu<SEItem> >
{ {
public: public:
virtual void Resize(); virtual void Resize();
@@ -38,7 +86,7 @@ class SearchEngine : public Screen< Menu< std::pair<Buffer *, MPD::Song *> > >
virtual bool isTabbable() { return true; } virtual bool isTabbable() { return true; }
virtual MPD::Song *CurrentSong(); virtual MPD::Song *CurrentSong();
virtual MPD::Song *GetSong(size_t pos) { return !w->isSeparator(pos) ? w->at(pos).second : 0; } virtual MPD::Song *GetSong(size_t pos) { return !w->isSeparator(pos) && w->at(pos).isSong() ? &w->at(pos).song() : 0; }
virtual bool allowsSelection() { return w->Choice() >= StaticOptions; } virtual bool allowsSelection() { return w->Choice() >= StaticOptions; }
virtual void ReverseSelection() { w->ReverseSelection(StaticOptions); } virtual void ReverseSelection() { w->ReverseSelection(StaticOptions); }
@@ -68,7 +116,7 @@ class SearchEngine : public Screen< Menu< std::pair<Buffer *, MPD::Song *> > >
const char **SearchMode; const char **SearchMode;
static std::string SearchEngineOptionToString(const std::pair<Buffer *, MPD::Song *> &, void *); static std::string SearchEngineOptionToString(const SEItem &, void *);
static const char *SearchModes[]; static const char *SearchModes[];

View File

@@ -179,8 +179,8 @@ void SelectedItemsAdder::EnterPressed()
{ {
std::string utf_playlist = locale_to_utf_cpy(playlist); std::string utf_playlist = locale_to_utf_cpy(playlist);
Mpd.StartCommandsList(); Mpd.StartCommandsList();
for (MPD::SongList::const_iterator it = list.begin(); it != list.end(); ++it) for (auto it = list.begin(); it != list.end(); ++it)
Mpd.AddToPlaylist(utf_playlist, **it); Mpd.AddToPlaylist(utf_playlist, *it);
if (Mpd.CommitCommandsList()) if (Mpd.CommitCommandsList())
ShowMessage("Selected item(s) added to playlist \"%s\"", playlist.c_str()); ShowMessage("Selected item(s) added to playlist \"%s\"", playlist.c_str());
} }
@@ -189,8 +189,8 @@ void SelectedItemsAdder::EnterPressed()
{ {
std::string playlist = locale_to_utf_cpy(w->Current()); std::string playlist = locale_to_utf_cpy(w->Current());
Mpd.StartCommandsList(); Mpd.StartCommandsList();
for (MPD::SongList::const_iterator it = list.begin(); it != list.end(); ++it) for (auto it = list.begin(); it != list.end(); ++it)
Mpd.AddToPlaylist(playlist, **it); Mpd.AddToPlaylist(playlist, *it);
if (Mpd.CommitCommandsList()) if (Mpd.CommitCommandsList())
ShowMessage("Selected item(s) added to playlist \"%s\"", w->Current().c_str()); ShowMessage("Selected item(s) added to playlist \"%s\"", w->Current().c_str());
} }
@@ -247,7 +247,6 @@ void SelectedItemsAdder::EnterPressed()
if (successful_operation) if (successful_operation)
ShowMessage("Selected item(s) added"); ShowMessage("Selected item(s) added");
} }
MPD::FreeSongList(list);
SwitchTo(); SwitchTo();
} }

View File

@@ -234,16 +234,16 @@ void NcmpcppStatusChanged(MPD::Connection *, MPD::StatusChanges changed, void *)
myPlaylist->Items->Reserve(playlist_length); myPlaylist->Items->Reserve(playlist_length);
for (MPD::SongList::const_iterator it = list.begin(); it != list.end(); ++it) for (MPD::SongList::const_iterator it = list.begin(); it != list.end(); ++it)
{ {
int pos = (*it)->getPosition(); int pos = it->getPosition();
if (pos < int(myPlaylist->Items->Size())) if (pos < int(myPlaylist->Items->Size()))
{ {
// if song's already in playlist, replace it with a new one // if song's already in playlist, replace it with a new one
myPlaylist->Items->at(pos) = **it; myPlaylist->Items->at(pos) = *it;
} }
else else
{ {
// otherwise just add it to playlist // otherwise just add it to playlist
myPlaylist->Items->AddOption(**it); myPlaylist->Items->AddOption(*it);
} }
} }
if (was_filtered) if (was_filtered)
@@ -252,7 +252,6 @@ void NcmpcppStatusChanged(MPD::Connection *, MPD::StatusChanges changed, void *)
if (myPlaylist->Items->Empty()) if (myPlaylist->Items->Empty())
myPlaylist->Items->ShowAll(); myPlaylist->Items->ShowAll();
} }
FreeSongList(list);
Playlist::ReloadTotalLength = true; Playlist::ReloadTotalLength = true;
Playlist::ReloadRemaining = true; Playlist::ReloadRemaining = true;