Remove CancelledFileLoader hack.

This commit is contained in:
John Preston 2019-04-30 18:32:49 +04:00
parent 274fed3cb0
commit 46d8d9f97a
3 changed files with 48 additions and 56 deletions

View File

@ -437,9 +437,6 @@ public slots:
}; };
static FileLoader * const CancelledFileLoader = SharedMemoryLocation<FileLoader, 0>();
static mtpFileLoader * const CancelledMtpFileLoader = static_cast<mtpFileLoader*>(CancelledFileLoader);
static webFileLoader * const CancelledWebFileLoader = static_cast<webFileLoader*>(CancelledFileLoader);
static WebLoadManager * const FinishedWebLoadManager = SharedMemoryLocation<WebLoadManager, 0>(); static WebLoadManager * const FinishedWebLoadManager = SharedMemoryLocation<WebLoadManager, 0>();
void stopWebLoadManager(); void stopWebLoadManager();

View File

@ -271,13 +271,14 @@ QByteArray LocalFileSource::bytesForCache() {
} }
QImage RemoteSource::takeLoaded() { QImage RemoteSource::takeLoaded() {
if (!loaderValid() || !_loader->finished()) { if (!_loader || !_loader->finished()) {
return QImage(); return QImage();
} }
auto data = _loader->imageData(shrinkBox()); auto data = _loader->imageData(shrinkBox());
if (data.isNull()) { if (data.isNull()) {
destroyLoader(CancelledFileLoader); _cancelled = true;
destroyLoader();
return QImage(); return QImage();
} }
@ -288,34 +289,33 @@ QImage RemoteSource::takeLoaded() {
return data; return data;
} }
bool RemoteSource::loaderValid() const { void RemoteSource::destroyLoader() {
return _loader && !cancelled(); if (!_loader) {
} return;
}
void RemoteSource::destroyLoader(FileLoader *newValue) { const auto loader = base::take(_loader);
Expects(loaderValid());
const auto loader = std::exchange(_loader, newValue);
if (cancelled()) { if (cancelled()) {
loader->cancel(); loader->cancel();
} }
loader->stop(); loader->stop();
delete loader;
} }
void RemoteSource::loadLocal() { void RemoteSource::loadLocal() {
if (loaderValid()) { if (_loader) {
return; return;
} }
_loader = createLoader(Data::FileOrigin(), LoadFromLocalOnly, true); _loader = createLoader(Data::FileOrigin(), LoadFromLocalOnly, true);
if (_loader) _loader->start(); if (_loader) {
_loader->start();
}
} }
void RemoteSource::setImageBytes(const QByteArray &bytes) { void RemoteSource::setImageBytes(const QByteArray &bytes) {
if (bytes.isEmpty()) { if (bytes.isEmpty()) {
return; return;
} else if (loaderValid()) { } else if (_loader) {
unload(); unload();
} }
_loader = createLoader({}, LoadFromLocalOnly, true); _loader = createLoader({}, LoadFromLocalOnly, true);
@ -334,7 +334,7 @@ void RemoteSource::setImageBytes(const QByteArray &bytes) {
} }
bool RemoteSource::loading() { bool RemoteSource::loading() {
return loaderValid(); return (_loader != nullptr);
} }
void RemoteSource::automaticLoad( void RemoteSource::automaticLoad(
@ -358,60 +358,55 @@ void RemoteSource::automaticLoad(
loadFromCloud ? LoadFromCloudOrLocal : LoadFromLocalOnly, loadFromCloud ? LoadFromCloudOrLocal : LoadFromLocalOnly,
true); true);
} }
if (loaderValid()) { if (_loader) {
_loader->start(); _loader->start();
} }
} }
void RemoteSource::automaticLoadSettingsChanged() { void RemoteSource::automaticLoadSettingsChanged() {
if (_loader == CancelledFileLoader) { _cancelled = false;
_loader = nullptr;
}
} }
void RemoteSource::load(Data::FileOrigin origin) { void RemoteSource::load(Data::FileOrigin origin) {
if (!_loader) { if (!_loader) {
_loader = createLoader(origin, LoadFromCloudOrLocal, false); _loader = createLoader(origin, LoadFromCloudOrLocal, false);
} }
if (loaderValid()) { if (_loader) {
_loader->start(); _loader->start();
} }
} }
bool RemoteSource::cancelled() const { bool RemoteSource::cancelled() const {
return (_loader == CancelledFileLoader); return _cancelled;
} }
void RemoteSource::loadEvenCancelled(Data::FileOrigin origin) { void RemoteSource::loadEvenCancelled(Data::FileOrigin origin) {
if (cancelled()) { _cancelled = false;
_loader = nullptr;
}
return load(origin); return load(origin);
} }
bool RemoteSource::displayLoading() { bool RemoteSource::displayLoading() {
return loaderValid() return _loader && (!_loader->loadingLocal() || !_loader->autoLoading());
&& (!_loader->loadingLocal() || !_loader->autoLoading());
} }
void RemoteSource::cancel() { void RemoteSource::cancel() {
if (!loaderValid()) return; if (!_loader) {
return;
destroyLoader(CancelledFileLoader); }
_cancelled = true;
destroyLoader();
} }
void RemoteSource::unload() { void RemoteSource::unload() {
if (loaderValid()) { base::take(_loader);
delete base::take(_loader);
}
} }
float64 RemoteSource::progress() { float64 RemoteSource::progress() {
return loaderValid() ? _loader->currentProgress() : 0.; return _loader ? _loader->currentProgress() : 0.;
} }
int RemoteSource::loadOffset() { int RemoteSource::loadOffset() {
return loaderValid() ? _loader->currentOffset() : 0; return _loader ? _loader->currentOffset() : 0;
} }
RemoteSource::~RemoteSource() { RemoteSource::~RemoteSource() {
@ -484,12 +479,12 @@ QSize StorageSource::shrinkBox() const {
return QSize(); return QSize();
} }
FileLoader *StorageSource::createLoader( std::unique_ptr<FileLoader> StorageSource::createLoader(
Data::FileOrigin origin, Data::FileOrigin origin,
LoadFromCloudSetting fromCloud, LoadFromCloudSetting fromCloud,
bool autoLoading) { bool autoLoading) {
return _location.valid() return _location.valid()
? new mtpFileLoader( ? std::make_unique<mtpFileLoader>(
_location.file(), _location.file(),
origin, origin,
UnknownFileLocation, UnknownFileLocation,
@ -554,18 +549,18 @@ QSize WebCachedSource::shrinkBox() const {
return _box; return _box;
} }
FileLoader *WebCachedSource::createLoader( std::unique_ptr<FileLoader> WebCachedSource::createLoader(
Data::FileOrigin origin, Data::FileOrigin origin,
LoadFromCloudSetting fromCloud, LoadFromCloudSetting fromCloud,
bool autoLoading) { bool autoLoading) {
return _location.isNull() return !_location.isNull()
? nullptr ? std::make_unique<mtpFileLoader>(
: new mtpFileLoader(
_location, _location,
_size, _size,
fromCloud, fromCloud,
autoLoading, autoLoading,
Data::kImageCacheTag); Data::kImageCacheTag)
: nullptr;
} }
GeoPointSource::GeoPointSource(const GeoPointLocation &location) GeoPointSource::GeoPointSource(const GeoPointLocation &location)
@ -604,11 +599,11 @@ QSize GeoPointSource::shrinkBox() const {
return QSize(); return QSize();
} }
FileLoader *GeoPointSource::createLoader( std::unique_ptr<FileLoader> GeoPointSource::createLoader(
Data::FileOrigin origin, Data::FileOrigin origin,
LoadFromCloudSetting fromCloud, LoadFromCloudSetting fromCloud,
bool autoLoading) { bool autoLoading) {
return new mtpFileLoader( return std::make_unique<mtpFileLoader>(
_location, _location,
_size, _size,
fromCloud, fromCloud,
@ -740,11 +735,11 @@ QSize WebUrlSource::shrinkBox() const {
return _box; return _box;
} }
FileLoader *WebUrlSource::createLoader( std::unique_ptr<FileLoader> WebUrlSource::createLoader(
Data::FileOrigin origin, Data::FileOrigin origin,
LoadFromCloudSetting fromCloud, LoadFromCloudSetting fromCloud,
bool autoLoading) { bool autoLoading) {
return new webFileLoader( return std::make_unique<webFileLoader>(
_url, _url,
QString(), QString(),
fromCloud, fromCloud,

View File

@ -142,7 +142,7 @@ protected:
// If after loading the image we need to shrink it to fit into a // If after loading the image we need to shrink it to fit into a
// specific size, you can return this size here. // specific size, you can return this size here.
virtual QSize shrinkBox() const = 0; virtual QSize shrinkBox() const = 0;
virtual FileLoader *createLoader( virtual std::unique_ptr<FileLoader> createLoader(
Data::FileOrigin origin, Data::FileOrigin origin,
LoadFromCloudSetting fromCloud, LoadFromCloudSetting fromCloud,
bool autoLoading) = 0; bool autoLoading) = 0;
@ -150,11 +150,11 @@ protected:
void loadLocal(); void loadLocal();
private: private:
bool loaderValid() const;
bool cancelled() const; bool cancelled() const;
void destroyLoader(FileLoader *newValue = nullptr); void destroyLoader();
FileLoader *_loader = nullptr; std::unique_ptr<FileLoader> _loader;
bool _cancelled = false;
}; };
@ -176,7 +176,7 @@ public:
protected: protected:
QSize shrinkBox() const override; QSize shrinkBox() const override;
FileLoader *createLoader( std::unique_ptr<FileLoader> createLoader(
Data::FileOrigin origin, Data::FileOrigin origin,
LoadFromCloudSetting fromCloud, LoadFromCloudSetting fromCloud,
bool autoLoading) override; bool autoLoading) override;
@ -204,7 +204,7 @@ public:
protected: protected:
QSize shrinkBox() const override; QSize shrinkBox() const override;
FileLoader *createLoader( std::unique_ptr<FileLoader> createLoader(
Data::FileOrigin origin, Data::FileOrigin origin,
LoadFromCloudSetting fromCloud, LoadFromCloudSetting fromCloud,
bool autoLoading) override; bool autoLoading) override;
@ -230,7 +230,7 @@ public:
protected: protected:
QSize shrinkBox() const override; QSize shrinkBox() const override;
FileLoader *createLoader( std::unique_ptr<FileLoader> createLoader(
Data::FileOrigin origin, Data::FileOrigin origin,
LoadFromCloudSetting fromCloud, LoadFromCloudSetting fromCloud,
bool autoLoading) override; bool autoLoading) override;
@ -289,7 +289,7 @@ public:
protected: protected:
QSize shrinkBox() const override; QSize shrinkBox() const override;
FileLoader *createLoader( std::unique_ptr<FileLoader> createLoader(
Data::FileOrigin origin, Data::FileOrigin origin,
LoadFromCloudSetting fromCloud, LoadFromCloudSetting fromCloud,
bool autoLoading) override; bool autoLoading) override;