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