db_print: move code to function db_selection_print()

Use it in handle_lsinfo(), and eliminate some duplicate code.
This commit is contained in:
Max Kellermann 2011-09-13 21:24:22 +02:00
parent c779e2674a
commit a236a439cc
8 changed files with 27 additions and 134 deletions

View File

@ -78,7 +78,6 @@ mpd_headers = \
src/decoder_internal.h \
src/directory.h \
src/directory_save.h \
src/directory_print.h \
src/database.h \
src/encoder_plugin.h \
src/encoder_list.h \
@ -269,7 +268,6 @@ src_mpd_SOURCES = \
src/decoder_print.c \
src/directory.c \
src/directory_save.c \
src/directory_print.c \
src/database.c \
src/db_error.h \
src/db_save.c src/db_save.h \

View File

@ -30,7 +30,6 @@
#include "uri.h"
#include "decoder_print.h"
#include "directory.h"
#include "directory_print.h"
#include "database.h"
#include "update.h"
#include "volume.h"
@ -45,6 +44,7 @@
#include "dbUtils.h"
#include "db_error.h"
#include "db_print.h"
#include "db_selection.h"
#include "tag.h"
#include "client.h"
#include "client_idle.h"
@ -840,7 +840,6 @@ static enum command_return
handle_lsinfo(struct client *client, int argc, char *argv[])
{
const char *uri;
const struct directory *directory;
if (argc == 2)
uri = argv[1];
@ -848,14 +847,12 @@ handle_lsinfo(struct client *client, int argc, char *argv[])
/* default is root directory */
uri = "";
directory = db_get_directory(uri);
if (directory == NULL) {
command_error(client, ACK_ERROR_NO_EXIST,
"directory not found");
return COMMAND_RETURN_ERROR;
}
struct db_selection selection;
db_selection_init(&selection, uri, false);
directory_print(client, directory);
GError *error = NULL;
if (!db_selection_print(client, &selection, true, &error))
return print_error(client, error);
if (isRootDirectory(uri)) {
GPtrArray *list = spl_list(NULL);

View File

@ -19,6 +19,7 @@
#include "config.h"
#include "db_print.h"
#include "db_selection.h"
#include "db_visitor.h"
#include "locate.h"
#include "directory.h"
@ -119,6 +120,14 @@ static const struct db_visitor print_info_visitor = {
.playlist = print_visitor_playlist_info,
};
bool
db_selection_print(struct client *client, const struct db_selection *selection,
bool full, GError **error_r)
{
return db_visit(selection, full ? &print_info_visitor : &print_visitor,
client, error_r);
}
struct search_data {
struct client *client;
const struct locate_item_list *criteria;
@ -233,14 +242,18 @@ searchStatsForSongsIn(struct client *client, const char *name,
bool
printAllIn(struct client *client, const char *uri_utf8, GError **error_r)
{
return db_walk(uri_utf8, &print_visitor, client, error_r);
struct db_selection selection;
db_selection_init(&selection, uri_utf8, true);
return db_selection_print(client, &selection, false, error_r);
}
bool
printInfoForAllIn(struct client *client, const char *uri_utf8,
GError **error_r)
{
return db_walk(uri_utf8, &print_info_visitor, client, error_r);
struct db_selection selection;
db_selection_init(&selection, uri_utf8, true);
return db_selection_print(client, &selection, true, error_r);
}
static ListCommandItem *

View File

@ -27,8 +27,14 @@
struct client;
struct locate_item_list;
struct db_selection;
struct db_visitor;
gcc_nonnull(1,2)
bool
db_selection_print(struct client *client, const struct db_selection *selection,
bool full, GError **error_r);
gcc_nonnull(1,2)
bool
printAllIn(struct client *client, const char *uri_utf8, GError **error_r);

View File

@ -1,74 +0,0 @@
/*
* 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.
*/
#include "config.h"
#include "directory_print.h"
#include "directory.h"
#include "client.h"
#include "song_print.h"
#include "mapper.h"
#include "decoder_list.h"
#include "path.h"
#include "uri.h"
#include "input_stream.h"
#include <sys/types.h>
#include <dirent.h>
static void
dirvec_print(struct client *client, const struct dirvec *dv)
{
size_t i;
for (i = 0; i < dv->nr; ++i)
client_printf(client, DIRECTORY_DIR "%s\n",
directory_get_path(dv->base[i]));
}
static void
print_playlist_in_directory(struct client *client,
const struct directory *directory,
const char *name_utf8)
{
if (directory_is_root(directory))
client_printf(client, "playlist: %s\n", name_utf8);
else
client_printf(client, "playlist: %s/%s\n",
directory_get_path(directory), name_utf8);
}
/**
* Print a list of playlists in the specified directory.
*/
static void
directory_print_playlists(struct client *client,
const struct directory *directory)
{
for (const struct playlist_metadata *pm = directory->playlists.head;
pm != NULL; pm = pm->next)
print_playlist_in_directory(client, directory, pm->name);
}
void
directory_print(struct client *client, const struct directory *directory)
{
dirvec_print(client, &directory->children);
songvec_print(client, &directory->songs);
directory_print_playlists(client, directory);
}

View File

@ -1,29 +0,0 @@
/*
* 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_DIRECTORY_PRINT_H
#define MPD_DIRECTORY_PRINT_H
struct client;
struct directory;
void
directory_print(struct client *client, const struct directory *directory);
#endif

View File

@ -94,18 +94,3 @@ song_print_info(struct client *client, struct song *song)
if (song->tag)
tag_print(client, song->tag);
}
static int
song_print_info_x(struct song *song, void *data)
{
struct client *client = data;
song_print_info(client, song);
return 0;
}
void
songvec_print(struct client *client, const struct songvec *sv)
{
songvec_for_each(sv, song_print_info_x, client);
}

View File

@ -27,9 +27,6 @@ struct songvec;
void
song_print_info(struct client *client, struct song *song);
void
songvec_print(struct client *client, const struct songvec *sv);
void
song_print_uri(struct client *client, struct song *song);