DatabaseGlue.cxx 3.1 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 32 33 34
}

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

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

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

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

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

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

58
	const char *plugin_name =
59
		param.GetBlockValue("plugin", "simple");
60 61
	is_simple = strcmp(plugin_name, "simple") == 0;

62 63 64 65 66 67 68 69
	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);
70
	return db != NULL;
71 72 73
}

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

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

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

	return db;
}

91 92 93 94 95 96 97 98 99 100 101 102
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;
}

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

108
	return is_simple;
109 110
}

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

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

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

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

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

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

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

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

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

152
	db_is_open = true;
153

154
	stats_update();
155

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

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

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