2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2011-01-29 10:13:54 +01:00
|
|
|
* Copyright (C) 2003-2011 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2004-02-24 00:41:20 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2004-02-24 00:41:20 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-02 22:52:08 +01:00
|
|
|
#include "Directory.hxx"
|
2012-08-29 19:12:26 +02:00
|
|
|
#include "SongFilter.hxx"
|
2013-01-02 20:25:20 +01:00
|
|
|
#include "PlaylistVector.hxx"
|
2013-01-02 20:56:21 +01:00
|
|
|
#include "DatabaseLock.hxx"
|
2013-04-17 22:25:57 +02:00
|
|
|
#include "SongSort.hxx"
|
2013-07-28 13:25:12 +02:00
|
|
|
#include "Song.hxx"
|
2012-07-30 07:26:08 +02:00
|
|
|
|
|
|
|
extern "C" {
|
2012-01-24 18:20:58 +01:00
|
|
|
#include "util/list_sort.h"
|
2012-07-30 07:26:08 +02:00
|
|
|
}
|
2005-03-06 20:00:58 +01:00
|
|
|
|
2009-01-02 16:26:19 +01:00
|
|
|
#include <glib.h>
|
|
|
|
|
2008-10-09 15:23:37 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
2009-01-02 16:26:19 +01:00
|
|
|
#include <stdlib.h>
|
2008-10-09 15:23:37 +02:00
|
|
|
|
2013-01-03 01:36:28 +01:00
|
|
|
inline Directory *
|
|
|
|
Directory::Allocate(const char *path)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-13 09:55:00 +02:00
|
|
|
assert(path != NULL);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2012-08-09 19:44:10 +02:00
|
|
|
const size_t path_size = strlen(path) + 1;
|
2013-01-02 23:06:20 +01:00
|
|
|
Directory *directory =
|
|
|
|
(Directory *)g_malloc0(sizeof(*directory)
|
|
|
|
- sizeof(directory->path)
|
|
|
|
+ path_size);
|
2013-01-03 01:36:28 +01:00
|
|
|
new(directory) Directory(path);
|
2012-08-09 19:44:10 +02:00
|
|
|
|
|
|
|
return directory;
|
|
|
|
}
|
|
|
|
|
2013-01-03 02:12:34 +01:00
|
|
|
Directory::Directory()
|
|
|
|
{
|
|
|
|
INIT_LIST_HEAD(&children);
|
|
|
|
INIT_LIST_HEAD(&songs);
|
|
|
|
|
|
|
|
path[0] = 0;
|
|
|
|
}
|
|
|
|
|
2013-01-03 01:36:28 +01:00
|
|
|
Directory::Directory(const char *_path)
|
2012-08-09 19:44:10 +02:00
|
|
|
{
|
2013-01-03 01:36:28 +01:00
|
|
|
INIT_LIST_HEAD(&children);
|
|
|
|
INIT_LIST_HEAD(&songs);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2013-01-03 01:36:28 +01:00
|
|
|
strcpy(path, _path);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2013-01-03 01:36:28 +01:00
|
|
|
Directory::~Directory()
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-07-28 13:25:12 +02:00
|
|
|
Song *song, *ns;
|
2013-01-02 23:06:10 +01:00
|
|
|
directory_for_each_song_safe(song, ns, this)
|
2013-07-28 13:25:12 +02:00
|
|
|
song->Free();
|
2008-10-31 09:20:02 +01:00
|
|
|
|
2013-01-02 23:06:20 +01:00
|
|
|
Directory *child, *n;
|
2013-01-02 23:06:10 +01:00
|
|
|
directory_for_each_child_safe(child, n, this)
|
|
|
|
child->Free();
|
2013-01-03 01:36:28 +01:00
|
|
|
}
|
2008-10-31 09:20:02 +01:00
|
|
|
|
2013-01-03 01:36:28 +01:00
|
|
|
Directory *
|
|
|
|
Directory::NewGeneric(const char *path, Directory *parent)
|
|
|
|
{
|
|
|
|
assert(path != NULL);
|
|
|
|
assert((*path == 0) == (parent == NULL));
|
|
|
|
|
|
|
|
Directory *directory = Allocate(path);
|
|
|
|
|
|
|
|
directory->parent = parent;
|
|
|
|
|
|
|
|
return directory;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Directory::Free()
|
|
|
|
{
|
|
|
|
this->Directory::~Directory();
|
2013-01-02 23:06:10 +01:00
|
|
|
g_free(this);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2012-01-24 19:07:11 +01:00
|
|
|
void
|
2013-01-02 23:06:20 +01:00
|
|
|
Directory::Delete()
|
2012-01-24 19:07:11 +01:00
|
|
|
{
|
2012-01-31 22:12:14 +01:00
|
|
|
assert(holding_db_lock());
|
2013-01-02 23:06:10 +01:00
|
|
|
assert(parent != nullptr);
|
2012-01-24 19:07:11 +01:00
|
|
|
|
2013-01-02 23:06:10 +01:00
|
|
|
list_del(&siblings);
|
|
|
|
Free();
|
2012-01-24 19:07:11 +01:00
|
|
|
}
|
|
|
|
|
2008-10-13 16:32:39 +02:00
|
|
|
const char *
|
2013-01-02 23:06:20 +01:00
|
|
|
Directory::GetName() const
|
2008-10-13 16:32:39 +02:00
|
|
|
{
|
2013-01-02 23:06:10 +01:00
|
|
|
assert(!IsRoot());
|
|
|
|
assert(path != nullptr);
|
2012-04-04 19:08:05 +02:00
|
|
|
|
2013-01-02 23:06:10 +01:00
|
|
|
const char *slash = strrchr(path, '/');
|
|
|
|
assert((slash == nullptr) == parent->IsRoot());
|
2012-04-04 19:08:05 +02:00
|
|
|
|
|
|
|
return slash != NULL
|
|
|
|
? slash + 1
|
2013-01-02 23:06:10 +01:00
|
|
|
: path;
|
2008-10-13 16:32:39 +02:00
|
|
|
}
|
|
|
|
|
2013-01-02 23:06:20 +01:00
|
|
|
Directory *
|
|
|
|
Directory::CreateChild(const char *name_utf8)
|
2012-01-24 19:07:11 +01:00
|
|
|
{
|
2012-01-31 22:12:14 +01:00
|
|
|
assert(holding_db_lock());
|
2012-01-24 19:07:11 +01:00
|
|
|
assert(name_utf8 != NULL);
|
|
|
|
assert(*name_utf8 != 0);
|
|
|
|
|
|
|
|
char *allocated;
|
|
|
|
const char *path_utf8;
|
2013-01-02 23:06:10 +01:00
|
|
|
if (IsRoot()) {
|
2012-01-24 19:07:11 +01:00
|
|
|
allocated = NULL;
|
|
|
|
path_utf8 = name_utf8;
|
|
|
|
} else {
|
2013-01-02 23:06:10 +01:00
|
|
|
allocated = g_strconcat(GetPath(),
|
2012-01-24 19:07:11 +01:00
|
|
|
"/", name_utf8, NULL);
|
|
|
|
path_utf8 = allocated;
|
|
|
|
}
|
|
|
|
|
2013-01-02 23:06:20 +01:00
|
|
|
Directory *child = NewGeneric(path_utf8, this);
|
2012-01-24 19:07:11 +01:00
|
|
|
g_free(allocated);
|
|
|
|
|
2013-01-02 23:06:10 +01:00
|
|
|
list_add_tail(&child->siblings, &children);
|
|
|
|
return child;
|
2012-01-24 19:07:11 +01:00
|
|
|
}
|
|
|
|
|
2013-01-02 23:06:20 +01:00
|
|
|
const Directory *
|
|
|
|
Directory::FindChild(const char *name) const
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2012-01-31 22:12:14 +01:00
|
|
|
assert(holding_db_lock());
|
2008-09-29 13:11:40 +02:00
|
|
|
|
2013-01-02 23:06:20 +01:00
|
|
|
const Directory *child;
|
2013-01-02 23:06:10 +01:00
|
|
|
directory_for_each_child(child, this)
|
|
|
|
if (strcmp(child->GetName(), name) == 0)
|
2012-01-24 18:20:58 +01:00
|
|
|
return child;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-08-14 11:52:00 +02:00
|
|
|
|
2012-01-24 18:20:58 +01:00
|
|
|
void
|
2013-01-02 23:06:20 +01:00
|
|
|
Directory::PruneEmpty()
|
2012-01-24 18:20:58 +01:00
|
|
|
{
|
2012-01-31 22:12:14 +01:00
|
|
|
assert(holding_db_lock());
|
|
|
|
|
2013-01-02 23:06:20 +01:00
|
|
|
Directory *child, *n;
|
2013-01-02 23:06:10 +01:00
|
|
|
directory_for_each_child_safe(child, n, this) {
|
|
|
|
child->PruneEmpty();
|
2009-08-14 11:52:00 +02:00
|
|
|
|
2013-01-02 23:06:10 +01:00
|
|
|
if (child->IsEmpty())
|
|
|
|
child->Delete();
|
2004-03-09 23:48:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-02 23:06:20 +01:00
|
|
|
Directory *
|
|
|
|
Directory::LookupDirectory(const char *uri)
|
2004-04-14 04:35:29 +02:00
|
|
|
{
|
2012-01-31 22:12:14 +01:00
|
|
|
assert(holding_db_lock());
|
2009-04-01 18:41:33 +02:00
|
|
|
assert(uri != NULL);
|
2008-10-08 11:06:44 +02:00
|
|
|
|
2009-04-01 18:41:33 +02:00
|
|
|
if (isRootDirectory(uri))
|
2013-01-02 23:06:10 +01:00
|
|
|
return this;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2012-01-29 14:42:28 +01:00
|
|
|
char *duplicated = g_strdup(uri), *name = duplicated;
|
2012-01-31 22:12:14 +01:00
|
|
|
|
2013-01-02 23:06:20 +01:00
|
|
|
Directory *d = this;
|
2008-09-29 13:11:40 +02:00
|
|
|
while (1) {
|
2012-01-29 14:42:28 +01:00
|
|
|
char *slash = strchr(name, '/');
|
|
|
|
if (slash == name) {
|
2013-01-02 23:06:10 +01:00
|
|
|
d = NULL;
|
2008-09-29 13:11:40 +02:00
|
|
|
break;
|
2012-01-29 14:42:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (slash != NULL)
|
|
|
|
*slash = '\0';
|
|
|
|
|
2013-01-02 23:06:10 +01:00
|
|
|
d = d->FindChild(name);
|
|
|
|
if (d == NULL || slash == NULL)
|
2008-09-29 13:11:40 +02:00
|
|
|
break;
|
2012-01-29 14:42:28 +01:00
|
|
|
|
|
|
|
name = slash + 1;
|
2008-09-29 13:11:40 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2009-01-25 18:47:21 +01:00
|
|
|
g_free(duplicated);
|
2004-04-14 04:35:29 +02:00
|
|
|
|
2013-01-02 23:06:10 +01:00
|
|
|
return d;
|
2004-04-14 04:35:29 +02:00
|
|
|
}
|
|
|
|
|
2012-01-24 21:38:31 +01:00
|
|
|
void
|
2013-07-28 13:25:12 +02:00
|
|
|
Directory::AddSong(Song *song)
|
2012-01-24 21:38:31 +01:00
|
|
|
{
|
2012-06-13 21:23:34 +02:00
|
|
|
assert(holding_db_lock());
|
2012-01-24 21:38:31 +01:00
|
|
|
assert(song != NULL);
|
2013-01-02 23:06:10 +01:00
|
|
|
assert(song->parent == this);
|
2012-01-24 21:38:31 +01:00
|
|
|
|
2013-01-02 23:06:10 +01:00
|
|
|
list_add_tail(&song->siblings, &songs);
|
2012-01-24 21:38:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-07-28 13:25:12 +02:00
|
|
|
Directory::RemoveSong(Song *song)
|
2012-01-24 21:38:31 +01:00
|
|
|
{
|
2012-06-13 21:23:34 +02:00
|
|
|
assert(holding_db_lock());
|
2012-01-24 21:38:31 +01:00
|
|
|
assert(song != NULL);
|
2013-01-02 23:06:10 +01:00
|
|
|
assert(song->parent == this);
|
2012-01-24 21:38:31 +01:00
|
|
|
|
2012-01-24 21:33:09 +01:00
|
|
|
list_del(&song->siblings);
|
2012-01-24 21:38:31 +01:00
|
|
|
}
|
|
|
|
|
2013-07-28 13:25:12 +02:00
|
|
|
const Song *
|
2013-01-02 23:06:20 +01:00
|
|
|
Directory::FindSong(const char *name_utf8) const
|
2012-01-24 21:38:31 +01:00
|
|
|
{
|
2012-01-31 22:12:14 +01:00
|
|
|
assert(holding_db_lock());
|
2012-01-24 21:38:31 +01:00
|
|
|
assert(name_utf8 != NULL);
|
|
|
|
|
2013-07-28 13:25:12 +02:00
|
|
|
Song *song;
|
2013-01-02 23:06:10 +01:00
|
|
|
directory_for_each_song(song, this) {
|
|
|
|
assert(song->parent == this);
|
2012-01-24 21:33:09 +01:00
|
|
|
|
2012-01-31 22:12:14 +01:00
|
|
|
if (strcmp(song->uri, name_utf8) == 0)
|
2012-01-24 21:33:09 +01:00
|
|
|
return song;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2012-01-24 21:38:31 +01:00
|
|
|
}
|
|
|
|
|
2013-07-28 13:25:12 +02:00
|
|
|
Song *
|
2013-01-02 23:06:20 +01:00
|
|
|
Directory::LookupSong(const char *uri)
|
2009-04-01 18:41:37 +02:00
|
|
|
{
|
|
|
|
char *duplicated, *base;
|
|
|
|
|
2012-01-31 22:12:14 +01:00
|
|
|
assert(holding_db_lock());
|
2009-04-01 18:41:37 +02:00
|
|
|
assert(uri != NULL);
|
|
|
|
|
|
|
|
duplicated = g_strdup(uri);
|
|
|
|
base = strrchr(duplicated, '/');
|
|
|
|
|
2013-01-02 23:06:20 +01:00
|
|
|
Directory *d = this;
|
2009-04-01 18:41:37 +02:00
|
|
|
if (base != NULL) {
|
|
|
|
*base++ = 0;
|
2013-01-02 23:06:10 +01:00
|
|
|
d = d->LookupDirectory(duplicated);
|
|
|
|
if (d == nullptr) {
|
2009-04-01 18:41:37 +02:00
|
|
|
g_free(duplicated);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
base = duplicated;
|
|
|
|
|
2013-07-28 13:25:12 +02:00
|
|
|
Song *song = d->FindSong(base);
|
2013-01-02 23:06:10 +01:00
|
|
|
assert(song == NULL || song->parent == d);
|
2009-04-01 18:41:37 +02:00
|
|
|
|
|
|
|
g_free(duplicated);
|
|
|
|
return song;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-01-24 18:20:58 +01:00
|
|
|
static int
|
2013-08-04 23:48:01 +02:00
|
|
|
directory_cmp(gcc_unused void *priv,
|
2012-01-24 18:20:58 +01:00
|
|
|
struct list_head *_a, struct list_head *_b)
|
|
|
|
{
|
2013-01-02 23:06:20 +01:00
|
|
|
const Directory *a = (const Directory *)_a;
|
|
|
|
const Directory *b = (const Directory *)_b;
|
2012-01-24 18:20:58 +01:00
|
|
|
return g_utf8_collate(a->path, b->path);
|
|
|
|
}
|
|
|
|
|
2008-10-08 10:48:48 +02:00
|
|
|
void
|
2013-01-02 23:06:20 +01:00
|
|
|
Directory::Sort()
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2012-01-31 22:12:14 +01:00
|
|
|
assert(holding_db_lock());
|
|
|
|
|
2013-01-02 23:06:10 +01:00
|
|
|
list_sort(NULL, &children, directory_cmp);
|
|
|
|
song_list_sort(&songs);
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2013-01-02 23:06:20 +01:00
|
|
|
Directory *child;
|
2013-01-02 23:06:10 +01:00
|
|
|
directory_for_each_child(child, this)
|
|
|
|
child->Sort();
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2011-09-10 19:24:30 +02:00
|
|
|
bool
|
2013-01-02 23:06:20 +01:00
|
|
|
Directory::Walk(bool recursive, const SongFilter *filter,
|
2012-07-30 07:26:08 +02:00
|
|
|
VisitDirectory visit_directory, VisitSong visit_song,
|
|
|
|
VisitPlaylist visit_playlist,
|
|
|
|
GError **error_r) const
|
2004-02-24 00:41:20 +01:00
|
|
|
{
|
2011-09-10 19:24:30 +02:00
|
|
|
assert(error_r == NULL || *error_r == NULL);
|
|
|
|
|
2012-07-30 07:26:08 +02:00
|
|
|
if (visit_song) {
|
2013-07-28 13:25:12 +02:00
|
|
|
Song *song;
|
2012-07-30 07:26:08 +02:00
|
|
|
directory_for_each_song(song, this)
|
2012-08-29 19:27:03 +02:00
|
|
|
if ((filter == nullptr || filter->Match(*song)) &&
|
2012-08-07 23:22:37 +02:00
|
|
|
!visit_song(*song, error_r))
|
2011-09-10 19:24:30 +02:00
|
|
|
return false;
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
|
|
|
|
2012-07-30 07:26:08 +02:00
|
|
|
if (visit_playlist) {
|
2013-01-02 22:16:52 +01:00
|
|
|
for (const PlaylistInfo &p : playlists)
|
|
|
|
if (!visit_playlist(p, *this, error_r))
|
2011-09-13 22:02:37 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-02 23:06:20 +01:00
|
|
|
Directory *child;
|
2012-07-30 07:26:08 +02:00
|
|
|
directory_for_each_child(child, this) {
|
|
|
|
if (visit_directory &&
|
2012-08-07 21:32:08 +02:00
|
|
|
!visit_directory(*child, error_r))
|
2011-09-13 20:38:12 +02:00
|
|
|
return false;
|
|
|
|
|
2011-09-10 18:48:10 +02:00
|
|
|
if (recursive &&
|
2012-08-29 19:27:03 +02:00
|
|
|
!child->Walk(recursive, filter,
|
2012-08-07 23:22:37 +02:00
|
|
|
visit_directory, visit_song, visit_playlist,
|
|
|
|
error_r))
|
2011-09-13 20:41:06 +02:00
|
|
|
return false;
|
2011-09-10 19:24:30 +02:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2011-09-10 19:24:30 +02:00
|
|
|
return true;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|