2006-07-13 21:20:34 +02:00
|
|
|
/* the Music Player Daemon (MPD)
|
2007-04-05 05:22:33 +02:00
|
|
|
* Copyright (C) 2003-2007 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"
|
2006-10-28 01:07:34 +02:00
|
|
|
#include "song.h"
|
2004-11-10 22:58:27 +01:00
|
|
|
#include "tag.h"
|
|
|
|
#include "tagTracker.h"
|
2004-11-11 03:36:25 +01:00
|
|
|
#include "log.h"
|
2007-05-16 14:02:10 +02:00
|
|
|
#include "storedPlaylist.h"
|
2004-11-10 22:58:27 +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;
|
2006-07-20 18:02:40 +02:00
|
|
|
LocateTagItem *conditionals;
|
2004-11-10 22:58:27 +01:00
|
|
|
} ListCommandItem;
|
|
|
|
|
2004-11-12 02:44:27 +01:00
|
|
|
typedef struct _LocateTagItemArray {
|
|
|
|
int numItems;
|
2006-07-20 18:02:40 +02:00
|
|
|
LocateTagItem *items;
|
2004-11-12 02:44:27 +01:00
|
|
|
} LocateTagItemArray;
|
|
|
|
|
2007-04-26 01:46:11 +02:00
|
|
|
typedef struct _SearchStats {
|
|
|
|
LocateTagItemArray locateArray;
|
|
|
|
int numberOfSongs;
|
2007-04-26 02:11:30 +02:00
|
|
|
unsigned long playTime;
|
2007-04-26 01:46:11 +02:00
|
|
|
} SearchStats;
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
static int countSongsInDirectory(int fd, Directory * directory, void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
int *count = (int *)data;
|
|
|
|
|
|
|
|
*count += directory->songs->numberOfNodes;
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
return 0;
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
static int printDirectoryInDirectory(int fd, Directory * directory,
|
2006-07-20 18:02:40 +02:00
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
if (directory->path) {
|
2006-07-30 05:43:38 +02:00
|
|
|
fdprintf(fd, "directory: %s\n", getDirectoryPath(directory));
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
return 0;
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
static int printSongInDirectory(int fd, Song * song, void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2006-07-30 05:43:38 +02:00
|
|
|
printSongUrl(fd, song);
|
2006-07-20 18:02:40 +02:00
|
|
|
return 0;
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
static int searchInDirectory(int fd, Song * song, void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
LocateTagItemArray *array = data;
|
2004-11-12 02:44:27 +01:00
|
|
|
|
2007-02-24 01:54:54 +01:00
|
|
|
if (strstrSearchTags(song, array->numItems, array->items))
|
|
|
|
printSongInfo(fd, song);
|
2004-11-12 02:44:27 +01:00
|
|
|
|
2004-11-10 22:58:27 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int searchForSongsIn(int fd, char *name, int numItems, LocateTagItem * items)
|
2004-11-12 02:44:27 +01:00
|
|
|
{
|
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
|
|
|
|
2006-08-26 08:25:57 +02:00
|
|
|
char **originalNeedles = xmalloc(numItems * sizeof(char *));
|
2004-11-12 02:44:27 +01:00
|
|
|
LocateTagItemArray array;
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < numItems; i++) {
|
2004-11-12 02:44:27 +01:00
|
|
|
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;
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
ret = traverseAllIn(fd, name, searchInDirectory, NULL, &array);
|
2004-11-12 02:44:27 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < numItems; i++) {
|
2004-11-12 02:44:27 +01:00
|
|
|
free(items[i].needle);
|
|
|
|
items[i].needle = originalNeedles[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
free(originalNeedles);
|
2004-11-10 22:58:27 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
static int findInDirectory(int fd, Song * song, void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
LocateTagItemArray *array = data;
|
2004-11-12 02:44:27 +01:00
|
|
|
|
2007-02-24 01:54:54 +01:00
|
|
|
if (tagItemsFoundAndMatches(song, array->numItems, array->items))
|
|
|
|
printSongInfo(fd, song);
|
2004-11-12 02:44:27 +01:00
|
|
|
|
2004-11-10 22:58:27 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int findSongsIn(int fd, char *name, int numItems, LocateTagItem * items)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-11-12 02:44:27 +01:00
|
|
|
LocateTagItemArray array;
|
|
|
|
|
|
|
|
array.numItems = numItems;
|
|
|
|
array.items = items;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
return traverseAllIn(fd, name, findInDirectory, NULL, (void *)&array);
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2007-04-26 01:46:11 +02:00
|
|
|
static void printSearchStats(int fd, SearchStats *stats)
|
|
|
|
{
|
|
|
|
fdprintf(fd, "songs: %i\n", stats->numberOfSongs);
|
2007-04-26 02:13:14 +02:00
|
|
|
fdprintf(fd, "playtime: %li\n", stats->playTime);
|
2007-04-26 01:46:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int searchStatsInDirectory(int fd, Song * song, void *data)
|
|
|
|
{
|
|
|
|
SearchStats *stats = data;
|
|
|
|
|
|
|
|
if (tagItemsFoundAndMatches(song, stats->locateArray.numItems,
|
|
|
|
stats->locateArray.items)) {
|
|
|
|
stats->numberOfSongs++;
|
|
|
|
if (song->tag->time > 0)
|
|
|
|
stats->playTime += song->tag->time;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int searchStatsForSongsIn(int fd, char *name, int numItems,
|
|
|
|
LocateTagItem * items)
|
|
|
|
{
|
|
|
|
SearchStats stats;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
stats.locateArray.numItems = numItems;
|
|
|
|
stats.locateArray.items = items;
|
|
|
|
stats.numberOfSongs = 0;
|
|
|
|
stats.playTime = 0;
|
|
|
|
|
|
|
|
ret = traverseAllIn(fd, name, searchStatsInDirectory, NULL, &stats);
|
|
|
|
if (ret == 0)
|
|
|
|
printSearchStats(fd, &stats);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int printAllIn(int fd, char *name)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2006-07-30 05:43:38 +02:00
|
|
|
return traverseAllIn(fd, name, printSongInDirectory,
|
2006-07-20 18:02:40 +02:00
|
|
|
printDirectoryInDirectory, NULL);
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
static int directoryAddSongToPlaylist(int fd, Song * song, void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-01-26 13:46:49 +01:00
|
|
|
return addSongToPlaylist(fd, song, NULL);
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2006-11-20 16:37:58 +01:00
|
|
|
static int directoryAddSongToStoredPlaylist(int fd, Song *song, void *data)
|
|
|
|
{
|
2007-05-16 14:02:10 +02:00
|
|
|
if (appendSongToStoredPlaylistByPath(fd, (char *)data, song) != 0)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
2006-11-20 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int addAllIn(int fd, char *name)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2006-07-30 05:43:38 +02:00
|
|
|
return traverseAllIn(fd, name, directoryAddSongToPlaylist, NULL, NULL);
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2006-11-20 16:37:58 +01:00
|
|
|
int addAllInToStoredPlaylist(int fd, char *name, char *utf8file)
|
|
|
|
{
|
|
|
|
return traverseAllIn(fd, name, directoryAddSongToStoredPlaylist, NULL,
|
|
|
|
(void *)utf8file);
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
static int directoryPrintSongInfo(int fd, Song * song, void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2006-07-30 05:43:38 +02:00
|
|
|
return printSongInfo(fd, song);
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
static int sumSongTime(int fd, Song * song, void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-01-26 13:46:21 +01:00
|
|
|
unsigned long *sum_time = (unsigned long *)data;
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (song->tag && song->tag->time >= 0)
|
2008-01-26 13:46:21 +01:00
|
|
|
*sum_time += song->tag->time;
|
2004-11-10 22:58:27 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int printInfoForAllIn(int fd, char *name)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2006-07-30 05:43:38 +02:00
|
|
|
return traverseAllIn(fd, name, directoryPrintSongInfo,
|
2006-07-20 18:02:40 +02:00
|
|
|
printDirectoryInDirectory, NULL);
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int countSongsIn(int fd, char *name)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-11-10 22:58:27 +01:00
|
|
|
int count = 0;
|
2006-07-20 18:02:40 +02:00
|
|
|
void *ptr = (void *)&count;
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
traverseAllIn(fd, name, NULL, countSongsInDirectory, ptr);
|
2004-11-10 22:58:27 +01:00
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
unsigned long sumSongTimesIn(int fd, char *name)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-11-10 22:58:27 +01:00
|
|
|
unsigned long dbPlayTime = 0;
|
2006-07-20 18:02:40 +02:00
|
|
|
void *ptr = (void *)&dbPlayTime;
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
traverseAllIn(fd, name, sumSongTime, NULL, ptr);
|
2004-11-10 22:58:27 +01:00
|
|
|
|
|
|
|
return dbPlayTime;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static ListCommandItem *newListCommandItem(int tagType, int numConditionals,
|
|
|
|
LocateTagItem * conditionals)
|
2004-11-10 22:58:27 +01:00
|
|
|
{
|
2006-08-26 08:25:57 +02:00
|
|
|
ListCommandItem *item = xmalloc(sizeof(ListCommandItem));
|
2004-11-10 22:58:27 +01:00
|
|
|
|
|
|
|
item->tagType = tagType;
|
|
|
|
item->numConditionals = numConditionals;
|
|
|
|
item->conditionals = conditionals;
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void freeListCommandItem(ListCommandItem * item)
|
|
|
|
{
|
2004-11-10 22:58:27 +01:00
|
|
|
free(item);
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
static void visitTag(int fd, Song * song, int tagType)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-11-10 22:58:27 +01:00
|
|
|
int i;
|
2006-07-20 18:02:40 +02:00
|
|
|
MpdTag *tag = song->tag;
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (tagType == LOCATE_TAG_FILE_TYPE) {
|
2006-07-30 05:43:38 +02:00
|
|
|
printSongUrl(fd, song);
|
2004-11-10 22:58:27 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!tag)
|
|
|
|
return;
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < tag->numOfItems; i++) {
|
|
|
|
if (tag->items[i].type == tagType) {
|
2004-11-11 14:15:41 +01:00
|
|
|
visitInTagTracker(tagType, tag->items[i].value);
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
static int listUniqueTagsInDirectory(int fd, Song * song, void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
ListCommandItem *item = data;
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2007-02-24 01:54:54 +01:00
|
|
|
if (tagItemsFoundAndMatches(song, item->numConditionals,
|
|
|
|
item->conditionals)) {
|
|
|
|
visitTag(fd, song, item->tagType);
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int listAllUniqueTags(int fd, int type, int numConditionals,
|
2006-07-20 18:02:40 +02:00
|
|
|
LocateTagItem * conditionals)
|
2004-11-10 22:58:27 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2006-07-20 18:02:40 +02:00
|
|
|
ListCommandItem *item = newListCommandItem(type, numConditionals,
|
|
|
|
conditionals);
|
|
|
|
|
|
|
|
if (type >= 0 && type <= TAG_NUM_OF_ITEM_TYPES) {
|
2004-11-10 22:58:27 +01:00
|
|
|
resetVisitedFlagsInTagTracker(type);
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
ret = traverseAllIn(fd, NULL, listUniqueTagsInDirectory, NULL,
|
2006-07-20 18:02:40 +02:00
|
|
|
(void *)item);
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (type >= 0 && type <= TAG_NUM_OF_ITEM_TYPES) {
|
2006-07-30 05:43:38 +02:00
|
|
|
printVisitedInTagTracker(fd, type);
|
2004-11-11 14:15:41 +01:00
|
|
|
}
|
|
|
|
|
2004-11-10 22:58:27 +01:00
|
|
|
freeListCommandItem(item);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2004-11-11 03:36:25 +01:00
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
static int sumSavedFilenameMemoryInDirectory(int fd, Directory * dir,
|
2006-07-20 18:02:40 +02:00
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
int *sum = data;
|
2004-11-11 03:36:25 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!dir->path)
|
|
|
|
return 0;
|
2004-11-11 03:36:25 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
*sum += (strlen(getDirectoryPath(dir)) + 1 - sizeof(Directory *)) *
|
|
|
|
dir->songs->numberOfNodes;
|
2004-11-11 03:59:16 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
static int sumSavedFilenameMemoryInSong(int fd, Song * song, void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
int *sum = data;
|
|
|
|
|
|
|
|
*sum += strlen(song->url) + 1;
|
2004-11-11 03:59:16 +01:00
|
|
|
|
2004-11-11 03:36:25 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void printSavedMemoryFromFilenames(void)
|
|
|
|
{
|
2006-07-13 21:40:14 +02:00
|
|
|
int sum = 0;
|
2004-11-11 06:25:05 +01:00
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
traverseAllIn(STDERR_FILENO, NULL, sumSavedFilenameMemoryInSong,
|
2006-07-20 18:02:40 +02:00
|
|
|
sumSavedFilenameMemoryInDirectory, (void *)&sum);
|
2006-07-13 21:40:14 +02:00
|
|
|
|
|
|
|
DEBUG("saved memory from filenames: %i\n", sum);
|
|
|
|
}
|