From 7310ed2b0c2454d17d393b94da5d92edc8cc5d35 Mon Sep 17 00:00:00 2001 From: Santanu Sinha Date: Mon, 25 May 2009 15:12:30 +0530 Subject: [PATCH] Automatic database creation The database is created in $HOME/.gnome2/solang/solang.db --- src/application/engine.cpp | 10 ++++- src/database/database.cpp | 80 +++++++++++++++++++++++++++++++- src/database/database.h | 6 +++ src/importer/camera-import-widget.cpp | 9 +++- src/importer/camera-source.cpp | 6 ++- 5 files changed, 104 insertions(+), 7 deletions(-) diff --git a/src/application/engine.cpp b/src/application/engine.cpp index 758830f..9c0f02c 100644 --- a/src/application/engine.cpp +++ b/src/application/engine.cpp @@ -49,8 +49,16 @@ Engine::Engine(int & argc, char ** & argv, photos_(), currentStorageSystems_(), currentRenderer_(), - database_("/tmp") + database_("") { + //TBD::CREATE + { + Glib::ustring dbPath = getenv("HOME"); + dbPath += "/.gnome2/"; + dbPath += PACKAGE; + dbPath += "/"; + database_.set_path( dbPath ); + } criterionChanged_.connect( sigc::mem_fun( *this, &Engine::on_criterion_changed)); diff --git a/src/database/database.cpp b/src/database/database.cpp index ff1cc66..c079f91 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -42,7 +42,7 @@ class Comparator } }; -const Glib::ustring Database::DB_NAME="solang.db"; +const Glib::ustring Database::DB_NAME="solang"; Database::Database( const Glib::ustring &path) : path_(path) @@ -83,9 +83,20 @@ void Database::open() throw(Error) dbPath += DB_NAME; gdaClient_ = Gnome::Gda::Client::create(); + bool dbExists = db_file_exists(); + if( !dbExists ) + { + Glib::RefPtr dbFile + = Gio::File::create_for_path( get_path() ); + dbFile->make_directory_with_parents(); + } gdaConnection_ = gdaClient_->open_connection_from_string( "SQLite", dbPath, "", "", - Gnome::Gda::ConnectionOptions(0)); + Gnome::Gda::ConnectionOptions(0)); + if( !dbExists ) + { + create_db( ); + } gdaDict_ = Gnome::Gda::Dict::create(); gdaDict_->set_connection( gdaConnection_ ); gdaDict_->update_dbms_meta_data(); @@ -181,7 +192,6 @@ PhotoList Database::search( { bool first = true; gint32 lastId = -1; - PhotoSearchCriteria::ClubbingOperationType lastOp; for( std::vector::const_iterator it = tmpList.begin(); it != tmpList.end(); it++ ) @@ -335,4 +345,68 @@ Database::get_dates_with_picture_count( const Glib::ustring &sql, } +bool +Database::db_file_exists() const throw() +{ + Glib::ustring dbPath; + dbPath = get_path(); + dbPath += "/"; + dbPath += DB_NAME; + dbPath +=".db"; + + Glib::RefPtr dbFile + = Gio::File::create_for_path( dbPath ); + return dbFile->query_exists() ; +} + +void +Database::create_db() throw(Error) +{ + try + { + //Photos + { + Glib::ustring sql = "CREATE TABLE photos(\ + photoid integer primary key,\ + uri varchar(255),\ + mod_day integer,\ + mod_month integer,\ + mod_year integer,\ + thumbnail varchar(255),\ + thumbnailx int,\ + thumbnaily int,\ + aperture varchar(100),\ + shutter_speed varchar(100),\ + exposure_program varchar(100),\ + iso_speed integer,\ + metering_mode varchar(100),\ + focal_length varchar(100),\ + white_balance varchar(100),\ + focal_length_in_film varchar(100),\ + picture_taken_time varchar(100))"; + gdaConnection_->execute_non_select_command( sql ); + } + //Tags + { + Glib::ustring sql = "CREATE TABLE tags(\ + tagid integer primary key,\ + tag varchar(255),\ + description varchar(255),\ + iconpath varchar(255))"; + gdaConnection_->execute_non_select_command( sql ); + } + //Photo Tags + { + Glib::ustring sql = "CREATE TABLE photo_tags(\ + photoid integer,\ + tagid integer,\ + primary key (photoid, tagid))"; + gdaConnection_->execute_non_select_command( sql ); + } + } + catch(Glib::Error &e) + { + } +} + } //namespace Solang diff --git a/src/database/database.h b/src/database/database.h index 62a554c..c084692 100644 --- a/src/database/database.h +++ b/src/database/database.h @@ -97,6 +97,12 @@ class Database get_dates_with_picture_count( const Glib::ustring &sql, const ProgressObserverPtr &); + bool + db_file_exists() const throw(); + + void + create_db() throw(Error); + // TagList getTags(const PhotoPtr &photo) const throw(Error); // void saveTags(const PhotoPtr &photo) throw(Error); diff --git a/src/importer/camera-import-widget.cpp b/src/importer/camera-import-widget.cpp index 4ee41d3..6b247e3 100644 --- a/src/importer/camera-import-widget.cpp +++ b/src/importer/camera-import-widget.cpp @@ -269,7 +269,14 @@ CameraImportWidget::save_thumbnail( const Glib::ustring &folder, tDir->make_directory_with_parents(); } - Glib::ustring thumbPath = "/tmp/thumb/" + folder + "/"+file; + Glib::ustring thumbPath = "/tmp/thumb/" + folder; + Glib::RefPtr th + = Gio::File::create_for_path( thumbPath ); + if( !th->query_exists() ) + { + th->make_directory_with_parents(); + } + thumbPath += "/"+file; int fd = open( thumbPath.c_str(), O_CREAT | O_WRONLY, 0644 ); if( gp_file_new_from_fd( &thumb, fd ) ) { diff --git a/src/importer/camera-source.cpp b/src/importer/camera-source.cpp index cbc61c8..1a9e26e 100644 --- a/src/importer/camera-source.cpp +++ b/src/importer/camera-source.cpp @@ -123,8 +123,9 @@ CameraSource::import(const IStoragePtr & storage, const TagList &tags, { PhotoList files; create_photo_list( files ); + PhotoList tmpList = import(files, storage, tags, db, observer); gpContext_.cleanup(); //So that this can be recreated - return import(files, storage, tags, db, observer); + return tmpList; } void @@ -198,8 +199,9 @@ CameraSource::download_file_from_camera( const PhotoPtr &photo ) cImg, gpContext_.context_ ) ) { gp_file_unref( cImg ); + return; } - gp_file_free( cImg ); + gp_file_unref( cImg ); photo->set_disk_file_path( cImgPath ); } -- 1.6.2.1