Merge topic branch t/ff-compat into master
[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_instance {
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_instance 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_instance *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 afs_callback_arg *aca = data;
61 struct osl_row *aft_row;
62 int ret = aft_get_row_of_path(path, &aft_row);
63
64 if (ret < 0)
65 afs_error(aca, "%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 afs_callback_arg *aca = data;
72 struct para_buffer *pb = &aca->pbout;
73 struct osl_object playlist_def;
74 char *playlist_name;
75 int ret = pl_get_name_and_def_by_row(row, &playlist_name, &playlist_def);
76
77 if (ret < 0) { /* log error, but continue */
78 afs_error(aca, "failed to get playlist data: %s\n",
79 para_strerror(-ret));
80 return 1;
81 }
82 if (*playlist_name) { /* skip dummy row */
83 para_printf(pb, "checking playlist %s...\n", playlist_name);
84 for_each_line(FELF_READ_ONLY, playlist_def.data,
85 playlist_def.size, check_playlist_path, aca);
86 }
87 osl_close_disk_object(&playlist_def);
88 return 1;
89 }
90
91 /**
92 * Check the playlist table for inconsistencies.
93 *
94 * \param aca This callback ignores ->query.
95 *
96 * \return Standard. Invalid paths are reported, but are not considered an
97 * error.
98 */
99 int playlist_check_callback(struct afs_callback_arg *aca)
100 {
101 para_printf(&aca->pbout, "checking playlists...\n");
102 return osl(osl_rbtree_loop(playlists_table, BLOBCOL_ID, aca,
103 check_playlist));
104 }
105
106 /** Free all resources of the current playlist, if any. */
107 void playlist_unload(void)
108 {
109 if (!current_playlist.name)
110 return;
111 free(current_playlist.name);
112 current_playlist.name = NULL;
113 current_playlist.length = 0;
114 }
115
116 /**
117 * Populate the score table from the paths of a playlist database object.
118 *
119 * This loads the blob object which corresponds to the given name from the
120 * playlist table. Each line of the blob is regarded as a path which is looked
121 * up in the audio file table. If the path lookup succeeds, a reference to the
122 * corresponding row of the audio file table is added to the score table.
123 *
124 * \param name The name of the playlist to load.
125 * \param msg Error message or playlist info is returned here.
126 *
127 * \return The length of the loaded playlist on success, negative error code
128 * else. Files which are listed in the playlist, but are not contained in the
129 * database are ignored. This is not considered an error.
130 */
131 int playlist_load(const char *name, char **msg)
132 {
133 int ret;
134 struct playlist_instance *playlist = &current_playlist;
135 struct osl_object playlist_def;
136
137 ret = pl_get_def_by_name(name, &playlist_def);
138 if (ret < 0) {
139 *msg = make_message("could not read playlist %s\n", name);
140 return ret;
141 }
142 playlist_unload();
143 ret = for_each_line(FELF_READ_ONLY, playlist_def.data,
144 playlist_def.size, add_playlist_entry, playlist);
145 osl_close_disk_object(&playlist_def);
146 if (ret < 0)
147 goto err;
148 ret = -E_PLAYLIST_EMPTY;
149 if (!playlist->length)
150 goto err;
151 playlist->name = para_strdup(name);
152 *msg = make_message("loaded playlist %s (%u files)\n", playlist->name,
153 playlist->length);
154 /* success */
155 return current_playlist.length;
156 err:
157 PARA_NOTICE_LOG("unable to load playlist %s\n", name);
158 *msg = make_message("unable to load playlist %s\n", name);
159 return ret;
160 }
161
162 static int search_path(char *path, void *data)
163 {
164 if (strcmp(path, data))
165 return 1;
166 return -E_PATH_FOUND;
167 }
168
169 static int handle_audio_file_event(enum afs_events event, void *data)
170 {
171 int ret;
172 bool was_admissible = false, is_admissible;
173 struct osl_object playlist_def;
174 char *new_path;
175 const struct osl_row *row = data;
176
177 if (event == AUDIO_FILE_RENAME)
178 was_admissible = row_belongs_to_score_table(row);
179 ret = get_audio_file_path_of_row(row, &new_path);
180 if (ret < 0)
181 return ret;
182 ret = pl_get_def_by_name(current_playlist.name, &playlist_def);
183 if (ret < 0)
184 return ret;
185 ret = for_each_line(FELF_READ_ONLY, playlist_def.data,
186 playlist_def.size, search_path, new_path);
187 osl_close_disk_object(&playlist_def);
188 is_admissible = (ret < 0);
189 if (was_admissible && is_admissible)
190 return 1;
191 if (!was_admissible && !is_admissible)
192 return 1;
193 if (was_admissible && !is_admissible) {
194 current_playlist.length--;
195 return score_delete(row);
196 }
197 /* !was_admissible && is_admissible */
198 current_playlist.length++;
199 return score_add(row, 0); /* play it immediately */
200 }
201
202 /**
203 * Handle afs events relevant to playlists.
204 *
205 * \param event The event type.
206 * \param pb Unused.
207 * \param data Depends on the event type.
208 *
209 * \return Standard.
210 */
211 int playlists_event_handler(enum afs_events event,
212 __a_unused struct para_buffer *pb, void *data)
213 {
214 struct afsi_change_event_data *aced = data;
215
216 if (!current_playlist.name)
217 return 1;
218 switch (event) {
219 case AFSI_CHANGE:
220 return playlist_update_audio_file(aced->aft_row);
221 case AUDIO_FILE_RENAME:
222 case AUDIO_FILE_ADD:
223 return handle_audio_file_event(event, data);
224 case AUDIO_FILE_REMOVE:
225 if (!row_belongs_to_score_table(data))
226 return 1;
227 current_playlist.length--;
228 return score_delete(data);
229 default:
230 return 1;
231 }
232 }