9153bf1f849246379e68b4605c749474fce2c656
[paraslash.git] / playlist.c
1 #include "para.h"
2 #include "error.h"
3 #include "afs.h"
4 #include "string.h"
5
6 /** \file playlist.c Functions for loading and saving playlists. */
7
8 /** Structure used for adding entries to a playlist. */
9 struct playlist_info {
10         /** The name of the playlist. */
11         char *name;
12         /** The number of entries currently in the playlist. */
13         unsigned length;
14 };
15 static struct playlist_info playlist;
16
17 /**
18  * Re-insert an audio file into the tree of admissible files.
19  *
20  * \param aft_row Determines the audio file.
21  *
22  * \return The return value of score_update().
23  */
24 int playlist_update_audio_file(struct osl_row *aft_row)
25 {
26         /* always re-insert to the top of the tree */
27         return score_update(aft_row, 0);
28 }
29
30 static int add_playlist_entry(char *line, void *private_data)
31 {
32         struct osl_row *aft_row;
33         struct playlist_info *pli = private_data;
34         int ret = aft_get_row_of_path(line, &aft_row);
35
36         if (ret < 0) {
37                 PARA_NOTICE_LOG("path not found in audio file table: %s\n",
38                         line);
39                 return 1;
40         }
41         ret = score_add(aft_row, -pli->length);
42         if (ret < 0) {
43                 PARA_ERROR_LOG("failed to add %s: %d\n", line, ret);
44                 return ret;
45         }
46         pli->length++;
47         return 1;
48 }
49
50 static int load_playlist(struct osl_row *row)
51 {
52         struct osl_object obj;
53         int ret;
54
55         ret = osl_get_object(playlists_table, row, BLOBCOL_NAME, &obj);
56         if (ret < 0)
57                 return ret;
58         playlist.name = para_strdup(obj.data);
59         playlist.length = 0;
60         ret = osl_open_disk_object(playlists_table, row, BLOBCOL_DEF, &obj);
61         if (ret < 0)
62                 goto err;
63         ret = for_each_line_ro(obj.data, obj.size, add_playlist_entry,
64                 &playlist);
65         osl_close_disk_object(&obj);
66         if (ret < 0)
67                 goto err;
68         ret = -E_PLAYLIST_EMPTY;
69         if (!playlist.length)
70                 goto err;
71         PARA_NOTICE_LOG("loaded playlist %s (%u files)\n", playlist.name,
72                 playlist.length);
73         return 1;
74 err:
75         free(playlist.name);
76         return ret;
77 }
78
79 /* returns -E_PLAYLIST_LOADED on _success_ to terminate the loop */
80 static int playlist_loop(struct osl_row *row, __a_unused void *private_data)
81 {
82         int ret = load_playlist(row);
83         if (ret < 0) {
84                 if (ret != -E_DUMMY_ROW)
85                         PARA_NOTICE_LOG("unable to load playlist, trying next\n");
86                 return 1;
87         }
88         return -E_PLAYLIST_LOADED;
89 }
90
91 static int load_first_available_playlist(void)
92 {
93         int ret = osl_rbtree_loop(playlists_table, BLOBCOL_NAME, NULL,
94                 playlist_loop);
95         if (ret == -E_PLAYLIST_LOADED) /* success */
96                 return 1;
97         if (ret < 0)
98                 return ret; /* error */
99         PARA_NOTICE_LOG("no valid playlist found\n");
100         return -E_NO_PLAYLIST;
101 }
102
103 /**
104  * Close the current playlist.
105  *
106  * \sa playlist_open().
107  */
108 void playlist_close(void)
109 {
110         free(playlist.name);
111         playlist.name = NULL;
112 }
113
114 /**
115  * Open the given playlist.
116  *
117  * \param name The name of the playlist to open.
118  *
119  * If name is \p NULL, load the first available playlist. Files which are
120  * listed in the playlist, but not contained in the database are ignored.
121  * This is not considered an error.
122  *
123  * \return Positive on success, negative on errors. Possible errors
124  * include: Given playlist not found, -E_NO_PLAYLIST (no playlist defined).
125  */
126 int playlist_open(char *name)
127 {
128         struct osl_object obj;
129         int ret;
130         struct osl_row *row;
131
132         if (!name)
133                 return load_first_available_playlist();
134         obj.data = name;
135         obj.size = strlen(obj.data);
136         ret = osl_get_row(playlists_table, BLOBCOL_NAME, &obj, &row);
137         if (ret < 0) {
138                 PARA_NOTICE_LOG("failed to load playlist %s\n", name);
139                 return ret;
140         }
141         return load_playlist(row);
142 }