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 "AlbumListPage.hxx"
21 #include "screen_interface.hxx"
22 #include "screen_status.hxx"
23 #include "screen_find.hxx"
24 #include "screen_browser.hxx"
27 #include "charset.hxx"
28 #include "mpdclient.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 album_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();
61 static char buf[BUFSIZE];
62 g_strlcpy(buf, Utf8ToLocale(str_utf8).c_str(), sizeof(buf));
66 /* list_window callback */
68 AlbumListCallback(unsigned idx, void *data)
70 const auto &list = *(const std::vector<std::string> *)data;
74 else if (idx == list.size() + 1)
75 return _("All tracks");
79 return album_lw_callback(idx, data);
83 recv_tag_values(struct mpd_connection *connection, enum mpd_tag_type tag,
84 std::vector<std::string> &list)
86 struct mpd_pair *pair;
88 while ((pair = mpd_recv_pair_tag(connection, tag)) != nullptr) {
89 list.emplace_back(pair->value);
90 mpd_return_pair(connection, pair);
95 AlbumListPage::LoadAlbumList(struct mpdclient &c)
97 struct mpd_connection *connection = mpdclient_get_connection(&c);
101 if (connection != nullptr) {
102 mpd_search_db_tags(connection, MPD_TAG_ALBUM);
103 mpd_search_add_tag_constraint(connection,
104 MPD_OPERATOR_DEFAULT,
107 mpd_search_commit(connection);
109 recv_tag_values(connection, MPD_TAG_ALBUM, album_list);
111 mpdclient_finish_command(&c);
115 std::sort(album_list.begin(), album_list.end(), CompareUTF8);
116 lw.SetLength(album_list.size() + 2);
120 AlbumListPage::Reload(struct mpdclient &c)
126 * Paint one item in the album list. There are two virtual items
127 * inserted: at the beginning, there's the special item ".." to go to
128 * the parent directory, and at the end, there's the item "All tracks"
129 * to view the tracks of all albums.
132 AlbumListPage::PaintListItem(WINDOW *w, unsigned i,
133 gcc_unused unsigned y, unsigned width,
141 else if (i == album_list.size() + 1)
144 p = q = utf8_to_locale(album_list[i - 1].c_str());
146 screen_browser_paint_directory(w, width, selected, p);
151 AlbumListPage::Paint() const
157 AlbumListPage::GetTitle(char *str, size_t size) const
162 g_snprintf(str, size, _("Albums of artist: %s"),
163 Utf8ToLocale(artist.c_str()).c_str());
168 AlbumListPage::Update(struct mpdclient &c, unsigned events)
170 if (events & MPD_IDLE_DATABASE) {
171 /* the db has changed -> update the list */
177 /* add_query - Add all songs satisfying specified criteria.
178 _artist is actually only used in the ALBUM case to distinguish albums with
179 the same name from different artists. */
181 add_query(struct mpdclient *c, enum mpd_tag_type table, const char *_filter,
184 struct mpd_connection *connection = mpdclient_get_connection(c);
186 assert(_filter != nullptr);
188 if (connection == nullptr)
191 screen_status_printf(_("Adding \'%s\' to queue"),
192 Utf8ToLocale(_filter).c_str());
194 mpd_search_add_db_songs(connection, true);
195 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
197 if (table == MPD_TAG_ALBUM)
198 mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
199 MPD_TAG_ARTIST, _artist);
200 mpd_search_commit(connection);
201 mpdclient_finish_command(c);
205 AlbumListPage::OnCommand(struct mpdclient &c, command_t cmd)
208 const char *selected;
211 if (lw.selected == 0) {
213 screen.OnCommand(c, CMD_GO_PARENT_DIRECTORY);
221 for (const unsigned i : lw.GetRange()) {
222 if(i == album_list.size() + 1)
223 add_query(&c, MPD_TAG_ARTIST, artist.c_str(),
226 selected = album_list[lw.selected - 1].c_str();
227 add_query(&c, MPD_TAG_ALBUM, selected,
229 cmd = CMD_LIST_NEXT; /* continue and select next item... */
234 /* continue and update... */
235 case CMD_SCREEN_UPDATE:
241 case CMD_LIST_FIND_NEXT:
242 case CMD_LIST_RFIND_NEXT:
243 screen_find(screen, &lw, cmd,
244 AlbumListCallback, &album_list);
249 screen_jump(screen, &lw,
250 AlbumListCallback, &album_list,
259 if (lw.HandleCommand(cmd)) {