2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2011-01-29 10:13:54 +01:00
|
|
|
* Copyright (C) 2003-2011 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2008-10-08 11:07:35 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2008-10-08 11:07:35 +02:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2012-08-02 18:39:17 +02:00
|
|
|
#include "DatabaseGlue.hxx"
|
2012-08-08 08:46:16 +02:00
|
|
|
#include "DatabaseRegistry.hxx"
|
2012-07-30 07:26:08 +02:00
|
|
|
|
|
|
|
extern "C" {
|
2008-10-08 11:07:35 +02:00
|
|
|
#include "database.h"
|
2011-09-10 19:24:30 +02:00
|
|
|
#include "db_error.h"
|
2011-09-10 07:33:13 +02:00
|
|
|
#include "db_save.h"
|
2008-10-08 11:07:35 +02:00
|
|
|
#include "stats.h"
|
2011-09-05 23:03:05 +02:00
|
|
|
#include "conf.h"
|
2011-09-13 20:54:27 +02:00
|
|
|
#include "glib_compat.h"
|
2012-07-30 07:26:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "directory.h"
|
|
|
|
|
|
|
|
#include "DatabasePlugin.hxx"
|
|
|
|
#include "db/SimpleDatabasePlugin.hxx"
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2008-12-29 17:28:32 +01:00
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2008-10-08 11:07:35 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
2009-01-02 17:22:54 +01:00
|
|
|
#include <errno.h>
|
2008-10-08 11:07:35 +02:00
|
|
|
|
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;
|
2011-09-05 23:03:05 +02:00
|
|
|
static bool db_is_open;
|
2013-01-02 19:19:40 +01:00
|
|
|
static bool is_simple;
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2011-09-05 23:03:05 +02:00
|
|
|
bool
|
2012-08-08 08:36:14 +02:00
|
|
|
db_init(const struct config_param *param, GError **error_r)
|
2008-10-08 11:07:35 +02:00
|
|
|
{
|
2011-09-05 23:03:05 +02:00
|
|
|
assert(db == NULL);
|
|
|
|
assert(!db_is_open);
|
2009-01-18 16:09:01 +01:00
|
|
|
|
2012-08-08 08:46:16 +02:00
|
|
|
const char *plugin_name =
|
|
|
|
config_get_block_string(param, "plugin", "simple");
|
2013-01-02 19:19:40 +01:00
|
|
|
is_simple = strcmp(plugin_name, "simple") == 0;
|
|
|
|
|
2012-08-08 08:46:16 +02:00
|
|
|
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);
|
2011-09-05 23:03:05 +02:00
|
|
|
return db != NULL;
|
2009-01-18 16:09:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-05 23:03:05 +02:00
|
|
|
db_finish(void)
|
2009-01-18 16:09:01 +01:00
|
|
|
{
|
2011-09-05 23:03:05 +02:00
|
|
|
if (db_is_open)
|
2012-07-30 07:26:08 +02:00
|
|
|
db->Close();
|
2009-01-18 16:56:07 +01:00
|
|
|
|
2011-09-05 23:03:05 +02:00
|
|
|
if (db != NULL)
|
2012-07-30 07:26:08 +02:00
|
|
|
delete db;
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 18:39:17 +02:00
|
|
|
const Database *
|
|
|
|
GetDatabase()
|
|
|
|
{
|
|
|
|
assert(db == NULL || db_is_open);
|
|
|
|
|
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
2012-08-22 21:40:20 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-08-08 08:19:30 +02:00
|
|
|
bool
|
|
|
|
db_is_simple(void)
|
|
|
|
{
|
|
|
|
assert(db == NULL || db_is_open);
|
|
|
|
|
2013-01-02 19:19:40 +01:00
|
|
|
return is_simple;
|
2012-08-08 08:19:30 +02:00
|
|
|
}
|
|
|
|
|
2008-10-08 11:07:35 +02:00
|
|
|
struct directory *
|
2008-10-08 11:07:55 +02:00
|
|
|
db_get_root(void)
|
2008-10-08 11:07:35 +02:00
|
|
|
{
|
2011-09-05 23:03:05 +02:00
|
|
|
assert(db != NULL);
|
2012-08-08 08:19:30 +02:00
|
|
|
assert(db_is_simple());
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2012-07-30 07:26:08 +02:00
|
|
|
return ((SimpleDatabase *)db)->GetRoot();
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct directory *
|
2008-10-08 11:07:55 +02:00
|
|
|
db_get_directory(const char *name)
|
2008-10-08 11:07:35 +02:00
|
|
|
{
|
2011-09-05 23:03:05 +02:00
|
|
|
if (db == NULL)
|
2009-01-18 16:56:07 +01:00
|
|
|
return NULL;
|
|
|
|
|
2011-09-05 23:03:05 +02:00
|
|
|
struct directory *music_root = db_get_root();
|
2008-10-08 11:07:35 +02:00
|
|
|
if (name == NULL)
|
|
|
|
return music_root;
|
|
|
|
|
2012-01-31 22:12:14 +01:00
|
|
|
struct directory *directory =
|
|
|
|
directory_lookup_directory(music_root, name);
|
|
|
|
return directory;
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct song *
|
2008-10-09 16:26:09 +02:00
|
|
|
db_get_song(const char *file)
|
2008-10-08 11:07:35 +02:00
|
|
|
{
|
2009-01-18 16:56:07 +01:00
|
|
|
assert(file != NULL);
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2008-12-29 17:28:37 +01:00
|
|
|
g_debug("get song: %s", file);
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2011-09-05 23:03:05 +02:00
|
|
|
if (db == NULL)
|
2009-01-18 16:56:07 +01:00
|
|
|
return NULL;
|
|
|
|
|
2012-07-30 07:26:08 +02:00
|
|
|
return db->GetSong(file, NULL);
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
2012-08-15 23:28:19 +02:00
|
|
|
void
|
|
|
|
db_return_song(struct song *song)
|
|
|
|
{
|
|
|
|
assert(db != nullptr);
|
|
|
|
assert(song != nullptr);
|
|
|
|
|
|
|
|
db->ReturnSong(song);
|
|
|
|
}
|
|
|
|
|
2009-01-04 21:18:40 +01:00
|
|
|
bool
|
2011-09-09 23:28:27 +02:00
|
|
|
db_save(GError **error_r)
|
2008-10-08 11:07:35 +02:00
|
|
|
{
|
2011-09-05 23:03:05 +02:00
|
|
|
assert(db != NULL);
|
|
|
|
assert(db_is_open);
|
2012-08-08 08:19:30 +02:00
|
|
|
assert(db_is_simple());
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2012-07-30 07:26:08 +02:00
|
|
|
return ((SimpleDatabase *)db)->Save(error_r);
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
2009-01-04 21:18:40 +01:00
|
|
|
bool
|
2009-03-02 15:42:21 +01:00
|
|
|
db_load(GError **error)
|
2008-10-08 11:07:35 +02:00
|
|
|
{
|
2011-09-05 23:03:05 +02:00
|
|
|
assert(db != NULL);
|
|
|
|
assert(!db_is_open);
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2012-07-30 07:26:08 +02:00
|
|
|
if (!db->Open(error))
|
2009-01-04 21:18:40 +01:00
|
|
|
return false;
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2011-09-05 23:03:05 +02:00
|
|
|
db_is_open = true;
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2009-01-04 20:57:06 +01:00
|
|
|
stats_update();
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2009-01-04 21:18:40 +01:00
|
|
|
return true;
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
2008-10-08 11:07:55 +02:00
|
|
|
time_t
|
|
|
|
db_get_mtime(void)
|
2008-10-08 11:07:35 +02:00
|
|
|
{
|
2011-09-05 23:03:05 +02:00
|
|
|
assert(db != NULL);
|
|
|
|
assert(db_is_open);
|
2012-08-08 08:19:30 +02:00
|
|
|
assert(db_is_simple());
|
2011-09-05 23:03:05 +02:00
|
|
|
|
2012-07-30 07:26:08 +02:00
|
|
|
return ((SimpleDatabase *)db)->GetLastModified();
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|