DatabaseGlue.cxx 3.14 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2011 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "config.h"
21
#include "DatabaseGlue.hxx"
22
#include "DatabaseSimple.hxx"
23
#include "DatabaseRegistry.hxx"
24
#include "DatabaseSave.hxx"
25
#include "Directory.hxx"
26
#include "conf.h"
27 28

extern "C" {
29
#include "db_error.h"
30
#include "stats.h"
31
#include "glib_compat.h"
32 33 34 35
}

#include "DatabasePlugin.hxx"
#include "db/SimpleDatabasePlugin.hxx"
36

Max Kellermann's avatar
Max Kellermann committed
37 38 39 40 41
#include <glib.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
42 43
#include <assert.h>
#include <string.h>
44
#include <errno.h>
45

46 47 48
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "database"

49
static Database *db;
50
static bool db_is_open;
51
static bool is_simple;
52

53
bool
54
DatabaseGlobalInit(const config_param *param, GError **error_r)
55
{
56 57
	assert(db == NULL);
	assert(!db_is_open);
58

59 60
	const char *plugin_name =
		config_get_block_string(param, "plugin", "simple");
61 62
	is_simple = strcmp(plugin_name, "simple") == 0;

63 64 65 66 67 68 69 70
	const DatabasePlugin *plugin = GetDatabasePluginByName(plugin_name);
	if (plugin == NULL) {
		g_set_error(error_r, db_quark(), 0,
			    "No such database plugin: %s", plugin_name);
		return false;
	}

	db = plugin->create(param, error_r);
71
	return db != NULL;
72 73 74
}

void
75
DatabaseGlobalDeinit(void)
76
{
77
	if (db_is_open)
78
		db->Close();
79

80
	if (db != NULL)
81
		delete db;
82 83
}

84 85 86 87 88 89 90 91
const Database *
GetDatabase()
{
	assert(db == NULL || db_is_open);

	return db;
}

92 93 94 95 96 97 98 99 100 101 102 103
const Database *
GetDatabase(GError **error_r)
{
	assert(db == nullptr || db_is_open);

	if (db == nullptr)
		g_set_error_literal(error_r, db_quark(), DB_DISABLED,
				    "No database");

	return db;
}

104 105 106 107 108
bool
db_is_simple(void)
{
	assert(db == NULL || db_is_open);

109
	return is_simple;
110 111
}

112
Directory *
113
db_get_root(void)
114
{
115
	assert(db != NULL);
116
	assert(db_is_simple());
117

118
	return ((SimpleDatabase *)db)->GetRoot();
119 120
}

121
Directory *
122
db_get_directory(const char *name)
123
{
124
	if (db == NULL)
125 126
		return NULL;

127
	Directory *music_root = db_get_root();
128 129 130
	if (name == NULL)
		return music_root;

131
	return music_root->LookupDirectory(name);
132 133
}

Max Kellermann's avatar
Max Kellermann committed
134
bool
135
db_save(GError **error_r)
136
{
137 138
	assert(db != NULL);
	assert(db_is_open);
139
	assert(db_is_simple());
140

141
	return ((SimpleDatabase *)db)->Save(error_r);
142 143
}

Max Kellermann's avatar
Max Kellermann committed
144
bool
145
DatabaseGlobalOpen(GError **error)
146
{
147 148
	assert(db != NULL);
	assert(!db_is_open);
149

150
	if (!db->Open(error))
Max Kellermann's avatar
Max Kellermann committed
151
		return false;
152

153
	db_is_open = true;
154

155
	stats_update();
156

Max Kellermann's avatar
Max Kellermann committed
157
	return true;
158 159
}

160 161
time_t
db_get_mtime(void)
162
{
163 164
	assert(db != NULL);
	assert(db_is_open);
165
	assert(db_is_simple());
166

167
	return ((SimpleDatabase *)db)->GetLastModified();
168
}