2016-08-19 17:26:31 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-08-19 17:26:31 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-08-19 17:26:31 +00:00
|
|
|
*/
|
|
|
|
#include "settings/settings_info_widget.h"
|
|
|
|
|
|
|
|
#include "styles/style_settings.h"
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2016-11-16 10:44:06 +00:00
|
|
|
#include "ui/widgets/labels.h"
|
2017-09-13 16:57:44 +00:00
|
|
|
#include "ui/wrap/slide_wrap.h"
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "boxes/username_box.h"
|
2017-07-18 15:15:02 +00:00
|
|
|
#include "boxes/add_contact_box.h"
|
2017-03-21 11:42:04 +00:00
|
|
|
#include "boxes/change_phone_box.h"
|
2018-01-17 16:21:01 +00:00
|
|
|
#include "data/data_session.h"
|
2016-08-22 17:16:21 +00:00
|
|
|
#include "observer_peer.h"
|
2017-03-10 17:25:43 +00:00
|
|
|
#include "messenger.h"
|
2018-01-17 16:21:01 +00:00
|
|
|
#include "auth_session.h"
|
2016-08-19 17:26:31 +00:00
|
|
|
|
|
|
|
namespace Settings {
|
|
|
|
|
2016-08-22 17:16:21 +00:00
|
|
|
using UpdateFlag = Notify::PeerUpdate::Flag;
|
|
|
|
|
2016-08-19 17:26:31 +00:00
|
|
|
InfoWidget::InfoWidget(QWidget *parent, UserData *self) : BlockWidget(parent, self, lang(lng_settings_section_info)) {
|
2017-07-18 15:15:02 +00:00
|
|
|
auto observeEvents = UpdateFlag::UsernameChanged | UpdateFlag::UserPhoneChanged | UpdateFlag::AboutChanged;
|
2016-09-26 14:50:24 +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 14:50:24 +00:00
|
|
|
}));
|
2016-08-22 17:16:21 +00:00
|
|
|
|
|
|
|
createControls();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InfoWidget::createControls() {
|
2017-07-19 19:20:38 +00:00
|
|
|
style::margins margin(0, 0, 0, 0);
|
|
|
|
style::margins slidedPadding(0, 0, 0, 0);
|
2017-09-13 16:57:44 +00:00
|
|
|
createChildRow(_mobileNumber, margin, slidedPadding, st::settingsBlockOneLineTextPart);
|
|
|
|
createChildRow(_username, margin, slidedPadding, st::settingsBlockOneLineTextPart);
|
|
|
|
createChildRow(_bio, margin, slidedPadding, st::settingsBioValue);
|
2016-08-19 17:26:31 +00:00
|
|
|
refreshControls();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InfoWidget::refreshControls() {
|
2016-08-22 17:16:21 +00:00
|
|
|
refreshMobileNumber();
|
|
|
|
refreshUsername();
|
2017-07-18 15:15:02 +00:00
|
|
|
refreshBio();
|
2016-08-22 17:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InfoWidget::refreshMobileNumber() {
|
|
|
|
TextWithEntities phoneText;
|
2018-01-17 16:21:01 +00:00
|
|
|
if (const auto user = self()->asUser()) {
|
|
|
|
phoneText.text = Auth().data().findContactPhone(user);
|
2016-08-22 17:16:21 +00:00
|
|
|
}
|
2017-09-13 16:57:44 +00:00
|
|
|
setLabeledText(
|
|
|
|
_mobileNumber,
|
|
|
|
lang(lng_profile_mobile_number),
|
|
|
|
phoneText,
|
|
|
|
TextWithEntities(),
|
|
|
|
lang(lng_profile_copy_phone));
|
2017-03-21 11:42:04 +00:00
|
|
|
if (auto text = _mobileNumber->entity()->textLabel()) {
|
|
|
|
text->setRichText(textcmdLink(1, phoneText.text));
|
2017-12-18 09:07:18 +00:00
|
|
|
text->setLink(1, std::make_shared<LambdaClickHandler>([] {
|
2017-03-21 11:42:04 +00:00
|
|
|
Ui::show(Box<ChangePhoneBox>());
|
|
|
|
}));
|
|
|
|
}
|
2016-08-22 17:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InfoWidget::refreshUsername() {
|
|
|
|
TextWithEntities usernameText;
|
|
|
|
QString copyText;
|
|
|
|
if (self()->username.isEmpty()) {
|
|
|
|
usernameText.text = lang(lng_settings_choose_username);
|
|
|
|
} else {
|
|
|
|
usernameText.text = '@' + self()->username;
|
|
|
|
copyText = lang(lng_context_copy_mention);
|
|
|
|
}
|
2017-11-05 18:09:05 +00:00
|
|
|
usernameText.entities.push_back(EntityInText(
|
|
|
|
EntityInTextCustomUrl,
|
|
|
|
0,
|
|
|
|
usernameText.text.size(),
|
|
|
|
Messenger::Instance().createInternalLinkFull(
|
|
|
|
self()->username)));
|
2017-09-13 16:57:44 +00:00
|
|
|
setLabeledText(
|
|
|
|
_username,
|
|
|
|
lang(lng_profile_username),
|
|
|
|
usernameText,
|
|
|
|
TextWithEntities(),
|
|
|
|
copyText);
|
2016-08-22 17:16:21 +00:00
|
|
|
if (auto text = _username->entity()->textLabel()) {
|
2016-09-26 12:09:59 +00:00
|
|
|
text->setClickHandlerHook([](const ClickHandlerPtr &handler, Qt::MouseButton button) {
|
2016-12-13 17:07:56 +00:00
|
|
|
Ui::show(Box<UsernameBox>());
|
2016-09-26 12:09:59 +00:00
|
|
|
return false;
|
|
|
|
});
|
2016-08-22 17:16:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-18 15:15:02 +00:00
|
|
|
void InfoWidget::refreshBio() {
|
|
|
|
TextWithEntities bioText;
|
|
|
|
auto aboutText = self()->about();
|
|
|
|
if (self()->about().isEmpty()) {
|
|
|
|
bioText.text = lang(lng_settings_empty_bio);
|
|
|
|
} else {
|
|
|
|
bioText.text = aboutText;
|
|
|
|
}
|
2017-11-05 18:09:05 +00:00
|
|
|
bioText.entities.push_back(EntityInText(
|
|
|
|
EntityInTextCustomUrl,
|
|
|
|
0,
|
|
|
|
bioText.text.size(),
|
|
|
|
QString("internal:edit_bio")));
|
2017-09-13 16:57:44 +00:00
|
|
|
setLabeledText(
|
|
|
|
_bio,
|
|
|
|
lang(lng_profile_bio),
|
|
|
|
bioText,
|
|
|
|
TextWithEntities(),
|
|
|
|
QString());
|
2017-07-18 15:15:02 +00:00
|
|
|
if (auto text = _bio->entity()->textLabel()) {
|
|
|
|
text->setClickHandlerHook([](const ClickHandlerPtr &handler, Qt::MouseButton button) {
|
|
|
|
Ui::show(Box<EditBioBox>(App::self()));
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 16:57:44 +00:00
|
|
|
void InfoWidget::setLabeledText(
|
|
|
|
LabeledWrap *row,
|
|
|
|
const QString &label,
|
|
|
|
const TextWithEntities &textWithEntities,
|
|
|
|
const TextWithEntities &shortTextWithEntities,
|
|
|
|
const QString ©Text) {
|
2017-03-15 15:10:18 +00:00
|
|
|
auto nonEmptyText = !textWithEntities.text.isEmpty();
|
|
|
|
if (nonEmptyText) {
|
2017-09-13 16:57:44 +00:00
|
|
|
row->entity()->setLabeledText(
|
|
|
|
label,
|
|
|
|
textWithEntities,
|
|
|
|
shortTextWithEntities,
|
|
|
|
copyText,
|
|
|
|
width());
|
2016-08-22 17:16:21 +00:00
|
|
|
}
|
2017-10-01 08:15:40 +00:00
|
|
|
row->toggle(nonEmptyText, anim::type::normal);
|
2016-08-19 17:26:31 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 16:57:44 +00:00
|
|
|
InfoWidget::LabeledWidget::LabeledWidget(QWidget *parent, const style::FlatLabel &valueSt) : RpWidget(parent)
|
2017-07-18 15:15:02 +00:00
|
|
|
, _valueSt(valueSt) {
|
2016-08-28 19:16:23 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 16:57:44 +00:00
|
|
|
void InfoWidget::LabeledWidget::setLabeledText(
|
|
|
|
const QString &label,
|
|
|
|
const TextWithEntities &textWithEntities,
|
|
|
|
const TextWithEntities &shortTextWithEntities,
|
|
|
|
const QString ©Text,
|
|
|
|
int availableWidth) {
|
2016-08-22 17:16:21 +00:00
|
|
|
_label.destroy();
|
|
|
|
_text.destroy();
|
|
|
|
_shortText.destroy();
|
|
|
|
if (textWithEntities.text.isEmpty()) return;
|
|
|
|
|
2016-11-16 10:44:06 +00:00
|
|
|
_label.create(this, label, Ui::FlatLabel::InitType::Simple, st::settingsBlockLabel);
|
2016-08-22 17:16:21 +00:00
|
|
|
_label->show();
|
|
|
|
setLabelText(_text, textWithEntities, copyText);
|
|
|
|
setLabelText(_shortText, shortTextWithEntities, copyText);
|
2017-09-13 16:57:44 +00:00
|
|
|
resizeToNaturalWidth(availableWidth);
|
2016-08-22 17:16:21 +00:00
|
|
|
}
|
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
Ui::FlatLabel *InfoWidget::LabeledWidget::textLabel() const {
|
|
|
|
return _text;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ui::FlatLabel *InfoWidget::LabeledWidget::shortTextLabel() const {
|
|
|
|
return _shortText;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InfoWidget::LabeledWidget::setLabelText(object_ptr<Ui::FlatLabel> &text, const TextWithEntities &textWithEntities, const QString ©Text) {
|
2016-08-22 17:16:21 +00:00
|
|
|
text.destroy();
|
|
|
|
if (textWithEntities.text.isEmpty()) return;
|
|
|
|
|
2017-07-18 15:15:02 +00:00
|
|
|
text.create(this, QString(), Ui::FlatLabel::InitType::Simple, _valueSt);
|
2016-08-22 17:16:21 +00:00
|
|
|
text->show();
|
|
|
|
text->setMarkedText(textWithEntities);
|
|
|
|
text->setContextCopyText(copyText);
|
|
|
|
text->setSelectable(true);
|
|
|
|
text->setDoubleClickSelectsParagraph(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InfoWidget::notifyPeerUpdated(const Notify::PeerUpdate &update) {
|
|
|
|
if (update.peer != self()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (update.flags & UpdateFlag::UsernameChanged) {
|
|
|
|
refreshUsername();
|
|
|
|
}
|
|
|
|
if (update.flags & (UpdateFlag::UserPhoneChanged)) {
|
|
|
|
refreshMobileNumber();
|
|
|
|
}
|
2017-07-18 15:15:02 +00:00
|
|
|
if (update.flags & UpdateFlag::AboutChanged) {
|
|
|
|
refreshBio();
|
|
|
|
}
|
2016-08-22 17:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int InfoWidget::LabeledWidget::naturalWidth() const {
|
|
|
|
if (!_text) return -1;
|
2016-08-28 19:16:23 +00:00
|
|
|
return _label->naturalWidth() + st::normalFont->spacew + _text->naturalWidth();
|
2016-08-22 17:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int InfoWidget::LabeledWidget::resizeGetHeight(int newWidth) {
|
|
|
|
if (!_label) return 0;
|
|
|
|
|
2017-09-20 19:44:22 +00:00
|
|
|
_label->moveToLeft(
|
|
|
|
0,
|
|
|
|
st::settingsBlockOneLineTextPart.margin.top(),
|
|
|
|
newWidth);
|
2017-09-13 16:57:44 +00:00
|
|
|
_label->resizeToNaturalWidth(newWidth);
|
2016-08-19 17:26:31 +00:00
|
|
|
|
2016-08-22 17:16:21 +00:00
|
|
|
int textLeft = _label->width() + st::normalFont->spacew;
|
|
|
|
int textWidth = _text->naturalWidth();
|
|
|
|
int availableWidth = newWidth - textLeft;
|
|
|
|
bool doesNotFit = (textWidth > availableWidth);
|
|
|
|
accumulate_min(textWidth, availableWidth);
|
|
|
|
accumulate_min(textWidth, st::msgMaxWidth);
|
2017-09-20 19:44:22 +00:00
|
|
|
if (textWidth < 0) {
|
|
|
|
textWidth = 0;
|
2016-08-22 17:16:21 +00:00
|
|
|
}
|
2017-09-20 19:44:22 +00:00
|
|
|
_text->resizeToWidth(textWidth);
|
|
|
|
_text->moveToLeft(
|
|
|
|
textLeft,
|
|
|
|
st::settingsBlockOneLineTextPart.margin.top(),
|
|
|
|
newWidth);
|
2016-08-22 17:16:21 +00:00
|
|
|
if (_shortText) {
|
2017-09-20 19:44:22 +00:00
|
|
|
_shortText->resizeToWidth(textWidth);
|
|
|
|
_shortText->moveToLeft(
|
|
|
|
textLeft,
|
|
|
|
st::settingsBlockOneLineTextPart.margin.top(),
|
|
|
|
newWidth);
|
2016-08-22 17:16:21 +00:00
|
|
|
if (doesNotFit) {
|
|
|
|
_shortText->show();
|
|
|
|
_text->hide();
|
|
|
|
} else {
|
|
|
|
_shortText->hide();
|
|
|
|
_text->show();
|
|
|
|
}
|
|
|
|
}
|
2017-09-20 19:44:22 +00:00
|
|
|
return st::settingsBlockOneLineTextPart.margin.top()
|
|
|
|
+ qMax(_label->heightNoMargins(), _text->heightNoMargins())
|
|
|
|
+ st::settingsBlockOneLineTextPart.margin.bottom();
|
2016-08-19 17:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Settings
|