From 0eec47038792685b61dba8a3c696983499530671 Mon Sep 17 00:00:00 2001 From: Ilya Fedin Date: Thu, 29 Dec 2022 12:49:03 +0400 Subject: [PATCH] Hide default download folder option if it's not available --- Telegram/Resources/langs/lang.strings | 1 + .../SourceFiles/boxes/download_path_box.cpp | 17 +++++++++++++---- Telegram/SourceFiles/core/application.cpp | 13 ++++++++----- Telegram/SourceFiles/core/application.h | 2 ++ Telegram/SourceFiles/core/file_utilities.cpp | 11 +++-------- Telegram/SourceFiles/settings/settings_chat.cpp | 4 +++- 6 files changed, 30 insertions(+), 18 deletions(-) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index 247ce7e3c..9e4a2eefe 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -755,6 +755,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_download_path" = "Download path"; "lng_download_path_temp" = "Temp folder"; "lng_download_path_default" = "Default folder"; +"lng_download_path_unset" = "Unset"; "lng_download_path_clear" = "Clear all"; "lng_download_path_header" = "Choose download path"; "lng_download_path_default_radio" = "Telegram folder in system «Downloads»"; diff --git a/Telegram/SourceFiles/boxes/download_path_box.cpp b/Telegram/SourceFiles/boxes/download_path_box.cpp index 10ec622b1..efbd96899 100644 --- a/Telegram/SourceFiles/boxes/download_path_box.cpp +++ b/Telegram/SourceFiles/boxes/download_path_box.cpp @@ -25,7 +25,14 @@ DownloadPathBox::DownloadPathBox( , _path(Core::App().settings().downloadPath()) , _pathBookmark(Core::App().settings().downloadPathBookmark()) , _group(std::make_shared>(typeFromPath(_path))) -, _default(this, _group, Directory::Downloads, tr::lng_download_path_default_radio(tr::now), st::defaultBoxCheckbox) +, _default(Core::App().canReadDefaultDownloadPath(true) + ? object_ptr>( + this, + _group, + Directory::Downloads, + tr::lng_download_path_default_radio(tr::now), + st::defaultBoxCheckbox) + : nullptr) , _temp(this, _group, Directory::Temp, tr::lng_download_path_temp_radio(tr::now), st::defaultBoxCheckbox) , _dir(this, _group, Directory::Custom, tr::lng_download_path_dir_radio(tr::now), st::defaultBoxCheckbox) , _pathLink(this, QString(), st::boxLinkButton) { @@ -50,7 +57,7 @@ void DownloadPathBox::updateControlsVisibility() { auto custom = (_group->value() == Directory::Custom); _pathLink->setVisible(custom); - auto newHeight = st::boxOptionListPadding.top() + _default->getMargins().top() + _default->heightNoMargins() + st::boxOptionListSkip + _temp->heightNoMargins() + st::boxOptionListSkip + _dir->heightNoMargins(); + auto newHeight = st::boxOptionListPadding.top() + (_default ? _default->getMargins().top() + _default->heightNoMargins() : 0) + st::boxOptionListSkip + _temp->heightNoMargins() + st::boxOptionListSkip + _dir->heightNoMargins(); if (custom) { newHeight += st::downloadPathSkip + _pathLink->height(); } @@ -62,8 +69,10 @@ void DownloadPathBox::updateControlsVisibility() { void DownloadPathBox::resizeEvent(QResizeEvent *e) { BoxContent::resizeEvent(e); - _default->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), st::boxOptionListPadding.top() + _default->getMargins().top()); - _temp->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), _default->bottomNoMargins() + st::boxOptionListSkip); + if (_default) { + _default->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), st::boxOptionListPadding.top() + _default->getMargins().top()); + } + _temp->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), (_default ? _default->bottomNoMargins() : 0) + st::boxOptionListSkip); _dir->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), _temp->bottomNoMargins() + st::boxOptionListSkip); auto inputx = st::boxPadding.left() + st::boxOptionListPadding.left() + st::defaultCheck.diameter + st::defaultBoxCheckbox.textPosition.x(); auto inputy = _dir->bottomNoMargins() + st::downloadPathSkip; diff --git a/Telegram/SourceFiles/core/application.cpp b/Telegram/SourceFiles/core/application.cpp index d892c4c15..b1d194ca5 100644 --- a/Telegram/SourceFiles/core/application.cpp +++ b/Telegram/SourceFiles/core/application.cpp @@ -595,11 +595,9 @@ void Application::saveSettings() { Local::writeSettings(); } -bool Application::canSaveFileWithoutAskingForPath() const { - if (Core::App().settings().askDownloadPath()) { - return false; - } else if (KSandbox::isInside() - && Core::App().settings().downloadPath().isEmpty()) { +bool Application::canReadDefaultDownloadPath(bool always) const { + if (KSandbox::isInside() + && (always || Core::App().settings().downloadPath().isEmpty())) { const auto path = QStandardPaths::writableLocation( QStandardPaths::DownloadLocation); return base::CanReadDirectory(path); @@ -607,6 +605,11 @@ bool Application::canSaveFileWithoutAskingForPath() const { return true; } +bool Application::canSaveFileWithoutAskingForPath() const { + return !Core::App().settings().askDownloadPath() + && canReadDefaultDownloadPath(); +} + MTP::Config &Application::fallbackProductionConfig() const { if (!_fallbackProductionConfig) { _fallbackProductionConfig = std::make_unique( diff --git a/Telegram/SourceFiles/core/application.h b/Telegram/SourceFiles/core/application.h index d2c19ba71..1d13c6f51 100644 --- a/Telegram/SourceFiles/core/application.h +++ b/Telegram/SourceFiles/core/application.h @@ -178,6 +178,8 @@ public: [[nodiscard]] Settings &settings(); void saveSettingsDelayed(crl::time delay = kDefaultSaveDelay); void saveSettings(); + + [[nodiscard]] bool canReadDefaultDownloadPath(bool always = false) const; [[nodiscard]] bool canSaveFileWithoutAskingForPath() const; // Fallback config and proxy. diff --git a/Telegram/SourceFiles/core/file_utilities.cpp b/Telegram/SourceFiles/core/file_utilities.cpp index e1db2e5ef..7b8d03cd9 100644 --- a/Telegram/SourceFiles/core/file_utilities.cpp +++ b/Telegram/SourceFiles/core/file_utilities.cpp @@ -24,8 +24,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include #include -#include - bool filedialogGetSaveFile( QPointer parent, QString &file, @@ -173,15 +171,12 @@ QString DefaultDownloadPathFolder(not_null session) { } QString DefaultDownloadPath(not_null session) { - const auto standardLocation = QStandardPaths::writableLocation( - QStandardPaths::DownloadLocation); - const auto realDefaultPath = standardLocation + const auto realDefaultPath = QStandardPaths::writableLocation( + QStandardPaths::DownloadLocation) + '/' + DefaultDownloadPathFolder(session) + '/'; - if (KSandbox::isInside() - && Core::App().settings().downloadPath().isEmpty() - && !base::CanReadDirectory(standardLocation)) { + if (!Core::App().canReadDefaultDownloadPath()) { QStringList files; QByteArray remoteContent; const auto success = Platform::FileDialog::Get( diff --git a/Telegram/SourceFiles/settings/settings_chat.cpp b/Telegram/SourceFiles/settings/settings_chat.cpp index 71a0d4a37..904bfed94 100644 --- a/Telegram/SourceFiles/settings/settings_chat.cpp +++ b/Telegram/SourceFiles/settings/settings_chat.cpp @@ -1055,7 +1055,9 @@ void SetupDataStorage( auto pathtext = Core::App().settings().downloadPathValue( ) | rpl::map([](const QString &text) { if (text.isEmpty()) { - return tr::lng_download_path_default(tr::now); + return Core::App().canReadDefaultDownloadPath(true) + ? tr::lng_download_path_default(tr::now) + : tr::lng_download_path_unset(tr::now); } else if (text == FileDialog::Tmp()) { return tr::lng_download_path_temp(tr::now); }