From cabf35f2b3c45cfa90c7624b35a50458e564dee0 Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 14 Jul 2017 15:28:08 +0300 Subject: [PATCH] Allow to create channel invite link in boxes. SetupChannelBox (public/private) and MaxInviteBox are suggesting to copy the channel invite link. Now they suggest to create it in case the channel didn't have the invite link already. --- .../SourceFiles/boxes/add_contact_box.cpp | 16 ++++++++++--- Telegram/SourceFiles/boxes/confirm_box.cpp | 24 ++++++++++++++----- Telegram/SourceFiles/boxes/confirm_box.h | 5 ++-- Telegram/SourceFiles/boxes/contacts_box.cpp | 2 +- .../profile/profile_channel_controllers.cpp | 2 +- 5 files changed, 36 insertions(+), 13 deletions(-) diff --git a/Telegram/SourceFiles/boxes/add_contact_box.cpp b/Telegram/SourceFiles/boxes/add_contact_box.cpp index 2cb7d5e16..441184ebb 100644 --- a/Telegram/SourceFiles/boxes/add_contact_box.cpp +++ b/Telegram/SourceFiles/boxes/add_contact_box.cpp @@ -482,6 +482,11 @@ void SetupChannelBox::prepare() { connect(&_checkTimer, SIGNAL(timeout()), this, SLOT(onCheck())); _privacyGroup->setChangedCallback([this](Privacy value) { privacyChanged(value); }); + subscribe(Notify::PeerUpdated(), Notify::PeerUpdatedHandler(Notify::PeerUpdate::Flag::InviteLinkChanged, [this](const Notify::PeerUpdate &update) { + if (update.peer == _channel) { + rtlupdate(_invitationLink); + } + })); updateMaxHeight(); } @@ -541,7 +546,8 @@ void SetupChannelBox::paintEvent(QPaintEvent *e) { option.setWrapMode(QTextOption::WrapAnywhere); p.setFont(_linkOver ? st::boxTextFont->underline() : st::boxTextFont); p.setPen(st::defaultLinkButton.color); - p.drawText(_invitationLink, _channel->inviteLink(), option); + auto inviteLinkText = _channel->inviteLink().isEmpty() ? lang(lng_group_invite_create) : _channel->inviteLink(); + p.drawText(_invitationLink, inviteLinkText, option); } } else { if (!_errorText.isEmpty()) { @@ -573,8 +579,12 @@ void SetupChannelBox::mouseMoveEvent(QMouseEvent *e) { void SetupChannelBox::mousePressEvent(QMouseEvent *e) { if (_linkOver) { - QGuiApplication::clipboard()->setText(_channel->inviteLink()); - Ui::Toast::Show(lang(lng_create_channel_link_copied)); + if (_channel->inviteLink().isEmpty()) { + App::api()->exportInviteLink(_channel); + } else { + QGuiApplication::clipboard()->setText(_channel->inviteLink()); + Ui::Toast::Show(lang(lng_create_channel_link_copied)); + } } } diff --git a/Telegram/SourceFiles/boxes/confirm_box.cpp b/Telegram/SourceFiles/boxes/confirm_box.cpp index f0c14e80a..4dde7a799 100644 --- a/Telegram/SourceFiles/boxes/confirm_box.cpp +++ b/Telegram/SourceFiles/boxes/confirm_box.cpp @@ -33,6 +33,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #include "core/click_handler_types.h" #include "storage/localstorage.h" #include "auth_session.h" +#include "observer_peer.h" TextParseOptions _confirmBoxTextOptions = { TextParseLinks | TextParseMultiline | TextParseRichText, // flags @@ -216,9 +217,9 @@ InformBox::InformBox(QWidget*, const QString &text, base::lambda closedC InformBox::InformBox(QWidget*, const QString &text, const QString &doneText, base::lambda closedCallback) : ConfirmBox(ConfirmBox::InformBoxTag(), text, doneText, std::move(closedCallback)) { } -MaxInviteBox::MaxInviteBox(QWidget*, const QString &link) -: _text(st::boxLabelStyle, lng_participant_invite_sorry(lt_count, Global::ChatSizeMax()), _confirmBoxTextOptions, st::boxWidth - st::boxPadding.left() - st::boxButtonPadding.right()) -, _link(link) { +MaxInviteBox::MaxInviteBox(QWidget*, gsl::not_null channel) : BoxContent() +, _channel(channel) +, _text(st::boxLabelStyle, lng_participant_invite_sorry(lt_count, Global::ChatSizeMax()), _confirmBoxTextOptions, st::boxWidth - st::boxPadding.left() - st::boxButtonPadding.right()) { } void MaxInviteBox::prepare() { @@ -229,6 +230,12 @@ void MaxInviteBox::prepare() { _textWidth = st::boxWidth - st::boxPadding.left() - st::boxButtonPadding.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()); + + subscribe(Notify::PeerUpdated(), Notify::PeerUpdatedHandler(Notify::PeerUpdate::Flag::InviteLinkChanged, [this](const Notify::PeerUpdate &update) { + if (update.peer == _channel) { + rtlupdate(_invitationLink); + } + })); } void MaxInviteBox::mouseMoveEvent(QMouseEvent *e) { @@ -238,8 +245,12 @@ void MaxInviteBox::mouseMoveEvent(QMouseEvent *e) { void MaxInviteBox::mousePressEvent(QMouseEvent *e) { mouseMoveEvent(e); if (_linkOver) { - Application::clipboard()->setText(_link); - Ui::Toast::Show(lang(lng_create_channel_link_copied)); + if (_channel->inviteLink().isEmpty()) { + App::api()->exportInviteLink(_channel); + } else { + QGuiApplication::clipboard()->setText(_channel->inviteLink()); + Ui::Toast::Show(lang(lng_create_channel_link_copied)); + } } } @@ -271,7 +282,8 @@ void MaxInviteBox::paintEvent(QPaintEvent *e) { option.setWrapMode(QTextOption::WrapAnywhere); p.setFont(_linkOver ? st::defaultInputField.font->underline() : st::defaultInputField.font); p.setPen(st::defaultLinkButton.color); - p.drawText(_invitationLink, _link, option); + auto inviteLinkText = _channel->inviteLink().isEmpty() ? lang(lng_group_invite_create) : _channel->inviteLink(); + p.drawText(_invitationLink, inviteLinkText, option); } void MaxInviteBox::resizeEvent(QResizeEvent *e) { diff --git a/Telegram/SourceFiles/boxes/confirm_box.h b/Telegram/SourceFiles/boxes/confirm_box.h index 40f9784bc..4c331ba15 100644 --- a/Telegram/SourceFiles/boxes/confirm_box.h +++ b/Telegram/SourceFiles/boxes/confirm_box.h @@ -100,7 +100,7 @@ public: class MaxInviteBox : public BoxContent { public: - MaxInviteBox(QWidget*, const QString &link); + MaxInviteBox(QWidget*, gsl::not_null channel); protected: void prepare() override; @@ -114,10 +114,11 @@ protected: private: void updateSelected(const QPoint &cursorGlobalPosition); + gsl::not_null _channel; + Text _text; int32 _textWidth, _textHeight; - QString _link; QRect _invitationLink; bool _linkOver = false; diff --git a/Telegram/SourceFiles/boxes/contacts_box.cpp b/Telegram/SourceFiles/boxes/contacts_box.cpp index 694e9583e..f1d9ae7b2 100644 --- a/Telegram/SourceFiles/boxes/contacts_box.cpp +++ b/Telegram/SourceFiles/boxes/contacts_box.cpp @@ -1468,7 +1468,7 @@ void ContactsBox::Inner::changeCheckState(ContactData *data, PeerData *peer) { } else if (selectedCount() < ((_channel && _channel->isMegagroup()) ? Global::MegagroupSizeMax() : Global::ChatSizeMax())) { changePeerCheckState(data, peer, true); } else if (_channel && !_channel->isMegagroup()) { - Ui::show(Box(_channel->inviteLink()), KeepOtherLayers); + Ui::show(Box(_channel), KeepOtherLayers); } else if (!_channel && selectedCount() >= Global::ChatSizeMax() && selectedCount() < Global::MegagroupSizeMax()) { Ui::show(Box(lng_profile_add_more_after_upgrade(lt_count, Global::MegagroupSizeMax())), KeepOtherLayers); } diff --git a/Telegram/SourceFiles/profile/profile_channel_controllers.cpp b/Telegram/SourceFiles/profile/profile_channel_controllers.cpp index 1b537dcc8..873c7d816 100644 --- a/Telegram/SourceFiles/profile/profile_channel_controllers.cpp +++ b/Telegram/SourceFiles/profile/profile_channel_controllers.cpp @@ -77,7 +77,7 @@ void ParticipantsBoxController::Start(gsl::not_null channel, Role void ParticipantsBoxController::addNewItem() { if (_role == Role::Members) { if (_channel->membersCount() >= Global::ChatSizeMax()) { - Ui::show(Box(_channel->inviteLink()), KeepOtherLayers); + Ui::show(Box(_channel), KeepOtherLayers); } else { auto already = MembersAlreadyIn(); for (auto i = 0, count = delegate()->peerListFullRowsCount(); i != count; ++i) {