Revert "string.c: Use $HOME to get the home directory."
[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 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 *private_data)
32 {
33 struct osl_row *aft_row;
34 struct playlist_info *pli = private_data;
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, -pli->length);
43 if (ret < 0) {
44 PARA_ERROR_LOG("failed to add %s: %d\n", line, ret);
45 return ret;
46 }
47 pli->length++;
48 return 1;
49 }
50
51 static int load_playlist(struct osl_row *row)
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, __a_unused void *private_data)
82 {
83 int ret = load_playlist(row);
84 if (ret < 0) {
85 if (ret != -E_DUMMY_ROW)
86 PARA_NOTICE_LOG("unable to load playlist, trying next\n");
87 return 1;
88 }
89 return -E_PLAYLIST_LOADED;
90 }
91
92 static int load_first_available_playlist(void)
93 {
94 int ret = osl_rbtree_loop(playlists_table, BLOBCOL_NAME, NULL,
95 playlist_loop);
96 if (ret == -E_PLAYLIST_LOADED) /* success */
97 return 1;
98 if (ret < 0)
99 return ret; /* error */
100 PARA_NOTICE_LOG("no valid playlist found\n");
101 return -E_NO_PLAYLIST;
102 }
103
104 /**
105 * Close the current playlist.
106 *
107 * \sa playlist_open().
108 */
109 void playlist_close(void)
110 {
111 free(playlist.name);
112 playlist.name = NULL;
113 }
114
115 /**
116 * Open the given playlist.
117 *
118 * \param name The name of the playlist to open.
119 *
120 * If name is \p NULL, load the first available playlist. Files which are
121 * listed in the playlist, but not contained in the database are ignored.
122 * This is not considered an error.
123 *
124 * \return Positive on success, negative on errors. Possible errors
125 * include: Given playlist not found, -E_NO_PLAYLIST (no playlist defined).
126 */
127 int playlist_open(char *name)
128 {
129 struct osl_object obj;
130 int ret;
131 struct osl_row *row;
132
133 if (!name)
134 return load_first_available_playlist();
135 obj.data = name;
136 obj.size = strlen(obj.data);
137 ret = osl_get_row(playlists_table, BLOBCOL_NAME, &obj, &row);
138 if (ret < 0) {
139 PARA_NOTICE_LOG("failed to load playlist %s\n", name);
140 return ret;
141 }
142 return load_playlist(row);
143 }