charset: add class Utf8ToLocale
[ncmpc-debian.git] / src / db_completion.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 "db_completion.hxx"
21 #include "charset.hxx"
22 #include "mpdclient.hxx"
23
24 GList *
25 gcmp_list_from_path(struct mpdclient *c, const gchar *path,
26                     GList *list, gint types)
27 {
28         struct mpd_connection *connection = mpdclient_get_connection(c);
29         if (connection == nullptr)
30                 return list;
31
32         mpd_send_list_meta(connection, path);
33
34         struct mpd_entity *entity;
35         while ((entity = mpd_recv_entity(connection)) != nullptr) {
36                 char *name;
37
38                 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_DIRECTORY &&
39                     types & GCMP_TYPE_DIR) {
40                         const struct mpd_directory *dir =
41                                 mpd_entity_get_directory(entity);
42                         name = g_strconcat(Utf8ToLocale(mpd_directory_get_path(dir)).c_str(),
43                                            "/", nullptr);
44                 } else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG &&
45                            types & GCMP_TYPE_FILE) {
46                         const struct mpd_song *song =
47                                 mpd_entity_get_song(entity);
48                         name = utf8_to_locale(mpd_song_get_uri(song));
49                 } else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_PLAYLIST &&
50                            types & GCMP_TYPE_PLAYLIST) {
51                         const struct mpd_playlist *playlist =
52                                 mpd_entity_get_playlist(entity);
53                         name = utf8_to_locale(mpd_playlist_get_path(playlist));
54                 } else {
55                         mpd_entity_free(entity);
56                         continue;
57                 }
58
59                 list = g_list_append(list, name);
60                 mpd_entity_free(entity);
61         }
62
63         return list;
64 }