2007-05-16 14:02:10 +02:00
|
|
|
/* the Music Player Daemon (MPD)
|
|
|
|
* Copyright (C) 2007 by Warren Dukes (warren.dukes@gmail.com)
|
|
|
|
* This project's homepage is: http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2008-10-22 17:21:57 +02:00
|
|
|
#include "stored_playlist.h"
|
2008-10-14 11:10:47 +02:00
|
|
|
#include "playlist_save.h"
|
2008-10-08 10:49:11 +02:00
|
|
|
#include "song.h"
|
2008-10-14 11:10:49 +02:00
|
|
|
#include "mapper.h"
|
2007-05-16 14:02:10 +02:00
|
|
|
#include "path.h"
|
|
|
|
#include "utils.h"
|
2007-05-26 16:09:09 +02:00
|
|
|
#include "ls.h"
|
2008-10-08 11:07:35 +02:00
|
|
|
#include "database.h"
|
2008-10-14 22:38:14 +02:00
|
|
|
#include "idle.h"
|
2008-01-03 08:29:49 +01:00
|
|
|
#include "os_compat.h"
|
2007-05-16 14:02:10 +02:00
|
|
|
|
2008-10-22 17:21:59 +02:00
|
|
|
static ListNode *
|
|
|
|
spl_get_index(List *list, int idx)
|
2007-05-16 14:02:10 +02:00
|
|
|
{
|
|
|
|
int forward;
|
|
|
|
ListNode *node;
|
|
|
|
int i;
|
|
|
|
|
2008-01-26 13:46:21 +01:00
|
|
|
if (idx >= list->numberOfNodes || idx < 0)
|
2007-05-16 14:02:10 +02:00
|
|
|
return NULL;
|
|
|
|
|
2008-01-26 13:46:21 +01:00
|
|
|
if (idx > (list->numberOfNodes/2)) {
|
2007-05-16 14:02:10 +02:00
|
|
|
forward = 0;
|
2008-01-01 11:09:36 +01:00
|
|
|
node = list->lastNode;
|
|
|
|
i = list->numberOfNodes - 1;
|
2007-05-16 14:02:10 +02:00
|
|
|
} else {
|
|
|
|
forward = 1;
|
2008-01-01 11:09:36 +01:00
|
|
|
node = list->firstNode;
|
2007-05-16 14:02:10 +02:00
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (node != NULL) {
|
2008-01-26 13:46:21 +01:00
|
|
|
if (i == idx)
|
2007-05-16 14:02:10 +02:00
|
|
|
return node;
|
|
|
|
|
|
|
|
if (forward) {
|
|
|
|
i++;
|
|
|
|
node = node->nextNode;
|
|
|
|
} else {
|
|
|
|
i--;
|
|
|
|
node = node->prevNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-09-07 13:44:12 +02:00
|
|
|
static enum playlist_result
|
2008-10-22 17:21:59 +02:00
|
|
|
spl_save(List *list, const char *utf8path)
|
2008-01-01 11:09:31 +01:00
|
|
|
{
|
|
|
|
ListNode *node;
|
|
|
|
FILE *file;
|
2008-09-09 09:59:34 +02:00
|
|
|
char path_max_tmp[MPD_PATH_MAX];
|
|
|
|
|
|
|
|
assert(utf8path != NULL);
|
2008-01-01 11:09:31 +01:00
|
|
|
|
2008-09-09 09:59:34 +02:00
|
|
|
utf8_to_fs_playlist_path(path_max_tmp, utf8path);
|
2008-01-01 11:09:31 +01:00
|
|
|
|
2008-09-09 09:59:34 +02:00
|
|
|
while (!(file = fopen(path_max_tmp, "w")) && errno == EINTR);
|
2008-09-07 13:44:12 +02:00
|
|
|
if (file == NULL)
|
|
|
|
return PLAYLIST_RESULT_ERRNO;
|
2008-01-01 11:09:31 +01:00
|
|
|
|
2008-01-01 11:09:36 +01:00
|
|
|
node = list->firstNode;
|
2008-01-01 11:09:31 +01:00
|
|
|
while (node != NULL) {
|
2008-10-14 11:10:47 +02:00
|
|
|
playlist_print_uri(file, (const char *)node->data);
|
2008-01-01 11:09:31 +01:00
|
|
|
node = node->nextNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (fclose(file) != 0 && errno == EINTR);
|
2008-09-07 13:44:12 +02:00
|
|
|
return PLAYLIST_RESULT_SUCCESS;
|
2008-01-01 11:09:31 +01:00
|
|
|
}
|
|
|
|
|
2008-10-22 17:21:59 +02:00
|
|
|
List *
|
|
|
|
spl_load(const char *utf8path)
|
2007-05-16 14:02:10 +02:00
|
|
|
{
|
2008-01-01 11:09:36 +01:00
|
|
|
List *list;
|
2007-05-16 14:02:10 +02:00
|
|
|
FILE *file;
|
2008-01-01 11:09:26 +01:00
|
|
|
char buffer[MPD_PATH_MAX];
|
2007-12-28 03:56:25 +01:00
|
|
|
char path_max_tmp[MPD_PATH_MAX];
|
2007-05-16 14:02:10 +02:00
|
|
|
|
2008-09-07 13:44:12 +02:00
|
|
|
if (!is_valid_playlist_name(utf8path))
|
2008-01-01 11:09:36 +01:00
|
|
|
return NULL;
|
2007-05-16 14:02:10 +02:00
|
|
|
|
2008-01-01 04:01:24 +01:00
|
|
|
utf8_to_fs_playlist_path(path_max_tmp, utf8path);
|
|
|
|
while (!(file = fopen(path_max_tmp, "r")) && errno == EINTR);
|
2008-09-07 13:44:12 +02:00
|
|
|
if (file == NULL)
|
2008-01-01 11:09:36 +01:00
|
|
|
return NULL;
|
2007-05-16 14:02:10 +02:00
|
|
|
|
2008-01-01 11:09:36 +01:00
|
|
|
list = makeList(DEFAULT_FREE_DATA_FUNC, 0);
|
2007-05-16 14:02:10 +02:00
|
|
|
|
2008-01-01 11:09:26 +01:00
|
|
|
while (myFgets(buffer, sizeof(buffer), file)) {
|
|
|
|
char *s = buffer;
|
2008-10-14 11:10:49 +02:00
|
|
|
const char *path_utf8;
|
2008-01-01 11:09:26 +01:00
|
|
|
|
|
|
|
if (*s == PLAYLIST_COMMENT)
|
|
|
|
continue;
|
2008-10-14 11:10:49 +02:00
|
|
|
|
|
|
|
if (isValidRemoteUtf8Url(s))
|
|
|
|
insertInListWithoutKey(list, xstrdup(s));
|
|
|
|
else {
|
|
|
|
struct song *song;
|
|
|
|
|
|
|
|
path_utf8 = map_fs_to_utf8(s, path_max_tmp);
|
|
|
|
if (path_utf8 == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
song = db_get_song(path_utf8);
|
|
|
|
if (song == NULL)
|
|
|
|
continue;
|
|
|
|
|
2008-10-08 11:05:34 +02:00
|
|
|
song_get_url(song, path_max_tmp);
|
2008-01-01 11:09:36 +01:00
|
|
|
insertInListWithoutKey(list, xstrdup(path_max_tmp));
|
2008-10-14 11:10:49 +02:00
|
|
|
}
|
2008-01-26 21:20:59 +01:00
|
|
|
|
|
|
|
if (list->numberOfNodes >= playlist_max_length)
|
|
|
|
break;
|
2007-05-16 14:02:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while (fclose(file) && errno == EINTR);
|
2008-01-01 11:09:36 +01:00
|
|
|
return list;
|
2007-05-16 14:02:10 +02:00
|
|
|
}
|
|
|
|
|
2008-10-22 17:21:59 +02:00
|
|
|
static int
|
|
|
|
spl_move_index_internal(List *list, int src, int dest)
|
2007-05-16 14:02:10 +02:00
|
|
|
{
|
|
|
|
ListNode *srcNode, *destNode;
|
|
|
|
|
2008-01-01 11:09:36 +01:00
|
|
|
if (src >= list->numberOfNodes || dest >= list->numberOfNodes ||
|
2008-09-07 13:44:12 +02:00
|
|
|
src < 0 || dest < 0 || src == dest)
|
2007-05-16 14:02:10 +02:00
|
|
|
return -1;
|
|
|
|
|
2008-10-22 17:21:59 +02:00
|
|
|
srcNode = spl_get_index(list, src);
|
2007-05-16 14:02:10 +02:00
|
|
|
if (!srcNode)
|
|
|
|
return -1;
|
|
|
|
|
2008-10-22 17:21:59 +02:00
|
|
|
destNode = spl_get_index(list, dest);
|
2007-05-16 14:02:10 +02:00
|
|
|
|
|
|
|
/* remove src */
|
|
|
|
if (srcNode->prevNode)
|
|
|
|
srcNode->prevNode->nextNode = srcNode->nextNode;
|
|
|
|
else
|
2008-01-01 11:09:36 +01:00
|
|
|
list->firstNode = srcNode->nextNode;
|
2007-05-16 14:02:10 +02:00
|
|
|
|
|
|
|
if (srcNode->nextNode)
|
|
|
|
srcNode->nextNode->prevNode = srcNode->prevNode;
|
|
|
|
else
|
2008-01-01 11:09:36 +01:00
|
|
|
list->lastNode = srcNode->prevNode;
|
2007-05-16 14:02:10 +02:00
|
|
|
|
|
|
|
/* this is all a bit complicated - but I tried to
|
|
|
|
* maintain the same order stuff is moved as in the
|
|
|
|
* real playlist */
|
|
|
|
if (dest == 0) {
|
2008-01-01 11:09:36 +01:00
|
|
|
list->firstNode->prevNode = srcNode;
|
|
|
|
srcNode->nextNode = list->firstNode;
|
2007-05-16 14:02:10 +02:00
|
|
|
srcNode->prevNode = NULL;
|
2008-01-01 11:09:36 +01:00
|
|
|
list->firstNode = srcNode;
|
|
|
|
} else if ((dest + 1) == list->numberOfNodes) {
|
|
|
|
list->lastNode->nextNode = srcNode;
|
2007-05-16 14:02:10 +02:00
|
|
|
srcNode->nextNode = NULL;
|
2008-01-01 11:09:36 +01:00
|
|
|
srcNode->prevNode = list->lastNode;
|
|
|
|
list->lastNode = srcNode;
|
2007-05-16 14:02:10 +02:00
|
|
|
} else {
|
|
|
|
if (destNode == NULL) {
|
|
|
|
/* this shouldn't be happening. */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (src > dest) {
|
|
|
|
destNode->prevNode->nextNode = srcNode;
|
|
|
|
srcNode->prevNode = destNode->prevNode;
|
|
|
|
srcNode->nextNode = destNode;
|
|
|
|
destNode->prevNode = srcNode;
|
|
|
|
} else {
|
|
|
|
destNode->nextNode->prevNode = srcNode;
|
|
|
|
srcNode->prevNode = destNode;
|
|
|
|
srcNode->nextNode = destNode->nextNode;
|
|
|
|
destNode->nextNode = srcNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-14 22:38:14 +02:00
|
|
|
idle_add(IDLE_STORED_PLAYLIST);
|
2007-05-16 14:02:10 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-07 13:44:12 +02:00
|
|
|
enum playlist_result
|
2008-10-22 17:21:59 +02:00
|
|
|
spl_move_index(const char *utf8path, int src, int dest)
|
2007-05-16 14:02:10 +02:00
|
|
|
{
|
2008-01-01 11:09:36 +01:00
|
|
|
List *list;
|
2008-09-07 13:44:12 +02:00
|
|
|
enum playlist_result result;
|
2008-01-01 11:09:31 +01:00
|
|
|
|
2008-10-22 17:21:59 +02:00
|
|
|
if (!(list = spl_load(utf8path)))
|
2008-09-07 13:44:12 +02:00
|
|
|
return PLAYLIST_RESULT_NO_SUCH_LIST;
|
2007-05-16 14:02:10 +02:00
|
|
|
|
2008-10-22 17:21:59 +02:00
|
|
|
if (spl_move_index_internal(list, src, dest) != 0) {
|
2008-01-01 11:09:36 +01:00
|
|
|
freeList(list);
|
2008-09-07 13:44:12 +02:00
|
|
|
return PLAYLIST_RESULT_BAD_RANGE;
|
2007-05-16 14:02:10 +02:00
|
|
|
}
|
|
|
|
|
2008-10-22 17:21:59 +02:00
|
|
|
result = spl_save(list, utf8path);
|
2007-05-16 14:02:10 +02:00
|
|
|
|
2008-01-01 11:09:36 +01:00
|
|
|
freeList(list);
|
2008-10-14 22:38:14 +02:00
|
|
|
|
|
|
|
idle_add(IDLE_STORED_PLAYLIST);
|
2008-09-07 13:44:12 +02:00
|
|
|
return result;
|
2007-05-16 14:02:10 +02:00
|
|
|
}
|
|
|
|
|
2008-09-07 13:44:12 +02:00
|
|
|
enum playlist_result
|
2008-10-22 17:21:59 +02:00
|
|
|
spl_clear(const char *utf8path)
|
2007-05-16 14:02:10 +02:00
|
|
|
{
|
2008-01-01 04:01:24 +01:00
|
|
|
char filename[MPD_PATH_MAX];
|
2007-05-16 14:02:10 +02:00
|
|
|
FILE *file;
|
|
|
|
|
2008-09-07 13:44:12 +02:00
|
|
|
if (!is_valid_playlist_name(utf8path))
|
|
|
|
return PLAYLIST_RESULT_BAD_NAME;
|
|
|
|
|
2008-01-01 04:01:24 +01:00
|
|
|
utf8_to_fs_playlist_path(filename, utf8path);
|
2007-05-16 14:02:10 +02:00
|
|
|
|
|
|
|
while (!(file = fopen(filename, "w")) && errno == EINTR);
|
2008-09-07 13:44:12 +02:00
|
|
|
if (file == NULL)
|
|
|
|
return PLAYLIST_RESULT_ERRNO;
|
2007-05-16 14:02:10 +02:00
|
|
|
|
|
|
|
while (fclose(file) != 0 && errno == EINTR);
|
2008-10-14 22:38:14 +02:00
|
|
|
|
|
|
|
idle_add(IDLE_STORED_PLAYLIST);
|
2008-09-07 13:44:12 +02:00
|
|
|
return PLAYLIST_RESULT_SUCCESS;
|
2007-05-16 14:02:10 +02:00
|
|
|
}
|
|
|
|
|
2008-10-22 17:21:59 +02:00
|
|
|
static int
|
|
|
|
spl_remove_index_internal(List *list, int pos)
|
2007-05-16 14:02:10 +02:00
|
|
|
{
|
2008-10-22 17:21:59 +02:00
|
|
|
ListNode *node = spl_get_index(list, pos);
|
2008-09-07 13:44:12 +02:00
|
|
|
if (!node)
|
2007-05-16 14:02:10 +02:00
|
|
|
return -1;
|
|
|
|
|
2008-01-01 11:09:36 +01:00
|
|
|
deleteNodeFromList(list, node);
|
2007-05-16 14:02:10 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-07 13:44:12 +02:00
|
|
|
enum playlist_result
|
2008-10-22 17:21:59 +02:00
|
|
|
spl_remove_index(const char *utf8path, int pos)
|
2007-05-16 14:02:10 +02:00
|
|
|
{
|
2008-01-01 11:09:36 +01:00
|
|
|
List *list;
|
2008-09-07 13:44:12 +02:00
|
|
|
enum playlist_result result;
|
2008-01-01 11:09:31 +01:00
|
|
|
|
2008-10-22 17:21:59 +02:00
|
|
|
if (!(list = spl_load(utf8path)))
|
2008-09-07 13:44:12 +02:00
|
|
|
return PLAYLIST_RESULT_NO_SUCH_LIST;
|
2007-05-16 14:02:10 +02:00
|
|
|
|
2008-10-22 17:21:59 +02:00
|
|
|
if (spl_remove_index_internal(list, pos) != 0) {
|
2008-01-01 11:09:36 +01:00
|
|
|
freeList(list);
|
2008-09-07 13:44:12 +02:00
|
|
|
return PLAYLIST_RESULT_BAD_RANGE;
|
2007-05-16 14:02:10 +02:00
|
|
|
}
|
|
|
|
|
2008-10-22 17:21:59 +02:00
|
|
|
result = spl_save(list, utf8path);
|
2007-05-16 14:02:10 +02:00
|
|
|
|
2008-01-01 11:09:36 +01:00
|
|
|
freeList(list);
|
2008-10-14 22:38:14 +02:00
|
|
|
|
|
|
|
idle_add(IDLE_STORED_PLAYLIST);
|
2008-09-07 13:44:12 +02:00
|
|
|
return result;
|
2007-05-16 14:02:10 +02:00
|
|
|
}
|
|
|
|
|
2008-09-07 13:44:12 +02:00
|
|
|
enum playlist_result
|
2008-10-22 17:21:59 +02:00
|
|
|
spl_append_song(const char *utf8path, struct song *song)
|
2007-05-16 14:02:10 +02:00
|
|
|
{
|
|
|
|
FILE *file;
|
2008-01-29 10:26:39 +01:00
|
|
|
struct stat st;
|
2007-12-28 03:56:25 +01:00
|
|
|
char path_max_tmp[MPD_PATH_MAX];
|
2007-05-16 14:02:10 +02:00
|
|
|
|
2008-09-07 13:44:12 +02:00
|
|
|
if (!is_valid_playlist_name(utf8path))
|
|
|
|
return PLAYLIST_RESULT_BAD_NAME;
|
2008-01-01 04:01:24 +01:00
|
|
|
utf8_to_fs_playlist_path(path_max_tmp, utf8path);
|
2007-05-16 14:02:10 +02:00
|
|
|
|
2008-01-29 10:26:39 +01:00
|
|
|
while (!(file = fopen(path_max_tmp, "a")) && errno == EINTR);
|
2007-05-16 14:02:10 +02:00
|
|
|
if (file == NULL) {
|
2008-09-07 13:44:12 +02:00
|
|
|
int save_errno = errno;
|
|
|
|
while (fclose(file) != 0 && errno == EINTR);
|
|
|
|
errno = save_errno;
|
|
|
|
return PLAYLIST_RESULT_ERRNO;
|
2007-05-16 14:02:10 +02:00
|
|
|
}
|
2008-09-07 13:44:12 +02:00
|
|
|
|
2008-01-29 10:26:39 +01:00
|
|
|
if (fstat(fileno(file), &st) < 0) {
|
2008-09-07 13:44:12 +02:00
|
|
|
int save_errno = errno;
|
2008-09-07 13:37:33 +02:00
|
|
|
while (fclose(file) != 0 && errno == EINTR);
|
2008-09-07 13:44:12 +02:00
|
|
|
errno = save_errno;
|
|
|
|
return PLAYLIST_RESULT_ERRNO;
|
2008-01-26 21:20:59 +01:00
|
|
|
}
|
2008-09-07 13:44:12 +02:00
|
|
|
|
2008-01-29 10:26:39 +01:00
|
|
|
if (st.st_size >= ((MPD_PATH_MAX+1) * playlist_max_length)) {
|
2008-09-07 13:37:33 +02:00
|
|
|
while (fclose(file) != 0 && errno == EINTR);
|
2008-09-07 13:44:12 +02:00
|
|
|
return PLAYLIST_RESULT_TOO_LARGE;
|
2008-01-26 21:20:59 +01:00
|
|
|
}
|
2007-05-16 14:02:10 +02:00
|
|
|
|
2008-10-14 11:10:47 +02:00
|
|
|
playlist_print_song(file, song);
|
2007-05-16 14:02:10 +02:00
|
|
|
|
|
|
|
while (fclose(file) != 0 && errno == EINTR);
|
2008-10-14 22:38:14 +02:00
|
|
|
|
|
|
|
idle_add(IDLE_STORED_PLAYLIST);
|
2008-09-07 13:44:12 +02:00
|
|
|
return PLAYLIST_RESULT_SUCCESS;
|
2007-05-16 14:02:10 +02:00
|
|
|
}
|
|
|
|
|
2008-09-07 13:44:12 +02:00
|
|
|
enum playlist_result
|
2008-10-22 17:21:59 +02:00
|
|
|
spl_rename(const char *utf8from, const char *utf8to)
|
2007-05-24 20:07:19 +02:00
|
|
|
{
|
|
|
|
struct stat st;
|
2008-01-01 04:01:24 +01:00
|
|
|
char from[MPD_PATH_MAX];
|
|
|
|
char to[MPD_PATH_MAX];
|
2007-05-24 20:07:19 +02:00
|
|
|
|
2008-09-07 13:44:12 +02:00
|
|
|
if (!is_valid_playlist_name(utf8from) ||
|
|
|
|
!is_valid_playlist_name(utf8to))
|
|
|
|
return PLAYLIST_RESULT_BAD_NAME;
|
2007-05-24 20:07:19 +02:00
|
|
|
|
2008-01-01 04:01:24 +01:00
|
|
|
utf8_to_fs_playlist_path(from, utf8from);
|
|
|
|
utf8_to_fs_playlist_path(to, utf8to);
|
2007-05-24 20:07:19 +02:00
|
|
|
|
2008-09-07 13:44:12 +02:00
|
|
|
if (stat(from, &st) != 0)
|
|
|
|
return PLAYLIST_RESULT_NO_SUCH_LIST;
|
2007-05-24 20:07:19 +02:00
|
|
|
|
2008-09-07 13:44:12 +02:00
|
|
|
if (stat(to, &st) == 0)
|
|
|
|
return PLAYLIST_RESULT_LIST_EXISTS;
|
2007-05-24 20:07:19 +02:00
|
|
|
|
2008-09-07 13:44:12 +02:00
|
|
|
if (rename(from, to) < 0)
|
|
|
|
return PLAYLIST_RESULT_ERRNO;
|
2007-05-24 20:07:19 +02:00
|
|
|
|
2008-10-14 22:38:14 +02:00
|
|
|
idle_add(IDLE_STORED_PLAYLIST);
|
2008-09-07 13:44:12 +02:00
|
|
|
return PLAYLIST_RESULT_SUCCESS;
|
2007-05-24 20:07:19 +02:00
|
|
|
}
|