command.c: Document return value of handle_connect().
[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 /* returns -E_PLAYLIST_LOADED on _success_ to terminate the loop */
59 static int load_playlist(struct osl_row *row, void *data)
60 {
61 struct playlist_info *playlist = data;
62 struct osl_object playlist_def;
63 char *playlist_name;
64 int ret;
65
66 ret = pl_get_name_and_def_by_row(row, &playlist_name, &playlist_def);
67 if (ret < 0)
68 goto err;
69 playlist->length = 0;
70 ret = for_each_line(FELF_READ_ONLY, playlist_def.data,
71 playlist_def.size, add_playlist_entry, playlist);
72 osl_close_disk_object(&playlist_def);
73 if (ret < 0)
74 goto err;
75 ret = -E_PLAYLIST_EMPTY;
76 if (!playlist->length)
77 goto err;
78 playlist->name = para_strdup(playlist_name);
79 PARA_NOTICE_LOG("loaded playlist %s (%u files)\n", playlist->name,
80 playlist->length);
81 return -E_PLAYLIST_LOADED;
82 err:
83 if (ret != -E_DUMMY_ROW)
84 PARA_NOTICE_LOG("unable to load playlist (%s)\n",
85 para_strerror(-ret));
86 return 1;
87 }
88
89 static int check_playlist_path(char *path, void *data)
90 {
91 struct para_buffer *pb = data;
92 struct osl_row *aft_row;
93 int ret = aft_get_row_of_path(path, &aft_row);
94
95 if (ret < 0)
96 para_printf(pb, "%s: %s\n", path, para_strerror(-ret));
97 return 1; /* do not fail the loop on bad paths */
98 }
99
100 static int check_playlist(struct osl_row *row, void *data)
101 {
102 struct para_buffer *pb = data;
103 struct osl_object playlist_def;
104 char *playlist_name;
105 int ret = pl_get_name_and_def_by_row(row, &playlist_name, &playlist_def);
106
107 if (ret < 0) { /* log error, but continue */
108 para_printf(pb, "failed to get playlist data: %s\n",
109 para_strerror(-ret));
110 return 1;
111 }
112 if (*playlist_name) { /* skip dummy row */
113 para_printf(pb, "checking playlist %s...\n", playlist_name);
114 for_each_line(FELF_READ_ONLY, playlist_def.data,
115 playlist_def.size, check_playlist_path, pb);
116 }
117 osl_close_disk_object(&playlist_def);
118 return 1;
119 }
120
121 /**
122 * Check the playlist table for inconsistencies.
123 *
124 * \param aca This callback ignores ->query.
125 *
126 * \return Standard. Invalid paths are reported, but are not considered an
127 * error.
128 */
129 int playlist_check_callback(struct afs_callback_arg *aca)
130 {
131 para_printf(&aca->pbout, "checking playlists...\n");
132 return osl(osl_rbtree_loop(playlists_table, BLOBCOL_ID, &aca->pbout,
133 check_playlist));
134 }
135
136 /**
137 * Close the current playlist.
138 *
139 * \sa \ref playlist_open().
140 */
141 void playlist_close(void)
142 {
143 if (!current_playlist.name)
144 return;
145 free(current_playlist.name);
146 current_playlist.name = NULL;
147 }
148
149 /**
150 * Open the given playlist.
151 *
152 * \param name The name of the playlist to open.
153 *
154 * Files which are listed in the playlist, but not contained in the database
155 * are ignored. This is not considered an error.
156 *
157 * \return Standard.
158 */
159 int playlist_open(const char *name)
160 {
161 struct osl_object obj;
162 int ret;
163 struct osl_row *row;
164
165 obj.data = (char *)name;
166 obj.size = strlen(obj.data);
167 ret = osl(osl_get_row(playlists_table, BLOBCOL_NAME, &obj, &row));
168 if (ret < 0) {
169 PARA_NOTICE_LOG("failed to load playlist %s\n", name);
170 return ret;
171 }
172 playlist_close();
173 ret = load_playlist(row, &current_playlist);
174 return (ret == -E_PLAYLIST_LOADED)? current_playlist.length : ret;
175 }
176
177 static int search_path(char *path, void *data)
178 {
179 if (strcmp(path, data))
180 return 1;
181 return -E_PATH_FOUND;
182 }
183
184 static int handle_audio_file_event(enum afs_events event, void *data)
185 {
186 int ret, was_admissible = 0, is_admissible;
187 struct osl_object playlist_def;
188 char *new_path;
189 const struct osl_row *row = data;
190
191 if (event == AUDIO_FILE_RENAME) {
192 ret = row_belongs_to_score_table(row, NULL);
193 if (ret < 0)
194 return ret;
195 was_admissible = ret;
196 }
197 ret = get_audio_file_path_of_row(row, &new_path);
198 if (ret < 0)
199 return ret;
200 ret = pl_get_def_by_name(current_playlist.name, &playlist_def);
201 if (ret < 0)
202 return ret;
203 ret = for_each_line(FELF_READ_ONLY, playlist_def.data,
204 playlist_def.size, search_path, new_path);
205 osl_close_disk_object(&playlist_def);
206 is_admissible = (ret < 0);
207 if (was_admissible && is_admissible)
208 return 1;
209 if (!was_admissible && !is_admissible)
210 return 1;
211 if (was_admissible && !is_admissible) {
212 current_playlist.length--;
213 return score_delete(row);
214 }
215 /* !was_admissible && is_admissible */
216 current_playlist.length++;
217 return score_add(row, 0); /* play it immediately */
218 }
219
220 /**
221 * Handle afs events relevant to playlists.
222 *
223 * \param event The event type.
224 * \param pb Unused.
225 * \param data Depends on the event type.
226 *
227 * \return Standard.
228 */
229 int playlists_event_handler(enum afs_events event,
230 __a_unused struct para_buffer *pb, void *data)
231 {
232 int ret;
233 struct afsi_change_event_data *aced = data;
234
235 if (!current_playlist.name)
236 return 1;
237 switch (event) {
238 case AFSI_CHANGE:
239 return playlist_update_audio_file(aced->aft_row);
240 case AUDIO_FILE_RENAME:
241 case AUDIO_FILE_ADD:
242 return handle_audio_file_event(event, data);
243 case AUDIO_FILE_REMOVE:
244 ret = row_belongs_to_score_table(data, NULL);
245 if (ret < 0)
246 return ret;
247 if (!ret)
248 return 1;
249 current_playlist.length--;
250 return score_delete(data);
251 default:
252 return 1;
253 }
254 }