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
|
|
|
|
Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
|
|
|
|
*/
|
|
|
|
#include "stdafx.h"
|
|
|
|
#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"
|
2016-05-14 16:57:06 +00:00
|
|
|
#include "lang.h"
|
|
|
|
#include "mainwidget.h"
|
2016-05-27 13:56:35 +00:00
|
|
|
#include "boxes/addcontactbox.h"
|
|
|
|
#include "boxes/confirmbox.h"
|
2016-05-27 15:45:35 +00:00
|
|
|
#include "observer_peer.h"
|
2016-11-06 17:23:13 +00:00
|
|
|
#include "window/top_bar_widget.h"
|
2016-05-14 16:57:06 +00:00
|
|
|
|
|
|
|
namespace Profile {
|
|
|
|
|
2016-11-11 13:46:04 +00:00
|
|
|
class BackButton final : public Ui::AbstractButton, private base::Subscriber {
|
2016-05-14 16:57:06 +00:00
|
|
|
public:
|
2016-11-11 13:46:04 +00:00
|
|
|
BackButton(QWidget *parent) : Ui::AbstractButton(parent) {
|
2016-05-14 16:57:06 +00:00
|
|
|
setCursor(style::cur_pointer);
|
2016-11-06 17:23:13 +00:00
|
|
|
|
|
|
|
subscribe(Adaptive::Changed(), [this] { updateAdaptiveLayout(); });
|
|
|
|
updateAdaptiveLayout();
|
2016-05-14 16:57:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2016-08-22 17:16:21 +00:00
|
|
|
int resizeGetHeight(int newWidth) override {
|
|
|
|
return st::profileTopBarHeight;
|
|
|
|
}
|
2016-05-25 17:59:21 +00:00
|
|
|
void paintEvent(QPaintEvent *e) override {
|
2016-05-14 16:57:06 +00:00
|
|
|
Painter p(this);
|
|
|
|
|
|
|
|
p.fillRect(e->rect(), st::profileBg);
|
|
|
|
st::profileTopBarBackIcon.paint(p, st::profileTopBarBackIconPosition, width());
|
|
|
|
|
|
|
|
p.setFont(st::profileTopBarBackFont);
|
|
|
|
p.setPen(st::profileTopBarBackFg);
|
|
|
|
p.drawTextLeft(st::profileTopBarBackPosition.x(), st::profileTopBarBackPosition.y(), width(), lang(lng_menu_back));
|
2016-11-06 17:23:13 +00:00
|
|
|
|
|
|
|
Window::TopBarWidget::paintUnreadCounter(p, width());
|
2016-05-14 16:57:06 +00:00
|
|
|
}
|
2016-11-11 13:46:04 +00:00
|
|
|
void onStateChanged(int oldState, StateChangeSource source) override {
|
|
|
|
if ((_state & StateDown) && !(oldState & StateDown)) {
|
2016-05-25 17:59:21 +00:00
|
|
|
emit clicked();
|
|
|
|
}
|
|
|
|
}
|
2016-05-14 16:57:06 +00:00
|
|
|
|
|
|
|
private:
|
2016-11-06 17:23:13 +00:00
|
|
|
void updateAdaptiveLayout() {
|
|
|
|
if (!Adaptive::OneColumn()) {
|
|
|
|
unsubscribe(base::take(_unreadCounterSubscription));
|
|
|
|
} else if (!_unreadCounterSubscription) {
|
|
|
|
_unreadCounterSubscription = subscribe(Global::RefUnreadCounterUpdate(), [this] {
|
|
|
|
rtlupdate(0, 0, st::titleUnreadCounterRight, st::titleUnreadCounterTop);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int _unreadCounterSubscription = 0;
|
2016-05-14 16:57:06 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
| UpdateFlag::ChannelAmEditor;
|
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)
|
|
|
|
, _backButton(this) {
|
|
|
|
_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()) {
|
|
|
|
addRightAction(RightActionType::ShareContact, lang(lng_profile_top_bar_share_contact), SLOT(onShareContact()));
|
|
|
|
}
|
|
|
|
if (_peerUser->isContact()) {
|
2016-05-27 13:56:35 +00:00
|
|
|
addRightAction(RightActionType::EditContact, lang(lng_profile_edit_contact), SLOT(onEditContact()));
|
|
|
|
addRightAction(RightActionType::DeleteContact, lang(lng_profile_delete_contact), SLOT(onDeleteContact()));
|
2016-05-27 15:45:35 +00:00
|
|
|
} else if (_peerUser->canAddContact()) {
|
2016-05-27 13:56:35 +00:00
|
|
|
addRightAction(RightActionType::AddContact, lang(lng_profile_add_contact), SLOT(onAddContact()));
|
|
|
|
}
|
|
|
|
}
|
2016-05-23 12:41:09 +00:00
|
|
|
|
2016-05-27 13:56:35 +00:00
|
|
|
void FixedBar::setChatActions() {
|
|
|
|
if (_peerChat->canEdit()) {
|
|
|
|
addRightAction(RightActionType::EditGroup, lang(lng_profile_edit_contact), SLOT(onEditGroup()));
|
|
|
|
}
|
|
|
|
addRightAction(RightActionType::LeaveGroup, lang(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() {
|
|
|
|
if (_peerMegagroup->amCreator() || _peerMegagroup->amEditor()) {
|
|
|
|
addRightAction(RightActionType::EditChannel, lang(lng_profile_edit_contact), SLOT(onEditChannel()));
|
|
|
|
}
|
|
|
|
}
|
2016-05-23 12:41:09 +00:00
|
|
|
|
2016-05-27 13:56:35 +00:00
|
|
|
void FixedBar::setChannelActions() {
|
|
|
|
if (_peerChannel->amCreator()) {
|
|
|
|
addRightAction(RightActionType::EditChannel, lang(lng_profile_edit_contact), SLOT(onEditChannel()));
|
|
|
|
}
|
2016-05-23 12:41:09 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 13:56:35 +00:00
|
|
|
void FixedBar::addRightAction(RightActionType type, const QString &text, const char *slot) {
|
|
|
|
if (_rightActions.size() > _currentAction) {
|
|
|
|
if (_rightActions.at(_currentAction).type == type) {
|
|
|
|
++_currentAction;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t_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;
|
2016-06-07 19:59:39 +00:00
|
|
|
_rightActions[_currentAction].button = new Ui::RoundButton(this, text, st::profileFixedBarButton);
|
2016-11-11 13:46:04 +00:00
|
|
|
_rightActions[_currentAction].button->setTextTransform(Ui::RoundButton::TextTransform::NoTransform);
|
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() {
|
|
|
|
Ui::showLayer(new EditChannelBox(_peerMegagroup ? _peerMegagroup : _peerChannel));
|
|
|
|
}
|
|
|
|
|
|
|
|
void FixedBar::onEditGroup() {
|
|
|
|
Ui::showLayer(new EditNameTitleBox(_peerChat));
|
|
|
|
}
|
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-05-27 13:56:35 +00:00
|
|
|
Ui::showLayer(new AddContactBox(firstName, lastName, phone));
|
2016-05-23 12:41:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FixedBar::onEditContact() {
|
2016-05-27 13:56:35 +00:00
|
|
|
Ui::showLayer(new AddContactBox(_peerUser));
|
|
|
|
}
|
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-05-27 13:56:35 +00:00
|
|
|
ConfirmBox *box = new ConfirmBox(lng_sure_delete_contact(lt_contact, App::peerName(_peerUser)), lang(lng_box_delete));
|
|
|
|
connect(box, SIGNAL(confirmed()), this, SLOT(onDeleteContactSure()));
|
|
|
|
Ui::showLayer(box);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FixedBar::onDeleteContactSure() {
|
|
|
|
Ui::showChatsList();
|
|
|
|
Ui::hideLayer();
|
|
|
|
MTP::send(MTPcontacts_DeleteContact(_peerUser->inputUser), App::main()->rpcDone(&MainWidget::deletedContact, _peerUser));
|
|
|
|
}
|
|
|
|
|
|
|
|
void FixedBar::onLeaveGroup() {
|
|
|
|
ConfirmBox *box = new ConfirmBox(lng_sure_delete_and_exit(lt_group, App::peerName(_peerChat)), lang(lng_box_leave), st::attentionBoxButton);
|
|
|
|
connect(box, SIGNAL(confirmed()), this, SLOT(onLeaveGroupSure()));
|
|
|
|
Ui::showLayer(box);
|
|
|
|
}
|
2016-05-23 12:41:09 +00:00
|
|
|
|
2016-05-27 13:56:35 +00:00
|
|
|
void FixedBar::onLeaveGroupSure() {
|
|
|
|
Ui::showChatsList();
|
|
|
|
Ui::hideLayer();
|
2016-06-01 20:05:37 +00:00
|
|
|
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;
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-05-14 16:57:06 +00:00
|
|
|
_backButton->resizeToWidth(newWidth);
|
2016-05-31 19:27:11 +00:00
|
|
|
_backButton->moveToLeft(0, 0);
|
2016-05-14 16:57:06 +00:00
|
|
|
newHeight += _backButton->height();
|
|
|
|
|
2016-08-22 17:16:21 +00:00
|
|
|
return newHeight;
|
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
|