00001 00002 // Name: imagecache.h 00003 // Purpose: A class for saving thumbnails for visited directories 00004 // Author: Julian Smart 00005 // Modified by: 00006 // Created: 2006-05-11 00007 // RCS-ID: $$ 00008 // Copyright: (c) Julian Smart 00009 // Licence: New BSD License 00011 00012 #ifndef _WX_IMAGECACHE_H_ 00013 #define _WX_IMAGECACHE_H_ 00014 00015 #include "wx/defs.h" 00016 #include "wx/image.h" 00017 #include "imageutils/imageblock.h" 00018 00019 /* 00020 00021 1. The app creates a wxImageCache that persists 00022 for the lifetime of the app. Sets the cache folder, 00023 and loads the index. 00024 00025 2. Showing an image browser: 00026 00027 i) when a folder is about to be shown, instead of reading 00028 the images immediately, try to load the cache for this folder. 00029 Load the cache index if it's been modified since last load. 00030 ii) If found, load cache and throw away any out of date 00031 items or items with the wrong size. 00032 iii) in idle time or timer callback, check each image file in folder. 00033 If in cache, display cached bitmap. If not, load image and 00034 add thumbnail to cache and display. 00035 iv) When scanning has finished (user intervention or no more files), 00036 if there are items in the cache, save the cache, and index in case 00037 of a crash, get file modification date so we know not to load it immediately. 00038 00039 00040 */ 00041 00042 class WXDLLIMPEXP_BASE wxDataInputStream; 00043 class WXDLLIMPEXP_BASE wxDataOutputStream; 00044 00045 // In future we might want to store other kinds of item 00046 00047 #define wxIMAGE_CACHE_TYPE_IMAGE 1 00048 00049 // File format version 00050 #define wxIMAGE_CACHE_FILE_VERSION 1 00051 00052 /* 00053 * wxImageCacheItem: an item in a folder cache 00054 */ 00055 00056 class wxImageCacheItem: public wxObject 00057 { 00058 public: 00059 wxImageCacheItem() { Init(); } 00060 00061 void Init() {}; 00062 00063 void MakeFromImage(const wxImage& image, const wxString& filename, const wxSize& sz = wxDefaultSize); 00064 00065 wxImageBlock m_imageBlock; 00066 wxString m_filename; 00067 wxDateTime m_lastUpdated; 00068 wxSize m_size; 00069 }; 00070 00071 /* 00072 * wxImageCache: a class for maintaining a cache of folders visited by 00073 * an image browser 00074 */ 00075 00076 class wxImageCache: public wxObject 00077 { 00078 DECLARE_CLASS(wxImageCache) 00079 public: 00080 wxImageCache() { Init(); } 00081 ~wxImageCache() { Clear(); } 00082 00084 void Init(); 00085 00087 void Clear(); 00088 00090 void ClearCache(); 00091 00093 void ClearIndex(); 00094 00096 void ClearAllCacheFiles(); 00097 00101 bool LoadFolderCache(const wxString& folder, bool force = false); 00102 00104 bool LoadCacheIndex(bool force = false); 00105 00107 bool SaveFolderCache(); 00108 00110 bool SaveCacheIndex(); 00111 00113 bool AddCacheItem(wxImageCacheItem* item); 00114 00116 bool AddCacheItem(const wxImage& image, const wxString& filename, const wxSize& sz); 00117 00120 bool NewCache(const wxString& folder); 00121 00123 bool UpdateCache(); 00124 00127 bool PurgeCache(const wxSize& sz = wxDefaultSize); 00128 00130 wxImageCacheItem* FindCacheItem(const wxString& filename); 00131 00133 void DeleteCacheItem(wxImageCacheItem* item); 00134 00136 00138 void SetCacheStorageFolder(const wxString& folder) { m_cacheStorageFolder = folder; } 00139 00141 const wxString& GetCacheStorageFolder() const { return m_cacheStorageFolder; } 00142 00144 void SetCacheStorageFolderFromTemp(); 00145 00147 void SetCurrentCacheFolder(const wxString& folder); 00148 00150 const wxString& GetCurrentCacheFolder() const { return m_currentFolder; } 00151 00153 bool IsCacheLoaded() const { return !m_currentFolder.IsEmpty(); } 00154 00156 void SetModified(bool mod) { m_modified = mod; } 00157 00159 bool IsModified() const { return m_modified; } 00160 00162 void SetIndexModified(bool mod) { m_indexModified = mod; } 00163 00165 void SetIndexName(const wxString& name) { m_indexName = name; } 00166 00168 void Enable(bool enable) { m_enabled = enable; } 00169 00171 bool IsIndexModified() const { return m_indexModified; } 00172 00174 wxString GetIndexFilename() const; 00175 00177 wxString GetIndexName() const { return m_indexName; } 00178 00180 wxString GetCacheFilename() const; 00181 00183 wxString GetFullPath(const wxString& filename) const; 00184 00186 wxString GetFullCachePath(const wxString& filename) const; 00187 00189 bool IsEnabled() const { return m_enabled; } 00190 00192 00194 bool ModifiedMoreRecently(const wxDateTime& date, const wxString& filename); 00195 00197 wxDateTime GetModificationTime(const wxString& filename); 00198 00200 wxString GetTempDir() const; 00201 00203 wxString MakeNewCacheFilename(const wxString& folder) const; 00204 00206 void AddToIndex(const wxString& folder); 00207 00208 protected: 00210 wxString m_cacheStorageFolder; 00211 00213 wxString m_currentFolder; 00214 00216 wxDateTime m_indexLoaded; 00217 00219 wxList m_cacheItems; 00220 00222 wxArrayString m_indexFolders; 00223 00225 wxArrayString m_indexPaths; 00226 00228 int m_maxCachedFolders; 00229 00231 bool m_modified; 00232 00234 bool m_indexModified; 00235 00237 bool m_enabled; 00238 00240 wxString m_indexName; 00241 }; 00242 00243 #endif 00244 // _WX_IMAGECACHE_H_