filelist: wrap in std::unique_ptr<>
[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                 filelist = std::make_unique<FileList>();
167                 lw.SetLength(0);
168         }
169         if (clear_pattern)
170                 pattern.clear();
171
172         SetDirty();
173 }
174
175 static std::unique_ptr<FileList>
176 search_simple_query(struct mpd_connection *connection, bool exact_match,
177                     int table, const char *local_pattern)
178 {
179         const LocaleToUtf8 filter_utf8(local_pattern);
180
181         if (table == SEARCH_ARTIST_TITLE) {
182                 mpd_command_list_begin(connection, false);
183
184                 mpd_search_db_songs(connection, exact_match);
185                 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
186                                               MPD_TAG_ARTIST,
187                                               filter_utf8.c_str());
188                 mpd_search_commit(connection);
189
190                 mpd_search_db_songs(connection, exact_match);
191                 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
192                                               MPD_TAG_TITLE,
193                                               filter_utf8.c_str());
194                 mpd_search_commit(connection);
195
196                 mpd_command_list_end(connection);
197
198                 auto list = filelist_new_recv(connection);
199                 list->RemoveDuplicateSongs();
200                 return list;
201         } else if (table == SEARCH_URI) {
202                 mpd_search_db_songs(connection, exact_match);
203                 mpd_search_add_uri_constraint(connection, MPD_OPERATOR_DEFAULT,
204                                               filter_utf8.c_str());
205                 mpd_search_commit(connection);
206
207                 return filelist_new_recv(connection);
208         } else {
209                 mpd_search_db_songs(connection, exact_match);
210                 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
211                                               (enum mpd_tag_type)table,
212                                               filter_utf8.c_str());
213                 mpd_search_commit(connection);
214
215                 return filelist_new_recv(connection);
216         }
217 }
218
219 /*-----------------------------------------------------------------------
220  * NOTE: This code exists to test a new search ui,
221  *       Its ugly and MUST be redesigned before the next release!
222  *-----------------------------------------------------------------------
223  */
224 static std::unique_ptr<FileList>
225 search_advanced_query(struct mpd_connection *connection, const char *query)
226 {
227         advanced_search_mode = false;
228         if (strchr(query, ':') == nullptr)
229                 return nullptr;
230
231         std::string str(query);
232
233         static constexpr size_t N = 10;
234
235         char *tabv[N];
236         char *matchv[N];
237         int table[N];
238
239         /*
240          * Replace every : with a '\0' and every space character
241          * before it unless spi = -1, link the resulting strings
242          * to their proper vector.
243          */
244         int spi = -1;
245         size_t n = 0;
246         for (size_t i = 0; str[i] != '\0' && n < N; i++) {
247                 switch(str[i]) {
248                 case ' ':
249                         spi = i;
250                         continue;
251                 case ':':
252                         str[i] = '\0';
253                         if (spi != -1)
254                                 str[spi] = '\0';
255
256                         matchv[n] = &str[i + 1];
257                         tabv[n] = &str[spi + 1];
258                         table[n] = search_get_tag_id(tabv[n]);
259                         if (table[n] < 0) {
260                                 screen_status_printf(_("Bad search tag %s"),
261                                                      tabv[n]);
262                                 return nullptr;
263                         }
264
265                         ++n;
266                         /* FALLTHROUGH */
267                 default:
268                         continue;
269                 }
270         }
271
272         /* Get rid of obvious failure case */
273         if (matchv[n - 1][0] == '\0') {
274                 screen_status_printf(_("No argument for search tag %s"), tabv[n - 1]);
275                 return nullptr;
276         }
277
278         advanced_search_mode = true;
279
280         /*-----------------------------------------------------------------------
281          * NOTE (again): This code exists to test a new search ui,
282          *               Its ugly and MUST be redesigned before the next release!
283          *             + the code below should live in mpdclient.c
284          *-----------------------------------------------------------------------
285          */
286         /** stupid - but this is just a test...... (fulhack)  */
287         mpd_search_db_songs(connection, false);
288
289         for (size_t i = 0; i < n; i++) {
290                 const LocaleToUtf8 value(matchv[i]);
291
292                 if (table[i] == SEARCH_URI)
293                         mpd_search_add_uri_constraint(connection,
294                                                       MPD_OPERATOR_DEFAULT,
295                                                       value.c_str());
296                 else
297                         mpd_search_add_tag_constraint(connection,
298                                                       MPD_OPERATOR_DEFAULT,
299                                                       (enum mpd_tag_type)table[i],
300                                                       value.c_str());
301         }
302
303         mpd_search_commit(connection);
304         auto fl = filelist_new_recv(connection);
305         if (!mpd_response_finish(connection))
306                 fl.reset();
307
308         return fl;
309 }
310
311 static std::unique_ptr<FileList>
312 do_search(struct mpdclient *c, const char *query)
313 {
314         auto *connection = c->GetConnection();
315         if (connection == nullptr)
316                 return nullptr;
317
318         auto fl = search_advanced_query(connection, query);
319         if (fl != nullptr)
320                 return fl;
321
322         if (mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS) {
323                 c->HandleError();
324                 return nullptr;
325         }
326
327         fl = search_simple_query(connection, false,
328                                  mode[options.search_mode].table,
329                                  query);
330         if (fl == nullptr)
331                 c->HandleError();
332         return fl;
333 }
334
335 void
336 SearchPage::Reload(struct mpdclient &c)
337 {
338         if (pattern.empty())
339                 return;
340
341         lw.EnableCursor();
342         filelist = do_search(&c, pattern.c_str());
343         if (filelist == nullptr)
344                 filelist = std::make_unique<FileList>();
345         lw.SetLength(filelist->size());
346
347         screen_browser_sync_highlights(*filelist, c.playlist);
348
349         SetDirty();
350 }
351
352 void
353 SearchPage::Start(struct mpdclient &c)
354 {
355         if (!c.IsConnected())
356                 return;
357
358         Clear(true);
359
360         pattern = screen_readln(_("Search"),
361                                 nullptr,
362                                 &search_history,
363                                 nullptr);
364
365         if (pattern.empty()) {
366                 lw.Reset();
367                 return;
368         }
369
370         Reload(c);
371 }
372
373 static std::unique_ptr<Page>
374 screen_search_init(ScreenManager &_screen, WINDOW *w, Size size)
375 {
376         return std::make_unique<SearchPage>(_screen, w, size);
377 }
378
379 void
380 SearchPage::Paint() const noexcept
381 {
382         if (filelist) {
383                 FileListPage::Paint();
384         } else {
385                 lw.Paint(TextListRenderer(SearchHelpText()));
386         }
387 }
388
389 const char *
390 SearchPage::GetTitle(char *str, size_t size) const noexcept
391 {
392         if (advanced_search_mode && !pattern.empty())
393                 snprintf(str, size, "%s '%s'", _("Search"), pattern.c_str());
394         else if (!pattern.empty())
395                 snprintf(str, size,
396                          "%s '%s' [%s]",
397                          _("Search"),
398                          pattern.c_str(),
399                          gettext(mode[options.search_mode].label));
400         else
401                 return _("Search");
402
403         return str;
404 }
405
406 void
407 SearchPage::Update(struct mpdclient &c, unsigned events) noexcept
408 {
409         if (filelist != nullptr && events & MPD_IDLE_QUEUE) {
410                 screen_browser_sync_highlights(*filelist, c.playlist);
411                 SetDirty();
412         }
413 }
414
415 bool
416 SearchPage::OnCommand(struct mpdclient &c, Command cmd)
417 {
418         switch (cmd) {
419         case Command::SEARCH_MODE:
420                 options.search_mode++;
421                 if (mode[options.search_mode].label == nullptr)
422                         options.search_mode = 0;
423                 screen_status_printf(_("Search mode: %s"),
424                                      gettext(mode[options.search_mode].label));
425
426                 if (pattern.empty())
427                         /* show the new mode in the help text */
428                         SetDirty();
429                 else if (!advanced_search_mode)
430                         /* reload only if the new search mode is going
431                            to be considered */
432                         Reload(c);
433                 return true;
434
435         case Command::SCREEN_UPDATE:
436                 Reload(c);
437                 return true;
438
439         case Command::SCREEN_SEARCH:
440                 Start(c);
441                 return true;
442
443         case Command::CLEAR:
444                 Clear(true);
445                 lw.Reset();
446                 return true;
447
448         default:
449                 break;
450         }
451
452         if (FileListPage::OnCommand(c, cmd))
453                 return true;
454
455         return false;
456 }
457
458 const PageMeta screen_search = {
459         "search",
460         N_("Search"),
461         Command::SCREEN_SEARCH,
462         screen_search_init,
463 };