ListWindow: convert list_window_callback_fn_t to an abstract class
[ncmpc-debian.git] / src / screen_search.cxx
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2018 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 "screen_search.hxx"
21 #include "screen_interface.hxx"
22 #include "screen_status.hxx"
23 #include "TextListRenderer.hxx"
24 #include "i18n.h"
25 #include "options.hxx"
26 #include "charset.hxx"
27 #include "mpdclient.hxx"
28 #include "strfsong.hxx"
29 #include "screen_utils.hxx"
30 #include "screen_browser.hxx"
31 #include "filelist.hxx"
32
33 #include <glib.h>
34
35 #include <string.h>
36
37 enum {
38         SEARCH_URI = MPD_TAG_COUNT + 100,
39         SEARCH_ARTIST_TITLE
40 };
41
42 static const 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 (g_ascii_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 typedef struct {
76         enum mpd_tag_type table;
77         const char *label;
78 } search_type_t;
79
80 static search_type_t 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         "Quick     -  Enter a string and ncmpc will search according",
91         "             to the current search mode (displayed above).",
92         "",
93         "Advanced  -  <tag>:<search term> [<tag>:<search term>...]",
94         "               Example: artist:radiohead album:pablo honey",
95         "",
96         "               Available tags: artist, album, title, track,",
97         "               name, genre, date composer, performer, comment, file",
98         "",
99 };
100
101 static bool advanced_search_mode = false;
102
103 class SearchPage final : public FileListPage {
104         History search_history;
105         std::string pattern;
106
107 public:
108         SearchPage(ScreenManager &_screen, WINDOW *_w, Size size)
109                 :FileListPage(_screen, _w, size,
110                               options.search_format != nullptr
111                               ? options.search_format
112                               : options.list_format) {
113                 lw.SetLength(G_N_ELEMENTS(help_text));
114                 lw.hide_cursor = true;
115         }
116
117 private:
118         void Clear(bool clear_pattern);
119         void Reload(struct mpdclient &c);
120         void Start(struct mpdclient &c);
121
122 public:
123         /* virtual methods from class Page */
124         void OnOpen(struct mpdclient &c) override;
125         void Paint() const override;
126         void Update(struct mpdclient &c, unsigned events) override;
127         bool OnCommand(struct mpdclient &c, command_t cmd) override;
128         const char *GetTitle(char *s, size_t size) const override;
129 };
130
131 /* search info */
132 class SearchHelpText final : public ListText {
133 public:
134         /* virtual methods from class ListText */
135         const char *GetListItemText(unsigned idx) const override {
136                 assert(idx < G_N_ELEMENTS(help_text));
137
138                 return help_text[idx];
139         }
140 };
141
142 void
143 SearchPage::Clear(bool clear_pattern)
144 {
145         if (filelist) {
146                 delete filelist;
147                 filelist = new FileList();
148                 lw.SetLength(0);
149         }
150         if (clear_pattern)
151                 pattern.clear();
152
153         SetDirty();
154 }
155
156 static FileList *
157 search_simple_query(struct mpd_connection *connection, bool exact_match,
158                     int table, const char *local_pattern)
159 {
160         FileList *list;
161         const LocaleToUtf8 filter_utf8(local_pattern);
162
163         if (table == SEARCH_ARTIST_TITLE) {
164                 mpd_command_list_begin(connection, false);
165
166                 mpd_search_db_songs(connection, exact_match);
167                 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
168                                               MPD_TAG_ARTIST,
169                                               filter_utf8.c_str());
170                 mpd_search_commit(connection);
171
172                 mpd_search_db_songs(connection, exact_match);
173                 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
174                                               MPD_TAG_TITLE,
175                                               filter_utf8.c_str());
176                 mpd_search_commit(connection);
177
178                 mpd_command_list_end(connection);
179
180                 list = filelist_new_recv(connection);
181                 list->RemoveDuplicateSongs();
182         } else if (table == SEARCH_URI) {
183                 mpd_search_db_songs(connection, exact_match);
184                 mpd_search_add_uri_constraint(connection, MPD_OPERATOR_DEFAULT,
185                                               filter_utf8.c_str());
186                 mpd_search_commit(connection);
187
188                 list = filelist_new_recv(connection);
189         } else {
190                 mpd_search_db_songs(connection, exact_match);
191                 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
192                                               (enum mpd_tag_type)table,
193                                               filter_utf8.c_str());
194                 mpd_search_commit(connection);
195
196                 list = filelist_new_recv(connection);
197         }
198
199         return list;
200 }
201
202 /*-----------------------------------------------------------------------
203  * NOTE: This code exists to test a new search ui,
204  *       Its ugly and MUST be redesigned before the next release!
205  *-----------------------------------------------------------------------
206  */
207 static FileList *
208 search_advanced_query(struct mpd_connection *connection, const char *query)
209 {
210         advanced_search_mode = false;
211         if (strchr(query, ':') == nullptr)
212                 return nullptr;
213
214         int i, j;
215         char *str = g_strdup(query);
216
217         char *tabv[10];
218         char *matchv[10];
219         int table[10];
220         char *arg[10];
221
222         memset(tabv, 0, 10 * sizeof(char *));
223         memset(matchv, 0, 10 * sizeof(char *));
224         memset(arg, 0, 10 * sizeof(char *));
225
226         for (i = 0; i < 10; i++)
227                 table[i] = -1;
228
229         /*
230          * Replace every : with a '\0' and every space character
231          * before it unless spi = -1, link the resulting strings
232          * to their proper vector.
233          */
234         int spi = -1;
235         j = 0;
236         for (i = 0; str[i] != '\0' && j < 10; i++) {
237                 switch(str[i]) {
238                 case ' ':
239                         spi = i;
240                         continue;
241                 case ':':
242                         str[i] = '\0';
243                         if (spi != -1)
244                                 str[spi] = '\0';
245
246                         matchv[j] = str + i + 1;
247                         tabv[j] = str + spi + 1;
248                         j++;
249                         /* FALLTHROUGH */
250                 default:
251                         continue;
252                 }
253         }
254
255         /* Get rid of obvious failure case */
256         if (matchv[j - 1][0] == '\0') {
257                 screen_status_printf(_("No argument for search tag %s"), tabv[j - 1]);
258                 g_free(str);
259                 return nullptr;
260         }
261
262         int id = j = i = 0;
263         while (matchv[i] && matchv[i][0] != '\0' && i < 10) {
264                 id = search_get_tag_id(tabv[i]);
265                 if (id == -1) {
266                         screen_status_printf(_("Bad search tag %s"), tabv[i]);
267                 } else {
268                         table[j] = id;
269                         arg[j] = locale_to_utf8(matchv[i]);
270                         j++;
271                         advanced_search_mode = true;
272                 }
273
274                 i++;
275         }
276
277         g_free(str);
278
279         if (!advanced_search_mode || j == 0) {
280                 for (i = 0; arg[i] != nullptr; ++i)
281                         g_free(arg[i]);
282                 return nullptr;
283         }
284
285         /*-----------------------------------------------------------------------
286          * NOTE (again): This code exists to test a new search ui,
287          *               Its ugly and MUST be redesigned before the next release!
288          *             + the code below should live in mpdclient.c
289          *-----------------------------------------------------------------------
290          */
291         /** stupid - but this is just a test...... (fulhack)  */
292         mpd_search_db_songs(connection, false);
293
294         for (i = 0; i < 10 && arg[i] != nullptr; i++) {
295                 if (table[i] == SEARCH_URI)
296                         mpd_search_add_uri_constraint(connection,
297                                                       MPD_OPERATOR_DEFAULT,
298                                                       arg[i]);
299                 else
300                         mpd_search_add_tag_constraint(connection,
301                                                       MPD_OPERATOR_DEFAULT,
302                                                       (enum mpd_tag_type)table[i], arg[i]);
303         }
304
305         mpd_search_commit(connection);
306         auto *fl = filelist_new_recv(connection);
307         if (!mpd_response_finish(connection)) {
308                 delete fl;
309                 fl = nullptr;
310         }
311
312         for (i = 0; arg[i] != nullptr; ++i)
313                 g_free(arg[i]);
314
315         return fl;
316 }
317
318 static FileList *
319 do_search(struct mpdclient *c, const char *query)
320 {
321         struct mpd_connection *connection = mpdclient_get_connection(c);
322         if (connection == nullptr)
323                 return nullptr;
324
325         auto *fl = search_advanced_query(connection, query);
326         if (fl != nullptr)
327                 return fl;
328
329         if (mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS) {
330                 mpdclient_handle_error(c);
331                 return nullptr;
332         }
333
334         fl = search_simple_query(connection, false,
335                                  mode[options.search_mode].table,
336                                  query);
337         if (fl == nullptr)
338                 mpdclient_handle_error(c);
339         return fl;
340 }
341
342 void
343 SearchPage::Reload(struct mpdclient &c)
344 {
345         if (pattern.empty())
346                 return;
347
348         lw.hide_cursor = false;
349         delete filelist;
350         filelist = do_search(&c, pattern.c_str());
351         if (filelist == nullptr)
352                 filelist = new FileList();
353         lw.SetLength(filelist->size());
354
355         screen_browser_sync_highlights(filelist, &c.playlist);
356
357         SetDirty();
358 }
359
360 void
361 SearchPage::Start(struct mpdclient &c)
362 {
363         if (!c.IsConnected())
364                 return;
365
366         Clear(true);
367
368         pattern = screen_readln(_("Search"),
369                                 nullptr,
370                                 &search_history,
371                                 nullptr);
372
373         if (pattern.empty()) {
374                 lw.Reset();
375                 return;
376         }
377
378         Reload(c);
379 }
380
381 static Page *
382 screen_search_init(ScreenManager &_screen, WINDOW *w, Size size)
383 {
384         return new SearchPage(_screen, w, size);
385 }
386
387 void
388 SearchPage::OnOpen(gcc_unused struct mpdclient &c)
389 {
390         //  if( pattern==nullptr )
391         //    search_new(screen, c);
392         // else
393         screen_status_printf(_("Press %s for a new search"),
394                              get_key_names(CMD_SCREEN_SEARCH, false));
395 }
396
397 void
398 SearchPage::Paint() const
399 {
400         if (filelist) {
401                 FileListPage::Paint();
402         } else {
403                 lw.Paint(TextListRenderer(SearchHelpText()));
404         }
405 }
406
407 const char *
408 SearchPage::GetTitle(char *str, size_t size) const
409 {
410         if (advanced_search_mode && !pattern.empty())
411                 g_snprintf(str, size, _("Search: %s"), pattern.c_str());
412         else if (!pattern.empty())
413                 g_snprintf(str, size,
414                            _("Search: Results for %s [%s]"),
415                            pattern.c_str(),
416                            _(mode[options.search_mode].label));
417         else
418                 g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
419                            get_key_names(CMD_SCREEN_SEARCH, false),
420                            _(mode[options.search_mode].label));
421
422         return str;
423 }
424
425 void
426 SearchPage::Update(struct mpdclient &c, unsigned events)
427 {
428         if (filelist != nullptr && events & MPD_IDLE_QUEUE) {
429                 screen_browser_sync_highlights(filelist, &c.playlist);
430                 SetDirty();
431         }
432 }
433
434 bool
435 SearchPage::OnCommand(struct mpdclient &c, command_t cmd)
436 {
437         switch (cmd) {
438         case CMD_SEARCH_MODE:
439                 options.search_mode++;
440                 if (mode[options.search_mode].label == nullptr)
441                         options.search_mode = 0;
442                 screen_status_printf(_("Search mode: %s"),
443                                      _(mode[options.search_mode].label));
444                 /* fall through */
445         case CMD_SCREEN_UPDATE:
446                 Reload(c);
447                 return true;
448
449         case CMD_SCREEN_SEARCH:
450                 Start(c);
451                 return true;
452
453         case CMD_CLEAR:
454                 Clear(true);
455                 lw.Reset();
456                 return true;
457
458         default:
459                 break;
460         }
461
462         if (FileListPage::OnCommand(c, cmd))
463                 return true;
464
465         return false;
466 }
467
468 const struct screen_functions screen_search = {
469         "search",
470         screen_search_init,
471 };