1 /*
2  * urilist.c
3  * Copyright 2010-2011 John Lindgren
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions, and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  *    this list of conditions, and the following disclaimer in the documentation
13  *    provided with the distribution.
14  *
15  * This software is provided "as is" and without any warranty, express or
16  * implied. In no event shall the authors be liable for any damages arising from
17  * the use of this software.
18  */
19 
20 #include <string.h>
21 
22 #include <libaudcore/audstrings.h>
23 #include <libaudcore/drct.h>
24 #include <libaudcore/playlist.h>
25 
26 #include "libaudgui.h"
27 
check_uri(const char * name)28 static String check_uri (const char * name)
29 {
30     return strstr (name, "://") ? String (name) : String (filename_to_uri (name));
31 }
32 
urilist_to_index(const char * list)33 static Index<PlaylistAddItem> urilist_to_index (const char * list)
34 {
35     Index<PlaylistAddItem> index;
36     const char * end, * next;
37 
38     while (list[0])
39     {
40         if ((end = strchr (list, '\n')))
41         {
42             next = end + 1;
43             if (end > list && end[-1] == '\r')
44                 end --;
45         }
46         else
47             next = end = strchr (list, 0);
48 
49         if (end > list)
50             index.append (check_uri (str_copy (list, end - list)));
51 
52         list = next;
53     }
54 
55     return index;
56 }
57 
audgui_urilist_open(const char * list)58 EXPORT void audgui_urilist_open (const char * list)
59 {
60     aud_drct_pl_open_list (urilist_to_index (list));
61 }
62 
audgui_urilist_insert(Playlist playlist,int at,const char * list)63 EXPORT void audgui_urilist_insert (Playlist playlist, int at, const char * list)
64 {
65     playlist.insert_items (at, urilist_to_index (list), false);
66 }
67 
audgui_urilist_create_from_selected(Playlist playlist)68 EXPORT Index<char> audgui_urilist_create_from_selected (Playlist playlist)
69 {
70     playlist.cache_selected ();
71 
72     Index<char> buf;
73     int entries = playlist.n_entries ();
74 
75     for (int i = 0; i < entries; i ++)
76     {
77         if (playlist.entry_selected (i))
78         {
79             if (buf.len ())
80                 buf.append ('\n');
81 
82             String filename = playlist.entry_filename (i);
83             buf.insert (filename, -1, strlen (filename));
84         }
85     }
86 
87     return buf;
88 }
89