tdesktop/Telegram/SourceFiles/boxes/addcontactbox.cpp

1400 lines
48 KiB
C++
Raw Normal View History

/*
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.
2015-10-03 13:16:42 +00:00
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
2016-02-08 10:56:18 +00:00
Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
*/
#include "stdafx.h"
#include "lang.h"
#include "application.h"
#include "addcontactbox.h"
#include "contactsbox.h"
2015-10-11 08:37:24 +00:00
#include "confirmbox.h"
#include "photocropbox.h"
#include "gui/filedialog.h"
#include "mainwidget.h"
#include "window.h"
2015-10-11 08:37:24 +00:00
AddContactBox::AddContactBox(QString fname, QString lname, QString phone) : AbstractBox(st::boxWidth)
, _user(0)
, _save(this, lang(lng_add_contact), st::defaultBoxButton)
, _cancel(this, lang(lng_cancel), st::cancelBoxButton)
, _retry(this, lang(lng_try_other_contact), st::defaultBoxButton)
, _first(this, st::defaultInputField, lang(lng_signup_firstname), fname)
, _last(this, st::defaultInputField, lang(lng_signup_lastname), lname)
, _phone(this, st::defaultInputField, lang(lng_contact_phone), phone)
, _invertOrder(langFirstNameGoesSecond())
, _contactId(0)
, _addRequest(0) {
if (!phone.isEmpty()) {
2015-10-11 08:37:24 +00:00
_phone.setDisabled(true);
}
initBox();
}
2015-10-11 08:37:24 +00:00
AddContactBox::AddContactBox(UserData *user) : AbstractBox(st::boxWidth)
, _user(user)
, _save(this, lang(lng_settings_save), st::defaultBoxButton)
, _cancel(this, lang(lng_cancel), st::cancelBoxButton)
, _retry(this, lang(lng_try_other_contact), st::defaultBoxButton)
, _first(this, st::defaultInputField, lang(lng_signup_firstname), user->firstName)
, _last(this, st::defaultInputField, lang(lng_signup_lastname), user->lastName)
, _phone(this, st::defaultInputField, lang(lng_contact_phone), user->phone)
, _invertOrder(langFirstNameGoesSecond())
, _contactId(0)
, _addRequest(0) {
_phone.setDisabled(true);
initBox();
}
void AddContactBox::initBox() {
if (_invertOrder) {
2015-10-11 08:37:24 +00:00
setTabOrder(&_last, &_first);
}
if (_user) {
_boxTitle = lang(lng_edit_contact_title);
} else {
2015-10-11 08:37:24 +00:00
bool readyToAdd = !_phone.getLastText().isEmpty() && (!_first.getLastText().isEmpty() || !_last.getLastText().isEmpty());
_boxTitle = lang(readyToAdd ? lng_confirm_contact_data : lng_enter_contact_data);
}
2015-10-11 08:37:24 +00:00
setMaxHeight(st::boxTitleHeight + st::contactPadding.top() + _first.height() + st::contactSkip + _last.height() + st::contactPhoneSkip + _phone.height() + st::contactPadding.bottom() + st::boxPadding.bottom() + st::boxButtonPadding.top() + _save.height() + st::boxButtonPadding.bottom());
_retry.hide();
2015-10-11 08:37:24 +00:00
connect(&_save, SIGNAL(clicked()), this, SLOT(onSave()));
connect(&_cancel, SIGNAL(clicked()), this, SLOT(onClose()));
connect(&_retry, SIGNAL(clicked()), this, SLOT(onRetry()));
connect(&_first, SIGNAL(submitted(bool)), this, SLOT(onSubmit()));
connect(&_last, SIGNAL(submitted(bool)), this, SLOT(onSubmit()));
connect(&_phone, SIGNAL(submitted(bool)), this, SLOT(onSubmit()));
prepare();
}
void AddContactBox::hideAll() {
2015-10-11 08:37:24 +00:00
_first.hide();
_last.hide();
_phone.hide();
_save.hide();
_cancel.hide();
_retry.hide();
}
void AddContactBox::showAll() {
2015-10-11 08:37:24 +00:00
_first.show();
_last.show();
_phone.show();
_save.show();
_cancel.show();
}
void AddContactBox::showDone() {
2015-10-11 08:37:24 +00:00
if ((_first.getLastText().isEmpty() && _last.getLastText().isEmpty()) || !_phone.isEnabled()) {
(_invertOrder ? _last : _first).setFocus();
} else {
2015-10-11 08:37:24 +00:00
_phone.setFocus();
}
}
void AddContactBox::paintEvent(QPaintEvent *e) {
2015-04-04 20:01:34 +00:00
Painter p(this);
if (paint(p)) return;
2015-10-11 08:37:24 +00:00
paintTitle(p, _boxTitle);
if (_retry.isHidden()) {
p.drawSpriteLeft(st::boxPadding.left(), _first.y() + st::contactIconTop, width(), st::contactUserIcon);
p.drawSpriteLeft(st::boxPadding.left(), _phone.y() + st::contactIconTop, width(), st::contactPhoneIcon);
} else {
p.setPen(st::black->p);
2015-10-11 08:37:24 +00:00
p.setFont(st::boxTextFont->f);
int32 h = height() - st::boxTitleHeight - st::contactPadding.top() - st::contactPadding.bottom() - st::boxPadding.bottom() - st::boxButtonPadding.top() - _retry.height() - st::boxButtonPadding.bottom();
p.drawText(QRect(st::boxPadding.left(), st::boxTitleHeight + st::contactPadding.top(), width() - st::boxPadding.left() - st::boxPadding.right(), h), lng_contact_not_joined(lt_name, _sentName), style::al_topleft);
}
}
void AddContactBox::resizeEvent(QResizeEvent *e) {
2015-10-11 08:37:24 +00:00
_first.resize(width() - st::boxPadding.left() - st::contactPadding.left() - st::boxPadding.right(), _first.height());
_last.resize(_first.width(), _last.height());
_phone.resize(_first.width(), _last.height());
if (_invertOrder) {
2015-10-11 08:37:24 +00:00
_last.moveToLeft(st::boxPadding.left() + st::contactPadding.left(), st::boxTitleHeight + st::contactPadding.top());
2015-10-14 11:51:37 +00:00
_first.moveToLeft(st::boxPadding.left() + st::contactPadding.left(), _last.y() + _last.height() + st::contactSkip);
_phone.moveToLeft(st::boxPadding.left() + st::contactPadding.left(), _first.y() + _first.height() + st::contactPhoneSkip);
} else {
2015-10-11 08:37:24 +00:00
_first.moveToLeft(st::boxPadding.left() + st::contactPadding.left(), st::boxTitleHeight + st::contactPadding.top());
2015-10-14 11:51:37 +00:00
_last.moveToLeft(st::boxPadding.left() + st::contactPadding.left(), _first.y() + _first.height() + st::contactSkip);
_phone.moveToLeft(st::boxPadding.left() + st::contactPadding.left(), _last.y() + _last.height() + st::contactPhoneSkip);
}
2015-10-11 08:37:24 +00:00
_save.moveToRight(st::boxButtonPadding.right(), height() - st::boxButtonPadding.bottom() - _save.height());
_retry.moveToRight(st::boxButtonPadding.right(), _save.y());
_cancel.moveToRight(st::boxButtonPadding.right() + (_retry.isHidden() ? _save.width() : _retry.width()) + st::boxButtonPadding.left(), _save.y());
}
2015-10-11 08:37:24 +00:00
void AddContactBox::onSubmit() {
if (_first.hasFocus()) {
_last.setFocus();
} else if (_last.hasFocus()) {
if (_phone.isEnabled()) {
_phone.setFocus();
} else {
onSave();
}
} else if (_phone.hasFocus()) {
onSave();
}
}
void AddContactBox::onSave() {
if (_addRequest) return;
QString firstName = prepareText(_first.getLastText());
QString lastName = prepareText(_last.getLastText());
2015-10-14 11:51:37 +00:00
QString phone = _phone.getLastText().trimmed();
if (firstName.isEmpty() && lastName.isEmpty()) {
if (_invertOrder) {
2015-10-11 08:37:24 +00:00
_last.setFocus();
_last.showError();
} else {
2015-10-11 08:37:24 +00:00
_first.setFocus();
_first.showError();
}
return;
2015-10-11 08:37:24 +00:00
} else if (!_user && !App::isValidPhone(phone)) {
_phone.setFocus();
_phone.showError();
return;
}
if (firstName.isEmpty()) {
firstName = lastName;
lastName = QString();
}
_sentName = firstName;
2015-10-11 08:37:24 +00:00
if (_user) {
_contactId = MTP::nonce<uint64>();
QVector<MTPInputContact> v(1, MTP_inputPhoneContact(MTP_long(_contactId), MTP_string(_user->phone), MTP_string(firstName), MTP_string(lastName)));
_addRequest = MTP::send(MTPcontacts_ImportContacts(MTP_vector<MTPInputContact>(v), MTP_bool(false)), rpcDone(&AddContactBox::onSaveUserDone), rpcFail(&AddContactBox::onSaveUserFail));
} else {
_contactId = MTP::nonce<uint64>();
QVector<MTPInputContact> v(1, MTP_inputPhoneContact(MTP_long(_contactId), MTP_string(phone), MTP_string(firstName), MTP_string(lastName)));
_addRequest = MTP::send(MTPcontacts_ImportContacts(MTP_vector<MTPInputContact>(v), MTP_bool(false)), rpcDone(&AddContactBox::onImportDone));
}
}
2015-10-11 08:37:24 +00:00
bool AddContactBox::onSaveUserFail(const RPCError &error) {
2015-10-06 19:49:23 +00:00
if (mtpIsFlood(error)) return false;
2015-04-04 20:01:34 +00:00
2014-10-22 18:39:03 +00:00
_addRequest = 0;
QString err(error.type());
2015-10-11 08:37:24 +00:00
QString firstName = _first.getLastText().trimmed(), lastName = _last.getLastText().trimmed();
if (err == "CHAT_TITLE_NOT_MODIFIED") {
2015-10-11 08:37:24 +00:00
_user->updateName(firstName, QString(), QString());
emit closed();
return true;
} else if (err == "NO_CHAT_TITLE") {
2015-10-11 08:37:24 +00:00
_first.setFocus();
_first.showError();
return true;
}
2015-10-11 08:37:24 +00:00
_first.setFocus();
return true;
}
void AddContactBox::onImportDone(const MTPcontacts_ImportedContacts &res) {
if (isHidden() || !App::main()) return;
const MTPDcontacts_importedContacts &d(res.c_contacts_importedContacts());
App::feedUsers(d.vusers);
const QVector<MTPImportedContact> &v(d.vimported.c_vector().v);
int32 uid = 0;
if (!v.isEmpty()) {
const MTPDimportedContact &c(v.front().c_importedContact());
if (c.vclient_id.v != _contactId) return;
uid = c.vuser_id.v;
if (uid && !App::userLoaded(uid)) {
uid = 0;
}
}
if (uid) {
Notify::userIsContactChanged(App::userLoaded(peerFromUser(uid)), true);
2015-12-07 18:09:05 +00:00
Ui::hideLayer();
} else {
2015-10-11 08:37:24 +00:00
_save.hide();
_first.hide();
_last.hide();
_phone.hide();
_retry.show();
resizeEvent(0);
update();
}
}
void AddContactBox::onSaveUserDone(const MTPcontacts_ImportedContacts &res) {
const MTPDcontacts_importedContacts &d(res.c_contacts_importedContacts());
App::feedUsers(d.vusers);
emit closed();
}
void AddContactBox::onRetry() {
_addRequest = 0;
_contactId = 0;
2015-10-11 08:37:24 +00:00
_save.show();
_retry.hide();
resizeEvent(0);
showAll();
2015-10-11 08:37:24 +00:00
_first.setText(QString());
_first.updatePlaceholder();
_last.setText(QString());
_last.updatePlaceholder();
_phone.clearText();
_phone.setDisabled(false);
_first.setFocus();
update();
}
NewGroupBox::NewGroupBox() : AbstractBox(),
_group(this, qsl("group_type"), 0, lang(lng_create_group_title), true),
_channel(this, qsl("group_type"), 1, lang(lng_create_channel_title)),
_aboutGroupWidth(width() - st::boxPadding.left() - st::boxButtonPadding.right() - st::newGroupPadding.left() - st::defaultRadiobutton.textPosition.x()),
_aboutGroup(st::normalFont, lng_create_group_about(lt_count, Global::MegagroupSizeMax()), _defaultOptions, _aboutGroupWidth),
2015-10-11 08:37:24 +00:00
_aboutChannel(st::normalFont, lang(lng_create_channel_about), _defaultOptions, _aboutGroupWidth),
_next(this, lang(lng_create_group_next), st::defaultBoxButton),
_cancel(this, lang(lng_cancel), st::cancelBoxButton) {
_aboutGroupHeight = _aboutGroup.countHeight(_aboutGroupWidth);
setMaxHeight(st::boxPadding.top() + st::newGroupPadding.top() + _group.height() + _aboutGroupHeight + st::newGroupSkip + _channel.height() + _aboutChannel.countHeight(_aboutGroupWidth) + st::newGroupPadding.bottom() + st::boxPadding.bottom() + st::boxButtonPadding.top() + _next.height() + st::boxButtonPadding.bottom());
connect(&_next, SIGNAL(clicked()), this, SLOT(onNext()));
connect(&_cancel, SIGNAL(clicked()), this, SLOT(onClose()));
prepare();
}
void NewGroupBox::hideAll() {
_group.hide();
_channel.hide();
_cancel.hide();
_next.hide();
}
void NewGroupBox::showAll() {
_group.show();
_channel.show();
_cancel.show();
_next.show();
}
void NewGroupBox::showDone() {
setFocus();
}
void NewGroupBox::keyPressEvent(QKeyEvent *e) {
if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
onNext();
} else {
AbstractBox::keyPressEvent(e);
}
}
void NewGroupBox::paintEvent(QPaintEvent *e) {
Painter p(this);
if (paint(p)) return;
p.setPen(st::newGroupAboutFg->p);
2015-10-14 11:51:37 +00:00
QRect aboutGroup(st::boxPadding.left() + st::newGroupPadding.left() + st::defaultRadiobutton.textPosition.x(), _group.y() + _group.height() + st::lineWidth, _aboutGroupWidth, _aboutGroupHeight);
_aboutGroup.drawLeft(p, aboutGroup.x(), aboutGroup.y(), aboutGroup.width(), width());
2015-10-11 08:37:24 +00:00
2015-10-14 11:51:37 +00:00
QRect aboutChannel(st::boxPadding.left() + st::newGroupPadding.left() + st::defaultRadiobutton.textPosition.x(), _channel.y() + _channel.height() + st::lineWidth, _aboutGroupWidth, _aboutGroupHeight);
_aboutChannel.drawLeft(p, aboutChannel.x(), aboutChannel.y(), aboutChannel.width(), width());
2015-10-11 08:37:24 +00:00
}
void NewGroupBox::resizeEvent(QResizeEvent *e) {
_group.moveToLeft(st::boxPadding.left() + st::newGroupPadding.left(), st::boxPadding.top() + st::newGroupPadding.top());
_channel.moveToLeft(st::boxPadding.left() + st::newGroupPadding.left(), _group.y() + _group.height() + _aboutGroupHeight + st::newGroupSkip);
_next.moveToRight(st::boxButtonPadding.right(), height() - st::boxButtonPadding.bottom() - _next.height());
_cancel.moveToRight(st::boxButtonPadding.right() + _next.width() + st::boxButtonPadding.left(), _next.y());
}
void NewGroupBox::onNext() {
2015-12-07 18:09:05 +00:00
Ui::showLayer(new GroupInfoBox(_group.checked() ? CreatingGroupGroup : CreatingGroupChannel, true), KeepOtherLayers);
2015-10-11 08:37:24 +00:00
}
GroupInfoBox::GroupInfoBox(CreatingGroupType creating, bool fromTypeChoose) : AbstractBox(),
_creating(creating),
a_photoOver(0, 0),
2015-12-08 12:33:37 +00:00
_a_photoOver(animation(this, &GroupInfoBox::step_photoOver)),
2015-10-11 08:37:24 +00:00
_photoOver(false),
2015-10-12 21:02:10 +00:00
_title(this, st::defaultInputField, lang(_creating == CreatingGroupChannel ? lng_dlg_new_channel_name : lng_dlg_new_group_name)),
2015-10-11 08:37:24 +00:00
_description(this, st::newGroupDescription, lang(lng_create_group_description)),
_next(this, lang(_creating == CreatingGroupChannel ? lng_create_group_create : lng_create_group_next), st::defaultBoxButton),
_cancel(this, lang(fromTypeChoose ? lng_create_group_back : lng_cancel), st::cancelBoxButton),
_creationRequestId(0), _createdChannel(0) {
setMouseTracking(true);
_title.setMaxLength(MaxGroupChannelTitle);
_description.setMaxLength(MaxChannelDescription);
_description.resize(width() - st::boxPadding.left() - st::newGroupInfoPadding.left() - st::boxPadding.right(), _description.height());
updateMaxHeight();
connect(&_description, SIGNAL(resized()), this, SLOT(onDescriptionResized()));
connect(&_description, SIGNAL(submitted(bool)), this, SLOT(onNext()));
connect(&_description, SIGNAL(cancelled()), this, SLOT(onClose()));
connect(&_title, SIGNAL(submitted(bool)), this, SLOT(onNameSubmit()));
connect(&_next, SIGNAL(clicked()), this, SLOT(onNext()));
connect(&_cancel, SIGNAL(clicked()), this, SLOT(onClose()));
prepare();
}
void GroupInfoBox::hideAll() {
_title.hide();
_description.hide();
_cancel.hide();
_next.hide();
}
void GroupInfoBox::showAll() {
_title.show();
if (_creating == CreatingGroupChannel) {
_description.show();
} else {
_description.hide();
}
_cancel.show();
_next.show();
}
void GroupInfoBox::showDone() {
_title.setFocus();
}
void GroupInfoBox::paintEvent(QPaintEvent *e) {
Painter p(this);
if (paint(p)) return;
QRect phRect(photoRect());
if (phRect.intersects(e->rect())) {
if (_photoSmall.isNull()) {
float64 o = a_photoOver.current();
if (o > 0) {
if (o < 1) {
QColor c;
c.setRedF(st::newGroupPhotoBg->c.redF() * (1. - o) + st::newGroupPhotoBgOver->c.redF() * o);
c.setGreenF(st::newGroupPhotoBg->c.greenF() * (1. - o) + st::newGroupPhotoBgOver->c.greenF() * o);
c.setBlueF(st::newGroupPhotoBg->c.blueF() * (1. - o) + st::newGroupPhotoBgOver->c.blueF() * o);
p.fillRect(phRect, c);
} else {
p.fillRect(phRect, st::newGroupPhotoBgOver->b);
}
} else {
p.fillRect(phRect, st::newGroupPhotoBg->b);
}
p.drawSprite(phRect.topLeft() + st::newGroupPhotoIconPosition, st::newGroupPhotoIcon);
} else {
p.drawPixmap(phRect.topLeft(), _photoSmall);
}
if (phRect.contains(e->rect())) {
return;
}
}
}
void GroupInfoBox::resizeEvent(QResizeEvent *e) {
int32 nameLeft = st::newGroupPhotoSize + st::newGroupNamePosition.x();
_title.resize(width() - st::boxPadding.left() - st::newGroupInfoPadding.left() - st::boxPadding.right() - nameLeft, _title.height());
_title.moveToLeft(st::boxPadding.left() + st::newGroupInfoPadding.left() + nameLeft, st::boxPadding.top() + st::newGroupInfoPadding.top() + st::newGroupNamePosition.y());
_description.moveToLeft(st::boxPadding.left() + st::newGroupInfoPadding.left(), st::boxPadding.top() + st::newGroupInfoPadding.top() + st::newGroupPhotoSize + st::newGroupDescriptionPadding.top());
_next.moveToRight(st::boxButtonPadding.right(), height() - st::boxButtonPadding.bottom() - _next.height());
_cancel.moveToRight(st::boxButtonPadding.right() + _next.width() + st::boxButtonPadding.left(), _next.y());
}
void GroupInfoBox::mouseMoveEvent(QMouseEvent *e) {
updateSelected(e->globalPos());
}
void GroupInfoBox::updateSelected(const QPoint &cursorGlobalPosition) {
QPoint p(mapFromGlobal(cursorGlobalPosition));
bool photoOver = photoRect().contains(p);
if (photoOver != _photoOver) {
_photoOver = photoOver;
if (_photoSmall.isNull()) {
a_photoOver.start(_photoOver ? 1 : 0);
_a_photoOver.start();
}
}
setCursor(_photoOver ? style::cur_pointer : style::cur_default);
}
void GroupInfoBox::mousePressEvent(QMouseEvent *e) {
mouseMoveEvent(e);
if (_photoOver) {
onPhoto();
}
}
void GroupInfoBox::leaveEvent(QEvent *e) {
updateSelected(QCursor::pos());
}
2015-12-08 12:33:37 +00:00
void GroupInfoBox::step_photoOver(float64 ms, bool timer) {
2015-10-11 08:37:24 +00:00
float64 dt = ms / st::setPhotoDuration;
if (dt >= 1) {
2015-12-08 12:33:37 +00:00
_a_photoOver.stop();
2015-10-11 08:37:24 +00:00
a_photoOver.finish();
} else {
a_photoOver.update(dt, anim::linear);
}
2015-12-08 12:33:37 +00:00
if (timer) update(photoRect());
2015-10-11 08:37:24 +00:00
}
void GroupInfoBox::onNameSubmit() {
if (_title.getLastText().trimmed().isEmpty()) {
_title.setFocus();
_title.showError();
} else if (_description.isHidden()) {
onNext();
} else {
_description.setFocus();
}
}
void GroupInfoBox::onNext() {
if (_creationRequestId) return;
QString title = prepareText(_title.getLastText()), description = prepareText(_description.getLastText(), true);
2015-10-11 08:37:24 +00:00
if (title.isEmpty()) {
_title.setFocus();
_title.showError();
return;
}
if (_creating == CreatingGroupGroup) {
2015-12-07 18:09:05 +00:00
Ui::showLayer(new ContactsBox(title, _photoBig), KeepOtherLayers);
2015-10-11 08:37:24 +00:00
} else {
2015-11-06 17:48:49 +00:00
bool mega = false;
2015-11-02 22:33:57 +00:00
int32 flags = mega ? MTPchannels_CreateChannel::flag_megagroup : MTPchannels_CreateChannel::flag_broadcast;
_creationRequestId = MTP::send(MTPchannels_CreateChannel(MTP_int(flags), MTP_string(title), MTP_string(description)), rpcDone(&GroupInfoBox::creationDone), rpcFail(&GroupInfoBox::creationFail));
2015-10-11 08:37:24 +00:00
}
}
void GroupInfoBox::creationDone(const MTPUpdates &updates) {
App::main()->sentUpdatesReceived(updates);
const QVector<MTPChat> *v = 0;
switch (updates.type()) {
case mtpc_updates: v = &updates.c_updates().vchats.c_vector().v; break;
case mtpc_updatesCombined: v = &updates.c_updatesCombined().vchats.c_vector().v; break;
default: LOG(("API Error: unexpected update cons %1 (GroupInfoBox::creationDone)").arg(updates.type())); break;
}
ChannelData *channel = 0;
if (v && !v->isEmpty() && v->front().type() == mtpc_channel) {
channel = App::channel(v->front().c_channel().vid.v);
if (channel) {
if (!_photoBig.isNull()) {
App::app()->uploadProfilePhoto(_photoBig, channel->id);
}
_createdChannel = channel;
_creationRequestId = MTP::send(MTPchannels_ExportInvite(_createdChannel->inputChannel), rpcDone(&GroupInfoBox::exportDone));
return;
}
} else {
LOG(("API Error: channel not found in updates (GroupInfoBox::creationDone)"));
}
onClose();
}
bool GroupInfoBox::creationFail(const RPCError &error) {
if (mtpIsFlood(error)) return false;
_creationRequestId = 0;
if (error.type() == "NO_CHAT_TITLE") {
_title.setFocus();
_title.showError();
return true;
}
return false;
}
void GroupInfoBox::exportDone(const MTPExportedChatInvite &result) {
_creationRequestId = 0;
if (result.type() == mtpc_chatInviteExported) {
_createdChannel->invitationUrl = qs(result.c_chatInviteExported().vlink);
}
2015-12-07 18:09:05 +00:00
Ui::showLayer(new SetupChannelBox(_createdChannel));
2015-10-11 08:37:24 +00:00
}
void GroupInfoBox::onDescriptionResized() {
updateMaxHeight();
update();
}
2015-09-21 20:57:42 +00:00
2015-10-11 08:37:24 +00:00
QRect GroupInfoBox::photoRect() const {
return myrtlrect(st::boxPadding.left() + st::newGroupInfoPadding.left(), st::boxPadding.top() + st::newGroupInfoPadding.top(), st::newGroupPhotoSize, st::newGroupPhotoSize);
}
void GroupInfoBox::updateMaxHeight() {
int32 h = st::boxPadding.top() + st::newGroupInfoPadding.top() + st::newGroupPhotoSize + st::boxPadding.bottom() + st::newGroupInfoPadding.bottom() + st::boxButtonPadding.top() + _next.height() + st::boxButtonPadding.bottom();
if (_creating == CreatingGroupChannel) {
h += st::newGroupDescriptionPadding.top() + _description.height() + st::newGroupDescriptionPadding.bottom();
}
setMaxHeight(h);
}
void GroupInfoBox::onPhoto() {
QStringList imgExtensions(cImgExtensions());
QString filter(qsl("Image files (*") + imgExtensions.join(qsl(" *")) + qsl(");;All files (*.*)"));
QImage img;
QString file;
QByteArray remoteContent;
if (filedialogGetOpenFile(file, remoteContent, lang(lng_choose_images), filter)) {
if (!remoteContent.isEmpty()) {
img = App::readImage(remoteContent);
} else {
if (!file.isEmpty()) {
img = App::readImage(file);
}
}
} else {
return;
}
if (img.isNull() || img.width() > 10 * img.height() || img.height() > 10 * img.width()) {
return;
}
2015-11-09 09:51:22 +00:00
PhotoCropBox *box = new PhotoCropBox(img, (_creating == CreatingGroupChannel) ? peerFromChannel(0) : peerFromChat(0));
2015-10-11 08:37:24 +00:00
connect(box, SIGNAL(ready(const QImage&)), this, SLOT(onPhotoReady(const QImage&)));
2015-12-07 18:09:05 +00:00
Ui::showLayer(box, KeepOtherLayers);
2015-10-11 08:37:24 +00:00
}
void GroupInfoBox::onPhotoReady(const QImage &img) {
_photoBig = img;
_photoSmall = QPixmap::fromImage(img.scaled(st::newGroupPhotoSize * cIntRetinaFactor(), st::newGroupPhotoSize * cIntRetinaFactor(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation), Qt::ColorOnly);
_photoSmall.setDevicePixelRatio(cRetinaFactor());
}
2015-12-08 12:33:37 +00:00
SetupChannelBox::SetupChannelBox(ChannelData *channel, bool existing) : AbstractBox()
, _channel(channel)
, _existing(existing)
, _public(this, qsl("channel_privacy"), 0, lang(channel->isMegagroup() ? lng_create_public_group_title : lng_create_public_channel_title), true)
, _private(this, qsl("channel_privacy"), 1, lang(channel->isMegagroup() ? lng_create_private_group_title : lng_create_private_channel_title))
2015-12-08 12:33:37 +00:00
, _comments(this, lang(lng_create_channel_comments), false)
, _aboutPublicWidth(width() - st::boxPadding.left() - st::boxButtonPadding.right() - st::newGroupPadding.left() - st::defaultRadiobutton.textPosition.x())
, _aboutPublic(st::normalFont, lang(channel->isMegagroup() ? lng_create_public_group_about : lng_create_public_channel_about), _defaultOptions, _aboutPublicWidth)
, _aboutPrivate(st::normalFont, lang(channel->isMegagroup() ? lng_create_private_group_about : lng_create_private_channel_about), _defaultOptions, _aboutPublicWidth)
2015-12-08 12:33:37 +00:00
, _aboutComments(st::normalFont, lang(lng_create_channel_comments_about), _defaultOptions, _aboutPublicWidth)
, _link(this, st::defaultInputField, QString(), channel->username, true)
, _linkOver(false)
, _save(this, lang(lng_settings_save), st::defaultBoxButton)
, _skip(this, lang(existing ? lng_cancel : lng_create_group_skip), st::cancelBoxButton)
, _tooMuchUsernames(false)
, _saveRequestId(0)
, _checkRequestId(0)
, a_goodOpacity(0, 0)
, _a_goodFade(animation(this, &SetupChannelBox::step_goodFade)) {
2015-10-11 08:37:24 +00:00
setMouseTracking(true);
_checkRequestId = MTP::send(MTPchannels_CheckUsername(_channel->inputChannel, MTP_string("preston")), RPCDoneHandlerPtr(), rpcFail(&SetupChannelBox::onFirstCheckFail));
_aboutPublicHeight = _aboutPublic.countHeight(_aboutPublicWidth);
updateMaxHeight();
2015-10-11 08:37:24 +00:00
connect(&_save, SIGNAL(clicked()), this, SLOT(onSave()));
connect(&_skip, SIGNAL(clicked()), this, SLOT(onClose()));
_comments.hide();
connect(&_link, SIGNAL(changed()), this, SLOT(onChange()));
_checkTimer.setSingleShot(true);
connect(&_checkTimer, SIGNAL(timeout()), this, SLOT(onCheck()));
connect(&_public, SIGNAL(changed()), this, SLOT(onPrivacyChange()));
connect(&_private, SIGNAL(changed()), this, SLOT(onPrivacyChange()));
prepare();
}
void SetupChannelBox::hideAll() {
_public.hide();
_private.hide();
_comments.hide();
_link.hide();
_save.hide();
_skip.hide();
}
void SetupChannelBox::showAll() {
_public.show();
_private.show();
//_comments.show();
if (_public.checked()) {
_link.show();
} else {
_link.hide();
}
_save.show();
_skip.show();
}
void SetupChannelBox::showDone() {
_link.setFocus();
}
void SetupChannelBox::updateMaxHeight() {
if (!_channel->isMegagroup() || _public.checked()) {
setMaxHeight(st::boxPadding.top() + st::newGroupPadding.top() + _public.height() + _aboutPublicHeight + st::newGroupSkip + _private.height() + _aboutPrivate.countHeight(_aboutPublicWidth)/* + st::newGroupSkip + _comments.height() + _aboutComments.countHeight(_aboutPublicWidth)*/ + st::newGroupSkip + st::newGroupPadding.bottom() + st::newGroupLinkPadding.top() + _link.height() + st::newGroupLinkPadding.bottom() + st::boxButtonPadding.top() + _save.height() + st::boxButtonPadding.bottom());
} else {
setMaxHeight(st::boxPadding.top() + st::newGroupPadding.top() + _public.height() + _aboutPublicHeight + st::newGroupSkip + _private.height() + _aboutPrivate.countHeight(_aboutPublicWidth)/* + st::newGroupSkip + _comments.height() + _aboutComments.countHeight(_aboutPublicWidth)*/ + st::newGroupSkip + st::newGroupPadding.bottom() + st::boxButtonPadding.top() + _save.height() + st::boxButtonPadding.bottom());
}
}
2015-10-11 08:37:24 +00:00
void SetupChannelBox::keyPressEvent(QKeyEvent *e) {
if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
if (_link.hasFocus()) {
if (_link.text().trimmed().isEmpty()) {
_link.setFocus();
_link.showError();
} else {
onSave();
}
}
} else {
AbstractBox::keyPressEvent(e);
}
}
void SetupChannelBox::paintEvent(QPaintEvent *e) {
Painter p(this);
if (paint(p)) return;
p.setPen(st::newGroupAboutFg);
2015-10-14 11:51:37 +00:00
QRect aboutPublic(st::boxPadding.left() + st::newGroupPadding.left() + st::defaultRadiobutton.textPosition.x(), _public.y() + _public.height(), _aboutPublicWidth, _aboutPublicHeight);
_aboutPublic.drawLeft(p, aboutPublic.x(), aboutPublic.y(), aboutPublic.width(), width());
2015-10-11 08:37:24 +00:00
2015-10-14 11:51:37 +00:00
QRect aboutPrivate(st::boxPadding.left() + st::newGroupPadding.left() + st::defaultRadiobutton.textPosition.x(), _private.y() + _private.height(), _aboutPublicWidth, _aboutPublicHeight);
_aboutPrivate.drawLeft(p, aboutPrivate.x(), aboutPrivate.y(), aboutPrivate.width(), width());
2015-10-11 08:37:24 +00:00
2015-10-14 11:51:37 +00:00
//QRect aboutComments(st::boxPadding.left() + st::newGroupPadding.left() + st::defaultRadiobutton.textPosition.x(), _comments.y() + _comments.height(), _aboutPublicWidth, _aboutPublicHeight);
//_aboutComments.drawLeft(p, aboutComments.x(), aboutComments.y(), aboutComments.width(), width());
2015-10-11 08:37:24 +00:00
if (!_channel->isMegagroup() || !_link.isHidden()) {
p.setPen(st::black);
p.setFont(st::newGroupLinkFont);
p.drawTextLeft(st::boxPadding.left() + st::newGroupPadding.left() + st::defaultInputField.textMargins.left(), _link.y() - st::newGroupLinkPadding.top() + st::newGroupLinkTop, width(), lang(_link.isHidden() ? lng_create_group_invite_link : lng_create_group_link));
}
2015-10-11 08:37:24 +00:00
if (_link.isHidden()) {
if (!_channel->isMegagroup()) {
QTextOption option(style::al_left);
option.setWrapMode(QTextOption::WrapAnywhere);
p.setFont(_linkOver ? st::boxTextFont->underline() : st::boxTextFont);
p.setPen(st::btnDefLink.color);
p.drawText(_invitationLink, _channel->invitationUrl, option);
if (!_goodTextLink.isEmpty() && a_goodOpacity.current() > 0) {
p.setOpacity(a_goodOpacity.current());
p.setPen(st::setGoodColor);
p.setFont(st::boxTextFont);
p.drawTextRight(st::boxPadding.right(), _link.y() - st::newGroupLinkPadding.top() + st::newGroupLinkTop + st::newGroupLinkFont->ascent - st::boxTextFont->ascent, width(), _goodTextLink);
p.setOpacity(1);
}
2015-10-11 08:37:24 +00:00
}
} else {
if (!_errorText.isEmpty()) {
p.setPen(st::setErrColor);
p.setFont(st::boxTextFont);
p.drawTextRight(st::boxPadding.right(), _link.y() - st::newGroupLinkPadding.top() + st::newGroupLinkTop + st::newGroupLinkFont->ascent - st::boxTextFont->ascent, width(), _errorText);
} else if (!_goodText.isEmpty()) {
p.setPen(st::setGoodColor);
p.setFont(st::boxTextFont);
p.drawTextRight(st::boxPadding.right(), _link.y() - st::newGroupLinkPadding.top() + st::newGroupLinkTop + st::newGroupLinkFont->ascent - st::boxTextFont->ascent, width(), _goodText);
}
}
}
void SetupChannelBox::resizeEvent(QResizeEvent *e) {
_public.moveToLeft(st::boxPadding.left() + st::newGroupPadding.left(), st::boxPadding.top() + st::newGroupPadding.top());
_private.moveToLeft(st::boxPadding.left() + st::newGroupPadding.left(), _public.y() + _public.height() + _aboutPublicHeight + st::newGroupSkip);
//_comments.moveToLeft(st::boxPadding.left() + st::newGroupPadding.left(), _private.y() + _private.height() + _aboutPrivate.countHeight(_aboutPublicWidth) + st::newGroupSkip);
_link.resize(width() - st::boxPadding.left() - st::newGroupLinkPadding.left() - st::boxPadding.right(), _link.height());
//_link.moveToLeft(st::boxPadding.left() + st::newGroupLinkPadding.left(), _comments.y() + _comments.height() + _aboutComments.countHeight(_aboutPublicWidth) + st::newGroupSkip + st::newGroupPadding.bottom() + st::newGroupLinkPadding.top());
_link.moveToLeft(st::boxPadding.left() + st::newGroupLinkPadding.left(), _private.y() + _private.height() + _aboutPrivate.countHeight(_aboutPublicWidth) + st::newGroupSkip + st::newGroupPadding.bottom() + st::newGroupLinkPadding.top());
_invitationLink = QRect(_link.x(), _link.y() + (_link.height() / 2) - st::boxTextFont->height, _link.width(), 2 * st::boxTextFont->height);
_save.moveToRight(st::boxButtonPadding.right(), height() - st::boxButtonPadding.bottom() - _save.height());
_skip.moveToRight(st::boxButtonPadding.right() + _save.width() + st::boxButtonPadding.left(), _save.y());
}
void SetupChannelBox::mouseMoveEvent(QMouseEvent *e) {
updateSelected(e->globalPos());
}
void SetupChannelBox::mousePressEvent(QMouseEvent *e) {
mouseMoveEvent(e);
if (_linkOver) {
2016-01-30 18:24:18 +00:00
Application::clipboard()->setText(_channel->invitationUrl);
2015-10-11 08:37:24 +00:00
_goodTextLink = lang(lng_create_channel_link_copied);
a_goodOpacity = anim::fvalue(1, 0);
_a_goodFade.start();
}
}
void SetupChannelBox::leaveEvent(QEvent *e) {
updateSelected(QCursor::pos());
}
void SetupChannelBox::updateSelected(const QPoint &cursorGlobalPosition) {
QPoint p(mapFromGlobal(cursorGlobalPosition));
bool linkOver = _invitationLink.contains(p);
if (linkOver != _linkOver) {
_linkOver = linkOver;
update();
setCursor(_linkOver ? style::cur_pointer : style::cur_default);
}
}
2015-12-08 12:33:37 +00:00
void SetupChannelBox::step_goodFade(float64 ms, bool timer) {
2015-10-11 08:37:24 +00:00
float dt = ms / st::newGroupLinkFadeDuration;
if (dt >= 1) {
2015-12-08 12:33:37 +00:00
_a_goodFade.stop();
2015-10-11 08:37:24 +00:00
a_goodOpacity.finish();
} else {
a_goodOpacity.update(dt, anim::linear);
}
2015-12-08 12:33:37 +00:00
if (timer) update();
2015-10-11 08:37:24 +00:00
}
void SetupChannelBox::closePressed() {
if (!_existing) {
2015-12-07 18:09:05 +00:00
Ui::showLayer(new ContactsBox(_channel));
2015-10-11 08:37:24 +00:00
}
}
void SetupChannelBox::onSave() {
if (!_public.checked()) {
if (!_existing && !_comments.isHidden() && _comments.checked()) {
MTP::send(MTPchannels_ToggleComments(_channel->inputChannel, MTP_bool(true)));
}
if (_existing) {
_sentUsername = QString();
_saveRequestId = MTP::send(MTPchannels_UpdateUsername(_channel->inputChannel, MTP_string(_sentUsername)), rpcDone(&SetupChannelBox::onUpdateDone), rpcFail(&SetupChannelBox::onUpdateFail));
} else {
onClose();
}
}
if (_saveRequestId) return;
QString link = _link.text().trimmed();
if (link.isEmpty()) {
_link.setFocus();
_link.showError();
return;
}
if (!_existing && !_comments.isHidden() && _comments.checked()) {
MTP::send(MTPchannels_ToggleComments(_channel->inputChannel, MTP_bool(true)), RPCResponseHandler(), 0, 5);
}
_sentUsername = link;
_saveRequestId = MTP::send(MTPchannels_UpdateUsername(_channel->inputChannel, MTP_string(_sentUsername)), rpcDone(&SetupChannelBox::onUpdateDone), rpcFail(&SetupChannelBox::onUpdateFail));
}
void SetupChannelBox::onChange() {
QString name = _link.text().trimmed();
if (name.isEmpty()) {
if (!_errorText.isEmpty() || !_goodText.isEmpty()) {
_errorText = _goodText = QString();
update();
}
_checkTimer.stop();
} else {
int32 i, len = name.size();
for (int32 i = 0; i < len; ++i) {
QChar ch = name.at(i);
if ((ch < 'A' || ch > 'Z') && (ch < 'a' || ch > 'z') && (ch < '0' || ch > '9') && ch != '_') {
if (_errorText != lang(lng_create_channel_link_bad_symbols)) {
_errorText = lang(lng_create_channel_link_bad_symbols);
update();
}
_checkTimer.stop();
return;
}
}
if (name.size() < MinUsernameLength) {
if (_errorText != lang(lng_create_channel_link_too_short)) {
_errorText = lang(lng_create_channel_link_too_short);
update();
}
_checkTimer.stop();
} else {
if (!_errorText.isEmpty() || !_goodText.isEmpty()) {
_errorText = _goodText = QString();
update();
}
_checkTimer.start(UsernameCheckTimeout);
}
}
}
void SetupChannelBox::onCheck() {
if (_checkRequestId) {
MTP::cancel(_checkRequestId);
}
QString link = _link.text().trimmed();
if (link.size() >= MinUsernameLength) {
_checkUsername = link;
_checkRequestId = MTP::send(MTPchannels_CheckUsername(_channel->inputChannel, MTP_string(link)), rpcDone(&SetupChannelBox::onCheckDone), rpcFail(&SetupChannelBox::onCheckFail));
}
}
void SetupChannelBox::onPrivacyChange() {
if (_public.checked()) {
if (_tooMuchUsernames) {
_private.setChecked(true);
2015-12-07 18:09:05 +00:00
Ui::showLayer(new InformBox(lang(lng_channels_too_much_public)), KeepOtherLayers);
2015-10-11 08:37:24 +00:00
return;
}
_link.show();
_link.setFocus();
} else {
_link.hide();
setFocus();
}
if (_channel->isMegagroup()) {
updateMaxHeight();
}
2015-10-11 08:37:24 +00:00
update();
}
void SetupChannelBox::onUpdateDone(const MTPBool &result) {
_channel->setName(textOneLine(_channel->name), _sentUsername);
onClose();
}
bool SetupChannelBox::onUpdateFail(const RPCError &error) {
if (mtpIsFlood(error)) return false;
_saveRequestId = 0;
QString err(error.type());
if (err == "USERNAME_NOT_MODIFIED" || _sentUsername == _channel->username) {
_channel->setName(textOneLine(_channel->name), textOneLine(_sentUsername));
onClose();
return true;
} else if (err == "USERNAME_INVALID") {
_link.setFocus();
_link.showError();
_errorText = lang(lng_create_channel_link_invalid);
update();
return true;
} else if (err == "USERNAME_OCCUPIED" || err == "USERNAMES_UNAVAILABLE") {
_link.setFocus();
_link.showError();
_errorText = lang(lng_create_channel_link_occupied);
update();
return true;
}
_link.setFocus();
return true;
}
void SetupChannelBox::onCheckDone(const MTPBool &result) {
_checkRequestId = 0;
2015-10-29 00:16:52 +00:00
QString newError = (mtpIsTrue(result) || _checkUsername == _channel->username) ? QString() : lang(lng_create_channel_link_occupied);
2015-10-11 08:37:24 +00:00
QString newGood = newError.isEmpty() ? lang(lng_create_channel_link_available) : QString();
if (_errorText != newError || _goodText != newGood) {
_errorText = newError;
_goodText = newGood;
update();
}
}
bool SetupChannelBox::onCheckFail(const RPCError &error) {
if (mtpIsFlood(error)) return false;
_checkRequestId = 0;
QString err(error.type());
2016-03-11 15:21:05 +00:00
if (err == qstr("CHANNEL_PUBLIC_GROUP_NA")) {
Ui::hideLayer();
return true;
} else if (err == qstr("CHANNELS_ADMIN_PUBLIC_TOO_MUCH")) {
2015-10-11 08:37:24 +00:00
if (_existing) {
2015-12-07 18:09:05 +00:00
Ui::showLayer(new InformBox(lang(lng_channels_too_much_public_existing)));
2015-10-11 08:37:24 +00:00
} else {
_tooMuchUsernames = true;
_private.setChecked(true);
onPrivacyChange();
}
return true;
2016-03-11 15:21:05 +00:00
} else if (err == qstr("USERNAME_INVALID")) {
2015-10-11 08:37:24 +00:00
_errorText = lang(lng_create_channel_link_invalid);
update();
return true;
2016-03-11 15:21:05 +00:00
} else if (err == qstr("USERNAME_OCCUPIED") && _checkUsername != _channel->username) {
2015-10-11 08:37:24 +00:00
_errorText = lang(lng_create_channel_link_occupied);
update();
return true;
}
_goodText = QString();
_link.setFocus();
return true;
}
bool SetupChannelBox::onFirstCheckFail(const RPCError &error) {
if (mtpIsFlood(error)) return false;
_checkRequestId = 0;
QString err(error.type());
2016-03-11 15:21:05 +00:00
if (err == qstr("CHANNEL_PUBLIC_GROUP_NA")) {
Ui::hideLayer();
return true;
} else if (err == qstr("CHANNELS_ADMIN_PUBLIC_TOO_MUCH")) {
2015-10-11 08:37:24 +00:00
if (_existing) {
2015-12-07 18:09:05 +00:00
Ui::showLayer(new InformBox(lang(lng_channels_too_much_public_existing)));
2015-10-11 08:37:24 +00:00
} else {
_tooMuchUsernames = true;
_private.setChecked(true);
onPrivacyChange();
}
return true;
}
_goodText = QString();
_link.setFocus();
return true;
}
2015-10-06 19:49:23 +00:00
EditNameTitleBox::EditNameTitleBox(PeerData *peer) :
_peer(peer),
_save(this, lang(lng_settings_save), st::defaultBoxButton),
2015-10-11 08:37:24 +00:00
_cancel(this, lang(lng_cancel), st::cancelBoxButton),
2015-10-06 19:49:23 +00:00
_first(this, st::defaultInputField, lang(peer->isUser() ? lng_signup_firstname : lng_dlg_new_group_name), peer->isUser() ? peer->asUser()->firstName : peer->name),
_last(this, st::defaultInputField, lang(lng_signup_lastname), peer->isUser() ? peer->asUser()->lastName : QString()),
_invertOrder(!peer->isChat() && langFirstNameGoesSecond()),
_requestId(0) {
if (_invertOrder) {
setTabOrder(&_last, &_first);
}
2015-10-11 08:37:24 +00:00
_first.setMaxLength(MaxGroupChannelTitle);
_last.setMaxLength(MaxGroupChannelTitle);
int32 h = st::boxTitleHeight + st::contactPadding.top() + _first.height();
2015-10-06 19:49:23 +00:00
if (_peer->isUser()) {
_boxTitle = lang(_peer == App::self() ? lng_edit_self_title : lng_edit_contact_title);
2015-10-11 08:37:24 +00:00
h += st::contactSkip + _last.height();
2015-10-06 19:49:23 +00:00
} else if (_peer->isChat()) {
_boxTitle = lang(lng_edit_group_title);
}
2015-10-11 08:37:24 +00:00
h += st::boxPadding.bottom() + st::contactPadding.bottom() + st::boxButtonPadding.top() + _save.height() + st::boxButtonPadding.bottom();
2015-10-06 19:49:23 +00:00
setMaxHeight(h);
connect(&_save, SIGNAL(clicked()), this, SLOT(onSave()));
connect(&_cancel, SIGNAL(clicked()), this, SLOT(onClose()));
connect(&_first, SIGNAL(submitted(bool)), this, SLOT(onSubmit()));
connect(&_last, SIGNAL(submitted(bool)), this, SLOT(onSubmit()));
prepare();
}
void EditNameTitleBox::hideAll() {
_first.hide();
_last.hide();
_save.hide();
_cancel.hide();
}
void EditNameTitleBox::showAll() {
_first.show();
if (_peer->isChat()) {
_last.hide();
} else {
_last.show();
}
_save.show();
_cancel.show();
}
void EditNameTitleBox::showDone() {
(_invertOrder ? _last : _first).setFocus();
}
void EditNameTitleBox::onSubmit() {
if (_first.hasFocus()) {
if (_peer->isChat()) {
if (_first.getLastText().trimmed().isEmpty()) {
_first.setFocus();
_first.showError();
} else {
onSave();
}
} else {
_last.setFocus();
}
} else if (_last.hasFocus()) {
if (_first.getLastText().trimmed().isEmpty()) {
_first.setFocus();
_first.showError();
} else if (_last.getLastText().trimmed().isEmpty()) {
_last.setFocus();
_last.showError();
} else {
onSave();
}
}
}
void EditNameTitleBox::paintEvent(QPaintEvent *e) {
Painter p(this);
if (paint(p)) return;
paintTitle(p, _boxTitle);
}
void EditNameTitleBox::resizeEvent(QResizeEvent *e) {
2015-10-11 08:37:24 +00:00
_first.resize(width() - st::boxPadding.left() - st::newGroupInfoPadding.left() - st::boxPadding.right(), _first.height());
2015-10-06 19:49:23 +00:00
_last.resize(_first.size());
if (_invertOrder) {
2015-10-11 08:37:24 +00:00
_last.moveToLeft(st::boxPadding.left() + st::newGroupInfoPadding.left(), st::boxTitleHeight + st::contactPadding.top());
_first.moveToLeft(st::boxPadding.left() + st::newGroupInfoPadding.left(), _last.y() + _last.height() + st::contactSkip);
2015-10-06 19:49:23 +00:00
} else {
2015-10-11 08:37:24 +00:00
_first.moveToLeft(st::boxPadding.left() + st::newGroupInfoPadding.left(), st::boxTitleHeight + st::contactPadding.top());
_last.moveToLeft(st::boxPadding.left() + st::newGroupInfoPadding.left(), _first.y() + _first.height() + st::contactSkip);
2015-10-06 19:49:23 +00:00
}
_save.moveToRight(st::boxButtonPadding.right(), height() - st::boxButtonPadding.bottom() - _save.height());
_cancel.moveToRight(st::boxButtonPadding.right() + _save.width() + st::boxButtonPadding.left(), _save.y());
}
void EditNameTitleBox::onSave() {
if (_requestId) return;
QString first = prepareText(_first.getLastText()), last = prepareText(_last.getLastText());
2015-10-06 19:49:23 +00:00
if (first.isEmpty() && last.isEmpty()) {
if (_invertOrder) {
_last.setFocus();
_last.showError();
} else {
_first.setFocus();
_first.showError();
}
return;
}
if (first.isEmpty()) {
first = last;
last = QString();
}
_sentName = first;
if (_peer == App::self()) {
int32 flags = MTPaccount_UpdateProfile::flag_first_name | MTPaccount_UpdateProfile::flag_last_name;
_requestId = MTP::send(MTPaccount_UpdateProfile(MTP_int(flags), MTP_string(first), MTP_string(last), MTPstring()), rpcDone(&EditNameTitleBox::onSaveSelfDone), rpcFail(&EditNameTitleBox::onSaveSelfFail));
2015-10-06 19:49:23 +00:00
} else if (_peer->isChat()) {
_requestId = MTP::send(MTPmessages_EditChatTitle(_peer->asChat()->inputChat, MTP_string(first)), rpcDone(&EditNameTitleBox::onSaveChatDone), rpcFail(&EditNameTitleBox::onSaveChatFail));
}
}
void EditNameTitleBox::onSaveSelfDone(const MTPUser &user) {
App::feedUsers(MTP_vector<MTPUser>(1, user));
emit closed();
}
bool EditNameTitleBox::onSaveSelfFail(const RPCError &error) {
if (mtpIsFlood(error)) return false;
QString err(error.type());
QString first = textOneLine(_first.getLastText().trimmed()), last = textOneLine(_last.getLastText().trimmed());
if (err == "NAME_NOT_MODIFIED") {
App::self()->setName(first, last, QString(), textOneLine(App::self()->username));
emit closed();
return true;
} else if (err == "FIRSTNAME_INVALID") {
_first.setFocus();
_first.showError();
return true;
} else if (err == "LASTNAME_INVALID") {
_last.setFocus();
_last.showError();
return true;
}
_first.setFocus();
return true;
}
bool EditNameTitleBox::onSaveChatFail(const RPCError &error) {
if (mtpIsFlood(error)) return false;
_requestId = 0;
QString err(error.type());
if (err == qstr("CHAT_TITLE_NOT_MODIFIED") || err == qstr("CHAT_NOT_MODIFIED")) {
_peer->updateName(_sentName, QString(), QString());
emit closed();
return true;
} else if (err == qstr("NO_CHAT_TITLE")) {
_first.setFocus();
_first.showError();
return true;
}
_first.setFocus();
return true;
}
void EditNameTitleBox::onSaveChatDone(const MTPUpdates &updates) {
App::main()->sentUpdatesReceived(updates);
emit closed();
}
EditChannelBox::EditChannelBox(ChannelData *channel) : AbstractBox()
, _channel(channel)
, _save(this, lang(lng_settings_save), st::defaultBoxButton)
, _cancel(this, lang(lng_cancel), st::cancelBoxButton)
, _title(this, st::defaultInputField, lang(lng_dlg_new_channel_name), _channel->name)
, _description(this, st::newGroupDescription, lang(lng_create_group_description), _channel->about)
, _sign(this, lang(lng_edit_sign_messages), channel->addsSignature())
, _publicLink(this, lang(channel->isPublic() ? lng_profile_edit_public_link : lng_profile_create_public_link), st::defaultBoxLinkButton)
, _saveTitleRequestId(0)
, _saveDescriptionRequestId(0)
, _saveSignRequestId(0) {
connect(App::main(), SIGNAL(peerNameChanged(PeerData*, const PeerData::Names&, const PeerData::NameFirstChars&)), this, SLOT(peerUpdated(PeerData*)));
2015-09-21 20:57:42 +00:00
setMouseTracking(true);
2015-10-11 08:37:24 +00:00
_title.setMaxLength(MaxGroupChannelTitle);
2015-10-06 19:49:23 +00:00
_description.setMaxLength(MaxChannelDescription);
2015-10-11 08:37:24 +00:00
_description.resize(width() - st::boxPadding.left() - st::newGroupInfoPadding.left() - st::boxPadding.right(), _description.height());
myEnsureResized(&_description);
2015-09-21 20:57:42 +00:00
updateMaxHeight();
connect(&_description, SIGNAL(resized()), this, SLOT(onDescriptionResized()));
2015-10-14 11:51:37 +00:00
connect(&_description, SIGNAL(submitted(bool)), this, SLOT(onSave()));
2015-09-21 20:57:42 +00:00
connect(&_description, SIGNAL(cancelled()), this, SLOT(onClose()));
2015-10-06 19:49:23 +00:00
connect(&_save, SIGNAL(clicked()), this, SLOT(onSave()));
connect(&_cancel, SIGNAL(clicked()), this, SLOT(onClose()));
2015-09-21 20:57:42 +00:00
connect(&_publicLink, SIGNAL(clicked()), this, SLOT(onPublicLink()));
2015-09-21 20:57:42 +00:00
prepare();
}
void EditChannelBox::hideAll() {
_title.hide();
_description.hide();
_sign.hide();
2015-10-06 19:49:23 +00:00
_save.hide();
_cancel.hide();
_publicLink.hide();
2015-09-21 20:57:42 +00:00
}
void EditChannelBox::showAll() {
_title.show();
_description.show();
2015-10-06 19:49:23 +00:00
_save.show();
_cancel.show();
2016-03-11 15:21:05 +00:00
if (_channel->canEditUsername()) {
2016-03-04 22:04:15 +00:00
_publicLink.show();
} else {
_publicLink.hide();
}
if (_channel->isMegagroup()) {
_sign.hide();
} else {
_sign.show();
}
2015-09-21 20:57:42 +00:00
}
void EditChannelBox::showDone() {
_title.setFocus();
}
void EditChannelBox::keyPressEvent(QKeyEvent *e) {
if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
if (_title.hasFocus()) {
onSave();
}
} else {
AbstractBox::keyPressEvent(e);
}
}
void EditChannelBox::paintEvent(QPaintEvent *e) {
Painter p(this);
if (paint(p)) return;
2015-11-02 22:33:57 +00:00
paintTitle(p, lang(_channel->isMegagroup() ? lng_edit_group : lng_edit_channel_title));
2015-09-21 20:57:42 +00:00
}
void EditChannelBox::peerUpdated(PeerData *peer) {
if (peer == _channel) {
_publicLink.setText(lang(_channel->isPublic() ? lng_profile_edit_public_link : lng_profile_create_public_link));
_sign.setChecked(_channel->addsSignature());
}
}
2015-09-21 20:57:42 +00:00
void EditChannelBox::onDescriptionResized() {
updateMaxHeight();
update();
}
void EditChannelBox::updateMaxHeight() {
2015-10-06 19:49:23 +00:00
int32 h = st::boxTitleHeight + st::newGroupInfoPadding.top() + _title.height();
h += st::newGroupDescriptionPadding.top() + _description.height() + st::newGroupDescriptionPadding.bottom();
if (!_channel->isMegagroup()) {
h += st::newGroupPublicLinkPadding.top() + _sign.height() + st::newGroupPublicLinkPadding.bottom();
}
2016-03-11 15:21:05 +00:00
if (_channel->canEditUsername()) {
2016-03-04 22:04:15 +00:00
h += st::newGroupPublicLinkPadding.top() + _publicLink.height() + st::newGroupPublicLinkPadding.bottom();
}
2015-10-06 19:49:23 +00:00
h += st::boxPadding.bottom() + st::newGroupInfoPadding.bottom() + st::boxButtonPadding.top() + _save.height() + st::boxButtonPadding.bottom();
2015-09-21 20:57:42 +00:00
setMaxHeight(h);
}
void EditChannelBox::resizeEvent(QResizeEvent *e) {
2015-10-11 08:37:24 +00:00
_title.resize(width() - st::boxPadding.left() - st::newGroupInfoPadding.left() - st::boxPadding.right(), _title.height());
2015-10-06 19:49:23 +00:00
_title.moveToLeft(st::boxPadding.left() + st::newGroupInfoPadding.left(), st::boxTitleHeight + st::newGroupInfoPadding.top() + st::newGroupNamePosition.y());
2015-09-21 20:57:42 +00:00
2015-10-06 19:49:23 +00:00
_description.moveToLeft(st::boxPadding.left() + st::newGroupInfoPadding.left(), _title.y() + _title.height() + st::newGroupDescriptionPadding.top());
2015-09-21 20:57:42 +00:00
_sign.moveToLeft(st::boxPadding.left() + st::newGroupInfoPadding.left(), _description.y() + _description.height() + st::newGroupDescriptionPadding.bottom() + st::newGroupPublicLinkPadding.top());
if (_channel->isMegagroup()) {
_publicLink.moveToLeft(st::boxPadding.left() + st::newGroupInfoPadding.left(), _description.y() + _description.height() + st::newGroupDescriptionPadding.bottom() + st::newGroupPublicLinkPadding.top());
} else {
_publicLink.moveToLeft(st::boxPadding.left() + st::newGroupInfoPadding.left(), _sign.y() + _sign.height() + st::newGroupDescriptionPadding.bottom() + st::newGroupPublicLinkPadding.top());
}
2015-09-21 20:57:42 +00:00
2015-10-06 19:49:23 +00:00
_save.moveToRight(st::boxButtonPadding.right(), height() - st::boxButtonPadding.bottom() - _save.height());
_cancel.moveToRight(st::boxButtonPadding.right() + _save.width() + st::boxButtonPadding.left(), _save.y());
2015-09-21 20:57:42 +00:00
}
void EditChannelBox::onSave() {
if (_saveTitleRequestId || _saveDescriptionRequestId || _saveSignRequestId) return;
2015-09-21 20:57:42 +00:00
QString title = prepareText(_title.getLastText()), description = prepareText(_description.getLastText(), true);
2015-09-21 20:57:42 +00:00
if (title.isEmpty()) {
_title.setFocus();
2015-10-06 19:49:23 +00:00
_title.showError();
2015-09-21 20:57:42 +00:00
return;
}
_sentTitle = title;
_sentDescription = description;
if (_sentTitle == _channel->name) {
saveDescription();
} else {
_saveTitleRequestId = MTP::send(MTPchannels_EditTitle(_channel->inputChannel, MTP_string(_sentTitle)), rpcDone(&EditChannelBox::onSaveTitleDone), rpcFail(&EditChannelBox::onSaveFail));
}
2015-09-21 20:57:42 +00:00
}
void EditChannelBox::onPublicLink() {
2015-12-07 18:09:05 +00:00
Ui::showLayer(new SetupChannelBox(_channel, true), KeepOtherLayers);
}
2015-09-21 20:57:42 +00:00
void EditChannelBox::saveDescription() {
if (_sentDescription == _channel->about) {
saveSign();
} else {
_saveDescriptionRequestId = MTP::send(MTPchannels_EditAbout(_channel->inputChannel, MTP_string(_sentDescription)), rpcDone(&EditChannelBox::onSaveDescriptionDone), rpcFail(&EditChannelBox::onSaveFail));
}
}
void EditChannelBox::saveSign() {
if (_channel->isMegagroup() || _channel->addsSignature() == _sign.checked()) {
onClose();
} else {
_saveSignRequestId = MTP::send(MTPchannels_ToggleSignatures(_channel->inputChannel, MTP_bool(_sign.checked())), rpcDone(&EditChannelBox::onSaveSignDone), rpcFail(&EditChannelBox::onSaveFail));
}
2015-09-21 20:57:42 +00:00
}
bool EditChannelBox::onSaveFail(const RPCError &error, mtpRequestId req) {
if (mtpIsFlood(error)) return false;
QString err(error.type());
if (req == _saveTitleRequestId) {
_saveTitleRequestId = 0;
if (err == qstr("CHAT_NOT_MODIFIED") || err == qstr("CHAT_TITLE_NOT_MODIFIED")) {
_channel->setName(_sentTitle, _channel->username);
saveDescription();
return true;
} else if (err == qstr("NO_CHAT_TITLE")) {
_title.setFocus();
2015-10-06 19:49:23 +00:00
_title.showError();
2015-09-21 20:57:42 +00:00
return true;
} else {
_title.setFocus();
}
} else if (req == _saveDescriptionRequestId) {
_saveDescriptionRequestId = 0;
if (err == qstr("CHAT_ABOUT_NOT_MODIFIED")) {
_channel->about = _sentDescription;
if (App::api()) {
emit App::api()->fullPeerUpdated(_channel);
}
saveSign();
return true;
2015-09-21 20:57:42 +00:00
} else {
_description.setFocus();
}
} else if (req == _saveSignRequestId) {
_saveSignRequestId = 0;
if (err == qstr("CHAT_NOT_MODIFIED")) {
onClose();
return true;
}
2015-09-21 20:57:42 +00:00
}
return true;
}
void EditChannelBox::onSaveTitleDone(const MTPUpdates &updates) {
_saveTitleRequestId = 0;
if (App::main()) {
App::main()->sentUpdatesReceived(updates);
}
2015-09-21 20:57:42 +00:00
saveDescription();
}
void EditChannelBox::onSaveDescriptionDone(const MTPBool &result) {
_saveDescriptionRequestId = 0;
_channel->about = _sentDescription;
if (App::api()) {
emit App::api()->fullPeerUpdated(_channel);
}
saveSign();
}
void EditChannelBox::onSaveSignDone(const MTPUpdates &updates) {
_saveSignRequestId = 0;
if (App::main()) {
App::main()->sentUpdatesReceived(updates);
}
2015-09-21 20:57:42 +00:00
onClose();
}