From 08003904d7af58c0402b49805b9f87b3d9ebbc27 Mon Sep 17 00:00:00 2001
From: "J. Alexander Treuman" <jat@spatialrift.net>
Date: Mon, 20 Nov 2006 15:37:58 +0000
Subject: [PATCH] Adding functions for clearing/adding to stored playlists. 
 Commands to make use of these functions are still being worked on.

git-svn-id: https://svn.musicpd.org/mpd/trunk@5075 09075e82-0dd4-0310-85a5-a0d7c8717e4f
---
 src/dbUtils.c  |  11 ++++++
 src/dbUtils.h  |   2 +
 src/playlist.c | 102 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/playlist.h |   6 +++
 4 files changed, 121 insertions(+)

diff --git a/src/dbUtils.c b/src/dbUtils.c
index d8b4c0018..b713b2a8b 100644
--- a/src/dbUtils.c
+++ b/src/dbUtils.c
@@ -304,11 +304,22 @@ static int directoryAddSongToPlaylist(int fd, Song * song, void *data)
 	return addSongToPlaylist(fd, song, 0);
 }
 
+static int directoryAddSongToStoredPlaylist(int fd, Song *song, void *data)
+{
+	return addSongToStoredPlaylist(fd, song, (char *)data);
+}
+
 int addAllIn(int fd, char *name)
 {
 	return traverseAllIn(fd, name, directoryAddSongToPlaylist, NULL, NULL);
 }
 
+int addAllInToStoredPlaylist(int fd, char *name, char *utf8file)
+{
+	return traverseAllIn(fd, name, directoryAddSongToStoredPlaylist, NULL,
+	                     (void *)utf8file);
+}
+
 static int directoryPrintSongInfo(int fd, Song * song, void *data)
 {
 	return printSongInfo(fd, song);
diff --git a/src/dbUtils.h b/src/dbUtils.h
index 1fc7e1053..5b0623161 100644
--- a/src/dbUtils.h
+++ b/src/dbUtils.h
@@ -47,6 +47,8 @@ int printAllIn(int fd, char *name);
 
 int addAllIn(int fd, char *name);
 
+int addAllInToStoredPlaylist(int fd, char *name, char *utf8file);
+
 int printInfoForAllIn(int fd, char *name);
 
 int searchForSongsIn(int fd, char *name, int numItems,
diff --git a/src/playlist.c b/src/playlist.c
index c977d0e4f..0c0d3cf25 100644
--- a/src/playlist.c
+++ b/src/playlist.c
@@ -236,6 +236,44 @@ int clearPlaylist(int fd)
 	return 0;
 }
 
+int clearStoredPlaylist(int fd, char *utf8file)
+{
+	int fileD;
+	char *file;
+	char *rfile;
+	char *actualFile;
+
+	if (strstr(utf8file, "/")) {
+		commandError(fd, ACK_ERROR_ARG,
+		             "cannot clear \"%s\", saving playlists to "
+		             "subdirectories is not supported", utf8file);
+		return -1;
+	}
+
+	file = utf8ToFsCharset(utf8file);
+
+	rfile = xmalloc(strlen(file) + strlen(".") +
+	                strlen(PLAYLIST_FILE_SUFFIX) + 1);
+
+	strcpy(rfile, file);
+	strcat(rfile, ".");
+	strcat(rfile, PLAYLIST_FILE_SUFFIX);
+
+	actualFile = rpp2app(rfile);
+
+	free(rfile);
+
+	while ((fileD = open(actualFile, O_WRONLY | O_TRUNC | O_CREAT)) == -1
+	       && errno == EINTR);
+	if (fileD == -1) {
+		commandError(fd, ACK_ERROR_SYSTEM, "problems opening file");
+		return -1;
+	}
+	while (close(fileD) == -1 && errno == EINTR);
+
+	return 0;
+}
+
 int showPlaylist(int fd)
 {
 	int i;
@@ -590,6 +628,24 @@ int addToPlaylist(int fd, char *url, int printId)
 	return addSongToPlaylist(fd, song, printId);
 }
 
+int addToStoredPlaylist(int fd, char *url, char *utf8file)
+{
+	Song *song;
+
+	DEBUG("add to stored playlist: %s\n", url);
+
+	if ((song = getSongFromDB(url))) {
+	} else if (!(isValidRemoteUtf8Url(url) &&
+	             (song = newSong(url, SONG_TYPE_URL, NULL)))) {
+		commandError(fd, ACK_ERROR_NO_EXIST,
+		             "\"%s\" is not in the music db or is "
+		             "not a valid url", url);
+		return -1;
+	}
+
+	return addSongToStoredPlaylist(fd, song, utf8file);
+}
+
 int addSongToPlaylist(int fd, Song * song, int printId)
 {
 	int id;
@@ -642,6 +698,52 @@ int addSongToPlaylist(int fd, Song * song, int printId)
 	return 0;
 }
 
+int addSongToStoredPlaylist(int fd, Song *song, char *utf8file)
+{
+	FILE *fileP;
+	char *file;
+	char *rfile;
+	char *actualFile;
+	char *url;
+
+	if (strstr(utf8file, "/")) {
+		commandError(fd, ACK_ERROR_ARG,
+		             "cannot add to \"%s\", saving playlists to "
+		             "subdirectories is not supported", utf8file);
+		return -1;
+	}
+
+	file = utf8ToFsCharset(utf8file);
+
+	rfile = xmalloc(strlen(file) + strlen(".") +
+	                strlen(PLAYLIST_FILE_SUFFIX) + 1);
+
+	strcpy(rfile, file);
+	strcat(rfile, ".");
+	strcat(rfile, PLAYLIST_FILE_SUFFIX);
+
+	actualFile = rpp2app(rfile);
+
+	free(rfile);
+
+	while (!(fileP = fopen(actualFile, "a")) && errno == EINTR);
+	if (fileP == NULL) {
+		commandError(fd, ACK_ERROR_SYSTEM, "problems opening file");
+		return -1;
+	}
+
+	url = utf8ToFsCharset(getSongUrl(song));
+
+	if (playlist_saveAbsolutePaths && song->type == SONG_TYPE_FILE)
+		fprintf(fileP, "%s\n", rmp2amp(url));
+	else
+		fprintf(fileP, "%s\n", url);
+
+	while (fclose(fileP) && errno == EINTR);
+
+	return 0;
+}
+
 int swapSongsInPlaylist(int fd, int song1, int song2)
 {
 	int queuedSong = -1;
diff --git a/src/playlist.h b/src/playlist.h
index c5f291101..286450103 100644
--- a/src/playlist.h
+++ b/src/playlist.h
@@ -40,10 +40,16 @@ void savePlaylistState(FILE *);
 
 int clearPlaylist(int fd);
 
+int clearStoredPlaylist(int fd, char *utf8file);
+
 int addToPlaylist(int fd, char *file, int printId);
 
+int addToStoredPlaylist(int fd, char *file, char *utf8file);
+
 int addSongToPlaylist(int fd, Song * song, int printId);
 
+int addSongToStoredPlaylist(int fd, Song *song, char *utf8file);
+
 int showPlaylist(int fd);
 
 int deleteFromPlaylist(int fd, int song);