Editor.cxx 2.74 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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" /* must be first for large file support */
21 22
#include "Editor.hxx"
#include "Remove.hxx"
23
#include "db/PlaylistVector.hxx"
Max Kellermann's avatar
Max Kellermann committed
24
#include "db/DatabaseLock.hxx"
25 26
#include "db/plugins/simple/Directory.hxx"
#include "db/plugins/simple/Song.hxx"
27 28 29 30

#include <assert.h>

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

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

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

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

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

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

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

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

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

79
	ClearDirectory(*directory);
80

81
	directory->Delete();
82 83
}

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

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

96 97
	bool modified = false;

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

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

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

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

	return modified;
}