use std::pair instead of two different containters

This commit is contained in:
unK
2008-09-10 10:50:59 +02:00
parent 0e1e3c6342
commit e36bf0a3cc
8 changed files with 132 additions and 118 deletions

View File

@@ -250,6 +250,11 @@ string TotalPlaylistLength()
return result; return result;
} }
string DisplayStringPair(const StringPair &pair, void *null)
{
return pair.first;
}
string DisplayItem(const Item &item, void *) string DisplayItem(const Item &item, void *)
{ {
switch (item.type) switch (item.type)

View File

@@ -46,6 +46,7 @@ bool Keypressed(int, const int *);
void WindowTitle(const string &); void WindowTitle(const string &);
string TotalPlaylistLength(); string TotalPlaylistLength();
string DisplayStringPair(const StringPair &, void * = NULL);
string DisplayItem(const Item &, void * = NULL); string DisplayItem(const Item &, void * = NULL);
string DisplayColumns(string); string DisplayColumns(string);
string DisplaySongInColumns(const Song &, void *); string DisplaySongInColumns(const Song &, void *);

View File

@@ -48,7 +48,7 @@ class Menu : public Window
typedef string (*ItemDisplayer) (const T &, void *); typedef string (*ItemDisplayer) (const T &, void *);
public: public:
Menu(int startx, int starty, int width, int height, string title, Color color, Border border) : itsItemDisplayer(0), itsItemDisplayerUserdata(0), Window(startx, starty, width, height, title, color, border), itsSelectedPrefix("[.r]"), itsSelectedSuffix("[/r]"), itsStaticsNumber(0), itsBeginning(0), itsHighlight(0), itsHighlightColor(itsBaseColor), itsHighlightEnabled(1) { SetColor(color); } Menu(int startx, int starty, int width, int height, string title, Color color, Border border) : itsItemDisplayer(0), itsItemDisplayerUserdata(0), Window(startx, starty, width, height, title, color, border), itsSelectedPrefix("[.r]"), itsSelectedSuffix("[/r]"), itsStaticsNumber(0), itsBeginning(0), itsHighlight(0), itsHighlightColor(itsBaseColor), itsHighlightEnabled(1) { }
Menu(const Menu &); Menu(const Menu &);
virtual ~Menu(); virtual ~Menu();
@@ -61,10 +61,13 @@ class Menu : public Window
void AddStaticBoldOption(const T &item, Location location = lLeft, bool separator = 0); void AddStaticBoldOption(const T &item, Location location = lLeft, bool separator = 0);
void AddSeparator(); void AddSeparator();
void UpdateOption(int, const T &, Location = lLeft, bool separator = 0); void UpdateOption(int, const T &, Location = lLeft, bool separator = 0);
void RefreshOption(int option = -1) { NeedsRedraw.push_back(option < 0 ? itsHighlight : option); }
void BoldOption(int, bool); void BoldOption(int, bool);
void MakeStatic(int, bool); void MakeStatic(int, bool);
void DeleteOption(int); void DeleteOption(int);
void Swap(int, int); void Swap(int, int);
void Insert(int, const T &, bool bold = 0, bool is_static = 0, bool separator = 0, Location location = lLeft);
void InsertSeparator(int where) { Insert(where, T(), 0, 1, 1); }
virtual string GetOption(int i = -1) const; virtual string GetOption(int i = -1) const;
virtual void Refresh(bool redraw_whole_window = 0); virtual void Refresh(bool redraw_whole_window = 0);
@@ -92,6 +95,10 @@ class Menu : public Window
virtual Window * Clone() const { return new Menu(*this); } virtual Window * Clone() const { return new Menu(*this); }
virtual Window * EmptyClone() const; virtual Window * EmptyClone() const;
T & Back() { return itsOptions.back()->item; }
const T & Back() const { return itsOptions.back()->item; }
T & Current() { return itsOptions.at(itsHighlight)->item; }
const T & Current() const { return itsOptions.at(itsHighlight)->item; }
T & at(int i) { return itsOptions.at(i)->item; } T & at(int i) { return itsOptions.at(i)->item; }
const T & at(int i) const { return itsOptions.at(i)->item; } const T & at(int i) const { return itsOptions.at(i)->item; }
const T & operator[](int i) const { return itsOptions[i]->item; } const T & operator[](int i) const { return itsOptions[i]->item; }
@@ -239,7 +246,7 @@ void Menu<T>::AddStaticBoldOption(const T &item, Location location, bool separat
template <class T> template <class T>
void Menu<T>::AddSeparator() void Menu<T>::AddSeparator()
{ {
AddStaticOption("", lLeft, 1); AddStaticOption(T(), lLeft, 1);
} }
template <class T> template <class T>
@@ -353,6 +360,20 @@ void Menu<T>::Swap(int one, int two)
} }
} }
template <class T>
void Menu<T>::Insert(int where, const T &item, bool bold, bool is_static, bool separator, Location location)
{
Option<T> *new_option = new Option<T>;
new_option->item = item;
new_option->location = lLeft;
new_option->have_separator = separator;
new_option->is_static = is_static;
new_option->is_bold = bold;
if (is_static)
itsStaticsNumber++;
itsOptions.insert(itsOptions.begin()+where, new_option);
}
template <class T> template <class T>
void Menu<T>::redraw_screen() void Menu<T>::redraw_screen()
{ {

View File

@@ -34,10 +34,10 @@
block_statusbar_update = 1; \ block_statusbar_update = 1; \
else \ else \
block_progressbar_update = 1; \ block_progressbar_update = 1; \
allow_statusbar_unblock = 0 allow_statusbar_unlock = 0
#define UNLOCK_STATUSBAR \ #define UNLOCK_STATUSBAR \
allow_statusbar_unblock = 1; \ allow_statusbar_unlock = 1; \
if (lock_statusbar_delay < 0) \ if (lock_statusbar_delay < 0) \
{ \ { \
if (Config.statusbar_visibility) \ if (Config.statusbar_visibility) \
@@ -68,10 +68,6 @@
ncmpcpp_config Config; ncmpcpp_config Config;
ncmpcpp_keys Key; ncmpcpp_keys Key;
SongList vSearched;
std::map<string, string, CaseInsensitiveSorting> vLibAlbums;
std::map<string, string, CaseInsensitiveSorting> vEditorAlbums;
vector<int> vFoundPositions; vector<int> vFoundPositions;
int found_pos = 0; int found_pos = 0;
@@ -80,15 +76,15 @@ Window *wPrev = 0;
Menu<Song> *mPlaylist; Menu<Song> *mPlaylist;
Menu<Item> *mBrowser; Menu<Item> *mBrowser;
Menu<string> *mSearcher; Menu< std::pair<string, Song> > *mSearcher;
Menu<string> *mLibArtists; Menu<string> *mLibArtists;
Menu<string> *mLibAlbums; Menu<StringPair> *mLibAlbums;
Menu<Song> *mLibSongs; Menu<Song> *mLibSongs;
#ifdef HAVE_TAGLIB_H #ifdef HAVE_TAGLIB_H
Menu<string> *mTagEditor; Menu<string> *mTagEditor;
Menu<string> *mEditorAlbums; Menu<StringPair> *mEditorAlbums;
Menu<string> *mEditorTagTypes; Menu<string> *mEditorTagTypes;
Menu<Song> *mEditorTags; Menu<Song> *mEditorTags;
#endif // HAVE_TAGLIB_H #endif // HAVE_TAGLIB_H
@@ -121,7 +117,7 @@ NcmpcppScreen prev_screen;
bool dont_change_now_playing = 0; bool dont_change_now_playing = 0;
bool block_progressbar_update = 0; bool block_progressbar_update = 0;
bool block_statusbar_update = 0; bool block_statusbar_update = 0;
bool allow_statusbar_unblock = 1; bool allow_statusbar_unlock = 1;
bool block_playlist_update = 0; bool block_playlist_update = 0;
bool block_found_item_list_update = 0; bool block_found_item_list_update = 0;
@@ -197,7 +193,8 @@ int main(int argc, char *argv[])
mBrowser->SetSelectSuffix(Config.selected_item_suffix); mBrowser->SetSelectSuffix(Config.selected_item_suffix);
mBrowser->SetItemDisplayer(DisplayItem); mBrowser->SetItemDisplayer(DisplayItem);
mSearcher = new Menu<string>(0, main_start_y, COLS, main_height, "", Config.main_color, brNone); mSearcher = new Menu< std::pair<string, Song> >(0, main_start_y, COLS, main_height, "", Config.main_color, brNone);
mSearcher->SetItemDisplayer(SearchEngineDisplayer);
mSearcher->SetSelectPrefix(Config.selected_item_prefix); mSearcher->SetSelectPrefix(Config.selected_item_prefix);
mSearcher->SetSelectSuffix(Config.selected_item_suffix); mSearcher->SetSelectSuffix(Config.selected_item_suffix);
@@ -208,7 +205,8 @@ int main(int argc, char *argv[])
int lib_songs_start_x = lib_artist_width+lib_albums_width+2; int lib_songs_start_x = lib_artist_width+lib_albums_width+2;
mLibArtists = new Menu<string>(0, main_start_y, lib_artist_width, main_height, "Artists", Config.main_color, brNone); mLibArtists = new Menu<string>(0, main_start_y, lib_artist_width, main_height, "Artists", Config.main_color, brNone);
mLibAlbums = new Menu<string>(lib_albums_start_x, main_start_y, lib_albums_width, main_height, "Albums", Config.main_color, brNone); mLibAlbums = new Menu<StringPair>(lib_albums_start_x, main_start_y, lib_albums_width, main_height, "Albums", Config.main_color, brNone);
mLibAlbums->SetItemDisplayer(DisplayStringPair);
mLibSongs = new Menu<Song>(lib_songs_start_x, main_start_y, lib_songs_width, main_height, "Songs", Config.main_color, brNone); mLibSongs = new Menu<Song>(lib_songs_start_x, main_start_y, lib_songs_width, main_height, "Songs", Config.main_color, brNone);
mLibSongs->SetSelectPrefix(Config.selected_item_prefix); mLibSongs->SetSelectPrefix(Config.selected_item_prefix);
mLibSongs->SetSelectSuffix(Config.selected_item_suffix); mLibSongs->SetSelectSuffix(Config.selected_item_suffix);
@@ -216,8 +214,9 @@ int main(int argc, char *argv[])
mLibSongs->SetItemDisplayerUserData(&Config.song_library_format); mLibSongs->SetItemDisplayerUserData(&Config.song_library_format);
# ifdef HAVE_TAGLIB_H # ifdef HAVE_TAGLIB_H
mTagEditor = static_cast<Menu<string> *>(mSearcher->Clone()); mTagEditor = new Menu<string>(0, main_start_y, COLS, main_height, "", Config.main_color, brNone);
mEditorAlbums = new Menu<string>(0, main_start_y, lib_artist_width, main_height, "Albums", Config.main_color, brNone); mEditorAlbums = new Menu<StringPair>(0, main_start_y, lib_artist_width, main_height, "Albums", Config.main_color, brNone);
mEditorAlbums->SetItemDisplayer(DisplayStringPair);
mEditorTagTypes = new Menu<string>(lib_albums_start_x, main_start_y, lib_albums_width, main_height, "Tag types", Config.main_color, brNone); mEditorTagTypes = new Menu<string>(lib_albums_start_x, main_start_y, lib_albums_width, main_height, "Tag types", Config.main_color, brNone);
mEditorTags = new Menu<Song>(lib_songs_start_x, main_start_y, lib_songs_width, main_height, "Tags", Config.main_color, brNone); mEditorTags = new Menu<Song>(lib_songs_start_x, main_start_y, lib_songs_width, main_height, "Tags", Config.main_color, brNone);
mEditorTags->SetItemDisplayer(DisplayTag); mEditorTags->SetItemDisplayer(DisplayTag);
@@ -523,8 +522,8 @@ int main(int argc, char *argv[])
if (mLibAlbums->Empty() && mLibSongs->Empty()) if (mLibAlbums->Empty() && mLibSongs->Empty())
{ {
mLibAlbums->Reset(); mLibAlbums->Reset();
vLibAlbums.clear();
TagList list; TagList list;
std::map<string, string, CaseInsensitiveSorting> maplist;
Mpd->GetAlbums(mLibArtists->GetOption(), list); Mpd->GetAlbums(mLibArtists->GetOption(), list);
for (TagList::const_iterator it = list.begin(); it != list.end(); it++) for (TagList::const_iterator it = list.begin(); it != list.end(); it++)
{ {
@@ -538,17 +537,17 @@ int main(int argc, char *argv[])
{ {
if ((*j)->GetYear() != EMPTY_TAG) if ((*j)->GetYear() != EMPTY_TAG)
{ {
vLibAlbums["(" + (*j)->GetYear() + ") " + *it] = *it; maplist["(" + (*j)->GetYear() + ") " + *it] = *it;
written = 1; written = 1;
break; break;
} }
} }
if (!written) if (!written)
vLibAlbums[*it] = *it; maplist[*it] = *it;
FreeSongList(l); FreeSongList(l);
} }
for (std::map<string, string>::const_iterator it = vLibAlbums.begin(); it != vLibAlbums.end(); it++) for (std::map<string, string>::const_iterator it = maplist.begin(); it != maplist.end(); it++)
mLibAlbums->AddOption(it->first); mLibAlbums->AddOption(StringPair(it->first, it->second));
mLibAlbums->Window::Clear(); mLibAlbums->Window::Clear();
mLibAlbums->Refresh(); mLibAlbums->Refresh();
} }
@@ -577,7 +576,7 @@ int main(int argc, char *argv[])
mLibSongs->Clear(0); mLibSongs->Clear(0);
Mpd->StartSearch(1); Mpd->StartSearch(1);
Mpd->AddSearch(MPD_TAG_ITEM_ARTIST, mLibArtists->GetOption()); Mpd->AddSearch(MPD_TAG_ITEM_ARTIST, mLibArtists->GetOption());
Mpd->AddSearch(MPD_TAG_ITEM_ALBUM, vLibAlbums[mLibAlbums->GetOption()]); Mpd->AddSearch(MPD_TAG_ITEM_ALBUM, mLibAlbums->Current().second);
Mpd->CommitSearch(list); Mpd->CommitSearch(list);
} }
sort(list.begin(), list.end(), SortSongsByTrack); sort(list.begin(), list.end(), SortSongsByTrack);
@@ -668,10 +667,10 @@ int main(int argc, char *argv[])
{ {
found_pos = 0; found_pos = 0;
vFoundPositions.clear(); vFoundPositions.clear();
vEditorAlbums.clear();
mEditorAlbums->Window::Clear(); mEditorAlbums->Window::Clear();
mEditorTags->Clear(); mEditorTags->Clear();
TagList list; TagList list;
std::map<string, string, CaseInsensitiveSorting> maplist;
mEditorAlbums->WriteXY(0, 0, "Fetching albums' list..."); mEditorAlbums->WriteXY(0, 0, "Fetching albums' list...");
Mpd->GetAlbums("", list); Mpd->GetAlbums("", list);
for (TagList::const_iterator it = list.begin(); it != list.end(); it++) for (TagList::const_iterator it = list.begin(); it != list.end(); it++)
@@ -681,11 +680,11 @@ int main(int argc, char *argv[])
Mpd->StartSearch(1); Mpd->StartSearch(1);
Mpd->AddSearch(MPD_TAG_ITEM_ALBUM, *it); Mpd->AddSearch(MPD_TAG_ITEM_ALBUM, *it);
Mpd->CommitSearch(l); Mpd->CommitSearch(l);
vEditorAlbums[DisplaySong(*l[0], &Config.tag_editor_album_format)] = *it; maplist[DisplaySong(*l[0], &Config.tag_editor_album_format)] = *it;
FreeSongList(l); FreeSongList(l);
} }
for (std::map<string, string>::const_iterator it = vEditorAlbums.begin(); it != vEditorAlbums.end(); it++) for (std::map<string, string>::const_iterator it = maplist.begin(); it != maplist.end(); it++)
mEditorAlbums->AddOption(it->first); mEditorAlbums->AddOption(StringPair(it->first, it->second));
mEditorAlbums->Refresh(); mEditorAlbums->Refresh();
mEditorTagTypes->Refresh(); mEditorTagTypes->Refresh();
} }
@@ -695,7 +694,7 @@ int main(int argc, char *argv[])
mEditorTags->Reset(); mEditorTags->Reset();
SongList list; SongList list;
Mpd->StartSearch(1); Mpd->StartSearch(1);
Mpd->AddSearch(MPD_TAG_ITEM_ALBUM, vEditorAlbums[mEditorAlbums->GetOption()]); Mpd->AddSearch(MPD_TAG_ITEM_ALBUM, mEditorAlbums->Current().second);
Mpd->CommitSearch(list); Mpd->CommitSearch(list);
sort(list.begin(), list.end(), CaseInsensitiveSorting()); sort(list.begin(), list.end(), CaseInsensitiveSorting());
for (SongList::iterator it = list.begin(); it != list.end(); it++) for (SongList::iterator it = list.begin(); it != list.end(); it++)
@@ -1028,10 +1027,7 @@ int main(int argc, char *argv[])
ShowMessage("Tags updated!"); ShowMessage("Tags updated!");
Mpd->UpdateDirectory(s.GetDirectory()); Mpd->UpdateDirectory(s.GetDirectory());
if (prev_screen == csSearcher) if (prev_screen == csSearcher)
{ mSearcher->Current().second = s;
*vSearched[mSearcher->GetRealChoice()-1] = s;
mSearcher->UpdateOption(mSearcher->GetChoice(), DisplaySong(s));
}
} }
ShowMessage("Error writing tags!"); ShowMessage("Error writing tags!");
} }
@@ -1073,7 +1069,7 @@ int main(int argc, char *argv[])
s.SetShortFilename(wFooter->GetString()); s.SetShortFilename(wFooter->GetString());
else else
s.SetShortFilename(wFooter->GetString(s.GetShortFilename())); s.SetShortFilename(wFooter->GetString(s.GetShortFilename()));
mSearcher->UpdateOption(option, "[.b]Filename:[/b] " + s.GetShortFilename()); mSearcher->Current().first = "[.b]Filename:[/b] " + s.GetShortFilename();
break; break;
} }
case 2: case 2:
@@ -1083,7 +1079,7 @@ int main(int argc, char *argv[])
s.SetTitle(wFooter->GetString()); s.SetTitle(wFooter->GetString());
else else
s.SetTitle(wFooter->GetString(s.GetTitle())); s.SetTitle(wFooter->GetString(s.GetTitle()));
mSearcher->UpdateOption(option, "[.b]Title:[/b] " + s.GetTitle()); mSearcher->Current().first = "[.b]Title:[/b] " + s.GetTitle();
break; break;
} }
case 3: case 3:
@@ -1093,7 +1089,7 @@ int main(int argc, char *argv[])
s.SetArtist(wFooter->GetString()); s.SetArtist(wFooter->GetString());
else else
s.SetArtist(wFooter->GetString(s.GetArtist())); s.SetArtist(wFooter->GetString(s.GetArtist()));
mSearcher->UpdateOption(option, "[.b]Artist:[/b] " + s.GetArtist()); mSearcher->Current().first = "[.b]Artist:[/b] " + s.GetArtist();
break; break;
} }
case 4: case 4:
@@ -1103,7 +1099,7 @@ int main(int argc, char *argv[])
s.SetAlbum(wFooter->GetString()); s.SetAlbum(wFooter->GetString());
else else
s.SetAlbum(wFooter->GetString(s.GetAlbum())); s.SetAlbum(wFooter->GetString(s.GetAlbum()));
mSearcher->UpdateOption(option, "[.b]Album:[/b] " + s.GetAlbum()); mSearcher->Current().first = "[.b]Album:[/b] " + s.GetAlbum();
break; break;
} }
case 5: case 5:
@@ -1113,7 +1109,7 @@ int main(int argc, char *argv[])
s.SetYear(wFooter->GetString(4)); s.SetYear(wFooter->GetString(4));
else else
s.SetYear(wFooter->GetString(s.GetYear(), 4)); s.SetYear(wFooter->GetString(s.GetYear(), 4));
mSearcher->UpdateOption(option, "[.b]Year:[/b] " + s.GetYear()); mSearcher->Current().first = "[.b]Year:[/b] " + s.GetYear();
break; break;
} }
case 6: case 6:
@@ -1123,7 +1119,7 @@ int main(int argc, char *argv[])
s.SetTrack(wFooter->GetString(3)); s.SetTrack(wFooter->GetString(3));
else else
s.SetTrack(wFooter->GetString(s.GetTrack(), 3)); s.SetTrack(wFooter->GetString(s.GetTrack(), 3));
mSearcher->UpdateOption(option, "[.b]Track:[/b] " + s.GetTrack()); mSearcher->Current().first = "[.b]Track:[/b] " + s.GetTrack();
break; break;
} }
case 7: case 7:
@@ -1133,7 +1129,7 @@ int main(int argc, char *argv[])
s.SetGenre(wFooter->GetString()); s.SetGenre(wFooter->GetString());
else else
s.SetGenre(wFooter->GetString(s.GetGenre())); s.SetGenre(wFooter->GetString(s.GetGenre()));
mSearcher->UpdateOption(option, "[.b]Genre:[/b] " + s.GetGenre()); mSearcher->Current().first = "[.b]Genre:[/b] " + s.GetGenre();
break; break;
} }
case 8: case 8:
@@ -1143,44 +1139,33 @@ int main(int argc, char *argv[])
s.SetComment(wFooter->GetString()); s.SetComment(wFooter->GetString());
else else
s.SetComment(wFooter->GetString(s.GetComment())); s.SetComment(wFooter->GetString(s.GetComment()));
mSearcher->UpdateOption(option, "[.b]Comment:[/b] " + s.GetComment()); mSearcher->Current().first = "[.b]Comment:[/b] " + s.GetComment();
break; break;
} }
case 10: case 10:
{ {
search_match_to_pattern = !search_match_to_pattern; search_match_to_pattern = !search_match_to_pattern;
mSearcher->UpdateOption(option, "[.b]Search mode:[/b] " + (search_match_to_pattern ? search_mode_normal : search_mode_strict)); mSearcher->Current().first = "[.b]Search mode:[/b] " + (search_match_to_pattern ? search_mode_normal : search_mode_strict);
break; break;
} }
case 11: case 11:
{ {
search_case_sensitive = !search_case_sensitive; search_case_sensitive = !search_case_sensitive;
mSearcher->UpdateOption(option, "[.b]Case sensitive:[/b] " + (string)(search_case_sensitive ? "Yes" : "No")); mSearcher->Current().first = "[.b]Case sensitive:[/b] " + string(search_case_sensitive ? "Yes" : "No");
break; break;
} }
case 13: case 13:
{ {
ShowMessage("Searching..."); ShowMessage("Searching...");
Search(vSearched, s); Search(s);
if (!vSearched.empty()) if (mSearcher->Back().first == ".")
{ {
bool bold = 0; bool bold = 0;
mSearcher->AddSeparator(); int found = mSearcher->Size()-search_engine_static_options;
mSearcher->AddStaticBoldOption("[.white]Search results:[/white] [.green]Found " + IntoStr(vSearched.size()) + (vSearched.size() > 1 ? " songs" : " song") + "[/green]"); mSearcher->InsertSeparator(14);
mSearcher->AddSeparator(); mSearcher->Insert(15, std::pair<string, Song>("[.white]Search results:[/white] [.green]Found " + IntoStr(found) + (found > 1 ? " songs" : " song") + "[/green]", Song()), 1, 1);
for (SongList::const_iterator it = vSearched.begin(); it != vSearched.end(); it++) mSearcher->InsertSeparator(16);
{ UpdateFoundList();
for (int j = 0; j < mPlaylist->Size(); j++)
{
if (mPlaylist->at(j).GetHash() == (*it)->GetHash())
{
bold = 1;
break;
}
}
bold ? mSearcher->AddBoldOption(DisplaySong(**it)) : mSearcher->AddOption(DisplaySong(**it));
bold = 0;
}
ShowMessage("Searching finished!"); ShowMessage("Searching finished!");
for (int i = 0; i < 13; i++) for (int i = 0; i < 13; i++)
mSearcher->MakeStatic(i, 1); mSearcher->MakeStatic(i, 1);
@@ -1195,7 +1180,6 @@ int main(int argc, char *argv[])
{ {
found_pos = 0; found_pos = 0;
vFoundPositions.clear(); vFoundPositions.clear();
FreeSongList(vSearched);
PrepareSearchEngine(sought_pattern); PrepareSearchEngine(sought_pattern);
ShowMessage("Search state reset"); ShowMessage("Search state reset");
break; break;
@@ -1203,7 +1187,7 @@ int main(int argc, char *argv[])
default: default:
{ {
block_found_item_list_update = 1; block_found_item_list_update = 1;
Song &s = *vSearched[mSearcher->GetRealChoice()-1]; const Song &s = mSearcher->Current().second;
int id = Mpd->AddSong(s); int id = Mpd->AddSong(s);
if (id >= 0) if (id >= 0)
{ {
@@ -1214,6 +1198,8 @@ int main(int argc, char *argv[])
break; break;
} }
} }
if (option <= 10)
mSearcher->RefreshOption(option);
UNLOCK_STATUSBAR; UNLOCK_STATUSBAR;
break; break;
} }
@@ -1250,7 +1236,7 @@ int main(int argc, char *argv[])
Mpd->QueueAddSong(mLibSongs->at(i)); Mpd->QueueAddSong(mLibSongs->at(i));
if (Mpd->CommitQueue()) if (Mpd->CommitQueue())
{ {
ShowMessage("Adding songs from: " + mLibArtists->GetOption() + " \"" + vLibAlbums[mLibAlbums->GetOption()] + "\""); ShowMessage("Adding songs from: " + mLibArtists->GetOption() + " \"" + mLibAlbums->Current().second + "\"");
Song *s = &mPlaylist->at(mPlaylist->Size()-mLibSongs->Size()); Song *s = &mPlaylist->at(mPlaylist->Size()-mLibSongs->Size());
if (s->GetHash() == mLibSongs->at(0).GetHash()) if (s->GetHash() == mLibSongs->at(0).GetHash())
{ {
@@ -1453,7 +1439,7 @@ int main(int argc, char *argv[])
{ {
if (Config.space_selects || wCurrent == mPlaylist) if (Config.space_selects || wCurrent == mPlaylist)
{ {
if (wCurrent == mPlaylist || (wCurrent == mBrowser && wCurrent->GetChoice() > (browsed_dir != "/" ? 1 : 0)) || (wCurrent == mSearcher && !vSearched.empty() && wCurrent->GetChoice() > search_engine_static_option) || wCurrent == mLibSongs || wCurrent == mPlaylistEditor) if (wCurrent == mPlaylist || (wCurrent == mBrowser && wCurrent->GetChoice() >= (browsed_dir != "/" ? 1 : 0)) || (wCurrent == mSearcher && mSearcher->Current().first == ".") || wCurrent == mLibSongs || wCurrent == mPlaylistEditor)
{ {
int i = wCurrent->GetChoice(); int i = wCurrent->GetChoice();
wCurrent->Select(i, !wCurrent->Selected(i)); wCurrent->Select(i, !wCurrent->Selected(i));
@@ -1513,13 +1499,10 @@ int main(int argc, char *argv[])
} }
mBrowser->Go(wDown); mBrowser->Go(wDown);
} }
else if (current_screen == csSearcher && !vSearched.empty()) else if (current_screen == csSearcher && mSearcher->Current().first == ".")
{ {
int id = mSearcher->GetChoice()-search_engine_static_option;
if (id < 0)
continue;
block_found_item_list_update = 1; block_found_item_list_update = 1;
Song &s = *vSearched[id]; Song &s = mSearcher->Current().second;
if (Mpd->AddSong(s) != -1) if (Mpd->AddSong(s) != -1)
{ {
ShowMessage("Added to playlist: " + DisplaySong(s, &Config.song_status_format)); ShowMessage("Added to playlist: " + DisplaySong(s, &Config.song_status_format));
@@ -2165,16 +2148,16 @@ int main(int argc, char *argv[])
{ {
LOCK_STATUSBAR; LOCK_STATUSBAR;
wFooter->WriteXY(0, Config.statusbar_visibility, "[.b]Album:[/b] ", 1); wFooter->WriteXY(0, Config.statusbar_visibility, "[.b]Album:[/b] ", 1);
string new_album = wFooter->GetString(vLibAlbums[mLibAlbums->GetOption()]); string new_album = wFooter->GetString(mLibAlbums->Current().second);
UNLOCK_STATUSBAR; UNLOCK_STATUSBAR;
if (!new_album.empty() && new_album != vLibAlbums[mLibAlbums->GetOption()]) if (!new_album.empty() && new_album != mLibAlbums->Current().second)
{ {
bool success = 1; bool success = 1;
SongList list; SongList list;
ShowMessage("Updating tags..."); ShowMessage("Updating tags...");
Mpd->StartSearch(1); Mpd->StartSearch(1);
Mpd->AddSearch(MPD_TAG_ITEM_ARTIST, mLibArtists->GetOption()); Mpd->AddSearch(MPD_TAG_ITEM_ARTIST, mLibArtists->GetOption());
Mpd->AddSearch(MPD_TAG_ITEM_ALBUM, vLibAlbums[mLibAlbums->GetOption()]); Mpd->AddSearch(MPD_TAG_ITEM_ALBUM, mLibAlbums->Current().second);
Mpd->CommitSearch(list); Mpd->CommitSearch(list);
for (SongList::const_iterator it = list.begin(); it != list.end(); it++) for (SongList::const_iterator it = list.begin(); it != list.end(); it++)
{ {
@@ -2197,7 +2180,7 @@ int main(int argc, char *argv[])
else if ( else if (
(wCurrent == mPlaylist && !mPlaylist->Empty()) (wCurrent == mPlaylist && !mPlaylist->Empty())
|| (wCurrent == mBrowser && mBrowser->at(mBrowser->GetChoice()).type == itSong) || (wCurrent == mBrowser && mBrowser->at(mBrowser->GetChoice()).type == itSong)
|| (wCurrent == mSearcher && !vSearched.empty() && mSearcher->GetChoice() >= search_engine_static_option) || (wCurrent == mSearcher && mSearcher->Current().first == ".")
|| (wCurrent == mLibSongs && !mLibSongs->Empty()) || (wCurrent == mLibSongs && !mLibSongs->Empty())
|| (wCurrent == mPlaylistEditor && !mPlaylistEditor->Empty())) || (wCurrent == mPlaylistEditor && !mPlaylistEditor->Empty()))
{ {
@@ -2211,7 +2194,7 @@ int main(int argc, char *argv[])
edited_song = *mBrowser->at(id).song; edited_song = *mBrowser->at(id).song;
break; break;
case csSearcher: case csSearcher:
edited_song = *vSearched[id-search_engine_static_option]; edited_song = mSearcher->at(id).second;
break; break;
case csLibrary: case csLibrary:
edited_song = mLibSongs->at(id); edited_song = mLibSongs->at(id);
@@ -2251,7 +2234,7 @@ int main(int argc, char *argv[])
else if (Keypressed(input, Key.GoToContainingDir)) else if (Keypressed(input, Key.GoToContainingDir))
{ {
if ((wCurrent == mPlaylist && !mPlaylist->Empty()) if ((wCurrent == mPlaylist && !mPlaylist->Empty())
|| (wCurrent == mSearcher && !vSearched.empty() && mSearcher->GetChoice() >= search_engine_static_option) || (wCurrent == mSearcher && mSearcher->Current().first == ".")
|| (wCurrent == mLibSongs && !mLibSongs->Empty()) || (wCurrent == mLibSongs && !mLibSongs->Empty())
|| (wCurrent == mPlaylistEditor && !mPlaylistEditor->Empty())) || (wCurrent == mPlaylistEditor && !mPlaylistEditor->Empty()))
{ {
@@ -2263,7 +2246,7 @@ int main(int argc, char *argv[])
s = &mPlaylist->at(id); s = &mPlaylist->at(id);
break; break;
case csSearcher: case csSearcher:
s = vSearched[id-search_engine_static_option]; s = &mSearcher->at(id).second;
break; break;
case csLibrary: case csLibrary:
s = &mLibSongs->at(id); s = &mLibSongs->at(id);
@@ -2318,7 +2301,7 @@ int main(int argc, char *argv[])
} }
else if (Keypressed(input, Key.ReverseSelection)) else if (Keypressed(input, Key.ReverseSelection))
{ {
if (wCurrent == mPlaylist || wCurrent == mBrowser || (wCurrent == mSearcher && !vSearched.empty()) || wCurrent == mLibSongs || wCurrent == mPlaylistEditor) if (wCurrent == mPlaylist || wCurrent == mBrowser || (wCurrent == mSearcher && mSearcher->Current().first == ".") || wCurrent == mLibSongs || wCurrent == mPlaylistEditor)
{ {
for (int i = 0; i < wCurrent->Size(); i++) for (int i = 0; i < wCurrent->Size(); i++)
wCurrent->Select(i, !wCurrent->Selected(i) && !wCurrent->IsStatic(i)); wCurrent->Select(i, !wCurrent->Selected(i) && !wCurrent->IsStatic(i));
@@ -2391,7 +2374,7 @@ int main(int argc, char *argv[])
} }
case csSearcher: case csSearcher:
{ {
Song *s = new Song(*vSearched[*it-search_engine_static_option]); Song *s = new Song(mSearcher->at(*it).second);
result.push_back(s); result.push_back(s);
break; break;
} }
@@ -2554,7 +2537,7 @@ int main(int argc, char *argv[])
else if (Keypressed(input, Key.FindForward) || Keypressed(input, Key.FindBackward)) else if (Keypressed(input, Key.FindForward) || Keypressed(input, Key.FindBackward))
{ {
if ((current_screen != csHelp && current_screen != csSearcher) if ((current_screen != csHelp && current_screen != csSearcher)
|| (current_screen == csSearcher && !vSearched.empty())) || (current_screen == csSearcher && mSearcher->Current().first == "."))
{ {
string how = Keypressed(input, Key.FindForward) ? "forward" : "backward"; string how = Keypressed(input, Key.FindForward) ? "forward" : "backward";
found_pos = -1; found_pos = -1;
@@ -2569,7 +2552,7 @@ int main(int argc, char *argv[])
transform(findme.begin(), findme.end(), findme.begin(), tolower); transform(findme.begin(), findme.end(), findme.begin(), tolower);
ShowMessage("Searching..."); ShowMessage("Searching...");
for (int i = (wCurrent == mSearcher ? search_engine_static_option-1 : 0); i < wCurrent->Size(); i++) for (int i = (wCurrent == mSearcher ? search_engine_static_options-1 : 0); i < wCurrent->Size(); i++)
{ {
string name = Window::OmitBBCodes(wCurrent->GetOption(i)); string name = Window::OmitBBCodes(wCurrent->GetOption(i));
transform(name.begin(), name.end(), name.begin(), tolower); transform(name.begin(), name.end(), name.begin(), tolower);
@@ -2661,7 +2644,7 @@ int main(int argc, char *argv[])
else if ( else if (
(wCurrent == mPlaylist && !mPlaylist->Empty()) (wCurrent == mPlaylist && !mPlaylist->Empty())
|| (wCurrent == mBrowser && mBrowser->at(mBrowser->GetChoice()).type == itSong) || (wCurrent == mBrowser && mBrowser->at(mBrowser->GetChoice()).type == itSong)
|| (wCurrent == mSearcher && !vSearched.empty() && mSearcher->GetChoice() >= search_engine_static_option) || (wCurrent == mSearcher && mSearcher->Current().first == ".")
|| (wCurrent == mLibSongs && !mLibSongs->Empty()) || (wCurrent == mLibSongs && !mLibSongs->Empty())
|| (wCurrent == mPlaylistEditor && !mPlaylistEditor->Empty())) || (wCurrent == mPlaylistEditor && !mPlaylistEditor->Empty()))
{ {
@@ -2676,7 +2659,7 @@ int main(int argc, char *argv[])
s = mBrowser->at(id).song; s = mBrowser->at(id).song;
break; break;
case csSearcher: case csSearcher:
s = vSearched[id-search_engine_static_option]; // first one is 'Reset' s = &mSearcher->at(id).second;
break; break;
case csLibrary: case csLibrary:
s = &mLibSongs->at(id); s = &mLibSongs->at(id);
@@ -2754,16 +2737,16 @@ int main(int argc, char *argv[])
{ {
found_pos = 0; found_pos = 0;
vFoundPositions.clear(); vFoundPositions.clear();
if (vSearched.empty()) if (mSearcher->Empty())
PrepareSearchEngine(sought_pattern); PrepareSearchEngine(sought_pattern);
wCurrent = mSearcher; wCurrent = mSearcher;
wCurrent->Hide(); wCurrent->Hide();
current_screen = csSearcher; current_screen = csSearcher;
redraw_screen = 1; redraw_screen = 1;
if (!vSearched.empty()) if (mSearcher->Back().first == ".")
{ {
wCurrent->WriteXY(0, 0, "Updating list..."); wCurrent->WriteXY(0, 0, "Updating list...");
UpdateFoundList(vSearched); UpdateFoundList();
} }
} }
} }

View File

@@ -41,6 +41,8 @@
#include "scrollpad.h" #include "scrollpad.h"
#include "misc.h" #include "misc.h"
typedef std::pair<string, string> StringPair;
enum NcmpcppScreen enum NcmpcppScreen
{ {
csHelp, csHelp,
@@ -55,7 +57,6 @@ enum NcmpcppScreen
}; };
const int ncmpcpp_window_timeout = 500; const int ncmpcpp_window_timeout = 500;
const int search_engine_static_option = 17;
const string home_folder = getenv("HOME"); const string home_folder = getenv("HOME");
const string TERMINAL_TYPE = getenv("TERM"); const string TERMINAL_TYPE = getenv("TERM");

View File

@@ -18,25 +18,29 @@
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/ ***************************************************************************/
#include <algorithm> #include "helpers.h"
#include "search_engine.h" #include "search_engine.h"
extern MPDConnection *Mpd; extern MPDConnection *Mpd;
extern Menu<Song> *mPlaylist; extern Menu<Song> *mPlaylist;
extern Menu<string> *mSearcher; extern Menu< std::pair<string, Song> > *mSearcher;
bool search_match_to_pattern = 0; bool search_match_to_pattern = 1;
bool search_case_sensitive = 1; bool search_case_sensitive = 1;
void UpdateFoundList(const SongList &v) string SearchEngineDisplayer(const std::pair<string, Song> &pair, void *null)
{
return pair.first == "." ? DisplaySong(pair.second) : pair.first;
}
void UpdateFoundList()
{ {
int i = search_engine_static_option;
bool bold = 0; bool bold = 0;
for (SongList::const_iterator it = v.begin(); it != v.end(); it++, i++) for (int i = search_engine_static_options-1; i < mSearcher->Size(); i++)
{ {
for (int j = 0; j < mPlaylist->Size(); j++) for (int j = 0; j < mPlaylist->Size(); j++)
{ {
if (mPlaylist->at(j).GetHash() == (*it)->GetHash()) if (mPlaylist->at(j).GetHash() == mSearcher->at(i).second.GetHash())
{ {
bold = 1; bold = 1;
break; break;
@@ -49,29 +53,29 @@ void UpdateFoundList(const SongList &v)
void PrepareSearchEngine(Song &s) void PrepareSearchEngine(Song &s)
{ {
// adding several empty songs may seem riddiculous, but they hardly steal memory
// and it's much cleaner solution than having two different containers imo
s.Clear(); s.Clear();
mSearcher->Clear(); mSearcher->Clear();
mSearcher->Reset(); mSearcher->Reset();
mSearcher->AddOption("[.b]Filename:[/b] " + s.GetShortFilename()); mSearcher->AddOption(std::pair<string, Song>("[.b]Filename:[/b] " + s.GetShortFilename(), Song()));
mSearcher->AddOption("[.b]Title:[/b] " + s.GetTitle()); mSearcher->AddOption(std::pair<string, Song>("[.b]Title:[/b] " + s.GetTitle(), Song()));
mSearcher->AddOption("[.b]Artist:[/b] " + s.GetArtist()); mSearcher->AddOption(std::pair<string, Song>("[.b]Artist:[/b] " + s.GetArtist(), Song()));
mSearcher->AddOption("[.b]Album:[/b] " + s.GetAlbum()); mSearcher->AddOption(std::pair<string, Song>("[.b]Album:[/b] " + s.GetAlbum(), Song()));
mSearcher->AddOption("[.b]Year:[/b] " + s.GetYear()); mSearcher->AddOption(std::pair<string, Song>("[.b]Year:[/b] " + s.GetYear(), Song()));
mSearcher->AddOption("[.b]Track:[/b] " + s.GetTrack()); mSearcher->AddOption(std::pair<string, Song>("[.b]Track:[/b] " + s.GetTrack(), Song()));
mSearcher->AddOption("[.b]Genre:[/b] " + s.GetGenre()); mSearcher->AddOption(std::pair<string, Song>("[.b]Genre:[/b] " + s.GetGenre(), Song()));
mSearcher->AddOption("[.b]Comment:[/b] " + s.GetComment()); mSearcher->AddOption(std::pair<string, Song>("[.b]Comment:[/b] " + s.GetComment(), Song()));
mSearcher->AddSeparator(); mSearcher->AddSeparator();
mSearcher->AddOption("[.b]Search mode:[/b] " + (search_match_to_pattern ? search_mode_normal : search_mode_strict)); mSearcher->AddOption(std::pair<string, Song>("[.b]Search mode:[/b] " + (search_match_to_pattern ? search_mode_normal : search_mode_strict), Song()));
mSearcher->AddOption("[.b]Case sensitive:[/b] " + (string)(search_case_sensitive ? "Yes" : "No")); mSearcher->AddOption(std::pair<string, Song>("[.b]Case sensitive:[/b] " + (string)(search_case_sensitive ? "Yes" : "No"), Song()));
mSearcher->AddSeparator(); mSearcher->AddSeparator();
mSearcher->AddOption("Search"); mSearcher->AddOption(std::pair<string, Song>("Search", Song()));
mSearcher->AddOption("Reset"); mSearcher->AddOption(std::pair<string, Song>("Reset", Song()));
} }
void Search(SongList &result, Song &s) void Search(Song &s)
{ {
FreeSongList(result);
if (s.Empty()) if (s.Empty())
return; return;
@@ -182,11 +186,7 @@ void Search(SongList &result, Song &s)
} }
if (found) if (found)
{ mSearcher->AddOption(std::pair<string, Song>(".", **it));
Song *ss = new Song(**it);
result.push_back(ss);
}
found = 1; found = 1;
} }
FreeSongList(list); FreeSongList(list);

View File

@@ -24,9 +24,12 @@
#include "mpdpp.h" #include "mpdpp.h"
#include "ncmpcpp.h" #include "ncmpcpp.h"
void UpdateFoundList(const SongList &); const int search_engine_static_options = 17;
string SearchEngineDisplayer(const std::pair<string, Song> &, void * = NULL);
void UpdateFoundList();
void PrepareSearchEngine(Song &s); void PrepareSearchEngine(Song &s);
void Search(SongList &, Song &); void Search(Song &);
#endif #endif

View File

@@ -62,7 +62,7 @@ extern string mpd_db_updating;
extern NcmpcppScreen current_screen; extern NcmpcppScreen current_screen;
extern bool dont_change_now_playing; extern bool dont_change_now_playing;
extern bool allow_statusbar_unblock; extern bool allow_statusbar_unlock;
extern bool block_progressbar_update; extern bool block_progressbar_update;
extern bool block_statusbar_update; extern bool block_statusbar_update;
extern bool block_playlist_update; extern bool block_playlist_update;
@@ -109,9 +109,9 @@ void TraceMpdStatus()
lock_statusbar_delay = -1; lock_statusbar_delay = -1;
if (Config.statusbar_visibility) if (Config.statusbar_visibility)
block_statusbar_update = !allow_statusbar_unblock; block_statusbar_update = !allow_statusbar_unlock;
else else
block_progressbar_update = !allow_statusbar_unblock; block_progressbar_update = !allow_statusbar_unlock;
MPDStatusChanges changes; MPDStatusChanges changes;
switch (Mpd->GetState()) switch (Mpd->GetState())
@@ -224,7 +224,7 @@ void NcmpcppStatusChanged(MPDConnection *Mpd, MPDStatusChanges changed, void *da
} }
else if (current_screen == csSearcher && !block_found_item_list_update) else if (current_screen == csSearcher && !block_found_item_list_update)
{ {
UpdateFoundList(vSearched); UpdateFoundList();
} }
else if (current_screen == csLibrary) else if (current_screen == csLibrary)
{ {