playlist.c: Cleanups.
[paraslash.git] / playlist.c
1 #include "para.h"
2 #include "error.h"
3 #include "afh.h"
4 #include "afs.h"
5 #include "string.h"
6
7 /** \file playlist.c Functions for loading and saving playlists. */
8
9 /** Structure used for adding entries to a playlist. */
10 struct playlist_info {
11 /** The name of the playlist. */
12 char *name;
13 /** The number of entries currently in the playlist. */
14 unsigned length;
15 };
16 static struct playlist_info current_playlist;
17
18 /**
19 * Re-insert an audio file into the tree of admissible files.
20 *
21 * \param aft_row Determines the audio file.
22 *
23 * \return The return value of score_update().
24 */
25 int playlist_update_audio_file(struct osl_row *aft_row)
26 {
27 /* always re-insert to the top of the tree */
28 return score_update(aft_row, 0);
29 }
30
31 static int add_playlist_entry(char *line, void *data)
32 {
33 struct playlist_info *playlist = data;
34 struct osl_row *aft_row;
35 int ret = aft_get_row_of_path(line, &aft_row);
36
37 if (ret < 0) {
38 PARA_NOTICE_LOG("path not found in audio file table: %s\n",
39 line);
40 return 1;
41 }
42 ret = score_add(aft_row, -playlist->length);
43 if (ret < 0) {
44 PARA_ERROR_LOG("failed to add %s: %d\n", line, ret);
45 return ret;
46 }
47 playlist->length++;
48 return 1;
49 }
50
51 static int load_playlist(struct osl_row *row, struct playlist_info *playlist)
52 {
53 struct osl_object obj;
54 int ret;
55
56 ret = osl_get_object(playlists_table, row, BLOBCOL_NAME, &obj);
57 if (ret < 0)
58 return ret;
59 playlist->name = para_strdup(obj.data);
60 playlist->length = 0;
61 ret = osl_open_disk_object(playlists_table, row, BLOBCOL_DEF, &obj);
62 if (ret < 0)
63 goto err;
64 ret = for_each_line_ro(obj.data, obj.size, add_playlist_entry,
65 playlist);
66 osl_close_disk_object(&obj);
67 if (ret < 0)
68 goto err;
69 ret = -E_PLAYLIST_EMPTY;
70 if (!playlist->length)
71 goto err;
72 PARA_NOTICE_LOG("loaded playlist %s (%u files)\n", playlist->name,
73 playlist->length);
74 return 1;
75 err:
76 free(playlist->name);
77 return ret;
78 }
79
80 /* returns -E_PLAYLIST_LOADED on _success_ to terminate the loop */
81 static int playlist_loop(struct osl_row *row, void *data)
82 {
83 struct playlist_info *playlist = data;
84 int ret = load_playlist(row, playlist);
85 if (ret < 0) {
86 if (ret != -E_DUMMY_ROW)
87 PARA_NOTICE_LOG("unable to load playlist, trying next\n");
88 return 1;
89 }
90 return -E_PLAYLIST_LOADED;
91 }
92
93 static int load_first_available_playlist(struct playlist_info *playlist)
94 {
95 int ret = osl_rbtree_loop(playlists_table, BLOBCOL_NAME, playlist,
96 playlist_loop);
97 if (ret == -E_PLAYLIST_LOADED) /* success */
98 return 1;
99 if (ret < 0)
100 return ret; /* error */
101 PARA_NOTICE_LOG("no valid playlist found\n");
102 return -E_NO_PLAYLIST;
103 }
104
105 /**
106 * Close the current playlist.
107 *
108 * \sa playlist_open().
109 */
110 void playlist_close(void)
111 {
112 free(current_playlist.name);
113 current_playlist.name = NULL;
114 }
115
116 /**
117 * Open the given playlist.
118 *
119 * \param name The name of the playlist to open.
120 *
121 * If name is \p NULL, load the first available playlist. Files which are
122 * listed in the playlist, but not contained in the database are ignored.
123 * This is not considered an error.
124 *
125 * \return Positive on success, negative on errors. Possible errors
126 * include: Given playlist not found, -E_NO_PLAYLIST (no playlist defined).
127 */
128 int playlist_open(char *name)
129 {
130 struct osl_object obj;
131 int ret;
132 struct osl_row *row;
133
134 if (!name)
135 return load_first_available_playlist(&current_playlist);
136 obj.data = name;
137 obj.size = strlen(obj.data);
138 ret = osl_get_row(playlists_table, BLOBCOL_NAME, &obj, &row);
139 if (ret < 0) {
140 PARA_NOTICE_LOG("failed to load playlist %s\n", name);
141 return ret;
142 }
143 return load_playlist(row, &current_playlist);
144 }