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