2016-05-14 16:57:06 +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-14 16:57:06 +00:00
|
|
|
*/
|
|
|
|
#include "profile/profile_fixed_bar.h"
|
|
|
|
|
|
|
|
#include "styles/style_profile.h"
|
2016-11-06 17:23:13 +00:00
|
|
|
#include "styles/style_window.h"
|
2016-11-11 13:46:04 +00:00
|
|
|
#include "ui/widgets/buttons.h"
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2016-05-14 16:57:06 +00:00
|
|
|
#include "mainwidget.h"
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "boxes/add_contact_box.h"
|
|
|
|
#include "boxes/confirm_box.h"
|
2016-05-27 15:45:35 +00:00
|
|
|
#include "observer_peer.h"
|
2016-11-15 11:56:49 +00:00
|
|
|
#include "styles/style_boxes.h"
|
2016-12-08 14:08:54 +00:00
|
|
|
#include "profile/profile_back_button.h"
|
2016-05-14 16:57:06 +00:00
|
|
|
|
|
|
|
namespace Profile {
|
2016-05-27 15:45:35 +00:00
|
|
|
namespace {
|
|
|
|
|
2016-06-01 20:05:37 +00:00
|
|
|
using UpdateFlag = Notify::PeerUpdate::Flag;
|
|
|
|
const auto ButtonsUpdateFlags = UpdateFlag::UserCanShareContact
|
|
|
|
| UpdateFlag::UserIsContact
|
|
|
|
| UpdateFlag::ChatCanEdit
|
2017-06-09 16:12:02 +00:00
|
|
|
| UpdateFlag::ChannelRightsChanged;
|
2016-05-27 15:45:35 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2016-05-14 16:57:06 +00:00
|
|
|
FixedBar::FixedBar(QWidget *parent, PeerData *peer) : TWidget(parent)
|
|
|
|
, _peer(peer)
|
|
|
|
, _peerUser(peer->asUser())
|
|
|
|
, _peerChat(peer->asChat())
|
|
|
|
, _peerChannel(peer->asChannel())
|
|
|
|
, _peerMegagroup(peer->isMegagroup() ? _peerChannel : nullptr)
|
2016-12-08 14:08:54 +00:00
|
|
|
, _backButton(this, lang(lng_menu_back)) {
|
2017-05-30 15:21:05 +00:00
|
|
|
subscribe(Lang::Current().updated(), [this] { refreshLang(); });
|
|
|
|
|
2016-05-14 16:57:06 +00:00
|
|
|
_backButton->moveToLeft(0, 0);
|
|
|
|
connect(_backButton, SIGNAL(clicked()), this, SLOT(onBack()));
|
2016-05-27 13:56:35 +00:00
|
|
|
|
2016-06-04 20:29:16 +00:00
|
|
|
auto observeEvents = ButtonsUpdateFlags
|
|
|
|
| UpdateFlag::MigrationChanged;
|
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
|
|
|
notifyPeerUpdate(update);
|
2016-09-26 18:33:34 +00:00
|
|
|
}));
|
2016-05-27 15:45:35 +00:00
|
|
|
|
2016-05-27 13:56:35 +00:00
|
|
|
refreshRightActions();
|
2016-05-14 16:57:06 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 15:45:35 +00:00
|
|
|
void FixedBar::notifyPeerUpdate(const Notify::PeerUpdate &update) {
|
|
|
|
if (update.peer != _peer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((update.flags & ButtonsUpdateFlags) != 0) {
|
|
|
|
refreshRightActions();
|
|
|
|
}
|
2016-06-04 20:29:16 +00:00
|
|
|
if (update.flags & UpdateFlag::MigrationChanged) {
|
|
|
|
if (_peerChat && _peerChat->migrateTo()) {
|
|
|
|
auto channel = _peerChat->migrateTo();
|
|
|
|
onBack();
|
|
|
|
Ui::showPeerProfile(channel);
|
|
|
|
}
|
|
|
|
}
|
2016-05-27 15:45:35 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 13:56:35 +00:00
|
|
|
void FixedBar::refreshRightActions() {
|
|
|
|
_currentAction = 0;
|
|
|
|
if (_peerUser) {
|
|
|
|
setUserActions();
|
|
|
|
} else if (_peerChat) {
|
|
|
|
setChatActions();
|
|
|
|
} else if (_peerMegagroup) {
|
|
|
|
setMegagroupActions();
|
|
|
|
} else if (_peerChannel) {
|
|
|
|
setChannelActions();
|
|
|
|
}
|
|
|
|
while (_rightActions.size() > _currentAction) {
|
|
|
|
delete _rightActions.back().button;
|
|
|
|
_rightActions.pop_back();
|
|
|
|
}
|
|
|
|
resizeToWidth(width());
|
2016-05-14 16:57:06 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 13:56:35 +00:00
|
|
|
void FixedBar::setUserActions() {
|
2016-05-27 15:45:35 +00:00
|
|
|
if (_peerUser->canShareThisContact()) {
|
2017-05-30 15:21:05 +00:00
|
|
|
addRightAction(RightActionType::ShareContact, langFactory(lng_profile_top_bar_share_contact), SLOT(onShareContact()));
|
2016-05-27 15:45:35 +00:00
|
|
|
}
|
|
|
|
if (_peerUser->isContact()) {
|
2017-05-30 15:21:05 +00:00
|
|
|
addRightAction(RightActionType::EditContact, langFactory(lng_profile_edit_contact), SLOT(onEditContact()));
|
|
|
|
addRightAction(RightActionType::DeleteContact, langFactory(lng_profile_delete_contact), SLOT(onDeleteContact()));
|
2016-05-27 15:45:35 +00:00
|
|
|
} else if (_peerUser->canAddContact()) {
|
2017-05-30 15:21:05 +00:00
|
|
|
addRightAction(RightActionType::AddContact, langFactory(lng_profile_add_contact), SLOT(onAddContact()));
|
2016-05-27 13:56:35 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-23 12:41:09 +00:00
|
|
|
|
2016-05-27 13:56:35 +00:00
|
|
|
void FixedBar::setChatActions() {
|
|
|
|
if (_peerChat->canEdit()) {
|
2017-05-30 15:21:05 +00:00
|
|
|
addRightAction(RightActionType::EditGroup, langFactory(lng_profile_edit_contact), SLOT(onEditGroup()));
|
2016-05-27 13:56:35 +00:00
|
|
|
}
|
2017-05-30 15:21:05 +00:00
|
|
|
addRightAction(RightActionType::LeaveGroup, langFactory(lng_profile_delete_and_exit), SLOT(onLeaveGroup()));
|
2016-05-23 12:41:09 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 13:56:35 +00:00
|
|
|
void FixedBar::setMegagroupActions() {
|
2017-06-09 16:12:02 +00:00
|
|
|
if (_peerMegagroup->canEditInformation()) {
|
2017-05-30 15:21:05 +00:00
|
|
|
addRightAction(RightActionType::EditChannel, langFactory(lng_profile_edit_contact), SLOT(onEditChannel()));
|
2016-05-27 13:56:35 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-23 12:41:09 +00:00
|
|
|
|
2016-05-27 13:56:35 +00:00
|
|
|
void FixedBar::setChannelActions() {
|
2017-06-09 16:12:02 +00:00
|
|
|
if (_peerChannel->canEditInformation()) {
|
2017-05-30 15:21:05 +00:00
|
|
|
addRightAction(RightActionType::EditChannel, langFactory(lng_profile_edit_contact), SLOT(onEditChannel()));
|
2016-05-27 13:56:35 +00:00
|
|
|
}
|
2016-05-23 12:41:09 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 15:21:05 +00:00
|
|
|
void FixedBar::addRightAction(RightActionType type, base::lambda<QString()> textFactory, const char *slot) {
|
2016-05-27 13:56:35 +00:00
|
|
|
if (_rightActions.size() > _currentAction) {
|
|
|
|
if (_rightActions.at(_currentAction).type == type) {
|
|
|
|
++_currentAction;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(_rightActions.size() == _currentAction);
|
2016-05-27 15:45:35 +00:00
|
|
|
_rightActions.push_back(RightAction());
|
2016-05-27 13:56:35 +00:00
|
|
|
}
|
|
|
|
_rightActions[_currentAction].type = type;
|
2016-05-27 15:45:35 +00:00
|
|
|
delete _rightActions[_currentAction].button;
|
2017-05-30 15:21:05 +00:00
|
|
|
_rightActions[_currentAction].button = new Ui::RoundButton(this, std::move(textFactory), st::profileFixedBarButton);
|
2016-05-27 13:56:35 +00:00
|
|
|
connect(_rightActions[_currentAction].button, SIGNAL(clicked()), this, slot);
|
|
|
|
bool showButton = !_animatingMode && (type != RightActionType::ShareContact || !_hideShareContactButton);
|
|
|
|
_rightActions[_currentAction].button->setVisible(showButton);
|
|
|
|
++_currentAction;
|
|
|
|
}
|
2016-05-23 12:41:09 +00:00
|
|
|
|
2016-05-27 13:56:35 +00:00
|
|
|
void FixedBar::onBack() {
|
|
|
|
App::main()->showBackFromStack();
|
2016-05-23 12:41:09 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 13:56:35 +00:00
|
|
|
void FixedBar::onEditChannel() {
|
2016-12-13 17:07:56 +00:00
|
|
|
Ui::show(Box<EditChannelBox>(_peerMegagroup ? _peerMegagroup : _peerChannel));
|
2016-05-27 13:56:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FixedBar::onEditGroup() {
|
2016-12-13 17:07:56 +00:00
|
|
|
Ui::show(Box<EditNameTitleBox>(_peerChat));
|
2016-05-27 13:56:35 +00:00
|
|
|
}
|
2016-05-23 12:41:09 +00:00
|
|
|
|
2016-05-27 13:56:35 +00:00
|
|
|
void FixedBar::onAddContact() {
|
|
|
|
auto firstName = _peerUser->firstName;
|
|
|
|
auto lastName = _peerUser->lastName;
|
2016-05-31 19:27:11 +00:00
|
|
|
auto phone = _peerUser->phone().isEmpty() ? App::phoneFromSharedContact(peerToUser(_peer->id)) : _peerUser->phone();
|
2016-12-13 17:07:56 +00:00
|
|
|
Ui::show(Box<AddContactBox>(firstName, lastName, phone));
|
2016-05-23 12:41:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FixedBar::onEditContact() {
|
2016-12-13 17:07:56 +00:00
|
|
|
Ui::show(Box<AddContactBox>(_peerUser));
|
2016-05-27 13:56:35 +00:00
|
|
|
}
|
2016-05-23 12:41:09 +00:00
|
|
|
|
2016-05-27 13:56:35 +00:00
|
|
|
void FixedBar::onShareContact() {
|
|
|
|
App::main()->shareContactLayer(_peerUser);
|
2016-05-23 12:41:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FixedBar::onDeleteContact() {
|
2016-12-13 17:07:56 +00:00
|
|
|
auto text = lng_sure_delete_contact(lt_contact, App::peerName(_peerUser));
|
|
|
|
Ui::show(Box<ConfirmBox>(text, lang(lng_box_delete), base::lambda_guarded(this, [this] {
|
|
|
|
Ui::showChatsList();
|
|
|
|
Ui::hideLayer();
|
|
|
|
MTP::send(MTPcontacts_DeleteContact(_peerUser->inputUser), App::main()->rpcDone(&MainWidget::deletedContact, _peerUser));
|
|
|
|
})));
|
2016-05-27 13:56:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FixedBar::onLeaveGroup() {
|
2016-12-13 17:07:56 +00:00
|
|
|
auto text = lng_sure_delete_and_exit(lt_group, App::peerName(_peerChat));
|
|
|
|
Ui::show(Box<ConfirmBox>(text, lang(lng_box_leave), st::attentionBoxButton, base::lambda_guarded(this, [this] {
|
|
|
|
Ui::showChatsList();
|
|
|
|
Ui::hideLayer();
|
|
|
|
App::main()->deleteAndExit(_peerChat);
|
|
|
|
})));
|
2016-05-23 12:41:09 +00:00
|
|
|
}
|
|
|
|
|
2016-08-22 17:16:21 +00:00
|
|
|
int FixedBar::resizeGetHeight(int newWidth) {
|
2016-05-14 16:57:06 +00:00
|
|
|
int newHeight = 0;
|
|
|
|
|
2017-05-30 15:21:05 +00:00
|
|
|
updateButtonsGeometry(newWidth);
|
|
|
|
|
|
|
|
_backButton->resizeToWidth(newWidth);
|
|
|
|
_backButton->moveToLeft(0, 0);
|
|
|
|
newHeight += _backButton->height();
|
|
|
|
|
|
|
|
return newHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FixedBar::updateButtonsGeometry(int newWidth) {
|
2016-05-27 13:56:35 +00:00
|
|
|
int buttonLeft = newWidth;
|
|
|
|
for (auto i = _rightActions.cend(), b = _rightActions.cbegin(); i != b;) {
|
|
|
|
--i;
|
|
|
|
buttonLeft -= i->button->width();
|
|
|
|
i->button->moveToLeft(buttonLeft, 0);
|
|
|
|
}
|
2017-05-30 15:21:05 +00:00
|
|
|
}
|
2016-05-27 13:56:35 +00:00
|
|
|
|
2017-05-30 15:21:05 +00:00
|
|
|
void FixedBar::refreshLang() {
|
|
|
|
InvokeQueued(this, [this] { updateButtonsGeometry(width()); });
|
2016-05-14 16:57:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FixedBar::setAnimatingMode(bool enabled) {
|
|
|
|
if (_animatingMode != enabled) {
|
|
|
|
_animatingMode = enabled;
|
|
|
|
setCursor(_animatingMode ? style::cur_pointer : style::cur_default);
|
|
|
|
if (_animatingMode) {
|
|
|
|
setAttribute(Qt::WA_OpaquePaintEvent, false);
|
|
|
|
hideChildren();
|
|
|
|
} else {
|
|
|
|
setAttribute(Qt::WA_OpaquePaintEvent);
|
|
|
|
showChildren();
|
2016-05-27 13:56:35 +00:00
|
|
|
if (_hideShareContactButton) {
|
|
|
|
applyHideShareContactButton();
|
|
|
|
}
|
2016-05-14 16:57:06 +00:00
|
|
|
}
|
|
|
|
show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-27 13:56:35 +00:00
|
|
|
void FixedBar::setHideShareContactButton(bool hideButton) {
|
|
|
|
_hideShareContactButton = hideButton;
|
|
|
|
if (!_animatingMode) {
|
|
|
|
applyHideShareContactButton();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FixedBar::applyHideShareContactButton() {
|
|
|
|
for_const (auto &action, _rightActions) {
|
|
|
|
if (action.type == RightActionType::ShareContact) {
|
2016-05-27 15:45:35 +00:00
|
|
|
action.button->setVisible(!_hideShareContactButton);
|
2016-05-27 13:56:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-14 16:57:06 +00:00
|
|
|
void FixedBar::mousePressEvent(QMouseEvent *e) {
|
|
|
|
if (e->button() == Qt::LeftButton) {
|
|
|
|
onBack();
|
|
|
|
} else {
|
|
|
|
TWidget::mousePressEvent(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Profile
|