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