2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2014-12-01 10:47:38 +00:00
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
2015-10-03 13:16:42 +00:00
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2017-01-11 18:31:31 +00:00
|
|
|
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
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
|
|
|
|
2016-10-28 09:20:24 +00:00
|
|
|
#include "lang.h"
|
2017-03-04 10:23:56 +00:00
|
|
|
#include "storage/localstorage.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"
|
2016-11-11 13:46:04 +00:00
|
|
|
#include "styles/style_boxes.h"
|
2015-11-26 17:34:52 +00:00
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
DownloadPathBox::DownloadPathBox(QWidget *parent)
|
|
|
|
: _path(Global::DownloadPath())
|
2016-08-27 04:49:18 +00:00
|
|
|
, _pathBookmark(Global::DownloadPathBookmark())
|
2017-03-18 21:06:10 +00:00
|
|
|
, _group(std::make_shared<Ui::RadioenumGroup<Directory>>(typeFromPath(_path)))
|
|
|
|
, _default(this, _group, Directory::Downloads, lang(lng_download_path_default_radio), st::defaultBoxCheckbox)
|
|
|
|
, _temp(this, _group, Directory::Temp, lang(lng_download_path_temp_radio), st::defaultBoxCheckbox)
|
|
|
|
, _dir(this, _group, Directory::Custom, lang(lng_download_path_dir_radio), st::defaultBoxCheckbox)
|
2016-12-13 17:07:56 +00:00
|
|
|
, _pathLink(this, QString(), st::boxLinkButton) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadPathBox::prepare() {
|
|
|
|
addButton(lang(lng_connection_save), [this] { save(); });
|
|
|
|
addButton(lang(lng_cancel), [this] { closeBox(); });
|
2015-10-06 19:49:23 +00:00
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
setTitle(lang(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
|
|
|
|
2016-10-28 09:20:24 +00:00
|
|
|
connect(_pathLink, SIGNAL(clicked()), this, SLOT(onEditPath()));
|
2015-10-06 19:49:23 +00:00
|
|
|
if (!_path.isEmpty() && _path != qsl("tmp")) {
|
|
|
|
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
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
auto newHeight = st::boxOptionListPadding.top() + _default->heightNoMargins() + 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();
|
|
|
|
}
|
|
|
|
newHeight += st::boxOptionListPadding.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
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
_default->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), st::boxOptionListPadding.top());
|
|
|
|
_temp->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), _default->bottomNoMargins() + st::boxOptionListSkip);
|
|
|
|
_dir->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), _temp->bottomNoMargins() + st::boxOptionListSkip);
|
|
|
|
auto inputx = st::boxPadding.left() + st::boxOptionListPadding.left() + st::defaultBoxCheckbox.textPosition.x();
|
|
|
|
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) {
|
2014-09-30 14:11:09 +00:00
|
|
|
if (_path.isEmpty() || _path == qsl("tmp")) {
|
2017-03-18 21:06:10 +00:00
|
|
|
_group->setValue(_path.isEmpty() ? Directory::Downloads : Directory::Temp);
|
2014-05-30 08:53:19 +00:00
|
|
|
onEditPath();
|
|
|
|
} 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) {
|
2014-09-30 14:11:09 +00:00
|
|
|
_path = qsl("tmp");
|
|
|
|
} 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadPathBox::onEditPath() {
|
2017-02-28 14:05:30 +00:00
|
|
|
auto initialPath = [] {
|
|
|
|
if (!Global::DownloadPath().isEmpty() && Global::DownloadPath() != qstr("tmp")) {
|
|
|
|
return Global::DownloadPath().left(Global::DownloadPath().size() - (Global::DownloadPath().endsWith('/') ? 1 : 0));
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
};
|
|
|
|
FileDialog::GetFolder(lang(lng_download_path_choose), initialPath(), base::lambda_guarded(this, [this](const QString &result) {
|
|
|
|
if (!result.isEmpty()) {
|
|
|
|
_path = 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
|
|
|
}
|
2017-02-28 14:05:30 +00:00
|
|
|
}));
|
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) {
|
|
|
|
return qsl("tmp");
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
};
|
|
|
|
Global::SetDownloadPath(computePath());
|
|
|
|
Global::SetDownloadPathBookmark((value == Directory::Custom) ? _pathBookmark : QByteArray());
|
2015-03-02 12:34:16 +00:00
|
|
|
Local::writeUserSettings();
|
2016-08-27 04:49:18 +00:00
|
|
|
Global::RefDownloadPathChanged().notify();
|
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) {
|
2016-12-13 17:07:56 +00:00
|
|
|
auto availw = st::boxWideWidth - st::boxPadding.left() - 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
|
|
|
}
|