UpdateRemove.cxx 2.29 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 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
#include "config.h" /* must be first for large file support */
Max Kellermann's avatar
Max Kellermann committed
21
#include "UpdateRemove.hxx"
22
#include "UpdateDomain.hxx"
23
#include "Playlist.hxx"
24
#include "GlobalEvents.hxx"
25 26
#include "thread/Mutex.hxx"
#include "thread/Cond.hxx"
27
#include "Song.hxx"
Max Kellermann's avatar
Max Kellermann committed
28
#include "Main.hxx"
29
#include "Instance.hxx"
30
#include "Log.hxx"
31 32

#ifdef ENABLE_SQLITE
Max Kellermann's avatar
Max Kellermann committed
33 34
#include "StickerDatabase.hxx"
#include "SongSticker.hxx"
35 36 37 38
#endif

#include <assert.h>

39
static const Song *removed_song;
40

41 42
static Mutex remove_mutex;
static Cond remove_cond;
43 44 45 46 47 48 49 50

/**
 * Safely remove a song from the database.  This must be done in the
 * main task, to be sure that there is no pointer left to it.
 */
static void
song_remove_event(void)
{
51
	assert(removed_song != nullptr);
52

53 54
	{
		const auto uri = removed_song->GetURI();
55
		FormatDefault(update_domain, "removing %s", uri.c_str());
56
	}
57 58 59 60

#ifdef ENABLE_SQLITE
	/* if the song has a sticker, remove it */
	if (sticker_enabled())
61
		sticker_song_delete(removed_song);
62 63
#endif

64
	instance->DeleteSong(*removed_song);
65

66
	/* clear "removed_song" and send signal to update thread */
67
	remove_mutex.lock();
68
	removed_song = nullptr;
69 70
	remove_cond.signal();
	remove_mutex.unlock();
71 72 73 74 75
}

void
update_remove_global_init(void)
{
76
	GlobalEvents::Register(GlobalEvents::DELETE, song_remove_event);
77 78 79
}

void
80
update_remove_song(const Song *song)
81
{
82
	assert(removed_song == nullptr);
83 84 85

	removed_song = song;

86
	GlobalEvents::Emit(GlobalEvents::DELETE);
87

88
	remove_mutex.lock();
89

90
	while (removed_song != nullptr)
91
		remove_cond.wait(remove_mutex);
92

93
	remove_mutex.unlock();
94
}