2
* Copyright (C) 2003-2011 The Music Player Daemon Project
3
* http://www.musicpd.org
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License along
16
* with this program; if not, write to the Free Software Foundation, Inc.,
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22
#include "db_selection.h"
23
#include "db_visitor.h"
25
#include "directory.h"
29
#include "song_print.h"
30
#include "playlist_vector.h"
36
typedef struct _ListCommandItem {
38
const struct locate_item_list *criteria;
41
typedef struct _SearchStats {
42
const struct locate_item_list *criteria;
44
unsigned long playTime;
48
print_visitor_directory(const struct directory *directory, void *data,
49
G_GNUC_UNUSED GError **error_r)
51
struct client *client = data;
53
if (!directory_is_root(directory))
54
client_printf(client, "directory: %s\n", directory_get_path(directory));
60
print_playlist_in_directory(struct client *client,
61
const struct directory *directory,
62
const char *name_utf8)
64
if (directory_is_root(directory))
65
client_printf(client, "playlist: %s\n", name_utf8);
67
client_printf(client, "playlist: %s/%s\n",
68
directory_get_path(directory), name_utf8);
72
print_visitor_song(struct song *song, void *data,
73
G_GNUC_UNUSED GError **error_r)
76
assert(song->parent != NULL);
78
struct client *client = data;
79
song_print_uri(client, song);
81
if (song->tag != NULL && song->tag->has_playlist)
82
/* this song file has an embedded CUE sheet */
83
print_playlist_in_directory(client, song->parent,
90
print_visitor_song_info(struct song *song, void *data,
91
G_GNUC_UNUSED GError **error_r)
94
assert(song->parent != NULL);
96
struct client *client = data;
97
song_print_info(client, song);
99
if (song->tag != NULL && song->tag->has_playlist)
100
/* this song file has an embedded CUE sheet */
101
print_playlist_in_directory(client, song->parent,
108
print_visitor_playlist(const struct playlist_metadata *playlist,
109
const struct directory *directory, void *ctx,
110
G_GNUC_UNUSED GError **error_r)
112
struct client *client = ctx;
113
print_playlist_in_directory(client, directory, playlist->name);
118
print_visitor_playlist_info(const struct playlist_metadata *playlist,
119
const struct directory *directory,
120
void *ctx, G_GNUC_UNUSED GError **error_r)
122
struct client *client = ctx;
123
print_playlist_in_directory(client, directory, playlist->name);
129
time_t t = playlist->mtime;
130
strftime(timestamp, sizeof(timestamp),
132
"%Y-%m-%dT%H:%M:%SZ",
139
client_printf(client, "Last-Modified: %s\n", timestamp);
144
static const struct db_visitor print_visitor = {
145
.directory = print_visitor_directory,
146
.song = print_visitor_song,
147
.playlist = print_visitor_playlist,
150
static const struct db_visitor print_info_visitor = {
151
.directory = print_visitor_directory,
152
.song = print_visitor_song_info,
153
.playlist = print_visitor_playlist_info,
157
db_selection_print(struct client *client, const struct db_selection *selection,
158
bool full, GError **error_r)
160
return db_visit(selection, full ? &print_info_visitor : &print_visitor,
165
struct client *client;
166
const struct locate_item_list *criteria;
170
search_visitor_song(struct song *song, void *_data,
171
G_GNUC_UNUSED GError **error_r)
173
struct search_data *data = _data;
175
if (locate_song_search(song, data->criteria))
176
song_print_info(data->client, song);
181
static const struct db_visitor search_visitor = {
182
.song = search_visitor_song,
186
searchForSongsIn(struct client *client, const char *name,
187
const struct locate_item_list *criteria,
190
struct locate_item_list *new_list
191
= locate_item_list_casefold(criteria);
192
struct search_data data;
194
data.client = client;
195
data.criteria = new_list;
197
bool success = db_walk(name, &search_visitor, &data, error_r);
199
locate_item_list_free(new_list);
205
find_visitor_song(struct song *song, void *_data,
206
G_GNUC_UNUSED GError **error_r)
208
struct search_data *data = _data;
210
if (locate_song_match(song, data->criteria))
211
song_print_info(data->client, song);
216
static const struct db_visitor find_visitor = {
217
.song = find_visitor_song,
221
findSongsIn(struct client *client, const char *name,
222
const struct locate_item_list *criteria,
225
struct search_data data;
227
data.client = client;
228
data.criteria = criteria;
230
return db_walk(name, &find_visitor, &data, error_r);
233
static void printSearchStats(struct client *client, SearchStats *stats)
235
client_printf(client, "songs: %i\n", stats->numberOfSongs);
236
client_printf(client, "playtime: %li\n", stats->playTime);
240
stats_visitor_song(struct song *song, void *data,
241
G_GNUC_UNUSED GError **error_r)
243
SearchStats *stats = data;
245
if (locate_song_match(song, stats->criteria)) {
246
stats->numberOfSongs++;
247
stats->playTime += song_get_duration(song);
253
static const struct db_visitor stats_visitor = {
254
.song = stats_visitor_song,
258
searchStatsForSongsIn(struct client *client, const char *name,
259
const struct locate_item_list *criteria,
264
stats.criteria = criteria;
265
stats.numberOfSongs = 0;
268
if (!db_walk(name, &stats_visitor, &stats, error_r))
271
printSearchStats(client, &stats);
276
printAllIn(struct client *client, const char *uri_utf8, GError **error_r)
278
struct db_selection selection;
279
db_selection_init(&selection, uri_utf8, true);
280
return db_selection_print(client, &selection, false, error_r);
284
printInfoForAllIn(struct client *client, const char *uri_utf8,
287
struct db_selection selection;
288
db_selection_init(&selection, uri_utf8, true);
289
return db_selection_print(client, &selection, true, error_r);
292
static ListCommandItem *
293
newListCommandItem(int tagType, const struct locate_item_list *criteria)
295
ListCommandItem *item = g_new(ListCommandItem, 1);
297
item->tagType = tagType;
298
item->criteria = criteria;
303
static void freeListCommandItem(ListCommandItem * item)
309
visitTag(struct client *client, struct strset *set,
310
struct song *song, enum tag_type tagType)
312
struct tag *tag = song->tag;
315
if (tagType == LOCATE_TAG_FILE_TYPE) {
316
song_print_uri(client, song);
323
for (unsigned i = 0; i < tag->num_items; i++) {
324
if (tag->items[i]->type == tagType) {
325
strset_add(set, tag->items[i]->value);
334
struct list_tags_data {
335
struct client *client;
336
ListCommandItem *item;
341
unique_tags_visitor_song(struct song *song, void *_data,
342
G_GNUC_UNUSED GError **error_r)
344
struct list_tags_data *data = _data;
345
ListCommandItem *item = data->item;
347
if (locate_song_match(song, item->criteria))
348
visitTag(data->client, data->set, song, item->tagType);
353
static const struct db_visitor unique_tags_visitor = {
354
.song = unique_tags_visitor_song,
358
listAllUniqueTags(struct client *client, int type,
359
const struct locate_item_list *criteria,
362
ListCommandItem *item = newListCommandItem(type, criteria);
363
struct list_tags_data data = {
368
if (type >= 0 && type <= TAG_NUM_OF_ITEM_TYPES) {
369
data.set = strset_new();
372
if (!db_walk("", &unique_tags_visitor, &data, error_r)) {
373
freeListCommandItem(item);
377
if (type >= 0 && type <= TAG_NUM_OF_ITEM_TYPES) {
380
strset_rewind(data.set);
382
while ((value = strset_next(data.set)) != NULL)
383
client_printf(client, "%s: %s\n",
384
tag_item_names[type],
387
strset_free(data.set);
390
freeListCommandItem(item);