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
|
|
|
*/
|
2016-10-23 21:03:10 +00:00
|
|
|
#include "profile/profile_block_info.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-10-23 21:03:10 +00:00
|
|
|
#include "ui/widgets/buttons.h"
|
|
|
|
#include "ui/effects/widget_slide_wrap.h"
|
2016-05-31 19:27:11 +00:00
|
|
|
#include "core/click_handler_types.h"
|
2016-12-08 14:08:54 +00:00
|
|
|
#include "mainwidget.h"
|
2016-05-31 19:27:11 +00:00
|
|
|
#include "observer_peer.h"
|
2016-10-23 21:03:10 +00:00
|
|
|
#include "apiwrap.h"
|
2016-05-31 19:27:11 +00:00
|
|
|
#include "lang.h"
|
|
|
|
|
|
|
|
namespace Profile {
|
|
|
|
|
2016-10-23 21:03:10 +00:00
|
|
|
constexpr int kCommonGroupsLimit = 20;
|
|
|
|
|
2016-06-01 20:05:37 +00:00
|
|
|
using UpdateFlag = Notify::PeerUpdate::Flag;
|
|
|
|
|
2016-05-31 19:27:11 +00:00
|
|
|
InfoWidget::InfoWidget(QWidget *parent, PeerData *peer) : BlockWidget(parent, peer, lang(lng_profile_info_section)) {
|
2016-06-01 20:05:37 +00:00
|
|
|
auto observeEvents = UpdateFlag::AboutChanged
|
|
|
|
| UpdateFlag::UsernameChanged
|
|
|
|
| UpdateFlag::UserPhoneChanged
|
2016-12-13 17:07:56 +00:00
|
|
|
| UpdateFlag::UserCanShareContact;
|
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-05-31 19:27:11 +00:00
|
|
|
|
|
|
|
refreshLabels();
|
2016-12-05 11:01:08 +00:00
|
|
|
}
|
|
|
|
|
2016-05-31 19:27:11 +00:00
|
|
|
void InfoWidget::notifyPeerUpdated(const Notify::PeerUpdate &update) {
|
|
|
|
if (update.peer != peer()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-01 20:05:37 +00:00
|
|
|
if (update.flags & UpdateFlag::AboutChanged) {
|
2016-05-31 19:27:11 +00:00
|
|
|
refreshAbout();
|
|
|
|
}
|
2016-06-01 20:05:37 +00:00
|
|
|
if (update.flags & UpdateFlag::UsernameChanged) {
|
2016-05-31 19:27:11 +00:00
|
|
|
refreshUsername();
|
|
|
|
refreshChannelLink();
|
|
|
|
}
|
2016-06-01 20:05:37 +00:00
|
|
|
if (update.flags & (UpdateFlag::UserPhoneChanged | UpdateFlag::UserCanShareContact)) {
|
2016-05-31 19:27:11 +00:00
|
|
|
refreshMobileNumber();
|
|
|
|
}
|
|
|
|
refreshVisibility();
|
|
|
|
|
|
|
|
contentSizeUpdated();
|
|
|
|
}
|
|
|
|
|
|
|
|
int InfoWidget::resizeGetHeight(int newWidth) {
|
2016-10-23 21:03:10 +00:00
|
|
|
int initialHeight = contentTop();
|
|
|
|
int newHeight = initialHeight;
|
2016-05-31 19:27:11 +00:00
|
|
|
|
|
|
|
int marginLeft = st::profileBlockTextPart.margin.left();
|
|
|
|
int marginRight = st::profileBlockTextPart.margin.right();
|
|
|
|
int left = st::profileBlockTitlePosition.x();
|
|
|
|
if (_about) {
|
|
|
|
int textWidth = _about->naturalWidth();
|
|
|
|
int availableWidth = newWidth - left - st::profileBlockMarginRight;
|
|
|
|
int maxWidth = st::msgMaxWidth;
|
|
|
|
accumulate_min(textWidth, availableWidth);
|
|
|
|
accumulate_min(textWidth, st::msgMaxWidth);
|
|
|
|
_about->resizeToWidth(textWidth + marginLeft + marginRight);
|
|
|
|
_about->moveToLeft(left - marginLeft, newHeight - st::profileBlockTextPart.margin.top());
|
|
|
|
newHeight += _about->height();
|
|
|
|
}
|
|
|
|
|
2016-11-16 10:44:06 +00:00
|
|
|
auto moveLabeledText = [&newHeight, left, newWidth, marginLeft, marginRight](Ui::FlatLabel *label, Ui::FlatLabel *text, Ui::FlatLabel *shortText) {
|
2016-05-31 19:27:11 +00:00
|
|
|
if (!label) return;
|
|
|
|
|
|
|
|
label->moveToLeft(left, newHeight);
|
|
|
|
int textLeft = left + label->width() + st::normalFont->spacew;
|
|
|
|
int textWidth = text->naturalWidth();
|
|
|
|
int availableWidth = newWidth - textLeft - st::profileBlockMarginRight;
|
|
|
|
bool doesNotFit = (textWidth > availableWidth);
|
|
|
|
accumulate_min(textWidth, availableWidth);
|
|
|
|
accumulate_min(textWidth, st::msgMaxWidth);
|
|
|
|
text->resizeToWidth(textWidth + marginLeft + marginRight);
|
|
|
|
text->moveToLeft(textLeft - marginLeft, newHeight - st::profileBlockOneLineTextPart.margin.top());
|
|
|
|
if (shortText) {
|
|
|
|
shortText->resizeToWidth(textWidth + marginLeft + marginRight);
|
|
|
|
shortText->moveToLeft(textLeft - marginLeft, newHeight - st::profileBlockOneLineTextPart.margin.top());
|
|
|
|
if (doesNotFit) {
|
|
|
|
shortText->show();
|
|
|
|
text->hide();
|
|
|
|
} else {
|
|
|
|
shortText->hide();
|
|
|
|
text->show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newHeight += label->height() + st::profileBlockOneLineSkip;
|
|
|
|
};
|
|
|
|
moveLabeledText(_channelLinkLabel, _channelLink, _channelLinkShort);
|
|
|
|
moveLabeledText(_mobileNumberLabel, _mobileNumber, nullptr);
|
|
|
|
moveLabeledText(_usernameLabel, _username, nullptr);
|
|
|
|
|
|
|
|
newHeight += st::profileBlockMarginBottom;
|
2016-12-13 17:07:56 +00:00
|
|
|
return newHeight;
|
2016-05-31 19:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-02-11 11:24:37 +00:00
|
|
|
void InfoWidget::leaveEventHook(QEvent *e) {
|
2016-05-31 19:27:11 +00:00
|
|
|
BotCommandClickHandler::setPeerForCommand(nullptr);
|
|
|
|
BotCommandClickHandler::setBotForCommand(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InfoWidget::refreshLabels() {
|
|
|
|
refreshAbout();
|
|
|
|
refreshMobileNumber();
|
|
|
|
refreshUsername();
|
|
|
|
refreshChannelLink();
|
|
|
|
|
|
|
|
refreshVisibility();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InfoWidget::refreshVisibility() {
|
2016-12-13 17:07:56 +00:00
|
|
|
setVisible(_about || _mobileNumber || _username || _channelLink);
|
2016-05-31 19:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InfoWidget::refreshAbout() {
|
|
|
|
auto getAboutText = [this]() -> QString {
|
|
|
|
if (auto user = peer()->asUser()) {
|
|
|
|
return user->about();
|
|
|
|
} else if (auto channel = peer()->asChannel()) {
|
|
|
|
return channel->about();
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
};
|
|
|
|
|
|
|
|
_about.destroy();
|
2016-07-28 17:01:08 +00:00
|
|
|
auto aboutText = textClean(getAboutText());
|
2016-05-31 19:27:11 +00:00
|
|
|
if (!aboutText.isEmpty()) {
|
2016-11-16 10:44:06 +00:00
|
|
|
_about.create(this, st::profileBlockTextPart);
|
2016-05-31 19:27:11 +00:00
|
|
|
_about->show();
|
|
|
|
|
|
|
|
EntitiesInText aboutEntities;
|
|
|
|
textParseEntities(aboutText, TextParseLinks | TextParseMentions | TextParseHashtags | TextParseBotCommands, &aboutEntities);
|
|
|
|
_about->setMarkedText({ aboutText, aboutEntities });
|
|
|
|
_about->setSelectable(true);
|
2016-09-26 12:09:59 +00:00
|
|
|
_about->setClickHandlerHook([this](const ClickHandlerPtr &handler, Qt::MouseButton button) {
|
|
|
|
BotCommandClickHandler::setPeerForCommand(peer());
|
|
|
|
return true;
|
|
|
|
});
|
2016-05-31 19:27:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InfoWidget::refreshMobileNumber() {
|
|
|
|
TextWithEntities phoneText;
|
|
|
|
if (auto user = peer()->asUser()) {
|
|
|
|
if (!user->phone().isEmpty()) {
|
|
|
|
phoneText.text = App::formatPhone(user->phone());
|
|
|
|
} else {
|
|
|
|
phoneText.text = App::phoneFromSharedContact(peerToUser(user->id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setLabeledText(&_mobileNumberLabel, lang(lng_profile_mobile_number), &_mobileNumber, phoneText, lang(lng_profile_copy_phone));
|
|
|
|
}
|
|
|
|
|
|
|
|
void InfoWidget::refreshUsername() {
|
|
|
|
TextWithEntities usernameText;
|
|
|
|
if (auto user = peer()->asUser()) {
|
|
|
|
if (!user->username.isEmpty()) {
|
|
|
|
usernameText.text = '@' + user->username;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setLabeledText(&_usernameLabel, lang(lng_profile_username), &_username, usernameText, lang(lng_context_copy_mention));
|
|
|
|
}
|
|
|
|
|
|
|
|
void InfoWidget::refreshChannelLink() {
|
|
|
|
TextWithEntities channelLinkText;
|
|
|
|
TextWithEntities channelLinkTextShort;
|
|
|
|
if (auto channel = peer()->asChannel()) {
|
|
|
|
if (!channel->username.isEmpty()) {
|
2017-01-07 11:55:05 +00:00
|
|
|
channelLinkText.text = CreateInternalLinkHttps(channel->username);
|
2016-05-31 19:27:11 +00:00
|
|
|
channelLinkText.entities.push_back(EntityInText(EntityInTextUrl, 0, channelLinkText.text.size()));
|
2017-01-07 11:55:05 +00:00
|
|
|
channelLinkTextShort.text = CreateInternalLink(channel->username);
|
|
|
|
channelLinkTextShort.entities.push_back(EntityInText(EntityInTextCustomUrl, 0, channelLinkTextShort.text.size(), CreateInternalLinkHttps(channel->username)));
|
2016-05-31 19:27:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
setLabeledText(nullptr, lang(lng_profile_link), &_channelLink, channelLinkText, QString());
|
|
|
|
setLabeledText(&_channelLinkLabel, lang(lng_profile_link), &_channelLinkShort, channelLinkTextShort, QString());
|
|
|
|
if (_channelLinkShort) {
|
|
|
|
_channelLinkShort->setExpandLinksMode(ExpandLinksUrlOnly);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
void InfoWidget::setLabeledText(object_ptr<Ui::FlatLabel> *labelWidget, const QString &label,
|
|
|
|
object_ptr<Ui::FlatLabel> *textWidget, const TextWithEntities &textWithEntities, const QString ©Text) {
|
2016-05-31 19:27:11 +00:00
|
|
|
if (labelWidget) labelWidget->destroy();
|
|
|
|
textWidget->destroy();
|
|
|
|
if (textWithEntities.text.isEmpty()) return;
|
|
|
|
|
|
|
|
if (labelWidget) {
|
2016-11-16 10:44:06 +00:00
|
|
|
labelWidget->create(this, label, Ui::FlatLabel::InitType::Simple, st::profileBlockLabel);
|
2016-05-31 19:27:11 +00:00
|
|
|
(*labelWidget)->show();
|
|
|
|
}
|
2016-11-16 10:44:06 +00:00
|
|
|
textWidget->create(this, QString(), Ui::FlatLabel::InitType::Simple, st::profileBlockOneLineTextPart);
|
2016-05-31 19:27:11 +00:00
|
|
|
(*textWidget)->show();
|
|
|
|
(*textWidget)->setMarkedText(textWithEntities);
|
|
|
|
(*textWidget)->setContextCopyText(copyText);
|
|
|
|
(*textWidget)->setSelectable(true);
|
|
|
|
(*textWidget)->setDoubleClickSelectsParagraph(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Profile
|