Mercurial > hg > svcore
comparison data/fileio/FileSource.h @ 460:93fb1ebff76b
* Add persistent cache file support to FileSource (e.g. for RDF
descriptions)
* Query RDF plugin data in a background thread on startup
author | Chris Cannam |
---|---|
date | Fri, 17 Oct 2008 13:32:55 +0000 |
parents | 183ee2a55fc7 |
children | 63b8ba45d953 |
comparison
equal
deleted
inserted
replaced
459:6441b31b37ac | 460:93fb1ebff76b |
---|---|
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 share a lifetime with their "owning" FileSource | 54 * Cached files (in their default temporary mode) share a lifetime |
55 * objects; when the last FileSource referring to a given URL is | 55 * with their "owning" FileSource objects. When the last FileSource |
56 * deleted or goes out of scope, its cached file (if any) is also | 56 * referring to a given URL is deleted or goes out of scope, its |
57 * removed. | 57 * cached file (if any) is also removed. You can change this |
58 * behaviour by using persistent cache mode; \see LocalCacheMode for | |
59 * details and caveats. | |
58 */ | 60 */ |
59 class FileSource : public QObject | 61 class FileSource : public QObject |
60 { | 62 { |
61 Q_OBJECT | 63 Q_OBJECT |
62 | 64 |
63 public: | 65 public: |
64 /** | 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 /** | |
65 * Construct a FileSource using the given local file path or URL. | 97 * Construct a FileSource using the given local file path or URL. |
66 * The URL may be raw or encoded. | 98 * The URL may be raw or encoded. |
67 * | 99 * |
68 * If a ProgressReporter is provided, it will be updated with | 100 * If a ProgressReporter is provided, it will be updated with |
69 * progress status. Note that the progress() signal will also be | 101 * progress status. Note that the progress() signal will also be |
70 * emitted regularly during retrieval, even if no reporter is | 102 * emitted regularly during retrieval, even if no reporter is |
71 * supplied here. Caller retains ownership of the reporter object. | 103 * supplied here. Caller retains ownership of the reporter object. |
72 */ | 104 * |
73 FileSource(QString fileOrUrl, ProgressReporter *reporter = 0); | 105 * If LocalCacheMode is PersistentCache, a persistent cache file |
106 * will be used. See LocalCacheMode documentation for details. | |
107 */ | |
108 FileSource(QString fileOrUrl, | |
109 ProgressReporter *reporter = 0, | |
110 LocalCacheMode mode = TemporaryCache); | |
74 | 111 |
75 /** | 112 /** |
76 * Construct a FileSource using the given remote URL. | 113 * Construct a FileSource using the given remote URL. |
77 * | 114 * |
78 * If a ProgressReporter is provided, it will be updated with | 115 * If a ProgressReporter is provided, it will be updated with |
79 * progress status. Note that the progress() signal will also be | 116 * progress status. Note that the progress() signal will also be |
80 * emitted regularly during retrieval, even if no reporter is | 117 * emitted regularly during retrieval, even if no reporter is |
81 * supplied here. Caller retains ownership of the reporter object. | 118 * supplied here. Caller retains ownership of the reporter object. |
82 */ | 119 * |
83 FileSource(QUrl url, ProgressReporter *reporter = 0); | 120 * If LocalCacheMode is PersistentCache, a persistent cache file |
121 * will be used. See LocalCacheMode documentation for details. | |
122 */ | |
123 FileSource(QUrl url, | |
124 ProgressReporter *reporter = 0, | |
125 LocalCacheMode mode = TemporaryCache); | |
84 | 126 |
85 FileSource(const FileSource &); | 127 FileSource(const FileSource &); |
86 | 128 |
87 virtual ~FileSource(); | 129 virtual ~FileSource(); |
88 | 130 |
156 QString getErrorString() const; | 198 QString getErrorString() const; |
157 | 199 |
158 /** | 200 /** |
159 * Specify whether any local, cached file should remain on disc | 201 * Specify whether any local, cached file should remain on disc |
160 * after this FileSource has been destroyed. The default is false | 202 * after this FileSource has been destroyed. The default is false |
161 * (cached files share their FileSource owners' lifespans). | 203 * (cached files share their FileSource owners' lifespans). This |
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. | |
162 */ | 208 */ |
163 void setLeaveLocalFile(bool leave); | 209 void setLeaveLocalFile(bool leave); |
164 | 210 |
165 /** | 211 /** |
166 * Return true if the given filename or URL refers to a remote | 212 * Return true if the given filename or URL refers to a remote |
171 /** | 217 /** |
172 * Return true if FileSource can handle the retrieval scheme for | 218 * Return true if FileSource can handle the retrieval scheme for |
173 * the given URL (or if the URL is for a local file). | 219 * the given URL (or if the URL is for a local file). |
174 */ | 220 */ |
175 static bool canHandleScheme(QUrl url); | 221 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); | |
176 | 233 |
177 signals: | 234 signals: |
178 /** | 235 /** |
179 * Emitted during URL retrieval, when the retrieval progress | 236 * Emitted during URL retrieval, when the retrieval progress |
180 * notches up to a new percentage. | 237 * notches up to a new percentage. |
204 | 261 |
205 protected: | 262 protected: |
206 FileSource &operator=(const FileSource &); // not provided | 263 FileSource &operator=(const FileSource &); // not provided |
207 | 264 |
208 QUrl m_url; | 265 QUrl m_url; |
266 LocalCacheMode m_cacheMode; | |
209 QFtp *m_ftp; | 267 QFtp *m_ftp; |
210 QHttp *m_http; | 268 QHttp *m_http; |
211 QFile *m_localFile; | 269 QFile *m_localFile; |
212 QString m_localFilename; | 270 QString m_localFilename; |
213 QString m_errorString; | 271 QString m_errorString; |
235 // Create a local file for m_url. If it already existed, return true. | 293 // Create a local file for m_url. If it already existed, return true. |
236 // The local filename is stored in m_localFilename. | 294 // The local filename is stored in m_localFilename. |
237 bool createCacheFile(); | 295 bool createCacheFile(); |
238 void deleteCacheFile(); | 296 void deleteCacheFile(); |
239 | 297 |
298 static QString getPersistentCacheDirectory(); | |
299 | |
240 static QMutex m_fileCreationMutex; | 300 static QMutex m_fileCreationMutex; |
241 static int m_count; | 301 static int m_count; |
242 }; | 302 }; |
243 | 303 |
244 #endif | 304 #endif |