intro_signup.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "intro/intro_signup.h"
  8. #include "boxes/abstract_box.h"
  9. #include "intro/intro_widget.h"
  10. #include "core/file_utilities.h"
  11. #include "ui/boxes/confirm_box.h"
  12. #include "lang/lang_keys.h"
  13. #include "ui/controls/userpic_button.h"
  14. #include "ui/widgets/buttons.h"
  15. #include "ui/widgets/fields/input_field.h"
  16. #include "ui/widgets/labels.h"
  17. #include "styles/style_intro.h"
  18. #include "styles/style_boxes.h"
  19. namespace Intro {
  20. namespace details {
  21. SignupWidget::SignupWidget(
  22. QWidget *parent,
  23. not_null<Main::Account*> account,
  24. not_null<Data*> data)
  25. : Step(parent, account, data)
  26. , _photo(
  27. this,
  28. data->controller,
  29. Ui::UserpicButton::Role::ChoosePhoto,
  30. st::defaultUserpicButton)
  31. , _first(this, st::introName, tr::lng_signup_firstname())
  32. , _last(this, st::introName, tr::lng_signup_lastname())
  33. , _invertOrder(langFirstNameGoesSecond()) {
  34. _photo->showCustomOnChosen();
  35. Lang::Updated(
  36. ) | rpl::start_with_next([=] {
  37. refreshLang();
  38. }, lifetime());
  39. if (_invertOrder) {
  40. setTabOrder(_last, _first);
  41. } else {
  42. setTabOrder(_first, _last);
  43. }
  44. setErrorCentered(true);
  45. setTitleText(tr::lng_signup_title());
  46. setDescriptionText(tr::lng_signup_desc());
  47. setMouseTracking(true);
  48. }
  49. void SignupWidget::finishInit() {
  50. showTerms();
  51. }
  52. void SignupWidget::refreshLang() {
  53. _invertOrder = langFirstNameGoesSecond();
  54. if (_invertOrder) {
  55. setTabOrder(_last, _first);
  56. } else {
  57. setTabOrder(_first, _last);
  58. }
  59. updateControlsGeometry();
  60. }
  61. void SignupWidget::resizeEvent(QResizeEvent *e) {
  62. Step::resizeEvent(e);
  63. updateControlsGeometry();
  64. }
  65. void SignupWidget::updateControlsGeometry() {
  66. auto photoRight = contentLeft() + st::introNextButton.width;
  67. auto photoTop = contentTop() + st::introPhotoTop;
  68. _photo->moveToLeft(photoRight - _photo->width(), photoTop);
  69. auto firstTop = contentTop() + st::introStepFieldTop;
  70. auto secondTop = firstTop + st::introName.heightMin + st::introPhoneTop;
  71. if (_invertOrder) {
  72. _last->moveToLeft(contentLeft(), firstTop);
  73. _first->moveToLeft(contentLeft(), secondTop);
  74. } else {
  75. _first->moveToLeft(contentLeft(), firstTop);
  76. _last->moveToLeft(contentLeft(), secondTop);
  77. }
  78. }
  79. void SignupWidget::setInnerFocus() {
  80. if (_invertOrder || _last->hasFocus()) {
  81. _last->setFocusFast();
  82. } else {
  83. _first->setFocusFast();
  84. }
  85. }
  86. void SignupWidget::activate() {
  87. Step::activate();
  88. _first->show();
  89. _last->show();
  90. _photo->show();
  91. setInnerFocus();
  92. }
  93. void SignupWidget::cancelled() {
  94. api().request(base::take(_sentRequest)).cancel();
  95. }
  96. void SignupWidget::nameSubmitDone(const MTPauth_Authorization &result) {
  97. finish(result);
  98. }
  99. void SignupWidget::nameSubmitFail(const MTP::Error &error) {
  100. if (MTP::IsFloodError(error)) {
  101. showError(tr::lng_flood_error());
  102. if (_invertOrder) {
  103. _first->setFocus();
  104. } else {
  105. _last->setFocus();
  106. }
  107. return;
  108. }
  109. auto &err = error.type();
  110. if (err == u"PHONE_NUMBER_FLOOD"_q) {
  111. Ui::show(Ui::MakeInformBox(tr::lng_error_phone_flood()));
  112. } else if (err == u"PHONE_NUMBER_INVALID"_q
  113. || err == u"PHONE_NUMBER_BANNED"_q
  114. || err == u"PHONE_CODE_EXPIRED"_q
  115. || err == u"PHONE_CODE_EMPTY"_q
  116. || err == u"PHONE_CODE_INVALID"_q
  117. || err == u"PHONE_NUMBER_OCCUPIED"_q) {
  118. goBack();
  119. } else if (err == "FIRSTNAME_INVALID") {
  120. showError(tr::lng_bad_name());
  121. _first->setFocus();
  122. } else if (err == "LASTNAME_INVALID") {
  123. showError(tr::lng_bad_name());
  124. _last->setFocus();
  125. } else {
  126. if (Logs::DebugEnabled()) { // internal server error
  127. showError(rpl::single(err + ": " + error.description()));
  128. } else {
  129. showError(rpl::single(Lang::Hard::ServerError()));
  130. }
  131. if (_invertOrder) {
  132. _last->setFocus();
  133. } else {
  134. _first->setFocus();
  135. }
  136. }
  137. }
  138. void SignupWidget::submit() {
  139. if (_sentRequest) {
  140. return;
  141. }
  142. if (_invertOrder) {
  143. if ((_last->hasFocus() || _last->getLastText().trimmed().length()) && !_first->getLastText().trimmed().length()) {
  144. _first->setFocus();
  145. return;
  146. } else if (!_last->getLastText().trimmed().length()) {
  147. _last->setFocus();
  148. return;
  149. }
  150. } else {
  151. if ((_first->hasFocus() || _first->getLastText().trimmed().length()) && !_last->getLastText().trimmed().length()) {
  152. _last->setFocus();
  153. return;
  154. } else if (!_first->getLastText().trimmed().length()) {
  155. _first->setFocus();
  156. return;
  157. }
  158. }
  159. const auto send = [&] {
  160. hideError();
  161. _firstName = _first->getLastText().trimmed();
  162. _lastName = _last->getLastText().trimmed();
  163. _sentRequest = api().request(MTPauth_SignUp(
  164. MTP_flags(0),
  165. MTP_string(getData()->phone),
  166. MTP_bytes(getData()->phoneHash),
  167. MTP_string(_firstName),
  168. MTP_string(_lastName)
  169. )).done([=](const MTPauth_Authorization &result) {
  170. nameSubmitDone(result);
  171. }).fail([=](const MTP::Error &error) {
  172. nameSubmitFail(error);
  173. }).handleFloodErrors().send();
  174. };
  175. if (_termsAccepted
  176. || getData()->termsLock.text.text.isEmpty()
  177. || !getData()->termsLock.popup) {
  178. send();
  179. } else {
  180. acceptTerms(crl::guard(this, [=] {
  181. _termsAccepted = true;
  182. send();
  183. }));
  184. }
  185. }
  186. rpl::producer<QString> SignupWidget::nextButtonText() const {
  187. return tr::lng_intro_finish();
  188. }
  189. } // namespace details
  190. } // namespace Intro