2018-04-04 19:26:40 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "passport/passport_panel_edit_scans.h"
|
|
|
|
|
|
|
|
#include "passport/passport_panel_controller.h"
|
2018-04-10 19:00:52 +00:00
|
|
|
#include "passport/passport_panel_details_row.h"
|
2018-04-04 19:26:40 +00:00
|
|
|
#include "info/profile/info_profile_button.h"
|
|
|
|
#include "ui/widgets/buttons.h"
|
|
|
|
#include "ui/widgets/labels.h"
|
|
|
|
#include "ui/wrap/fade_wrap.h"
|
|
|
|
#include "ui/wrap/slide_wrap.h"
|
|
|
|
#include "ui/wrap/vertical_layout.h"
|
2019-06-17 16:59:43 +00:00
|
|
|
#include "ui/text/text_utilities.h" // Ui::Text::ToUpper
|
2018-04-04 19:26:40 +00:00
|
|
|
#include "ui/text_options.h"
|
|
|
|
#include "core/file_utilities.h"
|
|
|
|
#include "lang/lang_keys.h"
|
|
|
|
#include "boxes/abstract_box.h"
|
2018-04-20 17:34:51 +00:00
|
|
|
#include "storage/storage_media_prepare.h"
|
2019-01-18 11:26:43 +00:00
|
|
|
#include "storage/file_upload.h" // For Storage::kUseBigFilesFrom.
|
2019-09-13 06:06:02 +00:00
|
|
|
#include "app.h"
|
2018-06-04 11:43:49 +00:00
|
|
|
#include "styles/style_boxes.h"
|
2018-04-04 19:26:40 +00:00
|
|
|
#include "styles/style_passport.h"
|
|
|
|
|
2019-09-04 07:19:15 +00:00
|
|
|
#include <QtCore/QBuffer>
|
|
|
|
|
2018-04-04 19:26:40 +00:00
|
|
|
namespace Passport {
|
2018-04-20 17:34:51 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kMaxDimensions = 2048;
|
|
|
|
constexpr auto kMaxSize = 10 * 1024 * 1024;
|
|
|
|
constexpr auto kJpegQuality = 89;
|
|
|
|
|
2019-01-18 11:26:43 +00:00
|
|
|
static_assert(kMaxSize <= Storage::kUseBigFilesFrom);
|
2018-04-20 17:34:51 +00:00
|
|
|
|
|
|
|
base::variant<ReadScanError, QByteArray> ProcessImage(QByteArray &&bytes) {
|
|
|
|
auto image = App::readImage(base::take(bytes));
|
|
|
|
if (image.isNull()) {
|
|
|
|
return ReadScanError::CantReadImage;
|
|
|
|
} else if (!Storage::ValidateThumbDimensions(image.width(), image.height())) {
|
|
|
|
return ReadScanError::BadImageSize;
|
|
|
|
}
|
|
|
|
if (std::max(image.width(), image.height()) > kMaxDimensions) {
|
|
|
|
image = std::move(image).scaled(
|
|
|
|
kMaxDimensions,
|
|
|
|
kMaxDimensions,
|
|
|
|
Qt::KeepAspectRatio,
|
|
|
|
Qt::SmoothTransformation);
|
|
|
|
}
|
|
|
|
auto result = QByteArray();
|
|
|
|
{
|
|
|
|
QBuffer buffer(&result);
|
|
|
|
if (!image.save(&buffer, QByteArray("JPG"), kJpegQuality)) {
|
|
|
|
return ReadScanError::Unknown;
|
|
|
|
}
|
|
|
|
base::take(image);
|
|
|
|
}
|
|
|
|
if (result.isEmpty()) {
|
|
|
|
return ReadScanError::Unknown;
|
|
|
|
} else if (result.size() > kMaxSize) {
|
|
|
|
return ReadScanError::FileTooLarge;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
2018-04-04 19:26:40 +00:00
|
|
|
|
|
|
|
class ScanButton : public Ui::AbstractButton {
|
|
|
|
public:
|
|
|
|
ScanButton(
|
|
|
|
QWidget *parent,
|
|
|
|
const style::PassportScanRow &st,
|
|
|
|
const QString &name,
|
|
|
|
const QString &status,
|
2018-04-17 17:54:52 +00:00
|
|
|
bool deleted,
|
|
|
|
bool error);
|
2018-04-04 19:26:40 +00:00
|
|
|
|
|
|
|
void setImage(const QImage &image);
|
|
|
|
void setStatus(const QString &status);
|
|
|
|
void setDeleted(bool deleted);
|
2018-04-17 17:54:52 +00:00
|
|
|
void setError(bool error);
|
2018-04-04 19:26:40 +00:00
|
|
|
|
|
|
|
rpl::producer<> deleteClicks() const {
|
2018-09-13 20:09:26 +00:00
|
|
|
return _delete->entity()->clicks(
|
|
|
|
) | rpl::map([] { return rpl::empty_value(); });
|
2018-04-04 19:26:40 +00:00
|
|
|
}
|
|
|
|
rpl::producer<> restoreClicks() const {
|
2018-09-13 20:09:26 +00:00
|
|
|
return _restore->entity()->clicks(
|
|
|
|
) | rpl::map([] { return rpl::empty_value(); });
|
2018-04-04 19:26:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
int resizeGetHeight(int newWidth) override;
|
|
|
|
|
|
|
|
void paintEvent(QPaintEvent *e) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
int countAvailableWidth() const;
|
|
|
|
|
|
|
|
const style::PassportScanRow &_st;
|
2019-06-12 13:26:04 +00:00
|
|
|
Ui::Text::String _name;
|
|
|
|
Ui::Text::String _status;
|
2018-04-04 19:26:40 +00:00
|
|
|
int _nameHeight = 0;
|
|
|
|
int _statusHeight = 0;
|
2018-04-17 17:54:52 +00:00
|
|
|
bool _error = false;
|
2018-04-04 19:26:40 +00:00
|
|
|
QImage _image;
|
|
|
|
object_ptr<Ui::FadeWrapScaled<Ui::IconButton>> _delete;
|
|
|
|
object_ptr<Ui::FadeWrapScaled<Ui::RoundButton>> _restore;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2018-05-11 22:55:56 +00:00
|
|
|
struct EditScans::SpecialScan {
|
|
|
|
SpecialScan(ScanInfo &&file);
|
|
|
|
|
|
|
|
ScanInfo file;
|
|
|
|
QPointer<Ui::SlideWrap<Ui::FlatLabel>> header;
|
|
|
|
QPointer<Ui::VerticalLayout> wrap;
|
|
|
|
base::unique_qptr<Ui::SlideWrap<ScanButton>> row;
|
|
|
|
QPointer<Info::Profile::Button> upload;
|
|
|
|
bool errorShown = false;
|
2019-04-02 09:13:30 +00:00
|
|
|
Ui::Animations::Simple errorAnimation;
|
2018-07-10 09:18:30 +00:00
|
|
|
rpl::variable<bool> rowCreated;
|
2018-05-11 22:55:56 +00:00
|
|
|
};
|
|
|
|
|
2018-08-14 15:27:47 +00:00
|
|
|
void UpdateFileRow(
|
|
|
|
not_null<ScanButton*> button,
|
|
|
|
const ScanInfo &info) {
|
|
|
|
button->setStatus(info.status);
|
|
|
|
button->setImage(info.thumb);
|
|
|
|
button->setDeleted(info.deleted);
|
|
|
|
button->setError(!info.error.isEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
base::unique_qptr<Ui::SlideWrap<ScanButton>> CreateScan(
|
|
|
|
not_null<Ui::VerticalLayout*> parent,
|
|
|
|
const ScanInfo &info,
|
|
|
|
const QString &name) {
|
|
|
|
auto result = base::unique_qptr<Ui::SlideWrap<ScanButton>>(
|
|
|
|
parent->add(object_ptr<Ui::SlideWrap<ScanButton>>(
|
|
|
|
parent,
|
|
|
|
object_ptr<ScanButton>(
|
|
|
|
parent,
|
|
|
|
st::passportScanRow,
|
|
|
|
name,
|
|
|
|
info.status,
|
|
|
|
info.deleted,
|
|
|
|
!info.error.isEmpty()))));
|
|
|
|
result->entity()->setImage(info.thumb);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
EditScans::List::List(
|
|
|
|
not_null<PanelController*> controller,
|
|
|
|
ScanListData &&data)
|
|
|
|
: controller(controller)
|
|
|
|
, files(std::move(data.files))
|
|
|
|
, initialCount(int(files.size()))
|
|
|
|
, errorMissing(data.errorMissing) {
|
|
|
|
}
|
|
|
|
|
|
|
|
EditScans::List::List(
|
|
|
|
not_null<PanelController*> controller,
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<ScanListData> &&data)
|
2018-08-14 15:27:47 +00:00
|
|
|
: controller(controller)
|
|
|
|
, files(data ? std::move(data->files) : std::vector<ScanInfo>())
|
2018-09-21 16:28:46 +00:00
|
|
|
, initialCount(data ? base::make_optional(int(files.size())) : std::nullopt)
|
2018-08-14 15:27:47 +00:00
|
|
|
, errorMissing(data ? std::move(data->errorMissing) : QString()) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EditScans::List::uploadedSomeMore() const {
|
|
|
|
if (!initialCount) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const auto from = begin(files) + *initialCount;
|
|
|
|
const auto till = end(files);
|
|
|
|
return std::find_if(from, till, [](const ScanInfo &file) {
|
|
|
|
return !file.deleted;
|
|
|
|
}) != till;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EditScans::List::uploadMoreRequired() const {
|
|
|
|
if (!upload) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const auto exists = ranges::find_if(
|
|
|
|
files,
|
|
|
|
[](const ScanInfo &file) { return !file.deleted; }) != end(files);
|
|
|
|
if (!exists) {
|
2018-08-17 12:07:41 +00:00
|
|
|
return true;
|
2018-08-14 15:27:47 +00:00
|
|
|
}
|
|
|
|
const auto errorExists = ranges::find_if(
|
|
|
|
files,
|
|
|
|
[](const ScanInfo &file) { return !file.error.isEmpty(); }
|
|
|
|
) != end(files);
|
|
|
|
return (errorExists || uploadMoreError) && !uploadedSomeMore();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ui::SlideWrap<ScanButton> *EditScans::List::nonDeletedErrorRow() const {
|
|
|
|
const auto nonDeletedErrorIt = ranges::find_if(
|
|
|
|
files,
|
|
|
|
[](const ScanInfo &file) {
|
|
|
|
return !file.error.isEmpty() && !file.deleted;
|
|
|
|
});
|
|
|
|
if (nonDeletedErrorIt == end(files)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
const auto index = (nonDeletedErrorIt - begin(files));
|
|
|
|
return rows[index].get();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<QString> EditScans::List::uploadButtonText() const {
|
2019-06-18 12:16:43 +00:00
|
|
|
return (files.empty()
|
|
|
|
? tr::lng_passport_upload_scans
|
|
|
|
: tr::lng_passport_upload_more)() | Ui::Text::ToUpper();
|
2018-08-14 15:27:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditScans::List::hideError() {
|
|
|
|
toggleError(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditScans::List::toggleError(bool shown) {
|
|
|
|
if (errorShown != shown) {
|
|
|
|
errorShown = shown;
|
|
|
|
errorAnimation.start(
|
|
|
|
[=] { errorAnimationCallback(); },
|
|
|
|
errorShown ? 0. : 1.,
|
|
|
|
errorShown ? 1. : 0.,
|
|
|
|
st::passportDetailsField.duration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditScans::List::errorAnimationCallback() {
|
2019-04-02 09:13:30 +00:00
|
|
|
const auto error = errorAnimation.value(errorShown ? 1. : 0.);
|
2018-08-14 15:27:47 +00:00
|
|
|
if (error == 0.) {
|
2018-09-21 16:28:46 +00:00
|
|
|
upload->setColorOverride(std::nullopt);
|
2018-08-14 15:27:47 +00:00
|
|
|
} else {
|
|
|
|
upload->setColorOverride(anim::color(
|
|
|
|
st::passportUploadButton.textFg,
|
|
|
|
st::boxTextFgError,
|
|
|
|
error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditScans::List::updateScan(ScanInfo &&info, int width) {
|
|
|
|
const auto i = ranges::find(files, info.key, [](const ScanInfo &file) {
|
|
|
|
return file.key;
|
|
|
|
});
|
|
|
|
if (i != files.end()) {
|
|
|
|
*i = std::move(info);
|
|
|
|
const auto scan = rows[i - files.begin()]->entity();
|
|
|
|
UpdateFileRow(scan, *i);
|
|
|
|
if (!i->deleted) {
|
|
|
|
hideError();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
files.push_back(std::move(info));
|
|
|
|
pushScan(files.back());
|
|
|
|
wrap->resizeToWidth(width);
|
|
|
|
rows.back()->show(anim::type::normal);
|
|
|
|
if (divider) {
|
|
|
|
divider->hide(anim::type::normal);
|
|
|
|
}
|
|
|
|
header->show(anim::type::normal);
|
|
|
|
uploadTexts.fire(uploadButtonText());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditScans::List::pushScan(const ScanInfo &info) {
|
|
|
|
const auto index = rows.size();
|
|
|
|
const auto type = info.type;
|
|
|
|
rows.push_back(CreateScan(
|
|
|
|
wrap,
|
|
|
|
info,
|
2019-06-19 16:39:25 +00:00
|
|
|
tr::lng_passport_scan_index(tr::now, lt_index, QString::number(index + 1))));
|
2018-08-14 15:27:47 +00:00
|
|
|
rows.back()->hide(anim::type::instant);
|
|
|
|
|
|
|
|
const auto scan = rows.back()->entity();
|
|
|
|
|
|
|
|
scan->deleteClicks(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
controller->deleteScan(type, index);
|
|
|
|
}, scan->lifetime());
|
|
|
|
|
|
|
|
scan->restoreClicks(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
controller->restoreScan(type, index);
|
|
|
|
}, scan->lifetime());
|
|
|
|
|
|
|
|
hideError();
|
|
|
|
}
|
|
|
|
|
2018-04-04 19:26:40 +00:00
|
|
|
ScanButton::ScanButton(
|
|
|
|
QWidget *parent,
|
|
|
|
const style::PassportScanRow &st,
|
|
|
|
const QString &name,
|
|
|
|
const QString &status,
|
2018-04-17 17:54:52 +00:00
|
|
|
bool deleted,
|
|
|
|
bool error)
|
2018-04-04 19:26:40 +00:00
|
|
|
: AbstractButton(parent)
|
|
|
|
, _st(st)
|
|
|
|
, _name(
|
|
|
|
st::passportScanNameStyle,
|
|
|
|
name,
|
|
|
|
Ui::NameTextOptions())
|
|
|
|
, _status(
|
|
|
|
st::defaultTextStyle,
|
|
|
|
status,
|
|
|
|
Ui::NameTextOptions())
|
2018-04-17 17:54:52 +00:00
|
|
|
, _error(error)
|
2018-04-04 19:26:40 +00:00
|
|
|
, _delete(this, object_ptr<Ui::IconButton>(this, _st.remove))
|
|
|
|
, _restore(
|
|
|
|
this,
|
|
|
|
object_ptr<Ui::RoundButton>(
|
|
|
|
this,
|
2019-06-18 16:53:27 +00:00
|
|
|
tr::lng_passport_delete_scan_undo(),
|
2018-04-04 19:26:40 +00:00
|
|
|
_st.restore)) {
|
|
|
|
_delete->toggle(!deleted, anim::type::instant);
|
|
|
|
_restore->toggle(deleted, anim::type::instant);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScanButton::setImage(const QImage &image) {
|
|
|
|
_image = image;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScanButton::setStatus(const QString &status) {
|
|
|
|
_status.setText(
|
|
|
|
st::defaultTextStyle,
|
|
|
|
status,
|
|
|
|
Ui::NameTextOptions());
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScanButton::setDeleted(bool deleted) {
|
|
|
|
_delete->toggle(!deleted, anim::type::instant);
|
|
|
|
_restore->toggle(deleted, anim::type::instant);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2018-04-17 17:54:52 +00:00
|
|
|
void ScanButton::setError(bool error) {
|
|
|
|
_error = error;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2018-04-04 19:26:40 +00:00
|
|
|
int ScanButton::resizeGetHeight(int newWidth) {
|
|
|
|
_nameHeight = st::semiboldFont->height;
|
|
|
|
_statusHeight = st::normalFont->height;
|
|
|
|
const auto result = _st.padding.top() + _st.size + _st.padding.bottom();
|
|
|
|
const auto right = _st.padding.right();
|
|
|
|
_delete->moveToRight(
|
|
|
|
right,
|
|
|
|
(result - _delete->height()) / 2,
|
|
|
|
newWidth);
|
|
|
|
_restore->moveToRight(
|
|
|
|
right,
|
|
|
|
(result - _restore->height()) / 2,
|
|
|
|
newWidth);
|
|
|
|
return result + st::lineWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ScanButton::countAvailableWidth() const {
|
|
|
|
return width()
|
|
|
|
- _st.padding.left()
|
|
|
|
- _st.textLeft
|
|
|
|
- _st.padding.right()
|
|
|
|
- std::max(_delete->width(), _restore->width());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScanButton::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
|
|
|
|
|
|
|
const auto left = _st.padding.left();
|
|
|
|
const auto top = _st.padding.top();
|
|
|
|
p.fillRect(
|
|
|
|
left,
|
|
|
|
height() - _st.border,
|
|
|
|
width() - left,
|
|
|
|
_st.border,
|
|
|
|
_st.borderFg);
|
|
|
|
|
2018-04-17 17:54:52 +00:00
|
|
|
const auto deleted = _restore->toggled();
|
|
|
|
if (deleted) {
|
2018-04-04 19:26:40 +00:00
|
|
|
p.setOpacity(st::passportScanDeletedOpacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_image.isNull()) {
|
|
|
|
p.fillRect(left, top, _st.size, _st.size, Qt::black);
|
|
|
|
} else {
|
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
const auto fromRect = [&] {
|
|
|
|
if (_image.width() > _image.height()) {
|
|
|
|
const auto shift = (_image.width() - _image.height()) / 2;
|
|
|
|
return QRect(shift, 0, _image.height(), _image.height());
|
|
|
|
} else {
|
|
|
|
const auto shift = (_image.height() - _image.width()) / 2;
|
|
|
|
return QRect(0, shift, _image.width(), _image.width());
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
p.drawImage(QRect(left, top, _st.size, _st.size), _image, fromRect);
|
|
|
|
}
|
|
|
|
const auto availableWidth = countAvailableWidth();
|
|
|
|
|
|
|
|
p.setPen(st::windowFg);
|
|
|
|
_name.drawLeftElided(
|
|
|
|
p,
|
|
|
|
left + _st.textLeft,
|
|
|
|
top + _st.nameTop,
|
|
|
|
availableWidth,
|
|
|
|
width());
|
2018-04-17 17:54:52 +00:00
|
|
|
p.setPen((_error && !deleted)
|
|
|
|
? st::boxTextFgError
|
|
|
|
: st::windowSubTextFg);
|
2018-04-04 19:26:40 +00:00
|
|
|
_status.drawLeftElided(
|
|
|
|
p,
|
|
|
|
left + _st.textLeft,
|
|
|
|
top + _st.statusTop,
|
|
|
|
availableWidth,
|
|
|
|
width());
|
|
|
|
}
|
|
|
|
|
2018-05-11 22:55:56 +00:00
|
|
|
EditScans::SpecialScan::SpecialScan(ScanInfo &&file)
|
|
|
|
: file(std::move(file)) {
|
|
|
|
}
|
|
|
|
|
2018-04-04 19:26:40 +00:00
|
|
|
EditScans::EditScans(
|
|
|
|
QWidget *parent,
|
|
|
|
not_null<PanelController*> controller,
|
2018-04-10 07:51:19 +00:00
|
|
|
const QString &header,
|
2018-08-14 11:37:03 +00:00
|
|
|
const QString &error,
|
2018-08-14 15:27:47 +00:00
|
|
|
ScanListData &&scans,
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<ScanListData> &&translations)
|
2018-04-04 19:26:40 +00:00
|
|
|
: RpWidget(parent)
|
|
|
|
, _controller(controller)
|
2018-08-14 11:37:03 +00:00
|
|
|
, _error(error)
|
2018-08-14 15:27:47 +00:00
|
|
|
, _content(this)
|
|
|
|
, _scansList(_controller, std::move(scans))
|
|
|
|
, _translationsList(_controller, std::move(translations)) {
|
2018-05-11 22:55:56 +00:00
|
|
|
setupScans(header);
|
|
|
|
}
|
|
|
|
|
|
|
|
EditScans::EditScans(
|
|
|
|
QWidget *parent,
|
|
|
|
not_null<PanelController*> controller,
|
2018-08-17 14:59:07 +00:00
|
|
|
const QString &header,
|
2018-08-14 11:37:03 +00:00
|
|
|
const QString &error,
|
2018-08-14 15:27:47 +00:00
|
|
|
std::map<FileType, ScanInfo> &&specialFiles,
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<ScanListData> &&translations)
|
2018-05-11 22:55:56 +00:00
|
|
|
: RpWidget(parent)
|
|
|
|
, _controller(controller)
|
2018-08-14 11:37:03 +00:00
|
|
|
, _error(error)
|
2018-08-14 15:27:47 +00:00
|
|
|
, _content(this)
|
|
|
|
, _scansList(_controller)
|
|
|
|
, _translationsList(_controller, std::move(translations)) {
|
2018-08-17 14:59:07 +00:00
|
|
|
setupSpecialScans(header, std::move(specialFiles));
|
2018-04-04 19:26:40 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<int> EditScans::validateGetErrorTop() {
|
|
|
|
auto result = std::optional<int>();
|
2018-05-11 22:55:56 +00:00
|
|
|
const auto suggestResult = [&](int value) {
|
|
|
|
if (!result || *result > value) {
|
|
|
|
result = value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-14 11:37:03 +00:00
|
|
|
if (_commonError && !somethingChanged()) {
|
|
|
|
suggestResult(_commonError->y());
|
|
|
|
}
|
2018-08-14 15:27:47 +00:00
|
|
|
const auto suggestList = [&](FileType type) {
|
|
|
|
auto &list = this->list(type);
|
|
|
|
if (list.uploadMoreRequired()) {
|
|
|
|
list.toggleError(true);
|
|
|
|
suggestResult((list.files.size() > 5)
|
|
|
|
? list.upload->y()
|
|
|
|
: list.header->y());
|
|
|
|
}
|
|
|
|
if (const auto row = list.nonDeletedErrorRow()) {
|
|
|
|
//toggleError(true);
|
|
|
|
suggestResult(row->y());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
suggestList(FileType::Scan);
|
2018-05-11 22:55:56 +00:00
|
|
|
for (const auto &[type, scan] : _specialScans) {
|
|
|
|
if (!scan.file.key.id
|
|
|
|
|| scan.file.deleted
|
|
|
|
|| !scan.file.error.isEmpty()) {
|
|
|
|
toggleSpecialScanError(type, true);
|
2018-08-17 14:59:07 +00:00
|
|
|
suggestResult(scan.header ? scan.header->y() : scan.wrap->y());
|
2018-04-19 06:03:17 +00:00
|
|
|
}
|
2018-04-13 10:54:17 +00:00
|
|
|
}
|
2018-08-14 15:27:47 +00:00
|
|
|
suggestList(FileType::Translation);
|
2018-04-19 06:03:17 +00:00
|
|
|
return result;
|
2018-04-13 10:54:17 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 15:27:47 +00:00
|
|
|
EditScans::List &EditScans::list(FileType type) {
|
|
|
|
switch (type) {
|
|
|
|
case FileType::Scan: return _scansList;
|
|
|
|
case FileType::Translation: return _translationsList;
|
|
|
|
}
|
|
|
|
Unexpected("Type in EditScans::list().");
|
|
|
|
}
|
|
|
|
|
|
|
|
const EditScans::List &EditScans::list(FileType type) const {
|
|
|
|
switch (type) {
|
|
|
|
case FileType::Scan: return _scansList;
|
|
|
|
case FileType::Translation: return _translationsList;
|
|
|
|
}
|
|
|
|
Unexpected("Type in EditScans::list() const.");
|
|
|
|
}
|
|
|
|
|
2018-05-11 22:55:56 +00:00
|
|
|
void EditScans::setupScans(const QString &header) {
|
2018-04-04 19:26:40 +00:00
|
|
|
const auto inner = _content.data();
|
|
|
|
inner->move(0, 0);
|
|
|
|
|
2018-08-14 11:37:03 +00:00
|
|
|
if (!_error.isEmpty()) {
|
|
|
|
_commonError = inner->add(
|
|
|
|
object_ptr<Ui::SlideWrap<Ui::FlatLabel>>(
|
|
|
|
inner,
|
|
|
|
object_ptr<Ui::FlatLabel>(
|
|
|
|
inner,
|
|
|
|
_error,
|
|
|
|
st::passportVerifyErrorLabel),
|
|
|
|
st::passportValueErrorPadding));
|
|
|
|
_commonError->toggle(true, anim::type::instant);
|
|
|
|
}
|
|
|
|
|
2018-08-14 15:27:47 +00:00
|
|
|
setupList(inner, FileType::Scan, header);
|
2019-06-19 15:09:03 +00:00
|
|
|
setupList(inner, FileType::Translation, tr::lng_passport_translation(tr::now));
|
2018-08-14 15:27:47 +00:00
|
|
|
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditScans::setupList(
|
|
|
|
not_null<Ui::VerticalLayout*> container,
|
|
|
|
FileType type,
|
|
|
|
const QString &header) {
|
|
|
|
auto &list = this->list(type);
|
|
|
|
if (!list.initialCount) {
|
|
|
|
return;
|
|
|
|
}
|
2018-04-04 19:26:40 +00:00
|
|
|
|
2018-08-14 15:27:47 +00:00
|
|
|
if (type == FileType::Scan) {
|
|
|
|
list.divider = container->add(
|
|
|
|
object_ptr<Ui::SlideWrap<BoxContentDivider>>(
|
|
|
|
container,
|
|
|
|
object_ptr<BoxContentDivider>(
|
|
|
|
container,
|
|
|
|
st::passportFormDividerHeight)));
|
|
|
|
list.divider->toggle(list.files.empty(), anim::type::instant);
|
|
|
|
}
|
|
|
|
list.header = container->add(
|
2018-04-04 19:26:40 +00:00
|
|
|
object_ptr<Ui::SlideWrap<Ui::FlatLabel>>(
|
2018-08-14 15:27:47 +00:00
|
|
|
container,
|
2018-04-04 19:26:40 +00:00
|
|
|
object_ptr<Ui::FlatLabel>(
|
2018-08-14 15:27:47 +00:00
|
|
|
container,
|
2018-04-10 07:51:19 +00:00
|
|
|
header,
|
2018-04-04 19:26:40 +00:00
|
|
|
st::passportFormHeader),
|
|
|
|
st::passportUploadHeaderPadding));
|
2018-08-14 15:27:47 +00:00
|
|
|
list.header->toggle(
|
|
|
|
!list.divider || !list.files.empty(),
|
|
|
|
anim::type::instant);
|
|
|
|
if (!list.errorMissing.isEmpty()) {
|
|
|
|
list.uploadMoreError = container->add(
|
2018-04-17 17:54:52 +00:00
|
|
|
object_ptr<Ui::SlideWrap<Ui::FlatLabel>>(
|
2018-08-14 15:27:47 +00:00
|
|
|
container,
|
2018-04-17 17:54:52 +00:00
|
|
|
object_ptr<Ui::FlatLabel>(
|
2018-08-14 15:27:47 +00:00
|
|
|
container,
|
|
|
|
list.errorMissing,
|
2018-04-17 17:54:52 +00:00
|
|
|
st::passportVerifyErrorLabel),
|
|
|
|
st::passportUploadErrorPadding));
|
2018-08-14 15:27:47 +00:00
|
|
|
list.uploadMoreError->toggle(true, anim::type::instant);
|
2018-04-17 17:54:52 +00:00
|
|
|
}
|
2018-08-14 15:27:47 +00:00
|
|
|
list.wrap = container->add(object_ptr<Ui::VerticalLayout>(container));
|
|
|
|
for (const auto &scan : list.files) {
|
|
|
|
list.pushScan(scan);
|
|
|
|
list.rows.back()->show(anim::type::instant);
|
2018-04-04 19:26:40 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 15:27:47 +00:00
|
|
|
list.upload = container->add(
|
2018-04-04 19:26:40 +00:00
|
|
|
object_ptr<Info::Profile::Button>(
|
2018-08-14 15:27:47 +00:00
|
|
|
container,
|
|
|
|
list.uploadTexts.events_starting_with(
|
|
|
|
list.uploadButtonText()
|
2018-04-04 19:26:40 +00:00
|
|
|
) | rpl::flatten_latest(),
|
|
|
|
st::passportUploadButton),
|
|
|
|
st::passportUploadButtonPadding);
|
2018-08-14 15:27:47 +00:00
|
|
|
list.upload->addClickHandler([=] {
|
|
|
|
chooseScan(type);
|
2018-04-04 19:26:40 +00:00
|
|
|
});
|
2018-04-10 19:00:52 +00:00
|
|
|
|
2018-08-14 15:27:47 +00:00
|
|
|
container->add(object_ptr<BoxContentDivider>(
|
|
|
|
container,
|
2018-04-10 19:00:52 +00:00
|
|
|
st::passportFormDividerHeight));
|
2018-05-11 22:55:56 +00:00
|
|
|
}
|
|
|
|
|
2018-08-17 14:59:07 +00:00
|
|
|
void EditScans::setupSpecialScans(
|
|
|
|
const QString &header,
|
|
|
|
std::map<FileType, ScanInfo> &&files) {
|
2018-08-14 15:27:47 +00:00
|
|
|
const auto requiresBothSides = files.find(FileType::ReverseSide)
|
2018-07-10 09:18:30 +00:00
|
|
|
!= end(files);
|
2018-08-14 15:27:47 +00:00
|
|
|
const auto title = [&](FileType type) {
|
2018-05-11 22:55:56 +00:00
|
|
|
switch (type) {
|
2018-08-14 15:27:47 +00:00
|
|
|
case FileType::FrontSide:
|
2019-06-19 15:09:03 +00:00
|
|
|
return requiresBothSides
|
|
|
|
? tr::lng_passport_front_side_title(tr::now)
|
|
|
|
: tr::lng_passport_main_page_title(tr::now);
|
2018-08-14 15:27:47 +00:00
|
|
|
case FileType::ReverseSide:
|
2019-06-19 15:09:03 +00:00
|
|
|
return tr::lng_passport_reverse_side_title(tr::now);
|
2018-08-14 15:27:47 +00:00
|
|
|
case FileType::Selfie:
|
2019-06-19 15:09:03 +00:00
|
|
|
return tr::lng_passport_selfie_title(tr::now);
|
2018-05-11 22:55:56 +00:00
|
|
|
}
|
|
|
|
Unexpected("Type in special row title.");
|
|
|
|
};
|
2019-06-18 12:16:43 +00:00
|
|
|
const auto uploadText = [=](FileType type, bool hasScan) {
|
2018-05-11 22:55:56 +00:00
|
|
|
switch (type) {
|
2018-08-14 15:27:47 +00:00
|
|
|
case FileType::FrontSide:
|
2018-07-10 09:18:30 +00:00
|
|
|
return requiresBothSides
|
|
|
|
? (hasScan
|
2019-06-18 12:16:43 +00:00
|
|
|
? tr::lng_passport_reupload_front_side
|
|
|
|
: tr::lng_passport_upload_front_side)
|
2018-07-10 09:18:30 +00:00
|
|
|
: (hasScan
|
2019-06-18 12:16:43 +00:00
|
|
|
? tr::lng_passport_reupload_main_page
|
|
|
|
: tr::lng_passport_upload_main_page);
|
2018-08-14 15:27:47 +00:00
|
|
|
case FileType::ReverseSide:
|
2018-07-10 09:18:30 +00:00
|
|
|
return hasScan
|
2019-06-18 12:16:43 +00:00
|
|
|
? tr::lng_passport_reupload_reverse_side
|
|
|
|
: tr::lng_passport_upload_reverse_side;
|
2018-08-14 15:27:47 +00:00
|
|
|
case FileType::Selfie:
|
2018-07-10 09:18:30 +00:00
|
|
|
return hasScan
|
2019-06-18 12:16:43 +00:00
|
|
|
? tr::lng_passport_reupload_selfie
|
|
|
|
: tr::lng_passport_upload_selfie;
|
2018-05-11 22:55:56 +00:00
|
|
|
}
|
|
|
|
Unexpected("Type in special row upload key.");
|
|
|
|
};
|
2018-08-14 15:27:47 +00:00
|
|
|
const auto description = [&](FileType type) {
|
2018-05-11 22:55:56 +00:00
|
|
|
switch (type) {
|
2018-08-14 15:27:47 +00:00
|
|
|
case FileType::FrontSide:
|
2019-06-18 12:16:43 +00:00
|
|
|
return requiresBothSides
|
|
|
|
? tr::lng_passport_front_side_description
|
|
|
|
: tr::lng_passport_main_page_description;
|
2018-08-14 15:27:47 +00:00
|
|
|
case FileType::ReverseSide:
|
2019-06-18 12:16:43 +00:00
|
|
|
return tr::lng_passport_reverse_side_description;
|
2018-08-14 15:27:47 +00:00
|
|
|
case FileType::Selfie:
|
2019-06-18 12:16:43 +00:00
|
|
|
return tr::lng_passport_selfie_description;
|
2018-05-11 22:55:56 +00:00
|
|
|
}
|
|
|
|
Unexpected("Type in special row upload key.");
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto inner = _content.data();
|
|
|
|
inner->move(0, 0);
|
2018-08-14 11:37:03 +00:00
|
|
|
|
|
|
|
if (!_error.isEmpty()) {
|
|
|
|
_commonError = inner->add(
|
|
|
|
object_ptr<Ui::SlideWrap<Ui::FlatLabel>>(
|
|
|
|
inner,
|
|
|
|
object_ptr<Ui::FlatLabel>(
|
|
|
|
inner,
|
|
|
|
_error,
|
|
|
|
st::passportVerifyErrorLabel),
|
|
|
|
st::passportValueErrorPadding));
|
|
|
|
_commonError->toggle(true, anim::type::instant);
|
|
|
|
}
|
|
|
|
|
2018-05-11 22:55:56 +00:00
|
|
|
for (auto &[type, info] : files) {
|
|
|
|
const auto i = _specialScans.emplace(
|
|
|
|
type,
|
|
|
|
SpecialScan(std::move(info))).first;
|
|
|
|
auto &scan = i->second;
|
|
|
|
|
2018-08-17 14:59:07 +00:00
|
|
|
if (_specialScans.size() == 1) {
|
|
|
|
scan.header = inner->add(
|
|
|
|
object_ptr<Ui::SlideWrap<Ui::FlatLabel>>(
|
2018-04-10 19:00:52 +00:00
|
|
|
inner,
|
2018-08-17 14:59:07 +00:00
|
|
|
object_ptr<Ui::FlatLabel>(
|
|
|
|
inner,
|
|
|
|
header,
|
|
|
|
st::passportFormHeader),
|
|
|
|
st::passportUploadHeaderPadding));
|
|
|
|
scan.header->toggle(scan.file.key.id != 0, anim::type::instant);
|
|
|
|
}
|
2018-05-11 22:55:56 +00:00
|
|
|
scan.wrap = inner->add(object_ptr<Ui::VerticalLayout>(inner));
|
|
|
|
if (scan.file.key.id) {
|
2018-07-10 09:18:30 +00:00
|
|
|
createSpecialScanRow(scan, scan.file, requiresBothSides);
|
2018-04-10 19:00:52 +00:00
|
|
|
}
|
2018-07-10 09:18:30 +00:00
|
|
|
auto label = scan.rowCreated.value(
|
2018-07-10 21:19:52 +00:00
|
|
|
) | rpl::map([=, type = type](bool created) {
|
2019-06-18 12:16:43 +00:00
|
|
|
return uploadText(type, created)();
|
2018-07-10 09:18:30 +00:00
|
|
|
}) | rpl::flatten_latest(
|
2019-06-17 16:59:43 +00:00
|
|
|
) | Ui::Text::ToUpper();
|
2018-05-11 22:55:56 +00:00
|
|
|
scan.upload = inner->add(
|
2018-04-10 19:00:52 +00:00
|
|
|
object_ptr<Info::Profile::Button>(
|
|
|
|
inner,
|
2018-07-10 09:18:30 +00:00
|
|
|
std::move(label),
|
2018-04-10 19:00:52 +00:00
|
|
|
st::passportUploadButton),
|
|
|
|
st::passportUploadButtonPadding);
|
2018-06-02 14:27:25 +00:00
|
|
|
scan.upload->addClickHandler([=, type = type] {
|
2018-08-14 15:27:47 +00:00
|
|
|
chooseScan(type);
|
2018-04-10 19:00:52 +00:00
|
|
|
});
|
|
|
|
|
2018-06-04 11:43:49 +00:00
|
|
|
inner->add(object_ptr<Ui::DividerLabel>(
|
2018-04-10 19:00:52 +00:00
|
|
|
inner,
|
|
|
|
object_ptr<Ui::FlatLabel>(
|
2018-08-17 13:42:07 +00:00
|
|
|
inner,
|
2019-06-18 12:16:43 +00:00
|
|
|
description(type)(tr::now),
|
2018-06-04 11:43:49 +00:00
|
|
|
st::boxDividerLabel),
|
2018-04-10 19:00:52 +00:00
|
|
|
st::passportFormLabelPadding));
|
|
|
|
}
|
|
|
|
|
2019-06-19 15:09:03 +00:00
|
|
|
setupList(inner, FileType::Translation, tr::lng_passport_translation(tr::now));
|
2018-08-14 15:27:47 +00:00
|
|
|
|
2018-05-11 22:55:56 +00:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditScans::init() {
|
2018-04-04 19:26:40 +00:00
|
|
|
_controller->scanUpdated(
|
|
|
|
) | rpl::start_with_next([=](ScanInfo &&info) {
|
|
|
|
updateScan(std::move(info));
|
|
|
|
}, lifetime());
|
|
|
|
|
|
|
|
widthValue(
|
|
|
|
) | rpl::start_with_next([=](int width) {
|
|
|
|
_content->resizeToWidth(width);
|
|
|
|
}, _content->lifetime());
|
|
|
|
|
|
|
|
_content->heightValue(
|
|
|
|
) | rpl::start_with_next([=](int height) {
|
|
|
|
resize(width(), height);
|
|
|
|
}, _content->lifetime());
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditScans::updateScan(ScanInfo &&info) {
|
2018-08-14 15:27:47 +00:00
|
|
|
if (info.type != FileType::Scan && info.type != FileType::Translation) {
|
|
|
|
updateSpecialScan(std::move(info));
|
2018-04-10 19:00:52 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-08-14 15:27:47 +00:00
|
|
|
list(info.type).updateScan(std::move(info), width());
|
2018-08-14 11:37:03 +00:00
|
|
|
updateErrorLabels();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditScans::scanFieldsChanged(bool changed) {
|
|
|
|
if (_scanFieldsChanged != changed) {
|
|
|
|
_scanFieldsChanged = changed;
|
|
|
|
updateErrorLabels();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditScans::updateErrorLabels() {
|
2018-08-14 15:27:47 +00:00
|
|
|
const auto updateList = [&](FileType type) {
|
|
|
|
auto &list = this->list(type);
|
|
|
|
if (list.uploadMoreError) {
|
|
|
|
list.uploadMoreError->toggle(
|
|
|
|
!list.uploadedSomeMore(),
|
|
|
|
anim::type::normal);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
updateList(FileType::Scan);
|
|
|
|
updateList(FileType::Translation);
|
2018-08-14 11:37:03 +00:00
|
|
|
if (_commonError) {
|
|
|
|
_commonError->toggle(!somethingChanged(), anim::type::normal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EditScans::somethingChanged() const {
|
2018-08-14 15:27:47 +00:00
|
|
|
return list(FileType::Scan).uploadedSomeMore()
|
|
|
|
|| list(FileType::Translation).uploadedSomeMore()
|
|
|
|
|| _scanFieldsChanged
|
|
|
|
|| _specialScanChanged;
|
2018-04-04 19:26:40 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 15:27:47 +00:00
|
|
|
void EditScans::updateSpecialScan(ScanInfo &&info) {
|
2018-04-18 17:23:37 +00:00
|
|
|
Expects(info.key.id != 0);
|
|
|
|
|
2018-08-14 15:27:47 +00:00
|
|
|
const auto type = info.type;
|
2018-05-11 22:55:56 +00:00
|
|
|
const auto i = _specialScans.find(type);
|
|
|
|
if (i == end(_specialScans)) {
|
2018-04-18 17:23:37 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-05-11 22:55:56 +00:00
|
|
|
auto &scan = i->second;
|
|
|
|
if (scan.file.key.id) {
|
2018-08-14 15:27:47 +00:00
|
|
|
UpdateFileRow(scan.row->entity(), info);
|
2018-07-10 09:18:30 +00:00
|
|
|
scan.rowCreated = !info.deleted;
|
2018-08-14 11:37:03 +00:00
|
|
|
if (scan.file.key.id != info.key.id) {
|
|
|
|
specialScanChanged(type, true);
|
2018-04-18 17:23:37 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-07-10 09:18:30 +00:00
|
|
|
const auto requiresBothSides
|
2018-08-14 15:27:47 +00:00
|
|
|
= (_specialScans.find(FileType::ReverseSide)
|
2018-07-10 09:18:30 +00:00
|
|
|
!= end(_specialScans));
|
|
|
|
createSpecialScanRow(scan, info, requiresBothSides);
|
2018-05-11 22:55:56 +00:00
|
|
|
scan.wrap->resizeToWidth(width());
|
|
|
|
scan.row->show(anim::type::normal);
|
2018-08-17 14:59:07 +00:00
|
|
|
if (scan.header) {
|
|
|
|
scan.header->show(anim::type::normal);
|
|
|
|
}
|
2018-08-14 11:37:03 +00:00
|
|
|
specialScanChanged(type, true);
|
2018-04-18 17:23:37 +00:00
|
|
|
}
|
2018-05-11 22:55:56 +00:00
|
|
|
scan.file = std::move(info);
|
2018-04-18 17:23:37 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 22:55:56 +00:00
|
|
|
void EditScans::createSpecialScanRow(
|
|
|
|
SpecialScan &scan,
|
2018-07-10 09:18:30 +00:00
|
|
|
const ScanInfo &info,
|
|
|
|
bool requiresBothSides) {
|
2018-08-14 15:27:47 +00:00
|
|
|
Expects(scan.file.type != FileType::Scan
|
|
|
|
&& scan.file.type != FileType::Translation);
|
2018-05-11 22:55:56 +00:00
|
|
|
|
2018-08-14 15:27:47 +00:00
|
|
|
const auto type = scan.file.type;
|
2018-05-11 22:55:56 +00:00
|
|
|
const auto name = [&] {
|
|
|
|
switch (type) {
|
2018-08-14 15:27:47 +00:00
|
|
|
case FileType::FrontSide:
|
2019-06-19 15:09:03 +00:00
|
|
|
return requiresBothSides
|
|
|
|
? tr::lng_passport_front_side_title(tr::now)
|
|
|
|
: tr::lng_passport_main_page_title(tr::now);
|
2018-08-14 15:27:47 +00:00
|
|
|
case FileType::ReverseSide:
|
2019-06-19 15:09:03 +00:00
|
|
|
return tr::lng_passport_reverse_side_title(tr::now);
|
2018-08-14 15:27:47 +00:00
|
|
|
case FileType::Selfie:
|
2019-06-19 15:09:03 +00:00
|
|
|
return tr::lng_passport_selfie_title(tr::now);
|
2018-05-11 22:55:56 +00:00
|
|
|
}
|
|
|
|
Unexpected("Type in special file name.");
|
|
|
|
}();
|
2018-08-14 15:27:47 +00:00
|
|
|
scan.row = CreateScan(scan.wrap, info, name);
|
2018-05-11 22:55:56 +00:00
|
|
|
const auto row = scan.row->entity();
|
2018-04-10 19:00:52 +00:00
|
|
|
|
|
|
|
row->deleteClicks(
|
|
|
|
) | rpl::start_with_next([=] {
|
2018-09-21 16:28:46 +00:00
|
|
|
_controller->deleteScan(type, std::nullopt);
|
2018-04-10 19:00:52 +00:00
|
|
|
}, row->lifetime());
|
|
|
|
|
|
|
|
row->restoreClicks(
|
|
|
|
) | rpl::start_with_next([=] {
|
2018-09-21 16:28:46 +00:00
|
|
|
_controller->restoreScan(type, std::nullopt);
|
2018-04-10 19:00:52 +00:00
|
|
|
}, row->lifetime());
|
2018-04-13 10:54:17 +00:00
|
|
|
|
2018-07-10 09:18:30 +00:00
|
|
|
scan.rowCreated = !info.deleted;
|
2018-04-10 19:00:52 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 15:27:47 +00:00
|
|
|
void EditScans::chooseScan(FileType type) {
|
|
|
|
if (!_controller->canAddScan(type)) {
|
2019-06-19 15:09:03 +00:00
|
|
|
_controller->showToast(tr::lng_passport_scans_limit_reached(tr::now));
|
2018-04-13 10:54:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-08-14 15:27:47 +00:00
|
|
|
ChooseScan(this, type, [=](QByteArray &&content) {
|
|
|
|
_controller->uploadScan(type, std::move(content));
|
2018-04-20 17:34:51 +00:00
|
|
|
}, [=](ReadScanError error) {
|
|
|
|
_controller->readScanError(error);
|
2018-08-14 15:27:47 +00:00
|
|
|
});
|
2018-04-10 19:00:52 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 15:19:34 +00:00
|
|
|
void EditScans::ChooseScan(
|
|
|
|
QPointer<QWidget> parent,
|
2018-08-14 15:27:47 +00:00
|
|
|
FileType type,
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<void(QByteArray&&)> doneCallback,
|
2018-08-14 15:27:47 +00:00
|
|
|
Fn<void(ReadScanError)> errorCallback) {
|
2018-04-17 15:19:34 +00:00
|
|
|
Expects(parent != nullptr);
|
|
|
|
|
2018-07-10 20:51:07 +00:00
|
|
|
const auto processFiles = std::make_shared<Fn<void(QStringList&&)>>();
|
2018-04-04 19:26:40 +00:00
|
|
|
const auto filter = FileDialog::AllFilesFilter()
|
|
|
|
+ qsl(";;Image files (*")
|
|
|
|
+ cImgExtensions().join(qsl(" *"))
|
|
|
|
+ qsl(")");
|
2018-06-04 15:35:11 +00:00
|
|
|
const auto guardedCallback = crl::guard(parent, doneCallback);
|
|
|
|
const auto guardedError = crl::guard(parent, errorCallback);
|
2018-07-10 20:51:07 +00:00
|
|
|
const auto onMainCallback = [=](
|
|
|
|
QByteArray &&content,
|
|
|
|
QStringList &&remainingFiles) {
|
|
|
|
crl::on_main([
|
|
|
|
=,
|
|
|
|
bytes = std::move(content),
|
|
|
|
remainingFiles = std::move(remainingFiles)
|
|
|
|
]() mutable {
|
2018-04-20 17:34:51 +00:00
|
|
|
guardedCallback(std::move(bytes));
|
2018-07-10 20:51:07 +00:00
|
|
|
(*processFiles)(std::move(remainingFiles));
|
2018-04-20 17:34:51 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
const auto onMainError = [=](ReadScanError error) {
|
|
|
|
crl::on_main([=] {
|
|
|
|
guardedError(error);
|
|
|
|
});
|
|
|
|
};
|
2018-07-10 20:51:07 +00:00
|
|
|
const auto processImage = [=](
|
|
|
|
QByteArray &&content,
|
|
|
|
QStringList &&remainingFiles) {
|
|
|
|
crl::async([
|
|
|
|
=,
|
|
|
|
bytes = std::move(content),
|
|
|
|
remainingFiles = std::move(remainingFiles)
|
|
|
|
]() mutable {
|
2018-04-20 17:34:51 +00:00
|
|
|
auto result = ProcessImage(std::move(bytes));
|
|
|
|
if (const auto error = base::get_if<ReadScanError>(&result)) {
|
|
|
|
onMainError(*error);
|
|
|
|
} else {
|
|
|
|
auto content = base::get_if<QByteArray>(&result);
|
|
|
|
Assert(content != nullptr);
|
2018-07-10 20:51:07 +00:00
|
|
|
onMainCallback(std::move(*content), std::move(remainingFiles));
|
2018-04-20 17:34:51 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2018-07-10 20:51:07 +00:00
|
|
|
const auto processOpened = [=](FileDialog::OpenResult &&result) {
|
|
|
|
if (result.paths.size() > 0) {
|
|
|
|
(*processFiles)(std::move(result.paths));
|
|
|
|
} else if (!result.remoteContent.isEmpty()) {
|
|
|
|
processImage(std::move(result.remoteContent), {});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
*processFiles = [=](QStringList &&files) {
|
|
|
|
while (!files.isEmpty()) {
|
|
|
|
auto file = files.front();
|
|
|
|
files.removeAt(0);
|
|
|
|
|
2018-04-10 07:51:19 +00:00
|
|
|
auto content = [&] {
|
2018-07-10 20:51:07 +00:00
|
|
|
QFile f(file);
|
2018-04-20 17:34:51 +00:00
|
|
|
if (f.size() > App::kImageSizeLimit) {
|
|
|
|
guardedError(ReadScanError::FileTooLarge);
|
|
|
|
return QByteArray();
|
|
|
|
} else if (!f.open(QIODevice::ReadOnly)) {
|
|
|
|
guardedError(ReadScanError::CantReadImage);
|
2018-04-10 07:51:19 +00:00
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
return f.readAll();
|
|
|
|
}();
|
|
|
|
if (!content.isEmpty()) {
|
2018-07-10 20:51:07 +00:00
|
|
|
processImage(std::move(content), std::move(files));
|
|
|
|
return;
|
2018-04-10 07:51:19 +00:00
|
|
|
}
|
2018-04-04 19:26:40 +00:00
|
|
|
}
|
|
|
|
};
|
2018-08-14 15:27:47 +00:00
|
|
|
const auto allowMany = (type == FileType::Scan)
|
|
|
|
|| (type == FileType::Translation);
|
2018-07-10 20:51:07 +00:00
|
|
|
(allowMany ? FileDialog::GetOpenPaths : FileDialog::GetOpenPath)(
|
2018-04-17 15:19:34 +00:00
|
|
|
parent,
|
2019-06-19 15:09:03 +00:00
|
|
|
tr::lng_passport_choose_image(tr::now),
|
2018-04-04 19:26:40 +00:00
|
|
|
filter,
|
2018-07-10 20:51:07 +00:00
|
|
|
processOpened,
|
|
|
|
nullptr);
|
2018-04-04 19:26:40 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 15:27:47 +00:00
|
|
|
void EditScans::hideSpecialScanError(FileType type) {
|
2018-05-11 22:55:56 +00:00
|
|
|
toggleSpecialScanError(type, false);
|
2018-04-13 10:54:17 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 15:27:47 +00:00
|
|
|
void EditScans::specialScanChanged(FileType type, bool changed) {
|
2018-08-14 11:37:03 +00:00
|
|
|
hideSpecialScanError(type);
|
|
|
|
if (_specialScanChanged != changed) {
|
|
|
|
_specialScanChanged = changed;
|
|
|
|
updateErrorLabels();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 15:27:47 +00:00
|
|
|
auto EditScans::findSpecialScan(FileType type) -> SpecialScan& {
|
2018-05-11 22:55:56 +00:00
|
|
|
const auto i = _specialScans.find(type);
|
|
|
|
Assert(i != end(_specialScans));
|
|
|
|
return i->second;
|
|
|
|
}
|
|
|
|
|
2018-08-14 15:27:47 +00:00
|
|
|
void EditScans::toggleSpecialScanError(FileType type, bool shown) {
|
2018-05-11 22:55:56 +00:00
|
|
|
auto &scan = findSpecialScan(type);
|
|
|
|
if (scan.errorShown != shown) {
|
|
|
|
scan.errorShown = shown;
|
|
|
|
scan.errorAnimation.start(
|
|
|
|
[=] { specialScanErrorAnimationCallback(type); },
|
|
|
|
scan.errorShown ? 0. : 1.,
|
|
|
|
scan.errorShown ? 1. : 0.,
|
2018-04-13 10:54:17 +00:00
|
|
|
st::passportDetailsField.duration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 15:27:47 +00:00
|
|
|
void EditScans::specialScanErrorAnimationCallback(FileType type) {
|
2018-05-11 22:55:56 +00:00
|
|
|
auto &scan = findSpecialScan(type);
|
2019-04-02 09:13:30 +00:00
|
|
|
const auto error = scan.errorAnimation.value(
|
2018-05-11 22:55:56 +00:00
|
|
|
scan.errorShown ? 1. : 0.);
|
2018-04-13 10:54:17 +00:00
|
|
|
if (error == 0.) {
|
2018-09-21 16:28:46 +00:00
|
|
|
scan.upload->setColorOverride(std::nullopt);
|
2018-04-13 10:54:17 +00:00
|
|
|
} else {
|
2018-05-11 22:55:56 +00:00
|
|
|
scan.upload->setColorOverride(anim::color(
|
2018-04-13 10:54:17 +00:00
|
|
|
st::passportUploadButton.textFg,
|
|
|
|
st::boxTextFgError,
|
|
|
|
error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-11 22:55:56 +00:00
|
|
|
EditScans::~EditScans() = default;
|
|
|
|
|
2018-04-04 19:26:40 +00:00
|
|
|
} // namespace Passport
|