[PATCH 1/1] bookmarks: Fix saving/loading content from database



Saving and restoring content from database were no in sync: some fields
are not saving, and others are restored in wrong position.

This patch fixes this problem.

Signed-off-by: Juan A. Suarez Romero <jasuarez igalia com>
---
 src/bookmarks/grl-bookmarks.c |   62 +++++++++++++++++++++++------------------
 1 files changed, 35 insertions(+), 27 deletions(-)

diff --git a/src/bookmarks/grl-bookmarks.c b/src/bookmarks/grl-bookmarks.c
index ac18ea5..8630769 100644
--- a/src/bookmarks/grl-bookmarks.c
+++ b/src/bookmarks/grl-bookmarks.c
@@ -47,16 +47,17 @@ GRL_LOG_DOMAIN_STATIC(bookmarks_log_domain);
 
 #define GRL_SQL_DB        ".grl-bookmarks"
 
-#define GRL_SQL_CREATE_TABLE_BOOKMARKS			 \
-  "CREATE TABLE IF NOT EXISTS bookmarks ("		 \
-  "id     INTEGER PRIMARY KEY AUTOINCREMENT,"		 \
-  "parent INTEGER REFERENCES bookmarks (id),"		 \
-  "type   INTEGER,"					 \
-  "url    TEXT,"					 \
-  "title  TEXT,"					 \
-  "date   TEXT,"					 \
-  "mime   TEXT,"					 \
-  "desc   TEXT)"
+#define GRL_SQL_CREATE_TABLE_BOOKMARKS                  \
+  "CREATE TABLE IF NOT EXISTS bookmarks ("              \
+  "id         INTEGER PRIMARY KEY AUTOINCREMENT,"       \
+  "parent     INTEGER REFERENCES bookmarks (id),"       \
+  "type       INTEGER,"                                 \
+  "url        TEXT,"                                    \
+  "title      TEXT,"                                    \
+  "date       TEXT,"                                    \
+  "mime       TEXT,"                                    \
+  "desc       TEXT,"                                    \
+  "childcount INTEGER)"
 
 #define GRL_SQL_GET_BOOKMARKS_BY_PARENT			\
   "SELECT b1.*, count(b2.parent <> '') "		\
@@ -74,10 +75,10 @@ GRL_LOG_DOMAIN_STATIC(bookmarks_log_domain);
   "GROUP BY b1.id "					\
   "LIMIT 1"
 
-#define GRL_SQL_STORE_BOOKMARK				  \
-  "INSERT INTO bookmarks "				  \
-  "(parent, type, title, url, date, desc, mime) "	  \
-  "VALUES (?, ?, ?, ?, ?, ?, ?)"
+#define GRL_SQL_STORE_BOOKMARK                                    \
+  "INSERT INTO bookmarks "                                        \
+  "(parent, type, url, title, date, mime, desc, childcount) "	  \
+  "VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
 
 #define GRL_SQL_REMOVE_BOOKMARK			\
   "DELETE FROM bookmarks "			\
@@ -134,7 +135,7 @@ enum {
   BOOKMARK_DATE,
   BOOKMARK_MIME,
   BOOKMARK_DESC,
-  BOOKMARK_LAST
+  BOOKMARK_CHILDCOUNT
 };
 
 struct _GrlBookmarksPrivate {
@@ -334,7 +335,7 @@ build_media_from_stmt (GrlMedia *content, sqlite3_stmt *sql_stmt)
   date = (gchar *) sqlite3_column_text (sql_stmt, BOOKMARK_DATE);
   mime = (gchar *) sqlite3_column_text (sql_stmt, BOOKMARK_MIME);
   type = (guint) sqlite3_column_int (sql_stmt, BOOKMARK_TYPE);
-  childcount = (guint) sqlite3_column_int (sql_stmt, BOOKMARK_LAST);
+  childcount = (guint) sqlite3_column_int (sql_stmt, BOOKMARK_CHILDCOUNT);
 
   if (!media) {
     if (type == BOOKMARK_TYPE_CATEGORY) {
@@ -568,6 +569,7 @@ store_bookmark (sqlite3 *db,
   gchar *date;
   guint type;
   gchar *id;
+  gint childcount;
 
   GRL_DEBUG ("store_bookmark");
 
@@ -575,6 +577,11 @@ store_bookmark (sqlite3 *db,
   url = grl_media_get_url (bookmark);
   desc = grl_media_get_description (bookmark);
   mime = grl_media_get_mime (bookmark);
+  if (GRL_IS_MEDIA_BOX (bookmark)) {
+    childcount = grl_media_box_get_childcount (GRL_MEDIA_BOX (bookmark));
+  } else {
+    childcount = 0;
+  }
   g_get_current_time (&now);
   date = g_time_val_to_iso8601 (&now);
 
@@ -609,29 +616,30 @@ store_bookmark (sqlite3 *db,
     type = BOOKMARK_TYPE_STREAM;
   }
 
-  sqlite3_bind_text (sql_stmt, 1, parent_id, -1, SQLITE_STATIC);
-  sqlite3_bind_int (sql_stmt, 2, type);
-  sqlite3_bind_text (sql_stmt, 3, title, -1, SQLITE_STATIC);
+  sqlite3_bind_text (sql_stmt, BOOKMARK_PARENT, parent_id, -1, SQLITE_STATIC);
+  sqlite3_bind_int (sql_stmt, BOOKMARK_TYPE, type);
+  sqlite3_bind_text (sql_stmt, BOOKMARK_TITLE, title, -1, SQLITE_STATIC);
   if (type == BOOKMARK_TYPE_STREAM) {
-    sqlite3_bind_text (sql_stmt, 4, url, -1, SQLITE_STATIC);
+    sqlite3_bind_text (sql_stmt, BOOKMARK_URL, url, -1, SQLITE_STATIC);
   } else {
-    sqlite3_bind_null (sql_stmt, 4);
+    sqlite3_bind_null (sql_stmt, BOOKMARK_URL);
   }
   if (date) {
-    sqlite3_bind_text (sql_stmt, 5, date, -1, SQLITE_STATIC);
+    sqlite3_bind_text (sql_stmt, BOOKMARK_DATE, date, -1, SQLITE_STATIC);
   } else {
-    sqlite3_bind_null (sql_stmt, 5);
+    sqlite3_bind_null (sql_stmt, BOOKMARK_DATE);
   }
   if (mime) {
-    sqlite3_bind_text (sql_stmt, 6, mime, -1, SQLITE_STATIC);
+    sqlite3_bind_text (sql_stmt, BOOKMARK_MIME, mime, -1, SQLITE_STATIC);
   } else {
-    sqlite3_bind_null (sql_stmt, 6);
+    sqlite3_bind_null (sql_stmt, BOOKMARK_MIME);
   }
   if (desc) {
-    sqlite3_bind_text (sql_stmt, 7, desc, -1, SQLITE_STATIC);
+    sqlite3_bind_text (sql_stmt, BOOKMARK_DESC, desc, -1, SQLITE_STATIC);
   } else {
-    sqlite3_bind_null (sql_stmt, 7);
+    sqlite3_bind_null (sql_stmt, BOOKMARK_DESC);
   }
+  sqlite3_bind_int (sql_stmt, BOOKMARK_CHILDCOUNT, childcount);
 
   while ((r = sqlite3_step (sql_stmt)) == SQLITE_BUSY);
 
-- 
1.7.3.5



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]