Chris@208: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@208: Chris@208: /* Chris@208: Sonic Visualiser Chris@208: An audio file viewer and annotation editor. Chris@208: Centre for Digital Music, Queen Mary, University of London. Chris@208: This file copyright 2007 QMUL. Chris@208: Chris@208: This program is free software; you can redistribute it and/or Chris@208: modify it under the terms of the GNU General Public License as Chris@208: published by the Free Software Foundation; either version 2 of the Chris@208: License, or (at your option) any later version. See the file Chris@208: COPYING included with this distribution for more information. Chris@208: */ Chris@208: Chris@317: #include "FileSource.h" Chris@357: Chris@208: #include "base/TempDirectory.h" Chris@208: #include "base/Exceptions.h" Chris@392: #include "base/ProgressReporter.h" Chris@502: #include "system/System.h" Chris@208: Chris@762: #include Chris@762: #include Chris@208: #include Chris@208: #include Chris@392: #include Chris@208: Chris@208: #include Chris@405: #include Chris@208: Chris@608: #include Chris@608: Chris@763: //#define DEBUG_FILE_SOURCE 1 Chris@327: Chris@208: int Chris@317: FileSource::m_count = 0; Chris@208: Chris@208: QMutex Chris@317: FileSource::m_fileCreationMutex; Chris@208: Chris@317: FileSource::RemoteRefCountMap Chris@317: FileSource::m_refCountMap; Chris@304: Chris@317: FileSource::RemoteLocalMap Chris@317: FileSource::m_remoteLocalMap; Chris@304: Chris@304: QMutex Chris@317: FileSource::m_mapMutex; Chris@304: Chris@529: #ifdef DEBUG_FILE_SOURCE Chris@529: static int extantCount = 0; Chris@529: static std::map urlExtantCountMap; Chris@529: static void incCount(QString url) { Chris@529: ++extantCount; Chris@529: if (urlExtantCountMap.find(url) == urlExtantCountMap.end()) { Chris@529: urlExtantCountMap[url] = 1; Chris@529: } else { Chris@529: ++urlExtantCountMap[url]; Chris@529: } Chris@529: std::cerr << "FileSource: Now " << urlExtantCountMap[url] << " for this url, " << extantCount << " total" << std::endl; Chris@529: } Chris@529: static void decCount(QString url) { Chris@529: --extantCount; Chris@529: --urlExtantCountMap[url]; Chris@529: std::cerr << "FileSource: Now " << urlExtantCountMap[url] << " for this url, " << extantCount << " total" << std::endl; Chris@529: } Chris@529: #endif Chris@529: Chris@762: static QNetworkAccessManager nm; Chris@762: Chris@520: FileSource::FileSource(QString fileOrUrl, ProgressReporter *reporter, Chris@520: QString preferredContentType) : Chris@614: m_url(fileOrUrl, QUrl::StrictMode), Chris@316: m_localFile(0), Chris@762: m_reply(0), Chris@520: m_preferredContentType(preferredContentType), Chris@316: m_ok(false), Chris@316: m_lastStatus(0), Chris@706: m_resource(fileOrUrl.startsWith(':')), Chris@316: m_remote(isRemote(fileOrUrl)), Chris@316: m_done(false), Chris@316: m_leaveLocalFile(false), Chris@392: m_reporter(reporter), Chris@316: m_refCounted(false) Chris@316: { Chris@706: if (m_resource) { // qrc file Chris@706: m_url = QUrl("qrc" + fileOrUrl); Chris@706: } Chris@706: Chris@661: if (m_url.toString() == "") { Chris@661: m_url = QUrl(fileOrUrl, QUrl::TolerantMode); Chris@661: } Chris@661: Chris@327: #ifdef DEBUG_FILE_SOURCE Chris@706: std::cerr << "FileSource::FileSource(" << fileOrUrl << "): url <" << m_url.toString() << ">" << std::endl; Chris@529: incCount(m_url.toString()); Chris@327: #endif Chris@316: Chris@316: if (!canHandleScheme(m_url)) { Chris@762: std::cerr << "FileSource::FileSource: ERROR: Unsupported scheme in URL \"" << m_url.toString() << "\"" << std::endl; Chris@316: m_errorString = tr("Unsupported scheme in URL"); Chris@316: return; Chris@316: } Chris@316: Chris@357: init(); Chris@316: Chris@614: if (!isRemote() && Chris@614: !isAvailable()) { Chris@614: #ifdef DEBUG_FILE_SOURCE Chris@706: std::cerr << "FileSource::FileSource: Failed to open local file with URL \"" << m_url.toString() << "\"; trying again assuming filename was encoded" << std::endl; Chris@614: #endif Chris@762: m_url = QUrl::fromEncoded(fileOrUrl.toLatin1()); Chris@614: init(); Chris@614: } Chris@614: Chris@316: if (isRemote() && Chris@316: (fileOrUrl.contains('%') || Chris@316: fileOrUrl.contains("--"))) { // for IDNA Chris@316: Chris@316: waitForStatus(); Chris@316: Chris@316: if (!isAvailable()) { Chris@336: Chris@316: // The URL was created on the assumption that the string Chris@316: // was human-readable. Let's try again, this time Chris@316: // assuming it was already encoded. Chris@317: std::cerr << "FileSource::FileSource: Failed to retrieve URL \"" Chris@316: << fileOrUrl.toStdString() Chris@316: << "\" as human-readable URL; " Chris@316: << "trying again treating it as encoded URL" Chris@316: << std::endl; Chris@336: Chris@336: // even though our cache file doesn't exist (because the Chris@336: // resource was 404), we still need to ensure we're no Chris@336: // longer associating a filename with this url in the Chris@336: // refcount map -- or createCacheFile will think we've Chris@336: // already done all the work and no request will be sent Chris@336: deleteCacheFile(); Chris@336: Chris@762: m_url = QUrl::fromEncoded(fileOrUrl.toLatin1()); Chris@336: Chris@336: m_ok = false; Chris@336: m_done = false; Chris@336: m_lastStatus = 0; Chris@357: init(); Chris@316: } Chris@316: } Chris@325: Chris@325: if (!isRemote()) { Chris@325: emit statusAvailable(); Chris@325: emit ready(); Chris@325: } Chris@497: Chris@504: #ifdef DEBUG_FILE_SOURCE Chris@762: std::cerr << "FileSource::FileSource(string) exiting" << std::endl; Chris@504: #endif Chris@316: } Chris@316: Chris@469: FileSource::FileSource(QUrl url, ProgressReporter *reporter) : Chris@304: m_url(url), Chris@208: m_localFile(0), Chris@762: m_reply(0), Chris@208: m_ok(false), Chris@210: m_lastStatus(0), Chris@706: m_resource(false), Chris@316: m_remote(isRemote(url.toString())), Chris@208: m_done(false), Chris@316: m_leaveLocalFile(false), Chris@392: m_reporter(reporter), Chris@316: m_refCounted(false) Chris@208: { Chris@327: #ifdef DEBUG_FILE_SOURCE Chris@762: std::cerr << "FileSource::FileSource(" << url.toString() << ") [as url]" << std::endl; Chris@529: incCount(m_url.toString()); Chris@327: #endif Chris@316: Chris@316: if (!canHandleScheme(m_url)) { Chris@762: std::cerr << "FileSource::FileSource: ERROR: Unsupported scheme in URL \"" << m_url.toString() << "\"" << std::endl; Chris@316: m_errorString = tr("Unsupported scheme in URL"); Chris@208: return; Chris@208: } Chris@208: Chris@357: init(); Chris@497: Chris@504: #ifdef DEBUG_FILE_SOURCE Chris@762: std::cerr << "FileSource::FileSource(url) exiting" << std::endl; Chris@504: #endif Chris@316: } Chris@304: Chris@317: FileSource::FileSource(const FileSource &rf) : Chris@316: QObject(), Chris@316: m_url(rf.m_url), Chris@316: m_localFile(0), Chris@762: m_reply(0), Chris@316: m_ok(rf.m_ok), Chris@316: m_lastStatus(rf.m_lastStatus), Chris@706: m_resource(rf.m_resource), Chris@316: m_remote(rf.m_remote), Chris@316: m_done(false), Chris@316: m_leaveLocalFile(false), Chris@392: m_reporter(rf.m_reporter), Chris@316: m_refCounted(false) Chris@316: { Chris@327: #ifdef DEBUG_FILE_SOURCE Chris@762: std::cerr << "FileSource::FileSource(" << m_url.toString() << ") [copy ctor]" << std::endl; Chris@529: incCount(m_url.toString()); Chris@327: #endif Chris@304: Chris@316: if (!canHandleScheme(m_url)) { Chris@762: std::cerr << "FileSource::FileSource: ERROR: Unsupported scheme in URL \"" << m_url.toString() << "\"" << std::endl; Chris@316: m_errorString = tr("Unsupported scheme in URL"); Chris@304: return; Chris@304: } Chris@304: Chris@469: if (!isRemote()) { Chris@316: m_localFilename = rf.m_localFilename; Chris@316: } else { Chris@316: QMutexLocker locker(&m_mapMutex); Chris@327: #ifdef DEBUG_FILE_SOURCE Chris@762: std::cerr << "FileSource::FileSource(copy ctor): ref count is " Chris@762: << m_refCountMap[m_url] << std::endl; Chris@327: #endif Chris@316: if (m_refCountMap[m_url] > 0) { Chris@316: m_refCountMap[m_url]++; Chris@327: #ifdef DEBUG_FILE_SOURCE Chris@316: std::cerr << "raised it to " << m_refCountMap[m_url] << std::endl; Chris@327: #endif Chris@316: m_localFilename = m_remoteLocalMap[m_url]; Chris@316: m_refCounted = true; Chris@316: } else { Chris@316: m_ok = false; Chris@316: m_lastStatus = 404; Chris@316: } Chris@316: } Chris@316: Chris@316: m_done = true; Chris@497: Chris@504: #ifdef DEBUG_FILE_SOURCE Chris@762: std::cerr << "FileSource::FileSource(" << m_url.toString() << ") [copy ctor]: note: local filename is \"" << m_localFilename << "\"" << std::endl; Chris@527: #endif Chris@527: Chris@527: #ifdef DEBUG_FILE_SOURCE Chris@762: std::cerr << "FileSource::FileSource(copy ctor) exiting" << std::endl; Chris@504: #endif Chris@316: } Chris@316: Chris@317: FileSource::~FileSource() Chris@316: { Chris@327: #ifdef DEBUG_FILE_SOURCE Chris@686: std::cerr << "FileSource(" << m_url.toString() << ")::~FileSource" << std::endl; Chris@529: decCount(m_url.toString()); Chris@327: #endif Chris@316: Chris@316: cleanup(); Chris@316: Chris@469: if (isRemote() && !m_leaveLocalFile) deleteCacheFile(); Chris@316: } Chris@316: Chris@316: void Chris@357: FileSource::init() Chris@316: { Chris@706: if (isResource()) { Chris@355: #ifdef DEBUG_FILE_SOURCE Chris@706: std::cerr << "FileSource::init: Is a resource" << std::endl; Chris@706: #endif Chris@706: QString resourceFile = m_url.toString(); Chris@706: resourceFile.replace(QRegExp("^qrc:"), ":"); Chris@706: Chris@706: if (!QFileInfo(resourceFile).exists()) { Chris@706: #ifdef DEBUG_FILE_SOURCE Chris@706: std::cerr << "FileSource::init: Resource file of this name does not exist, switching to non-resource URL" << std::endl; Chris@706: #endif Chris@706: m_url = resourceFile; Chris@706: m_resource = false; Chris@706: } Chris@706: } Chris@706: Chris@706: if (!isRemote() && !isResource()) { Chris@706: #ifdef DEBUG_FILE_SOURCE Chris@706: std::cerr << "FileSource::init: Not a remote URL" << std::endl; Chris@355: #endif Chris@355: bool literal = false; Chris@316: m_localFilename = m_url.toLocalFile(); Chris@342: if (m_localFilename == "") { Chris@342: // QUrl may have mishandled the scheme (e.g. in a DOS path) Chris@342: m_localFilename = m_url.toString(); Chris@355: literal = true; Chris@342: } Chris@439: m_localFilename = QFileInfo(m_localFilename).absoluteFilePath(); Chris@439: Chris@355: #ifdef DEBUG_FILE_SOURCE Chris@706: std::cerr << "FileSource::init: URL translates to local filename \"" Chris@706: << m_localFilename << "\" (with literal=" << literal << ")" Chris@706: << std::endl; Chris@355: #endif Chris@316: m_ok = true; Chris@355: m_lastStatus = 200; Chris@355: Chris@316: if (!QFileInfo(m_localFilename).exists()) { Chris@355: if (literal) { Chris@355: m_lastStatus = 404; Chris@355: } else { Chris@614: #ifdef DEBUG_FILE_SOURCE Chris@706: std::cerr << "FileSource::init: Local file of this name does not exist, trying URL as a literal filename" << std::endl; Chris@614: #endif Chris@355: // Again, QUrl may have been mistreating us -- Chris@355: // e.g. dropping a part that looks like query data Chris@355: m_localFilename = m_url.toString(); Chris@355: literal = true; Chris@355: if (!QFileInfo(m_localFilename).exists()) { Chris@355: m_lastStatus = 404; Chris@355: } Chris@355: } Chris@316: } Chris@355: Chris@316: m_done = true; Chris@316: return; Chris@316: } Chris@316: Chris@316: if (createCacheFile()) { Chris@327: #ifdef DEBUG_FILE_SOURCE Chris@706: std::cerr << "FileSource::init: Already have this one" << std::endl; Chris@327: #endif Chris@316: m_ok = true; Chris@316: if (!QFileInfo(m_localFilename).exists()) { Chris@316: m_lastStatus = 404; Chris@316: } else { Chris@316: m_lastStatus = 200; Chris@316: } Chris@316: m_done = true; Chris@316: return; Chris@316: } Chris@316: Chris@208: if (m_localFilename == "") return; Chris@706: Chris@208: m_localFile = new QFile(m_localFilename); Chris@208: m_localFile->open(QFile::WriteOnly); Chris@208: Chris@706: if (isResource()) { Chris@706: Chris@706: // Absent resource file case was dealt with at the top -- this Chris@706: // is the successful case Chris@706: Chris@706: QString resourceFileName = m_url.toString(); Chris@706: resourceFileName.replace(QRegExp("^qrc:"), ":"); Chris@706: QFile resourceFile(resourceFileName); Chris@706: resourceFile.open(QFile::ReadOnly); Chris@706: QByteArray ba(resourceFile.readAll()); Chris@706: Chris@706: #ifdef DEBUG_FILE_SOURCE Chris@706: std::cerr << "Copying " << ba.size() << " bytes from resource file to cache file" << std::endl; Chris@706: #endif Chris@706: Chris@706: qint64 written = m_localFile->write(ba); Chris@706: m_localFile->close(); Chris@706: delete m_localFile; Chris@706: m_localFile = 0; Chris@706: Chris@706: if (written != ba.size()) { Chris@706: #ifdef DEBUG_FILE_SOURCE Chris@706: std::cerr << "Copy failed (wrote " << written << " bytes)" << std::endl; Chris@706: #endif Chris@706: m_ok = false; Chris@706: return; Chris@706: } else { Chris@706: m_ok = true; Chris@706: m_lastStatus = 200; Chris@706: m_done = true; Chris@706: } Chris@706: Chris@706: } else { Chris@706: Chris@706: QString scheme = m_url.scheme().toLower(); Chris@316: Chris@327: #ifdef DEBUG_FILE_SOURCE Chris@706: std::cerr << "FileSource::init: Don't have local copy of \"" Chris@706: << m_url.toString() << "\", retrieving" << std::endl; Chris@327: #endif Chris@208: Chris@762: if (scheme == "http" || scheme == "https" || scheme == "ftp") { Chris@762: initRemote(); Chris@520: #ifdef DEBUG_FILE_SOURCE Chris@762: std::cerr << "FileSource: initRemote returned" << std::endl; Chris@520: #endif Chris@706: } else { Chris@706: m_remote = false; Chris@706: m_ok = false; Chris@706: } Chris@208: } Chris@208: Chris@208: if (m_ok) { Chris@316: Chris@316: QMutexLocker locker(&m_mapMutex); Chris@316: Chris@316: if (m_refCountMap[m_url] > 0) { Chris@316: // someone else has been doing the same thing at the same time, Chris@316: // but has got there first Chris@316: cleanup(); Chris@316: m_refCountMap[m_url]++; Chris@327: #ifdef DEBUG_FILE_SOURCE Chris@706: std::cerr << "FileSource::init: Another FileSource has got there first, abandoning our download and using theirs" << std::endl; Chris@327: #endif Chris@316: m_localFilename = m_remoteLocalMap[m_url]; Chris@316: m_refCounted = true; Chris@316: m_ok = true; Chris@316: if (!QFileInfo(m_localFilename).exists()) { Chris@316: m_lastStatus = 404; Chris@316: } Chris@316: m_done = true; Chris@316: return; Chris@316: } Chris@304: Chris@304: m_remoteLocalMap[m_url] = m_localFilename; Chris@304: m_refCountMap[m_url]++; Chris@316: m_refCounted = true; Chris@304: Chris@706: if (m_reporter && !m_done) { Chris@392: m_reporter->setMessage Chris@392: (tr("Downloading %1...").arg(m_url.toString())); Chris@392: connect(m_reporter, SIGNAL(cancelled()), this, SLOT(cancelled())); Chris@357: connect(this, SIGNAL(progress(int)), Chris@392: m_reporter, SLOT(setProgress(int))); Chris@316: } Chris@208: } Chris@208: } Chris@208: Chris@316: void Chris@762: FileSource::initRemote() Chris@208: { Chris@316: m_ok = true; Chris@316: Chris@762: QNetworkRequest req; Chris@762: req.setUrl(m_url); Chris@762: Chris@762: if (m_preferredContentType != "") { Chris@520: #ifdef DEBUG_FILE_SOURCE Chris@520: std::cerr << "FileSource: indicating preferred content type of \"" Chris@686: << m_preferredContentType << "\"" << std::endl; Chris@520: #endif Chris@762: req.setRawHeader Chris@762: ("Accept", Chris@762: QString("%1, */*").arg(m_preferredContentType).toLatin1()); Chris@520: } Chris@316: Chris@762: m_reply = nm.get(req); Chris@762: Chris@762: connect(m_reply, SIGNAL(readyRead()), Chris@762: this, SLOT(readyRead())); Chris@762: connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), Chris@762: this, SLOT(replyFailed(QNetworkReply::NetworkError))); Chris@762: connect(m_reply, SIGNAL(finished()), Chris@762: this, SLOT(replyFinished())); Chris@764: connect(m_reply, SIGNAL(metaDataChanged()), Chris@764: this, SLOT(metaDataChanged())); Chris@762: connect(m_reply, SIGNAL(downloadProgress(qint64, qint64)), Chris@762: this, SLOT(downloadProgress(qint64, qint64))); Chris@211: } Chris@211: Chris@211: void Chris@317: FileSource::cleanup() Chris@211: { Chris@470: if (m_done) { Chris@470: delete m_localFile; // does not actually delete the file Chris@470: m_localFile = 0; Chris@470: } Chris@211: m_done = true; Chris@762: if (m_reply) { Chris@762: QNetworkReply *r = m_reply; Chris@762: m_reply = 0; Chris@762: r->abort(); Chris@762: r->deleteLater(); Chris@214: } Chris@470: if (m_localFile) { Chris@470: delete m_localFile; // does not actually delete the file Chris@470: m_localFile = 0; Chris@470: } Chris@208: } Chris@208: Chris@208: bool Chris@317: FileSource::isRemote(QString fileOrUrl) Chris@304: { Chris@342: // Note that a "scheme" with length 1 is probably a DOS drive letter Chris@316: QString scheme = QUrl(fileOrUrl).scheme().toLower(); Chris@342: if (scheme == "" || scheme == "file" || scheme.length() == 1) return false; Chris@342: return true; Chris@304: } Chris@304: Chris@304: bool Chris@317: FileSource::canHandleScheme(QUrl url) Chris@208: { Chris@342: // Note that a "scheme" with length 1 is probably a DOS drive letter Chris@208: QString scheme = url.scheme().toLower(); Chris@762: return (scheme == "http" || scheme == "https" || Chris@762: scheme == "ftp" || scheme == "file" || scheme == "qrc" || Chris@706: scheme == "" || scheme.length() == 1); Chris@208: } Chris@208: Chris@210: bool Chris@317: FileSource::isAvailable() Chris@210: { Chris@316: waitForStatus(); Chris@211: bool available = true; Chris@211: if (!m_ok) available = false; Chris@211: else available = (m_lastStatus / 100 == 2); Chris@327: #ifdef DEBUG_FILE_SOURCE Chris@762: std::cerr << "FileSource::isAvailable: " << (available ? "yes" : "no") Chris@762: << std::endl; Chris@327: #endif Chris@211: return available; Chris@210: } Chris@210: Chris@208: void Chris@317: FileSource::waitForStatus() Chris@316: { Chris@316: while (m_ok && (!m_done && m_lastStatus == 0)) { Chris@316: // std::cerr << "waitForStatus: processing (last status " << m_lastStatus << ")" << std::endl; Chris@392: QCoreApplication::processEvents(); Chris@316: } Chris@316: } Chris@316: Chris@316: void Chris@317: FileSource::waitForData() Chris@208: { Chris@211: while (m_ok && !m_done) { Chris@762: // std::cerr << "FileSource::waitForData: calling QApplication::processEvents" << std::endl; Chris@392: QCoreApplication::processEvents(); Chris@497: usleep(10000); Chris@208: } Chris@208: } Chris@208: Chris@316: void Chris@317: FileSource::setLeaveLocalFile(bool leave) Chris@316: { Chris@316: m_leaveLocalFile = leave; Chris@316: } Chris@316: Chris@208: bool Chris@317: FileSource::isOK() const Chris@208: { Chris@208: return m_ok; Chris@208: } Chris@208: Chris@208: bool Chris@317: FileSource::isDone() const Chris@208: { Chris@208: return m_done; Chris@208: } Chris@208: Chris@316: bool Chris@706: FileSource::isResource() const Chris@706: { Chris@706: return m_resource; Chris@706: } Chris@706: Chris@706: bool Chris@317: FileSource::isRemote() const Chris@316: { Chris@316: return m_remote; Chris@316: } Chris@316: Chris@316: QString Chris@317: FileSource::getLocation() const Chris@316: { Chris@316: return m_url.toString(); Chris@316: } Chris@316: Chris@208: QString Chris@317: FileSource::getLocalFilename() const Chris@208: { Chris@208: return m_localFilename; Chris@208: } Chris@208: Chris@208: QString Chris@678: FileSource::getBasename() const Chris@678: { Chris@678: return QFileInfo(m_localFilename).fileName(); Chris@678: } Chris@678: Chris@678: QString Chris@317: FileSource::getContentType() const Chris@316: { Chris@316: return m_contentType; Chris@316: } Chris@316: Chris@316: QString Chris@317: FileSource::getExtension() const Chris@316: { Chris@316: if (m_localFilename != "") { Chris@316: return QFileInfo(m_localFilename).suffix().toLower(); Chris@316: } else { Chris@316: return QFileInfo(m_url.toLocalFile()).suffix().toLower(); Chris@316: } Chris@316: } Chris@316: Chris@316: QString Chris@317: FileSource::getErrorString() const Chris@208: { Chris@208: return m_errorString; Chris@208: } Chris@208: Chris@208: void Chris@762: FileSource::readyRead() Chris@208: { Chris@762: m_localFile->write(m_reply->readAll()); Chris@208: } Chris@208: Chris@208: void Chris@764: FileSource::metaDataChanged() Chris@210: { Chris@520: #ifdef DEBUG_FILE_SOURCE Chris@764: std::cerr << "FileSource::metaDataChanged" << std::endl; Chris@520: #endif Chris@497: Chris@762: if (!m_reply) { Chris@764: std::cerr << "WARNING: FileSource::metaDataChanged() called without a reply object being known to us" << std::endl; Chris@762: return; Chris@762: } Chris@762: Chris@762: int status = Chris@762: m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); Chris@762: Chris@762: if (status / 100 == 3) { Chris@762: QString location = m_reply->header Chris@762: (QNetworkRequest::LocationHeader).toString(); Chris@496: #ifdef DEBUG_FILE_SOURCE Chris@764: std::cerr << "FileSource::metaDataChanged: redirect to \"" Chris@762: << location << "\" received" << std::endl; Chris@496: #endif Chris@496: if (location != "") { Chris@496: QUrl newUrl(location); Chris@496: if (newUrl != m_url) { Chris@497: cleanup(); Chris@497: deleteCacheFile(); Chris@529: #ifdef DEBUG_FILE_SOURCE Chris@529: decCount(m_url.toString()); Chris@529: incCount(newUrl.toString()); Chris@529: #endif Chris@496: m_url = newUrl; Chris@497: m_localFile = 0; Chris@496: m_lastStatus = 0; Chris@497: m_done = false; Chris@497: m_refCounted = false; Chris@496: init(); Chris@496: return; Chris@496: } Chris@496: } Chris@496: } Chris@497: Chris@762: m_lastStatus = status; Chris@210: if (m_lastStatus / 100 >= 4) { Chris@210: m_errorString = QString("%1 %2") Chris@762: .arg(status) Chris@762: .arg(m_reply->attribute Chris@762: (QNetworkRequest::HttpReasonPhraseAttribute).toString()); Chris@327: #ifdef DEBUG_FILE_SOURCE Chris@764: std::cerr << "FileSource::metaDataChanged: " Chris@762: << m_errorString << std::endl; Chris@327: #endif Chris@211: } else { Chris@327: #ifdef DEBUG_FILE_SOURCE Chris@764: std::cerr << "FileSource::metaDataChanged: " Chris@762: << m_lastStatus << std::endl; Chris@327: #endif Chris@762: m_contentType = Chris@762: m_reply->header(QNetworkRequest::ContentTypeHeader).toString(); Chris@325: } Chris@325: emit statusAvailable(); Chris@210: } Chris@210: Chris@210: void Chris@762: FileSource::downloadProgress(qint64 done, qint64 total) Chris@208: { Chris@208: int percent = int((double(done) / double(total)) * 100.0 - 0.1); Chris@208: emit progress(percent); Chris@210: } Chris@210: Chris@210: void Chris@317: FileSource::cancelled() Chris@210: { Chris@210: m_done = true; Chris@316: cleanup(); Chris@316: Chris@210: m_ok = false; Chris@210: m_errorString = tr("Download cancelled"); Chris@208: } Chris@208: Chris@208: void Chris@762: FileSource::replyFinished() Chris@208: { Chris@327: emit progress(100); Chris@327: Chris@327: #ifdef DEBUG_FILE_SOURCE Chris@762: std::cerr << "FileSource::replyFinished()" << std::endl; Chris@327: #endif Chris@211: Chris@211: if (m_done) return; Chris@211: Chris@762: bool error = (m_lastStatus / 100 >= 4); Chris@210: Chris@211: cleanup(); Chris@208: Chris@211: if (!error) { Chris@208: QFileInfo fi(m_localFilename); Chris@208: if (!fi.exists()) { Chris@208: m_errorString = tr("Failed to create local file %1").arg(m_localFilename); Chris@211: error = true; Chris@208: } else if (fi.size() == 0) { Chris@208: m_errorString = tr("File contains no data!"); Chris@211: error = true; Chris@208: } Chris@208: } Chris@211: Chris@211: if (error) { Chris@327: #ifdef DEBUG_FILE_SOURCE Chris@469: std::cerr << "FileSource::done: error is " << error << ", deleting cache file" << std::endl; Chris@327: #endif Chris@316: deleteCacheFile(); Chris@211: } Chris@211: Chris@211: m_ok = !error; Chris@520: if (m_localFile) m_localFile->flush(); Chris@211: m_done = true; Chris@304: emit ready(); Chris@211: } Chris@211: Chris@211: void Chris@763: FileSource::replyFailed(QNetworkReply::NetworkError) Chris@763: { Chris@763: emit progress(100); Chris@763: m_errorString = m_reply->errorString(); Chris@763: m_ok = false; Chris@763: m_done = true; Chris@763: cleanup(); Chris@763: emit ready(); Chris@763: } Chris@763: Chris@763: void Chris@317: FileSource::deleteCacheFile() Chris@211: { Chris@327: #ifdef DEBUG_FILE_SOURCE Chris@762: std::cerr << "FileSource::deleteCacheFile(\"" << m_localFilename << "\")" << std::endl; Chris@327: #endif Chris@211: Chris@211: cleanup(); Chris@211: Chris@316: if (m_localFilename == "") { Chris@316: return; Chris@316: } Chris@211: Chris@316: if (!isRemote()) { Chris@327: #ifdef DEBUG_FILE_SOURCE Chris@316: std::cerr << "not a cache file" << std::endl; Chris@327: #endif Chris@316: return; Chris@316: } Chris@316: Chris@316: if (m_refCounted) { Chris@304: Chris@304: QMutexLocker locker(&m_mapMutex); Chris@316: m_refCounted = false; Chris@304: Chris@304: if (m_refCountMap[m_url] > 0) { Chris@304: m_refCountMap[m_url]--; Chris@327: #ifdef DEBUG_FILE_SOURCE Chris@316: std::cerr << "reduced ref count to " << m_refCountMap[m_url] << std::endl; Chris@327: #endif Chris@304: if (m_refCountMap[m_url] > 0) { Chris@304: m_done = true; Chris@304: return; Chris@304: } Chris@304: } Chris@304: } Chris@304: Chris@211: m_fileCreationMutex.lock(); Chris@211: Chris@211: if (!QFile(m_localFilename).remove()) { Chris@469: #ifdef DEBUG_FILE_SOURCE Chris@686: std::cerr << "FileSource::deleteCacheFile: ERROR: Failed to delete file \"" << m_localFilename << "\"" << std::endl; Chris@469: #endif Chris@211: } else { Chris@327: #ifdef DEBUG_FILE_SOURCE Chris@762: std::cerr << "FileSource::deleteCacheFile: Deleted cache file \"" << m_localFilename << "\"" << std::endl; Chris@327: #endif Chris@211: m_localFilename = ""; Chris@211: } Chris@211: Chris@211: m_fileCreationMutex.unlock(); Chris@211: Chris@208: m_done = true; Chris@208: } Chris@208: Chris@316: bool Chris@317: FileSource::createCacheFile() Chris@208: { Chris@316: { Chris@316: QMutexLocker locker(&m_mapMutex); Chris@316: Chris@327: #ifdef DEBUG_FILE_SOURCE Chris@762: std::cerr << "FileSource::createCacheFile: refcount is " << m_refCountMap[m_url] << std::endl; Chris@327: #endif Chris@316: Chris@316: if (m_refCountMap[m_url] > 0) { Chris@316: m_refCountMap[m_url]++; Chris@316: m_localFilename = m_remoteLocalMap[m_url]; Chris@327: #ifdef DEBUG_FILE_SOURCE Chris@316: std::cerr << "raised it to " << m_refCountMap[m_url] << std::endl; Chris@327: #endif Chris@316: m_refCounted = true; Chris@316: return true; Chris@316: } Chris@316: } Chris@316: Chris@208: QDir dir; Chris@208: try { Chris@208: dir = TempDirectory::getInstance()->getSubDirectoryPath("download"); Chris@208: } catch (DirectoryCreationFailed f) { Chris@327: #ifdef DEBUG_FILE_SOURCE Chris@317: std::cerr << "FileSource::createCacheFile: ERROR: Failed to create temporary directory: " << f.what() << std::endl; Chris@327: #endif Chris@208: return ""; Chris@208: } Chris@208: Chris@316: QString filepart = m_url.path().section('/', -1, -1, Chris@316: QString::SectionSkipEmpty); Chris@208: Chris@457: QString extension = ""; Chris@457: if (filepart.contains('.')) extension = filepart.section('.', -1); Chris@457: Chris@208: QString base = filepart; Chris@208: if (extension != "") { Chris@208: base = base.left(base.length() - extension.length() - 1); Chris@208: } Chris@208: if (base == "") base = "remote"; Chris@208: Chris@208: QString filename; Chris@208: Chris@208: if (extension == "") { Chris@208: filename = base; Chris@208: } else { Chris@208: filename = QString("%1.%2").arg(base).arg(extension); Chris@208: } Chris@208: Chris@208: QString filepath(dir.filePath(filename)); Chris@208: Chris@327: #ifdef DEBUG_FILE_SOURCE Chris@706: std::cerr << "FileSource::createCacheFile: URL is \"" << m_url.toString() << "\", dir is \"" << dir.path() << "\", base \"" << base << "\", extension \"" << extension << "\", filebase \"" << filename << "\", filename \"" << filepath << "\"" << std::endl; Chris@327: #endif Chris@208: Chris@316: QMutexLocker fcLocker(&m_fileCreationMutex); Chris@316: Chris@208: ++m_count; Chris@208: Chris@208: if (QFileInfo(filepath).exists() || Chris@208: !QFile(filepath).open(QFile::WriteOnly)) { Chris@208: Chris@327: #ifdef DEBUG_FILE_SOURCE Chris@317: std::cerr << "FileSource::createCacheFile: Failed to create local file \"" Chris@686: << filepath << "\" for URL \"" Chris@686: << m_url.toString() << "\" (or file already exists): appending suffix instead" << std::endl; Chris@327: #endif Chris@208: Chris@208: if (extension == "") { Chris@208: filename = QString("%1_%2").arg(base).arg(m_count); Chris@208: } else { Chris@208: filename = QString("%1_%2.%3").arg(base).arg(m_count).arg(extension); Chris@208: } Chris@208: filepath = dir.filePath(filename); Chris@208: Chris@208: if (QFileInfo(filepath).exists() || Chris@208: !QFile(filepath).open(QFile::WriteOnly)) { Chris@208: Chris@327: #ifdef DEBUG_FILE_SOURCE Chris@317: std::cerr << "FileSource::createCacheFile: ERROR: Failed to create local file \"" Chris@686: << filepath << "\" for URL \"" Chris@686: << m_url.toString() << "\" (or file already exists)" << std::endl; Chris@327: #endif Chris@208: Chris@208: return ""; Chris@208: } Chris@208: } Chris@208: Chris@327: #ifdef DEBUG_FILE_SOURCE Chris@706: std::cerr << "FileSource::createCacheFile: url " Chris@686: << m_url.toString() << " -> local filename " Chris@706: << filepath << std::endl; Chris@327: #endif Chris@316: Chris@316: m_localFilename = filepath; Chris@208: Chris@316: return false; Chris@208: } Chris@327: