comparison data/fileio/FileSource.h @ 469:a8a7b8f698c8

* Revert revisions 1244 and 1245 on FileSource -- backing out its built-in persistent cache support, as CachedFile now does the same thing and will do so more properly
author Chris Cannam
date Tue, 28 Oct 2008 11:07:08 +0000
parents 63b8ba45d953
children e340b2fb9471
comparison
equal deleted inserted replaced
468:70b333085952 469:a8a7b8f698c8
49 * construct many FileSource objects with the same URL and you will 49 * construct many FileSource objects with the same URL and you will
50 * receive the same cached file for each; it is also reasonable to 50 * receive the same cached file for each; it is also reasonable to
51 * pass FileSource objects by value. FileSource only makes sense for 51 * pass FileSource objects by value. FileSource only makes sense for
52 * stateless URLs that result in the same data on each request. 52 * stateless URLs that result in the same data on each request.
53 * 53 *
54 * Cached files (in their default temporary mode) share a lifetime 54 * Cached files share a lifetime with their "owning" FileSource
55 * with their "owning" FileSource objects. When the last FileSource 55 * objects; when the last FileSource referring to a given URL is
56 * referring to a given URL is deleted or goes out of scope, its 56 * deleted or goes out of scope, its cached file (if any) is also
57 * cached file (if any) is also removed. You can change this 57 * removed.
58 * behaviour by using persistent cache mode; \see LocalCacheMode for
59 * details and caveats.
60 */ 58 */
61 class FileSource : public QObject 59 class FileSource : public QObject
62 { 60 {
63 Q_OBJECT 61 Q_OBJECT
64 62
65 public: 63 public:
66 /**
67 * Type of local cache to be used when retrieving remote files.
68 *
69 * Temporary cache files are created when a FileSource object is
70 * first created for a given URL, and removed when the last extant
71 * temporary cache mode FileSource object referring to a given URL
72 * is deleted (i.e. when its reference count is lowered to zero).
73 * They are also stored in a temporary directory that will be
74 * deleted when the program exits.
75 *
76 * Persistent cache files are created only when first retrieving a
77 * URL for which no persistent cache already exists, and are never
78 * deleted (by FileSource anyway). They are stored in a directory
79 * that is not deleted when the program exits. FileSource creates
80 * a unique local file name for each source URL, so as long as the
81 * local cache file remains on disc, the remote URL will not be
82 * retrieved again during any further run of the program. You can
83 * find out what local file name will be used for the persistent
84 * cache of a given URL by calling getPersistentCacheFilePath, if
85 * you want to do something such as delete it by hand.
86 *
87 * Note that FileSource does not cache local files (i.e. does not
88 * make a copy of files that already appear to be stored on the
89 * local filesystem) in either mode.
90 */
91 enum LocalCacheMode {
92 TemporaryCache,
93 PersistentCache
94 };
95
96 /** 64 /**
97 * Construct a FileSource using the given local file path or URL. 65 * Construct a FileSource using the given local file path or URL.
98 * The URL may be raw or encoded. 66 * The URL may be raw or encoded.
99 * 67 *
100 * If a ProgressReporter is provided, it will be updated with 68 * If a ProgressReporter is provided, it will be updated with
101 * progress status. Note that the progress() signal will also be 69 * progress status. Note that the progress() signal will also be
102 * emitted regularly during retrieval, even if no reporter is 70 * emitted regularly during retrieval, even if no reporter is
103 * supplied here. Caller retains ownership of the reporter object. 71 * supplied here. Caller retains ownership of the reporter object.
104 * 72 */
105 * If LocalCacheMode is PersistentCache, a persistent cache file 73 FileSource(QString fileOrUrl, ProgressReporter *reporter = 0);
106 * will be used. See LocalCacheMode documentation for details.
107 */
108 FileSource(QString fileOrUrl,
109 ProgressReporter *reporter = 0,
110 LocalCacheMode mode = TemporaryCache);
111 74
112 /** 75 /**
113 * Construct a FileSource using the given remote URL. 76 * Construct a FileSource using the given remote URL.
114 * 77 *
115 * If a ProgressReporter is provided, it will be updated with 78 * If a ProgressReporter is provided, it will be updated with
116 * progress status. Note that the progress() signal will also be 79 * progress status. Note that the progress() signal will also be
117 * emitted regularly during retrieval, even if no reporter is 80 * emitted regularly during retrieval, even if no reporter is
118 * supplied here. Caller retains ownership of the reporter object. 81 * supplied here. Caller retains ownership of the reporter object.
119 * 82 */
120 * If LocalCacheMode is PersistentCache, a persistent cache file 83 FileSource(QUrl url, ProgressReporter *reporter = 0);
121 * will be used. See LocalCacheMode documentation for details.
122 */
123 FileSource(QUrl url,
124 ProgressReporter *reporter = 0,
125 LocalCacheMode mode = TemporaryCache);
126 84
127 FileSource(const FileSource &); 85 FileSource(const FileSource &);
128 86
129 virtual ~FileSource(); 87 virtual ~FileSource();
130 88
198 QString getErrorString() const; 156 QString getErrorString() const;
199 157
200 /** 158 /**
201 * Specify whether any local, cached file should remain on disc 159 * Specify whether any local, cached file should remain on disc
202 * after this FileSource has been destroyed. The default is false 160 * after this FileSource has been destroyed. The default is false
203 * (cached files share their FileSource owners' lifespans). This 161 * (cached files share their FileSource owners' lifespans).
204 * is only meaningful in TemporaryCache mode; even if this setting
205 * is true, the temporary cache will still be deleted when the
206 * program exits. Use PersistentCache mode if you want the cache
207 * to outlast the program.
208 */ 162 */
209 void setLeaveLocalFile(bool leave); 163 void setLeaveLocalFile(bool leave);
210 164
211 /** 165 /**
212 * Return true if the given filename or URL refers to a remote 166 * Return true if the given filename or URL refers to a remote
217 /** 171 /**
218 * Return true if FileSource can handle the retrieval scheme for 172 * Return true if FileSource can handle the retrieval scheme for
219 * the given URL (or if the URL is for a local file). 173 * the given URL (or if the URL is for a local file).
220 */ 174 */
221 static bool canHandleScheme(QUrl url); 175 static bool canHandleScheme(QUrl url);
222
223 /**
224 * Return the path that will be used for the cache file copy of
225 * the given remote URL by a FileSource object constructed in
226 * PersistentCache mode.
227 *
228 * This method also creates the containing directory for such
229 * cache files, if it does not already exist, and so may throw
230 * DirectoryCreationFailed.
231 */
232 static QString getPersistentCacheFilePath(QUrl url);
233 176
234 signals: 177 signals:
235 /** 178 /**
236 * Emitted during URL retrieval, when the retrieval progress 179 * Emitted during URL retrieval, when the retrieval progress
237 * notches up to a new percentage. 180 * notches up to a new percentage.
261 204
262 protected: 205 protected:
263 FileSource &operator=(const FileSource &); // not provided 206 FileSource &operator=(const FileSource &); // not provided
264 207
265 QUrl m_url; 208 QUrl m_url;
266 LocalCacheMode m_cacheMode;
267 QFtp *m_ftp; 209 QFtp *m_ftp;
268 QHttp *m_http; 210 QHttp *m_http;
269 QFile *m_localFile; 211 QFile *m_localFile;
270 QString m_localFilename; 212 QString m_localFilename;
271 QString m_errorString; 213 QString m_errorString;
293 // Create a local file for m_url. If it already existed, return true. 235 // Create a local file for m_url. If it already existed, return true.
294 // The local filename is stored in m_localFilename. 236 // The local filename is stored in m_localFilename.
295 bool createCacheFile(); 237 bool createCacheFile();
296 void deleteCacheFile(); 238 void deleteCacheFile();
297 239
298 static QString getPersistentCacheDirectory();
299
300 static QMutex m_fileCreationMutex; 240 static QMutex m_fileCreationMutex;
301 static int m_count; 241 static int m_count;
302 }; 242 };
303 243
304 #endif 244 #endif