PlaylistUpdate.cxx 1.88 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 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 "Playlist.hxx"
21
#include "db/Interface.hxx"
22 23
#include "song/LightSong.hxx"
#include "song/DetachedSong.hxx"
24

25
static bool
26
UpdatePlaylistSong(const Database &db, DetachedSong &song)
27
{
28
	if (!song.IsInDatabase() || !song.IsFile())
29 30 31 32
		/* only update Songs instances that are "detached"
		   from the Database */
		return false;

33 34
	const LightSong *original;
	try {
35
		original = db.GetSong(song.GetURI());
36
	} catch (...) {
37 38 39 40
		/* not found - shouldn't happen, because the update
		   thread should ensure that all stale Song instances
		   have been purged */
		return false;
41 42 43
	}

	assert(original != nullptr);
44

45
	if (original->mtime == song.GetLastModified()) {
46 47 48 49 50
		/* not modified */
		db.ReturnSong(original);
		return false;
	}

51
	song.SetLastModified(original->mtime);
52
	song.SetTag(original->tag);
53 54 55 56 57 58

	db.ReturnSong(original);
	return true;
}

void
59
playlist::DatabaseModified(const Database &db)
60 61 62 63
{
	bool modified = false;

	for (unsigned i = 0, n = queue.GetLength(); i != n; ++i) {
64
		if (UpdatePlaylistSong(db, queue.Get(i))) {
65 66 67 68 69
			queue.ModifyAtPosition(i);
			modified = true;
		}
	}

70 71
	if (modified)
		OnModified();
72
}