29 #include <QCryptographicHash> 41 Profiler p(
"CachedFile::getLocalFilenameFor");
46 QString::fromLocal8Bit
47 (QCryptographicHash::hash(url.toString().toLocal8Bit(),
48 QCryptographicHash::Sha1).toHex());
50 return dir.filePath(filename);
58 QString cacheDirName(
"cache");
60 QFileInfo fi(dir.filePath(cacheDirName));
62 if ((fi.exists() && !fi.isDir()) ||
63 (!fi.exists() && !dir.mkdir(cacheDirName))) {
73 QString preferredContentType) :
75 m_preferredContentType(preferredContentType),
79 Profiler p(
"CachedFile::CachedFile[1]");
81 SVDEBUG <<
"CachedFile::CachedFile: origin is \"" 82 << origin <<
"\"" << endl;
88 QString preferredContentType) :
89 m_origin(url.toString()),
90 m_preferredContentType(preferredContentType),
94 Profiler p(
"CachedFile::CachedFile[2]");
96 SVDEBUG <<
"CachedFile::CachedFile: url is \"" 97 << url.toString() <<
"\"" << endl;
135 SVDEBUG <<
"CachedFile::check: Local file does not exist, making a note that it hasn't been retrieved" << endl;
141 if (lastRetrieval.isValid()) {
142 SVDEBUG <<
"CachedFile::check: Valid last retrieval at " 143 << lastRetrieval.toString() << endl;
147 if (lastRetrieval.addDays(2) < QDateTime::currentDateTime()) {
148 SVDEBUG <<
"CachedFile::check: Out of date; trying to retrieve again" << endl;
157 SVDEBUG <<
"CachedFile::check: Retrieval succeeded" << endl;
160 cerr <<
"CachedFile::check: Retrieval failed, will try again later (using existing file for now)" << endl;
164 SVDEBUG <<
"CachedFile::check: No valid last retrieval" << endl;
167 SVDEBUG <<
"CachedFile::check: Retrieval succeeded" << endl;
171 cerr <<
"CachedFile::check: Retrieval failed, remaining in invalid state" << endl;
194 SVDEBUG <<
"CachedFile::retrieve: ERROR: FileSource reported unavailable or failure" << endl;
201 SVDEBUG <<
"CachedFile::retrieve: ERROR: FileSource reported failure during receive" << endl;
206 QFile tempFile(tempName);
207 if (!tempFile.exists()) {
208 SVDEBUG <<
"CachedFile::retrieve: ERROR: FileSource reported success, but local temporary file \"" << tempName <<
"\" does not exist" << endl;
213 if (previous.exists()) {
214 if (!previous.remove()) {
215 cerr <<
"CachedFile::retrieve: ERROR: Failed to remove previous copy of cached file at \"" <<
m_localFilename <<
"\"" << endl;
225 cerr <<
"CachedFile::retrieve: ERROR: Failed to copy newly retrieved file from \"" << tempName <<
"\" to \"" <<
m_localFilename <<
"\"" << endl;
229 SVDEBUG <<
"CachedFile::retrieve: Successfully copied newly retrieved file \"" << tempName <<
"\" to its home at \"" <<
m_localFilename <<
"\"" << endl;
238 settings.beginGroup(
"FileCache");
240 QString key(
"last-retrieval-times");
242 QMap<QString, QVariant> timeMap = settings.value(key).toMap();
257 settings.beginGroup(
"FileCache");
259 QString key(
"last-retrieval-times");
261 QMap<QString, QVariant> timeMap = settings.value(key).toMap();
264 if (successful) dt = QDateTime::currentDateTime();
267 settings.setValue(key, timeMap);
CachedFile(QString fileOrUrl, ProgressReporter *reporter=0, QString preferredContentType="")
static OriginLocalFilenameMap m_knownGoodCaches
QString m_preferredContentType
void waitForData()
Block on a sub-event-loop until the whole of the data has been retrieved (if it is remote).
QDateTime getLastRetrieval()
bool isOK() const
Return true if the FileSource object is valid and neither error nor cancellation occurred while retri...
std::map< QString, QString > OriginLocalFilenameMap
QString getLocalFilename() const
static TempDirectory * getInstance()
static QString getLocalFilenameFor(QUrl url)
FileSource is a class used to refer to the contents of a file that may be either local or at a remote...
static QString getCacheDirectory()
QString getLocalFilename() const
Return the name of the local file this FileSource refers to.
QString getContainingPath()
Return the path of the directory in which the temporary directory has been or will be created.
ProgressReporter * m_reporter
void updateLastRetrieval(bool successful)
Profile point instance class.
bool isAvailable()
Return true if the file or remote URL exists.