+/**
+ * Copyright (C) 2008-2011 by ProFUSION embedded systems
+ * Copyright (C) 2007 by INdT
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+ * @author Gustavo Sverzut Barbieri <barbieri@profusion.mobi>
+ */
+
#include <lightmediascanner_db.h>
#include "lightmediascanner_db_private.h"
+#include <lightmediascanner_dlna.h>
#include <stdlib.h>
#include <stdio.h>
unsigned int _is_started:1;
};
-static lms_db_image_t *_singleton = NULL;
+static struct lms_db_cache _cache = { };
static int
_db_table_updater_images_0(sqlite3 *db, const char *table, unsigned int current_version, int is_last_run) {
return ret;
}
+static int
+_db_table_updater_images_1(sqlite3 *db, const char *table, unsigned int current_version, int is_last_run)
+{
+ int ret;
+ char *err;
+
+ ret = sqlite3_exec(
+ db, "BEGIN TRANSACTION;"
+ "ALTER TABLE images ADD COLUMN dlna_profile TEXT DEFAULT NULL;"
+ "ALTER TABLE images ADD COLUMN dlna_mime TEXT DEFAULT NULL;"
+ "COMMIT;",
+ NULL, NULL, &err);
+ if (ret != SQLITE_OK) {
+ fprintf(stderr, "ERROR: could add columns to images table: %s\n", err);
+ sqlite3_free(err);
+ }
+
+ return ret;
+}
+
+static int
+_db_table_updater_images_2(sqlite3 *db, const char *table, unsigned int current_version, int is_last_run)
+{
+ int ret;
+ char *err;
+
+ ret = sqlite3_exec(
+ db, "BEGIN TRANSACTION;"
+ "ALTER TABLE images ADD COLUMN container TEXT DEFAULT NULL;"
+ "COMMIT;",
+ NULL, NULL, &err);
+ if (ret != SQLITE_OK) {
+ fprintf(stderr, "ERROR: could add columns to images table: %s\n", err);
+ sqlite3_free(err);
+ }
+
+ return ret;
+}
+
static lms_db_table_updater_t _db_table_updater_images[] = {
- _db_table_updater_images_0
+ _db_table_updater_images_0,
+ _db_table_updater_images_1,
+ _db_table_updater_images_2
};
_db_table_updater_images);
}
+/**
+ * Create image DB access tool.
+ *
+ * Creates or get a reference to tools to access 'images' table in an
+ * optimized and easy way.
+ *
+ * This is usually called from plugin's @b setup() callback with the @p db
+ * got from @c ctxt.
+ *
+ * @param db database connection.
+ *
+ * @return DB access tool handle.
+ * @ingroup LMS_Plugins
+ */
lms_db_image_t *
lms_db_image_new(sqlite3 *db)
{
lms_db_image_t *ldi;
+ void *p;
- if (_singleton) {
- _singleton->_references++;
- return _singleton;
+ if (lms_db_cache_get(&_cache, db, &p) == 0) {
+ ldi = p;
+ ldi->_references++;
+ return ldi;
}
if (!db)
ldi->_references = 1;
ldi->db = db;
+ if (lms_db_cache_add(&_cache, db, ldi) != 0) {
+ lms_db_image_free(ldi);
+ return NULL;
+ }
+
return ldi;
}
+/**
+ * Start image DB access tool.
+ *
+ * Compile SQL statements and other initialization functions.
+ *
+ * This is usually called from plugin's @b start() callback.
+ *
+ * @param ldi handle returned by lms_db_image_new().
+ *
+ * @return On success 0 is returned.
+ * @ingroup LMS_Plugins
+ */
int
lms_db_image_start(lms_db_image_t *ldi)
{
ldi->insert = lms_db_compile_stmt(ldi->db,
"INSERT OR REPLACE INTO images ("
"id, title, artist, date, width, height, orientation, "
- "gps_lat, gps_long, gps_alt) VALUES ("
- "?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
+ "gps_lat, gps_long, gps_alt, dlna_profile, dlna_mime, container) "
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
if (!ldi->insert)
return -2;
return 0;
}
+/**
+ * Free image DB access tool.
+ *
+ * Unreference and possible free resources allocated to access tool.
+ *
+ * This is usually called from plugin's @b finish() callback.
+ *
+ * @param ldi handle returned by lms_db_image_new().
+ *
+ * @return On success 0 is returned.
+ * @ingroup LMS_Plugins
+ */
int
lms_db_image_free(lms_db_image_t *ldi)
{
+ int r;
+
if (!ldi)
return -1;
if (ldi->_references == 0) {
if (ldi->insert)
lms_db_finalize_stmt(ldi->insert, "insert");
+ r = lms_db_cache_del(&_cache, ldi->db, ldi);
free(ldi);
- _singleton = NULL;
- return 0;
+ return r;
}
static int
if (ret != 0)
goto done;
+ ret = lms_db_bind_text(stmt, 11, info->dlna_profile.str,
+ info->dlna_profile.len);
+ if (ret != 0)
+ goto done;
+
+ ret = lms_db_bind_text(stmt, 12, info->dlna_mime.str, info->dlna_mime.len);
+ if (ret != 0)
+ goto done;
+
+ ret = lms_db_bind_text(stmt, 13, info->container.str, info->container.len);
+ if (ret != 0)
+ goto done;
+
r = sqlite3_step(stmt);
if (r != SQLITE_DONE) {
fprintf(stderr, "ERROR: could not insert image info: %s\n",
return ret;
}
+/**
+ * Add image file to DB.
+ *
+ * This is usually called from plugin's @b parse() callback.
+ *
+ * @param ldi handle returned by lms_db_image_new().
+ * @param info image information to store.
+ *
+ * @return On success 0 is returned.
+ * @ingroup LMS_Plugins
+ */
int
lms_db_image_add(lms_db_image_t *ldi, struct lms_image_info *info)
{
+ const struct lms_dlna_image_profile *dlna;
+
if (!ldi)
return -1;
if (!info)
if (info->id < 1)
return -3;
+ if (info->dlna_mime.len == 0 && info->dlna_profile.len == 0) {
+ dlna = lms_dlna_get_image_profile(info);
+ if (dlna) {
+ info->dlna_mime = *dlna->dlna_mime;
+ info->dlna_profile = *dlna->dlna_profile;
+ }
+ }
+
return _db_insert(ldi, info);
}