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