2016-05-31 19:27:11 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
|
|
|
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
|
2016-05-31 19:27:11 +00:00
|
|
|
*/
|
|
|
|
#include "stdafx.h"
|
2016-10-23 21:03:10 +00:00
|
|
|
#include "profile/profile_block_invite_link.h"
|
2016-05-31 19:27:11 +00:00
|
|
|
|
|
|
|
#include "styles/style_profile.h"
|
2016-11-16 10:44:06 +00:00
|
|
|
#include "ui/widgets/labels.h"
|
2016-12-07 13:32:25 +00:00
|
|
|
#include "ui/toast/toast.h"
|
2016-06-01 16:40:51 +00:00
|
|
|
#include "boxes/confirmbox.h"
|
|
|
|
#include "observer_peer.h"
|
2016-12-07 13:32:25 +00:00
|
|
|
#include "mainwindow.h"
|
2016-05-31 19:27:11 +00:00
|
|
|
#include "lang.h"
|
|
|
|
|
|
|
|
namespace Profile {
|
|
|
|
|
2016-06-14 16:26:41 +00:00
|
|
|
using UpdateFlag = Notify::PeerUpdate::Flag;
|
|
|
|
|
2016-06-01 16:40:51 +00:00
|
|
|
InviteLinkWidget::InviteLinkWidget(QWidget *parent, PeerData *peer) : BlockWidget(parent, peer, lang(lng_profile_invite_link_section)) {
|
2016-06-14 16:26:41 +00:00
|
|
|
auto observeEvents = UpdateFlag::InviteLinkChanged | UpdateFlag::UsernameChanged;
|
2016-09-26 18:33:34 +00:00
|
|
|
subscribe(Notify::PeerUpdated(), Notify::PeerUpdatedHandler(observeEvents, [this](const Notify::PeerUpdate &update) {
|
2016-09-26 13:57:08 +00:00
|
|
|
notifyPeerUpdated(update);
|
2016-09-26 18:33:34 +00:00
|
|
|
}));
|
2016-06-01 16:40:51 +00:00
|
|
|
|
|
|
|
refreshLink();
|
|
|
|
refreshVisibility();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InviteLinkWidget::notifyPeerUpdated(const Notify::PeerUpdate &update) {
|
|
|
|
if (update.peer != peer()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-14 16:26:41 +00:00
|
|
|
if (update.flags & (UpdateFlag::InviteLinkChanged | UpdateFlag::UsernameChanged)) {
|
2016-06-01 16:40:51 +00:00
|
|
|
refreshLink();
|
|
|
|
refreshVisibility();
|
|
|
|
|
|
|
|
contentSizeUpdated();
|
|
|
|
}
|
2016-05-31 19:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int InviteLinkWidget::resizeGetHeight(int newWidth) {
|
|
|
|
int newHeight = contentTop();
|
|
|
|
|
2016-06-01 16:40:51 +00:00
|
|
|
int marginLeft = st::profileBlockTextPart.margin.left();
|
|
|
|
int marginRight = st::profileBlockTextPart.margin.right();
|
|
|
|
int left = st::profileBlockTitlePosition.x();
|
|
|
|
if (_link) {
|
|
|
|
int textWidth = _link->naturalWidth();
|
|
|
|
int availableWidth = newWidth - left - st::profileBlockMarginRight;
|
|
|
|
int maxWidth = st::msgMaxWidth;
|
|
|
|
accumulate_min(textWidth, availableWidth);
|
|
|
|
accumulate_min(textWidth, st::msgMaxWidth);
|
|
|
|
_link->resizeToWidth(textWidth + marginLeft + marginRight);
|
|
|
|
_link->moveToLeft(left - marginLeft, newHeight - st::profileBlockTextPart.margin.top());
|
|
|
|
newHeight += _link->height();
|
|
|
|
}
|
|
|
|
|
|
|
|
newHeight += st::profileBlockMarginBottom;
|
2016-05-31 19:27:11 +00:00
|
|
|
return newHeight;
|
|
|
|
}
|
|
|
|
|
2016-06-01 16:40:51 +00:00
|
|
|
void InviteLinkWidget::refreshVisibility() {
|
|
|
|
setVisible(_link != nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString InviteLinkWidget::getInviteLink() const {
|
|
|
|
if (auto chat = peer()->asChat()) {
|
|
|
|
return chat->inviteLink();
|
|
|
|
} else if (auto channel = peer()->asChannel()) {
|
2016-06-14 16:26:41 +00:00
|
|
|
return channel->isPublic() ? QString() : channel->inviteLink();
|
2016-06-01 16:40:51 +00:00
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
};
|
|
|
|
|
|
|
|
void InviteLinkWidget::refreshLink() {
|
|
|
|
_link.destroy();
|
|
|
|
TextWithEntities linkData = { getInviteLink(), EntitiesInText() };
|
2016-06-14 16:26:41 +00:00
|
|
|
if (linkData.text.isEmpty()) {
|
|
|
|
_link.destroy();
|
|
|
|
} else {
|
2016-11-16 10:44:06 +00:00
|
|
|
_link.create(this, QString(), Ui::FlatLabel::InitType::Simple, st::profileInviteLinkText);
|
2016-06-01 16:40:51 +00:00
|
|
|
_link->show();
|
|
|
|
|
|
|
|
linkData.entities.push_back(EntityInText(EntityInTextUrl, 0, linkData.text.size()));
|
|
|
|
_link->setMarkedText(linkData);
|
|
|
|
_link->setSelectable(true);
|
|
|
|
_link->setContextCopyText(QString());
|
2016-09-26 12:09:59 +00:00
|
|
|
_link->setClickHandlerHook([this](const ClickHandlerPtr &handler, Qt::MouseButton button) {
|
|
|
|
auto link = getInviteLink();
|
|
|
|
if (link.isEmpty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QApplication::clipboard()->setText(link);
|
2017-02-03 20:07:26 +00:00
|
|
|
Ui::Toast::Show(lang(lng_group_invite_copied));
|
2016-09-26 12:09:59 +00:00
|
|
|
return false;
|
|
|
|
});
|
2016-06-01 16:40:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-31 19:27:11 +00:00
|
|
|
} // namespace Profile
|