Code

utils: move gcmp_list_from_path() to db_completion.c
[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 #include <string.h>
26 GList *
27 gcmp_list_from_path(struct mpdclient *c, const gchar *path,
28                     GList *list, gint types)
29 {
30         struct mpd_connection *connection = mpdclient_get_connection(c);
31         if (connection == NULL)
32                 return list;
34         mpd_send_list_meta(connection, path);
36         struct mpd_entity *entity;
37         while ((entity = mpd_recv_entity(connection)) != NULL) {
38                 char *name;
40                 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_DIRECTORY &&
41                     types & GCMP_TYPE_DIR) {
42                         const struct mpd_directory *dir =
43                                 mpd_entity_get_directory(entity);
44                         gchar *tmp = utf8_to_locale(mpd_directory_get_path(dir));
45                         gsize size = strlen(tmp)+2;
47                         name = g_malloc(size);
48                         g_strlcpy(name, tmp, size);
49                         g_strlcat(name, "/", size);
50                         g_free(tmp);
51                 } else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG &&
52                            types & GCMP_TYPE_FILE) {
53                         const struct mpd_song *song =
54                                 mpd_entity_get_song(entity);
55                         name = utf8_to_locale(mpd_song_get_uri(song));
56                 } else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_PLAYLIST &&
57                            types & GCMP_TYPE_PLAYLIST) {
58                         const struct mpd_playlist *playlist =
59                                 mpd_entity_get_playlist(entity);
60                         name = utf8_to_locale(mpd_playlist_get_path(playlist));
61                 } else {
62                         mpd_entity_free(entity);
63                         continue;
64                 }
66                 list = g_list_append(list, name);
67                 mpd_entity_free(entity);
68         }
70         return list;
71 }