tdesktop/Telegram/SourceFiles/boxes/confirm_box.cpp

1050 lines
30 KiB
C++
Raw Normal View History

/*
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
*/
2017-04-06 14:38:10 +00:00
#include "boxes/confirm_box.h"
2017-04-13 08:27:10 +00:00
#include "lang/lang_keys.h"
#include "mainwidget.h"
#include "mainwindow.h"
#include "apiwrap.h"
#include "history/history.h"
#include "history/history_item.h"
#include "ui/widgets/checkbox.h"
#include "ui/widgets/buttons.h"
#include "ui/widgets/labels.h"
2018-12-05 08:07:17 +00:00
#include "ui/wrap/vertical_layout.h"
#include "ui/toast/toast.h"
#include "ui/image/image.h"
#include "ui/text/text_utilities.h"
#include "ui/empty_userpic.h"
#include "core/click_handler_types.h"
#include "window/window_session_controller.h"
2017-03-04 10:23:56 +00:00
#include "storage/localstorage.h"
2019-08-08 22:39:42 +00:00
#include "data/data_scheduled_messages.h"
#include "data/data_session.h"
#include "data/data_photo.h"
#include "data/data_channel.h"
#include "data/data_chat.h"
#include "data/data_user.h"
#include "data/data_file_origin.h"
#include "data/data_histories.h"
2020-05-25 14:16:04 +00:00
#include "data/data_photo_media.h"
#include "data/data_changes.h"
#include "base/unixtime.h"
2019-07-24 11:45:24 +00:00
#include "main/main_session.h"
#include "mtproto/mtproto_config.h"
#include "facades.h" // Ui::showChatsList
#include "app.h"
2019-09-18 11:19:05 +00:00
#include "styles/style_layers.h"
#include "styles/style_boxes.h"
2019-09-04 07:19:15 +00:00
#include <QtGui/QGuiApplication>
#include <QtGui/QClipboard>
namespace {
TextParseOptions kInformBoxTextOptions = {
(TextParseLinks
| TextParseMultiline
| TextParseMarkdown
| TextParseRichText), // flags
0, // maxw
0, // maxh
Qt::LayoutDirectionAuto, // dir
};
TextParseOptions kMarkedTextBoxOptions = {
(TextParseLinks
| TextParseMultiline
| TextParseMarkdown
| TextParseRichText
| TextParseMentions
| TextParseHashtags), // flags
0, // maxw
0, // maxh
Qt::LayoutDirectionAuto, // dir
};
} // namespace
ConfirmBox::ConfirmBox(
QWidget*,
const QString &text,
FnMut<void()> confirmedCallback,
FnMut<void()> cancelledCallback)
2019-06-19 15:09:03 +00:00
: _confirmText(tr::lng_box_ok(tr::now))
, _cancelText(tr::lng_cancel(tr::now))
, _confirmStyle(st::defaultBoxButton)
, _text(st::boxWidth - st::boxPadding.left() - st::defaultBox.buttonPadding.right())
, _confirmedCallback(std::move(confirmedCallback))
, _cancelledCallback(std::move(cancelledCallback)) {
init(text);
}
ConfirmBox::ConfirmBox(
QWidget*,
const QString &text,
const QString &confirmText,
FnMut<void()> confirmedCallback,
FnMut<void()> cancelledCallback)
: _confirmText(confirmText)
2019-06-19 15:09:03 +00:00
, _cancelText(tr::lng_cancel(tr::now))
, _confirmStyle(st::defaultBoxButton)
, _text(st::boxWidth - st::boxPadding.left() - st::defaultBox.buttonPadding.right())
, _confirmedCallback(std::move(confirmedCallback))
, _cancelledCallback(std::move(cancelledCallback)) {
init(text);
}
ConfirmBox::ConfirmBox(
QWidget*,
const TextWithEntities &text,
const QString &confirmText,
FnMut<void()> confirmedCallback,
FnMut<void()> cancelledCallback)
: _confirmText(confirmText)
2019-06-19 15:09:03 +00:00
, _cancelText(tr::lng_cancel(tr::now))
, _confirmStyle(st::defaultBoxButton)
, _text(st::boxWidth - st::boxPadding.left() - st::defaultBox.buttonPadding.right())
, _confirmedCallback(std::move(confirmedCallback))
, _cancelledCallback(std::move(cancelledCallback)) {
init(text);
}
ConfirmBox::ConfirmBox(
QWidget*,
const QString &text,
const QString &confirmText,
const style::RoundButton &confirmStyle,
FnMut<void()> confirmedCallback,
FnMut<void()> cancelledCallback)
: _confirmText(confirmText)
2019-06-19 15:09:03 +00:00
, _cancelText(tr::lng_cancel(tr::now))
, _confirmStyle(confirmStyle)
, _text(st::boxWidth - st::boxPadding.left() - st::defaultBox.buttonPadding.right())
, _confirmedCallback(std::move(confirmedCallback))
, _cancelledCallback(std::move(cancelledCallback)) {
init(text);
}
ConfirmBox::ConfirmBox(
QWidget*,
const QString &text,
const QString &confirmText,
const QString &cancelText,
FnMut<void()> confirmedCallback,
FnMut<void()> cancelledCallback)
: _confirmText(confirmText)
, _cancelText(cancelText)
, _confirmStyle(st::defaultBoxButton)
, _text(st::boxWidth - st::boxPadding.left() - st::defaultBox.buttonPadding.right())
, _confirmedCallback(std::move(confirmedCallback))
, _cancelledCallback(std::move(cancelledCallback)) {
init(text);
}
ConfirmBox::ConfirmBox(
QWidget*,
const QString &text,
const QString &confirmText,
const style::RoundButton &confirmStyle,
const QString &cancelText,
FnMut<void()> confirmedCallback,
FnMut<void()> cancelledCallback)
: _confirmText(confirmText)
, _cancelText(cancelText)
, _confirmStyle(st::defaultBoxButton)
, _text(st::boxWidth - st::boxPadding.left() - st::defaultBox.buttonPadding.right())
, _confirmedCallback(std::move(confirmedCallback))
, _cancelledCallback(std::move(cancelledCallback)) {
init(text);
}
ConfirmBox::ConfirmBox(
const InformBoxTag &,
const QString &text,
const QString &doneText,
Fn<void()> closedCallback)
: _confirmText(doneText)
, _confirmStyle(st::defaultBoxButton)
2016-03-04 22:04:15 +00:00
, _informative(true)
, _text(st::boxWidth - st::boxPadding.left() - st::defaultBox.buttonPadding.right())
, _confirmedCallback(generateInformCallback(closedCallback))
, _cancelledCallback(generateInformCallback(closedCallback)) {
init(text);
}
ConfirmBox::ConfirmBox(
const InformBoxTag &,
const TextWithEntities &text,
const QString &doneText,
Fn<void()> closedCallback)
: _confirmText(doneText)
, _confirmStyle(st::defaultBoxButton)
, _informative(true)
, _text(st::boxWidth - st::boxPadding.left() - st::defaultBox.buttonPadding.right())
, _confirmedCallback(generateInformCallback(closedCallback))
, _cancelledCallback(generateInformCallback(closedCallback)) {
init(text);
}
FnMut<void()> ConfirmBox::generateInformCallback(
Fn<void()> closedCallback) {
return crl::guard(this, [=] {
closeBox();
if (closedCallback) {
closedCallback();
}
});
}
void ConfirmBox::init(const QString &text) {
_text.setText(
st::boxLabelStyle,
text,
_informative ? kInformBoxTextOptions : _textPlainOptions);
}
void ConfirmBox::init(const TextWithEntities &text) {
_text.setMarkedText(st::boxLabelStyle, text, kMarkedTextBoxOptions);
}
void ConfirmBox::prepare() {
addButton(
rpl::single(_confirmText),
[=] { confirmed(); },
_confirmStyle);
if (!_informative) {
addButton(
rpl::single(_cancelText),
[=] { _cancelled = true; closeBox(); });
}
boxClosing() | rpl::start_with_next([=] {
2018-11-26 11:00:31 +00:00
if (!_confirmed && (!_strictCancel || _cancelled)) {
if (auto callback = std::move(_cancelledCallback)) {
callback();
}
}
}, lifetime());
textUpdated();
}
2018-05-30 15:08:12 +00:00
void ConfirmBox::setMaxLineCount(int count) {
if (_maxLineCount != count) {
_maxLineCount = count;
textUpdated();
}
}
void ConfirmBox::textUpdated() {
_textWidth = st::boxWidth - st::boxPadding.left() - st::defaultBox.buttonPadding.right();
2018-05-30 15:08:12 +00:00
_textHeight = _text.countHeight(_textWidth);
if (_maxLineCount > 0) {
accumulate_min(_textHeight, _maxLineCount * st::boxLabelStyle.lineHeight);
}
setDimensions(st::boxWidth, st::boxPadding.top() + _textHeight + st::boxPadding.bottom());
setMouseTracking(_text.hasLinks());
}
void ConfirmBox::confirmed() {
if (!_confirmed) {
_confirmed = true;
2018-11-26 11:00:31 +00:00
if (auto callback = std::move(_confirmedCallback)) {
callback();
}
}
}
void ConfirmBox::mouseMoveEvent(QMouseEvent *e) {
_lastMousePos = e->globalPos();
updateHover();
}
void ConfirmBox::mousePressEvent(QMouseEvent *e) {
_lastMousePos = e->globalPos();
updateHover();
ClickHandler::pressed();
return BoxContent::mousePressEvent(e);
}
void ConfirmBox::mouseReleaseEvent(QMouseEvent *e) {
_lastMousePos = e->globalPos();
updateHover();
2018-11-26 11:00:31 +00:00
if (const auto activated = ClickHandler::unpressed()) {
2019-09-16 11:14:06 +00:00
const auto guard = window();
2015-12-07 18:09:05 +00:00
Ui::hideLayer();
2019-09-16 11:14:06 +00:00
ActivateClickHandler(guard, activated, e->button());
2018-11-26 11:00:31 +00:00
return;
}
2018-11-26 11:00:31 +00:00
BoxContent::mouseReleaseEvent(e);
}
void ConfirmBox::leaveEventHook(QEvent *e) {
ClickHandler::clearActive(this);
}
void ConfirmBox::clickHandlerActiveChanged(const ClickHandlerPtr &p, bool active) {
setCursor(active ? style::cur_pointer : style::cur_default);
update();
}
void ConfirmBox::clickHandlerPressedChanged(const ClickHandlerPtr &p, bool pressed) {
update();
}
void ConfirmBox::updateLink() {
_lastMousePos = QCursor::pos();
updateHover();
}
void ConfirmBox::updateHover() {
2017-06-21 21:38:31 +00:00
auto m = mapFromGlobal(_lastMousePos);
auto state = _text.getStateLeft(m - QPoint(st::boxPadding.left(), st::boxPadding.top()), _textWidth, width());
ClickHandler::setActive(state.link, this);
}
void ConfirmBox::keyPressEvent(QKeyEvent *e) {
if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
confirmed();
} else {
BoxContent::keyPressEvent(e);
}
}
void ConfirmBox::paintEvent(QPaintEvent *e) {
BoxContent::paintEvent(e);
Painter p(this);
// draw box title / text
2016-10-31 12:29:26 +00:00
p.setPen(st::boxTextFg);
2018-05-30 15:08:12 +00:00
if (_maxLineCount > 0) {
_text.drawLeftElided(p, st::boxPadding.left(), st::boxPadding.top(), _textWidth, width(), _maxLineCount, style::al_left);
} else {
_text.drawLeft(p, st::boxPadding.left(), st::boxPadding.top(), _textWidth, width(), style::al_left);
}
}
2019-06-19 15:09:03 +00:00
InformBox::InformBox(QWidget*, const QString &text, Fn<void()> closedCallback) : ConfirmBox(ConfirmBox::InformBoxTag(), text, tr::lng_box_ok(tr::now), std::move(closedCallback)) {
}
InformBox::InformBox(QWidget*, const QString &text, const QString &doneText, Fn<void()> closedCallback) : ConfirmBox(ConfirmBox::InformBoxTag(), text, doneText, std::move(closedCallback)) {
}
2019-06-19 15:09:03 +00:00
InformBox::InformBox(QWidget*, const TextWithEntities &text, Fn<void()> closedCallback) : ConfirmBox(ConfirmBox::InformBoxTag(), text, tr::lng_box_ok(tr::now), std::move(closedCallback)) {
}
InformBox::InformBox(QWidget*, const TextWithEntities &text, const QString &doneText, Fn<void()> closedCallback) : ConfirmBox(ConfirmBox::InformBoxTag(), text, doneText, std::move(closedCallback)) {
}
MaxInviteBox::MaxInviteBox(QWidget*, not_null<ChannelData*> channel) : BoxContent()
, _channel(channel)
, _text(
st::boxLabelStyle,
tr::lng_participant_invite_sorry(
tr::now,
lt_count,
channel->session().serverConfig().chatSizeMax),
kInformBoxTextOptions,
(st::boxWidth
- st::boxPadding.left()
- st::defaultBox.buttonPadding.right())) {
2016-09-15 19:15:49 +00:00
}
void MaxInviteBox::prepare() {
setMouseTracking(true);
addButton(tr::lng_box_ok(), [=] { closeBox(); });
_textWidth = st::boxWidth - st::boxPadding.left() - st::defaultBox.buttonPadding.right();
_textHeight = qMin(_text.countHeight(_textWidth), 16 * st::boxLabelStyle.lineHeight);
setDimensions(st::boxWidth, st::boxPadding.top() + _textHeight + st::boxTextFont->height + st::boxTextFont->height * 2 + st::newGroupLinkPadding.bottom());
_channel->session().changes().peerUpdates(
_channel,
Data::PeerUpdate::Flag::InviteLink
) | rpl::start_with_next([=] {
rtlupdate(_invitationLink);
}, lifetime());
}
void MaxInviteBox::mouseMoveEvent(QMouseEvent *e) {
updateSelected(e->globalPos());
}
void MaxInviteBox::mousePressEvent(QMouseEvent *e) {
mouseMoveEvent(e);
if (_linkOver) {
if (_channel->inviteLink().isEmpty()) {
_channel->session().api().exportInviteLink(_channel);
} else {
QGuiApplication::clipboard()->setText(_channel->inviteLink());
2019-06-19 15:09:03 +00:00
Ui::Toast::Show(tr::lng_create_channel_link_copied(tr::now));
}
}
}
void MaxInviteBox::leaveEventHook(QEvent *e) {
updateSelected(QCursor::pos());
}
void MaxInviteBox::updateSelected(const QPoint &cursorGlobalPosition) {
QPoint p(mapFromGlobal(cursorGlobalPosition));
bool linkOver = _invitationLink.contains(p);
if (linkOver != _linkOver) {
_linkOver = linkOver;
update();
setCursor(_linkOver ? style::cur_pointer : style::cur_default);
}
}
void MaxInviteBox::paintEvent(QPaintEvent *e) {
BoxContent::paintEvent(e);
Painter p(this);
// draw box title / text
2016-10-31 12:29:26 +00:00
p.setPen(st::boxTextFg);
2015-10-14 11:51:37 +00:00
_text.drawLeftElided(p, st::boxPadding.left(), st::boxPadding.top(), _textWidth, width(), 16, style::al_left);
QTextOption option(style::al_left);
option.setWrapMode(QTextOption::WrapAnywhere);
2015-10-12 21:02:10 +00:00
p.setFont(_linkOver ? st::defaultInputField.font->underline() : st::defaultInputField.font);
p.setPen(st::defaultLinkButton.color);
2019-06-19 15:09:03 +00:00
auto inviteLinkText = _channel->inviteLink().isEmpty() ? tr::lng_group_invite_create(tr::now) : _channel->inviteLink();
p.drawText(_invitationLink, inviteLinkText, option);
}
void MaxInviteBox::resizeEvent(QResizeEvent *e) {
BoxContent::resizeEvent(e);
2015-10-11 08:37:24 +00:00
_invitationLink = myrtlrect(st::boxPadding.left(), st::boxPadding.top() + _textHeight + st::boxTextFont->height, width() - st::boxPadding.left() - st::boxPadding.right(), 2 * st::boxTextFont->height);
}
2016-03-04 22:04:15 +00:00
PinMessageBox::PinMessageBox(
QWidget*,
not_null<PeerData*> peer,
MsgId msgId)
: _peer(peer)
, _api(&peer->session().mtp())
, _msgId(msgId)
2019-06-19 15:09:03 +00:00
, _text(this, tr::lng_pinned_pin_sure(tr::now), st::boxLabel) {
}
void PinMessageBox::prepare() {
addButton(tr::lng_pinned_pin(), [this] { pinMessage(); });
addButton(tr::lng_cancel(), [this] { closeBox(); });
if (_peer->isChat() || _peer->isMegagroup()) {
2019-06-19 15:09:03 +00:00
_notify.create(this, tr::lng_pinned_notify(tr::now), true, st::defaultBoxCheckbox);
2017-11-21 13:23:56 +00:00
}
auto height = st::boxPadding.top() + _text->height() + st::boxPadding.bottom();
if (_notify) {
height += st::boxMediumSkip + _notify->heightNoMargins();
}
setDimensions(st::boxWidth, height);
}
void PinMessageBox::resizeEvent(QResizeEvent *e) {
BoxContent::resizeEvent(e);
_text->moveToLeft(st::boxPadding.left(), st::boxPadding.top());
2017-11-21 13:23:56 +00:00
if (_notify) {
_notify->moveToLeft(st::boxPadding.left(), _text->y() + _text->height() + st::boxMediumSkip);
}
}
void PinMessageBox::keyPressEvent(QKeyEvent *e) {
if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
pinMessage();
} else {
BoxContent::keyPressEvent(e);
}
}
void PinMessageBox::pinMessage() {
if (_requestId) return;
auto flags = MTPmessages_UpdatePinnedMessage::Flags(0);
2017-11-21 13:23:56 +00:00
if (_notify && !_notify->checked()) {
flags |= MTPmessages_UpdatePinnedMessage::Flag::f_silent;
}
_requestId = _api.request(MTPmessages_UpdatePinnedMessage(
MTP_flags(flags),
_peer->input,
MTP_int(_msgId)
)).done([=](const MTPUpdates &result) {
_peer->session().api().applyUpdates(result);
Ui::hideLayer();
}).fail([=](const RPCError &error) {
Ui::hideLayer();
}).send();
}
DeleteMessagesBox::DeleteMessagesBox(
QWidget*,
not_null<HistoryItem*> item,
bool suggestModerateActions)
2019-07-24 14:00:30 +00:00
: _session(&item->history()->session())
, _ids(1, item->fullId()) {
2017-06-04 11:09:29 +00:00
if (suggestModerateActions) {
_moderateBan = item->suggestBanReport();
_moderateDeleteAll = item->suggestDeleteAllReport();
if (_moderateBan || _moderateDeleteAll) {
_moderateFrom = item->from()->asUser();
_moderateInChannel = item->history()->peer->asChannel();
}
}
}
DeleteMessagesBox::DeleteMessagesBox(
QWidget*,
2019-07-24 14:00:30 +00:00
not_null<Main::Session*> session,
MessageIdsList &&selected)
2019-07-24 14:00:30 +00:00
: _session(session)
, _ids(std::move(selected)) {
Expects(!_ids.empty());
}
2019-03-21 13:48:40 +00:00
DeleteMessagesBox::DeleteMessagesBox(
QWidget*,
not_null<PeerData*> peer,
bool justClear)
2019-07-24 14:00:30 +00:00
: _session(&peer->session())
, _wipeHistoryPeer(peer)
2019-03-21 13:48:40 +00:00
, _wipeHistoryJustClear(justClear) {
}
void DeleteMessagesBox::prepare() {
2019-03-21 12:49:27 +00:00
auto details = TextWithEntities();
const auto appendDetails = [&](TextWithEntities &&text) {
details.append(qstr("\n\n")).append(std::move(text));
2019-03-21 12:49:27 +00:00
};
auto deleteText = tr::lng_box_delete();
2019-03-21 13:48:40 +00:00
auto deleteStyle = &st::defaultBoxButton;
if (const auto peer = _wipeHistoryPeer) {
if (_wipeHistoryJustClear) {
details.text = peer->isSelf()
2019-06-19 15:09:03 +00:00
? tr::lng_sure_delete_saved_messages(tr::now)
2019-03-21 13:48:40 +00:00
: peer->isUser()
? tr::lng_sure_delete_history(tr::now, lt_contact, peer->name)
: tr::lng_sure_delete_group_history(tr::now, lt_group, peer->name);
2019-03-21 13:48:40 +00:00
deleteStyle = &st::attentionBoxButton;
} else {
details.text = peer->isSelf()
2019-06-19 15:09:03 +00:00
? tr::lng_sure_delete_saved_messages(tr::now)
2019-03-21 13:48:40 +00:00
: peer->isUser()
2019-06-19 15:09:03 +00:00
? tr::lng_sure_delete_history(tr::now, lt_contact, peer->name)
2019-03-21 13:48:40 +00:00
: peer->isChat()
2019-06-19 15:09:03 +00:00
? tr::lng_sure_delete_and_exit(tr::now, lt_group, peer->name)
: peer->isMegagroup()
? tr::lng_sure_leave_group(tr::now)
: tr::lng_sure_leave_channel(tr::now);
deleteText = _wipeHistoryPeer->isUser()
? tr::lng_box_delete()
: tr::lng_box_leave();
2019-03-21 13:48:40 +00:00
deleteStyle = &(peer->isChannel()
? st::defaultBoxButton
: st::attentionBoxButton);
}
if (auto revoke = revokeText(peer)) {
_revoke.create(this, revoke->checkbox, false, st::defaultBoxCheckbox);
appendDetails(std::move(revoke->description));
}
} else if (_moderateFrom) {
Assert(_moderateInChannel != nullptr);
2019-06-19 15:09:03 +00:00
details.text = tr::lng_selected_delete_sure_this(tr::now);
2017-06-04 11:09:29 +00:00
if (_moderateBan) {
2019-06-19 15:09:03 +00:00
_banUser.create(this, tr::lng_ban_user(tr::now), false, st::defaultBoxCheckbox);
2017-06-04 11:09:29 +00:00
}
2019-06-19 15:09:03 +00:00
_reportSpam.create(this, tr::lng_report_spam(tr::now), false, st::defaultBoxCheckbox);
2017-06-04 11:09:29 +00:00
if (_moderateDeleteAll) {
2019-06-19 15:09:03 +00:00
_deleteAll.create(this, tr::lng_delete_all_from(tr::now), false, st::defaultBoxCheckbox);
2017-06-04 11:09:29 +00:00
}
} else {
2019-03-21 12:49:27 +00:00
details.text = (_ids.size() == 1)
2019-06-19 15:09:03 +00:00
? tr::lng_selected_delete_sure_this(tr::now)
: tr::lng_selected_delete_sure(tr::now, lt_count, _ids.size());
2019-03-20 13:02:02 +00:00
if (const auto peer = checkFromSinglePeer()) {
auto count = int(_ids.size());
2019-08-16 15:08:51 +00:00
if (hasScheduledMessages()) {
} else if (auto revoke = revokeText(peer)) {
2019-03-21 12:49:27 +00:00
_revoke.create(this, revoke->checkbox, false, st::defaultBoxCheckbox);
appendDetails(std::move(revoke->description));
2019-08-16 15:08:51 +00:00
} else if (peer->isChannel()) {
2019-03-20 13:02:02 +00:00
if (peer->isMegagroup()) {
appendDetails({ tr::lng_delete_for_everyone_hint(tr::now, lt_count, count) });
}
2019-03-20 13:02:02 +00:00
} else if (peer->isChat()) {
appendDetails({ tr::lng_delete_for_me_chat_hint(tr::now, lt_count, count) });
2019-03-20 13:02:02 +00:00
} else if (!peer->isSelf()) {
appendDetails({ tr::lng_delete_for_me_hint(tr::now, lt_count, count) });
}
}
}
2019-03-21 12:49:27 +00:00
_text.create(this, rpl::single(std::move(details)), st::boxLabel);
2019-03-21 13:48:40 +00:00
addButton(
std::move(deleteText),
2019-03-21 13:48:40 +00:00
[=] { deleteAndClear(); },
*deleteStyle);
addButton(tr::lng_cancel(), [=] { closeBox(); });
auto fullHeight = st::boxPadding.top() + _text->height() + st::boxPadding.bottom();
if (_moderateFrom) {
2017-06-04 11:09:29 +00:00
fullHeight += st::boxMediumSkip;
if (_banUser) {
fullHeight += _banUser->heightNoMargins() + st::boxLittleSkip;
}
fullHeight += _reportSpam->heightNoMargins();
if (_deleteAll) {
fullHeight += st::boxLittleSkip + _deleteAll->heightNoMargins();
}
2019-03-20 13:02:02 +00:00
} else if (_revoke) {
fullHeight += st::boxMediumSkip + _revoke->heightNoMargins();
}
setDimensions(st::boxWidth, fullHeight);
}
2019-08-16 15:08:51 +00:00
bool DeleteMessagesBox::hasScheduledMessages() const {
for (const auto fullId : std::as_const(_ids)) {
if (const auto item = _session->data().message(fullId)) {
if (item->isScheduled()) {
return true;
}
}
}
return false;
}
2019-03-20 13:02:02 +00:00
PeerData *DeleteMessagesBox::checkFromSinglePeer() const {
auto result = (PeerData*)nullptr;
for (const auto fullId : std::as_const(_ids)) {
2019-07-24 14:00:30 +00:00
if (const auto item = _session->data().message(fullId)) {
2019-03-20 13:02:02 +00:00
const auto peer = item->history()->peer;
if (!result) {
result = peer;
} else if (result != peer) {
return nullptr;
}
}
}
return result;
}
2019-03-21 12:49:27 +00:00
auto DeleteMessagesBox::revokeText(not_null<PeerData*> peer) const
-> std::optional<RevokeConfig> {
2019-03-21 13:48:40 +00:00
auto result = RevokeConfig();
if (peer == _wipeHistoryPeer) {
if (!peer->canRevokeFullHistory()) {
return std::nullopt;
} else if (const auto user = peer->asUser()) {
result.checkbox = tr::lng_delete_for_other_check(
tr::now,
2019-03-21 13:48:40 +00:00
lt_user,
user->firstName);
} else {
2019-06-19 15:09:03 +00:00
result.checkbox = tr::lng_delete_for_everyone_check(tr::now);
2019-03-21 13:48:40 +00:00
}
return result;
}
2019-03-20 13:02:02 +00:00
const auto items = ranges::view::all(
_ids
2019-04-25 12:45:15 +00:00
) | ranges::view::transform([&](FullMsgId id) {
return peer->owner().message(id);
2019-03-20 13:02:02 +00:00
}) | ranges::view::filter([](HistoryItem *item) {
return (item != nullptr);
}) | ranges::to_vector;
2019-03-21 13:48:40 +00:00
2019-03-20 13:02:02 +00:00
if (items.size() != _ids.size()) {
// We don't have information about all messages.
2019-03-21 12:49:27 +00:00
return std::nullopt;
2019-03-20 13:02:02 +00:00
}
const auto now = base::unixtime::now();
2019-03-21 12:49:27 +00:00
const auto canRevoke = [&](HistoryItem * item) {
return item->canDeleteForEveryone(now);
};
2019-03-20 13:02:02 +00:00
const auto cannotRevoke = [&](HistoryItem *item) {
return !item->canDeleteForEveryone(now);
};
const auto canRevokeAll = ranges::none_of(items, cannotRevoke);
2019-03-20 13:02:02 +00:00
auto outgoing = items | ranges::view::filter(&HistoryItem::out);
2019-03-21 12:49:27 +00:00
const auto canRevokeOutgoingCount = canRevokeAll
? -1
: ranges::count_if(outgoing, canRevoke);
2019-03-20 13:02:02 +00:00
2019-03-21 12:49:27 +00:00
if (canRevokeAll) {
2019-03-21 13:48:40 +00:00
if (const auto user = peer->asUser()) {
result.checkbox = tr::lng_delete_for_other_check(
tr::now,
2019-03-20 13:02:02 +00:00
lt_user,
2019-03-21 13:48:40 +00:00
user->firstName);
} else {
result.checkbox = tr::lng_delete_for_everyone_check(tr::now);
2019-03-21 13:48:40 +00:00
}
2019-03-21 12:49:27 +00:00
return result;
} else if (canRevokeOutgoingCount > 0) {
result.checkbox = tr::lng_delete_for_other_my(tr::now);
2019-03-21 12:49:27 +00:00
if (const auto user = peer->asUser()) {
if (canRevokeOutgoingCount == 1) {
result.description = tr::lng_selected_unsend_about_user_one(
tr::now,
2019-03-21 12:49:27 +00:00
lt_user,
Ui::Text::Bold(user->shortName()),
Ui::Text::WithEntities);
2019-03-21 12:49:27 +00:00
} else {
result.description = tr::lng_selected_unsend_about_user(
tr::now,
2019-03-21 12:49:27 +00:00
lt_count,
canRevokeOutgoingCount,
lt_user,
Ui::Text::Bold(user->shortName()),
Ui::Text::WithEntities);
2019-03-21 12:49:27 +00:00
}
} else if (canRevokeOutgoingCount == 1) {
result.description = tr::lng_selected_unsend_about_group_one(
tr::now,
Ui::Text::WithEntities);
2019-03-21 12:49:27 +00:00
} else {
result.description = tr::lng_selected_unsend_about_group(
tr::now,
lt_count,
canRevokeOutgoingCount,
Ui::Text::WithEntities);
2019-03-21 12:49:27 +00:00
}
return result;
}
return std::nullopt;
2019-03-20 13:02:02 +00:00
}
void DeleteMessagesBox::resizeEvent(QResizeEvent *e) {
BoxContent::resizeEvent(e);
_text->moveToLeft(st::boxPadding.left(), st::boxPadding.top());
if (_moderateFrom) {
2017-06-04 11:09:29 +00:00
auto top = _text->bottomNoMargins() + st::boxMediumSkip;
if (_banUser) {
_banUser->moveToLeft(st::boxPadding.left(), top);
top += _banUser->heightNoMargins() + st::boxLittleSkip;
}
_reportSpam->moveToLeft(st::boxPadding.left(), top);
top += _reportSpam->heightNoMargins() + st::boxLittleSkip;
if (_deleteAll) {
_deleteAll->moveToLeft(st::boxPadding.left(), top);
}
2019-03-20 13:02:02 +00:00
} else if (_revoke) {
const auto availableWidth = width() - 2 * st::boxPadding.left();
_revoke->resizeToNaturalWidth(availableWidth);
_revoke->moveToLeft(st::boxPadding.left(), _text->bottomNoMargins() + st::boxMediumSkip);
}
}
void DeleteMessagesBox::keyPressEvent(QKeyEvent *e) {
if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
deleteAndClear();
} else {
BoxContent::keyPressEvent(e);
}
}
void DeleteMessagesBox::deleteAndClear() {
2019-03-21 13:48:40 +00:00
const auto revoke = _revoke ? _revoke->checked() : false;
if (const auto peer = _wipeHistoryPeer) {
const auto justClear = _wipeHistoryJustClear;
closeBox();
if (justClear) {
peer->session().api().clearHistory(peer, revoke);
} else {
for (const auto controller : peer->session().windows()) {
if (controller->activeChatCurrent().peer() == peer) {
Ui::showChatsList(&peer->session());
}
2019-03-21 13:48:40 +00:00
}
// Don't delete old history by default,
// because Android app doesn't.
//
//if (const auto from = peer->migrateFrom()) {
// peer->session().api().deleteConversation(from, false);
//}
peer->session().api().deleteConversation(peer, revoke);
}
return;
}
if (_moderateFrom) {
2017-06-04 11:09:29 +00:00
if (_banUser && _banUser->checked()) {
_moderateInChannel->session().api().kickParticipant(
_moderateInChannel,
_moderateFrom,
2019-01-03 14:39:19 +00:00
MTP_chatBannedRights(MTP_flags(0), MTP_int(0)));
}
if (_reportSpam->checked()) {
_moderateInChannel->session().api().request(
MTPchannels_ReportSpam(
_moderateInChannel->inputChannel,
_moderateFrom->inputUser,
MTP_vector<MTPint>(1, MTP_int(_ids[0].msg)))
).send();
}
2017-06-04 11:09:29 +00:00
if (_deleteAll && _deleteAll->checked()) {
_moderateInChannel->session().api().deleteAllFromUser(
_moderateInChannel,
_moderateFrom);
}
}
if (_deleteConfirmedCallback) {
_deleteConfirmedCallback();
}
auto remove = std::vector<not_null<HistoryItem*>>();
remove.reserve(_ids.size());
base::flat_map<not_null<History*>, QVector<MTPint>> idsByPeer;
2019-08-08 22:39:42 +00:00
base::flat_map<not_null<PeerData*>, QVector<MTPint>> scheduledIdsByPeer;
for (const auto itemId : _ids) {
2019-07-24 14:00:30 +00:00
if (const auto item = _session->data().message(itemId)) {
2019-01-15 11:57:45 +00:00
const auto history = item->history();
2019-08-08 22:39:42 +00:00
if (item->isScheduled()) {
2019-08-12 16:33:36 +00:00
const auto wasOnServer = !item->isSending()
&& !item->hasFailed();
if (wasOnServer) {
scheduledIdsByPeer[history->peer].push_back(MTP_int(
_session->data().scheduledMessages().lookupId(item)));
} else {
_session->data().scheduledMessages().removeSending(item);
}
2019-08-08 22:39:42 +00:00
continue;
}
remove.push_back(item);
if (IsServerMsgId(item->id)) {
idsByPeer[history].push_back(MTP_int(itemId.msg));
}
}
}
for (const auto &[history, ids] : idsByPeer) {
history->owner().histories().deleteMessages(history, ids, revoke);
}
2019-08-08 22:39:42 +00:00
for (const auto &[peer, ids] : scheduledIdsByPeer) {
peer->session().api().request(MTPmessages_DeleteScheduledMessages(
peer->input,
MTP_vector<MTPint>(ids)
)).done([peer=peer](const MTPUpdates &result) {
peer->session().api().applyUpdates(result);
2019-08-08 22:39:42 +00:00
}).send();
}
for (const auto item : remove) {
const auto history = item->history();
const auto wasLast = (history->lastMessage() == item);
const auto wasInChats = (history->chatListMessage() == item);
item->destroy();
if (wasLast || wasInChats) {
history->requestChatListMessage();
}
}
2019-07-24 14:00:30 +00:00
const auto session = _session;
Ui::hideLayer();
2019-07-24 14:00:30 +00:00
session->data().sendHistoryChangeNotifications();
}
2018-07-13 21:25:47 +00:00
ConfirmInviteBox::ConfirmInviteBox(
QWidget*,
2019-07-24 14:00:30 +00:00
not_null<Main::Session*> session,
const MTPDchatInvite &data,
Fn<void()> submit)
2019-07-24 14:00:30 +00:00
: _session(session)
, _submit(std::move(submit))
, _title(this, st::confirmInviteTitle)
, _status(this, st::confirmInviteStatus)
2019-07-24 14:00:30 +00:00
, _participants(GetParticipants(_session, data))
, _isChannel(data.is_channel() && !data.is_megagroup()) {
2019-07-05 13:38:38 +00:00
const auto title = qs(data.vtitle());
const auto count = data.vparticipants_count().v;
const auto status = [&] {
2019-06-19 15:09:03 +00:00
return (!_participants.empty() && _participants.size() < count)
? tr::lng_group_invite_members(tr::now, lt_count, count)
: (count > 0)
? tr::lng_chat_status_members(tr::now, lt_count_decimal, count)
: _isChannel
? tr::lng_channel_status(tr::now)
: tr::lng_group_status(tr::now);
}();
_title->setText(title);
_status->setText(status);
2019-07-24 14:00:30 +00:00
const auto photo = _session->data().processPhoto(data.vphoto());
if (!photo->isNull()) {
2020-05-25 14:16:04 +00:00
_photo = photo->createMediaView();
_photo->wanted(Data::PhotoSize::Small, Data::FileOrigin());
if (!_photo->image(Data::PhotoSize::Small)) {
_session->downloaderTaskFinished(
) | rpl::start_with_next([=] {
update();
}, lifetime());
}
} else {
_photoEmpty = std::make_unique<Ui::EmptyUserpic>(
Data::PeerUserpicColor(0),
title);
}
}
2020-05-28 14:32:10 +00:00
auto ConfirmInviteBox::GetParticipants(
not_null<Main::Session*> session,
const MTPDchatInvite &data)
-> std::vector<Participant> {
2019-07-05 13:38:38 +00:00
const auto participants = data.vparticipants();
if (!participants) {
return {};
}
2019-07-05 13:38:38 +00:00
const auto &v = participants->v;
2020-05-28 14:32:10 +00:00
auto result = std::vector<Participant>();
result.reserve(v.size());
for (const auto &participant : v) {
2019-07-24 14:00:30 +00:00
if (const auto user = session->data().processUser(participant)) {
2020-05-28 14:32:10 +00:00
result.push_back(Participant{ user });
}
}
return result;
}
void ConfirmInviteBox::prepare() {
addButton(
(_isChannel
? tr::lng_profile_join_channel()
: tr::lng_profile_join_group()),
_submit);
addButton(tr::lng_cancel(), [=] { closeBox(); });
while (_participants.size() > 4) {
_participants.pop_back();
}
auto newHeight = st::confirmInviteStatusTop + _status->height() + st::boxPadding.bottom();
if (!_participants.empty()) {
int skip = (st::boxWideWidth - 4 * st::confirmInviteUserPhotoSize) / 5;
int padding = skip / 2;
_userWidth = (st::confirmInviteUserPhotoSize + 2 * padding);
int sumWidth = _participants.size() * _userWidth;
int left = (st::boxWideWidth - sumWidth) / 2;
2020-05-28 14:32:10 +00:00
for (const auto &participant : _participants) {
auto name = new Ui::FlatLabel(this, st::confirmInviteUserName);
name->resizeToWidth(st::confirmInviteUserPhotoSize + padding);
2020-05-28 14:32:10 +00:00
name->setText(participant.user->firstName.isEmpty()
? participant.user->name
: participant.user->firstName);
name->moveToLeft(left + (padding / 2), st::confirmInviteUserNameTop);
left += _userWidth;
}
newHeight += st::confirmInviteUserHeight;
}
setDimensions(st::boxWideWidth, newHeight);
}
void ConfirmInviteBox::resizeEvent(QResizeEvent *e) {
BoxContent::resizeEvent(e);
_title->move((width() - _title->width()) / 2, st::confirmInviteTitleTop);
_status->move((width() - _status->width()) / 2, st::confirmInviteStatusTop);
}
void ConfirmInviteBox::paintEvent(QPaintEvent *e) {
BoxContent::paintEvent(e);
Painter p(this);
if (_photo) {
2020-05-25 14:16:04 +00:00
if (const auto image = _photo->image(Data::PhotoSize::Small)) {
p.drawPixmap(
(width() - st::confirmInvitePhotoSize) / 2,
st::confirmInvitePhotoTop,
image->pixCircled(
st::confirmInvitePhotoSize,
st::confirmInvitePhotoSize));
}
} else if (_photoEmpty) {
2018-07-13 21:25:47 +00:00
_photoEmpty->paint(
p,
(width() - st::confirmInvitePhotoSize) / 2,
st::confirmInvitePhotoTop,
width(),
st::confirmInvitePhotoSize);
}
int sumWidth = _participants.size() * _userWidth;
int left = (width() - sumWidth) / 2;
2020-05-28 14:32:10 +00:00
for (auto &participant : _participants) {
participant.user->paintUserpicLeft(
2018-07-13 21:25:47 +00:00
p,
2020-05-28 14:32:10 +00:00
participant.userpic,
2018-07-13 21:25:47 +00:00
left + (_userWidth - st::confirmInviteUserPhotoSize) / 2,
st::confirmInviteUserPhotoTop,
width(),
st::confirmInviteUserPhotoSize);
left += _userWidth;
}
}
ConfirmInviteBox::~ConfirmInviteBox() = default;
2018-12-05 08:07:17 +00:00
ConfirmDontWarnBox::ConfirmDontWarnBox(
QWidget*,
rpl::producer<TextWithEntities> text,
2018-12-05 08:07:17 +00:00
const QString &checkbox,
rpl::producer<QString> confirm,
2018-12-05 08:07:17 +00:00
FnMut<void(bool)> callback)
: _confirm(std::move(confirm))
, _content(setupContent(std::move(text), checkbox, std::move(callback))) {
2018-12-05 08:07:17 +00:00
}
void ConfirmDontWarnBox::prepare() {
setDimensionsToContent(st::boxWidth, _content);
addButton(std::move(_confirm), [=] { _callback(); });
addButton(tr::lng_cancel(), [=] { closeBox(); });
2018-12-05 08:07:17 +00:00
}
not_null<Ui::RpWidget*> ConfirmDontWarnBox::setupContent(
rpl::producer<TextWithEntities> text,
2018-12-05 08:07:17 +00:00
const QString &checkbox,
FnMut<void(bool)> callback) {
const auto result = Ui::CreateChild<Ui::VerticalLayout>(this);
result->add(
object_ptr<Ui::FlatLabel>(
result,
std::move(text),
2018-12-05 08:07:17 +00:00
st::boxLabel),
st::boxPadding);
const auto control = result->add(
object_ptr<Ui::Checkbox>(
result,
checkbox,
false,
st::defaultBoxCheckbox),
style::margins(
st::boxPadding.left(),
st::boxPadding.bottom(),
st::boxPadding.right(),
st::boxPadding.bottom()));
_callback = [=, callback = std::move(callback)]() mutable {
const auto checked = control->checked();
auto local = std::move(callback);
closeBox();
local(checked);
};
return result;
}