Editor.cxx 2.68 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20 21
#include "Editor.hxx"
#include "Remove.hxx"
22
#include "db/PlaylistVector.hxx"
Max Kellermann's avatar
Max Kellermann committed
23
#include "db/DatabaseLock.hxx"
24 25
#include "db/plugins/simple/Directory.hxx"
#include "db/plugins/simple/Song.hxx"
26 27 28 29

#include <assert.h>

void
30
DatabaseEditor::DeleteSong(Directory &dir, Song *del)
31
{
32
	assert(del->parent == &dir);
33 34

	/* first, prevent traversers in main task from getting this */
35
	dir.RemoveSong(del);
36

37 38
	/* temporary unlock, because update_remove_song() blocks */
	const ScopeDatabaseUnlock unlock;
39 40

	/* now take it out of the playlist (in the main_task) */
41
	remove.Remove(del->GetURI());
42 43

	/* finally, all possible references gone, free it */
44
	del->Free();
45 46
}

47 48 49
void
DatabaseEditor::LockDeleteSong(Directory &parent, Song *song)
{
50
	const ScopeDatabaseLock protect;
51 52 53
	DeleteSong(parent, song);
}

54 55 56 57 58 59
/**
 * Recursively remove all sub directories and songs from a directory,
 * leaving an empty directory.
 *
 * Caller must lock the #db_mutex.
 */
60 61
inline void
DatabaseEditor::ClearDirectory(Directory &directory)
62
{
63 64 65 66 67 68 69 70
	directory.ForEachChildSafe([this](Directory &child){
			DeleteDirectory(&child);
		});

	directory.ForEachSongSafe([this, &directory](Song &song){
			assert(song.parent == &directory);
			DeleteSong(directory, &song);
		});
71 72 73
}

void
74
DatabaseEditor::DeleteDirectory(Directory *directory)
75
{
76
	assert(directory->parent != nullptr);
77

78
	ClearDirectory(*directory);
79

80
	directory->Delete();
81 82
}

83 84 85
void
DatabaseEditor::LockDeleteDirectory(Directory *directory)
{
86
	const ScopeDatabaseLock protect;
87 88 89
	DeleteDirectory(directory);
}

90
bool
91
DatabaseEditor::DeleteNameIn(Directory &parent, const char *name)
92
{
93 94
	const ScopeDatabaseLock protect;

95 96
	bool modified = false;

97
	Directory *directory = parent.FindChild(name);
98

99
	if (directory != nullptr) {
100
		DeleteDirectory(directory);
101 102 103
		modified = true;
	}

104
	Song *song = parent.FindSong(name);
105
	if (song != nullptr) {
106
		DeleteSong(parent, song);
107 108 109
		modified = true;
	}

110
	parent.playlists.erase(name);
111 112 113

	return modified;
}