db layer: call the lms_dlna_* functions
[platform/upstream/lightmediascanner.git] / src / lib / lightmediascanner_db_image.c
index 3f6bcae..87877fe 100644 (file)
@@ -1,25 +1,28 @@
 /**
+ * Copyright (C) 2008-2011 by ProFUSION embedded systems
  * Copyright (C) 2007 by INdT
  *
- * This program is free software; you can redistribute it and/or
+ * 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
- * of the License, or (at your option) any later version.
+ * as published by the Free Software Foundation; either version 2.1 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.
+ * 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 program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * 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 <gustavo.barbieri@openbossa.org>
+ * @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>
 
@@ -30,7 +33,7 @@ struct lms_db_image {
     unsigned int _is_started:1;
 };
 
-static struct lms_db_cache _cache = {0, 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) {
@@ -86,8 +89,49 @@ _db_table_updater_images_0(sqlite3 *db, const char *table, unsigned int current_
     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
 };
 
 
@@ -168,8 +212,8 @@ 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;
 
@@ -262,6 +306,19 @@ _db_insert(lms_db_image_t *ldi, const struct lms_image_info *info)
     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",
@@ -292,6 +349,8 @@ _db_insert(lms_db_image_t *ldi, const struct lms_image_info *info)
 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)
@@ -299,5 +358,13 @@ lms_db_image_add(lms_db_image_t *ldi, struct lms_image_info *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);
 }