download_path_box.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "boxes/download_path_box.h"
  8. #include "lang/lang_keys.h"
  9. #include "core/file_utilities.h"
  10. #include "ui/widgets/checkbox.h"
  11. #include "ui/widgets/buttons.h"
  12. #include "platform/platform_specific.h"
  13. #include "core/application.h"
  14. #include "core/core_settings.h"
  15. #include "storage/storage_account.h"
  16. #include "styles/style_layers.h"
  17. #include "styles/style_boxes.h"
  18. DownloadPathBox::DownloadPathBox(
  19. QWidget *parent,
  20. not_null<Window::SessionController*> controller)
  21. : _controller(controller)
  22. , _path(Core::App().settings().downloadPath())
  23. , _pathBookmark(Core::App().settings().downloadPathBookmark())
  24. , _group(std::make_shared<Ui::RadioenumGroup<Directory>>(typeFromPath(_path)))
  25. , _default(Core::App().canReadDefaultDownloadPath()
  26. ? object_ptr<Ui::Radioenum<Directory>>(
  27. this,
  28. _group,
  29. Directory::Downloads,
  30. tr::lng_download_path_default_radio(tr::now),
  31. st::defaultBoxCheckbox)
  32. : nullptr)
  33. , _temp(this, _group, Directory::Temp, tr::lng_download_path_temp_radio(tr::now), st::defaultBoxCheckbox)
  34. , _dir(this, _group, Directory::Custom, tr::lng_download_path_dir_radio(tr::now), st::defaultBoxCheckbox)
  35. , _pathLink(this, QString(), st::boxLinkButton) {
  36. }
  37. void DownloadPathBox::prepare() {
  38. addButton(tr::lng_connection_save(), [this] { save(); });
  39. addButton(tr::lng_cancel(), [this] { closeBox(); });
  40. setTitle(tr::lng_download_path_header());
  41. _group->setChangedCallback([this](Directory value) {
  42. radioChanged(value);
  43. });
  44. _pathLink->addClickHandler([=] { editPath(); });
  45. if (!_path.isEmpty() && _path != FileDialog::Tmp()) {
  46. setPathText(QDir::toNativeSeparators(_path));
  47. }
  48. updateControlsVisibility();
  49. }
  50. void DownloadPathBox::updateControlsVisibility() {
  51. auto custom = (_group->current() == Directory::Custom);
  52. _pathLink->setVisible(custom);
  53. auto newHeight = st::boxOptionListPadding.top() + (_default ? _default->getMargins().top() + _default->heightNoMargins() : 0) + st::boxOptionListSkip + _temp->heightNoMargins() + st::boxOptionListSkip + _dir->heightNoMargins();
  54. if (custom) {
  55. newHeight += st::downloadPathSkip + _pathLink->height();
  56. }
  57. newHeight += st::boxOptionListPadding.bottom() + _dir->getMargins().bottom();
  58. setDimensions(st::boxWideWidth, newHeight);
  59. }
  60. void DownloadPathBox::resizeEvent(QResizeEvent *e) {
  61. BoxContent::resizeEvent(e);
  62. if (_default) {
  63. _default->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), st::boxOptionListPadding.top() + _default->getMargins().top());
  64. }
  65. _temp->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), (_default ? _default->bottomNoMargins() : 0) + st::boxOptionListSkip);
  66. _dir->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), _temp->bottomNoMargins() + st::boxOptionListSkip);
  67. auto inputx = st::boxPadding.left() + st::boxOptionListPadding.left() + st::defaultCheck.diameter + st::defaultBoxCheckbox.textPosition.x();
  68. auto inputy = _dir->bottomNoMargins() + st::downloadPathSkip;
  69. _pathLink->moveToLeft(inputx, inputy);
  70. }
  71. void DownloadPathBox::radioChanged(Directory value) {
  72. if (value == Directory::Custom) {
  73. if (_path.isEmpty() || _path == FileDialog::Tmp()) {
  74. _group->setValue(_path.isEmpty() ? Directory::Downloads : Directory::Temp);
  75. editPath();
  76. } else {
  77. setPathText(QDir::toNativeSeparators(_path));
  78. }
  79. } else if (value == Directory::Temp) {
  80. _path = FileDialog::Tmp();
  81. } else {
  82. _path = QString();
  83. }
  84. updateControlsVisibility();
  85. update();
  86. }
  87. void DownloadPathBox::editPath() {
  88. const auto initialPath = [] {
  89. const auto path = Core::App().settings().downloadPath();
  90. if (!path.isEmpty() && path != FileDialog::Tmp()) {
  91. return path.left(path.size() - (path.endsWith('/') ? 1 : 0));
  92. }
  93. return QString();
  94. }();
  95. const auto handleFolder = [=](const QString &result) {
  96. if (!result.isEmpty()) {
  97. _path = result.endsWith('/') ? result : (result + '/');
  98. _pathBookmark = psDownloadPathBookmark(_path);
  99. setPathText(QDir::toNativeSeparators(_path));
  100. _group->setValue(Directory::Custom);
  101. }
  102. };
  103. FileDialog::GetFolder(
  104. this,
  105. tr::lng_download_path_choose(tr::now),
  106. initialPath,
  107. crl::guard(this, handleFolder));
  108. }
  109. void DownloadPathBox::save() {
  110. #ifndef OS_WIN_STORE
  111. auto value = _group->current();
  112. auto computePath = [this, value] {
  113. if (value == Directory::Custom) {
  114. return _path;
  115. } else if (value == Directory::Temp) {
  116. return FileDialog::Tmp();
  117. }
  118. return QString();
  119. };
  120. Core::App().settings().setDownloadPathBookmark(
  121. (value == Directory::Custom) ? _pathBookmark : QByteArray());
  122. Core::App().settings().setDownloadPath(computePath());
  123. Core::App().saveSettings();
  124. closeBox();
  125. #endif // OS_WIN_STORE
  126. }
  127. void DownloadPathBox::setPathText(const QString &text) {
  128. auto availw = st::boxWideWidth - st::boxPadding.left() - st::defaultCheck.diameter - st::defaultBoxCheckbox.textPosition.x() - st::boxPadding.right();
  129. _pathLink->setText(st::boxTextFont->elided(text, availw));
  130. }
  131. DownloadPathBox::Directory DownloadPathBox::typeFromPath(
  132. const QString &path) {
  133. if (path.isEmpty()) {
  134. return Core::App().canReadDefaultDownloadPath()
  135. ? Directory::Downloads
  136. : Directory::Temp;
  137. } else if (path == FileDialog::Tmp()) {
  138. return Directory::Temp;
  139. }
  140. return Directory::Custom;
  141. }