[Rawstudio-commit] r2383 - in branches/rawstudio-library: . src

Anders Kvist anders at kvistmail.dk
Sat Apr 18 01:52:01 CEST 2009


Author: akv
Date: 2009-04-18 01:52:01 +0200 (Sat, 18 Apr 2009)
New Revision: 2383

Added:
   branches/rawstudio-library/src/rs-library.c
   branches/rawstudio-library/src/rs-library.h
Modified:
   branches/rawstudio-library/configure.in
   branches/rawstudio-library/src/Makefile.am
   branches/rawstudio-library/src/application.c
   branches/rawstudio-library/src/application.h
Log:
Initial library code.

Modified: branches/rawstudio-library/configure.in
===================================================================
--- branches/rawstudio-library/configure.in	2009-04-17 23:42:39 UTC (rev 2382)
+++ branches/rawstudio-library/configure.in	2009-04-17 23:52:01 UTC (rev 2383)
@@ -56,7 +56,7 @@
 fi
 AC_SUBST(LIBTIFF)
 
-pkg_modules="gtk+-2.0 >= 2.8.0 libxml-2.0 >= 2.4 gconf-2.0 >= 2.0 lcms dbus-1 exiv2 fftw3f lensfun"
+pkg_modules="gtk+-2.0 >= 2.8.0 libxml-2.0 >= 2.4 gconf-2.0 >= 2.0 lcms dbus-1 exiv2 fftw3f lensfun sqlite3"
 PKG_CHECK_MODULES(PACKAGE, [$pkg_modules])
 AC_SUBST(PACKAGE_CFLAGS)
 AC_SUBST(PACKAGE_LIBS)

Modified: branches/rawstudio-library/src/Makefile.am
===================================================================
--- branches/rawstudio-library/src/Makefile.am	2009-04-17 23:42:39 UTC (rev 2382)
+++ branches/rawstudio-library/src/Makefile.am	2009-04-17 23:52:01 UTC (rev 2383)
@@ -51,7 +51,8 @@
 	rs-pixbuf.c rs-pixbuf.h \
 	rs-external-editor.c rs-external-editor.h \
 	rs-dir-selector.c rs-dir-selector.h \
-	rs-exif.cc rs-exif.h
+	rs-exif.cc rs-exif.h \
+	rs-library.c rs-library.h
 
 rawstudio_LDADD = ../librawstudio/librawstudio- at VERSION@.la @PACKAGE_LIBS@ @LIBJPEG@ @LIBTIFF@ $(INTLLIBS)
 

Modified: branches/rawstudio-library/src/application.c
===================================================================
--- branches/rawstudio-library/src/application.c	2009-04-17 23:42:39 UTC (rev 2382)
+++ branches/rawstudio-library/src/application.c	2009-04-17 23:52:01 UTC (rev 2383)
@@ -40,6 +40,7 @@
 #include "rs-histogram.h"
 #include "rs-photo.h"
 #include "rs-exif.h"
+#include "rs-library.h"
 
 static void photo_settings_changed(RS_PHOTO *photo, RSSettingsMask mask, RS_BLOB *rs);
 static void photo_spatial_changed(RS_PHOTO *photo, RS_BLOB *rs);
@@ -376,6 +377,9 @@
 	for(c=0;c<3;c++)
 		rs->settings[c] = rs_settings_new();
 
+	/* FIXME: should be done with rs_library_new() */
+	rs->library = g_malloc(sizeof(RS_LIBRARY));
+
 	/* Build basic filter chain */
 	rs->filter_input = rs_filter_new("RSInputImage16", NULL);
 	rs->filter_demosaic = rs_filter_new("RSDemosaic", rs->filter_input);
@@ -668,6 +672,8 @@
 
 	rs_stock_init();
 
+	rs_library_init(rs->library);
+
 #if GTK_CHECK_VERSION(2,10,0)
 	gtk_link_button_set_uri_hook(runuri,NULL,NULL);
 #endif

Modified: branches/rawstudio-library/src/application.h
===================================================================
--- branches/rawstudio-library/src/application.h	2009-04-17 23:42:39 UTC (rev 2382)
+++ branches/rawstudio-library/src/application.h	2009-04-17 23:52:01 UTC (rev 2383)
@@ -26,6 +26,7 @@
 #include <stdint.h>
 #include "rs-arch.h"
 #include "rs-cms.h"
+#include "rs-library.h"
 
 /* Check for thread support */
 #if (!defined(G_THREADS_ENABLED) || defined(G_THREADS_IMPL_NONE))
@@ -63,6 +64,7 @@
 	RS_QUEUE *queue;
 	RS_CMS *cms;
 	RSStore *store;
+	RS_LIBRARY *library;
 
 	/* These should be moved to a future RS_WINDOW */
 	GtkWidget *window;

Added: branches/rawstudio-library/src/rs-library.c
===================================================================
--- branches/rawstudio-library/src/rs-library.c	                        (rev 0)
+++ branches/rawstudio-library/src/rs-library.c	2009-04-17 23:52:01 UTC (rev 2383)
@@ -0,0 +1,399 @@
+/*
+ * Copyright (C) 2006-2009 Anders Brander <anders at brander.dk> and 
+ * Anders Kvist <akv at lnxbx.dk>
+ *
+ * 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.
+ *
+ * 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.
+ */
+
+/* Documentation:
+ * http://www.sqlite.org/capi3ref.html
+ */
+
+#include <glib.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sqlite3.h>
+#include <string.h>
+#include "rawstudio.h"
+#include "rs-library.h"
+
+void library_sqlite_error(sqlite3 *db, gint result);
+gint library_create_tables(sqlite3 *db);
+gint library_find_tag_id(RS_LIBRARY *library, gchar *tagname);
+gint library_find_photo_id(RS_LIBRARY *library, gchar *photo);
+void library_photo_add_tag(RS_LIBRARY *library, gint photo_id, gint tag_id);
+gboolean library_is_photo_tagged(RS_LIBRARY *library, gint photo_id, gint tag_id);
+void library_add_photo(RS_LIBRARY *library, gchar *filename);
+void library_add_tag(RS_LIBRARY *library, gchar *tagname);
+void library_delete_photo(RS_LIBRARY *library, gint photo_id);
+void library_delete_tag(RS_LIBRARY *library, gint tag_id);
+void library_photo_delete_tags(RS_LIBRARY *library, gint photo_id);
+void library_tag_delete_photos(RS_LIBRARY *library, gint tag_id);
+gboolean library_tag_is_used(RS_LIBRARY *library, gint tag_id);
+
+/*
+ * TODO:
+ * rs_library_search(RS_LIBRARY *library, GList *tags)
+ */
+
+/* BEGIN PRIVATE FUNCTIONS */
+
+void
+library_sqlite_error(sqlite3 *db, gint result)
+{
+	if (result != SQLITE_OK)
+	{
+		g_warning("sqlite3 warning: %s\n", sqlite3_errmsg(db));
+	}
+}
+
+gint
+library_create_tables(sqlite3 *db)
+{
+	sqlite3_stmt *stmt;
+	gint rc;
+       
+	/* Create table (library) to hold all known photos */
+	sqlite3_prepare_v2(db, "create table library (id integer primary key, filename varchar(1024))", -1, &stmt, NULL);
+	rc = sqlite3_step(stmt);
+	sqlite3_finalize(stmt);
+
+	/* Create table (tags) with all known tags */
+	sqlite3_prepare_v2(db, "create table tags (id integer primary key, tagname varchar(128))", -1, &stmt, NULL);
+	rc = sqlite3_step(stmt);
+	sqlite3_finalize(stmt);
+
+	/* Create table (phototags) to bind tags and photos together */
+	sqlite3_prepare_v2(db, "create table phototags (photo integer, tag integer)", -1, &stmt, NULL);
+	rc = sqlite3_step(stmt);
+	sqlite3_finalize(stmt);
+
+	return SQLITE_OK;
+}
+
+
+gint
+library_find_tag_id(RS_LIBRARY *library, gchar *tagname)
+{
+	sqlite3 *db = library->db;
+	sqlite3_stmt *stmt;
+	gint rc, tag_id = -1;
+
+	rc = sqlite3_prepare_v2(db, "SELECT id FROM tags WHERE tagname = ?1;", -1, &stmt, NULL);
+	rc = sqlite3_bind_text(stmt, 1, tagname, strlen(tagname), SQLITE_TRANSIENT);
+	rc = sqlite3_step(stmt);
+	if (rc == SQLITE_ROW)
+		tag_id = sqlite3_column_int(stmt, 0);
+	rc = sqlite3_finalize(stmt);
+	return tag_id;
+}
+
+gint
+library_find_photo_id(RS_LIBRARY *library, gchar *photo)
+{
+	sqlite3 *db = library->db;
+	sqlite3_stmt *stmt;
+	gint rc, photo_id = -1;
+
+	rc = sqlite3_prepare_v2(db, "SELECT id FROM library WHERE filename = ?1;", -1, &stmt, NULL);
+	rc = sqlite3_bind_text(stmt, 1, photo, strlen(photo), SQLITE_TRANSIENT);
+	library_sqlite_error(db, rc);
+	rc = sqlite3_step(stmt);
+	if (rc == SQLITE_ROW)
+		photo_id = sqlite3_column_int(stmt, 0);
+	rc = sqlite3_finalize(stmt);
+	return photo_id;
+}
+
+void
+library_photo_add_tag(RS_LIBRARY *library, gint photo_id, gint tag_id)
+{
+	sqlite3 *db = library->db;
+	gint rc;
+	sqlite3_stmt *stmt;
+
+	rc = sqlite3_prepare_v2(db, "INSERT INTO phototags (photo, tag) VALUES (?1, ?2);", -1, &stmt, NULL);
+	rc = sqlite3_bind_int (stmt, 1, photo_id);
+	rc = sqlite3_bind_int (stmt, 2, tag_id);
+	rc = sqlite3_step(stmt);
+	if (rc != SQLITE_DONE)
+		library_sqlite_error(db, rc);
+	sqlite3_finalize(stmt);
+}
+
+gboolean
+library_is_photo_tagged(RS_LIBRARY *library, gint photo_id, gint tag_id)
+{
+	sqlite3 *db = library->db;
+	gint rc;
+	sqlite3_stmt *stmt;
+
+	rc = sqlite3_prepare_v2(db, "SELECT * FROM phototags WHERE photo = ?1 AND tag = ?2;", -1, &stmt, NULL);
+	rc = sqlite3_bind_int (stmt, 1, photo_id);
+	rc = sqlite3_bind_int (stmt, 2, tag_id);
+	rc = sqlite3_step(stmt);
+	sqlite3_finalize(stmt);
+
+	if (rc == SQLITE_ROW)
+		return TRUE;
+	else
+		return FALSE;
+}
+
+void
+library_add_photo(RS_LIBRARY *library, gchar *filename)
+{
+	sqlite3 *db = library->db;
+	gint rc;
+	sqlite3_stmt *stmt;
+
+	sqlite3_prepare_v2(db, "INSERT INTO library (filename) VALUES (?1);", -1, &stmt, NULL);
+	rc = sqlite3_bind_text(stmt, 1, filename, strlen(filename), SQLITE_TRANSIENT);
+	rc = sqlite3_step(stmt);
+	if (rc != SQLITE_DONE)
+		library_sqlite_error(db, rc);
+	sqlite3_finalize(stmt);
+}
+
+void
+library_add_tag(RS_LIBRARY *library, gchar *tagname)
+{
+	sqlite3 *db = library->db;
+	gint rc;
+	sqlite3_stmt *stmt;
+
+	sqlite3_prepare_v2(db, "INSERT INTO tags (tagname) VALUES (?1);", -1, &stmt, NULL);
+	rc = sqlite3_bind_text(stmt, 1, tagname, strlen(tagname), SQLITE_TRANSIENT);
+	rc = sqlite3_step(stmt);
+	if (rc != SQLITE_DONE)
+		library_sqlite_error(db, rc);
+	sqlite3_finalize(stmt);
+}
+
+void 
+library_delete_photo(RS_LIBRARY *library, gint photo_id)
+{
+	sqlite3 *db = library->db;
+	sqlite3_stmt *stmt;
+	gint rc;
+
+	rc = sqlite3_prepare_v2(db, "DELETE FROM library WHERE id = ?1;", -1, &stmt, NULL);
+	rc = sqlite3_bind_int(stmt, 1, photo_id);
+	library_sqlite_error(db, rc);
+	rc = sqlite3_step(stmt);
+	if (rc != SQLITE_DONE)
+		library_sqlite_error(db, rc);
+	rc = sqlite3_finalize(stmt);
+}
+
+void 
+library_delete_tag(RS_LIBRARY *library, gint tag_id)
+{
+	sqlite3 *db = library->db;
+	sqlite3_stmt *stmt;
+	gint rc;
+
+	rc = sqlite3_prepare_v2(db, "DELETE FROM library WHERE filename = ?1;", -1, &stmt, NULL);
+	rc = sqlite3_bind_int(stmt, 1, tag_id);
+	library_sqlite_error(db, rc);
+	rc = sqlite3_step(stmt);
+	if (rc != SQLITE_DONE)
+		library_sqlite_error(db, rc);
+	rc = sqlite3_finalize(stmt);
+}
+
+void 
+library_photo_delete_tags(RS_LIBRARY *library, gint photo_id)
+{
+	sqlite3 *db = library->db;
+	sqlite3_stmt *stmt;
+	gint rc;
+
+	rc = sqlite3_prepare_v2(db, "DELETE FROM phototags WHERE photo = ?1;", -1, &stmt, NULL);
+	rc = sqlite3_bind_int(stmt, 1, photo_id);
+	library_sqlite_error(db, rc);
+	rc = sqlite3_step(stmt);
+	if (rc != SQLITE_DONE)
+		library_sqlite_error(db, rc);
+	rc = sqlite3_finalize(stmt);
+}
+
+void
+library_tag_delete_photos(RS_LIBRARY *library, gint tag_id)
+{
+	sqlite3 *db = library->db;
+	sqlite3_stmt *stmt;
+	gint rc;
+
+	rc = sqlite3_prepare_v2(db, "DELETE FROM phototags WHERE tag = ?1;", -1, &stmt, NULL);
+	rc = sqlite3_bind_int(stmt, 1, tag_id);
+	library_sqlite_error(db, rc);
+	rc = sqlite3_step(stmt);
+	if (rc != SQLITE_DONE)
+		library_sqlite_error(db, rc);
+	rc = sqlite3_finalize(stmt);
+}
+
+gboolean
+library_tag_is_used(RS_LIBRARY *library, gint tag_id)
+{
+	sqlite3 *db = library->db;
+	gint rc;
+	sqlite3_stmt *stmt;
+
+	rc = sqlite3_prepare_v2(db, "SELECT * FROM phototags WHERE tag = ?1;", -1, &stmt, NULL);
+	rc = sqlite3_bind_int (stmt, 1, tag_id);
+	rc = sqlite3_step(stmt);
+	sqlite3_finalize(stmt);
+
+	if (rc == SQLITE_ROW)
+		return TRUE;
+	else
+		return FALSE;
+}
+
+/* END PRIVATE FUNCTIONS */
+
+
+/* BEGIN PUBLIC FUNCTIONS */
+
+void
+rs_library_init(RS_LIBRARY *library)
+{
+	int rc;
+
+	gchar *database = g_strdup_printf("%s/.rawstudio/library.db", g_get_home_dir());
+
+	/* If unable to create database we exit */
+	if(sqlite3_open(database, &(library->db)))
+	{
+		g_debug("sqlite3 debug: could not open database %s\n", database);
+		sqlite3_close(library->db);
+		exit(1);
+	}
+	g_free(database);
+
+	rc = library_create_tables(library->db);
+	library_sqlite_error(library->db, rc);
+
+	rs_library_add_photo(library, "/home/akv/test/1.cr2");
+	rs_library_add_photo(library, "/home/akv/test/2.cr2");
+	rs_library_add_photo(library, "/home/akv/test/3.cr2");
+
+	rs_library_add_tag(library, "Testing");
+	rs_library_add_tag(library, "Rawstudio");
+
+	rs_library_photo_add_tag(library, "/home/akv/test/2.cr2", "Rawstudio");
+	rs_library_photo_add_tag(library, "/home/akv/test/3.cr2", "Rawstudio");
+	rs_library_photo_add_tag(library, "/home/akv/test/3.cr2", "Testing");
+
+	rs_library_delete_photo(library, "/home/akv/test/2.cr2");
+
+	rs_library_delete_tag(library, "Testing", TRUE);
+
+	exit(1);
+}
+
+void
+rs_library_add_photo(RS_LIBRARY *library, gchar *filename)
+{
+	if (library_find_photo_id(library, filename) == -1)
+	{
+		g_debug("Adding photo to library: %s",filename);
+		library_add_photo(library, filename);
+	}
+}
+
+void
+rs_library_add_tag(RS_LIBRARY *library, gchar *tagname)
+{
+	if (library_find_tag_id(library, tagname) == -1)
+	{
+		g_debug("Adding tag to tags: %s",tagname);
+		library_add_tag(library, tagname);
+	}
+
+}
+
+void
+rs_library_photo_add_tag(RS_LIBRARY *library, gchar *filename, gchar *tagname)
+{
+	gint photo_id = 0, tag_id;
+
+	photo_id = library_find_photo_id(library, filename);
+	if (photo_id == -1)
+	{
+		g_warning("Photo not known...");
+		return;
+	}
+
+	tag_id = library_find_tag_id(library, tagname);
+	if (tag_id == -1)
+	{
+		g_warning("Tag not known...");
+		return;
+	}
+
+	if (!library_is_photo_tagged(library, photo_id, tag_id))
+		library_photo_add_tag(library, photo_id, tag_id);
+
+	return;
+}
+
+void
+rs_library_delete_photo(RS_LIBRARY *library, gchar *photo)
+{
+	gint photo_id = -1;
+
+	photo_id = library_find_photo_id(library, photo);
+	if (photo_id == -1)
+	{
+		g_warning("Photo not known...");
+		return;
+	}
+
+	library_photo_delete_tags(library, photo_id);
+	library_delete_photo(library, photo_id);
+}
+
+gboolean
+rs_library_delete_tag(RS_LIBRARY *library, gchar *tag, gboolean force)
+{
+	gint tag_id = -1;
+
+	tag_id = library_find_tag_id(library, tag);
+	if (tag_id == -1)
+	{
+		g_warning("Tag not known...");
+		return FALSE;
+	}
+
+	if (library_tag_is_used(library, tag_id))
+		if (force)
+		{
+			library_tag_delete_photos(library, tag_id);
+			library_delete_tag(library, tag_id);
+		}
+		else
+		{
+			g_warning("Tag is in use...");
+			return FALSE;
+		}
+	else
+		library_delete_tag(library, tag_id);
+	return TRUE;
+}
+
+/* END PUBLIC FUNCTIONS */

Added: branches/rawstudio-library/src/rs-library.h
===================================================================
--- branches/rawstudio-library/src/rs-library.h	                        (rev 0)
+++ branches/rawstudio-library/src/rs-library.h	2009-04-17 23:52:01 UTC (rev 2383)
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2006-2009 Anders Brander <anders at brander.dk> and 
+ * Anders Kvist <akv at lnxbx.dk>
+ *
+ * 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.
+ *
+ * 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.
+ */
+
+#ifndef RS_LIBRARY_H
+#define RS_LIBRARY_H
+
+#include <glib.h>
+#include <rawstudio.h>
+#include "sqlite3.h"
+
+typedef struct
+{
+	sqlite3 *db;
+} RS_LIBRARY;
+
+void rs_library_init(RS_LIBRARY *library);
+void rs_library_add_photo(RS_LIBRARY *library, gchar *filename);
+void rs_library_add_tag(RS_LIBRARY *library, gchar *tagname);
+void rs_library_photo_add_tag(RS_LIBRARY *library, gchar *filename, gchar *tagname);
+void rs_library_delete_photo(RS_LIBRARY *library, gchar *photo);
+gboolean rs_library_delete_tag(RS_LIBRARY *library, gchar *tag, gboolean force);
+
+#endif /* RS_LIBRARY_H */




More information about the Rawstudio-commit mailing list