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"
|
2009-01-24 14:52:05 +01:00
|
|
|
#include "locate.h"
|
2004-11-10 22:58:27 +01:00
|
|
|
#include "directory.h"
|
2008-10-08 11:07:35 +02:00
|
|
|
#include "database.h"
|
2008-09-07 13:53:55 +02:00
|
|
|
#include "client.h"
|
2004-11-10 22:58:27 +01:00
|
|
|
#include "playlist.h"
|
2006-10-28 01:07:34 +02:00
|
|
|
#include "song.h"
|
2008-09-07 13:35:01 +02:00
|
|
|
#include "song_print.h"
|
2004-11-10 22:58:27 +01:00
|
|
|
#include "tag.h"
|
2008-09-08 11:47:57 +02:00
|
|
|
#include "strset.h"
|
2004-11-11 03:36:25 +01:00
|
|
|
#include "log.h"
|
2008-10-22 17:21:57 +02:00
|
|
|
#include "stored_playlist.h"
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2008-10-15 19:36:37 +02:00
|
|
|
#include <glib.h>
|
|
|
|
|
2009-01-03 13:37:36 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2004-11-12 02:44:27 +01:00
|
|
|
typedef struct _ListCommandItem {
|
2008-09-29 13:29:33 +02:00
|
|
|
int8_t tagType;
|
2009-01-24 15:56:34 +01:00
|
|
|
const struct locate_item_list *criteria;
|
2004-11-10 22:58:27 +01:00
|
|
|
} ListCommandItem;
|
|
|
|
|
2007-04-26 01:46:11 +02:00
|
|
|
typedef struct _SearchStats {
|
2009-01-24 15:56:34 +01:00
|
|
|
const struct locate_item_list *criteria;
|
2007-04-26 01:46:11 +02:00
|
|
|
int numberOfSongs;
|
2007-04-26 02:11:30 +02:00
|
|
|
unsigned long playTime;
|
2007-04-26 01:46:11 +02:00
|
|
|
} SearchStats;
|
|
|
|
|
2008-10-08 10:49:05 +02:00
|
|
|
static int
|
|
|
|
printDirectoryInDirectory(struct directory *directory, void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-07 13:53:55 +02:00
|
|
|
struct client *client = data;
|
2009-01-04 19:08:52 +01:00
|
|
|
|
|
|
|
if (!directory_is_root(directory))
|
2008-10-08 11:07:58 +02:00
|
|
|
client_printf(client, "directory: %s\n", directory_get_path(directory));
|
2009-01-04 19:08:52 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
return 0;
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
static int
|
2008-11-24 14:42:47 +01:00
|
|
|
printSongInDirectory(struct song *song, G_GNUC_UNUSED void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-07 13:53:55 +02:00
|
|
|
struct client *client = data;
|
2008-10-07 22:06:59 +02:00
|
|
|
song_print_url(client, song);
|
2006-07-20 18:02:40 +02:00
|
|
|
return 0;
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2008-09-07 13:48:24 +02:00
|
|
|
struct search_data {
|
2008-09-07 13:53:55 +02:00
|
|
|
struct client *client;
|
2009-01-24 15:56:34 +01:00
|
|
|
const struct locate_item_list *criteria;
|
2008-09-07 13:48:24 +02:00
|
|
|
};
|
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
static int
|
|
|
|
searchInDirectory(struct song *song, void *_data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-07 13:48:24 +02:00
|
|
|
struct search_data *data = _data;
|
2004-11-12 02:44:27 +01:00
|
|
|
|
2009-01-24 15:56:34 +01:00
|
|
|
if (locate_song_search(song, data->criteria))
|
2008-10-07 22:07:44 +02:00
|
|
|
return song_print_info(data->client, song);
|
2004-11-12 02:44:27 +01:00
|
|
|
|
2004-11-10 22:58:27 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-01-24 14:52:05 +01:00
|
|
|
int
|
|
|
|
searchForSongsIn(struct client *client, const char *name,
|
2009-01-24 15:56:34 +01:00
|
|
|
const struct locate_item_list *criteria)
|
2004-11-12 02:44:27 +01:00
|
|
|
{
|
2008-03-26 11:37:36 +01:00
|
|
|
int ret;
|
2009-01-24 15:56:34 +01:00
|
|
|
struct locate_item_list *new_list
|
|
|
|
= locate_item_list_casefold(criteria);
|
2008-09-07 13:48:24 +02:00
|
|
|
struct search_data data;
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2008-09-07 13:53:55 +02:00
|
|
|
data.client = client;
|
2009-01-24 15:56:34 +01:00
|
|
|
data.criteria = new_list;
|
2004-11-12 02:44:27 +01:00
|
|
|
|
2008-10-08 11:07:55 +02:00
|
|
|
ret = db_walk(name, searchInDirectory, NULL, &data);
|
2004-11-12 02:44:27 +01:00
|
|
|
|
2009-01-24 15:56:30 +01:00
|
|
|
locate_item_list_free(new_list);
|
2004-11-10 22:58:27 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
static int
|
|
|
|
findInDirectory(struct song *song, void *_data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-07 13:48:24 +02:00
|
|
|
struct search_data *data = _data;
|
2004-11-12 02:44:27 +01:00
|
|
|
|
2009-01-24 15:56:34 +01:00
|
|
|
if (locate_song_match(song, data->criteria))
|
2008-10-07 22:07:44 +02:00
|
|
|
return song_print_info(data->client, song);
|
2004-11-12 02:44:27 +01:00
|
|
|
|
2004-11-10 22:58:27 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-01-24 14:52:05 +01:00
|
|
|
int
|
|
|
|
findSongsIn(struct client *client, const char *name,
|
2009-01-24 15:56:34 +01:00
|
|
|
const struct locate_item_list *criteria)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-07 13:48:24 +02:00
|
|
|
struct search_data data;
|
2004-11-12 02:44:27 +01:00
|
|
|
|
2008-09-07 13:53:55 +02:00
|
|
|
data.client = client;
|
2009-01-24 15:56:34 +01:00
|
|
|
data.criteria = criteria;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2008-10-08 11:07:55 +02:00
|
|
|
return db_walk(name, findInDirectory, NULL, &data);
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2008-09-07 13:53:55 +02:00
|
|
|
static void printSearchStats(struct client *client, SearchStats *stats)
|
2007-04-26 01:46:11 +02:00
|
|
|
{
|
2008-09-07 13:53:55 +02:00
|
|
|
client_printf(client, "songs: %i\n", stats->numberOfSongs);
|
|
|
|
client_printf(client, "playtime: %li\n", stats->playTime);
|
2007-04-26 01:46:11 +02:00
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
static int
|
|
|
|
searchStatsInDirectory(struct song *song, void *data)
|
2007-04-26 01:46:11 +02:00
|
|
|
{
|
|
|
|
SearchStats *stats = data;
|
|
|
|
|
2009-01-24 15:56:34 +01:00
|
|
|
if (locate_song_match(song, stats->criteria)) {
|
2007-04-26 01:46:11 +02:00
|
|
|
stats->numberOfSongs++;
|
|
|
|
if (song->tag->time > 0)
|
|
|
|
stats->playTime += song->tag->time;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-01-24 14:52:05 +01:00
|
|
|
int
|
|
|
|
searchStatsForSongsIn(struct client *client, const char *name,
|
2009-01-24 15:56:34 +01:00
|
|
|
const struct locate_item_list *criteria)
|
2007-04-26 01:46:11 +02:00
|
|
|
{
|
|
|
|
SearchStats stats;
|
|
|
|
int ret;
|
|
|
|
|
2009-01-24 15:56:34 +01:00
|
|
|
stats.criteria = criteria;
|
2007-04-26 01:46:11 +02:00
|
|
|
stats.numberOfSongs = 0;
|
|
|
|
stats.playTime = 0;
|
|
|
|
|
2008-10-08 11:07:55 +02:00
|
|
|
ret = db_walk(name, searchStatsInDirectory, NULL, &stats);
|
2007-04-26 01:46:11 +02:00
|
|
|
if (ret == 0)
|
2008-09-07 13:53:55 +02:00
|
|
|
printSearchStats(client, &stats);
|
2007-04-26 01:46:11 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-09-07 13:53:55 +02:00
|
|
|
int printAllIn(struct client *client, const char *name)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-08 11:07:55 +02:00
|
|
|
return db_walk(name, printSongInDirectory,
|
|
|
|
printDirectoryInDirectory, client);
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
static int
|
2008-11-24 14:42:47 +01:00
|
|
|
directoryAddSongToPlaylist(struct song *song, G_GNUC_UNUSED void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-07 13:39:31 +02:00
|
|
|
return addSongToPlaylist(song, NULL);
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2008-09-07 13:48:24 +02:00
|
|
|
struct add_data {
|
|
|
|
const char *path;
|
|
|
|
};
|
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
static int
|
|
|
|
directoryAddSongToStoredPlaylist(struct song *song, void *_data)
|
2006-11-20 16:37:58 +01:00
|
|
|
{
|
2008-09-07 13:48:24 +02:00
|
|
|
struct add_data *data = _data;
|
|
|
|
|
2008-10-22 17:21:59 +02:00
|
|
|
if (spl_append_song(data->path, song) != 0)
|
2008-09-07 13:48:24 +02:00
|
|
|
return -1;
|
|
|
|
return 0;
|
2006-11-20 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
2008-09-07 13:48:37 +02:00
|
|
|
int addAllIn(const char *name)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-08 11:07:55 +02:00
|
|
|
return db_walk(name, directoryAddSongToPlaylist, NULL, NULL);
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2008-09-07 13:48:37 +02:00
|
|
|
int addAllInToStoredPlaylist(const char *name, const char *utf8file)
|
2006-11-20 16:37:58 +01:00
|
|
|
{
|
2008-09-07 13:48:24 +02:00
|
|
|
struct add_data data = {
|
|
|
|
.path = utf8file,
|
|
|
|
};
|
|
|
|
|
2008-10-08 11:07:55 +02:00
|
|
|
return db_walk(name, directoryAddSongToStoredPlaylist, NULL, &data);
|
2006-11-20 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
static int
|
|
|
|
directoryPrintSongInfo(struct song *song, void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-07 13:53:55 +02:00
|
|
|
struct client *client = data;
|
2008-10-07 22:06:59 +02:00
|
|
|
song_print_info(client, song);
|
|
|
|
return 0;
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2008-09-07 13:53:55 +02:00
|
|
|
int printInfoForAllIn(struct client *client, const char *name)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-08 11:07:55 +02:00
|
|
|
return db_walk(name, directoryPrintSongInfo,
|
2008-09-07 13:53:55 +02:00
|
|
|
printDirectoryInDirectory, client);
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2009-01-24 14:52:05 +01:00
|
|
|
static ListCommandItem *
|
2009-01-24 15:56:34 +01:00
|
|
|
newListCommandItem(int tagType, const struct locate_item_list *criteria)
|
2004-11-10 22:58:27 +01:00
|
|
|
{
|
2009-01-03 13:37:36 +01:00
|
|
|
ListCommandItem *item = g_new(ListCommandItem, 1);
|
2004-11-10 22:58:27 +01:00
|
|
|
|
|
|
|
item->tagType = tagType;
|
2009-01-24 15:56:34 +01:00
|
|
|
item->criteria = criteria;
|
2004-11-10 22:58:27 +01:00
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void freeListCommandItem(ListCommandItem * item)
|
|
|
|
{
|
2009-01-25 18:47:21 +01:00
|
|
|
g_free(item);
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
static void
|
|
|
|
visitTag(struct client *client, struct strset *set,
|
|
|
|
struct song *song, enum tag_type tagType)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-11-10 22:58:27 +01:00
|
|
|
int i;
|
2008-08-29 09:38:11 +02:00
|
|
|
struct tag *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) {
|
2008-10-07 22:06:59 +02:00
|
|
|
song_print_url(client, 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++) {
|
2008-08-29 09:38:29 +02:00
|
|
|
if (tag->items[i]->type == tagType) {
|
2008-09-08 11:47:57 +02:00
|
|
|
strset_add(set, tag->items[i]->value);
|
2008-09-29 13:18:49 +02:00
|
|
|
return;
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
}
|
2008-09-29 13:18:49 +02:00
|
|
|
strset_add(set, "");
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2008-09-07 13:48:24 +02:00
|
|
|
struct list_tags_data {
|
2008-09-07 13:53:55 +02:00
|
|
|
struct client *client;
|
2008-09-07 13:48:24 +02:00
|
|
|
ListCommandItem *item;
|
2008-09-08 11:47:57 +02:00
|
|
|
struct strset *set;
|
2008-09-07 13:48:24 +02:00
|
|
|
};
|
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
static int
|
|
|
|
listUniqueTagsInDirectory(struct song *song, void *_data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-07 13:48:24 +02:00
|
|
|
struct list_tags_data *data = _data;
|
|
|
|
ListCommandItem *item = data->item;
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2009-01-24 15:56:34 +01:00
|
|
|
if (locate_song_match(song, item->criteria))
|
2008-09-08 11:47:57 +02:00
|
|
|
visitTag(data->client, data->set, song, item->tagType);
|
2004-11-10 22:58:27 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-01-24 15:56:34 +01:00
|
|
|
int listAllUniqueTags(struct client *client, int type,
|
|
|
|
const struct locate_item_list *criteria)
|
2004-11-10 22:58:27 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2009-01-24 15:56:34 +01:00
|
|
|
ListCommandItem *item = newListCommandItem(type, criteria);
|
2008-09-07 13:48:24 +02:00
|
|
|
struct list_tags_data data = {
|
2008-09-07 13:53:55 +02:00
|
|
|
.client = client,
|
2008-09-07 13:48:24 +02:00
|
|
|
.item = item,
|
|
|
|
};
|
2006-07-20 18:02:40 +02:00
|
|
|
|
|
|
|
if (type >= 0 && type <= TAG_NUM_OF_ITEM_TYPES) {
|
2008-09-08 11:47:57 +02:00
|
|
|
data.set = strset_new();
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2008-10-08 11:07:55 +02:00
|
|
|
ret = db_walk(NULL, listUniqueTagsInDirectory, NULL, &data);
|
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) {
|
2008-09-08 11:47:57 +02:00
|
|
|
const char *value;
|
|
|
|
|
|
|
|
strset_rewind(data.set);
|
|
|
|
|
|
|
|
while ((value = strset_next(data.set)) != NULL)
|
|
|
|
client_printf(client, "%s: %s\n",
|
|
|
|
mpdTagItemKeys[type],
|
|
|
|
value);
|
|
|
|
|
|
|
|
strset_free(data.set);
|
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
|
|
|
|
2008-10-08 10:49:05 +02:00
|
|
|
static int
|
|
|
|
sumSavedFilenameMemoryInDirectory(struct directory *dir, void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
int *sum = data;
|
2004-11-11 03:36:25 +01:00
|
|
|
|
2009-01-04 19:08:52 +01:00
|
|
|
if (directory_is_root(dir))
|
2006-07-20 18:02:40 +02:00
|
|
|
return 0;
|
2004-11-11 03:36:25 +01:00
|
|
|
|
2008-10-08 11:07:58 +02:00
|
|
|
*sum += (strlen(directory_get_path(dir)) + 1
|
2008-10-08 10:49:05 +02:00
|
|
|
- sizeof(struct directory *)) * dir->songs.nr;
|
2004-11-11 03:59:16 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
static int
|
|
|
|
sumSavedFilenameMemoryInSong(struct 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
|
|
|
|
2008-10-08 11:07:55 +02:00
|
|
|
db_walk(NULL, sumSavedFilenameMemoryInSong,
|
|
|
|
sumSavedFilenameMemoryInDirectory, (void *)&sum);
|
2006-07-13 21:40:14 +02:00
|
|
|
|
|
|
|
DEBUG("saved memory from filenames: %i\n", sum);
|
|
|
|
}
|