#include <FileSource.h>
Collaboration diagram for FileSource:

When a FileSource object is constructed, the file or URL passed to its constructor is tested for validity, and if it refers to a remote HTTP or FTP location it is retrieved asynchronously (the Qt event loop must be running) and cached locally. You can then query a local path for the file and refer to that as a normal filename.
Use isAvailable() to test whether the file or remote URL exists, and isOK() to test for internal validity or transmission errors. Call waitForStatus() to block until the availability and validity of the URL have been established so that isAvailable may be called, and waitForData() to block until the entire file has been cached.
FileSource handles reference counting for cache files. You can construct many FileSource objects with the same URL and you will receive the same cached file for each; it is also reasonable to pass FileSource objects by value. FileSource only makes sense for stateless URLs that result in the same data on each request.
Cached files share a lifetime with their "owning" FileSource objects; when the last FileSource referring to a given URL is deleted or goes out of scope, its cached file (if any) is also removed.
Definition at line 60 of file FileSource.h.
Public Types | |
| enum | ShowProgressType { ProgressNone, ProgressDialog, ProgressToConsole } |
Signals | |
| void | progress (int percent) |
| Emitted during URL retrieval, when the retrieval progress notches up to a new percentage. | |
| void | statusAvailable () |
| Emitted when the file's existence has been tested and/or response header received. | |
| void | ready () |
| Emitted when the entire file data has been retrieved and the local file is complete (if no error has occurred). | |
Public Member Functions | |
| FileSource (QString fileOrUrl, ShowProgressType progressType=ProgressNone) | |
| Construct a FileSource using the given local file path or URL. | |
| FileSource (QUrl url, ShowProgressType progressType=ProgressNone) | |
| Construct a FileSource using the given remote URL. | |
| FileSource (const FileSource &) | |
| virtual | ~FileSource () |
| void | waitForStatus () |
| Block on a sub-event-loop until the availability of the file or remote URL is known. | |
| void | waitForData () |
| Block on a sub-event-loop until the whole of the data has been retrieved (if it is remote). | |
| bool | isOK () const |
| Return true if the FileSource object is valid and no error occurred in looking up the file or remote URL. | |
| bool | isAvailable () |
| Return true if the file or remote URL exists. | |
| bool | isDone () const |
| Return true if the entire file has been retrieved and is available. | |
| bool | isRemote () const |
| Return true if this FileSource is referring to a remote URL. | |
| QString | getLocation () const |
| Return the location filename or URL as passed to the constructor. | |
| QString | getLocalFilename () const |
| Return the name of the local file this FileSource refers to. | |
| QString | getContentType () const |
| Return the MIME content type of this file, if known. | |
| QString | getExtension () const |
| Return the file extension for this file, if any. | |
| QString | getErrorString () const |
| Return an error message, if isOK() is false. | |
| void | setLeaveLocalFile (bool leave) |
| Specify whether any local, cached file should remain on disc after this FileSource has been destroyed. | |
Static Public Member Functions | |
| static bool | isRemote (QString fileOrUrl) |
| Return true if the given filename or URL refers to a remote URL. | |
| static bool | canHandleScheme (QUrl url) |
| Return true if FileSource can handle the retrieval scheme for the given URL (or if the URL is for a local file). | |
Protected Types | |
| typedef std::map< QUrl, int > | RemoteRefCountMap |
| typedef std::map< QUrl, QString > | RemoteLocalMap |
Protected Slots | |
| void | dataReadProgress (int done, int total) |
| void | httpResponseHeaderReceived (const QHttpResponseHeader &resp) |
| void | ftpCommandFinished (int, bool) |
| void | dataTransferProgress (qint64 done, qint64 total) |
| void | done (bool error) |
| void | showProgressDialog () |
| void | cancelled () |
Protected Member Functions | |
| FileSource & | operator= (const FileSource &) |
| void | init () |
| void | initHttp () |
| void | initFtp () |
| void | cleanup () |
| bool | createCacheFile () |
| void | deleteCacheFile () |
Protected Attributes | |
| QUrl | m_url |
| QFtp * | m_ftp |
| QHttp * | m_http |
| QFile * | m_localFile |
| QString | m_localFilename |
| QString | m_errorString |
| QString | m_contentType |
| bool | m_ok |
| int | m_lastStatus |
| bool | m_remote |
| bool | m_done |
| bool | m_leaveLocalFile |
| ShowProgressType | m_progressType |
| ProgressPrinter * | m_progressPrinter |
| QProgressDialog * | m_progressDialog |
| QTimer | m_progressShowTimer |
| bool | m_refCounted |
Static Protected Attributes | |
| static RemoteRefCountMap | m_refCountMap |
| static RemoteLocalMap | m_remoteLocalMap |
| static QMutex | m_mapMutex |
| static QMutex | m_fileCreationMutex |
| static int | m_count |
typedef std::map<QUrl, int> FileSource::RemoteRefCountMap [protected] |
Definition at line 238 of file FileSource.h.
typedef std::map<QUrl, QString> FileSource::RemoteLocalMap [protected] |
Definition at line 239 of file FileSource.h.
Definition at line 66 of file FileSource.h.
| FileSource::FileSource | ( | QString | fileOrUrl, | |
| ShowProgressType | progressType = ProgressNone | |||
| ) |
Construct a FileSource using the given local file path or URL.
The URL may be raw or encoded.
If progressType is ProgressDialog, a progress dialog will be shown for any network transfers; if it is ProgressToConsole, a progress indication will be sent to the console. Note that the progress() signal will also be emitted regularly during retrieval, even if progressType is ProgressNone.
Definition at line 49 of file FileSource.cpp.
References canHandleScheme(), deleteCacheFile(), init(), isAvailable(), isRemote(), m_done, m_errorString, m_lastStatus, m_ok, m_url, ready(), statusAvailable(), and waitForStatus().
| FileSource::FileSource | ( | QUrl | url, | |
| ShowProgressType | progressType = ProgressNone | |||
| ) |
Construct a FileSource using the given remote URL.
If progressType is ProgressDialog, a progress dialog will be shown for any network transfers; if it is ProgressToConsole, a progress indication will be sent to the console. Note that the progress() signal also will be emitted regularly during retrieval, even if progressType is ProgressNone.
Definition at line 116 of file FileSource.cpp.
References canHandleScheme(), init(), m_errorString, and m_url.
| FileSource::FileSource | ( | const FileSource & | ) |
Definition at line 145 of file FileSource.cpp.
References canHandleScheme(), isRemote(), m_done, m_errorString, m_lastStatus, m_localFilename, m_mapMutex, m_ok, m_refCounted, m_refCountMap, m_remoteLocalMap, and m_url.
| FileSource::~FileSource | ( | ) | [virtual] |
Definition at line 196 of file FileSource.cpp.
References cleanup(), deleteCacheFile(), isRemote(), m_leaveLocalFile, and m_url.
| void FileSource::waitForStatus | ( | ) |
Block on a sub-event-loop until the availability of the file or remote URL is known.
Definition at line 485 of file FileSource.cpp.
References m_done, m_lastStatus, and m_ok.
Referenced by FileSource(), isAvailable(), and SVFileReader::readModel().
| void FileSource::waitForData | ( | ) |
Block on a sub-event-loop until the whole of the data has been retrieved (if it is remote).
Definition at line 494 of file FileSource.cpp.
Referenced by MainWindowBase::open(), MainWindowBase::openAudio(), MainWindowBase::openLayer(), MainWindowBase::openPlaylist(), MainWindowBase::openSession(), SVFileReader::readModel(), ImageDialog::updatePreview(), and WaveFileModel::WaveFileModel().
| bool FileSource::isOK | ( | ) | const |
Return true if the FileSource object is valid and no error occurred in looking up the file or remote URL.
Non-existence of the file or URL is not an error -- call isAvailable() to test for that.
Definition at line 509 of file FileSource.cpp.
References m_ok.
Referenced by ImageLayer::checkAddRemote(), AudioFileReaderFactory::create(), SVFileReader::readModel(), ImageDialog::updatePreview(), and WaveFileModel::WaveFileModel().
| bool FileSource::isAvailable | ( | ) |
Return true if the file or remote URL exists.
This may block on a sub-event-loop (calling waitForStatus) if the status is not yet available.
Definition at line 471 of file FileSource.cpp.
References m_lastStatus, m_ok, and waitForStatus().
Referenced by AudioFileReaderFactory::create(), FileSource(), MainWindowBase::open(), MainWindowBase::openAudio(), MainWindowBase::openLayer(), MainWindowBase::openPlaylist(), MainWindowBase::openSession(), and SVFileReader::readModel().
| bool FileSource::isDone | ( | ) | const |
Return true if the entire file has been retrieved and is available.
Definition at line 515 of file FileSource.cpp.
References m_done.
| bool FileSource::isRemote | ( | ) | const |
Return true if this FileSource is referring to a remote URL.
Definition at line 521 of file FileSource.cpp.
References m_remote.
Referenced by ImageLayer::checkAddRemote(), deleteCacheFile(), FileSource(), FileFinder::findRelative(), init(), MainWindowBase::openAudio(), MainWindowBase::openLayer(), MainWindowBase::openSession(), ImageDialog::updatePreview(), and ~FileSource().
| QString FileSource::getLocation | ( | ) | const |
Return the location filename or URL as passed to the constructor.
Definition at line 527 of file FileSource.cpp.
References m_url.
Referenced by AudioFileReaderFactory::create(), WavFileReader::getLocation(), ResamplingWavFileReader::getLocation(), OggVorbisFileReader::getLocation(), MP3FileReader::getLocation(), MainWindowBase::openAudio(), MainWindowBase::openImage(), MainWindowBase::openLayer(), and MainWindowBase::openSession().
| QString FileSource::getLocalFilename | ( | ) | const |
Return the name of the local file this FileSource refers to.
This is the filename that should be used when reading normally from the FileSource. If the filename passed to the constructor was a local file, this will return the same filename; otherwise this will be the name of the temporary cache file owned by the FileSource.
Definition at line 533 of file FileSource.cpp.
References m_localFilename.
Referenced by ImageLayer::checkAddRemote(), MainWindowBase::openAudio(), MainWindowBase::openLayer(), MainWindowBase::openPlaylist(), MainWindowBase::openSession(), and ImageDialog::updatePreview().
| QString FileSource::getContentType | ( | ) | const |
Return the MIME content type of this file, if known.
Definition at line 539 of file FileSource.cpp.
References m_contentType.
Referenced by WavFileReader::supports(), OggVorbisFileReader::supports(), and MP3FileReader::supports().
| QString FileSource::getExtension | ( | ) | const |
Return the file extension for this file, if any.
Definition at line 545 of file FileSource.cpp.
References m_localFilename, and m_url.
Referenced by MainWindowBase::openLayer(), MainWindowBase::openPlaylist(), MainWindowBase::openSession(), WavFileReader::supports(), OggVorbisFileReader::supports(), and MP3FileReader::supports().
| QString FileSource::getErrorString | ( | ) | const |
Return an error message, if isOK() is false.
Definition at line 555 of file FileSource.cpp.
References m_errorString.
Referenced by SVFileReader::readModel(), and ImageDialog::updatePreview().
| void FileSource::setLeaveLocalFile | ( | bool | leave | ) |
Specify whether any local, cached file should remain on disc after this FileSource has been destroyed.
The default is false (cached files share their FileSource owners' lifespans).
Definition at line 503 of file FileSource.cpp.
References m_leaveLocalFile.
| bool FileSource::isRemote | ( | QString | fileOrUrl | ) | [static] |
Return true if the given filename or URL refers to a remote URL.
Definition at line 453 of file FileSource.cpp.
| bool FileSource::canHandleScheme | ( | QUrl | url | ) | [static] |
Return true if FileSource can handle the retrieval scheme for the given URL (or if the URL is for a local file).
Definition at line 462 of file FileSource.cpp.
Referenced by FileSource(), FileFinder::find(), and ImageDialog::updatePreview().
| void FileSource::progress | ( | int | percent | ) | [signal] |
Emitted during URL retrieval, when the retrieval progress notches up to a new percentage.
Referenced by dataTransferProgress(), done(), and init().
| void FileSource::statusAvailable | ( | ) | [signal] |
Emitted when the file's existence has been tested and/or response header received.
Calls to isAvailable() after this has been emitted will not block.
Referenced by FileSource(), and httpResponseHeaderReceived().
| void FileSource::ready | ( | ) | [signal] |
Emitted when the entire file data has been retrieved and the local file is complete (if no error has occurred).
Referenced by done(), and FileSource().
| void FileSource::dataReadProgress | ( | int | done, | |
| int | total | |||
| ) | [protected, slot] |
Definition at line 561 of file FileSource.cpp.
References dataTransferProgress().
Referenced by initHttp().
| void FileSource::httpResponseHeaderReceived | ( | const QHttpResponseHeader & | resp | ) | [protected, slot] |
Definition at line 567 of file FileSource.cpp.
References m_contentType, m_errorString, m_lastStatus, and statusAvailable().
Referenced by initHttp().
| void FileSource::ftpCommandFinished | ( | int | , | |
| bool | ||||
| ) | [protected, slot] |
Definition at line 588 of file FileSource.cpp.
References m_errorString, m_ftp, and m_lastStatus.
Referenced by initFtp().
| void FileSource::dataTransferProgress | ( | qint64 | done, | |
| qint64 | total | |||
| ) | [protected, slot] |
Definition at line 620 of file FileSource.cpp.
References m_progressDialog, and progress().
Referenced by dataReadProgress(), and initFtp().
| void FileSource::done | ( | bool | error | ) | [protected, slot] |
Definition at line 644 of file FileSource.cpp.
References cleanup(), deleteCacheFile(), m_done, m_errorString, m_ftp, m_http, m_lastStatus, m_localFilename, m_ok, progress(), and ready().
Referenced by initFtp(), and initHttp().
| void FileSource::showProgressDialog | ( | ) | [protected, slot] |
| void FileSource::cancelled | ( | ) | [protected, slot] |
Definition at line 634 of file FileSource.cpp.
References cleanup(), m_done, m_errorString, and m_ok.
Referenced by init().
| FileSource& FileSource::operator= | ( | const FileSource & | ) | [protected] |
| void FileSource::init | ( | ) | [protected] |
Definition at line 208 of file FileSource.cpp.
References cancelled(), cleanup(), createCacheFile(), initFtp(), initHttp(), isRemote(), m_done, m_lastStatus, m_localFile, m_localFilename, m_mapMutex, m_ok, m_progressDialog, m_progressPrinter, m_progressShowTimer, m_progressType, m_refCounted, m_refCountMap, m_remote, m_remoteLocalMap, m_url, progress(), ProgressDialog, ProgressNone, ProgressToConsole, and showProgressDialog().
Referenced by FileSource().
| void FileSource::initHttp | ( | ) | [protected] |
Definition at line 334 of file FileSource.cpp.
References dataReadProgress(), done(), httpResponseHeaderReceived(), m_http, m_localFile, m_ok, and m_url.
Referenced by init().
| void FileSource::initFtp | ( | ) | [protected] |
Definition at line 397 of file FileSource.cpp.
References dataTransferProgress(), done(), ftpCommandFinished(), m_ftp, m_localFile, m_ok, and m_url.
Referenced by init().
| void FileSource::cleanup | ( | ) | [protected] |
Definition at line 429 of file FileSource.cpp.
References m_done, m_ftp, m_http, m_localFile, m_progressDialog, and m_progressPrinter.
Referenced by cancelled(), deleteCacheFile(), done(), init(), and ~FileSource().
| bool FileSource::createCacheFile | ( | ) | [protected] |
Definition at line 753 of file FileSource.cpp.
References TempDirectory::getInstance(), TempDirectory::getSubDirectoryPath(), m_count, m_fileCreationMutex, m_localFilename, m_mapMutex, m_refCounted, m_refCountMap, m_remoteLocalMap, m_url, and DirectoryCreationFailed::what().
Referenced by init().
| void FileSource::deleteCacheFile | ( | ) | [protected] |
Definition at line 692 of file FileSource.cpp.
References cleanup(), isRemote(), m_done, m_fileCreationMutex, m_localFilename, m_mapMutex, m_refCounted, m_refCountMap, and m_url.
Referenced by done(), FileSource(), and ~FileSource().
QUrl FileSource::m_url [protected] |
Definition at line 221 of file FileSource.h.
Referenced by createCacheFile(), deleteCacheFile(), FileSource(), getExtension(), getLocation(), init(), initFtp(), initHttp(), and ~FileSource().
QFtp* FileSource::m_ftp [protected] |
Definition at line 222 of file FileSource.h.
Referenced by cleanup(), done(), ftpCommandFinished(), and initFtp().
QHttp* FileSource::m_http [protected] |
QFile* FileSource::m_localFile [protected] |
Definition at line 224 of file FileSource.h.
Referenced by cleanup(), init(), initFtp(), and initHttp().
QString FileSource::m_localFilename [protected] |
Definition at line 225 of file FileSource.h.
Referenced by createCacheFile(), deleteCacheFile(), done(), FileSource(), getExtension(), getLocalFilename(), and init().
QString FileSource::m_errorString [protected] |
Definition at line 226 of file FileSource.h.
Referenced by cancelled(), done(), FileSource(), ftpCommandFinished(), getErrorString(), and httpResponseHeaderReceived().
QString FileSource::m_contentType [protected] |
Definition at line 227 of file FileSource.h.
Referenced by getContentType(), and httpResponseHeaderReceived().
bool FileSource::m_ok [protected] |
Definition at line 228 of file FileSource.h.
Referenced by cancelled(), done(), FileSource(), init(), initFtp(), initHttp(), isAvailable(), isOK(), waitForData(), and waitForStatus().
int FileSource::m_lastStatus [protected] |
Definition at line 229 of file FileSource.h.
Referenced by done(), FileSource(), ftpCommandFinished(), httpResponseHeaderReceived(), init(), isAvailable(), and waitForStatus().
bool FileSource::m_remote [protected] |
bool FileSource::m_done [protected] |
Definition at line 231 of file FileSource.h.
Referenced by cancelled(), cleanup(), deleteCacheFile(), done(), FileSource(), init(), isDone(), waitForData(), and waitForStatus().
bool FileSource::m_leaveLocalFile [protected] |
ShowProgressType FileSource::m_progressType [protected] |
ProgressPrinter* FileSource::m_progressPrinter [protected] |
QProgressDialog* FileSource::m_progressDialog [protected] |
Definition at line 235 of file FileSource.h.
Referenced by cleanup(), dataTransferProgress(), init(), and showProgressDialog().
QTimer FileSource::m_progressShowTimer [protected] |
FileSource::RemoteRefCountMap FileSource::m_refCountMap [static, protected] |
Definition at line 240 of file FileSource.h.
Referenced by createCacheFile(), deleteCacheFile(), FileSource(), and init().
FileSource::RemoteLocalMap FileSource::m_remoteLocalMap [static, protected] |
Definition at line 241 of file FileSource.h.
Referenced by createCacheFile(), FileSource(), and init().
QMutex FileSource::m_mapMutex [static, protected] |
Definition at line 242 of file FileSource.h.
Referenced by createCacheFile(), deleteCacheFile(), FileSource(), and init().
bool FileSource::m_refCounted [protected] |
Definition at line 243 of file FileSource.h.
Referenced by createCacheFile(), deleteCacheFile(), FileSource(), and init().
QMutex FileSource::m_fileCreationMutex [static, protected] |
Definition at line 256 of file FileSource.h.
Referenced by createCacheFile(), and deleteCacheFile().
int FileSource::m_count [static, protected] |
1.5.1