2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "boxes/download_path_box.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2017-02-28 14:05:30 +00:00
|
|
|
#include "core/file_utilities.h"
|
2016-11-11 13:46:04 +00:00
|
|
|
#include "ui/widgets/checkbox.h"
|
|
|
|
#include "ui/widgets/buttons.h"
|
2017-03-04 10:23:56 +00:00
|
|
|
#include "platform/platform_specific.h"
|
2020-06-18 18:04:16 +00:00
|
|
|
#include "core/application.h"
|
|
|
|
#include "core/core_settings.h"
|
2020-06-09 16:57:05 +00:00
|
|
|
#include "storage/storage_account.h"
|
2019-09-18 11:19:05 +00:00
|
|
|
#include "styles/style_layers.h"
|
2016-11-11 13:46:04 +00:00
|
|
|
#include "styles/style_boxes.h"
|
2015-11-26 17:34:52 +00:00
|
|
|
|
2020-06-09 16:57:05 +00:00
|
|
|
DownloadPathBox::DownloadPathBox(
|
|
|
|
QWidget *parent,
|
|
|
|
not_null<Window::SessionController*> controller)
|
|
|
|
: _controller(controller)
|
2020-06-18 18:04:16 +00:00
|
|
|
, _path(Core::App().settings().downloadPath())
|
|
|
|
, _pathBookmark(Core::App().settings().downloadPathBookmark())
|
2017-03-18 21:06:10 +00:00
|
|
|
, _group(std::make_shared<Ui::RadioenumGroup<Directory>>(typeFromPath(_path)))
|
2022-12-29 08:49:03 +00:00
|
|
|
, _default(Core::App().canReadDefaultDownloadPath(true)
|
|
|
|
? object_ptr<Ui::Radioenum<Directory>>(
|
|
|
|
this,
|
|
|
|
_group,
|
|
|
|
Directory::Downloads,
|
|
|
|
tr::lng_download_path_default_radio(tr::now),
|
|
|
|
st::defaultBoxCheckbox)
|
|
|
|
: nullptr)
|
2019-06-19 15:09:03 +00:00
|
|
|
, _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)
|
2016-12-13 17:07:56 +00:00
|
|
|
, _pathLink(this, QString(), st::boxLinkButton) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadPathBox::prepare() {
|
2019-06-18 16:53:27 +00:00
|
|
|
addButton(tr::lng_connection_save(), [this] { save(); });
|
|
|
|
addButton(tr::lng_cancel(), [this] { closeBox(); });
|
2015-10-06 19:49:23 +00:00
|
|
|
|
2019-06-18 15:00:55 +00:00
|
|
|
setTitle(tr::lng_download_path_header());
|
2015-10-06 19:49:23 +00:00
|
|
|
|
2017-03-18 21:06:10 +00:00
|
|
|
_group->setChangedCallback([this](Directory value) { radioChanged(value); });
|
2015-10-06 19:49:23 +00:00
|
|
|
|
2018-10-31 07:12:04 +00:00
|
|
|
_pathLink->addClickHandler([=] { editPath(); });
|
2022-11-30 00:05:55 +00:00
|
|
|
if (!_path.isEmpty() && _path != FileDialog::Tmp()) {
|
2015-10-06 19:49:23 +00:00
|
|
|
setPathText(QDir::toNativeSeparators(_path));
|
|
|
|
}
|
2016-11-18 13:34:58 +00:00
|
|
|
updateControlsVisibility();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 13:34:58 +00:00
|
|
|
void DownloadPathBox::updateControlsVisibility() {
|
2017-03-18 21:06:10 +00:00
|
|
|
auto custom = (_group->value() == Directory::Custom);
|
|
|
|
_pathLink->setVisible(custom);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2022-12-29 08:49:03 +00:00
|
|
|
auto newHeight = st::boxOptionListPadding.top() + (_default ? _default->getMargins().top() + _default->heightNoMargins() : 0) + st::boxOptionListSkip + _temp->heightNoMargins() + st::boxOptionListSkip + _dir->heightNoMargins();
|
2017-03-18 21:06:10 +00:00
|
|
|
if (custom) {
|
2016-12-13 17:07:56 +00:00
|
|
|
newHeight += st::downloadPathSkip + _pathLink->height();
|
|
|
|
}
|
2017-07-04 13:59:00 +00:00
|
|
|
newHeight += st::boxOptionListPadding.bottom() + _dir->getMargins().bottom();
|
2016-08-16 16:53:10 +00:00
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
setDimensions(st::boxWideWidth, newHeight);
|
2015-04-02 10:33:19 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-04-02 10:33:19 +00:00
|
|
|
void DownloadPathBox::resizeEvent(QResizeEvent *e) {
|
2016-12-13 17:07:56 +00:00
|
|
|
BoxContent::resizeEvent(e);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2022-12-29 08:49:03 +00:00
|
|
|
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);
|
2016-12-13 17:07:56 +00:00
|
|
|
_dir->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), _temp->bottomNoMargins() + st::boxOptionListSkip);
|
2017-07-07 11:27:13 +00:00
|
|
|
auto inputx = st::boxPadding.left() + st::boxOptionListPadding.left() + st::defaultCheck.diameter + st::defaultBoxCheckbox.textPosition.x();
|
2016-12-13 17:07:56 +00:00
|
|
|
auto inputy = _dir->bottomNoMargins() + st::downloadPathSkip;
|
2015-04-02 10:33:19 +00:00
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
_pathLink->moveToLeft(inputx, inputy);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 21:06:10 +00:00
|
|
|
void DownloadPathBox::radioChanged(Directory value) {
|
|
|
|
if (value == Directory::Custom) {
|
2022-11-30 00:05:55 +00:00
|
|
|
if (_path.isEmpty() || _path == FileDialog::Tmp()) {
|
2017-03-18 21:06:10 +00:00
|
|
|
_group->setValue(_path.isEmpty() ? Directory::Downloads : Directory::Temp);
|
2018-10-31 07:12:04 +00:00
|
|
|
editPath();
|
2014-05-30 08:53:19 +00:00
|
|
|
} else {
|
2015-10-06 19:49:23 +00:00
|
|
|
setPathText(QDir::toNativeSeparators(_path));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2017-03-18 21:06:10 +00:00
|
|
|
} else if (value == Directory::Temp) {
|
2022-11-30 00:05:55 +00:00
|
|
|
_path = FileDialog::Tmp();
|
2014-09-30 14:11:09 +00:00
|
|
|
} else {
|
|
|
|
_path = QString();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-11-18 13:34:58 +00:00
|
|
|
updateControlsVisibility();
|
2014-05-30 08:53:19 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2018-10-31 07:12:04 +00:00
|
|
|
void DownloadPathBox::editPath() {
|
2018-04-17 15:19:34 +00:00
|
|
|
const auto initialPath = [] {
|
2020-06-18 18:04:16 +00:00
|
|
|
const auto path = Core::App().settings().downloadPath();
|
2022-11-30 00:05:55 +00:00
|
|
|
if (!path.isEmpty() && path != FileDialog::Tmp()) {
|
2020-06-18 18:04:16 +00:00
|
|
|
return path.left(path.size() - (path.endsWith('/') ? 1 : 0));
|
2017-02-28 14:05:30 +00:00
|
|
|
}
|
|
|
|
return QString();
|
2018-04-17 15:19:34 +00:00
|
|
|
}();
|
|
|
|
const auto handleFolder = [=](const QString &result) {
|
2017-02-28 14:05:30 +00:00
|
|
|
if (!result.isEmpty()) {
|
2018-06-20 18:12:47 +00:00
|
|
|
_path = result.endsWith('/') ? result : (result + '/');
|
2015-11-26 17:34:52 +00:00
|
|
|
_pathBookmark = psDownloadPathBookmark(_path);
|
2015-10-06 19:49:23 +00:00
|
|
|
setPathText(QDir::toNativeSeparators(_path));
|
2017-03-18 21:06:10 +00:00
|
|
|
_group->setValue(Directory::Custom);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2018-04-17 15:19:34 +00:00
|
|
|
};
|
|
|
|
FileDialog::GetFolder(
|
|
|
|
this,
|
2019-06-19 15:09:03 +00:00
|
|
|
tr::lng_download_path_choose(tr::now),
|
2018-04-17 15:19:34 +00:00
|
|
|
initialPath,
|
2018-06-04 15:35:11 +00:00
|
|
|
crl::guard(this, handleFolder));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
void DownloadPathBox::save() {
|
2017-02-03 10:17:40 +00:00
|
|
|
#ifndef OS_WIN_STORE
|
2017-03-18 21:06:10 +00:00
|
|
|
auto value = _group->value();
|
|
|
|
auto computePath = [this, value] {
|
|
|
|
if (value == Directory::Custom) {
|
|
|
|
return _path;
|
|
|
|
} else if (value == Directory::Temp) {
|
2022-11-30 00:05:55 +00:00
|
|
|
return FileDialog::Tmp();
|
2017-03-18 21:06:10 +00:00
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
};
|
2020-06-18 18:04:16 +00:00
|
|
|
Core::App().settings().setDownloadPathBookmark(
|
|
|
|
(value == Directory::Custom) ? _pathBookmark : QByteArray());
|
|
|
|
Core::App().settings().setDownloadPath(computePath());
|
|
|
|
Core::App().saveSettings();
|
2016-12-13 17:07:56 +00:00
|
|
|
closeBox();
|
2017-02-03 10:17:40 +00:00
|
|
|
#endif // OS_WIN_STORE
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2015-10-06 19:49:23 +00:00
|
|
|
|
|
|
|
void DownloadPathBox::setPathText(const QString &text) {
|
2017-07-07 11:27:13 +00:00
|
|
|
auto availw = st::boxWideWidth - st::boxPadding.left() - st::defaultCheck.diameter - st::defaultBoxCheckbox.textPosition.x() - st::boxPadding.right();
|
2016-10-28 09:20:24 +00:00
|
|
|
_pathLink->setText(st::boxTextFont->elided(text, availw));
|
2015-10-06 19:49:23 +00:00
|
|
|
}
|
2022-11-30 00:05:55 +00:00
|
|
|
|
|
|
|
DownloadPathBox::Directory DownloadPathBox::typeFromPath(
|
|
|
|
const QString &path) {
|
|
|
|
if (path.isEmpty()) {
|
2023-01-02 13:22:21 +00:00
|
|
|
return Core::App().canReadDefaultDownloadPath(true)
|
|
|
|
? Directory::Downloads
|
|
|
|
: Directory::Temp;
|
2022-11-30 00:05:55 +00:00
|
|
|
} else if (path == FileDialog::Tmp()) {
|
|
|
|
return Directory::Temp;
|
|
|
|
}
|
|
|
|
return Directory::Custom;
|
|
|
|
}
|