From cad202f62e331e7776e3b0f566e4d6082638f4e0 Mon Sep 17 00:00:00 2001 From: Santanu Sinha Date: Thu, 28 May 2009 18:45:13 +0530 Subject: [PATCH] Directory Storage improvement + No copy support for directory storage + Thumbnail, DB and photo store creation at proper places --- src/application/application.cpp | 12 +++- src/application/engine.cpp | 3 +- src/importer/importer-dialog.h | 6 ++ src/importer/importer.cpp | 11 +++ src/storage/Makefile.am | 3 +- src/storage/directory-storage.cpp | 135 +++++++++++++++++++++++++------------ src/storage/directory-storage.h | 20 +++++- src/storage/token-replacer.h | 92 +++++++++++++++++++++++++ 8 files changed, 232 insertions(+), 50 deletions(-) create mode 100644 src/storage/token-replacer.h diff --git a/src/application/application.cpp b/src/application/application.cpp index cf7dfe3..8b2f59b 100644 --- a/src/application/application.cpp +++ b/src/application/application.cpp @@ -26,6 +26,8 @@ #include #include +#include + #include "application.h" #include "browser-model-column-record.h" #include "browser-renderer.h" @@ -183,10 +185,16 @@ Application::Application(int & argc, char ** & argv) throw() : renderers_(), initEnd_() { - ThumbnailStore thumbnail_store("/tmp/solang/thumbnails"); + Glib::ustring thumbStorePath = Glib::get_user_data_dir() + "/"; + thumbStorePath += PACKAGE_NAME; + thumbStorePath += "/thumbnails/"; + Glib::ustring photoStorePath + = Glib::get_user_special_dir( G_USER_DIRECTORY_PICTURES ); + photoStorePath += "/Solang/"; + ThumbnailStore thumbnail_store( thumbStorePath ); IStoragePtr directory_storage(new DirectoryStorage(thumbnail_store, engine_.get_db(), - "/tmp/solang")); + photoStorePath)); engine_.add_current_storage_system( directory_storage->get_storage_uri_prefix(), directory_storage); diff --git a/src/application/engine.cpp b/src/application/engine.cpp index 9c0f02c..381f2da 100644 --- a/src/application/engine.cpp +++ b/src/application/engine.cpp @@ -53,8 +53,7 @@ Engine::Engine(int & argc, char ** & argv, { //TBD::CREATE { - Glib::ustring dbPath = getenv("HOME"); - dbPath += "/.gnome2/"; + Glib::ustring dbPath = Glib::get_user_data_dir() + "/"; dbPath += PACKAGE; dbPath += "/"; database_.set_path( dbPath ); diff --git a/src/importer/importer-dialog.h b/src/importer/importer-dialog.h index d959c3d..fc906c9 100644 --- a/src/importer/importer-dialog.h +++ b/src/importer/importer-dialog.h @@ -43,6 +43,12 @@ class ImporterDialog : return tagView_; } + inline bool + get_to_copy() + { + return copyCheckButton_.get_active(); + } + protected: virtual bool on_delete_event(GdkEventAny * event); diff --git a/src/importer/importer.cpp b/src/importer/importer.cpp index 604090a..ed989d7 100644 --- a/src/importer/importer.cpp +++ b/src/importer/importer.cpp @@ -22,8 +22,10 @@ #endif // HAVE_CONFIG_H #include +#include #include "application.h" +#include "directory-storage.h" #include "i-photo-source.h" #include "importer-dialog.h" #include "importer.h" @@ -165,6 +167,15 @@ Importer::on_action_photo_import() throw() const IStoragePtr & storage = engine.get_current_storage_system("file"); + bool doNotCopy = !importer_dialog.get_to_copy(); + + const std::tr1::shared_ptr dirStorage// = storage; + = std::tr1::dynamic_pointer_cast( storage ); + if( dirStorage ) + { + dirStorage->set_do_not_copy( doNotCopy ); + } + thread_pool.push( sigc::bind(sigc::mem_fun(engine, &Engine::import), photoSource_, storage, tags)); diff --git a/src/storage/Makefile.am b/src/storage/Makefile.am index d2b219e..6aa6922 100644 --- a/src/storage/Makefile.am +++ b/src/storage/Makefile.am @@ -6,7 +6,8 @@ libstorage_la_SOURCES = \ storage.cpp \ storage.h \ thumbnail-store.cpp \ - thumbnail-store.h + thumbnail-store.h \ + token-replacer.h AM_CPPFLAGS = \ -DPACKAGE_LOCALE_DIR=\"${SOLANG_LOCALE_DIR}\" \ diff --git a/src/storage/directory-storage.cpp b/src/storage/directory-storage.cpp index 74cc4c4..de63787 100644 --- a/src/storage/directory-storage.cpp +++ b/src/storage/directory-storage.cpp @@ -30,11 +30,23 @@ #include "directory-storage.h" #include "exif-data.h" +#include "token-replacer.h" namespace Solang { +typedef TokenReplacer<3> FormatStringCreator; + +const FormatStringCreator::ReplacementEntry replacementEntries[] = { + { "", "%1" }, + { "", "%2" }, + { "", "%3" } +}; + +FormatStringCreator replacer( replacementEntries ); + + DirectoryStorage::DirectoryStorage( ThumbnailStore &store, const DatabasePtr &db, @@ -42,6 +54,7 @@ DirectoryStorage::DirectoryStorage( :Storage( store, db ), path_( path ) { + set_target_path_format("//"); } DirectoryStorage::~DirectoryStorage() throw() @@ -115,60 +128,77 @@ void DirectoryStorage::save(const PhotoPtr &photo, bool move) throw(Error) } - std::ostringstream sout; - sout<<"/"< destPath - = Gio::File::create_for_path( filePath ); - - if( !destPath->query_exists() ) + filePath = path_ + "/" + subPath; + } + else + { + filePath = (*photo).get_disk_file_path(); + } + + Glib::RefPtr src = Gio::File::create_for_path( + (*photo).get_disk_file_path() ); + if( !doNotCopy_ ) + { + { - -#ifdef GLIBMM_EXCEPTIONS_ENABLED - try - { - destPath->make_directory_with_parents(); - } - catch( Glib::Error &) + Glib::RefPtr destPath + = Gio::File::create_for_path( filePath ); + + if( !destPath->query_exists() ) { - //TBD::Error - } + +#ifdef GLIBMM_EXCEPTIONS_ENABLED + try + { + destPath->make_directory_with_parents(); + } + catch( Glib::Error &) + { + //TBD::Error + } #else - std::auto_ptr error; - destPath->make_directory_with_parents(error); + std::auto_ptr error; + destPath->make_directory_with_parents(error); #endif + } } - } - Glib::RefPtr src = Gio::File::create_for_path( - (*photo).get_disk_file_path() ); - filePath += "/"; - filePath += src->get_basename(); - Glib::RefPtr dest - = Gio::File::create_for_path( filePath ); - Glib::RefPtr canc = Gio::Cancellable::create(); - - //src->copy_async( dest, NULL, canc ); - try - { - if( true == move ) - src->move( dest, Gio::FILE_COPY_OVERWRITE ); - else - src->copy( dest, Gio::FILE_COPY_OVERWRITE ); + filePath += "/"; + filePath += src->get_basename(); + Glib::RefPtr dest + = Gio::File::create_for_path( filePath ); + Glib::RefPtr canc + = Gio::Cancellable::create(); + + //src->copy_async( dest, NULL, canc ); + try + { + if( true == move ) + src->move( dest, Gio::FILE_COPY_OVERWRITE ); + else + src->copy( dest, Gio::FILE_COPY_OVERWRITE ); + } + catch( Glib::Error &e ) + { + std::cerr<<"Error:"<get_basename(); + uri += filePath; photo->set_uri( uri ); photo->set_disk_file_path(filePath); @@ -180,7 +210,7 @@ void DirectoryStorage::save(const PhotoPtr &photo, bool move) throw(Error) //Now generate thumbnails - Glib::ustring tPath = store_.get_path() + sout.str(); + Glib::ustring tPath = store_.get_path() + subPath; tPath += "/"; tPath += src->get_basename() + "-thumb" ; @@ -188,7 +218,8 @@ void DirectoryStorage::save(const PhotoPtr &photo, bool move) throw(Error) thumb.set_path( tPath ); thumb.generate( exifData, *photo ); - tPath += exifData.thumbnailExtension(); + //tPath += exifData.thumbnailExtension(); + tPath += ".jpg"; thumb.set_path( tPath ); photo->set_thumbnail( thumb ); @@ -209,7 +240,7 @@ Glib::ustring DirectoryStorage::retrieve( { //Just retrieve path from uri and send back return (photo.get_disk_file_path().empty() ) - ? (path_+ "/" + photo.get_uri().substr( + ? (photo.get_uri().substr( photo.get_uri().find(":") + 1, photo.get_uri().length() ) ) : photo.get_disk_file_path(); @@ -254,4 +285,24 @@ void DirectoryStorage::final(Application & application) throw(Error) } +void +DirectoryStorage::set_target_path_format( + const Glib::ustring &fmt) throw() +{ + formatString_ = replacer.extract( fmt ); +} + +Glib::ustring +DirectoryStorage::get_target_path_format() const throw() +{ + return formatString_; +} + +void +DirectoryStorage::set_do_not_copy( bool value ) throw() +{ + doNotCopy_ = value; +} + + } // namespace Solang diff --git a/src/storage/directory-storage.h b/src/storage/directory-storage.h index 3acbf00..28f6361 100644 --- a/src/storage/directory-storage.h +++ b/src/storage/directory-storage.h @@ -36,6 +36,8 @@ class DirectoryStorage : { private: Glib::ustring path_; + Glib::ustring formatString_; + bool doNotCopy_; public: DirectoryStorage( @@ -50,15 +52,16 @@ class DirectoryStorage : virtual void save(const PhotoPtr &) throw(Error); virtual void save(const PhotoPtr &, bool) throw(Error); - //The following should return the path of the file in the local disk + //The following should return the path of the file + // in the local disk // If required it will download from the storage // For raw files, the thumbnail will be extracted // and path for the same will be returned virtual Glib::ustring retrieve(const Photo &) throw(Error); virtual void downloadToDisk( - const PhotoList &, - ProgressObserverPtr &observer) throw(Error); + const PhotoList &, + ProgressObserverPtr &observer) throw(Error); virtual void remove(const PhotoPtr &photo) throw(Error); @@ -67,6 +70,17 @@ class DirectoryStorage : virtual std::string get_storage_uri_prefix() throw(); virtual void final(Application & application) throw(Error); + + void + set_target_path_format(const Glib::ustring &) throw(); + + Glib::ustring + get_target_path_format() const throw(); + + void + set_do_not_copy( bool ) throw(); + + protected: }; } // namespace Solang diff --git a/src/storage/token-replacer.h b/src/storage/token-replacer.h new file mode 100644 index 0000000..86918ee --- /dev/null +++ b/src/storage/token-replacer.h @@ -0,0 +1,92 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */ +/* + * Copyright (C) Santanu Sinha 2009 + * + * Solang 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 3 of the License, or + * (at your option) any later version. + * + * Solang 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, see . + */ + +#ifndef SOLANG_TOKEN_REPLACER_H +#define SOLANG_TOKEN_REPLACER_H + +namespace Solang +{ + +template +class TokenReplacer +{ + public: + struct ReplacementEntry + { + Glib::ustring token_; + Glib::ustring replacement_; + }; + public: + + TokenReplacer( + const ReplacementEntry (&replacementEntries)[numEntries_]); + + inline Glib::ustring + extract( const Glib::ustring &str ); + + private: + inline void + replaceString( Glib::ustring &str, + const Glib::ustring &token, + const Glib::ustring &replacement, + Glib::ustring::size_type pos = 0); + + const ReplacementEntry (&replacementEntries_)[ numEntries_ ]; + +}; + +template +TokenReplacer::TokenReplacer( + const ReplacementEntry (&replacementEntries)[numEntries_]) + :replacementEntries_( replacementEntries ) +{ +} + +template +inline Glib::ustring +TokenReplacer::extract( const Glib::ustring &str ) +{ + Glib::ustring tmp = str; + for( size_t i = 0; i < numEntries_; i++ ) + { + const ReplacementEntry &entry = replacementEntries_[ i ]; + replaceString( tmp, entry.token_, entry.replacement_ ); + } + return tmp; +} + +template +inline void +TokenReplacer::replaceString( Glib::ustring &str, + const Glib::ustring &token, + const Glib::ustring &replacement, + Glib::ustring::size_type pos) +{ + size_t spos = str.find(token, pos); + if( spos != Glib::ustring::npos ) + { + str.replace( spos, token.length(), replacement); + replaceString( str, token, replacement, spos ); + } + return; +} + + +} // namespace Solang + +#endif //SOLANG_TOKEN_REPLACER_H -- 1.6.2.1