002ab46bbda43aac13b5e9ee5c807ff0b6705b0e
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 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.
18 */
20 #include "utils.h"
21 #include "options.h"
22 #include "charset.h"
24 #include <ctype.h>
25 #include <stdlib.h>
26 #include <string.h>
28 GList *
29 string_list_free(GList *string_list)
30 {
31 GList *list = g_list_first(string_list);
33 while (list) {
34 g_free(list->data);
35 list->data = NULL;
36 list = list->next;
37 }
39 g_list_free(string_list);
40 return NULL;
41 }
43 GList *
44 string_list_find(GList *string_list, const gchar *str)
45 {
46 GList *list = g_list_first(string_list);
48 while(list) {
49 if( strcmp(str, (gchar *) list->data) == 0 )
50 return list;
51 list = list->next;
52 }
53 return NULL;
54 }
56 GList *
57 string_list_remove(GList *string_list, const gchar *str)
58 {
59 GList *list = g_list_first(string_list);
61 while(list) {
62 if( strcmp(str, (gchar *) list->data) == 0 ) {
63 g_free(list->data);
64 list->data = NULL;
65 return g_list_delete_link(string_list, list);
66 }
67 list = list->next;
68 }
69 return list;
70 }
72 /* create a list suiteble for GCompletion from path */
73 GList *
74 gcmp_list_from_path(mpdclient_t *c, const gchar *path, GList *list, gint types)
75 {
76 guint i;
77 mpdclient_filelist_t *filelist;
79 if ((filelist = mpdclient_filelist_get(c, path)) == NULL)
80 return list;
82 for (i = 0; i < filelist_length(filelist); ++i) {
83 struct filelist_entry *entry = filelist_get(filelist, i);
84 mpd_InfoEntity *entity = entry ? entry->entity : NULL;
85 char *name = NULL;
87 if (entity && entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY &&
88 types & GCMP_TYPE_DIR) {
89 mpd_Directory *dir = entity->info.directory;
90 gchar *tmp = utf8_to_locale(dir->path);
91 gsize size = strlen(tmp)+2;
93 name = g_malloc(size);
94 g_strlcpy(name, tmp, size);
95 g_strlcat(name, "/", size);
96 g_free(tmp);
97 } else if (entity &&
98 entity->type == MPD_INFO_ENTITY_TYPE_SONG &&
99 types & GCMP_TYPE_FILE) {
100 mpd_Song *song = entity->info.song;
101 name = utf8_to_locale(song->file);
102 } else if (entity &&
103 entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE &&
104 types & GCMP_TYPE_PLAYLIST) {
105 mpd_PlaylistFile *plf = entity->info.playlistFile;
106 name = utf8_to_locale(plf->path);
107 }
109 if (name)
110 list = g_list_append(list, name);
111 }
113 filelist_free(filelist);
114 return list;
115 }