e90a7a0f7657cbd8eefdd131f8b31a85f9f21739
[ncmpc-debian.git] / src / SearchPage.cxx
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2020 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  *
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.
9  *
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.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 #include "SearchPage.hxx"
21 #include "PageMeta.hxx"
22 #include "screen_status.hxx"
23 #include "TextListRenderer.hxx"
24 #include "i18n.h"
25 #include "Options.hxx"
26 #include "Bindings.hxx"
27 #include "GlobalBindings.hxx"
28 #include "charset.hxx"
29 #include "mpdclient.hxx"
30 #include "screen_utils.hxx"
31 #include "FileListPage.hxx"
32 #include "filelist.hxx"
33 #include "util/Macros.hxx"
34
35 #include <string.h>
36
37 enum {
38         SEARCH_URI = MPD_TAG_COUNT + 100,
39         SEARCH_ARTIST_TITLE,
40 };
41
42 static constexpr struct {
43         enum mpd_tag_type tag_type;
44         const char *name;
45         const char *localname;
46 } search_tag[MPD_TAG_COUNT] = {
47         { MPD_TAG_ARTIST, "artist", N_("artist") },
48         { MPD_TAG_ALBUM, "album", N_("album") },
49         { MPD_TAG_TITLE, "title", N_("title") },
50         { MPD_TAG_TRACK, "track", N_("track") },
51         { MPD_TAG_NAME, "name", N_("name") },
52         { MPD_TAG_GENRE, "genre", N_("genre") },
53         { MPD_TAG_DATE, "date", N_("date") },
54         { MPD_TAG_COMPOSER, "composer", N_("composer") },
55         { MPD_TAG_PERFORMER, "performer", N_("performer") },
56         { MPD_TAG_COMMENT, "comment", N_("comment") },
57         { MPD_TAG_COUNT, nullptr, nullptr }
58 };
59
60 static int
61 search_get_tag_id(const char *name)
62 {
63         if (strcasecmp(name, "file") == 0 ||
64             strcasecmp(name, _("file")) == 0)
65                 return SEARCH_URI;
66
67         for (unsigned i = 0; search_tag[i].name != nullptr; ++i)
68                 if (strcasecmp(search_tag[i].name, name) == 0 ||
69                     strcasecmp(search_tag[i].localname, name) == 0)
70                         return search_tag[i].tag_type;
71
72         return -1;
73 }
74
75 struct SearchMode {
76         enum mpd_tag_type table;
77         const char *label;
78 };
79
80 static constexpr SearchMode mode[] = {
81         { MPD_TAG_TITLE, N_("Title") },
82         { MPD_TAG_ARTIST, N_("Artist") },
83         { MPD_TAG_ALBUM, N_("Album") },
84         { (enum mpd_tag_type)SEARCH_URI, N_("Filename") },
85         { (enum mpd_tag_type)SEARCH_ARTIST_TITLE, N_("Artist + Title") },
86         { MPD_TAG_COUNT, nullptr }
87 };
88
89 static const char *const help_text[] = {
90         "",
91         "",
92         "",
93         "Quick     -  Enter a string and ncmpc will search according",
94         "             to the current search mode (displayed above).",
95         "",
96         "Advanced  -  <tag>:<search term> [<tag>:<search term>...]",
97         "               Example: artist:radiohead album:pablo honey",
98         "",
99         "               Available tags: artist, album, title, track,",
100         "               name, genre, date composer, performer, comment, file",
101         "",
102 };
103
104 static bool advanced_search_mode = false;
105
106 class SearchPage final : public FileListPage {
107         History search_history;
108         std::string pattern;
109
110 public:
111         SearchPage(ScreenManager &_screen, WINDOW *_w, Size size)
112                 :FileListPage(_screen, _w, size,
113                               !options.search_format.empty()
114                               ? options.search_format.c_str()
115                               : options.list_format.c_str()) {
116                 lw.DisableCursor();
117                 lw.SetLength(ARRAY_SIZE(help_text));
118         }
119
120 private:
121         void Clear(bool clear_pattern);
122         void Reload(struct mpdclient &c);
123         void Start(struct mpdclient &c);
124
125 public:
126         /* virtual methods from class Page */
127         void Paint() const noexcept override;
128         void Update(struct mpdclient &c, unsigned events) noexcept override;
129         bool OnCommand(struct mpdclient &c, Command cmd) override;
130         const char *GetTitle(char *s, size_t size) const noexcept override;
131 };
132
133 /* search info */
134 class SearchHelpText final : public ListText {
135 public:
136         /* virtual methods from class ListText */
137         const char *GetListItemText(char *buffer, size_t size,
138                                     unsigned idx) const noexcept override {
139                 assert(idx < ARRAY_SIZE(help_text));
140
141                 if (idx == 0) {
142                         snprintf(buffer, size,
143                                  " %s : %s",
144                                  GetGlobalKeyBindings().GetKeyNames(Command::SCREEN_SEARCH).c_str(),
145                                  "New search");
146                         return buffer;
147                 }
148
149                 if (idx == 1) {
150                         snprintf(buffer, size,
151                                  " %s : %s [%s]",
152                                  GetGlobalKeyBindings().GetKeyNames(Command::SEARCH_MODE).c_str(),
153                                  get_key_description(Command::SEARCH_MODE),
154                                  gettext(mode[options.search_mode].label));
155                         return buffer;
156                 }
157
158                 return help_text[idx];
159         }
160 };
161
162 void
163 SearchPage::Clear(bool clear_pattern)
164 {
165         if (filelist) {
166                 delete filelist;
167                 filelist = new FileList();
168                 lw.SetLength(0);
169         }
170         if (clear_pattern)
171                 pattern.clear();
172
173         SetDirty();
174 }
175
176 static FileList *
177 search_simple_query(struct mpd_connection *connection, bool exact_match,
178                     int table, const char *local_pattern)
179 {
180         FileList *list;
181         const LocaleToUtf8 filter_utf8(local_pattern);
182
183         if (table == SEARCH_ARTIST_TITLE) {
184                 mpd_command_list_begin(connection, false);
185
186                 mpd_search_db_songs(connection, exact_match);
187                 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
188                                               MPD_TAG_ARTIST,
189                                               filter_utf8.c_str());
190                 mpd_search_commit(connection);
191
192                 mpd_search_db_songs(connection, exact_match);
193                 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
194                                               MPD_TAG_TITLE,
195                                               filter_utf8.c_str());
196                 mpd_search_commit(connection);
197
198                 mpd_command_list_end(connection);
199
200                 list = filelist_new_recv(connection);
201                 list->RemoveDuplicateSongs();
202         } else if (table == SEARCH_URI) {
203                 mpd_search_db_songs(connection, exact_match);
204                 mpd_search_add_uri_constraint(connection, MPD_OPERATOR_DEFAULT,
205                                               filter_utf8.c_str());
206                 mpd_search_commit(connection);
207
208                 list = filelist_new_recv(connection);
209         } else {
210                 mpd_search_db_songs(connection, exact_match);
211                 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
212                                               (enum mpd_tag_type)table,
213                                               filter_utf8.c_str());
214                 mpd_search_commit(connection);
215
216                 list = filelist_new_recv(connection);
217         }
218
219         return list;
220 }
221
222 /*-----------------------------------------------------------------------
223  * NOTE: This code exists to test a new search ui,
224  *       Its ugly and MUST be redesigned before the next release!
225  *-----------------------------------------------------------------------
226  */
227 static FileList *
228 search_advanced_query(struct mpd_connection *connection, const char *query)
229 {
230         advanced_search_mode = false;
231         if (strchr(query, ':') == nullptr)
232                 return nullptr;
233
234         std::string str(query);
235
236         static constexpr size_t N = 10;
237
238         char *tabv[N];
239         char *matchv[N];
240         int table[N];
241
242         /*
243          * Replace every : with a '\0' and every space character
244          * before it unless spi = -1, link the resulting strings
245          * to their proper vector.
246          */
247         int spi = -1;
248         size_t n = 0;
249         for (size_t i = 0; str[i] != '\0' && n < N; i++) {
250                 switch(str[i]) {
251                 case ' ':
252                         spi = i;
253                         continue;
254                 case ':':
255                         str[i] = '\0';
256                         if (spi != -1)
257                                 str[spi] = '\0';
258
259                         matchv[n] = &str[i + 1];
260                         tabv[n] = &str[spi + 1];
261                         table[n] = search_get_tag_id(tabv[n]);
262                         if (table[n] < 0) {
263                                 screen_status_printf(_("Bad search tag %s"),
264                                                      tabv[n]);
265                                 return nullptr;
266                         }
267
268                         ++n;
269                         /* FALLTHROUGH */
270                 default:
271                         continue;
272                 }
273         }
274
275         /* Get rid of obvious failure case */
276         if (matchv[n - 1][0] == '\0') {
277                 screen_status_printf(_("No argument for search tag %s"), tabv[n - 1]);
278                 return nullptr;
279         }
280
281         advanced_search_mode = true;
282
283         /*-----------------------------------------------------------------------
284          * NOTE (again): This code exists to test a new search ui,
285          *               Its ugly and MUST be redesigned before the next release!
286          *             + the code below should live in mpdclient.c
287          *-----------------------------------------------------------------------
288          */
289         /** stupid - but this is just a test...... (fulhack)  */
290         mpd_search_db_songs(connection, false);
291
292         for (size_t i = 0; i < n; i++) {
293                 const LocaleToUtf8 value(matchv[i]);
294
295                 if (table[i] == SEARCH_URI)
296                         mpd_search_add_uri_constraint(connection,
297                                                       MPD_OPERATOR_DEFAULT,
298                                                       value.c_str());
299                 else
300                         mpd_search_add_tag_constraint(connection,
301                                                       MPD_OPERATOR_DEFAULT,
302                                                       (enum mpd_tag_type)table[i],
303                                                       value.c_str());
304         }
305
306         mpd_search_commit(connection);
307         auto *fl = filelist_new_recv(connection);
308         if (!mpd_response_finish(connection)) {
309                 delete fl;
310                 fl = nullptr;
311         }
312
313         return fl;
314 }
315
316 static FileList *
317 do_search(struct mpdclient *c, const char *query)
318 {
319         auto *connection = c->GetConnection();
320         if (connection == nullptr)
321                 return nullptr;
322
323         auto *fl = search_advanced_query(connection, query);
324         if (fl != nullptr)
325                 return fl;
326
327         if (mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS) {
328                 c->HandleError();
329                 return nullptr;
330         }
331
332         fl = search_simple_query(connection, false,
333                                  mode[options.search_mode].table,
334                                  query);
335         if (fl == nullptr)
336                 c->HandleError();
337         return fl;
338 }
339
340 void
341 SearchPage::Reload(struct mpdclient &c)
342 {
343         if (pattern.empty())
344                 return;
345
346         lw.EnableCursor();
347         delete filelist;
348         filelist = do_search(&c, pattern.c_str());
349         if (filelist == nullptr)
350                 filelist = new FileList();
351         lw.SetLength(filelist->size());
352
353         screen_browser_sync_highlights(*filelist, c.playlist);
354
355         SetDirty();
356 }
357
358 void
359 SearchPage::Start(struct mpdclient &c)
360 {
361         if (!c.IsConnected())
362                 return;
363
364         Clear(true);
365
366         pattern = screen_readln(_("Search"),
367                                 nullptr,
368                                 &search_history,
369                                 nullptr);
370
371         if (pattern.empty()) {
372                 lw.Reset();
373                 return;
374         }
375
376         Reload(c);
377 }
378
379 static std::unique_ptr<Page>
380 screen_search_init(ScreenManager &_screen, WINDOW *w, Size size)
381 {
382         return std::make_unique<SearchPage>(_screen, w, size);
383 }
384
385 void
386 SearchPage::Paint() const noexcept
387 {
388         if (filelist) {
389                 FileListPage::Paint();
390         } else {
391                 lw.Paint(TextListRenderer(SearchHelpText()));
392         }
393 }
394
395 const char *
396 SearchPage::GetTitle(char *str, size_t size) const noexcept
397 {
398         if (advanced_search_mode && !pattern.empty())
399                 snprintf(str, size, "%s '%s'", _("Search"), pattern.c_str());
400         else if (!pattern.empty())
401                 snprintf(str, size,
402                          "%s '%s' [%s]",
403                          _("Search"),
404                          pattern.c_str(),
405                          gettext(mode[options.search_mode].label));
406         else
407                 return _("Search");
408
409         return str;
410 }
411
412 void
413 SearchPage::Update(struct mpdclient &c, unsigned events) noexcept
414 {
415         if (filelist != nullptr && events & MPD_IDLE_QUEUE) {
416                 screen_browser_sync_highlights(*filelist, c.playlist);
417                 SetDirty();
418         }
419 }
420
421 bool
422 SearchPage::OnCommand(struct mpdclient &c, Command cmd)
423 {
424         switch (cmd) {
425         case Command::SEARCH_MODE:
426                 options.search_mode++;
427                 if (mode[options.search_mode].label == nullptr)
428                         options.search_mode = 0;
429                 screen_status_printf(_("Search mode: %s"),
430                                      gettext(mode[options.search_mode].label));
431
432                 if (pattern.empty())
433                         /* show the new mode in the help text */
434                         SetDirty();
435                 else if (!advanced_search_mode)
436                         /* reload only if the new search mode is going
437                            to be considered */
438                         Reload(c);
439                 return true;
440
441         case Command::SCREEN_UPDATE:
442                 Reload(c);
443                 return true;
444
445         case Command::SCREEN_SEARCH:
446                 Start(c);
447                 return true;
448
449         case Command::CLEAR:
450                 Clear(true);
451                 lw.Reset();
452                 return true;
453
454         default:
455                 break;
456         }
457
458         if (FileListPage::OnCommand(c, cmd))
459                 return true;
460
461         return false;
462 }
463
464 const PageMeta screen_search = {
465         "search",
466         N_("Search"),
467         Command::SCREEN_SEARCH,
468         screen_search_init,
469 };