summaryrefslogtreecommitdiff
path: root/src/song.c
blob: 41e3d4b66ff756f788c41aa05894c109ed41c1b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* the Music Player Daemon (MPD)
 * (c)2003-2004 by Warren Dukes (shank@mercury.chem.pitt.edu)
 * 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
 */

#include "song.h"
#include "ls.h"
#include "directory.h"
#include "tables.h"
#include "utils.h"
#include "tag.h"
#include "log.h"
#include "path.h"
#include "playlist.h"
#include "tables.h"
#include "inputPlugin.h"

#define SONG_KEY	"key: "
#define SONG_FILE	"file: "
#define SONG_ARTIST	"Artist: "
#define SONG_ALBUM	"Album: "
#define SONG_TRACK	"Track: "
#define SONG_TITLE	"Title: "
#define SONG_NAME	"Name: "
#define SONG_TIME	"Time: "
#define SONG_MTIME	"mtime: "

#include <stdlib.h>
#include <string.h>

Song * newNullSong() {
	Song * song = malloc(sizeof(Song));

	song->tag = NULL;
	song->utf8url = NULL;
	song->type = SONG_TYPE_FILE;

	return song;
}

Song * newSong(char * utf8url, SONG_TYPE type) {
	Song * song = NULL;

        if(strchr(utf8url, '\n')) return NULL;

        song  = newNullSong();

	song->utf8url = strdup(utf8url);
	song->type = type;

	if(song->type == SONG_TYPE_FILE) {
                InputPlugin * plugin;
		if((plugin = isMusic(utf8url,&(song->mtime)))) {
		        song->tag = plugin->tagDupFunc(
                                        rmp2amp(utf8ToFsCharset(utf8url)));
	                if(song->tag) validateUtf8Tag(song->tag);
                }
		if(!song->tag || song->tag->time<0) {
			freeSong(song);
			song = NULL;
		}
		else addSongToTables(song);
	}

	return song;
}

void freeSong(Song * song) {
	deleteASongFromPlaylist(song);
	if(song->type == SONG_TYPE_FILE) removeASongFromTables(song);
	free(song->utf8url);
	if(song->tag) freeMpdTag(song->tag);
	free(song);
}

void freeJustSong(Song * song) {
	free(song->utf8url);
	if(song->tag) freeMpdTag(song->tag);
	free(song);
}

SongList * newSongList() {
	return makeList((ListFreeDataFunc *)freeSong);
}

Song * addSongToList(SongList * list, char * key, char * utf8url, 
		SONG_TYPE type) 
{
	Song * song = NULL;

	switch(type) {
	case SONG_TYPE_FILE:
		if(isMusic(utf8url,NULL)) {
			song = newSong(utf8url,type);
		}
		break;
	case SONG_TYPE_URL:
		song = newSong(utf8url,type);
		break;
	}

	if(song==NULL) return NULL;
	
	insertInList(list,key,(void *)song);

	return song;
}

void freeSongList(SongList * list) {
	freeList(list);
}

int printSongInfo(FILE * fp, Song * song) {
	myfprintf(fp,"%s%s\n",SONG_FILE,song->utf8url);

	if(song->tag) printMpdTag(fp,song->tag);

	return 0;
}

int printSongInfoFromList(FILE * fp, SongList * list) {
	ListNode * tempNode = list->firstNode;

	while(tempNode!=NULL) {
		printSongInfo(fp,(Song *)tempNode->data);
		tempNode = tempNode->nextNode;
	}

	return 0;
}

void writeSongInfoFromList(FILE * fp, SongList * list) {
	ListNode * tempNode = list->firstNode;

	myfprintf(fp,"%s\n",SONG_BEGIN);

	while(tempNode!=NULL) {
		myfprintf(fp,"%s%s\n",SONG_KEY,tempNode->key);
		printSongInfo(fp,(Song *)tempNode->data);
		myfprintf(fp,"%s%li\n",SONG_MTIME,(long)((Song *)tempNode->data)->mtime);
		tempNode = tempNode->nextNode;
	}

	myfprintf(fp,"%s\n",SONG_END);
}

void insertSongIntoList(SongList * list, ListNode ** nextSongNode, char * key,
		Song * song)
{
	ListNode * nodeTemp;
	int cmpRet= 0;

	while(*nextSongNode && (cmpRet = strcmp(key,(*nextSongNode)->key)) > 0) 
	{
		nodeTemp = (*nextSongNode)->nextNode;
		deleteNodeFromList(list,*nextSongNode);
		*nextSongNode = nodeTemp;
	}

	if(!(*nextSongNode)) {
		insertInList(list,key,(void *)song);
		addSongToTables(song);
	}
	else if(cmpRet == 0) {
		Song * tempSong = (Song *)((*nextSongNode)->data);
		if(tempSong->mtime != song->mtime) {
			removeASongFromTables(tempSong);
			freeMpdTag(tempSong->tag);
			tempSong->tag = song->tag;
			tempSong->mtime = song->mtime;
			song->tag = NULL;
			addSongToTables(tempSong);
		}
		freeJustSong(song);
		*nextSongNode = (*nextSongNode)->nextNode;
	}
	else {
		addSongToTables(song);
		insertInListBeforeNode(list,*nextSongNode,key,(void *)song);
	}
}

void readSongInfoIntoList(FILE * fp, SongList * list) {
	char buffer[MAXPATHLEN+1024];
	int bufferSize = MAXPATHLEN+1024;
	Song * song = NULL;
	char * key = NULL;
	ListNode * nextSongNode = list->firstNode;
	ListNode * nodeTemp;

	while(myFgets(buffer,bufferSize,fp) && 0!=strcmp(SONG_END,buffer)) {
		if(0==strncmp(SONG_KEY,buffer,strlen(SONG_KEY))) {
			if(song) {
				insertSongIntoList(list,&nextSongNode,key,song);
				song = NULL;
				free(key);
			}

			key = strdup(&(buffer[strlen(SONG_KEY)]));
			song = newNullSong();
			song->type = SONG_TYPE_FILE;
		}
		else if(0==strncmp(SONG_FILE,buffer,strlen(SONG_FILE))) {
			if(!song || song->utf8url) {
				ERROR("Problems reading song info\n");
				exit(EXIT_FAILURE);
			}
			song->utf8url = strdup(&(buffer[strlen(SONG_FILE)]));
		}
		else if(0==strncmp(SONG_ARTIST,buffer,strlen(SONG_ARTIST))) {
			if(!song->tag) song->tag = newMpdTag();
			song->tag->artist = strdup(&(buffer[strlen(SONG_ARTIST)]));
		}
		else if(0==strncmp(SONG_ALBUM,buffer,strlen(SONG_ALBUM))) {
			if(!song->tag) song->tag = newMpdTag();
			song->tag->album = strdup(&(buffer[strlen(SONG_ALBUM)]));
		}
		else if(0==strncmp(SONG_TRACK,buffer,strlen(SONG_TRACK))) {
			if(!song->tag) song->tag = newMpdTag();
			song->tag->track = strdup(&(buffer[strlen(SONG_TRACK)]));
		}
		else if(0==strncmp(SONG_TITLE,buffer,strlen(SONG_TITLE))) {
			if(!song->tag) song->tag = newMpdTag();
			song->tag->title = strdup(&(buffer[strlen(SONG_TITLE)]));
		}
		else if(0==strncmp(SONG_NAME,buffer,strlen(SONG_NAME))) {
			if(!song->tag) song->tag = newMpdTag();
			song->tag->name = strdup(&(buffer[strlen(SONG_NAME)]));
		}
		else if(0==strncmp(SONG_TIME,buffer,strlen(SONG_TIME))) {
			if(!song->tag) song->tag = newMpdTag();
			song->tag->time = atoi(&(buffer[strlen(SONG_TIME)]));
		}
		else if(0==strncmp(SONG_MTIME,buffer,strlen(SONG_MTIME))) {
			song->mtime = atoi(&(buffer[strlen(SONG_TITLE)]));
		}
		else {
			ERROR("songinfo: unknown line in db: %s\n",buffer);
			exit(EXIT_FAILURE);
		}
	}
	
	if(song) {
		insertSongIntoList(list,&nextSongNode,key,song);
		song = NULL;
		free(key);
	}

	while(nextSongNode) {
		nodeTemp = nextSongNode->nextNode;
		deleteNodeFromList(list,nextSongNode);
		nextSongNode = nodeTemp;
	}
}

int updateSongInfo(Song * song) {
	char * utf8url = song->utf8url;

	if(song->type == SONG_TYPE_FILE) {
                InputPlugin * plugin;

		removeASongFromTables(song);
		if(song->tag) freeMpdTag(song->tag);

		song->tag = NULL;

		if((plugin = isMusic(utf8url,&(song->mtime)))) {
		        song->tag = plugin->tagDupFunc(
                                        rmp2amp(utf8ToFsCharset(utf8url)));
	                if(song->tag) validateUtf8Tag(song->tag);
                }
		if(!song->tag || song->tag->time<0) return -1;
		else addSongToTables(song);
	}

	return 0;
}

Song * songDup(Song * song) {
	Song * ret = malloc(sizeof(Song));

	ret->utf8url = strdup(song->utf8url);
	ret->mtime = song->mtime;
	ret->tag = mpdTagDup(song->tag);
	ret->type = song->type;

	return ret;
}
/* vim:set shiftwidth=4 tabstop=8 expandtab: */