1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2018 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
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 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.
20 #include "ArtistListPage.hxx"
21 #include "screen_interface.hxx"
22 #include "screen_status.hxx"
23 #include "screen_find.hxx"
24 #include "screen_browser.hxx"
26 #include "charset.hxx"
27 #include "mpdclient.hxx"
28 #include "filelist.hxx"
41 CompareUTF8(const std::string &a, const std::string &b)
43 char *key1 = g_utf8_collate_key(a.c_str(), -1);
44 char *key2 = g_utf8_collate_key(b.c_str(), -1);
45 int n = strcmp(key1,key2);
51 /* list_window callback */
53 screen_artist_lw_callback(unsigned idx, void *data)
55 const auto &list = *(const std::vector<std::string> *)data;
57 assert(idx < list.size());
59 const char *str_utf8 = list[idx].c_str();
60 char *str = utf8_to_locale(str_utf8);
62 static char buf[BUFSIZE];
63 g_strlcpy(buf, str, sizeof(buf));
70 recv_tag_values(struct mpd_connection *connection, enum mpd_tag_type tag,
71 std::vector<std::string> &list)
73 struct mpd_pair *pair;
75 while ((pair = mpd_recv_pair_tag(connection, tag)) != nullptr) {
76 list.emplace_back(pair->value);
77 mpd_return_pair(connection, pair);
82 ArtistListPage::LoadArtistList(struct mpdclient &c)
84 struct mpd_connection *connection = mpdclient_get_connection(&c);
88 if (connection != nullptr) {
89 mpd_search_db_tags(connection, MPD_TAG_ARTIST);
90 mpd_search_commit(connection);
91 recv_tag_values(connection, MPD_TAG_ARTIST, artist_list);
93 mpdclient_finish_command(&c);
97 std::sort(artist_list.begin(), artist_list.end(), CompareUTF8);
98 lw.SetLength(artist_list.size());
102 ArtistListPage::Reload(struct mpdclient &c)
108 * Paint one item in the artist list.
111 paint_artist_callback(WINDOW *w, unsigned i,
112 gcc_unused unsigned y, unsigned width,
113 bool selected, const void *data)
115 const auto &list = *(const std::vector<std::string> *)data;
116 char *p = utf8_to_locale(list[i].c_str());
118 screen_browser_paint_directory(w, width, selected, p);
123 ArtistListPage::Paint() const
125 lw.Paint(paint_artist_callback, &artist_list);
129 ArtistListPage::GetTitle(char *, size_t) const
131 return _("All artists");
135 ArtistListPage::Update(struct mpdclient &c, unsigned events)
137 if (events & MPD_IDLE_DATABASE) {
138 /* the db has changed -> update the list */
144 /* add_query - Add all songs satisfying specified criteria.
145 _artist is actually only used in the ALBUM case to distinguish albums with
146 the same name from different artists. */
148 add_query(struct mpdclient *c, enum mpd_tag_type table, const char *_filter,
151 struct mpd_connection *connection = mpdclient_get_connection(c);
153 assert(_filter != nullptr);
155 if (connection == nullptr)
158 char *str = utf8_to_locale(_filter);
159 screen_status_printf(_("Adding \'%s\' to queue"), str);
162 mpd_search_db_songs(connection, true);
163 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
165 if (table == MPD_TAG_ALBUM)
166 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
167 MPD_TAG_ARTIST, _artist);
168 mpd_search_commit(connection);
170 auto *addlist = filelist_new_recv(connection);
172 if (mpdclient_finish_command(c))
173 mpdclient_filelist_add_all(c, addlist);
179 ArtistListPage::OnListCommand(command_t cmd)
181 if (lw.HandleCommand(cmd)) {
190 ArtistListPage::OnCommand(struct mpdclient &c, command_t cmd)
193 const char *selected;
197 if (lw.selected >= artist_list.size())
200 for (const unsigned i : lw.GetRange()) {
201 selected = artist_list[i].c_str();
202 add_query(&c, MPD_TAG_ARTIST, selected, nullptr);
203 cmd = CMD_LIST_NEXT; /* continue and select next item... */
208 /* continue and update... */
209 case CMD_SCREEN_UPDATE:
215 case CMD_LIST_FIND_NEXT:
216 case CMD_LIST_RFIND_NEXT:
217 screen_find(screen, &lw, cmd,
218 screen_artist_lw_callback, &artist_list);
223 screen_jump(screen, &lw,
224 screen_artist_lw_callback, &artist_list,
225 paint_artist_callback, &artist_list);
233 if (OnListCommand(cmd))