Code

added type argument to gcmp_list_from_path()
[ncmpc.git] / src / utils.c
1 /* 
2  * $Id$
3  *
4  * (c) 2004 by Kalle Wallin <kaw@linux.se>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
21 #include <ctype.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <glib.h>
27 #include "config.h"
28 #include "ncmpc.h"
29 #include "options.h"
30 #include "support.h"
31 #include "mpdclient.h"
32 #include "utils.h"
35 GList *
36 string_list_free(GList *string_list)
37 {
38   GList *list = g_list_first(string_list);
40   while(list)
41     {
42       g_free(list->data);
43       list->data=NULL;
44       list=list->next;
45     }
46   g_list_free(string_list);
47   return NULL;
48 }
50 GList *
51 string_list_find(GList *string_list, gchar *str)
52 {
53   GList *list = g_list_first(string_list);
55   while(list)
56     {
57       if( strcmp(str, (gchar *) list->data) ==  0 )
58         return list;
59       list = list->next;
60     }
61   return NULL;
62 }
64 GList *
65 string_list_remove(GList *string_list, gchar *str)
66 {
67   GList *list = g_list_first(string_list);
69   while(list)
70     {
71       if( strcmp(str, (gchar *) list->data) ==  0 )
72         {
73           g_free(list->data);
74           list->data = NULL;
75           return g_list_delete_link(string_list, list);
76         }
77       list = list->next;
78     }
79   return list;
80 }
82 /* create a list suiteble for GCompletion from path */
83 GList *
84 gcmp_list_from_path(mpdclient_t *c, gchar *path, GList *list, gint types)
85 {
86   GList *flist = NULL;
87   mpdclient_filelist_t *filelist;
88   
89   if( (filelist=mpdclient_filelist_get(c, path)) == NULL )
90     return list;
91   D("retreived filelist!\n");
92   flist = filelist->list;
93   while( flist )
94     {
95       filelist_entry_t *entry = flist->data;
96       mpd_InfoEntity *entity = entry ? entry->entity : NULL;
97       char *name = NULL;
98       
99       if( entity && entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY && 
100           types & GCMP_TYPE_DIR) 
101         {
102           mpd_Directory *dir = entity->info.directory;
103           gchar *tmp = utf8_to_locale(dir->path);
104           
105           name = g_malloc(strlen(tmp)+2);
106           strcpy(name, tmp);
107           strcat(name, "/");
108           g_free(tmp);
109         }
110       else if( entity && entity->type==MPD_INFO_ENTITY_TYPE_SONG &&
111                types & GCMP_TYPE_FILE )
112         {
113           mpd_Song *song = entity->info.song;
114           name = utf8_to_locale(song->file);
115         }
116       else if( entity && entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE &&
117                types & GCMP_TYPE_PLAYLIST )
118         {
119           mpd_PlaylistFile *plf = entity->info.playlistFile;
120           name = utf8_to_locale(plf->path);
121         }
123       if( name )
124         list = g_list_append(list, name);
126       flist = flist->next;
127     }
128   mpdclient_filelist_free(filelist);
129   return list;