From f8a17bd9c92f84da4dff11528b94f670b645949f Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Wed, 30 Nov 2022 03:05:55 +0300 Subject: [PATCH] Moved tmp string for temporary folder to single place. --- .../SourceFiles/boxes/download_path_box.cpp | 20 ++++++++++++++----- .../SourceFiles/boxes/download_path_box.h | 9 +-------- Telegram/SourceFiles/core/application.cpp | 2 +- Telegram/SourceFiles/core/file_utilities.cpp | 5 +++++ Telegram/SourceFiles/core/file_utilities.h | 1 + Telegram/SourceFiles/data/data_document.cpp | 2 +- .../media/view/media_view_overlay_widget.cpp | 2 +- .../menu/menu_item_download_files.cpp | 4 ++-- .../SourceFiles/settings/settings_chat.cpp | 2 +- .../details/storage_settings_scheme.cpp | 8 ++++++-- 10 files changed, 34 insertions(+), 21 deletions(-) diff --git a/Telegram/SourceFiles/boxes/download_path_box.cpp b/Telegram/SourceFiles/boxes/download_path_box.cpp index 8f28d96a8a..10ec622b1c 100644 --- a/Telegram/SourceFiles/boxes/download_path_box.cpp +++ b/Telegram/SourceFiles/boxes/download_path_box.cpp @@ -40,7 +40,7 @@ void DownloadPathBox::prepare() { _group->setChangedCallback([this](Directory value) { radioChanged(value); }); _pathLink->addClickHandler([=] { editPath(); }); - if (!_path.isEmpty() && _path != u"tmp"_q) { + if (!_path.isEmpty() && _path != FileDialog::Tmp()) { setPathText(QDir::toNativeSeparators(_path)); } updateControlsVisibility(); @@ -73,14 +73,14 @@ void DownloadPathBox::resizeEvent(QResizeEvent *e) { void DownloadPathBox::radioChanged(Directory value) { if (value == Directory::Custom) { - if (_path.isEmpty() || _path == u"tmp"_q) { + if (_path.isEmpty() || _path == FileDialog::Tmp()) { _group->setValue(_path.isEmpty() ? Directory::Downloads : Directory::Temp); editPath(); } else { setPathText(QDir::toNativeSeparators(_path)); } } else if (value == Directory::Temp) { - _path = u"tmp"_q; + _path = FileDialog::Tmp(); } else { _path = QString(); } @@ -91,7 +91,7 @@ void DownloadPathBox::radioChanged(Directory value) { void DownloadPathBox::editPath() { const auto initialPath = [] { const auto path = Core::App().settings().downloadPath(); - if (!path.isEmpty() && path != u"tmp"_q) { + if (!path.isEmpty() && path != FileDialog::Tmp()) { return path.left(path.size() - (path.endsWith('/') ? 1 : 0)); } return QString(); @@ -118,7 +118,7 @@ void DownloadPathBox::save() { if (value == Directory::Custom) { return _path; } else if (value == Directory::Temp) { - return qsl("tmp"); + return FileDialog::Tmp(); } return QString(); }; @@ -134,3 +134,13 @@ void DownloadPathBox::setPathText(const QString &text) { auto availw = st::boxWideWidth - st::boxPadding.left() - st::defaultCheck.diameter - st::defaultBoxCheckbox.textPosition.x() - st::boxPadding.right(); _pathLink->setText(st::boxTextFont->elided(text, availw)); } + +DownloadPathBox::Directory DownloadPathBox::typeFromPath( + const QString &path) { + if (path.isEmpty()) { + return Directory::Downloads; + } else if (path == FileDialog::Tmp()) { + return Directory::Temp; + } + return Directory::Custom; +} diff --git a/Telegram/SourceFiles/boxes/download_path_box.h b/Telegram/SourceFiles/boxes/download_path_box.h index 43d04cd4ae..6964c86f1c 100644 --- a/Telegram/SourceFiles/boxes/download_path_box.h +++ b/Telegram/SourceFiles/boxes/download_path_box.h @@ -39,14 +39,7 @@ private: Custom, }; void radioChanged(Directory value); - Directory typeFromPath(const QString &path) { - if (path.isEmpty()) { - return Directory::Downloads; - } else if (path == u"tmp"_q) { - return Directory::Temp; - } - return Directory::Custom; - } + Directory typeFromPath(const QString &path); void save(); void updateControlsVisibility(); diff --git a/Telegram/SourceFiles/core/application.cpp b/Telegram/SourceFiles/core/application.cpp index a85181034d..e5f3bb478c 100644 --- a/Telegram/SourceFiles/core/application.cpp +++ b/Telegram/SourceFiles/core/application.cpp @@ -255,7 +255,7 @@ void Application::run() { if (KSandbox::isInside()) { const auto path = settings().downloadPath(); if (!path.isEmpty() - && path != qstr("tmp") + && path != FileDialog::Tmp() && !base::CanReadDirectory(path)) { settings().setDownloadPath(QString()); } diff --git a/Telegram/SourceFiles/core/file_utilities.cpp b/Telegram/SourceFiles/core/file_utilities.cpp index ae26d6495e..e1db2e5ef4 100644 --- a/Telegram/SourceFiles/core/file_utilities.cpp +++ b/Telegram/SourceFiles/core/file_utilities.cpp @@ -359,6 +359,11 @@ QString PhotoVideoFilesFilter() { + AllFilesFilter(); } +const QString &Tmp() { + static const auto tmp = u"tmp"_q; + return tmp; +} + namespace internal { void InitLastPathDefault() { diff --git a/Telegram/SourceFiles/core/file_utilities.h b/Telegram/SourceFiles/core/file_utilities.h index 7201f5c835..f93fe5c922 100644 --- a/Telegram/SourceFiles/core/file_utilities.h +++ b/Telegram/SourceFiles/core/file_utilities.h @@ -92,6 +92,7 @@ void GetFolder( [[nodiscard]] QString AllOrImagesFilter(); [[nodiscard]] QString ImagesOrAllFilter(); [[nodiscard]] QString PhotoVideoFilesFilter(); +[[nodiscard]] const QString &Tmp(); namespace internal { diff --git a/Telegram/SourceFiles/data/data_document.cpp b/Telegram/SourceFiles/data/data_document.cpp index 41fa5e26a2..8676e63f80 100644 --- a/Telegram/SourceFiles/data/data_document.cpp +++ b/Telegram/SourceFiles/data/data_document.cpp @@ -149,7 +149,7 @@ QString FileNameUnsafe( const auto path = Core::App().settings().downloadPath(); if (path.isEmpty()) { return File::DefaultDownloadPath(session); - } else if (path == u"tmp"_q) { + } else if (path == FileDialog::Tmp()) { return session->local().tempDirectory(); } else { return path; diff --git a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp index 44e66f5283..b04066a7b0 100644 --- a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp +++ b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp @@ -1745,7 +1745,7 @@ void OverlayWidget::downloadMedia() { const auto session = _photo ? &_photo->session() : &_document->session(); if (Core::App().settings().downloadPath().isEmpty()) { path = File::DefaultDownloadPath(session); - } else if (Core::App().settings().downloadPath() == u"tmp"_q) { + } else if (Core::App().settings().downloadPath() == FileDialog::Tmp()) { path = session->local().tempDirectory(); } else { path = Core::App().settings().downloadPath(); diff --git a/Telegram/SourceFiles/menu/menu_item_download_files.cpp b/Telegram/SourceFiles/menu/menu_item_download_files.cpp index 18d709b770..69ec0f6979 100644 --- a/Telegram/SourceFiles/menu/menu_item_download_files.cpp +++ b/Telegram/SourceFiles/menu/menu_item_download_files.cpp @@ -83,7 +83,7 @@ void AddAction( : folderPath; const auto path = downloadPath.isEmpty() ? File::DefaultDownloadPath(session) - : (downloadPath == u"tmp"_q) + : (downloadPath == FileDialog::Tmp()) ? session->local().tempDirectory() : downloadPath; if (path.isEmpty()) { @@ -144,7 +144,7 @@ void AddAction( if (Core::App().settings().askDownloadPath()) { const auto initialPath = [] { const auto path = Core::App().settings().downloadPath(); - if (!path.isEmpty() && path != u"tmp"_q) { + if (!path.isEmpty() && path != FileDialog::Tmp()) { return path.left(path.size() - (path.endsWith('/') ? 1 : 0)); } diff --git a/Telegram/SourceFiles/settings/settings_chat.cpp b/Telegram/SourceFiles/settings/settings_chat.cpp index 1441de581f..71a0d4a371 100644 --- a/Telegram/SourceFiles/settings/settings_chat.cpp +++ b/Telegram/SourceFiles/settings/settings_chat.cpp @@ -1056,7 +1056,7 @@ void SetupDataStorage( ) | rpl::map([](const QString &text) { if (text.isEmpty()) { return tr::lng_download_path_default(tr::now); - } else if (text == u"tmp"_q) { + } else if (text == FileDialog::Tmp()) { return tr::lng_download_path_temp(tr::now); } return QDir::toNativeSeparators(text); diff --git a/Telegram/SourceFiles/storage/details/storage_settings_scheme.cpp b/Telegram/SourceFiles/storage/details/storage_settings_scheme.cpp index fda6ba02e7..77ae93264d 100644 --- a/Telegram/SourceFiles/storage/details/storage_settings_scheme.cpp +++ b/Telegram/SourceFiles/storage/details/storage_settings_scheme.cpp @@ -936,7 +936,9 @@ bool ReadSetting( stream >> v; if (!CheckStreamStatus(stream)) return false; #ifndef OS_WIN_STORE - if (!v.isEmpty() && v != u"tmp"_q && !v.endsWith('/')) v += '/'; + if (!v.isEmpty() && v != FileDialog::Tmp() && !v.endsWith('/')) { + v += '/'; + } Core::App().settings().setDownloadPathBookmark(QByteArray()); Core::App().settings().setDownloadPath(v); #endif // OS_WIN_STORE @@ -950,7 +952,9 @@ bool ReadSetting( if (!CheckStreamStatus(stream)) return false; #ifndef OS_WIN_STORE - if (!v.isEmpty() && v != u"tmp"_q && !v.endsWith('/')) v += '/'; + if (!v.isEmpty() && v != FileDialog::Tmp() && !v.endsWith('/')) { + v += '/'; + } Core::App().settings().setDownloadPathBookmark(bookmark); Core::App().settings().setDownloadPath(v); psDownloadPathEnableAccess();