1 /***************************************************************************
2 * Copyright (C) 2008-2014 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
22 #include <boost/range/detail/any_iterator.hpp>
28 #include "menu_impl.h"
30 #include "search_engine.h"
33 #include "statusbar.h"
34 #include "helpers/song_iterator_maker.h"
35 #include "utility/comparators.h"
37 #include "screen_switcher.h"
39 using Global::MainHeight
;
40 using Global::MainStartY
;
42 namespace ph
= std::placeholders
;
44 SearchEngine
*mySearcher
;
48 /*const std::array<const std::string, 11> constraintsNames = {{
62 const std::array<const char *, 3> searchModes = {{
63 "Match if tag contains searched phrase (no regexes)",
64 "Match if tag contains searched phrase (regexes supported)",
65 "Match only if both values are the same"
69 const size_t searchIn = constraintsNames.size()-1+1+1; // separated
70 const size_t searchMode = searchIn+1;
71 const size_t search = searchMode+1+1; // separated
72 const size_t reset = search+1;
75 std::string
SEItemToString(const SEItem
&ei
);
76 bool SEItemEntryMatcher(const Regex::Regex
&rx
, const NC::Menu
<SEItem
>::Item
&item
, bool filter
);
81 typedef SongExtractor type
;
83 typedef typename
NC::Menu
<SEItem
>::Item MenuItem
;
84 typedef typename
std::conditional
<Const
, const MenuItem
, MenuItem
>::type Item
;
85 typedef typename
std::conditional
<Const
, const MPD::Song
, MPD::Song
>::type Song
;
87 Song
*operator()(Item
&item
) const
90 if (!item
.isSeparator() && item
.value().isSong())
91 ptr
= &item
.value().song();
98 SongIterator
SearchEngineWindow::currentS()
100 return makeSongIterator_
<SEItem
>(current(), SongExtractor
<false>());
103 ConstSongIterator
SearchEngineWindow::currentS() const
105 return makeConstSongIterator_
<SEItem
>(current(), SongExtractor
<true>());
108 SongIterator
SearchEngineWindow::beginS()
110 return makeSongIterator_
<SEItem
>(begin(), SongExtractor
<false>());
113 ConstSongIterator
SearchEngineWindow::beginS() const
115 return makeConstSongIterator_
<SEItem
>(begin(), SongExtractor
<true>());
118 SongIterator
SearchEngineWindow::endS()
120 return makeSongIterator_
<SEItem
>(end(), SongExtractor
<false>());
123 ConstSongIterator
SearchEngineWindow::endS() const
125 return makeConstSongIterator_
<SEItem
>(end(), SongExtractor
<true>());
128 std::vector
<MPD::Song
> SearchEngineWindow::getSelectedSongs()
130 std::vector
<MPD::Song
> result
;
131 for (auto &item
: *this)
133 if (item
.isSelected())
135 assert(item
.value().isSong());
136 result
.push_back(item
.value().song());
139 // If no item is selected, add the current one if it's a song.
140 if (result
.empty() && !empty() && current()->value().isSong())
141 result
.push_back(current()->value().song());
145 /**********************************************************************/
147 const char *SearchEngine::ConstraintsNames
[] =
162 const char *SearchEngine::SearchModes
[] =
164 "Match if tag contains searched phrase (no regexes)",
165 "Match if tag contains searched phrase (regexes supported)",
166 "Match only if both values are the same",
170 size_t SearchEngine::StaticOptions
= 20;
171 size_t SearchEngine::ResetButton
= 16;
172 size_t SearchEngine::SearchButton
= 15;
174 SearchEngine::SearchEngine()
175 : Screen(NC::Menu
<SEItem
>(0, MainStartY
, COLS
, MainHeight
, "", Config
.main_color
, NC::Border()))
177 w
.setHighlightColor(Config
.main_highlight_color
);
178 w
.cyclicScrolling(Config
.use_cyclic_scrolling
);
179 w
.centeredCursor(Config
.centered_cursor
);
180 w
.setItemDisplayer(std::bind(Display::SEItems
, ph::_1
, std::cref(w
)));
181 w
.setSelectedPrefix(Config
.selected_item_prefix
);
182 w
.setSelectedSuffix(Config
.selected_item_suffix
);
183 SearchMode
= &SearchModes
[Config
.search_engine_default_search_mode
];
186 void SearchEngine::resize()
188 size_t x_offset
, width
;
189 getWindowResizeParams(x_offset
, width
);
190 w
.resize(width
, MainHeight
);
191 w
.moveTo(x_offset
, MainStartY
);
192 switch (Config
.search_engine_display_mode
)
194 case DisplayMode::Columns
:
195 if (Config
.titles_visibility
)
197 w
.setTitle(Display::Columns(w
.getWidth()));
200 case DisplayMode::Classic
:
206 void SearchEngine::switchTo()
208 SwitchTo::execute(this);
211 markSongsInPlaylist(w
);
215 std::wstring
SearchEngine::title()
217 return L
"Search engine";
220 void SearchEngine::mouseButtonPressed(MEVENT me
)
222 if (w
.empty() || !w
.hasCoords(me
.x
, me
.y
) || size_t(me
.y
) >= w
.size())
224 if (me
.bstate
& (BUTTON1_PRESSED
| BUTTON3_PRESSED
))
229 if ((me
.bstate
& BUTTON3_PRESSED
)
230 && w
.choice() < StaticOptions
)
232 else if (w
.choice() >= StaticOptions
)
234 bool play
= me
.bstate
& BUTTON3_PRESSED
;
235 addItemToPlaylist(play
);
239 Screen
<WindowType
>::mouseButtonPressed(me
);
242 /***********************************************************************/
244 bool SearchEngine::allowsSearching()
246 return w
.rbegin()->value().isSong();
249 const std::string
&SearchEngine::searchConstraint()
251 return m_search_predicate
.constraint();
254 void SearchEngine::setSearchConstraint(const std::string
&constraint
)
256 m_search_predicate
= Regex::ItemFilter
<SEItem
>(
259 std::bind(SEItemEntryMatcher
, ph::_1
, ph::_2
, false));
262 void SearchEngine::clearSearchConstraint()
264 m_search_predicate
.clear();
267 bool SearchEngine::search(SearchDirection direction
, bool wrap
, bool skip_current
)
269 return ::search(w
, m_search_predicate
, direction
, wrap
, skip_current
);
272 /***********************************************************************/
274 bool SearchEngine::actionRunnable()
276 return !w
.empty() && !w
.current()->value().isSong();
279 void SearchEngine::runAction()
281 size_t option
= w
.choice();
282 if (option
> ConstraintsNumber
&& option
< SearchButton
)
283 w
.current()->value().buffer().clear();
285 if (option
< ConstraintsNumber
)
287 Statusbar::ScopedLock slock
;
288 std::string constraint
= ConstraintsNames
[option
];
289 Statusbar::put() << NC::Format::Bold
<< constraint
<< NC::Format::NoBold
<< ": ";
290 itsConstraints
[option
] = Global::wFooter
->prompt(itsConstraints
[option
]);
291 w
.current()->value().buffer().clear();
292 constraint
.resize(13, ' ');
293 w
.current()->value().buffer() << NC::Format::Bold
<< constraint
<< NC::Format::NoBold
<< ": ";
294 ShowTag(w
.current()->value().buffer(), itsConstraints
[option
]);
296 else if (option
== ConstraintsNumber
+1)
298 Config
.search_in_db
= !Config
.search_in_db
;
299 w
.current()->value().buffer() << NC::Format::Bold
<< "Search in:" << NC::Format::NoBold
<< ' ' << (Config
.search_in_db
? "Database" : "Current playlist");
301 else if (option
== ConstraintsNumber
+2)
304 SearchMode
= &SearchModes
[0];
305 w
.current()->value().buffer() << NC::Format::Bold
<< "Search mode:" << NC::Format::NoBold
<< ' ' << *SearchMode
;
307 else if (option
== SearchButton
)
309 Statusbar::print("Searching...");
310 if (w
.size() > StaticOptions
)
313 if (w
.rbegin()->value().isSong())
315 if (Config
.search_engine_display_mode
== DisplayMode::Columns
)
316 w
.setTitle(Config
.titles_visibility
? Display::Columns(w
.getWidth()) : "");
317 size_t found
= w
.size()-SearchEngine::StaticOptions
;
318 found
+= 3; // don't count options inserted below
319 w
.insertSeparator(ResetButton
+1);
320 w
.insertItem(ResetButton
+2, SEItem(), NC::List::Properties::Bold
| NC::List::Properties::Inactive
);
321 w
.at(ResetButton
+2).value().mkBuffer() << Config
.color1
<< "Search results: " << Config
.color2
<< "Found " << found
<< (found
> 1 ? " songs" : " song") << NC::Color::Default
;
322 w
.insertSeparator(ResetButton
+3);
323 markSongsInPlaylist(w
);
324 Statusbar::print("Searching finished");
325 if (Config
.block_search_constraints_change
)
326 for (size_t i
= 0; i
< StaticOptions
-4; ++i
)
327 w
.at(i
).setInactive(true);
328 w
.scroll(NC::Scroll::Down
);
329 w
.scroll(NC::Scroll::Down
);
332 Statusbar::print("No results found");
334 else if (option
== ResetButton
)
339 addSongToPlaylist(w
.current()->value().song(), true);
342 /***********************************************************************/
344 bool SearchEngine::itemAvailable()
346 return !w
.empty() && w
.current()->value().isSong();
349 bool SearchEngine::addItemToPlaylist(bool play
)
351 return addSongToPlaylist(w
.current()->value().song(), play
);
354 std::vector
<MPD::Song
> SearchEngine::getSelectedSongs()
356 return w
.getSelectedSongs();
359 /***********************************************************************/
361 void SearchEngine::Prepare()
365 w
.resizeList(StaticOptions
-3);
368 item
.setSelectable(false);
370 w
.at(ConstraintsNumber
).setSeparator(true);
371 w
.at(SearchButton
-1).setSeparator(true);
373 for (size_t i
= 0; i
< ConstraintsNumber
; ++i
)
375 std::string constraint
= ConstraintsNames
[i
];
376 constraint
.resize(13, ' ');
377 w
[i
].value().mkBuffer() << NC::Format::Bold
<< constraint
<< NC::Format::NoBold
<< ": ";
378 ShowTag(w
[i
].value().buffer(), itsConstraints
[i
]);
381 w
.at(ConstraintsNumber
+1).value().mkBuffer() << NC::Format::Bold
<< "Search in:" << NC::Format::NoBold
<< ' ' << (Config
.search_in_db
? "Database" : "Current playlist");
382 w
.at(ConstraintsNumber
+2).value().mkBuffer() << NC::Format::Bold
<< "Search mode:" << NC::Format::NoBold
<< ' ' << *SearchMode
;
384 w
.at(SearchButton
).value().mkBuffer() << "Search";
385 w
.at(ResetButton
).value().mkBuffer() << "Reset";
388 void SearchEngine::reset()
390 for (size_t i
= 0; i
< ConstraintsNumber
; ++i
)
391 itsConstraints
[i
].clear();
394 Statusbar::print("Search state reset");
397 void SearchEngine::Search()
399 bool constraints_empty
= 1;
400 for (size_t i
= 0; i
< ConstraintsNumber
; ++i
)
402 if (!itsConstraints
[i
].empty())
404 constraints_empty
= 0;
408 if (constraints_empty
)
411 if (Config
.search_in_db
&& (SearchMode
== &SearchModes
[0] || SearchMode
== &SearchModes
[2])) // use built-in mpd searching
413 Mpd
.StartSearch(SearchMode
== &SearchModes
[2]);
414 if (!itsConstraints
[0].empty())
415 Mpd
.AddSearchAny(itsConstraints
[0]);
416 if (!itsConstraints
[1].empty())
417 Mpd
.AddSearch(MPD_TAG_ARTIST
, itsConstraints
[1]);
418 if (!itsConstraints
[2].empty())
419 Mpd
.AddSearch(MPD_TAG_ALBUM_ARTIST
, itsConstraints
[2]);
420 if (!itsConstraints
[3].empty())
421 Mpd
.AddSearch(MPD_TAG_TITLE
, itsConstraints
[3]);
422 if (!itsConstraints
[4].empty())
423 Mpd
.AddSearch(MPD_TAG_ALBUM
, itsConstraints
[4]);
424 if (!itsConstraints
[5].empty())
425 Mpd
.AddSearchURI(itsConstraints
[5]);
426 if (!itsConstraints
[6].empty())
427 Mpd
.AddSearch(MPD_TAG_COMPOSER
, itsConstraints
[6]);
428 if (!itsConstraints
[7].empty())
429 Mpd
.AddSearch(MPD_TAG_PERFORMER
, itsConstraints
[7]);
430 if (!itsConstraints
[8].empty())
431 Mpd
.AddSearch(MPD_TAG_GENRE
, itsConstraints
[8]);
432 if (!itsConstraints
[9].empty())
433 Mpd
.AddSearch(MPD_TAG_DATE
, itsConstraints
[9]);
434 if (!itsConstraints
[10].empty())
435 Mpd
.AddSearch(MPD_TAG_COMMENT
, itsConstraints
[10]);
436 for (MPD::SongIterator s
= Mpd
.CommitSearchSongs(), end
; s
!= end
; ++s
)
437 w
.addItem(std::move(*s
));
441 Regex::Regex rx
[ConstraintsNumber
];
442 if (SearchMode
!= &SearchModes
[2]) // match to pattern
444 for (size_t i
= 0; i
< ConstraintsNumber
; ++i
)
446 if (!itsConstraints
[i
].empty())
450 rx
[i
] = Regex::make(itsConstraints
[i
], Config
.regex_type
);
452 catch (boost::bad_expression
&) { }
457 typedef boost::range_detail::any_iterator
<
459 boost::single_pass_traversal_tag
,
462 > input_song_iterator
;
463 input_song_iterator s
, end
;
464 if (Config
.search_in_db
)
466 s
= input_song_iterator(getDatabaseIterator(Mpd
));
467 end
= input_song_iterator(MPD::SongIterator());
471 s
= input_song_iterator(myPlaylist
->main().beginV());
472 end
= input_song_iterator(myPlaylist
->main().endV());
475 LocaleStringComparison
cmp(std::locale(), Config
.ignore_leading_the
);
476 for (; s
!= end
; ++s
)
478 bool any_found
= true, found
= true;
480 if (SearchMode
!= &SearchModes
[2]) // match to pattern
484 Regex::search(s
->getArtist(), rx
[0])
485 || Regex::search(s
->getAlbumArtist(), rx
[0])
486 || Regex::search(s
->getTitle(), rx
[0])
487 || Regex::search(s
->getAlbum(), rx
[0])
488 || Regex::search(s
->getName(), rx
[0])
489 || Regex::search(s
->getComposer(), rx
[0])
490 || Regex::search(s
->getPerformer(), rx
[0])
491 || Regex::search(s
->getGenre(), rx
[0])
492 || Regex::search(s
->getDate(), rx
[0])
493 || Regex::search(s
->getComment(), rx
[0]);
494 if (found
&& !rx
[1].empty())
495 found
= Regex::search(s
->getArtist(), rx
[1]);
496 if (found
&& !rx
[2].empty())
497 found
= Regex::search(s
->getAlbumArtist(), rx
[2]);
498 if (found
&& !rx
[3].empty())
499 found
= Regex::search(s
->getTitle(), rx
[3]);
500 if (found
&& !rx
[4].empty())
501 found
= Regex::search(s
->getAlbum(), rx
[4]);
502 if (found
&& !rx
[5].empty())
503 found
= Regex::search(s
->getName(), rx
[5]);
504 if (found
&& !rx
[6].empty())
505 found
= Regex::search(s
->getComposer(), rx
[6]);
506 if (found
&& !rx
[7].empty())
507 found
= Regex::search(s
->getPerformer(), rx
[7]);
508 if (found
&& !rx
[8].empty())
509 found
= Regex::search(s
->getGenre(), rx
[8]);
510 if (found
&& !rx
[9].empty())
511 found
= Regex::search(s
->getDate(), rx
[9]);
512 if (found
&& !rx
[10].empty())
513 found
= Regex::search(s
->getComment(), rx
[10]);
515 else // match only if values are equal
517 if (!itsConstraints
[0].empty())
519 !cmp(s
->getArtist(), itsConstraints
[0])
520 || !cmp(s
->getAlbumArtist(), itsConstraints
[0])
521 || !cmp(s
->getTitle(), itsConstraints
[0])
522 || !cmp(s
->getAlbum(), itsConstraints
[0])
523 || !cmp(s
->getName(), itsConstraints
[0])
524 || !cmp(s
->getComposer(), itsConstraints
[0])
525 || !cmp(s
->getPerformer(), itsConstraints
[0])
526 || !cmp(s
->getGenre(), itsConstraints
[0])
527 || !cmp(s
->getDate(), itsConstraints
[0])
528 || !cmp(s
->getComment(), itsConstraints
[0]);
530 if (found
&& !itsConstraints
[1].empty())
531 found
= !cmp(s
->getArtist(), itsConstraints
[1]);
532 if (found
&& !itsConstraints
[2].empty())
533 found
= !cmp(s
->getAlbumArtist(), itsConstraints
[2]);
534 if (found
&& !itsConstraints
[3].empty())
535 found
= !cmp(s
->getTitle(), itsConstraints
[3]);
536 if (found
&& !itsConstraints
[4].empty())
537 found
= !cmp(s
->getAlbum(), itsConstraints
[4]);
538 if (found
&& !itsConstraints
[5].empty())
539 found
= !cmp(s
->getName(), itsConstraints
[5]);
540 if (found
&& !itsConstraints
[6].empty())
541 found
= !cmp(s
->getComposer(), itsConstraints
[6]);
542 if (found
&& !itsConstraints
[7].empty())
543 found
= !cmp(s
->getPerformer(), itsConstraints
[7]);
544 if (found
&& !itsConstraints
[8].empty())
545 found
= !cmp(s
->getGenre(), itsConstraints
[8]);
546 if (found
&& !itsConstraints
[9].empty())
547 found
= !cmp(s
->getDate(), itsConstraints
[9]);
548 if (found
&& !itsConstraints
[10].empty())
549 found
= !cmp(s
->getComment(), itsConstraints
[10]);
552 if (any_found
&& found
)
559 std::string
SEItemToString(const SEItem
&ei
)
564 switch (Config
.search_engine_display_mode
)
566 case DisplayMode::Classic
:
567 result
= Format::stringify
<char>(Config
.song_list_format
, &ei
.song());
569 case DisplayMode::Columns
:
570 result
= Format::stringify
<char>(Config
.song_columns_mode_format
, &ei
.song());
575 result
= ei
.buffer().str();
579 bool SEItemEntryMatcher(const Regex::Regex
&rx
, const NC::Menu
<SEItem
>::Item
&item
, bool filter
)
581 if (item
.isSeparator() || !item
.value().isSong())
583 return Regex::search(SEItemToString(item
.value()), rx
);