TuttleOFX
1
|
00001 #ifndef _TUTTLEOFX_HOST_THUMBNAILDISKCACHE_HPP_ 00002 #define _TUTTLEOFX_HOST_THUMBNAILDISKCACHE_HPP_ 00003 00004 #include <tuttle/host/diskCache/DiskCacheTranslator.hpp> 00005 00006 #include <boost/filesystem/path.hpp> 00007 00008 #include <string> 00009 #include <cstddef> 00010 00011 namespace tuttle { 00012 namespace host { 00013 namespace attribute { 00014 class Image; 00015 } 00016 00017 ::boost::shared_ptr<attribute::Image> loadImage( const std::string& imagePath ); 00018 00019 /** 00020 * @brief An helper to cache image thumbnails on your HDD. 00021 */ 00022 class ThumbnailDiskCache 00023 { 00024 public: 00025 static const std::string s_thumbnailExtension; 00026 static const int s_thumbnailMaxSize; 00027 typedef DiskCacheTranslator::KeyType KeyType; 00028 typedef ::boost::shared_ptr<attribute::Image> TImage; 00029 00030 public: 00031 ThumbnailDiskCache() 00032 {} 00033 00034 /** 00035 * @brief Set the base directory for all cached files. 00036 */ 00037 void setRootDir( const boost::filesystem::path& rootDir ) { _diskCacheTranslator.setRootDir( rootDir ); } 00038 void setRootDir( const std::string& rootDir ) { setRootDir( boost::filesystem::path(rootDir) ); } 00039 00040 /** 00041 * @brief Check if the @p key exists in the cache. 00042 * 00043 * @warning There is no check if the associated thumbnail is up-to-date. 00044 * @see containsUpToDate 00045 */ 00046 bool contains( const KeyType& key ) const 00047 { 00048 return _diskCacheTranslator.contains( key ); 00049 } 00050 00051 /** 00052 * @brief Check if the @p imagePath has a thumbnail in the cache. 00053 */ 00054 bool containsUpToDate( const boost::filesystem::path& imagePath ) const; 00055 00056 /** 00057 * @brief Retrieve an existing image directly from the cache without any check. 00058 * Throw an error if the key doesn't exist. 00059 * 00060 * @param[in] key cache key 00061 */ 00062 TImage retrieveThumbnail( const KeyType key ) const; 00063 00064 inline TImage retrieveThumbnail( const boost::filesystem::path& imagePath ) const 00065 { 00066 return retrieveThumbnail( buildKey( imagePath ) ); 00067 } 00068 00069 /** 00070 * @brief Create a thumbnail for an image path and the associated key. 00071 * 00072 * @param[out] key cache key 00073 * @param[in] imagePath path to the full image 00074 * @return thumbnail thumbnail generated from the @p imagePath file. 00075 */ 00076 TImage create( KeyType& key, const boost::filesystem::path& imagePath ); 00077 00078 /** 00079 * @brief Tranform the original image path into a cache key. 00080 * @param[in] imagePath The path of an image file 00081 */ 00082 KeyType buildKey( const boost::filesystem::path& imagePath ) const; 00083 00084 /** 00085 * @brief Get a thumbnail from an image filepath. If not in the cache, 00086 * creates and adds it into the cache. 00087 * 00088 * @param[out] key 00089 * @param[in] imagePath 00090 */ 00091 TImage getThumbnail( KeyType& key, const boost::filesystem::path& imagePath ); 00092 00093 TImage getThumbnail( const boost::filesystem::path& imagePath ) { KeyType ignoreKey; return getThumbnail(ignoreKey, imagePath); } 00094 00095 TImage getThumbnail( const std::string& imagePath ) { return getThumbnail(boost::filesystem::path(imagePath)); } 00096 00097 private: 00098 DiskCacheTranslator _diskCacheTranslator; 00099 }; 00100 00101 } 00102 } 00103 00104 #endif 00105