Instance.cxx 3.44 KB
Newer Older
1
/*
2
 * Copyright 2003-2019 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * 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"
#include "Instance.hxx"
#include "Partition.hxx"
#include "Idle.hxx"
24
#include "Stats.hxx"
25
#include "client/List.hxx"
26
#include "input/cache/Manager.hxx"
27

28 29 30 31 32
#ifdef ENABLE_CURL
#include "RemoteTagCache.hxx"
#include "util/UriUtil.hxx"
#endif

33
#ifdef ENABLE_DATABASE
34
#include "db/DatabaseError.hxx"
35
#include "db/Interface.hxx"
36
#include "db/update/Service.hxx"
37
#include "storage/StorageInterface.hxx"
38

39 40 41 42
#ifdef ENABLE_NEIGHBOR_PLUGINS
#include "neighbor/Glue.hxx"
#endif

43
#ifdef ENABLE_SQLITE
44
#include "sticker/Database.hxx"
45 46
#include "sticker/SongSticker.hxx"
#endif
47 48
#endif

49
#include <exception>
50

51
Instance::Instance()
52
	:rtio_thread(true),
53 54 55
#ifdef ENABLE_SYSTEMD_DAEMON
	 systemd_watchdog(event_loop),
#endif
56
	 idle_monitor(event_loop, BIND_THIS_METHOD(OnIdle))
57 58 59
{
}

60 61 62
Instance::~Instance() noexcept
{
#ifdef ENABLE_DATABASE
63 64
	delete update;

65 66
	if (database != nullptr) {
		database->Close();
67
		database.reset();
68 69 70 71 72
	}

	delete storage;
#endif
}
73

74
Partition *
75
Instance::FindPartition(const char *name) noexcept
76 77 78 79 80 81 82 83
{
	for (auto &partition : partitions)
		if (partition.name == name)
			return &partition;

	return nullptr;
}

84
#ifdef ENABLE_DATABASE
85

86 87 88 89 90 91 92 93 94 95
const Database &
Instance::GetDatabaseOrThrow() const
{
	if (database == nullptr)
		throw DatabaseError(DatabaseErrorCode::DISABLED,
				    "No database");

	return *database;
}

96
void
97
Instance::OnDatabaseModified() noexcept
98
{
99 100 101 102 103
	assert(database != nullptr);

	/* propagate the change to all subsystems */

	stats_invalidate();
104 105 106

	for (auto &partition : partitions)
		partition.DatabaseModified(*database);
107
}
108

109
void
110
Instance::OnDatabaseSongRemoved(const char *uri) noexcept
111 112 113 114 115
{
	assert(database != nullptr);

#ifdef ENABLE_SQLITE
	/* if the song has a sticker, remove it */
116
	if (HasStickerDatabase()) {
117
		try {
118
			sticker_song_delete(*sticker_database, uri);
119
		} catch (...) {
120 121
		}
	}
122 123
#endif

124 125
	for (auto &partition : partitions)
		partition.StaleSong(uri);
126 127
}

128 129
#endif

130 131 132
#ifdef ENABLE_NEIGHBOR_PLUGINS

void
133
Instance::FoundNeighbor(gcc_unused const NeighborInfo &info) noexcept
134
{
135 136
	for (auto &partition : partitions)
		partition.EmitIdle(IDLE_NEIGHBOR);
137 138 139
}

void
140
Instance::LostNeighbor(gcc_unused const NeighborInfo &info) noexcept
141
{
142 143
	for (auto &partition : partitions)
		partition.EmitIdle(IDLE_NEIGHBOR);
144 145 146
}

#endif
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

#ifdef ENABLE_CURL

void
Instance::LookupRemoteTag(const char *uri) noexcept
{
	if (!uri_has_scheme(uri))
		return;

	if (!remote_tag_cache)
		remote_tag_cache = std::make_unique<RemoteTagCache>(event_loop,
								    *this);

	remote_tag_cache->Lookup(uri);
}

void
Instance::OnRemoteTag(const char *uri, const Tag &tag) noexcept
{
	if (!tag.IsDefined())
		/* boring */
		return;

	for (auto &partition : partitions)
		partition.TagModified(uri, tag);
}

#endif