2006-07-13 21:20:34 +02:00
|
|
|
/* the Music Player Daemon (MPD)
|
2006-07-14 21:37:45 +02:00
|
|
|
* (c)2003-2006 by Warren Dukes (warren.dukes@gmail.com)
|
2006-07-13 21:20:34 +02:00
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2004-11-10 22:58:27 +01:00
|
|
|
#include "dbUtils.h"
|
|
|
|
|
|
|
|
#include "directory.h"
|
|
|
|
#include "myfprintf.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "playlist.h"
|
|
|
|
#include "tag.h"
|
|
|
|
#include "tagTracker.h"
|
2004-11-11 03:36:25 +01:00
|
|
|
#include "log.h"
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2004-11-10 23:12:11 +01:00
|
|
|
#define LOCATE_TAG_FILE_TYPE TAG_NUM_OF_ITEM_TYPES+10
|
|
|
|
#define LOCATE_TAG_FILE_KEY "filename"
|
2005-03-01 00:57:47 +01:00
|
|
|
#define LOCATE_TAG_ANY_TYPE TAG_NUM_OF_ITEM_TYPES+20
|
|
|
|
#define LOCATE_TAG_ANY_KEY "any"
|
|
|
|
|
2004-11-10 23:12:11 +01:00
|
|
|
|
2004-11-12 02:44:27 +01:00
|
|
|
typedef struct _ListCommandItem {
|
2004-11-10 22:58:27 +01:00
|
|
|
mpd_sint8 tagType;
|
|
|
|
int numConditionals;
|
|
|
|
LocateTagItem * conditionals;
|
|
|
|
} ListCommandItem;
|
|
|
|
|
2004-11-12 02:44:27 +01:00
|
|
|
typedef struct _LocateTagItemArray {
|
|
|
|
int numItems;
|
|
|
|
LocateTagItem * items;
|
|
|
|
} LocateTagItemArray;
|
|
|
|
|
2004-11-10 22:58:27 +01:00
|
|
|
int getLocateTagItemType(char * str) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if(0 == strcasecmp(str, LOCATE_TAG_FILE_KEY)) {
|
|
|
|
return LOCATE_TAG_FILE_TYPE;
|
|
|
|
}
|
|
|
|
|
2005-03-01 00:57:47 +01:00
|
|
|
if(0 == strcasecmp(str, LOCATE_TAG_ANY_KEY)) {
|
|
|
|
return LOCATE_TAG_ANY_TYPE;
|
|
|
|
}
|
|
|
|
|
2004-11-10 22:58:27 +01:00
|
|
|
for(i = 0; i < TAG_NUM_OF_ITEM_TYPES; i++) {
|
|
|
|
if(0 == strcasecmp(str, mpdTagItemKeys[i])) return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-11-12 02:44:27 +01:00
|
|
|
static int initLocateTagItem(LocateTagItem * item, char * typeStr,
|
|
|
|
char * needle)
|
|
|
|
{
|
|
|
|
item->tagType = getLocateTagItemType(typeStr);
|
|
|
|
|
|
|
|
if(item->tagType < 0) return -1;
|
|
|
|
|
|
|
|
item->needle = strdup(needle);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-11-10 22:58:27 +01:00
|
|
|
LocateTagItem * newLocateTagItem(char * typeStr, char * needle) {
|
|
|
|
LocateTagItem * ret = malloc(sizeof(LocateTagItem));
|
|
|
|
|
2004-11-12 02:44:27 +01:00
|
|
|
if(initLocateTagItem(ret, typeStr, needle) < 0) {
|
2004-11-10 22:58:27 +01:00
|
|
|
free(ret);
|
2004-11-12 02:44:27 +01:00
|
|
|
ret = NULL;
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2004-11-12 02:44:27 +01:00
|
|
|
void freeLocateTagItemArray(int count, LocateTagItem * array) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i = 0; i < count; i++) free(array[i].needle);
|
|
|
|
|
|
|
|
free(array);
|
|
|
|
}
|
|
|
|
|
|
|
|
int newLocateTagItemArrayFromArgArray(char * argArray[],
|
|
|
|
int numArgs,
|
|
|
|
LocateTagItem ** arrayRet)
|
|
|
|
{
|
|
|
|
int i,j;
|
|
|
|
LocateTagItem * item;
|
|
|
|
|
|
|
|
if(numArgs == 0) return 0;
|
|
|
|
|
|
|
|
if(numArgs%2 != 0) return -1;
|
|
|
|
|
|
|
|
*arrayRet = malloc(sizeof(LocateTagItem)*numArgs/2);
|
|
|
|
|
|
|
|
for(i = 0, item = *arrayRet; i < numArgs/2; i++, item++) {
|
|
|
|
if(initLocateTagItem(item, argArray[i*2], argArray[i*2+1]) < 0)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
return numArgs/2;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
for(j = 0; j < i; j++) {
|
|
|
|
free((*arrayRet)[j].needle);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(*arrayRet);
|
|
|
|
*arrayRet = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-11-10 22:58:27 +01:00
|
|
|
void freeLocateTagItem(LocateTagItem * item) {
|
|
|
|
free(item->needle);
|
|
|
|
free(item);
|
|
|
|
}
|
|
|
|
|
2006-07-13 20:46:58 +02:00
|
|
|
static int countSongsInDirectory(FILE * fp, Directory * directory, void * data) {
|
2004-11-10 22:58:27 +01:00
|
|
|
int * count = (int *)data;
|
|
|
|
|
|
|
|
*count+=directory->songs->numberOfNodes;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-13 20:46:58 +02:00
|
|
|
static int printDirectoryInDirectory(FILE * fp, Directory * directory, void * data) {
|
2004-11-12 18:38:52 +01:00
|
|
|
if(directory->path) {
|
2004-11-11 05:34:26 +01:00
|
|
|
myfprintf(fp,"directory: %s\n", getDirectoryPath(directory));
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-13 20:46:58 +02:00
|
|
|
static int printSongInDirectory(FILE * fp, Song * song, void * data) {
|
2004-11-11 01:41:28 +01:00
|
|
|
printSongUrl(fp, song);
|
2004-11-10 22:58:27 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-15 15:42:57 +02:00
|
|
|
static int strstrSearchTag(Song * song, int type, char * str) {
|
2004-11-10 22:58:27 +01:00
|
|
|
int i;
|
|
|
|
char * dup;
|
|
|
|
int ret = 0;
|
|
|
|
|
2005-03-01 00:57:47 +01:00
|
|
|
if(type == LOCATE_TAG_FILE_TYPE || type == LOCATE_TAG_ANY_TYPE) {
|
2004-11-11 03:36:25 +01:00
|
|
|
dup = strDupToUpper(getSongUrl(song));
|
2004-11-10 22:58:27 +01:00
|
|
|
if(strstr(dup, str)) ret = 1;
|
|
|
|
free(dup);
|
2005-03-01 00:57:47 +01:00
|
|
|
if (ret == 1 || type == LOCATE_TAG_FILE_TYPE) {
|
|
|
|
return ret;
|
|
|
|
}
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!song->tag) return 0;
|
|
|
|
|
|
|
|
for(i = 0; i < song->tag->numOfItems && !ret; i++) {
|
2005-03-01 00:57:47 +01:00
|
|
|
if(type != LOCATE_TAG_ANY_TYPE &&
|
|
|
|
song->tag->items[i].type != type)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2004-11-10 22:58:27 +01:00
|
|
|
|
|
|
|
dup = strDupToUpper(song->tag->items[i].value);
|
|
|
|
if(strstr(dup, str)) ret = 1;
|
|
|
|
free(dup);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-07-13 20:46:58 +02:00
|
|
|
static int searchInDirectory(FILE * fp, Song * song, void * data) {
|
2004-11-12 02:44:27 +01:00
|
|
|
LocateTagItemArray * array = data;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i = 0; i < array->numItems; i++) {
|
|
|
|
if(!strstrSearchTag(song, array->items[i].tagType,
|
|
|
|
array->items[i].needle))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
2004-11-12 02:44:27 +01:00
|
|
|
|
|
|
|
printSongInfo(fp, song);
|
|
|
|
|
2004-11-10 22:58:27 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-11-12 02:44:27 +01:00
|
|
|
int searchForSongsIn(FILE * fp, char * name, int numItems,
|
|
|
|
LocateTagItem * items)
|
|
|
|
{
|
2004-11-10 22:58:27 +01:00
|
|
|
int ret = -1;
|
2004-11-12 02:44:27 +01:00
|
|
|
int i;
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2004-11-12 02:44:27 +01:00
|
|
|
char ** originalNeedles = malloc(numItems*sizeof(char *));
|
|
|
|
LocateTagItemArray array;
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2004-11-12 02:44:27 +01:00
|
|
|
for(i = 0; i < numItems; i++) {
|
|
|
|
originalNeedles[i] = items[i].needle;
|
|
|
|
items[i].needle = strDupToUpper(originalNeedles[i]);
|
|
|
|
}
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2004-11-12 02:44:27 +01:00
|
|
|
array.numItems = numItems;
|
|
|
|
array.items = items;
|
|
|
|
|
|
|
|
ret = traverseAllIn(fp,name,searchInDirectory, NULL, &array);
|
|
|
|
|
|
|
|
for(i = 0; i < numItems; i++) {
|
|
|
|
free(items[i].needle);
|
|
|
|
items[i].needle = originalNeedles[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
free(originalNeedles);
|
2004-11-10 22:58:27 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-07-15 15:42:57 +02:00
|
|
|
static int tagItemFoundAndMatches(Song * song, int type, char * str) {
|
2004-11-10 22:58:27 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if(type == LOCATE_TAG_FILE_TYPE) {
|
2004-11-11 03:36:25 +01:00
|
|
|
if(0 == strcmp(str, getSongUrl(song))) return 1;
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!song->tag) return 0;
|
|
|
|
|
|
|
|
for(i = 0; i < song->tag->numOfItems; i++) {
|
|
|
|
if(song->tag->items[i].type != type) continue;
|
|
|
|
|
|
|
|
if(0 == strcmp(str, song->tag->items[i].value)) return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-13 20:46:58 +02:00
|
|
|
static int findInDirectory(FILE * fp, Song * song, void * data) {
|
2004-11-12 02:44:27 +01:00
|
|
|
LocateTagItemArray * array = data;
|
|
|
|
int i;
|
|
|
|
|
2004-11-13 20:17:38 +01:00
|
|
|
for(i = 0; i < array->numItems; i++) {
|
2004-11-12 02:44:27 +01:00
|
|
|
if(!tagItemFoundAndMatches(song, array->items[i].tagType,
|
|
|
|
array->items[i].needle))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
2004-11-12 02:44:27 +01:00
|
|
|
|
|
|
|
printSongInfo(fp, song);
|
|
|
|
|
2004-11-10 22:58:27 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-11-12 02:44:27 +01:00
|
|
|
int findSongsIn(FILE * fp, char * name, int numItems, LocateTagItem * items) {
|
|
|
|
LocateTagItemArray array;
|
|
|
|
|
|
|
|
array.numItems = numItems;
|
|
|
|
array.items = items;
|
|
|
|
|
2004-11-10 22:58:27 +01:00
|
|
|
return traverseAllIn(fp, name, findInDirectory, NULL,
|
2004-11-12 02:44:27 +01:00
|
|
|
(void *)&array);
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int printAllIn(FILE * fp, char * name) {
|
|
|
|
return traverseAllIn(fp,name,printSongInDirectory,
|
|
|
|
printDirectoryInDirectory,NULL);
|
|
|
|
}
|
|
|
|
|
2006-07-13 20:46:58 +02:00
|
|
|
static int directoryAddSongToPlaylist(FILE * fp, Song * song, void * data) {
|
2004-11-10 22:58:27 +01:00
|
|
|
return addSongToPlaylist(fp, song, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int addAllIn(FILE * fp, char * name) {
|
|
|
|
return traverseAllIn(fp,name,directoryAddSongToPlaylist,NULL,NULL);
|
|
|
|
}
|
|
|
|
|
2006-07-13 20:46:58 +02:00
|
|
|
static int directoryPrintSongInfo(FILE * fp, Song * song, void * data) {
|
2004-11-10 22:58:27 +01:00
|
|
|
return printSongInfo(fp,song);
|
|
|
|
}
|
|
|
|
|
2006-07-13 20:46:58 +02:00
|
|
|
static int sumSongTime(FILE * fp, Song * song, void * data) {
|
2004-11-10 22:58:27 +01:00
|
|
|
unsigned long * time = (unsigned long *)data;
|
|
|
|
|
|
|
|
if(song->tag && song->tag->time>=0) *time+=song->tag->time;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int printInfoForAllIn(FILE * fp, char * name) {
|
|
|
|
return traverseAllIn(fp,name,directoryPrintSongInfo,printDirectoryInDirectory,NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int countSongsIn(FILE * fp, char * name) {
|
|
|
|
int count = 0;
|
|
|
|
void * ptr = (void *)&count;
|
|
|
|
|
|
|
|
traverseAllIn(fp,name,NULL,countSongsInDirectory,ptr);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long sumSongTimesIn(FILE * fp, char * name) {
|
|
|
|
unsigned long dbPlayTime = 0;
|
|
|
|
void * ptr = (void *)&dbPlayTime;
|
|
|
|
|
|
|
|
traverseAllIn(fp,name,sumSongTime,NULL,ptr);
|
|
|
|
|
|
|
|
return dbPlayTime;
|
|
|
|
}
|
|
|
|
|
2006-07-13 20:46:58 +02:00
|
|
|
static ListCommandItem * newListCommandItem(int tagType, int numConditionals,
|
2004-11-10 22:58:27 +01:00
|
|
|
LocateTagItem * conditionals)
|
|
|
|
{
|
|
|
|
ListCommandItem * item = malloc(sizeof(ListCommandItem));
|
|
|
|
|
|
|
|
item->tagType = tagType;
|
|
|
|
item->numConditionals = numConditionals;
|
|
|
|
item->conditionals = conditionals;
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2006-07-13 20:46:58 +02:00
|
|
|
static void freeListCommandItem(ListCommandItem * item) {
|
2004-11-10 22:58:27 +01:00
|
|
|
free(item);
|
|
|
|
}
|
|
|
|
|
2006-07-13 20:46:58 +02:00
|
|
|
static void visitTag(FILE * fp, Song * song, int tagType) {
|
2004-11-10 22:58:27 +01:00
|
|
|
int i;
|
|
|
|
MpdTag * tag = song->tag;
|
|
|
|
|
|
|
|
if(tagType == LOCATE_TAG_FILE_TYPE) {
|
2004-11-11 03:36:25 +01:00
|
|
|
printSongUrl(fp, song);
|
2004-11-10 22:58:27 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!tag) return;
|
|
|
|
|
|
|
|
for(i = 0; i < tag->numOfItems; i++) {
|
2004-11-11 14:15:41 +01:00
|
|
|
if(tag->items[i].type == tagType) {
|
|
|
|
visitInTagTracker(tagType, tag->items[i].value);
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-13 20:46:58 +02:00
|
|
|
static int listUniqueTagsInDirectory(FILE * fp, Song * song, void * data) {
|
2004-11-10 22:58:27 +01:00
|
|
|
ListCommandItem * item = data;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i = 0; i < item->numConditionals; i++) {
|
|
|
|
if(!tagItemFoundAndMatches(song, item->conditionals[i].tagType,
|
|
|
|
item->conditionals[i].needle))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-11 14:15:41 +01:00
|
|
|
visitTag(fp, song, item->tagType);
|
2004-11-10 22:58:27 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int listAllUniqueTags(FILE * fp, int type, int numConditionals,
|
|
|
|
LocateTagItem * conditionals)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
ListCommandItem * item = newListCommandItem(type, numConditionals,
|
|
|
|
conditionals);
|
|
|
|
|
|
|
|
if(type >= 0 && type <= TAG_NUM_OF_ITEM_TYPES) {
|
|
|
|
resetVisitedFlagsInTagTracker(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = traverseAllIn(fp, NULL, listUniqueTagsInDirectory, NULL,
|
|
|
|
(void *)item);
|
|
|
|
|
2004-11-11 14:15:41 +01:00
|
|
|
if(type >= 0 && type <= TAG_NUM_OF_ITEM_TYPES) {
|
|
|
|
printVisitedInTagTracker(fp, type);
|
|
|
|
}
|
|
|
|
|
2004-11-10 22:58:27 +01:00
|
|
|
freeListCommandItem(item);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2004-11-11 03:36:25 +01:00
|
|
|
|
2006-07-13 20:46:58 +02:00
|
|
|
static int sumSavedFilenameMemoryInDirectory(FILE * fp, Directory * dir, void * data) {
|
2004-11-11 03:36:25 +01:00
|
|
|
int * sum = data;
|
|
|
|
|
2004-11-12 18:38:52 +01:00
|
|
|
if(!dir->path) return 0;
|
2004-11-11 03:36:25 +01:00
|
|
|
|
2004-11-11 05:34:26 +01:00
|
|
|
*sum += (strlen(getDirectoryPath(dir))+1-sizeof(Directory *))*
|
2004-11-11 03:36:25 +01:00
|
|
|
dir->songs->numberOfNodes;
|
2004-11-11 03:59:16 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-13 20:46:58 +02:00
|
|
|
static int sumSavedFilenameMemoryInSong(FILE * fp, Song * song, void * data) {
|
2004-11-11 03:59:16 +01:00
|
|
|
int * sum = data;
|
|
|
|
|
|
|
|
*sum += strlen(song->url)+1;
|
2004-11-11 03:36:25 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-13 21:40:14 +02:00
|
|
|
void printSavedMemoryFromFilenames() {
|
|
|
|
int sum = 0;
|
2004-11-11 06:25:05 +01:00
|
|
|
|
2006-07-13 21:40:14 +02:00
|
|
|
traverseAllIn(stderr, NULL, sumSavedFilenameMemoryInSong,
|
|
|
|
sumSavedFilenameMemoryInDirectory, (void *)&sum);
|
|
|
|
|
|
|
|
DEBUG("saved memory from filenames: %i\n", sum);
|
|
|
|
}
|