ListWindow: convert list_window_callback_fn_t to an abstract class
[ncmpc-debian.git] / src / AlbumListPage.cxx
index 4120801..b8b5a35 100644 (file)
@@ -26,7 +26,6 @@
 #include "i18n.h"
 #include "charset.hxx"
 #include "mpdclient.hxx"
-#include "filelist.hxx"
 
 #include <glib.h>
 
@@ -49,38 +48,23 @@ CompareUTF8(const std::string &a, const std::string &b)
        return n < 0;
 }
 
-/* list_window callback */
-static const char *
-album_lw_callback(unsigned idx, void *data)
-{
-       const auto &list = *(const std::vector<std::string> *)data;
-
-       assert(idx < list.size());
-
-       const char *str_utf8 = list[idx].c_str();
-       char *str = utf8_to_locale(str_utf8);
-
-       static char buf[BUFSIZE];
-       g_strlcpy(buf, str, sizeof(buf));
-       g_free(str);
-
-       return buf;
-}
-
-/* list_window callback */
-static const char *
-AlbumListCallback(unsigned idx, void *data)
+const char *
+AlbumListPage::GetListItemText(unsigned idx) const
 {
-       const auto &list = *(const std::vector<std::string> *)data;
-
        if (idx == 0)
                return "..";
-       else if (idx == list.size() + 1)
+       else if (idx == album_list.size() + 1)
                return _("All tracks");
 
        --idx;
 
-       return album_lw_callback(idx, data);
+       assert(idx < album_list.size());
+
+       const char *str_utf8 = album_list[idx].c_str();
+
+       static char buf[BUFSIZE];
+       g_strlcpy(buf, Utf8ToLocale(str_utf8).c_str(), sizeof(buf));
+       return buf;
 }
 
 static void
@@ -132,21 +116,20 @@ AlbumListPage::Reload(struct mpdclient &c)
  * the parent directory, and at the end, there's the item "All tracks"
  * to view the tracks of all albums.
  */
-static void
-paint_album_callback(WINDOW *w, unsigned i,
-                    gcc_unused unsigned y, unsigned width,
-                    bool selected, const void *data)
+void
+AlbumListPage::PaintListItem(WINDOW *w, unsigned i,
+                            gcc_unused unsigned y, unsigned width,
+                            bool selected) const
 {
-       const auto &list = *(const std::vector<std::string> *)data;
        const char *p;
        char *q = nullptr;
 
        if (i == 0)
                p = "..";
-       else if (i == list.size() + 1)
+       else if (i == album_list.size() + 1)
                p = _("All tracks");
        else
-               p = q = utf8_to_locale(list[i - 1].c_str());
+               p = q = utf8_to_locale(album_list[i - 1].c_str());
 
        screen_browser_paint_directory(w, width, selected, p);
        g_free(q);
@@ -155,7 +138,7 @@ paint_album_callback(WINDOW *w, unsigned i,
 void
 AlbumListPage::Paint() const
 {
-       lw.Paint(paint_album_callback, &album_list);
+       lw.Paint(*this);
 }
 
 const char *
@@ -164,9 +147,8 @@ AlbumListPage::GetTitle(char *str, size_t size) const
        if (artist.empty())
                return _("Albums");
 
-       char *s1 = utf8_to_locale(artist.c_str());
-       g_snprintf(str, size, _("Albums of artist: %s"), s1);
-       g_free(s1);
+       g_snprintf(str, size, _("Albums of artist: %s"),
+                  Utf8ToLocale(artist.c_str()).c_str());
        return str;
 }
 
@@ -194,27 +176,17 @@ add_query(struct mpdclient *c, enum mpd_tag_type table, const char *_filter,
        if (connection == nullptr)
                return;
 
-       char *str = utf8_to_locale(_filter);
-       if (table == MPD_TAG_ALBUM)
-               screen_status_printf(_("Adding album %s..."), str);
-       else
-               screen_status_printf(_("Adding %s..."), str);
-       g_free(str);
+       screen_status_printf(_("Adding \'%s\' to queue"),
+                            Utf8ToLocale(_filter).c_str());
 
-       mpd_search_db_songs(connection, true);
+       mpd_search_add_db_songs(connection, true);
        mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
                                      table, _filter);
        if (table == MPD_TAG_ALBUM)
                mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
                                              MPD_TAG_ARTIST, _artist);
        mpd_search_commit(connection);
-
-       auto *addlist = filelist_new_recv(connection);
-
-       if (mpdclient_finish_command(c))
-               mpdclient_filelist_add_all(c, addlist);
-
-       delete addlist;
+       mpdclient_finish_command(c);
 }
 
 bool
@@ -256,15 +228,12 @@ AlbumListPage::OnCommand(struct mpdclient &c, command_t cmd)
        case CMD_LIST_RFIND:
        case CMD_LIST_FIND_NEXT:
        case CMD_LIST_RFIND_NEXT:
-               screen_find(screen, &lw, cmd,
-                           AlbumListCallback, &album_list);
+               screen_find(screen, &lw, cmd, *this);
                SetDirty();
                return true;
 
        case CMD_LIST_JUMP:
-               screen_jump(screen, &lw,
-                           AlbumListCallback, &album_list,
-                           paint_album_callback, &album_list);
+               screen_jump(screen, &lw, *this, *this);
                SetDirty();
                return true;