Code

db_completion: use g_strconcat()
[ncmpc.git] / src / db_completion.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2017 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  */
20 #include "db_completion.h"
21 #include "charset.h"
22 #include "mpdclient.h"
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 == NULL)
30                 return list;
32         mpd_send_list_meta(connection, path);
34         struct mpd_entity *entity;
35         while ((entity = mpd_recv_entity(connection)) != NULL) {
36                 char *name;
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                         gchar *tmp = utf8_to_locale(mpd_directory_get_path(dir));
43                         name = g_strconcat(tmp, "/", NULL);
44                         g_free(tmp);
45                 } else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG &&
46                            types & GCMP_TYPE_FILE) {
47                         const struct mpd_song *song =
48                                 mpd_entity_get_song(entity);
49                         name = utf8_to_locale(mpd_song_get_uri(song));
50                 } else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_PLAYLIST &&
51                            types & GCMP_TYPE_PLAYLIST) {
52                         const struct mpd_playlist *playlist =
53                                 mpd_entity_get_playlist(entity);
54                         name = utf8_to_locale(mpd_playlist_get_path(playlist));
55                 } else {
56                         mpd_entity_free(entity);
57                         continue;
58                 }
60                 list = g_list_append(list, name);
61                 mpd_entity_free(entity);
62         }
64         return list;
65 }