2 Copyright (C) 2006 by Jonas Kramer
3 Published under the terms of the GNU General Public License (GPL).
14 #include <sys/types.h>
27 #include "interface.h"
32 int expand(struct playlist
* list
) {
33 struct hash p
= { 0, NULL
};
38 set(& p
, "discovery", (!!enabled(DISCOVERY
)) ? "true" : "false");
39 response
= rest("radio.getPlaylist", & p
);
41 if(response
!= NULL
) {
42 int result
= parse_playlist(list
, response
);
51 void trim(char * string
){
53 while(string
[offset
] == ' ')
57 memmove(string
, string
+ offset
, strlen(string
+ offset
) + 1);
61 int parse_playlist(struct playlist
* list
, char * plain_json
) {
62 json_value
* playlist
, * track_array
, * track
, * extension
;
66 assert(plain_json
!= NULL
);
68 playlist
= json_parse(plain_json
);
70 assert(playlist
!= NULL
);
72 track_array
= json_query(playlist
, "playlist", "trackList", "track", NULL
);
74 assert(track_array
!= NULL
);
76 for(n
= 0; n
< track_array
->u
.array
.length
; ++n
) {
77 struct tracknode
* node
= NULL
;
80 node
= malloc(sizeof(struct tracknode
));
83 memset(node
, 0, sizeof(struct tracknode
));
86 track
= track_array
->u
.array
.values
[n
];
87 extension
= json_query(track
, "extension", NULL
);
89 json_hash(track
, & node
->track
, NULL
);
90 json_hash(extension
, & node
->track
, NULL
);
92 if((duration
= strdup(value(& node
->track
, "duration"))) != NULL
) {
93 duration
[strlen(duration
) - 3] = 0;
94 set(& node
->track
, "duration", duration
);
101 set(& node
->track
, "station", list
->title
);
104 set(& node
->track
, "station", "Unknown Station");
109 debug("track location: %s\n", value(& node
->track
, "location"));
112 json_value_free(playlist
);
118 void freelist(struct playlist
* list
) {
119 if(list
->title
!= NULL
)
125 memset(list
, 0, sizeof(struct playlist
));
129 void push(struct playlist
* list
, struct tracknode
* node
) {
133 struct tracknode
* last
= list
->track
;
134 while(last
->next
!= NULL
)
143 void shift(struct playlist
* list
) {
145 struct tracknode
* node
= list
->track
;
146 list
->track
= node
->next
;
147 empty(& node
->track
);
153 void preview(struct playlist list
) {
154 struct tracknode
* node
;
157 if (list
.track
!= NULL
)
158 node
= list
.track
->next
;
161 puts("No tracks in queue.");
166 puts("No tracks in queue.");
169 puts("Upcoming tracks:");
170 while(node
!= NULL
) {
173 format
= haskey(& rc
, "preview-format")
174 ? value(& rc
, "preview-format")
177 printf("%2d %s\n", n
++, meta(format
, M_COLORED
, & node
->track
));