Editor.cxx 2.73 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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

#include <assert.h>
29
#include <stddef.h>
30 31

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

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

	db_unlock(); /* temporary unlock, because update_remove_song() blocks */

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

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

	db_lock();
}

50 51 52 53 54 55 56 57
void
DatabaseEditor::LockDeleteSong(Directory &parent, Song *song)
{
	db_lock();
	DeleteSong(parent, song);
	db_unlock();
}

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

	directory.ForEachSongSafe([this, &directory](Song &song){
			assert(song.parent == &directory);
			DeleteSong(directory, &song);
		});
75 76 77
}

void
78
DatabaseEditor::DeleteDirectory(Directory *directory)
79
{
80
	assert(directory->parent != nullptr);
81

82
	ClearDirectory(*directory);
83

84
	directory->Delete();
85 86
}

87 88 89 90 91 92 93 94
void
DatabaseEditor::LockDeleteDirectory(Directory *directory)
{
	db_lock();
	DeleteDirectory(directory);
	db_unlock();
}

95
bool
96
DatabaseEditor::DeleteNameIn(Directory &parent, const char *name)
97 98 99 100
{
	bool modified = false;

	db_lock();
101
	Directory *directory = parent.FindChild(name);
102

103
	if (directory != nullptr) {
104
		DeleteDirectory(directory);
105 106 107
		modified = true;
	}

108
	Song *song = parent.FindSong(name);
109
	if (song != nullptr) {
110
		DeleteSong(parent, song);
111 112 113
		modified = true;
	}

114
	parent.playlists.erase(name);
115

116 117
	db_unlock();

118 119
	return modified;
}