]> git.tuebingen.mpg.de Git - paraslash.git/blob - playlist.c
56e19fed307459fb7fd487bfa82c02db7c7b2f8b
[paraslash.git] / playlist.c
1 #include "para.h"
2 #include "error.h"
3 #include "string.h"
4 #include "afh.h"
5 #include "afs.h"
6
7 /** \file playlist.c Functions for loading and saving playlists. */
8
9 /** Structure used for adding entries to a playlist. */
10 struct playlist_info {
11         /** The name of the playlist. */
12         char *name;
13         /** The number of entries currently in the playlist. */
14         unsigned length;
15 };
16 static struct playlist_info current_playlist;
17
18 /**
19  * Re-insert an audio file into the tree of admissible files.
20  *
21  * \param aft_row Determines the audio file.
22  *
23  * \return The return value of score_update().
24  */
25 int playlist_update_audio_file(struct osl_row *aft_row)
26 {
27         /* always re-insert to the top of the tree */
28         return score_update(aft_row, 0);
29 }
30
31 static int add_playlist_entry(char *path, void *data)
32 {
33         struct playlist_info *playlist = data;
34         struct osl_row *aft_row;
35         int ret = aft_get_row_of_path(path, &aft_row);
36
37         if (ret < 0) {
38                 PARA_NOTICE_LOG("%s: %s\n", path, PARA_STRERROR(-ret));
39                 return 1;
40         }
41         ret = score_add(aft_row, -playlist->length);
42         if (ret < 0) {
43                 PARA_ERROR_LOG("failed to add %s: %s\n", path, PARA_STRERROR(-ret));
44                 return ret;
45         }
46         playlist->length++;
47         return 1;
48 }
49
50 /* returns -E_PLAYLIST_LOADED on _success_ to terminate the loop */
51 static int load_playlist(struct osl_row *row, void *data)
52 {
53         struct playlist_info *playlist = data;
54         struct osl_object playlist_def;
55         char *playlist_name;
56         int ret;
57
58         ret = pl_get_name_and_def_by_row(row, &playlist_name, &playlist_def);
59         if (ret < 0)
60                 goto err;
61         playlist->length = 0;
62         ret = for_each_line_ro(playlist_def.data, playlist_def.size,
63                 add_playlist_entry, playlist);
64         osl_close_disk_object(&playlist_def);
65         if (ret < 0)
66                 goto err;
67         ret = -E_PLAYLIST_EMPTY;
68         if (!playlist->length)
69                 goto err;
70         playlist->name = para_strdup(playlist_name);
71         PARA_NOTICE_LOG("loaded playlist %s (%u files)\n", playlist->name,
72                 playlist->length);
73         return -E_PLAYLIST_LOADED;
74 err:
75         if (ret != -E_DUMMY_ROW)
76                 PARA_NOTICE_LOG("unable to load playlist (%s)\n",
77                         PARA_STRERROR(-ret));
78         return 1;
79 }
80
81 static int load_first_available_playlist(struct playlist_info *playlist)
82 {
83         int ret = osl_rbtree_loop(playlists_table, BLOBCOL_NAME, playlist,
84                 load_playlist);
85         if (ret == -E_PLAYLIST_LOADED) /* success */
86                 return 1;
87         if (ret < 0)
88                 return ret; /* error */
89         PARA_NOTICE_LOG("no valid playlist found\n");
90         return -E_NO_PLAYLIST;
91 }
92
93 static int check_playlist_path(char *path, void *data)
94 {
95         struct para_buffer *pb = data;
96         struct osl_row *aft_row;
97         int ret = aft_get_row_of_path(path, &aft_row);
98
99         if (ret < 0)
100                 para_printf(pb, "%s: %s\n", path, PARA_STRERROR(-ret));
101         return 1;
102 }
103
104 static int check_playlist(struct osl_row *row, void *data)
105 {
106         struct para_buffer *pb = data;
107         struct osl_object playlist_def;
108         char *playlist_name;
109         int ret = pl_get_name_and_def_by_row(row, &playlist_name, &playlist_def);
110
111         if (ret < 0) {
112                 para_printf(pb, "failed to get playlist data: %s\n",
113                         PARA_STRERROR(-ret));
114                 return 1;
115         }
116         if (*playlist_name) { /* skip dummy row */
117                 para_printf(pb, "checking playlist %s...\n", playlist_name);
118                 for_each_line_ro(playlist_def.data, playlist_def.size,
119                         check_playlist_path, pb);
120         }
121         osl_close_disk_object(&playlist_def);
122         return 1;
123 }
124
125 /**
126  * Check the playlist table for inconsistencies.
127  *
128  * \param query Unused.
129  * \param result Contains messages about inconsistencies.
130  *
131  * \return The return value of the underlying call to osl_rbtree_loop().
132  */
133 int playlist_check_callback(__a_unused const struct osl_object *query,
134                 struct osl_object *result)
135 {
136         struct para_buffer pb = {.buf = NULL};
137
138         para_printf(&pb, "checking playlists...\n");
139         osl_rbtree_loop(playlists_table, BLOBCOL_ID, &pb,
140                 check_playlist);
141         result->data = pb.buf;
142         result->size = pb.size;
143         return 1;
144 }
145
146 /**
147  * Close the current playlist.
148  *
149  * \sa playlist_open().
150  */
151 void playlist_close(void)
152 {
153         free(current_playlist.name);
154         current_playlist.name = NULL;
155 }
156
157 /**
158  * Open the given playlist.
159  *
160  * \param name The name of the playlist to open.
161  *
162  * If name is \p NULL, load the first available playlist. Files which are
163  * listed in the playlist, but not contained in the database are ignored.
164  * This is not considered an error.
165  *
166  * \return Positive on success, negative on errors. Possible errors
167  * include: Given playlist not found, -E_NO_PLAYLIST (no playlist defined).
168  */
169 int playlist_open(char *name)
170 {
171         struct osl_object obj;
172         int ret;
173         struct osl_row *row;
174
175         if (!name)
176                 return load_first_available_playlist(&current_playlist);
177         obj.data = name;
178         obj.size = strlen(obj.data);
179         ret = osl_get_row(playlists_table, BLOBCOL_NAME, &obj, &row);
180         if (ret < 0) {
181                 PARA_NOTICE_LOG("failed to load playlist %s\n", name);
182                 return ret;
183         }
184         ret = load_playlist(row, &current_playlist);
185         return (ret == -E_PLAYLIST_LOADED)? 1 : ret;
186 }
187
188 static int search_path(char *path, void *data)
189 {
190         if (strcmp(path, data))
191                 return 1;
192         return -E_PATH_FOUND;
193 }
194
195 static int handle_audio_file_event(enum afs_events event, void *data)
196 {
197         int ret, was_admissible = 0, is_admissible;
198         struct osl_object playlist_def;
199         char *new_path;
200         const struct osl_row *row = data;
201
202         if (!current_playlist.name)
203                 return 1;
204         if (event == AUDIO_FILE_RENAME) {
205                 ret = row_belongs_to_score_table(row, NULL);
206                 if (ret < 0)
207                         return ret;
208                 was_admissible = ret;
209         }
210         ret = get_audio_file_path_of_row(row, &new_path);
211         if (ret < 0)
212                 return ret;
213         ret = pl_get_def_by_name(current_playlist.name, &playlist_def);
214         if (ret < 0)
215                 return ret;
216         ret = for_each_line_ro(playlist_def.data, playlist_def.size,
217                 search_path, new_path);
218         osl_close_disk_object(&playlist_def);
219         is_admissible = (ret < 0);
220         if (was_admissible && is_admissible)
221                 return 1;
222         if (!was_admissible && !is_admissible)
223                 return 1;
224         if (was_admissible && !is_admissible) {
225                 current_playlist.length--;
226                 return score_delete(row);
227         }
228         /* !was_admissible && is_admissible */
229         current_playlist.length++;
230         return score_add(row, 0); /* play it immediately */
231 }
232
233 int playlists_event_handler(enum afs_events event, struct para_buffer *pb,
234                 void *data)
235 {
236         int ret;
237
238         switch(event) {
239         case AUDIO_FILE_RENAME:
240         case AUDIO_FILE_ADD:
241                 return handle_audio_file_event(event, data);
242         case AUDIO_FILE_REMOVE:
243                 ret = row_belongs_to_score_table(data, NULL);
244                 if (ret < 0)
245                         return ret;
246                 if (!ret)
247                         return 1;
248                 current_playlist.length--;
249                 return score_delete(data);
250         default:
251                 return 1;
252         }
253 }