plugin: convert plugin_fd_add() to method
[ncmpc-debian.git] / src / db_completion.cxx
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2019 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 "db_completion.hxx"
21 #include "Completion.hxx"
22 #include "charset.hxx"
23 #include "mpdclient.hxx"
24 #include "util/ScopeExit.hxx"
25
26 #include <string>
27
28 void
29 gcmp_list_from_path(struct mpdclient *c, const char *path,
30                     Completion &completion,
31                     int types)
32 {
33         auto *connection = c->GetConnection();
34         if (connection == nullptr)
35                 return;
36
37         mpd_send_list_meta(connection, path);
38
39         struct mpd_entity *entity;
40         while ((entity = mpd_recv_entity(connection)) != nullptr) {
41                 AtScopeExit(entity) { mpd_entity_free(entity); };
42
43                 std::string name;
44                 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_DIRECTORY &&
45                     types & GCMP_TYPE_DIR) {
46                         const struct mpd_directory *dir =
47                                 mpd_entity_get_directory(entity);
48                         name = Utf8ToLocale(mpd_directory_get_path(dir)).c_str();
49                         name.push_back('/');
50                 } else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG &&
51                            types & GCMP_TYPE_FILE) {
52                         const struct mpd_song *song =
53                                 mpd_entity_get_song(entity);
54                         name = Utf8ToLocale(mpd_song_get_uri(song)).c_str();
55                 } else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_PLAYLIST &&
56                            types & GCMP_TYPE_PLAYLIST) {
57                         const struct mpd_playlist *playlist =
58                                 mpd_entity_get_playlist(entity);
59                         name = Utf8ToLocale(mpd_playlist_get_path(playlist)).c_str();
60                 } else {
61                         continue;
62                 }
63
64                 completion.emplace(std::move(name));
65         }
66 }