From 0d888eea85a1d0adc07b2d8e868c60c450b73ed2 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 14 Mar 2019 13:20:52 +0400 Subject: [PATCH] Fix local cache time limit setting storing. Fixes #5611. --- .../SourceFiles/boxes/local_storage_box.cpp | 6 +-- Telegram/SourceFiles/storage/localstorage.cpp | 53 ++++++++++--------- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/Telegram/SourceFiles/boxes/local_storage_box.cpp b/Telegram/SourceFiles/boxes/local_storage_box.cpp index 22192a63dd..e9f7fb86bb 100644 --- a/Telegram/SourceFiles/boxes/local_storage_box.cpp +++ b/Telegram/SourceFiles/boxes/local_storage_box.cpp @@ -557,9 +557,9 @@ void LocalStorageBox::setupLimits(not_null container) { TimeLimit, LimitToValue(_timeLimit), [=](not_null label, size_type limit) { - const auto text = TimeLimitText(ValueToLimit(limit)); + _timeLimit = ValueToLimit(limit); + const auto text = TimeLimitText(_timeLimit); label->setText(lng_local_storage_time_limit(lt_limit, text)); - _timeLimit = limit; limitsChanged(); }); } @@ -594,7 +594,7 @@ void LocalStorageBox::save() { update.totalTimeLimit = _timeLimit; auto updateBig = Storage::Cache::Database::SettingsUpdate(); updateBig.totalSizeLimit = _mediaSizeLimit; - update.totalTimeLimit = _timeLimit; + updateBig.totalTimeLimit = _timeLimit; Local::updateCacheSettings(update, updateBig); Auth().data().cache().updateSettings(update); closeBox(); diff --git a/Telegram/SourceFiles/storage/localstorage.cpp b/Telegram/SourceFiles/storage/localstorage.cpp index 626ed62977..7f26572831 100644 --- a/Telegram/SourceFiles/storage/localstorage.cpp +++ b/Telegram/SourceFiles/storage/localstorage.cpp @@ -186,9 +186,9 @@ struct FileReadDescriptor { QDataStream stream; ~FileReadDescriptor() { if (version) { - stream.setDevice(0); + stream.setDevice(nullptr); if (buffer.isOpen()) buffer.close(); - buffer.setBuffer(0); + buffer.setBuffer(nullptr); } } }; @@ -212,9 +212,9 @@ struct EncryptedDescriptor { QBuffer buffer; QDataStream stream; void finish() { - if (stream.device()) stream.setDevice(0); + if (stream.device()) stream.setDevice(nullptr); if (buffer.isOpen()) buffer.close(); - buffer.setBuffer(0); + buffer.setBuffer(nullptr); } ~EncryptedDescriptor() { finish(); @@ -305,7 +305,7 @@ struct FileWriteDescriptor { void finish() { if (!file.isOpen()) return; - stream.setDevice(0); + stream.setDevice(nullptr); md5.feed(&dataSize, sizeof(dataSize)); qint32 version = AppVersion; @@ -473,9 +473,9 @@ bool readEncryptedFile(FileReadDescriptor &result, const QString &name, FileOpti EncryptedDescriptor data; if (!decryptLocal(data, encrypted, key)) { - result.stream.setDevice(0); + result.stream.setDevice(nullptr); if (result.buffer.isOpen()) result.buffer.close(); - result.buffer.setBuffer(0); + result.buffer.setBuffer(nullptr); result.data = QByteArray(); result.version = 0; return false; @@ -672,6 +672,14 @@ qint32 _cacheTotalTimeLimit = Database::Settings().totalTimeLimit; qint64 _cacheBigFileTotalSizeLimit = Database::Settings().totalSizeLimit; qint32 _cacheBigFileTotalTimeLimit = Database::Settings().totalTimeLimit; +bool NoTimeLimit(qint32 storedLimitValue) { + // This is a workaround for a bug in storing the cache time limit. + // See https://github.com/telegramdesktop/tdesktop/issues/5611 + return !storedLimitValue + || (storedLimitValue == qint32(std::numeric_limits::max())) + || (storedLimitValue == qint32(std::numeric_limits::max())); +} + FileKey _exportSettingsKey = 0; FileKey _savedPeersKey = 0; @@ -1078,13 +1086,13 @@ bool _readSetting(quint32 blockId, QDataStream &stream, int version, ReadSetting stream >> size >> time; if (!_checkStreamStatus(stream) || size <= Database::Settings().maxDataSize - || time < 0) { + || (!NoTimeLimit(time) && time < 0)) { return false; } _cacheTotalSizeLimit = size; - _cacheTotalTimeLimit = time; + _cacheTotalTimeLimit = NoTimeLimit(time) ? 0 : time; _cacheBigFileTotalSizeLimit = size; - _cacheBigFileTotalTimeLimit = time; + _cacheBigFileTotalTimeLimit = NoTimeLimit(time) ? 0 : time; } break; case dbiCacheSettings: { @@ -1094,15 +1102,15 @@ bool _readSetting(quint32 blockId, QDataStream &stream, int version, ReadSetting if (!_checkStreamStatus(stream) || size <= Database::Settings().maxDataSize || sizeBig <= Database::Settings().maxDataSize - || time < 0 - || timeBig < 0) { + || (!NoTimeLimit(time) && time < 0) + || (!NoTimeLimit(timeBig) && timeBig < 0)) { return false; } _cacheTotalSizeLimit = size; - _cacheTotalTimeLimit = time; + _cacheTotalTimeLimit = NoTimeLimit(time) ? 0 : time; _cacheBigFileTotalSizeLimit = sizeBig; - _cacheBigFileTotalTimeLimit = timeBig; + _cacheBigFileTotalTimeLimit = NoTimeLimit(timeBig) ? 0 : timeBig; } break; case dbiAnimationsDisabled: { @@ -3269,21 +3277,18 @@ public: , _data(doc->data()) , _wavemax(0) { if (_data.isEmpty() && !_loc.accessEnable()) { - _doc = 0; + _doc = nullptr; } } - void process() { + void process() override { if (!_doc) return; _waveform = audioCountWaveform(_loc, _data); - uchar wavemax = 0; - for (int32 i = 0, l = _waveform.size(); i < l; ++i) { - uchar waveat = _waveform.at(i); - if (wavemax < waveat) wavemax = waveat; - } - _wavemax = wavemax; + _wavemax = _waveform.empty() + ? char(0) + : *ranges::max_element(_waveform); } - void finish() { + void finish() override { if (const auto voice = _doc ? _doc->voice() : nullptr) { if (!_waveform.isEmpty()) { voice->waveform = _waveform; @@ -3300,7 +3305,7 @@ public: Auth().data().requestDocumentViewRepaint(_doc); } } - virtual ~CountWaveformTask() { + ~CountWaveformTask() { if (_data.isEmpty() && _doc) { _loc.accessDisable(); }