mpd/src/DatabaseGlue.cxx

168 lines
3.1 KiB
C++
Raw Normal View History

/*
2011-01-29 10:13:54 +01:00
* Copyright (C) 2003-2011 The Music Player Daemon Project
* 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 "DatabaseGlue.hxx"
#include "DatabaseSimple.hxx"
#include "DatabaseRegistry.hxx"
2013-01-02 19:52:57 +01:00
#include "DatabaseSave.hxx"
2013-08-10 19:43:27 +02:00
#include "DatabaseError.hxx"
2013-01-02 22:52:08 +01:00
#include "Directory.hxx"
#include "util/Error.hxx"
2013-01-07 09:38:02 +01:00
#include "conf.h"
2012-07-30 07:26:08 +02:00
extern "C" {
#include "stats.h"
2012-07-30 07:26:08 +02:00
}
#include "DatabasePlugin.hxx"
#include "db/SimpleDatabasePlugin.hxx"
#include <glib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
2009-01-02 17:22:54 +01:00
#include <errno.h>
2008-12-29 17:28:37 +01:00
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "database"
2012-07-30 07:26:08 +02:00
static Database *db;
static bool db_is_open;
static bool is_simple;
bool
DatabaseGlobalInit(const config_param &param, Error &error)
{
assert(db == NULL);
assert(!db_is_open);
const char *plugin_name =
param.GetBlockValue("plugin", "simple");
is_simple = strcmp(plugin_name, "simple") == 0;
const DatabasePlugin *plugin = GetDatabasePluginByName(plugin_name);
if (plugin == NULL) {
error.Format(db_domain,
"No such database plugin: %s", plugin_name);
return false;
}
db = plugin->create(param, error);
return db != NULL;
}
void
DatabaseGlobalDeinit(void)
{
if (db_is_open)
2012-07-30 07:26:08 +02:00
db->Close();
if (db != NULL)
2012-07-30 07:26:08 +02:00
delete db;
}
const Database *
GetDatabase()
{
assert(db == NULL || db_is_open);
return db;
}
const Database *
GetDatabase(Error &error)
{
assert(db == nullptr || db_is_open);
if (db == nullptr)
error.Set(db_domain, DB_DISABLED, "No database");
return db;
}
bool
db_is_simple(void)
{
assert(db == NULL || db_is_open);
return is_simple;
}
Directory *
db_get_root(void)
{
assert(db != NULL);
assert(db_is_simple());
2012-07-30 07:26:08 +02:00
return ((SimpleDatabase *)db)->GetRoot();
}
Directory *
db_get_directory(const char *name)
{
if (db == NULL)
return NULL;
Directory *music_root = db_get_root();
if (name == NULL)
return music_root;
2013-01-02 23:06:10 +01:00
return music_root->LookupDirectory(name);
}
bool
db_save(Error &error)
{
assert(db != NULL);
assert(db_is_open);
assert(db_is_simple());
return ((SimpleDatabase *)db)->Save(error);
}
bool
DatabaseGlobalOpen(Error &error)
{
assert(db != NULL);
assert(!db_is_open);
2012-07-30 07:26:08 +02:00
if (!db->Open(error))
return false;
db_is_open = true;
2009-01-04 20:57:06 +01:00
stats_update();
return true;
}
time_t
db_get_mtime(void)
{
assert(db != NULL);
assert(db_is_open);
assert(db_is_simple());
2012-07-30 07:26:08 +02:00
return ((SimpleDatabase *)db)->GetLastModified();
}