UpdateRemove.cxx 2.31 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 "GlobalEvents.hxx"
24 25
#include "thread/Mutex.hxx"
#include "thread/Cond.hxx"
26
#include "Song.hxx"
Max Kellermann's avatar
Max Kellermann committed
27
#include "Main.hxx"
28
#include "Instance.hxx"
29
#include "Log.hxx"
30 31

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

#include <assert.h>

38
static const Song *removed_song;
39

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

/**
 * 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)
{
50
	assert(removed_song != nullptr);
51

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

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

63 64 65 66
	{
		const auto uri = removed_song->GetURI();
		instance->DeleteSong(uri.c_str());
	}
67

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

void
update_remove_global_init(void)
{
78
	GlobalEvents::Register(GlobalEvents::DELETE, song_remove_event);
79 80 81
}

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

	removed_song = song;

88
	GlobalEvents::Emit(GlobalEvents::DELETE);
89

90
	remove_mutex.lock();
91

92
	while (removed_song != nullptr)
93
		remove_cond.wait(remove_mutex);
94

95
	remove_mutex.unlock();
96
}