2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2014-12-01 10:47:38 +00:00
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2014-12-01 10:47:38 +00:00
|
|
|
Copyright (c) 2014 John Preston, https://desktop.telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "lang.h"
|
|
|
|
|
|
|
|
#include "application.h"
|
|
|
|
#include "addcontactbox.h"
|
|
|
|
#include "mainwidget.h"
|
|
|
|
#include "window.h"
|
|
|
|
|
|
|
|
AddContactBox::AddContactBox(QString fname, QString lname, QString phone) :
|
2014-06-16 09:31:10 +00:00
|
|
|
_peer(0),
|
2014-05-30 08:53:19 +00:00
|
|
|
_addButton(this, lang(lng_add_contact), st::btnSelectDone),
|
|
|
|
_retryButton(this, lang(lng_try_other_contact), st::btnSelectDone),
|
|
|
|
_cancelButton(this, lang(lng_cancel), st::btnSelectCancel),
|
2014-06-16 09:31:10 +00:00
|
|
|
_firstInput(this, st::inpAddContact, lang(lng_signup_firstname), fname),
|
|
|
|
_lastInput(this, st::inpAddContact, lang(lng_signup_lastname), lname),
|
|
|
|
_phoneInput(this, st::inpAddContact, lang(lng_contact_phone), phone.isEmpty() ? phone : App::formatPhone(phone)),
|
2015-04-02 10:33:19 +00:00
|
|
|
_contactId(0), _addRequest(0) {
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
if (!phone.isEmpty()) {
|
|
|
|
_phoneInput.setDisabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
initBox();
|
|
|
|
}
|
|
|
|
|
|
|
|
AddContactBox::AddContactBox(PeerData *peer) :
|
2014-06-16 09:31:10 +00:00
|
|
|
_peer(peer),
|
2014-05-30 08:53:19 +00:00
|
|
|
_addButton(this, lang(lng_settings_save), st::btnSelectDone),
|
|
|
|
_retryButton(this, lang(lng_try_other_contact), st::btnSelectDone),
|
|
|
|
_cancelButton(this, lang(lng_cancel), st::btnSelectCancel),
|
2014-06-16 09:31:10 +00:00
|
|
|
_firstInput(this, st::inpAddContact, lang(peer->chat ? lng_dlg_new_group_name : lng_signup_firstname), peer->chat ? peer->name : peer->asUser()->firstName),
|
|
|
|
_lastInput(this, st::inpAddContact, lang(lng_signup_lastname), peer->chat ? QString() : peer->asUser()->lastName),
|
|
|
|
_phoneInput(this, st::inpAddContact, lang(lng_contact_phone)),
|
2015-04-02 10:33:19 +00:00
|
|
|
_contactId(0), _addRequest(0) {
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
initBox();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddContactBox::initBox() {
|
|
|
|
if (_peer) {
|
|
|
|
if (_peer->chat) {
|
|
|
|
_boxTitle = lang(lng_edit_group_title);
|
2015-04-02 10:33:19 +00:00
|
|
|
setMaxHeight(st::boxTitleHeight + st::addContactPadding.top() + 1 * _firstInput.height() + st::addContactPadding.bottom() + _addButton.height());
|
2014-05-30 08:53:19 +00:00
|
|
|
} else {
|
|
|
|
_boxTitle = lang(_peer == App::self() ? lng_edit_self_title : lng_edit_contact_title);
|
2015-04-02 10:33:19 +00:00
|
|
|
setMaxHeight(st::boxTitleHeight + st::addContactPadding.top() + 2 * _firstInput.height() + 1 * st::addContactDelta + st::addContactPadding.bottom() + _addButton.height());
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bool readyToAdd = !_phoneInput.text().isEmpty() && (!_firstInput.text().isEmpty() || !_lastInput.text().isEmpty());
|
|
|
|
_boxTitle = lang(readyToAdd ? lng_confirm_contact_data : lng_enter_contact_data);
|
2015-04-02 10:33:19 +00:00
|
|
|
setMaxHeight(st::boxTitleHeight + st::addContactPadding.top() + 3 * _firstInput.height() + 2 * st::addContactDelta + st::addContactPadding.bottom() + _addButton.height());
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
_retryButton.hide();
|
|
|
|
|
|
|
|
connect(&_addButton, SIGNAL(clicked()), this, SLOT(onSend()));
|
|
|
|
connect(&_retryButton, SIGNAL(clicked()), this, SLOT(onRetry()));
|
2015-04-02 10:33:19 +00:00
|
|
|
connect(&_cancelButton, SIGNAL(clicked()), this, SLOT(onClose()));
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-04-02 10:33:19 +00:00
|
|
|
prepare();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddContactBox::hideAll() {
|
|
|
|
_firstInput.hide();
|
|
|
|
_lastInput.hide();
|
|
|
|
_phoneInput.hide();
|
|
|
|
_addButton.hide();
|
|
|
|
_retryButton.hide();
|
|
|
|
_cancelButton.hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddContactBox::showAll() {
|
|
|
|
_firstInput.show();
|
|
|
|
if (_peer && _peer->chat) {
|
|
|
|
_lastInput.hide();
|
|
|
|
} else {
|
|
|
|
_lastInput.show();
|
|
|
|
}
|
|
|
|
if (_peer) {
|
|
|
|
_phoneInput.hide();
|
|
|
|
} else {
|
|
|
|
_phoneInput.show();
|
|
|
|
}
|
|
|
|
_addButton.show();
|
|
|
|
_cancelButton.show();
|
|
|
|
}
|
|
|
|
|
2015-04-02 10:33:19 +00:00
|
|
|
void AddContactBox::showDone() {
|
|
|
|
if ((_firstInput.text().isEmpty() && _lastInput.text().isEmpty()) || _phoneInput.isHidden() || !_phoneInput.isEnabled()) {
|
|
|
|
_firstInput.setFocus();
|
|
|
|
} else {
|
|
|
|
_phoneInput.setFocus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
void AddContactBox::keyPressEvent(QKeyEvent *e) {
|
|
|
|
if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
|
|
|
|
if (_firstInput.hasFocus()) {
|
|
|
|
if (_peer && _peer->chat) {
|
|
|
|
if (_firstInput.text().trimmed().isEmpty()) {
|
|
|
|
_firstInput.setFocus();
|
|
|
|
_firstInput.notaBene();
|
|
|
|
} else {
|
|
|
|
onSend();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_lastInput.setFocus();
|
|
|
|
}
|
|
|
|
} else if (_lastInput.hasFocus()) {
|
|
|
|
if (_peer) {
|
|
|
|
if (_firstInput.text().trimmed().isEmpty()) {
|
|
|
|
_firstInput.setFocus();
|
|
|
|
_firstInput.notaBene();
|
|
|
|
} else if (_lastInput.text().trimmed().isEmpty()) {
|
|
|
|
_lastInput.setFocus();
|
|
|
|
_lastInput.notaBene();
|
|
|
|
} else {
|
|
|
|
onSend();
|
|
|
|
}
|
|
|
|
} else if (_phoneInput.isEnabled()) {
|
|
|
|
_phoneInput.setFocus();
|
|
|
|
} else {
|
|
|
|
onSend();
|
|
|
|
}
|
|
|
|
} else if (_phoneInput.hasFocus()) {
|
|
|
|
if (_firstInput.text().trimmed().isEmpty()) {
|
|
|
|
_firstInput.setFocus();
|
|
|
|
_firstInput.notaBene();
|
|
|
|
} else if (_lastInput.text().trimmed().isEmpty()) {
|
|
|
|
_lastInput.setFocus();
|
|
|
|
_lastInput.notaBene();
|
|
|
|
} else {
|
|
|
|
onSend();
|
|
|
|
}
|
|
|
|
}
|
2015-04-02 10:33:19 +00:00
|
|
|
} else {
|
|
|
|
AbstractBox::keyPressEvent(e);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddContactBox::paintEvent(QPaintEvent *e) {
|
2015-04-04 20:01:34 +00:00
|
|
|
Painter p(this);
|
2015-04-02 10:33:19 +00:00
|
|
|
if (paint(p)) return;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-04-02 10:33:19 +00:00
|
|
|
if (_retryButton.isHidden()) {
|
|
|
|
paintTitle(p, _boxTitle, true);
|
2014-05-30 08:53:19 +00:00
|
|
|
} else {
|
2015-04-02 10:33:19 +00:00
|
|
|
// draw box text
|
|
|
|
p.setPen(st::black->p);
|
|
|
|
p.setFont(st::boxTitleFont->f);
|
|
|
|
int32 h = size().height() - st::boxPadding.top() * 2 - _retryButton.height() - st::boxPadding.bottom();
|
|
|
|
p.drawText(QRect(st::boxPadding.left(), st::boxPadding.top(), width() - st::boxPadding.left() - st::boxPadding.right(), h), lng_contact_not_joined(lt_name, _sentName), style::al_topleft);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2015-04-02 10:33:19 +00:00
|
|
|
|
|
|
|
// paint shadows
|
|
|
|
p.fillRect(0, size().height() - st::btnSelectCancel.height - st::scrollDef.bottomsh, width(), st::scrollDef.bottomsh, st::scrollDef.shColor->b);
|
|
|
|
|
|
|
|
// paint button sep
|
|
|
|
p.fillRect(st::btnSelectCancel.width, size().height() - st::btnSelectCancel.height, st::lineWidth, st::btnSelectCancel.height, st::btnSelectSep->b);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2015-04-02 10:33:19 +00:00
|
|
|
void AddContactBox::resizeEvent(QResizeEvent *e) {
|
|
|
|
_firstInput.setGeometry(st::addContactPadding.left(), st::boxTitleHeight + st::addContactPadding.top(), width() - st::addContactPadding.left() - st::addContactPadding.right(), _firstInput.height());
|
|
|
|
_lastInput.setGeometry(st::addContactPadding.left(), _firstInput.y() + _firstInput.height() + st::addContactDelta, _firstInput.width(), _firstInput.height());
|
|
|
|
_phoneInput.setGeometry(st::addContactPadding.left(), _lastInput.y() + _lastInput.height() + st::addContactDelta, _lastInput.width(), _lastInput.height());
|
|
|
|
|
|
|
|
_cancelButton.move(0, height() - _cancelButton.height());
|
|
|
|
_addButton.move(width() - _addButton.width(), height() - _addButton.height());
|
|
|
|
_retryButton.move(width() - _retryButton.width(), height() - _retryButton.height());
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddContactBox::onSend() {
|
|
|
|
if (_addRequest) return;
|
|
|
|
|
|
|
|
QString firstName = _firstInput.text().trimmed(), lastName = _lastInput.text().trimmed(), phone = _phoneInput.text().trimmed();
|
|
|
|
if (firstName.isEmpty() && lastName.isEmpty()) {
|
|
|
|
_firstInput.setFocus();
|
|
|
|
_firstInput.notaBene();
|
|
|
|
return;
|
|
|
|
} else if (!_peer && !App::isValidPhone(phone)) {
|
|
|
|
_phoneInput.setFocus();
|
|
|
|
_phoneInput.notaBene();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (firstName.isEmpty()) {
|
|
|
|
firstName = lastName;
|
|
|
|
lastName = QString();
|
|
|
|
}
|
|
|
|
_sentName = firstName;
|
|
|
|
if (_peer == App::self()) {
|
|
|
|
_addRequest = MTP::send(MTPaccount_UpdateProfile(MTP_string(firstName), MTP_string(lastName)), rpcDone(&AddContactBox::onSaveSelfDone), rpcFail(&AddContactBox::onSaveSelfFail));
|
|
|
|
} else if (_peer) {
|
|
|
|
if (_peer->chat) {
|
2014-06-16 09:31:10 +00:00
|
|
|
_addRequest = MTP::send(MTPmessages_EditChatTitle(MTP_int(App::chatFromPeer(_peer->id)), MTP_string(firstName)), rpcDone(&AddContactBox::onSaveChatDone), rpcFail(&AddContactBox::onSaveFail));
|
2014-05-30 08:53:19 +00:00
|
|
|
} else {
|
|
|
|
_contactId = MTP::nonce<uint64>();
|
|
|
|
QVector<MTPInputContact> v(1, MTP_inputPhoneContact(MTP_long(_contactId), MTP_string(_peer->asUser()->phone), MTP_string(firstName), MTP_string(lastName)));
|
|
|
|
_addRequest = MTP::send(MTPcontacts_ImportContacts(MTP_vector<MTPInputContact>(v), MTP_bool(false)), rpcDone(&AddContactBox::onSaveUserDone), rpcFail(&AddContactBox::onSaveFail));
|
|
|
|
}
|
|
|
|
} 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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddContactBox::onSaveSelfDone(const MTPUser &user) {
|
2014-11-05 17:43:32 +00:00
|
|
|
App::feedUsers(MTP_vector<MTPUser>(1, user));
|
2014-05-30 08:53:19 +00:00
|
|
|
emit closed();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AddContactBox::onSaveSelfFail(const RPCError &error) {
|
2015-04-04 20:01:34 +00:00
|
|
|
if (error.type().startsWith(qsl("FLOOD_WAIT_"))) return false;
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
QString err(error.type());
|
|
|
|
QString firstName = textOneLine(_firstInput.text()), lastName = textOneLine(_lastInput.text());
|
|
|
|
if (err == "NAME_NOT_MODIFIED") {
|
2014-12-03 13:10:32 +00:00
|
|
|
App::self()->setName(firstName, lastName, QString(), textOneLine(App::self()->username));
|
2014-05-30 08:53:19 +00:00
|
|
|
emit closed();
|
|
|
|
return true;
|
|
|
|
} else if (err == "FIRSTNAME_INVALID") {
|
|
|
|
_firstInput.setFocus();
|
|
|
|
_firstInput.notaBene();
|
|
|
|
return true;
|
|
|
|
} else if (err == "LASTNAME_INVALID") {
|
|
|
|
_lastInput.setFocus();
|
|
|
|
_lastInput.notaBene();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
_firstInput.setFocus();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AddContactBox::onSaveFail(const RPCError &error) {
|
2015-04-04 20:01:34 +00:00
|
|
|
if (error.type().startsWith(qsl("FLOOD_WAIT_"))) return false;
|
|
|
|
|
2014-10-22 18:39:03 +00:00
|
|
|
_addRequest = 0;
|
2014-05-30 08:53:19 +00:00
|
|
|
QString err(error.type());
|
|
|
|
QString firstName = _firstInput.text().trimmed(), lastName = _lastInput.text().trimmed();
|
|
|
|
if (err == "CHAT_TITLE_NOT_MODIFIED") {
|
2014-11-14 23:23:35 +00:00
|
|
|
_peer->updateName(firstName, QString(), QString());
|
2014-05-30 08:53:19 +00:00
|
|
|
emit closed();
|
|
|
|
return true;
|
|
|
|
} else if (err == "NO_CHAT_TITLE") {
|
|
|
|
_firstInput.setFocus();
|
|
|
|
_firstInput.notaBene();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
_firstInput.setFocus();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddContactBox::onImportDone(const MTPcontacts_ImportedContacts &res) {
|
2015-04-02 10:33:19 +00:00
|
|
|
if (isHidden() || !App::main()) return;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
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) {
|
|
|
|
App::main()->addNewContact(uid);
|
|
|
|
App::main()->showPeer(App::peerFromUser(uid));
|
|
|
|
App::wnd()->hideLayer();
|
|
|
|
} else {
|
|
|
|
_addButton.hide();
|
|
|
|
_firstInput.hide();
|
|
|
|
_lastInput.hide();
|
|
|
|
_phoneInput.hide();
|
|
|
|
_retryButton.show();
|
2015-04-02 10:33:19 +00:00
|
|
|
int32 theight = st::boxTitleFont->m.boundingRect(0, 0, width() - st::boxPadding.left() - st::boxPadding.right(), 1, Qt::TextWordWrap, lng_contact_not_joined(lt_name, _sentName)).height();
|
2014-05-30 08:53:19 +00:00
|
|
|
int32 h = st::boxPadding.top() * 2 + theight + _retryButton.height() + st::boxPadding.bottom();
|
2015-04-02 10:33:19 +00:00
|
|
|
setMaxHeight(h);
|
2014-05-30 08:53:19 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-02 10:33:19 +00:00
|
|
|
void AddContactBox::onSaveChatDone(const MTPUpdates &updates) {
|
|
|
|
App::main()->sentUpdatesReceived(updates);
|
2014-05-30 08:53:19 +00:00
|
|
|
emit closed();
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
_addButton.show();
|
|
|
|
_cancelButton.move(_cancelButton.x(), _addButton.y());
|
|
|
|
showAll();
|
|
|
|
_firstInput.setText(QString());
|
|
|
|
_firstInput.updatePlaceholder();
|
|
|
|
_lastInput.setText(QString());
|
|
|
|
_lastInput.updatePlaceholder();
|
|
|
|
_phoneInput.setText(QString());
|
|
|
|
_phoneInput.updatePlaceholder();
|
|
|
|
_phoneInput.setDisabled(false);
|
|
|
|
_retryButton.hide();
|
|
|
|
_firstInput.setFocus();
|
2015-04-02 10:33:19 +00:00
|
|
|
setMaxHeight(st::boxTitleHeight + st::addContactPadding.top() + 3 * _firstInput.height() + 2 * st::addContactDelta + st::addContactPadding.bottom() + _addButton.height());
|
2014-05-30 08:53:19 +00:00
|
|
|
update();
|
|
|
|
}
|