Commit 169db88c authored by Max Kellermann's avatar Max Kellermann

database: add struct db_visitor

Use this struct for db_walk().
parent ca419c84
...@@ -271,9 +271,11 @@ src_mpd_SOURCES = \ ...@@ -271,9 +271,11 @@ src_mpd_SOURCES = \
src/directory_save.c \ src/directory_save.c \
src/directory_print.c \ src/directory_print.c \
src/database.c \ src/database.c \
src/db_error.h \
src/db_save.c src/db_save.h \ src/db_save.c src/db_save.h \
src/db_print.c src/db_print.h \ src/db_print.c src/db_print.h \
src/db_plugin.h \ src/db_plugin.h \
src/db_visitor.h \
src/db/simple_db_plugin.c src/db/simple_db_plugin.h \ src/db/simple_db_plugin.c src/db/simple_db_plugin.h \
src/dirvec.c \ src/dirvec.c \
src/exclude.c \ src/exclude.c \
......
...@@ -43,6 +43,7 @@ ...@@ -43,6 +43,7 @@
#include "output_print.h" #include "output_print.h"
#include "locate.h" #include "locate.h"
#include "dbUtils.h" #include "dbUtils.h"
#include "db_error.h"
#include "db_print.h" #include "db_print.h"
#include "tag.h" #include "tag.h"
#include "client.h" #include "client.h"
...@@ -392,6 +393,13 @@ print_error(struct client *client, GError *error) ...@@ -392,6 +393,13 @@ print_error(struct client *client, GError *error)
enum playlist_result result = error->code; enum playlist_result result = error->code;
g_error_free(error); g_error_free(error);
return print_playlist_result(client, result); return print_playlist_result(client, result);
} else if (error->domain == db_quark()) {
switch ((enum db_error)error->code) {
case DB_NOT_FOUND:
g_error_free(error);
command_error(client, ACK_ERROR_NO_EXIST, "Not found");
return COMMAND_RETURN_ERROR;
}
} else if (error->domain == g_file_error_quark()) { } else if (error->domain == g_file_error_quark()) {
command_error(client, ACK_ERROR_SYSTEM, "%s", command_error(client, ACK_ERROR_SYSTEM, "%s",
g_strerror(error->code)); g_strerror(error->code));
...@@ -657,14 +665,10 @@ handle_add(struct client *client, G_GNUC_UNUSED int argc, char *argv[]) ...@@ -657,14 +665,10 @@ handle_add(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
return print_playlist_result(client, result); return print_playlist_result(client, result);
} }
result = addAllIn(client->player_control, uri); GError *error = NULL;
if (result == (enum playlist_result)-1) { return addAllIn(client->player_control, uri, &error)
command_error(client, ACK_ERROR_NO_EXIST, ? COMMAND_RETURN_OK
"directory or file not found"); : print_error(client, error);
return COMMAND_RETURN_ERROR;
}
return print_playlist_result(client, result);
} }
static enum command_return static enum command_return
...@@ -941,7 +945,6 @@ handle_playlistid(struct client *client, int argc, char *argv[]) ...@@ -941,7 +945,6 @@ handle_playlistid(struct client *client, int argc, char *argv[])
static enum command_return static enum command_return
handle_find(struct client *client, int argc, char *argv[]) handle_find(struct client *client, int argc, char *argv[])
{ {
int ret;
struct locate_item_list *list = struct locate_item_list *list =
locate_item_list_parse(argv + 1, argc - 1); locate_item_list_parse(argv + 1, argc - 1);
...@@ -953,10 +956,10 @@ handle_find(struct client *client, int argc, char *argv[]) ...@@ -953,10 +956,10 @@ handle_find(struct client *client, int argc, char *argv[])
return COMMAND_RETURN_ERROR; return COMMAND_RETURN_ERROR;
} }
ret = findSongsIn(client, NULL, list); GError *error = NULL;
if (ret == -1) enum command_return ret = findSongsIn(client, NULL, list, &error)
command_error(client, ACK_ERROR_NO_EXIST, ? COMMAND_RETURN_OK
"directory or file not found"); : print_error(client, error);
locate_item_list_free(list); locate_item_list_free(list);
...@@ -966,7 +969,6 @@ handle_find(struct client *client, int argc, char *argv[]) ...@@ -966,7 +969,6 @@ handle_find(struct client *client, int argc, char *argv[])
static enum command_return static enum command_return
handle_findadd(struct client *client, int argc, char *argv[]) handle_findadd(struct client *client, int argc, char *argv[])
{ {
int ret;
struct locate_item_list *list = struct locate_item_list *list =
locate_item_list_parse(argv + 1, argc - 1); locate_item_list_parse(argv + 1, argc - 1);
if (list == NULL || list->length == 0) { if (list == NULL || list->length == 0) {
...@@ -977,10 +979,11 @@ handle_findadd(struct client *client, int argc, char *argv[]) ...@@ -977,10 +979,11 @@ handle_findadd(struct client *client, int argc, char *argv[])
return COMMAND_RETURN_ERROR; return COMMAND_RETURN_ERROR;
} }
ret = findAddIn(client->player_control, NULL, list); GError *error = NULL;
if (ret == -1) enum command_return ret =
command_error(client, ACK_ERROR_NO_EXIST, findAddIn(client->player_control, NULL, list, &error)
"directory or file not found"); ? COMMAND_RETURN_OK
: print_error(client, error);
locate_item_list_free(list); locate_item_list_free(list);
...@@ -990,7 +993,6 @@ handle_findadd(struct client *client, int argc, char *argv[]) ...@@ -990,7 +993,6 @@ handle_findadd(struct client *client, int argc, char *argv[])
static enum command_return static enum command_return
handle_search(struct client *client, int argc, char *argv[]) handle_search(struct client *client, int argc, char *argv[])
{ {
int ret;
struct locate_item_list *list = struct locate_item_list *list =
locate_item_list_parse(argv + 1, argc - 1); locate_item_list_parse(argv + 1, argc - 1);
...@@ -1002,10 +1004,10 @@ handle_search(struct client *client, int argc, char *argv[]) ...@@ -1002,10 +1004,10 @@ handle_search(struct client *client, int argc, char *argv[])
return COMMAND_RETURN_ERROR; return COMMAND_RETURN_ERROR;
} }
ret = searchForSongsIn(client, NULL, list); GError *error = NULL;
if (ret == -1) enum command_return ret = searchForSongsIn(client, NULL, list, &error)
command_error(client, ACK_ERROR_NO_EXIST, ? COMMAND_RETURN_OK
"directory or file not found"); : print_error(client, error);
locate_item_list_free(list); locate_item_list_free(list);
...@@ -1015,7 +1017,6 @@ handle_search(struct client *client, int argc, char *argv[]) ...@@ -1015,7 +1017,6 @@ handle_search(struct client *client, int argc, char *argv[])
static enum command_return static enum command_return
handle_count(struct client *client, int argc, char *argv[]) handle_count(struct client *client, int argc, char *argv[])
{ {
int ret;
struct locate_item_list *list = struct locate_item_list *list =
locate_item_list_parse(argv + 1, argc - 1); locate_item_list_parse(argv + 1, argc - 1);
...@@ -1027,10 +1028,11 @@ handle_count(struct client *client, int argc, char *argv[]) ...@@ -1027,10 +1028,11 @@ handle_count(struct client *client, int argc, char *argv[])
return COMMAND_RETURN_ERROR; return COMMAND_RETURN_ERROR;
} }
ret = searchStatsForSongsIn(client, NULL, list); GError *error = NULL;
if (ret == -1) enum command_return ret =
command_error(client, ACK_ERROR_NO_EXIST, searchStatsForSongsIn(client, NULL, list, &error)
"directory or file not found"); ? COMMAND_RETURN_OK
: print_error(client, error);
locate_item_list_free(list); locate_item_list_free(list);
...@@ -1259,17 +1261,14 @@ static enum command_return ...@@ -1259,17 +1261,14 @@ static enum command_return
handle_listall(struct client *client, G_GNUC_UNUSED int argc, char *argv[]) handle_listall(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
{ {
char *directory = NULL; char *directory = NULL;
int ret;
if (argc == 2) if (argc == 2)
directory = argv[1]; directory = argv[1];
ret = printAllIn(client, directory); GError *error = NULL;
if (ret == -1) return printAllIn(client, directory, &error)
command_error(client, ACK_ERROR_NO_EXIST, ? COMMAND_RETURN_OK
"directory or file not found"); : print_error(client, error);
return ret;
} }
static enum command_return static enum command_return
...@@ -1388,7 +1387,6 @@ handle_list(struct client *client, int argc, char *argv[]) ...@@ -1388,7 +1387,6 @@ handle_list(struct client *client, int argc, char *argv[])
{ {
struct locate_item_list *conditionals; struct locate_item_list *conditionals;
int tagType = locate_parse_type(argv[1]); int tagType = locate_parse_type(argv[1]);
int ret;
if (tagType < 0) { if (tagType < 0) {
command_error(client, ACK_ERROR_ARG, "\"%s\" is not known", argv[1]); command_error(client, ACK_ERROR_ARG, "\"%s\" is not known", argv[1]);
...@@ -1425,14 +1423,14 @@ handle_list(struct client *client, int argc, char *argv[]) ...@@ -1425,14 +1423,14 @@ handle_list(struct client *client, int argc, char *argv[])
} }
} }
ret = listAllUniqueTags(client, tagType, conditionals); GError *error = NULL;
enum command_return ret =
listAllUniqueTags(client, tagType, conditionals, &error)
? COMMAND_RETURN_OK
: print_error(client, error);
locate_item_list_free(conditionals); locate_item_list_free(conditionals);
if (ret == -1)
command_error(client, ACK_ERROR_NO_EXIST,
"directory or file not found");
return ret; return ret;
} }
...@@ -1534,17 +1532,14 @@ static enum command_return ...@@ -1534,17 +1532,14 @@ static enum command_return
handle_listallinfo(struct client *client, G_GNUC_UNUSED int argc, char *argv[]) handle_listallinfo(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
{ {
char *directory = NULL; char *directory = NULL;
int ret;
if (argc == 2) if (argc == 2)
directory = argv[1]; directory = argv[1];
ret = printInfoForAllIn(client, directory); GError *error = NULL;
if (ret == -1) return printInfoForAllIn(client, directory, &error)
command_error(client, ACK_ERROR_NO_EXIST, ? COMMAND_RETURN_OK
"directory or file not found"); : print_error(client, error);
return ret;
} }
static enum command_return static enum command_return
......
...@@ -19,7 +19,9 @@ ...@@ -19,7 +19,9 @@
#include "config.h" #include "config.h"
#include "database.h" #include "database.h"
#include "db_error.h"
#include "db_save.h" #include "db_save.h"
#include "db_visitor.h"
#include "db_plugin.h" #include "db_plugin.h"
#include "db/simple_db_plugin.h" #include "db/simple_db_plugin.h"
#include "directory.h" #include "directory.h"
...@@ -41,15 +43,6 @@ ...@@ -41,15 +43,6 @@
static struct db *db; static struct db *db;
static bool db_is_open; static bool db_is_open;
/**
* The quark used for GError.domain.
*/
static inline GQuark
db_quark(void)
{
return g_quark_from_static_string("database");
}
bool bool
db_init(const struct config_param *path, GError **error_r) db_init(const struct config_param *path, GError **error_r)
{ {
...@@ -114,25 +107,28 @@ db_get_song(const char *file) ...@@ -114,25 +107,28 @@ db_get_song(const char *file)
return directory_lookup_song(music_root, file); return directory_lookup_song(music_root, file);
} }
int bool
db_walk(const char *name, db_walk(const char *uri,
int (*forEachSong)(struct song *, void *), const struct db_visitor *visitor, void *ctx,
int (*forEachDir)(struct directory *, void *), void *data) GError **error_r)
{ {
struct directory *directory; struct directory *directory;
if (db == NULL) if (db == NULL)
return -1; return -1;
if ((directory = db_get_directory(name)) == NULL) { if ((directory = db_get_directory(uri)) == NULL) {
struct song *song; struct song *song;
if ((song = db_get_song(name)) && forEachSong) { if (visitor->song != NULL &&
return forEachSong(song, data); (song = db_get_song(uri)) != NULL)
} return visitor->song(song, ctx, error_r);
return -1;
g_set_error(error_r, db_quark(), DB_NOT_FOUND,
"No such directory: %s", uri);
return false;
} }
return directory_walk(directory, forEachSong, forEachDir, data); return directory_walk(directory, visitor, ctx, error_r);
} }
bool bool
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
struct config_param; struct config_param;
struct directory; struct directory;
struct db_visitor;
/** /**
* Initialize the database library. * Initialize the database library.
...@@ -52,9 +53,10 @@ db_get_directory(const char *name); ...@@ -52,9 +53,10 @@ db_get_directory(const char *name);
struct song * struct song *
db_get_song(const char *file); db_get_song(const char *file);
int db_walk(const char *name, bool
int (*forEachSong)(struct song *, void *), db_walk(const char *uri,
int (*forEachDir)(struct directory *, void *), void *data); const struct db_visitor *visitor, void *ctx,
GError **error_r);
bool bool
db_check(GError **error_r); db_check(GError **error_r);
......
...@@ -21,52 +21,66 @@ ...@@ -21,52 +21,66 @@
#include "dbUtils.h" #include "dbUtils.h"
#include "locate.h" #include "locate.h"
#include "database.h" #include "database.h"
#include "db_visitor.h"
#include "playlist.h" #include "playlist.h"
#include "stored_playlist.h" #include "stored_playlist.h"
#include <glib.h> #include <glib.h>
static int static bool
directoryAddSongToPlaylist(struct song *song, void *data) add_to_queue_song(struct song *song, void *ctx, GError **error_r)
{ {
struct player_control *pc = data; struct player_control *pc = ctx;
enum playlist_result result =
playlist_append_song(&g_playlist, pc, song, NULL);
if (result != PLAYLIST_RESULT_SUCCESS) {
g_set_error(error_r, playlist_quark(), result,
"Playlist error");
return false;
}
return true;
}
static const struct db_visitor add_to_queue_visitor = {
.song = add_to_queue_song,
};
return playlist_append_song(&g_playlist, pc, song, NULL); bool
addAllIn(struct player_control *pc, const char *uri, GError **error_r)
{
return db_walk(uri, &add_to_queue_visitor, pc, error_r);
} }
struct add_data { struct add_data {
const char *path; const char *path;
GError **error_r;
}; };
static int static bool
directoryAddSongToStoredPlaylist(struct song *song, void *_data) add_to_spl_song(struct song *song, void *ctx, GError **error_r)
{ {
struct add_data *data = _data; struct add_data *data = ctx;
if (!spl_append_song(data->path, song, data->error_r)) { if (!spl_append_song(data->path, song, error_r))
return -1; return false;
}
return 0; return true;
} }
int static const struct db_visitor add_to_spl_visitor = {
addAllIn(struct player_control *pc, const char *name) .song = add_to_spl_song,
{ };
return db_walk(name, directoryAddSongToPlaylist, NULL, pc);
}
bool bool
addAllInToStoredPlaylist(const char *name, const char *utf8file, addAllInToStoredPlaylist(const char *uri_utf8, const char *path_utf8,
GError **error_r) GError **error_r)
{ {
struct add_data data = { struct add_data data = {
.path = utf8file, .path = path_utf8,
.error_r = error_r,
}; };
return db_walk(name, directoryAddSongToStoredPlaylist, NULL, &data) == 0; return db_walk(uri_utf8, &add_to_spl_visitor, &data, error_r);
} }
struct find_add_data { struct find_add_data {
...@@ -74,26 +88,37 @@ struct find_add_data { ...@@ -74,26 +88,37 @@ struct find_add_data {
const struct locate_item_list *criteria; const struct locate_item_list *criteria;
}; };
static int static bool
findAddInDirectory(struct song *song, void *_data) find_add_song(struct song *song, void *ctx, GError **error_r)
{ {
struct find_add_data *data = _data; struct find_add_data *data = ctx;
if (locate_song_match(song, data->criteria)) if (!locate_song_match(song, data->criteria))
return playlist_append_song(&g_playlist, return true;
data->pc,
song, NULL); enum playlist_result result =
playlist_append_song(&g_playlist, data->pc,
song, NULL);
if (result != PLAYLIST_RESULT_SUCCESS) {
g_set_error(error_r, playlist_quark(), result,
"Playlist error");
return false;
}
return 0; return true;
} }
int static const struct db_visitor find_add_visitor = {
.song = find_add_song,
};
bool
findAddIn(struct player_control *pc, const char *name, findAddIn(struct player_control *pc, const char *name,
const struct locate_item_list *criteria) const struct locate_item_list *criteria, GError **error_r)
{ {
struct find_add_data data; struct find_add_data data;
data.pc = pc; data.pc = pc;
data.criteria = criteria; data.criteria = criteria;
return db_walk(name, findAddInDirectory, NULL, &data); return db_walk(name, &find_add_visitor, &data, error_r);
} }
...@@ -26,16 +26,16 @@ ...@@ -26,16 +26,16 @@
struct locate_item_list; struct locate_item_list;
struct player_control; struct player_control;
int bool
addAllIn(struct player_control *pc, const char *name); addAllIn(struct player_control *pc, const char *uri, GError **error_r);
bool bool
addAllInToStoredPlaylist(const char *uri_utf8, const char *path_utf8, addAllInToStoredPlaylist(const char *uri_utf8, const char *path_utf8,
GError **error_r); GError **error_r);
int bool
findAddIn(struct player_control *pc, const char *name, findAddIn(struct player_control *pc, const char *name,
const struct locate_item_list *criteria); const struct locate_item_list *criteria, GError **error_r);
unsigned long sumSongTimesIn(const char *name); unsigned long sumSongTimesIn(const char *name);
......
/*
* Copyright (C) 2003-2011 The Music Player Daemon Project
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_DB_ERROR_H
#define MPD_DB_ERROR_H
#include <glib.h>
enum db_error {
DB_NOT_FOUND,
};
/**
* Quark for GError.domain; the code is an enum #db_error.
*/
G_GNUC_CONST
static inline GQuark
db_quark(void)
{
return g_quark_from_static_string("db");
}
#endif
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "config.h" #include "config.h"
#include "db_print.h" #include "db_print.h"
#include "db_visitor.h"
#include "locate.h" #include "locate.h"
#include "directory.h" #include "directory.h"
#include "database.h" #include "database.h"
...@@ -41,46 +42,72 @@ typedef struct _SearchStats { ...@@ -41,46 +42,72 @@ typedef struct _SearchStats {
unsigned long playTime; unsigned long playTime;
} SearchStats; } SearchStats;
static int static bool
printDirectoryInDirectory(struct directory *directory, void *data) print_visitor_directory(const struct directory *directory, void *data,
G_GNUC_UNUSED GError **error_r)
{ {
struct client *client = data; struct client *client = data;
if (!directory_is_root(directory)) if (!directory_is_root(directory))
client_printf(client, "directory: %s\n", directory_get_path(directory)); client_printf(client, "directory: %s\n", directory_get_path(directory));
return 0; return true;
} }
static int static bool
printSongInDirectory(struct song *song, G_GNUC_UNUSED void *data) print_visitor_song(struct song *song, void *data,
G_GNUC_UNUSED GError **error_r)
{ {
struct client *client = data; struct client *client = data;
song_print_uri(client, song); song_print_uri(client, song);
return 0; return true;
} }
static bool
print_visitor_song_info(struct song *song, void *data,
G_GNUC_UNUSED GError **error_r)
{
struct client *client = data;
song_print_info(client, song);
return true;
}
static const struct db_visitor print_visitor = {
.directory = print_visitor_directory,
.song = print_visitor_song,
};
static const struct db_visitor print_info_visitor = {
.directory = print_visitor_directory,
.song = print_visitor_song_info,
};
struct search_data { struct search_data {
struct client *client; struct client *client;
const struct locate_item_list *criteria; const struct locate_item_list *criteria;
}; };
static int static bool
searchInDirectory(struct song *song, void *_data) search_visitor_song(struct song *song, void *_data,
G_GNUC_UNUSED GError **error_r)
{ {
struct search_data *data = _data; struct search_data *data = _data;
if (locate_song_search(song, data->criteria)) if (locate_song_search(song, data->criteria))
song_print_info(data->client, song); song_print_info(data->client, song);
return 0; return true;
} }
int static const struct db_visitor search_visitor = {
.song = search_visitor_song,
};
bool
searchForSongsIn(struct client *client, const char *name, searchForSongsIn(struct client *client, const char *name,
const struct locate_item_list *criteria) const struct locate_item_list *criteria,
GError **error_r)
{ {
int ret;
struct locate_item_list *new_list struct locate_item_list *new_list
= locate_item_list_casefold(criteria); = locate_item_list_casefold(criteria);
struct search_data data; struct search_data data;
...@@ -88,34 +115,40 @@ searchForSongsIn(struct client *client, const char *name, ...@@ -88,34 +115,40 @@ searchForSongsIn(struct client *client, const char *name,
data.client = client; data.client = client;
data.criteria = new_list; data.criteria = new_list;
ret = db_walk(name, searchInDirectory, NULL, &data); bool success = db_walk(name, &search_visitor, &data, error_r);
locate_item_list_free(new_list); locate_item_list_free(new_list);
return ret; return success;
} }
static int static bool
findInDirectory(struct song *song, void *_data) find_visitor_song(struct song *song, void *_data,
G_GNUC_UNUSED GError **error_r)
{ {
struct search_data *data = _data; struct search_data *data = _data;
if (locate_song_match(song, data->criteria)) if (locate_song_match(song, data->criteria))
song_print_info(data->client, song); song_print_info(data->client, song);
return 0; return true;
} }
int static const struct db_visitor find_visitor = {
.song = find_visitor_song,
};
bool
findSongsIn(struct client *client, const char *name, findSongsIn(struct client *client, const char *name,
const struct locate_item_list *criteria) const struct locate_item_list *criteria,
GError **error_r)
{ {
struct search_data data; struct search_data data;
data.client = client; data.client = client;
data.criteria = criteria; data.criteria = criteria;
return db_walk(name, findInDirectory, NULL, &data); return db_walk(name, &find_visitor, &data, error_r);
} }
static void printSearchStats(struct client *client, SearchStats *stats) static void printSearchStats(struct client *client, SearchStats *stats)
...@@ -124,8 +157,9 @@ static void printSearchStats(struct client *client, SearchStats *stats) ...@@ -124,8 +157,9 @@ static void printSearchStats(struct client *client, SearchStats *stats)
client_printf(client, "playtime: %li\n", stats->playTime); client_printf(client, "playtime: %li\n", stats->playTime);
} }
static int static bool
searchStatsInDirectory(struct song *song, void *data) stats_visitor_song(struct song *song, void *data,
G_GNUC_UNUSED GError **error_r)
{ {
SearchStats *stats = data; SearchStats *stats = data;
...@@ -134,45 +168,42 @@ searchStatsInDirectory(struct song *song, void *data) ...@@ -134,45 +168,42 @@ searchStatsInDirectory(struct song *song, void *data)
stats->playTime += song_get_duration(song); stats->playTime += song_get_duration(song);
} }
return 0; return true;
} }
int static const struct db_visitor stats_visitor = {
.song = stats_visitor_song,
};
bool
searchStatsForSongsIn(struct client *client, const char *name, searchStatsForSongsIn(struct client *client, const char *name,
const struct locate_item_list *criteria) const struct locate_item_list *criteria,
GError **error_r)
{ {
SearchStats stats; SearchStats stats;
int ret;
stats.criteria = criteria; stats.criteria = criteria;
stats.numberOfSongs = 0; stats.numberOfSongs = 0;
stats.playTime = 0; stats.playTime = 0;
ret = db_walk(name, searchStatsInDirectory, NULL, &stats); if (!db_walk(name, &stats_visitor, &stats, error_r))
if (ret == 0) return false;
printSearchStats(client, &stats);
return ret; printSearchStats(client, &stats);
return true;
} }
int printAllIn(struct client *client, const char *name) bool
printAllIn(struct client *client, const char *uri_utf8, GError **error_r)
{ {
return db_walk(name, printSongInDirectory, return db_walk(uri_utf8, &print_visitor, client, error_r);
printDirectoryInDirectory, client);
} }
static int bool
directoryPrintSongInfo(struct song *song, void *data) printInfoForAllIn(struct client *client, const char *uri_utf8,
GError **error_r)
{ {
struct client *client = data; return db_walk(uri_utf8, &print_info_visitor, client, error_r);
song_print_info(client, song);
return 0;
}
int printInfoForAllIn(struct client *client, const char *name)
{
return db_walk(name, directoryPrintSongInfo,
printDirectoryInDirectory, client);
} }
static ListCommandItem * static ListCommandItem *
...@@ -223,8 +254,9 @@ struct list_tags_data { ...@@ -223,8 +254,9 @@ struct list_tags_data {
struct strset *set; struct strset *set;
}; };
static int static bool
listUniqueTagsInDirectory(struct song *song, void *_data) unique_tags_visitor_song(struct song *song, void *_data,
G_GNUC_UNUSED GError **error_r)
{ {
struct list_tags_data *data = _data; struct list_tags_data *data = _data;
ListCommandItem *item = data->item; ListCommandItem *item = data->item;
...@@ -232,13 +264,18 @@ listUniqueTagsInDirectory(struct song *song, void *_data) ...@@ -232,13 +264,18 @@ listUniqueTagsInDirectory(struct song *song, void *_data)
if (locate_song_match(song, item->criteria)) if (locate_song_match(song, item->criteria))
visitTag(data->client, data->set, song, item->tagType); visitTag(data->client, data->set, song, item->tagType);
return 0; return true;
} }
int listAllUniqueTags(struct client *client, int type, static const struct db_visitor unique_tags_visitor = {
const struct locate_item_list *criteria) .song = unique_tags_visitor_song,
};
bool
listAllUniqueTags(struct client *client, int type,
const struct locate_item_list *criteria,
GError **error_r)
{ {
int ret;
ListCommandItem *item = newListCommandItem(type, criteria); ListCommandItem *item = newListCommandItem(type, criteria);
struct list_tags_data data = { struct list_tags_data data = {
.client = client, .client = client,
...@@ -249,7 +286,10 @@ int listAllUniqueTags(struct client *client, int type, ...@@ -249,7 +286,10 @@ int listAllUniqueTags(struct client *client, int type,
data.set = strset_new(); data.set = strset_new();
} }
ret = db_walk(NULL, listUniqueTagsInDirectory, NULL, &data); if (!db_walk(NULL, &unique_tags_visitor, &data, error_r)) {
freeListCommandItem(item);
return false;
}
if (type >= 0 && type <= TAG_NUM_OF_ITEM_TYPES) { if (type >= 0 && type <= TAG_NUM_OF_ITEM_TYPES) {
const char *value; const char *value;
...@@ -266,5 +306,5 @@ int listAllUniqueTags(struct client *client, int type, ...@@ -266,5 +306,5 @@ int listAllUniqueTags(struct client *client, int type,
freeListCommandItem(item); freeListCommandItem(item);
return ret; return true;
} }
...@@ -20,27 +20,38 @@ ...@@ -20,27 +20,38 @@
#ifndef MPD_DB_PRINT_H #ifndef MPD_DB_PRINT_H
#define MPD_DB_PRINT_H #define MPD_DB_PRINT_H
#include <glib.h>
#include <stdbool.h>
struct client; struct client;
struct locate_item_list; struct locate_item_list;
struct db_visitor;
int printAllIn(struct client *client, const char *name); bool
printAllIn(struct client *client, const char *uri_utf8, GError **error_r);
int printInfoForAllIn(struct client *client, const char *name); bool
printInfoForAllIn(struct client *client, const char *uri_utf8,
GError **error_r);
int bool
searchForSongsIn(struct client *client, const char *name, searchForSongsIn(struct client *client, const char *name,
const struct locate_item_list *criteria); const struct locate_item_list *criteria,
GError **error_r);
int bool
findSongsIn(struct client *client, const char *name, findSongsIn(struct client *client, const char *name,
const struct locate_item_list *criteria); const struct locate_item_list *criteria,
GError **error_r);
int bool
searchStatsForSongsIn(struct client *client, const char *name, searchStatsForSongsIn(struct client *client, const char *name,
const struct locate_item_list *criteria); const struct locate_item_list *criteria,
GError **error_r);
int bool
listAllUniqueTags(struct client *client, int type, listAllUniqueTags(struct client *client, int type,
const struct locate_item_list *criteria); const struct locate_item_list *criteria,
GError **error_r);
#endif #endif
/*
* Copyright (C) 2003-2011 The Music Player Daemon Project
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_DB_VISITOR_H
#define MPD_DB_VISITOR_H
struct directory;
struct song;
struct db_visitor {
/**
* Visit a directory. Optional method.
*
* @return true to continue the operation, false on error (set error_r)
*/
bool (*directory)(const struct directory *directory, void *ctx,
GError **error_r);
/**
* Visit a song. Optional method.
*
* @return true to continue the operation, false on error (set error_r)
*/
bool (*song)(struct song *song, void *ctx, GError **error_r);
};
#endif
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "directory.h" #include "directory.h"
#include "song.h" #include "song.h"
#include "path.h" #include "path.h"
#include "db_visitor.h"
#include <glib.h> #include <glib.h>
...@@ -167,28 +168,33 @@ directory_sort(struct directory *directory) ...@@ -167,28 +168,33 @@ directory_sort(struct directory *directory)
directory_sort(dv->base[i]); directory_sort(dv->base[i]);
} }
int bool
directory_walk(struct directory *directory, directory_walk(struct directory *directory,
int (*forEachSong)(struct song *, void *), const struct db_visitor *visitor, void *ctx,
int (*forEachDir)(struct directory *, void *), GError **error_r)
void *data)
{ {
struct dirvec *dv = &directory->children; assert(directory != NULL);
int err = 0; assert(visitor != NULL);
size_t j; assert(error_r == NULL || *error_r == NULL);
if (forEachDir && (err = forEachDir(directory, data)) < 0) if (visitor->directory != NULL &&
return err; !visitor->directory(directory, ctx, error_r))
return false;
if (forEachSong) {
err = songvec_for_each(&directory->songs, forEachSong, data); if (visitor->song != NULL) {
if (err < 0) struct songvec *sv = &directory->songs;
return err; for (size_t i = 0; i < sv->nr; ++i)
if (!visitor->song(sv->base[i], ctx, error_r))
return false;
} }
for (j = 0; err >= 0 && j < dv->nr; ++j) if (visitor->directory != NULL) {
err = directory_walk(dv->base[j], forEachSong, const struct dirvec *dv = &directory->children;
forEachDir, data); for (size_t i = 0; i < dv->nr; ++i)
if (!directory_walk(dv->base[i], visitor, ctx,
error_r))
return false;
}
return err; return true;
} }
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include "songvec.h" #include "songvec.h"
#include "playlist_vector.h" #include "playlist_vector.h"
#include <glib.h>
#include <stdbool.h> #include <stdbool.h>
#include <sys/types.h> #include <sys/types.h>
...@@ -33,6 +34,8 @@ ...@@ -33,6 +34,8 @@
#define DEVICE_INARCHIVE (dev_t)(-1) #define DEVICE_INARCHIVE (dev_t)(-1)
#define DEVICE_CONTAINER (dev_t)(-2) #define DEVICE_CONTAINER (dev_t)(-2)
struct db_visitor;
struct directory { struct directory {
struct dirvec children; struct dirvec children;
struct songvec songs; struct songvec songs;
...@@ -127,10 +130,9 @@ directory_lookup_song(struct directory *directory, const char *uri); ...@@ -127,10 +130,9 @@ directory_lookup_song(struct directory *directory, const char *uri);
void void
directory_sort(struct directory *directory); directory_sort(struct directory *directory);
int bool
directory_walk(struct directory *directory, directory_walk(struct directory *directory,
int (*forEachSong)(struct song *, void *), const struct db_visitor *visitor, void *ctx,
int (*forEachDir)(struct directory *, void *), GError **error_r);
void *data);
#endif #endif
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "config.h" #include "config.h"
#include "stats.h" #include "stats.h"
#include "database.h" #include "database.h"
#include "db_visitor.h"
#include "tag.h" #include "tag.h"
#include "song.h" #include "song.h"
#include "client.h" #include "client.h"
...@@ -68,8 +69,9 @@ visit_tag(struct visit_data *data, const struct tag *tag) ...@@ -68,8 +69,9 @@ visit_tag(struct visit_data *data, const struct tag *tag)
} }
} }
static int static bool
stats_collect_song(struct song *song, void *_data) collect_stats_song(struct song *song, void *_data,
G_GNUC_UNUSED GError **error_r)
{ {
struct visit_data *data = _data; struct visit_data *data = _data;
...@@ -78,9 +80,13 @@ stats_collect_song(struct song *song, void *_data) ...@@ -78,9 +80,13 @@ stats_collect_song(struct song *song, void *_data)
if (song->tag != NULL) if (song->tag != NULL)
visit_tag(data, song->tag); visit_tag(data, song->tag);
return 0; return true;
} }
static const struct db_visitor collect_stats_visitor = {
.song = collect_stats_song,
};
void stats_update(void) void stats_update(void)
{ {
struct visit_data data; struct visit_data data;
...@@ -92,7 +98,7 @@ void stats_update(void) ...@@ -92,7 +98,7 @@ void stats_update(void)
data.artists = strset_new(); data.artists = strset_new();
data.albums = strset_new(); data.albums = strset_new();
db_walk(NULL, stats_collect_song, NULL, &data); db_walk(NULL, &collect_stats_visitor, &data, NULL);
stats.artist_count = strset_size(data.artists); stats.artist_count = strset_size(data.artists);
stats.album_count = strset_size(data.albums); stats.album_count = strset_size(data.albums);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment