2009-01-19 18:51:57 +01:00
|
|
|
/*
|
2010-01-01 05:55:13 +01:00
|
|
|
* Copyright (C) 2003-2010 The Music Player Daemon Project
|
2009-01-19 18:51:57 +01:00
|
|
|
* 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.
|
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.
|
2009-01-19 18:51:57 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2009-01-19 18:51:57 +01:00
|
|
|
#include "sticker.h"
|
2009-01-25 16:23:02 +01:00
|
|
|
#include "idle.h"
|
2009-01-19 18:51:57 +01:00
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#include <sqlite3.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "sticker"
|
|
|
|
|
2009-11-10 20:55:29 +01:00
|
|
|
#if SQLITE_VERSION_NUMBER < 3003009
|
|
|
|
#define sqlite3_prepare_v2 sqlite3_prepare
|
|
|
|
#endif
|
|
|
|
|
2009-03-14 14:20:01 +01:00
|
|
|
struct sticker {
|
|
|
|
GHashTable *table;
|
|
|
|
};
|
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
enum sticker_sql {
|
|
|
|
STICKER_SQL_GET,
|
|
|
|
STICKER_SQL_LIST,
|
|
|
|
STICKER_SQL_UPDATE,
|
|
|
|
STICKER_SQL_INSERT,
|
|
|
|
STICKER_SQL_DELETE,
|
2009-04-28 20:23:27 +02:00
|
|
|
STICKER_SQL_DELETE_VALUE,
|
2009-04-01 18:42:06 +02:00
|
|
|
STICKER_SQL_FIND,
|
2009-03-27 17:05:04 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static const char *const sticker_sql[] = {
|
|
|
|
[STICKER_SQL_GET] =
|
|
|
|
"SELECT value FROM sticker WHERE type=? AND uri=? AND name=?",
|
|
|
|
[STICKER_SQL_LIST] =
|
|
|
|
"SELECT name,value FROM sticker WHERE type=? AND uri=?",
|
|
|
|
[STICKER_SQL_UPDATE] =
|
|
|
|
"UPDATE sticker SET value=? WHERE type=? AND uri=? AND name=?",
|
|
|
|
[STICKER_SQL_INSERT] =
|
|
|
|
"INSERT INTO sticker(type,uri,name,value) VALUES(?, ?, ?, ?)",
|
|
|
|
[STICKER_SQL_DELETE] =
|
|
|
|
"DELETE FROM sticker WHERE type=? AND uri=?",
|
2009-04-28 20:23:27 +02:00
|
|
|
[STICKER_SQL_DELETE_VALUE] =
|
|
|
|
"DELETE FROM sticker WHERE type=? AND uri=? AND name=?",
|
2009-04-01 18:42:06 +02:00
|
|
|
[STICKER_SQL_FIND] =
|
|
|
|
"SELECT uri,value FROM sticker WHERE type=? AND uri LIKE (? || '%') AND name=?",
|
2009-03-27 17:05:04 +01:00
|
|
|
};
|
|
|
|
|
2009-01-19 18:51:57 +01:00
|
|
|
static const char sticker_sql_create[] =
|
|
|
|
"CREATE TABLE IF NOT EXISTS sticker("
|
|
|
|
" type VARCHAR NOT NULL, "
|
|
|
|
" uri VARCHAR NOT NULL, "
|
|
|
|
" name VARCHAR NOT NULL, "
|
|
|
|
" value VARCHAR NOT NULL"
|
|
|
|
");"
|
|
|
|
"CREATE UNIQUE INDEX IF NOT EXISTS"
|
|
|
|
" sticker_value ON sticker(type, uri, name);"
|
|
|
|
"";
|
|
|
|
|
|
|
|
static sqlite3 *sticker_db;
|
2009-03-27 17:05:04 +01:00
|
|
|
static sqlite3_stmt *sticker_stmt[G_N_ELEMENTS(sticker_sql)];
|
2009-01-19 18:51:57 +01:00
|
|
|
|
2009-07-05 08:29:47 +02:00
|
|
|
static GQuark
|
|
|
|
sticker_quark(void)
|
|
|
|
{
|
|
|
|
return g_quark_from_static_string("sticker");
|
|
|
|
}
|
|
|
|
|
2009-01-19 18:51:57 +01:00
|
|
|
static sqlite3_stmt *
|
2009-07-05 08:29:47 +02:00
|
|
|
sticker_prepare(const char *sql, GError **error_r)
|
2009-01-19 18:51:57 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
|
|
|
|
ret = sqlite3_prepare_v2(sticker_db, sql, -1, &stmt, NULL);
|
2009-07-05 08:29:47 +02:00
|
|
|
if (ret != SQLITE_OK) {
|
|
|
|
g_set_error(error_r, sticker_quark(), ret,
|
|
|
|
"sqlite3_prepare_v2() failed: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-01-19 18:51:57 +01:00
|
|
|
|
|
|
|
return stmt;
|
|
|
|
}
|
|
|
|
|
2009-07-05 08:29:47 +02:00
|
|
|
bool
|
|
|
|
sticker_global_init(const char *path, GError **error_r)
|
2009-01-19 18:51:57 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (path == NULL)
|
|
|
|
/* not configured */
|
2009-07-05 08:29:47 +02:00
|
|
|
return true;
|
2009-01-19 18:51:57 +01:00
|
|
|
|
|
|
|
/* open/create the sqlite database */
|
|
|
|
|
|
|
|
ret = sqlite3_open(path, &sticker_db);
|
2009-07-05 08:29:47 +02:00
|
|
|
if (ret != SQLITE_OK) {
|
|
|
|
g_set_error(error_r, sticker_quark(), ret,
|
|
|
|
"Failed to open sqlite database '%s': %s",
|
|
|
|
path, sqlite3_errmsg(sticker_db));
|
|
|
|
return false;
|
|
|
|
}
|
2009-01-19 18:51:57 +01:00
|
|
|
|
|
|
|
/* create the table and index */
|
|
|
|
|
|
|
|
ret = sqlite3_exec(sticker_db, sticker_sql_create, NULL, NULL, NULL);
|
2009-07-05 08:29:47 +02:00
|
|
|
if (ret != SQLITE_OK) {
|
|
|
|
g_set_error(error_r, sticker_quark(), ret,
|
|
|
|
"Failed to create sticker table: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
|
|
|
return false;
|
|
|
|
}
|
2009-01-19 18:51:57 +01:00
|
|
|
|
|
|
|
/* prepare the statements we're going to use */
|
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
for (unsigned i = 0; i < G_N_ELEMENTS(sticker_sql); ++i) {
|
|
|
|
assert(sticker_sql[i] != NULL);
|
2009-01-19 18:51:57 +01:00
|
|
|
|
2009-07-05 08:29:47 +02:00
|
|
|
sticker_stmt[i] = sticker_prepare(sticker_sql[i], error_r);
|
|
|
|
if (sticker_stmt[i] == NULL)
|
|
|
|
return false;
|
2009-03-27 17:05:04 +01:00
|
|
|
}
|
2009-07-05 08:29:47 +02:00
|
|
|
|
|
|
|
return true;
|
2009-01-19 18:51:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sticker_global_finish(void)
|
|
|
|
{
|
|
|
|
if (sticker_db == NULL)
|
|
|
|
/* not configured */
|
|
|
|
return;
|
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
for (unsigned i = 0; i < G_N_ELEMENTS(sticker_stmt); ++i) {
|
|
|
|
assert(sticker_stmt[i] != NULL);
|
|
|
|
|
|
|
|
sqlite3_finalize(sticker_stmt[i]);
|
|
|
|
}
|
|
|
|
|
2009-01-19 18:51:57 +01:00
|
|
|
sqlite3_close(sticker_db);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
sticker_enabled(void)
|
|
|
|
{
|
|
|
|
return sticker_db != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
sticker_load_value(const char *type, const char *uri, const char *name)
|
|
|
|
{
|
2009-03-27 17:05:04 +01:00
|
|
|
sqlite3_stmt *const stmt = sticker_stmt[STICKER_SQL_GET];
|
2009-01-19 18:51:57 +01:00
|
|
|
int ret;
|
|
|
|
char *value;
|
|
|
|
|
|
|
|
assert(sticker_enabled());
|
|
|
|
assert(type != NULL);
|
|
|
|
assert(uri != NULL);
|
|
|
|
assert(name != NULL);
|
|
|
|
|
|
|
|
if (*name == 0)
|
|
|
|
return NULL;
|
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
sqlite3_reset(stmt);
|
2009-01-19 18:51:57 +01:00
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
ret = sqlite3_bind_text(stmt, 1, type, -1, NULL);
|
2009-01-19 18:51:57 +01:00
|
|
|
if (ret != SQLITE_OK) {
|
|
|
|
g_warning("sqlite3_bind_text() failed: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
2009-03-14 14:29:54 +01:00
|
|
|
return NULL;
|
2009-01-19 18:51:57 +01:00
|
|
|
}
|
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
ret = sqlite3_bind_text(stmt, 2, uri, -1, NULL);
|
2009-01-19 18:51:57 +01:00
|
|
|
if (ret != SQLITE_OK) {
|
|
|
|
g_warning("sqlite3_bind_text() failed: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
2009-03-14 14:29:54 +01:00
|
|
|
return NULL;
|
2009-01-19 18:51:57 +01:00
|
|
|
}
|
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
ret = sqlite3_bind_text(stmt, 3, name, -1, NULL);
|
2009-01-19 18:51:57 +01:00
|
|
|
if (ret != SQLITE_OK) {
|
|
|
|
g_warning("sqlite3_bind_text() failed: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
2009-03-14 14:29:54 +01:00
|
|
|
return NULL;
|
2009-01-19 18:51:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2009-03-27 17:05:04 +01:00
|
|
|
ret = sqlite3_step(stmt);
|
2009-01-19 18:51:57 +01:00
|
|
|
} while (ret == SQLITE_BUSY);
|
|
|
|
|
|
|
|
if (ret == SQLITE_ROW) {
|
|
|
|
/* record found */
|
2009-03-27 17:05:04 +01:00
|
|
|
value = g_strdup((const char*)sqlite3_column_text(stmt, 0));
|
2009-01-19 18:51:57 +01:00
|
|
|
} else if (ret == SQLITE_DONE) {
|
|
|
|
/* no record found */
|
|
|
|
value = NULL;
|
|
|
|
} else {
|
|
|
|
/* error */
|
|
|
|
g_warning("sqlite3_step() failed: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
2009-03-14 14:29:54 +01:00
|
|
|
return NULL;
|
2009-01-19 18:51:57 +01:00
|
|
|
}
|
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
sqlite3_reset(stmt);
|
|
|
|
sqlite3_clear_bindings(stmt);
|
2009-01-19 18:51:57 +01:00
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2009-03-14 14:21:11 +01:00
|
|
|
static bool
|
|
|
|
sticker_list_values(GHashTable *hash, const char *type, const char *uri)
|
2009-03-03 07:49:23 +01:00
|
|
|
{
|
2009-03-27 17:05:04 +01:00
|
|
|
sqlite3_stmt *const stmt = sticker_stmt[STICKER_SQL_LIST];
|
2009-03-03 07:49:23 +01:00
|
|
|
int ret;
|
|
|
|
char *name, *value;
|
|
|
|
|
2009-03-14 14:21:11 +01:00
|
|
|
assert(hash != NULL);
|
2009-03-03 07:49:23 +01:00
|
|
|
assert(type != NULL);
|
|
|
|
assert(uri != NULL);
|
|
|
|
assert(sticker_enabled());
|
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
sqlite3_reset(stmt);
|
2009-03-03 07:49:23 +01:00
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
ret = sqlite3_bind_text(stmt, 1, type, -1, NULL);
|
2009-03-03 07:49:23 +01:00
|
|
|
if (ret != SQLITE_OK) {
|
|
|
|
g_warning("sqlite3_bind_text() failed: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
2009-03-14 14:21:11 +01:00
|
|
|
return false;
|
2009-03-03 07:49:23 +01:00
|
|
|
}
|
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
ret = sqlite3_bind_text(stmt, 2, uri, -1, NULL);
|
2009-03-03 07:49:23 +01:00
|
|
|
if (ret != SQLITE_OK) {
|
|
|
|
g_warning("sqlite3_bind_text() failed: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
2009-03-14 14:21:11 +01:00
|
|
|
return false;
|
2009-03-03 07:49:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2009-03-27 17:05:04 +01:00
|
|
|
ret = sqlite3_step(stmt);
|
2009-03-03 07:49:23 +01:00
|
|
|
switch (ret) {
|
|
|
|
case SQLITE_ROW:
|
2009-03-27 17:05:04 +01:00
|
|
|
name = g_strdup((const char*)sqlite3_column_text(stmt, 0));
|
|
|
|
value = g_strdup((const char*)sqlite3_column_text(stmt, 1));
|
2009-03-12 00:03:01 +01:00
|
|
|
g_hash_table_insert(hash, name, value);
|
2009-03-03 07:49:23 +01:00
|
|
|
break;
|
|
|
|
case SQLITE_DONE:
|
|
|
|
break;
|
|
|
|
case SQLITE_BUSY:
|
|
|
|
/* no op */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_warning("sqlite3_step() failed: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
2009-03-14 14:21:11 +01:00
|
|
|
return false;
|
2009-03-03 07:49:23 +01:00
|
|
|
}
|
|
|
|
} while (ret != SQLITE_DONE);
|
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
sqlite3_reset(stmt);
|
|
|
|
sqlite3_clear_bindings(stmt);
|
2009-03-03 07:49:23 +01:00
|
|
|
|
2009-03-14 14:21:11 +01:00
|
|
|
return true;
|
2009-03-03 07:49:23 +01:00
|
|
|
}
|
|
|
|
|
2009-01-19 18:51:57 +01:00
|
|
|
static bool
|
|
|
|
sticker_update_value(const char *type, const char *uri,
|
|
|
|
const char *name, const char *value)
|
|
|
|
{
|
2009-03-27 17:05:04 +01:00
|
|
|
sqlite3_stmt *const stmt = sticker_stmt[STICKER_SQL_UPDATE];
|
2009-01-19 18:51:57 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
assert(type != NULL);
|
|
|
|
assert(uri != NULL);
|
|
|
|
assert(name != NULL);
|
|
|
|
assert(*name != 0);
|
|
|
|
assert(value != NULL);
|
|
|
|
|
|
|
|
assert(sticker_enabled());
|
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
sqlite3_reset(stmt);
|
2009-01-19 18:51:57 +01:00
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
ret = sqlite3_bind_text(stmt, 1, value, -1, NULL);
|
2009-01-19 18:51:57 +01:00
|
|
|
if (ret != SQLITE_OK) {
|
|
|
|
g_warning("sqlite3_bind_text() failed: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
ret = sqlite3_bind_text(stmt, 2, type, -1, NULL);
|
2009-01-19 18:51:57 +01:00
|
|
|
if (ret != SQLITE_OK) {
|
|
|
|
g_warning("sqlite3_bind_text() failed: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
ret = sqlite3_bind_text(stmt, 3, uri, -1, NULL);
|
2009-01-19 18:51:57 +01:00
|
|
|
if (ret != SQLITE_OK) {
|
|
|
|
g_warning("sqlite3_bind_text() failed: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
ret = sqlite3_bind_text(stmt, 4, name, -1, NULL);
|
2009-01-19 18:51:57 +01:00
|
|
|
if (ret != SQLITE_OK) {
|
|
|
|
g_warning("sqlite3_bind_text() failed: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2009-03-27 17:05:04 +01:00
|
|
|
ret = sqlite3_step(stmt);
|
2009-01-19 18:51:57 +01:00
|
|
|
} while (ret == SQLITE_BUSY);
|
|
|
|
|
|
|
|
if (ret != SQLITE_DONE) {
|
|
|
|
g_warning("sqlite3_step() failed: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = sqlite3_changes(sticker_db);
|
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
sqlite3_reset(stmt);
|
|
|
|
sqlite3_clear_bindings(stmt);
|
2009-01-19 18:51:57 +01:00
|
|
|
|
2009-01-25 16:23:02 +01:00
|
|
|
idle_add(IDLE_STICKER);
|
2009-01-19 18:51:57 +01:00
|
|
|
return ret > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
sticker_insert_value(const char *type, const char *uri,
|
|
|
|
const char *name, const char *value)
|
|
|
|
{
|
2009-03-27 17:05:04 +01:00
|
|
|
sqlite3_stmt *const stmt = sticker_stmt[STICKER_SQL_INSERT];
|
2009-01-19 18:51:57 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
assert(type != NULL);
|
|
|
|
assert(uri != NULL);
|
|
|
|
assert(name != NULL);
|
|
|
|
assert(*name != 0);
|
|
|
|
assert(value != NULL);
|
|
|
|
|
|
|
|
assert(sticker_enabled());
|
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
sqlite3_reset(stmt);
|
2009-01-19 18:51:57 +01:00
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
ret = sqlite3_bind_text(stmt, 1, type, -1, NULL);
|
2009-01-19 18:51:57 +01:00
|
|
|
if (ret != SQLITE_OK) {
|
|
|
|
g_warning("sqlite3_bind_text() failed: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
ret = sqlite3_bind_text(stmt, 2, uri, -1, NULL);
|
2009-01-19 18:51:57 +01:00
|
|
|
if (ret != SQLITE_OK) {
|
|
|
|
g_warning("sqlite3_bind_text() failed: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
ret = sqlite3_bind_text(stmt, 3, name, -1, NULL);
|
2009-01-19 18:51:57 +01:00
|
|
|
if (ret != SQLITE_OK) {
|
|
|
|
g_warning("sqlite3_bind_text() failed: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
ret = sqlite3_bind_text(stmt, 4, value, -1, NULL);
|
2009-01-19 18:51:57 +01:00
|
|
|
if (ret != SQLITE_OK) {
|
|
|
|
g_warning("sqlite3_bind_text() failed: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2009-03-27 17:05:04 +01:00
|
|
|
ret = sqlite3_step(stmt);
|
2009-01-19 18:51:57 +01:00
|
|
|
} while (ret == SQLITE_BUSY);
|
|
|
|
|
|
|
|
if (ret != SQLITE_DONE) {
|
|
|
|
g_warning("sqlite3_step() failed: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
sqlite3_reset(stmt);
|
|
|
|
sqlite3_clear_bindings(stmt);
|
2009-01-19 18:51:57 +01:00
|
|
|
|
2009-01-25 16:23:02 +01:00
|
|
|
|
|
|
|
idle_add(IDLE_STICKER);
|
2009-01-19 18:51:57 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
sticker_store_value(const char *type, const char *uri,
|
|
|
|
const char *name, const char *value)
|
|
|
|
{
|
|
|
|
assert(sticker_enabled());
|
|
|
|
assert(type != NULL);
|
|
|
|
assert(uri != NULL);
|
|
|
|
assert(name != NULL);
|
|
|
|
assert(value != NULL);
|
|
|
|
|
|
|
|
if (*name == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return sticker_update_value(type, uri, name, value) ||
|
|
|
|
sticker_insert_value(type, uri, name, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
sticker_delete(const char *type, const char *uri)
|
|
|
|
{
|
2009-03-27 17:05:04 +01:00
|
|
|
sqlite3_stmt *const stmt = sticker_stmt[STICKER_SQL_DELETE];
|
2009-01-19 18:51:57 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
assert(sticker_enabled());
|
|
|
|
assert(type != NULL);
|
|
|
|
assert(uri != NULL);
|
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
sqlite3_reset(stmt);
|
2009-01-19 18:51:57 +01:00
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
ret = sqlite3_bind_text(stmt, 1, type, -1, NULL);
|
2009-01-19 18:51:57 +01:00
|
|
|
if (ret != SQLITE_OK) {
|
|
|
|
g_warning("sqlite3_bind_text() failed: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
ret = sqlite3_bind_text(stmt, 2, uri, -1, NULL);
|
2009-01-19 18:51:57 +01:00
|
|
|
if (ret != SQLITE_OK) {
|
|
|
|
g_warning("sqlite3_bind_text() failed: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2009-03-27 17:05:04 +01:00
|
|
|
ret = sqlite3_step(stmt);
|
2009-01-19 18:51:57 +01:00
|
|
|
} while (ret == SQLITE_BUSY);
|
|
|
|
|
|
|
|
if (ret != SQLITE_DONE) {
|
|
|
|
g_warning("sqlite3_step() failed: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
sqlite3_reset(stmt);
|
|
|
|
sqlite3_clear_bindings(stmt);
|
2009-01-19 18:51:57 +01:00
|
|
|
|
2009-01-25 16:23:02 +01:00
|
|
|
idle_add(IDLE_STICKER);
|
2009-01-19 18:51:57 +01:00
|
|
|
return true;
|
|
|
|
}
|
2009-03-14 14:20:01 +01:00
|
|
|
|
2009-04-28 20:23:27 +02:00
|
|
|
bool
|
|
|
|
sticker_delete_value(const char *type, const char *uri, const char *name)
|
|
|
|
{
|
|
|
|
sqlite3_stmt *const stmt = sticker_stmt[STICKER_SQL_DELETE_VALUE];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
assert(sticker_enabled());
|
|
|
|
assert(type != NULL);
|
|
|
|
assert(uri != NULL);
|
|
|
|
|
|
|
|
sqlite3_reset(stmt);
|
|
|
|
|
|
|
|
ret = sqlite3_bind_text(stmt, 1, type, -1, NULL);
|
|
|
|
if (ret != SQLITE_OK) {
|
|
|
|
g_warning("sqlite3_bind_text() failed: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = sqlite3_bind_text(stmt, 2, uri, -1, NULL);
|
|
|
|
if (ret != SQLITE_OK) {
|
|
|
|
g_warning("sqlite3_bind_text() failed: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = sqlite3_bind_text(stmt, 3, name, -1, NULL);
|
|
|
|
if (ret != SQLITE_OK) {
|
|
|
|
g_warning("sqlite3_bind_text() failed: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
ret = sqlite3_step(stmt);
|
|
|
|
} while (ret == SQLITE_BUSY);
|
|
|
|
|
|
|
|
if (ret != SQLITE_DONE) {
|
|
|
|
g_warning("sqlite3_step() failed: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = sqlite3_changes(sticker_db);
|
|
|
|
|
|
|
|
sqlite3_reset(stmt);
|
|
|
|
sqlite3_clear_bindings(stmt);
|
|
|
|
|
|
|
|
idle_add(IDLE_STICKER);
|
|
|
|
return ret > 0;
|
|
|
|
}
|
|
|
|
|
2009-03-14 14:21:11 +01:00
|
|
|
static struct sticker *
|
|
|
|
sticker_new(void)
|
|
|
|
{
|
|
|
|
struct sticker *sticker = g_new(struct sticker, 1);
|
|
|
|
|
|
|
|
sticker->table = g_hash_table_new_full(g_str_hash, g_str_equal,
|
|
|
|
g_free, g_free);
|
|
|
|
return sticker;
|
|
|
|
}
|
|
|
|
|
2009-03-14 14:20:01 +01:00
|
|
|
void
|
|
|
|
sticker_free(struct sticker *sticker)
|
|
|
|
{
|
|
|
|
assert(sticker != NULL);
|
|
|
|
assert(sticker->table != NULL);
|
|
|
|
|
|
|
|
g_hash_table_destroy(sticker->table);
|
|
|
|
g_free(sticker);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2009-04-01 17:27:59 +02:00
|
|
|
sticker_get_value(const struct sticker *sticker, const char *name)
|
2009-03-14 14:20:01 +01:00
|
|
|
{
|
|
|
|
return g_hash_table_lookup(sticker->table, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sticker_foreach_data {
|
|
|
|
void (*func)(const char *name, const char *value,
|
|
|
|
gpointer user_data);
|
|
|
|
gpointer user_data;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
sticker_foreach_func(gpointer key, gpointer value, gpointer user_data)
|
|
|
|
{
|
|
|
|
struct sticker_foreach_data *data = user_data;
|
|
|
|
|
|
|
|
data->func(key, value, data->user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-04-01 17:27:59 +02:00
|
|
|
sticker_foreach(const struct sticker *sticker,
|
2009-03-14 14:20:01 +01:00
|
|
|
void (*func)(const char *name, const char *value,
|
|
|
|
gpointer user_data),
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
struct sticker_foreach_data data = {
|
|
|
|
.func = func,
|
|
|
|
.user_data = user_data,
|
|
|
|
};
|
|
|
|
|
|
|
|
g_hash_table_foreach(sticker->table, sticker_foreach_func, &data);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sticker *
|
|
|
|
sticker_load(const char *type, const char *uri)
|
|
|
|
{
|
2009-03-14 14:21:11 +01:00
|
|
|
struct sticker *sticker = sticker_new();
|
|
|
|
bool success;
|
|
|
|
|
|
|
|
success = sticker_list_values(sticker->table, type, uri);
|
2011-07-18 12:47:06 +02:00
|
|
|
if (!success) {
|
|
|
|
sticker_free(sticker);
|
2009-03-14 14:21:11 +01:00
|
|
|
return NULL;
|
2011-07-18 12:47:06 +02:00
|
|
|
}
|
2009-03-14 14:20:01 +01:00
|
|
|
|
2009-03-14 14:21:11 +01:00
|
|
|
if (g_hash_table_size(sticker->table) == 0) {
|
|
|
|
/* don't return empty sticker objects */
|
|
|
|
sticker_free(sticker);
|
2009-03-14 14:20:01 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sticker;
|
|
|
|
}
|
2009-04-01 18:42:06 +02:00
|
|
|
|
|
|
|
bool
|
|
|
|
sticker_find(const char *type, const char *base_uri, const char *name,
|
|
|
|
void (*func)(const char *uri, const char *value,
|
|
|
|
gpointer user_data),
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
sqlite3_stmt *const stmt = sticker_stmt[STICKER_SQL_FIND];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
assert(type != NULL);
|
|
|
|
assert(name != NULL);
|
|
|
|
assert(func != NULL);
|
|
|
|
assert(sticker_enabled());
|
|
|
|
|
|
|
|
sqlite3_reset(stmt);
|
|
|
|
|
|
|
|
ret = sqlite3_bind_text(stmt, 1, type, -1, NULL);
|
|
|
|
if (ret != SQLITE_OK) {
|
|
|
|
g_warning("sqlite3_bind_text() failed: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (base_uri == NULL)
|
|
|
|
base_uri = "";
|
|
|
|
|
|
|
|
ret = sqlite3_bind_text(stmt, 2, base_uri, -1, NULL);
|
|
|
|
if (ret != SQLITE_OK) {
|
|
|
|
g_warning("sqlite3_bind_text() failed: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = sqlite3_bind_text(stmt, 3, name, -1, NULL);
|
|
|
|
if (ret != SQLITE_OK) {
|
|
|
|
g_warning("sqlite3_bind_text() failed: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
ret = sqlite3_step(stmt);
|
|
|
|
switch (ret) {
|
|
|
|
case SQLITE_ROW:
|
|
|
|
func((const char*)sqlite3_column_text(stmt, 0),
|
|
|
|
(const char*)sqlite3_column_text(stmt, 1),
|
|
|
|
user_data);
|
|
|
|
break;
|
|
|
|
case SQLITE_DONE:
|
|
|
|
break;
|
|
|
|
case SQLITE_BUSY:
|
|
|
|
/* no op */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_warning("sqlite3_step() failed: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} while (ret != SQLITE_DONE);
|
|
|
|
|
|
|
|
sqlite3_reset(stmt);
|
|
|
|
sqlite3_clear_bindings(stmt);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|