settings_privacy_controllers.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629
  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 "settings/settings_privacy_controllers.h"
  8. #include "api/api_global_privacy.h"
  9. #include "api/api_peer_photo.h"
  10. #include "apiwrap.h"
  11. #include "base/call_delayed.h"
  12. #include "base/event_filter.h"
  13. #include "base/unixtime.h"
  14. #include "boxes/abstract_box.h"
  15. #include "boxes/peer_list_controllers.h"
  16. #include "boxes/peers/peer_short_info_box.h"
  17. #include "boxes/peers/prepare_short_info_box.h"
  18. #include "calls/calls_instance.h"
  19. #include "core/application.h"
  20. #include "data/data_changes.h"
  21. #include "data/data_file_origin.h"
  22. #include "data/data_peer_values.h" // Data::AmPremiumValue.
  23. #include "data/data_photo_media.h"
  24. #include "data/data_session.h"
  25. #include "data/data_user.h"
  26. #include "data/data_user_photos.h" // UserPhotosViewer.
  27. #include "editor/photo_editor_common.h"
  28. #include "editor/photo_editor_layer_widget.h"
  29. #include "history/admin_log/history_admin_log_item.h"
  30. #include "history/history.h"
  31. #include "history/history_item_components.h"
  32. #include "history/view/history_view_message.h"
  33. #include "lang/lang_keys.h"
  34. #include "main/main_session.h"
  35. #include "settings/settings_premium.h"
  36. #include "settings/settings_privacy_security.h"
  37. #include "ui/boxes/confirm_box.h"
  38. #include "ui/chat/chat_style.h"
  39. #include "ui/chat/chat_theme.h"
  40. #include "ui/painter.h"
  41. #include "ui/vertical_list.h"
  42. #include "ui/text/format_values.h" // Ui::FormatPhone
  43. #include "ui/text/text_utilities.h"
  44. #include "ui/toast/toast.h"
  45. #include "ui/widgets/checkbox.h"
  46. #include "ui/wrap/slide_wrap.h"
  47. #include "window/section_widget.h"
  48. #include "window/window_controller.h"
  49. #include "window/window_session_controller.h"
  50. #include "styles/style_chat.h"
  51. #include "styles/style_chat_helpers.h"
  52. #include "styles/style_settings.h"
  53. #include "styles/style_info.h"
  54. #include "styles/style_layers.h"
  55. #include "styles/style_menu_icons.h"
  56. #include <QtGui/QGuiApplication>
  57. #include <QtGui/QClipboard>
  58. namespace Settings {
  59. namespace {
  60. using UserPrivacy = Api::UserPrivacy;
  61. using PrivacyRule = Api::UserPrivacy::Rule;
  62. using Option = EditPrivacyBox::Option;
  63. [[nodiscard]] QString PublicLinkByPhone(not_null<UserData*> user) {
  64. return user->session().createInternalLinkFull('+' + user->phone());
  65. }
  66. class BlockPeerBoxController final : public ChatsListBoxController {
  67. public:
  68. explicit BlockPeerBoxController(not_null<Main::Session*> session);
  69. Main::Session &session() const override;
  70. void rowClicked(not_null<PeerListRow*> row) override;
  71. void setBlockPeerCallback(Fn<void(not_null<PeerData*> peer)> callback) {
  72. _blockPeerCallback = std::move(callback);
  73. }
  74. protected:
  75. void prepareViewHook() override;
  76. std::unique_ptr<Row> createRow(not_null<History*> history) override;
  77. void updateRowHook(not_null<Row*> row) override {
  78. updateIsBlocked(row, row->peer());
  79. delegate()->peerListUpdateRow(row);
  80. }
  81. private:
  82. void updateIsBlocked(not_null<PeerListRow*> row, PeerData *peer) const;
  83. const not_null<Main::Session*> _session;
  84. Fn<void(not_null<PeerData*> peer)> _blockPeerCallback;
  85. };
  86. BlockPeerBoxController::BlockPeerBoxController(
  87. not_null<Main::Session*> session)
  88. : ChatsListBoxController(session)
  89. , _session(session) {
  90. }
  91. Main::Session &BlockPeerBoxController::session() const {
  92. return *_session;
  93. }
  94. void BlockPeerBoxController::prepareViewHook() {
  95. delegate()->peerListSetTitle(tr::lng_blocked_list_add_title());
  96. session().changes().peerUpdates(
  97. Data::PeerUpdate::Flag::IsBlocked
  98. ) | rpl::start_with_next([=](const Data::PeerUpdate &update) {
  99. if (auto row = delegate()->peerListFindRow(update.peer->id.value)) {
  100. updateIsBlocked(row, update.peer);
  101. delegate()->peerListUpdateRow(row);
  102. }
  103. }, lifetime());
  104. }
  105. void BlockPeerBoxController::updateIsBlocked(
  106. not_null<PeerListRow*> row,
  107. PeerData *peer) const {
  108. if (!peer) {
  109. return;
  110. }
  111. const auto blocked = peer->isBlocked();
  112. row->setDisabledState(blocked
  113. ? PeerListRow::State::DisabledChecked
  114. : PeerListRow::State::Active);
  115. if (blocked) {
  116. row->setCustomStatus(tr::lng_blocked_list_already_blocked(tr::now));
  117. } else {
  118. row->clearCustomStatus();
  119. }
  120. }
  121. void BlockPeerBoxController::rowClicked(not_null<PeerListRow*> row) {
  122. _blockPeerCallback(row->peer());
  123. }
  124. auto BlockPeerBoxController::createRow(not_null<History*> history)
  125. -> std::unique_ptr<BlockPeerBoxController::Row> {
  126. const auto peer = history->peer;
  127. if (!peer->isUser()
  128. || peer->isServiceUser()
  129. || peer->isSelf()
  130. || peer->isRepliesChat()
  131. || peer->isVerifyCodes()) {
  132. return nullptr;
  133. }
  134. auto row = std::make_unique<Row>(history);
  135. updateIsBlocked(row.get(), peer);
  136. return row;
  137. }
  138. AdminLog::OwnedItem GenerateForwardedItem(
  139. not_null<HistoryView::ElementDelegate*> delegate,
  140. not_null<History*> history,
  141. const QString &text) {
  142. Expects(history->peer->isUser());
  143. using Flag = MTPDmessage::Flag;
  144. const auto flags = Flag::f_from_id | Flag::f_fwd_from;
  145. const auto item = MTP_message(
  146. MTP_flags(flags),
  147. MTP_int(0), // Not used (would've been trimmed to 32 bits).
  148. peerToMTP(history->peer->id),
  149. MTPint(), // from_boosts_applied
  150. peerToMTP(history->peer->id),
  151. MTPPeer(), // saved_peer_id
  152. MTP_messageFwdHeader(
  153. MTP_flags(MTPDmessageFwdHeader::Flag::f_from_id),
  154. peerToMTP(history->session().userPeerId()),
  155. MTPstring(), // from_name
  156. MTP_int(base::unixtime::now()),
  157. MTPint(), // channel_post
  158. MTPstring(), // post_author
  159. MTPPeer(), // saved_from_peer
  160. MTPint(), // saved_from_msg_id
  161. MTPPeer(), // saved_from_id
  162. MTPstring(), // saved_from_name
  163. MTPint(), // saved_date
  164. MTPstring()), // psa_type
  165. MTPlong(), // via_bot_id
  166. MTPlong(), // via_business_bot_id
  167. MTPMessageReplyHeader(),
  168. MTP_int(base::unixtime::now()), // date
  169. MTP_string(text),
  170. MTPMessageMedia(),
  171. MTPReplyMarkup(),
  172. MTPVector<MTPMessageEntity>(),
  173. MTPint(), // views
  174. MTPint(), // forwards
  175. MTPMessageReplies(),
  176. MTPint(), // edit_date
  177. MTPstring(), // post_author
  178. MTPlong(), // grouped_id
  179. MTPMessageReactions(),
  180. MTPVector<MTPRestrictionReason>(),
  181. MTPint(), // ttl_period
  182. MTPint(), // quick_reply_shortcut_id
  183. MTPlong(), // effect
  184. MTPFactCheck(),
  185. MTPint(), // report_delivery_until_date
  186. MTPlong() // paid_message_stars
  187. ).match([&](const MTPDmessage &data) {
  188. return history->makeMessage(
  189. history->nextNonHistoryEntryId(),
  190. data,
  191. MessageFlag::FakeHistoryItem);
  192. }, [](auto &&) -> not_null<HistoryItem*> {
  193. Unexpected("Type in GenerateForwardedItem.");
  194. });
  195. return AdminLog::OwnedItem(delegate, item);
  196. }
  197. struct ForwardedTooltip {
  198. QRect geometry;
  199. Fn<void(QPainter&)> paint;
  200. };
  201. [[nodiscard]] ForwardedTooltip PrepareForwardedTooltip(
  202. not_null<HistoryView::Element*> view,
  203. Option value) {
  204. // This breaks HistoryView::Element encapsulation :(
  205. const auto forwarded = view->data()->Get<HistoryMessageForwarded>();
  206. const auto availableWidth = view->width()
  207. - st::msgMargin.left()
  208. - st::msgMargin.right();
  209. const auto bubbleWidth = ranges::min({
  210. availableWidth,
  211. view->maxWidth(),
  212. st::msgMaxWidth
  213. });
  214. const auto innerWidth = bubbleWidth
  215. - st::msgPadding.left()
  216. - st::msgPadding.right();
  217. const auto phrase = tr::lng_forwarded(
  218. tr::now,
  219. lt_user,
  220. view->history()->session().user()->name());
  221. const auto kReplacementPosition = QChar(0x0001);
  222. const auto possiblePosition = tr::lng_forwarded(
  223. tr::now,
  224. lt_user,
  225. QString(1, kReplacementPosition)
  226. ).indexOf(kReplacementPosition);
  227. const auto position = (possiblePosition >= 0
  228. && possiblePosition < phrase.size())
  229. ? possiblePosition
  230. : 0;
  231. const auto before = phrase.mid(0, position);
  232. const auto skip = st::msgMargin.left() + st::msgPadding.left();
  233. const auto small = forwarded->text.countHeight(innerWidth)
  234. < 2 * st::msgServiceFont->height;
  235. const auto nameLeft = skip
  236. + (small ? st::msgServiceFont->width(before) : 0);
  237. const auto right = skip + innerWidth;
  238. const auto text = [&] {
  239. switch (value) {
  240. case Option::Everyone:
  241. return tr::lng_edit_privacy_forwards_sample_everyone(tr::now);
  242. case Option::Contacts:
  243. case Option::CloseFriends:
  244. return tr::lng_edit_privacy_forwards_sample_contacts(tr::now);
  245. case Option::Nobody:
  246. return tr::lng_edit_privacy_forwards_sample_nobody(tr::now);
  247. }
  248. Unexpected("Option value in ForwardsPrivacyController.");
  249. }();
  250. const auto &font = st::defaultToast.style.font;
  251. const auto textWidth = font->width(text);
  252. const auto arrowSkip = st::settingsForwardPrivacyArrowSkip;
  253. const auto arrowSize = st::settingsForwardPrivacyArrowSize;
  254. const auto padding = st::settingsForwardPrivacyTooltipPadding;
  255. const auto rect = QRect(0, 0, textWidth, font->height).marginsAdded(
  256. padding
  257. ).translated(padding.left(), padding.top());
  258. const auto top = st::settingsForwardPrivacyPadding
  259. + view->marginTop()
  260. + st::msgPadding.top()
  261. - arrowSize
  262. - rect.height();
  263. const auto left1 = std::min(nameLeft, right - rect.width());
  264. const auto left2 = std::max(left1, skip);
  265. const auto left = left2;
  266. const auto arrowLeft1 = nameLeft + arrowSkip;
  267. const auto arrowLeft2 = std::min(
  268. arrowLeft1,
  269. std::max((left + right) / 2, right - arrowSkip));
  270. const auto arrowLeft = arrowLeft2;
  271. const auto geometry = rect.translated(left, top);
  272. const auto line = st::lineWidth;
  273. const auto full = geometry.marginsAdded(
  274. { line, line, line, line + arrowSize });
  275. const auto origin = full.topLeft();
  276. const auto rounded = std::make_shared<Ui::RoundRect>(
  277. ImageRoundRadius::Large,
  278. st::toastBg);
  279. const auto paint = [=](QPainter &p) {
  280. p.translate(-origin);
  281. rounded->paint(p, geometry);
  282. p.setFont(font);
  283. p.setPen(st::toastFg);
  284. p.drawText(
  285. geometry.x() + padding.left(),
  286. geometry.y() + padding.top() + font->ascent,
  287. text);
  288. const auto bottom = full.y() + full.height() - line;
  289. QPainterPath path;
  290. path.moveTo(arrowLeft - arrowSize, bottom - arrowSize);
  291. path.lineTo(arrowLeft, bottom);
  292. path.lineTo(arrowLeft + arrowSize, bottom - arrowSize);
  293. path.lineTo(arrowLeft - arrowSize, bottom - arrowSize);
  294. {
  295. PainterHighQualityEnabler hq(p);
  296. p.setPen(Qt::NoPen);
  297. p.fillPath(path, st::toastBg);
  298. }
  299. };
  300. return { .geometry = full, .paint = paint };
  301. }
  302. } // namespace
  303. BlockedBoxController::BlockedBoxController(
  304. not_null<Window::SessionController*> window)
  305. : _window(window) {
  306. }
  307. Main::Session &BlockedBoxController::session() const {
  308. return _window->session();
  309. }
  310. void BlockedBoxController::prepare() {
  311. delegate()->peerListSetTitle(tr::lng_blocked_list_title());
  312. setDescriptionText(tr::lng_contacts_loading(tr::now));
  313. delegate()->peerListRefreshRows();
  314. session().changes().peerUpdates(
  315. Data::PeerUpdate::Flag::IsBlocked
  316. ) | rpl::start_with_next([=](const Data::PeerUpdate &update) {
  317. handleBlockedEvent(update.peer);
  318. }, lifetime());
  319. session().api().blockedPeers().slice(
  320. ) | rpl::take(
  321. 1
  322. ) | rpl::start_with_next([=](const Api::BlockedPeers::Slice &result) {
  323. setDescriptionText(tr::lng_blocked_list_about(tr::now));
  324. applySlice(result);
  325. loadMoreRows();
  326. }, lifetime());
  327. }
  328. void BlockedBoxController::loadMoreRows() {
  329. if (_allLoaded) {
  330. return;
  331. }
  332. session().api().blockedPeers().request(
  333. _offset,
  334. crl::guard(&_guard, [=](const Api::BlockedPeers::Slice &slice) {
  335. applySlice(slice);
  336. }));
  337. }
  338. void BlockedBoxController::rowClicked(not_null<PeerListRow*> row) {
  339. const auto peer = row->peer();
  340. const auto window = _window;
  341. crl::on_main(window, [=] {
  342. window->showPeerHistory(peer);
  343. });
  344. }
  345. void BlockedBoxController::rowRightActionClicked(
  346. not_null<PeerListRow*> row) {
  347. session().api().blockedPeers().unblock(row->peer());
  348. }
  349. void BlockedBoxController::applySlice(
  350. const Api::BlockedPeers::Slice &slice) {
  351. if (slice.list.empty()) {
  352. _allLoaded = true;
  353. }
  354. _offset += slice.list.size();
  355. for (const auto &item : slice.list) {
  356. if (const auto peer = session().data().peerLoaded(item.id)) {
  357. appendRow(peer);
  358. peer->setIsBlocked(true);
  359. }
  360. }
  361. if (_offset >= slice.total) {
  362. _allLoaded = true;
  363. }
  364. delegate()->peerListRefreshRows();
  365. }
  366. void BlockedBoxController::handleBlockedEvent(not_null<PeerData*> user) {
  367. if (user->isBlocked()) {
  368. if (prependRow(user)) {
  369. delegate()->peerListRefreshRows();
  370. delegate()->peerListScrollToTop();
  371. }
  372. } else if (auto row = delegate()->peerListFindRow(user->id.value)) {
  373. delegate()->peerListRemoveRow(row);
  374. delegate()->peerListRefreshRows();
  375. _rowsCountChanges.fire(delegate()->peerListFullRowsCount());
  376. }
  377. }
  378. void BlockedBoxController::BlockNewPeer(
  379. not_null<Window::SessionController*> window) {
  380. auto controller = std::make_unique<BlockPeerBoxController>(
  381. &window->session());
  382. auto initBox = [=, controller = controller.get()](
  383. not_null<PeerListBox*> box) {
  384. controller->setBlockPeerCallback([=](not_null<PeerData*> peer) {
  385. window->session().api().blockedPeers().block(peer);
  386. box->closeBox();
  387. });
  388. box->addButton(tr::lng_cancel(), [box] { box->closeBox(); });
  389. };
  390. window->show(
  391. Box<PeerListBox>(std::move(controller), std::move(initBox)));
  392. }
  393. bool BlockedBoxController::appendRow(not_null<PeerData*> peer) {
  394. if (delegate()->peerListFindRow(peer->id.value)) {
  395. return false;
  396. }
  397. delegate()->peerListAppendRow(createRow(peer));
  398. _rowsCountChanges.fire(delegate()->peerListFullRowsCount());
  399. return true;
  400. }
  401. bool BlockedBoxController::prependRow(not_null<PeerData*> peer) {
  402. if (delegate()->peerListFindRow(peer->id.value)) {
  403. return false;
  404. }
  405. delegate()->peerListPrependRow(createRow(peer));
  406. _rowsCountChanges.fire(delegate()->peerListFullRowsCount());
  407. return true;
  408. }
  409. std::unique_ptr<PeerListRow> BlockedBoxController::createRow(
  410. not_null<PeerData*> peer) const {
  411. auto row = std::make_unique<PeerListRowWithLink>(peer);
  412. row->setActionLink(tr::lng_blocked_list_unblock(tr::now));
  413. const auto status = [&] {
  414. const auto user = peer->asUser();
  415. if (!user) {
  416. return tr::lng_group_status(tr::now);
  417. } else if (!user->phone().isEmpty()) {
  418. return Ui::FormatPhone(user->phone());
  419. } else if (!user->username().isEmpty()) {
  420. return '@' + user->username();
  421. } else if (user->isBot()) {
  422. return tr::lng_status_bot(tr::now);
  423. }
  424. return tr::lng_blocked_list_unknown_phone(tr::now);
  425. }();
  426. row->setCustomStatus(status);
  427. return row;
  428. }
  429. rpl::producer<int> BlockedBoxController::rowsCountChanges() const {
  430. return _rowsCountChanges.events();
  431. }
  432. PhoneNumberPrivacyController::PhoneNumberPrivacyController(
  433. not_null<Window::SessionController*> controller)
  434. : _controller(controller) {
  435. }
  436. UserPrivacy::Key PhoneNumberPrivacyController::key() const {
  437. return Key::PhoneNumber;
  438. }
  439. rpl::producer<QString> PhoneNumberPrivacyController::title() const {
  440. return tr::lng_edit_privacy_phone_number_title();
  441. }
  442. auto PhoneNumberPrivacyController::optionsTitleKey() const
  443. -> rpl::producer<QString> {
  444. return tr::lng_edit_privacy_phone_number_header();
  445. }
  446. auto PhoneNumberPrivacyController::warning() const
  447. -> rpl::producer<TextWithEntities> {
  448. using namespace rpl::mappers;
  449. const auto self = _controller->session().user();
  450. return rpl::combine(
  451. _phoneNumberOption.value(),
  452. _addedByPhone.value(),
  453. (_1 == Option::Nobody) && (_2 != Option::Everyone)
  454. ) | rpl::map([=](bool onlyContactsSee) {
  455. return onlyContactsSee
  456. ? tr::lng_edit_privacy_phone_number_contacts(
  457. Ui::Text::WithEntities)
  458. : rpl::combine(
  459. tr::lng_edit_privacy_phone_number_warning(),
  460. tr::lng_username_link()
  461. ) | rpl::map([=](const QString &warning, const QString &added) {
  462. auto base = TextWithEntities{
  463. warning + "\n\n" + added + "\n",
  464. };
  465. const auto link = PublicLinkByPhone(self);
  466. return base.append(Ui::Text::Link(link, link));
  467. });
  468. }) | rpl::flatten_latest();
  469. }
  470. void PhoneNumberPrivacyController::prepareWarningLabel(
  471. not_null<Ui::FlatLabel*> warning) const {
  472. warning->overrideLinkClickHandler([=] {
  473. QGuiApplication::clipboard()->setText(PublicLinkByPhone(
  474. _controller->session().user()));
  475. _controller->window().showToast(
  476. tr::lng_username_copied(tr::now));
  477. });
  478. }
  479. rpl::producer<QString> PhoneNumberPrivacyController::exceptionButtonTextKey(
  480. Exception exception) const {
  481. switch (exception) {
  482. case Exception::Always:
  483. return tr::lng_edit_privacy_phone_number_always_empty();
  484. case Exception::Never:
  485. return tr::lng_edit_privacy_phone_number_never_empty();
  486. }
  487. Unexpected("Invalid exception value.");
  488. }
  489. rpl::producer<QString> PhoneNumberPrivacyController::exceptionBoxTitle(
  490. Exception exception) const {
  491. switch (exception) {
  492. case Exception::Always: {
  493. return tr::lng_edit_privacy_phone_number_always_title();
  494. };
  495. case Exception::Never: {
  496. return tr::lng_edit_privacy_phone_number_never_title();
  497. };
  498. }
  499. Unexpected("Invalid exception value.");
  500. }
  501. auto PhoneNumberPrivacyController::exceptionsDescription() const
  502. -> rpl::producer<QString> {
  503. return tr::lng_edit_privacy_phone_number_exceptions();
  504. }
  505. object_ptr<Ui::RpWidget> PhoneNumberPrivacyController::setupMiddleWidget(
  506. not_null<Window::SessionController*> controller,
  507. not_null<QWidget*> parent,
  508. rpl::producer<Option> optionValue) {
  509. const auto key = UserPrivacy::Key::AddedByPhone;
  510. controller->session().api().userPrivacy().reload(key);
  511. _phoneNumberOption = std::move(optionValue);
  512. auto widget = object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
  513. parent,
  514. object_ptr<Ui::VerticalLayout>(parent));
  515. const auto container = widget->entity();
  516. Ui::AddSkip(container);
  517. Ui::AddSubsectionTitle(
  518. container,
  519. tr::lng_edit_privacy_phone_number_find());
  520. const auto group = std::make_shared<Ui::RadioenumGroup<Option>>();
  521. group->setChangedCallback([=](Option value) {
  522. _addedByPhone = value;
  523. });
  524. controller->session().api().userPrivacy().value(
  525. key
  526. ) | rpl::take(
  527. 1
  528. ) | rpl::start_with_next([=](const PrivacyRule &value) {
  529. group->setValue(value.option);
  530. }, widget->lifetime());
  531. const auto addOption = [&](Option option) {
  532. return EditPrivacyBox::AddOption(container, this, group, option);
  533. };
  534. addOption(Option::Everyone);
  535. addOption(Option::Contacts);
  536. Ui::AddSkip(
  537. container,
  538. st::defaultVerticalListSkip + st::settingsPrivacySkipTop);
  539. Ui::AddDivider(container);
  540. using namespace rpl::mappers;
  541. widget->toggleOn(_phoneNumberOption.value(
  542. ) | rpl::map(
  543. _1 == Option::Nobody
  544. ));
  545. _saveAdditional = [=] {
  546. controller->session().api().userPrivacy().save(
  547. Api::UserPrivacy::Key::AddedByPhone,
  548. Api::UserPrivacy::Rule{ .option = group->current() });
  549. };
  550. return widget;
  551. }
  552. void PhoneNumberPrivacyController::saveAdditional() {
  553. if (_saveAdditional) {
  554. _saveAdditional();
  555. }
  556. }
  557. LastSeenPrivacyController::LastSeenPrivacyController(
  558. not_null<::Main::Session*> session)
  559. : _session(session) {
  560. }
  561. UserPrivacy::Key LastSeenPrivacyController::key() const {
  562. return Key::LastSeen;
  563. }
  564. rpl::producer<QString> LastSeenPrivacyController::title() const {
  565. return tr::lng_edit_privacy_lastseen_title();
  566. }
  567. rpl::producer<QString> LastSeenPrivacyController::optionsTitleKey() const {
  568. return tr::lng_edit_privacy_lastseen_header();
  569. }
  570. auto LastSeenPrivacyController::warning() const
  571. -> rpl::producer<TextWithEntities> {
  572. return tr::lng_edit_privacy_lastseen_warning(Ui::Text::WithEntities);
  573. }
  574. rpl::producer<QString> LastSeenPrivacyController::exceptionButtonTextKey(
  575. Exception exception) const {
  576. switch (exception) {
  577. case Exception::Always:
  578. return tr::lng_edit_privacy_lastseen_always_empty();
  579. case Exception::Never:
  580. return tr::lng_edit_privacy_lastseen_never_empty();
  581. }
  582. Unexpected("Invalid exception value.");
  583. }
  584. rpl::producer<QString> LastSeenPrivacyController::exceptionBoxTitle(
  585. Exception exception) const {
  586. switch (exception) {
  587. case Exception::Always: {
  588. return tr::lng_edit_privacy_lastseen_always_title();
  589. };
  590. case Exception::Never: {
  591. return tr::lng_edit_privacy_lastseen_never_title();
  592. };
  593. }
  594. Unexpected("Invalid exception value.");
  595. }
  596. auto LastSeenPrivacyController::exceptionsDescription() const
  597. -> rpl::producer<QString>{
  598. return tr::lng_edit_privacy_lastseen_exceptions();
  599. }
  600. object_ptr<Ui::RpWidget> LastSeenPrivacyController::setupBelowWidget(
  601. not_null<Window::SessionController*> controller,
  602. not_null<QWidget*> parent,
  603. rpl::producer<Option> option) {
  604. using namespace rpl::mappers;
  605. auto result = object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
  606. parent,
  607. object_ptr<Ui::VerticalLayout>(parent));
  608. _option = std::move(option);
  609. const auto content = result->entity();
  610. Ui::AddSkip(content);
  611. const auto privacy = &controller->session().api().globalPrivacy();
  612. content->add(object_ptr<Ui::SettingsButton>(
  613. content,
  614. tr::lng_edit_lastseen_hide_read_time(),
  615. st::settingsButtonNoIcon
  616. ))->toggleOn(privacy->hideReadTime())->toggledValue(
  617. ) | rpl::start_with_next([=](bool value) {
  618. _hideReadTime = value;
  619. }, content->lifetime());
  620. Ui::AddSkip(content);
  621. Ui::AddDividerText(
  622. content,
  623. tr::lng_edit_lastseen_hide_read_time_about());
  624. if (!controller->session().premium()) {
  625. Ui::AddSkip(content);
  626. content->add(object_ptr<Ui::SettingsButton>(
  627. content,
  628. tr::lng_edit_lastseen_subscribe(),
  629. st::settingsButtonLightNoIcon
  630. ))->setClickedCallback([=] {
  631. Settings::ShowPremium(controller, u"lastseen"_q);
  632. });
  633. Ui::AddSkip(content);
  634. Ui::AddDividerText(
  635. content,
  636. tr::lng_edit_lastseen_subscribe_about());
  637. }
  638. result->toggleOn(rpl::combine(
  639. _option.value(),
  640. _exceptionsNever.value(),
  641. (_1 != Option::Everyone) || (_2 > 0)));
  642. return result;
  643. }
  644. void LastSeenPrivacyController::handleExceptionsChange(
  645. Exception exception,
  646. rpl::producer<int> value) {
  647. if (exception == Exception::Never) {
  648. _exceptionsNever = std::move(value);
  649. }
  650. }
  651. void LastSeenPrivacyController::confirmSave(
  652. bool someAreDisallowed,
  653. Fn<void()> saveCallback) {
  654. if (someAreDisallowed
  655. && !Core::App().settings().lastSeenWarningSeen()) {
  656. auto callback = [
  657. =,
  658. saveCallback = std::move(saveCallback)
  659. ](Fn<void()> &&close) {
  660. close();
  661. saveCallback();
  662. Core::App().settings().setLastSeenWarningSeen(true);
  663. Core::App().saveSettingsDelayed();
  664. };
  665. auto box = Ui::MakeConfirmBox({
  666. .text = tr::lng_edit_privacy_lastseen_warning(),
  667. .confirmed = std::move(callback),
  668. .confirmText = tr::lng_continue(),
  669. });
  670. Ui::show(std::move(box), Ui::LayerOption::KeepOther);
  671. } else {
  672. saveCallback();
  673. }
  674. }
  675. void LastSeenPrivacyController::saveAdditional() {
  676. if (_option.current() == Option::Everyone
  677. && !_exceptionsNever.current()) {
  678. return;
  679. }
  680. const auto privacy = &_session->api().globalPrivacy();
  681. if (privacy->hideReadTimeCurrent() != _hideReadTime) {
  682. privacy->updateHideReadTime(_hideReadTime);
  683. }
  684. }
  685. UserPrivacy::Key GroupsInvitePrivacyController::key() const {
  686. return Key::Invites;
  687. }
  688. rpl::producer<QString> GroupsInvitePrivacyController::title() const {
  689. return tr::lng_edit_privacy_groups_title();
  690. }
  691. rpl::producer<QString> GroupsInvitePrivacyController::optionsTitleKey(
  692. ) const {
  693. return tr::lng_edit_privacy_groups_header();
  694. }
  695. auto GroupsInvitePrivacyController::exceptionButtonTextKey(
  696. Exception exception) const
  697. -> rpl::producer<QString> {
  698. switch (exception) {
  699. case Exception::Always:
  700. return tr::lng_edit_privacy_groups_always_empty();
  701. case Exception::Never:
  702. return tr::lng_edit_privacy_groups_never_empty();
  703. }
  704. Unexpected("Invalid exception value.");
  705. }
  706. rpl::producer<QString> GroupsInvitePrivacyController::exceptionBoxTitle(
  707. Exception exception) const {
  708. switch (exception) {
  709. case Exception::Always:
  710. return tr::lng_edit_privacy_groups_always_title();
  711. case Exception::Never:
  712. return tr::lng_edit_privacy_groups_never_title();
  713. }
  714. Unexpected("Invalid exception value.");
  715. }
  716. auto GroupsInvitePrivacyController::exceptionsDescription() const
  717. -> rpl::producer<QString> {
  718. return tr::lng_edit_privacy_groups_exceptions();
  719. }
  720. bool GroupsInvitePrivacyController::allowPremiumsToggle(
  721. Exception exception) const {
  722. return (exception == Exception::Always);
  723. }
  724. UserPrivacy::Key CallsPrivacyController::key() const {
  725. return Key::Calls;
  726. }
  727. rpl::producer<QString> CallsPrivacyController::title() const {
  728. return tr::lng_edit_privacy_calls_title();
  729. }
  730. rpl::producer<QString> CallsPrivacyController::optionsTitleKey() const {
  731. return tr::lng_edit_privacy_calls_header();
  732. }
  733. rpl::producer<QString> CallsPrivacyController::exceptionButtonTextKey(
  734. Exception exception) const {
  735. switch (exception) {
  736. case Exception::Always:
  737. return tr::lng_edit_privacy_calls_always_empty();
  738. case Exception::Never:
  739. return tr::lng_edit_privacy_calls_never_empty();
  740. }
  741. Unexpected("Invalid exception value.");
  742. }
  743. rpl::producer<QString> CallsPrivacyController::exceptionBoxTitle(
  744. Exception exception) const {
  745. switch (exception) {
  746. case Exception::Always:
  747. return tr::lng_edit_privacy_calls_always_title();
  748. case Exception::Never: return tr::lng_edit_privacy_calls_never_title();
  749. }
  750. Unexpected("Invalid exception value.");
  751. }
  752. auto CallsPrivacyController::exceptionsDescription() const
  753. -> rpl::producer<QString>{
  754. return tr::lng_edit_privacy_calls_exceptions();
  755. }
  756. object_ptr<Ui::RpWidget> CallsPrivacyController::setupBelowWidget(
  757. not_null<Window::SessionController*> controller,
  758. not_null<QWidget*> parent,
  759. rpl::producer<Option> option) {
  760. auto result = object_ptr<Ui::VerticalLayout>(parent);
  761. const auto content = result.data();
  762. Ui::AddSkip(content, st::settingsPeerToPeerSkip);
  763. Ui::AddSubsectionTitle(
  764. content,
  765. tr::lng_settings_calls_peer_to_peer_title());
  766. Settings::AddPrivacyButton(
  767. controller,
  768. content,
  769. tr::lng_settings_calls_peer_to_peer_button(),
  770. { &st::menuIconNetwork },
  771. UserPrivacy::Key::CallsPeer2Peer,
  772. [] { return std::make_unique<CallsPeer2PeerPrivacyController>(); },
  773. &st::settingsButton);
  774. Ui::AddSkip(content);
  775. return result;
  776. }
  777. UserPrivacy::Key CallsPeer2PeerPrivacyController::key() const {
  778. return Key::CallsPeer2Peer;
  779. }
  780. rpl::producer<QString> CallsPeer2PeerPrivacyController::title() const {
  781. return tr::lng_edit_privacy_calls_p2p_title();
  782. }
  783. auto CallsPeer2PeerPrivacyController::optionsTitleKey() const
  784. -> rpl::producer<QString> {
  785. return tr::lng_edit_privacy_calls_p2p_header();
  786. }
  787. QString CallsPeer2PeerPrivacyController::optionLabel(
  788. EditPrivacyBox::Option option) const {
  789. switch (option) {
  790. case Option::Everyone:
  791. return tr::lng_edit_privacy_calls_p2p_everyone(tr::now);
  792. case Option::Contacts:
  793. return tr::lng_edit_privacy_calls_p2p_contacts(tr::now);
  794. case Option::CloseFriends:
  795. return tr::lng_edit_privacy_close_friends(tr::now); // unused
  796. case Option::Nobody:
  797. return tr::lng_edit_privacy_calls_p2p_nobody(tr::now);
  798. }
  799. Unexpected("Option value in optionsLabelKey.");
  800. }
  801. auto CallsPeer2PeerPrivacyController::warning() const
  802. -> rpl::producer<TextWithEntities> {
  803. return tr::lng_settings_peer_to_peer_about(Ui::Text::WithEntities);
  804. }
  805. auto CallsPeer2PeerPrivacyController::exceptionButtonTextKey(
  806. Exception exception) const
  807. -> rpl::producer<QString> {
  808. switch (exception) {
  809. case Exception::Always: {
  810. return tr::lng_edit_privacy_calls_p2p_always_empty();
  811. };
  812. case Exception::Never: {
  813. return tr::lng_edit_privacy_calls_p2p_never_empty();
  814. };
  815. }
  816. Unexpected("Invalid exception value.");
  817. }
  818. rpl::producer<QString> CallsPeer2PeerPrivacyController::exceptionBoxTitle(
  819. Exception exception) const {
  820. switch (exception) {
  821. case Exception::Always: {
  822. return tr::lng_edit_privacy_calls_p2p_always_title();
  823. };
  824. case Exception::Never: {
  825. return tr::lng_edit_privacy_calls_p2p_never_title();
  826. };
  827. }
  828. Unexpected("Invalid exception value.");
  829. }
  830. auto CallsPeer2PeerPrivacyController::exceptionsDescription() const
  831. -> rpl::producer<QString>{
  832. return tr::lng_edit_privacy_calls_p2p_exceptions();
  833. }
  834. ForwardsPrivacyController::ForwardsPrivacyController(
  835. not_null<Window::SessionController*> controller)
  836. : SimpleElementDelegate(controller, [] {})
  837. , _controller(controller)
  838. , _chatStyle(
  839. std::make_unique<Ui::ChatStyle>(
  840. controller->session().colorIndicesValue())) {
  841. _chatStyle->apply(controller->defaultChatTheme().get());
  842. }
  843. UserPrivacy::Key ForwardsPrivacyController::key() const {
  844. return Key::Forwards;
  845. }
  846. rpl::producer<QString> ForwardsPrivacyController::title() const {
  847. return tr::lng_edit_privacy_forwards_title();
  848. }
  849. rpl::producer<QString> ForwardsPrivacyController::optionsTitleKey() const {
  850. return tr::lng_edit_privacy_forwards_header();
  851. }
  852. auto ForwardsPrivacyController::warning() const
  853. -> rpl::producer<TextWithEntities> {
  854. return tr::lng_edit_privacy_forwards_warning(Ui::Text::WithEntities);
  855. }
  856. rpl::producer<QString> ForwardsPrivacyController::exceptionButtonTextKey(
  857. Exception exception) const {
  858. switch (exception) {
  859. case Exception::Always: {
  860. return tr::lng_edit_privacy_forwards_always_empty();
  861. };
  862. case Exception::Never: {
  863. return tr::lng_edit_privacy_forwards_never_empty();
  864. };
  865. }
  866. Unexpected("Invalid exception value.");
  867. }
  868. rpl::producer<QString> ForwardsPrivacyController::exceptionBoxTitle(
  869. Exception exception) const {
  870. switch (exception) {
  871. case Exception::Always: {
  872. return tr::lng_edit_privacy_forwards_always_title();
  873. };
  874. case Exception::Never: {
  875. return tr::lng_edit_privacy_forwards_never_title();
  876. };
  877. }
  878. Unexpected("Invalid exception value.");
  879. }
  880. auto ForwardsPrivacyController::exceptionsDescription() const
  881. -> rpl::producer<QString> {
  882. return tr::lng_edit_privacy_forwards_exceptions();
  883. }
  884. object_ptr<Ui::RpWidget> ForwardsPrivacyController::setupAboveWidget(
  885. not_null<Window::SessionController*> controller,
  886. not_null<QWidget*> parent,
  887. rpl::producer<Option> optionValue,
  888. not_null<QWidget*> outerContainer) {
  889. using namespace rpl::mappers;
  890. auto message = GenerateForwardedItem(
  891. delegate(),
  892. controller->session().data().history(
  893. PeerData::kServiceNotificationsId),
  894. tr::lng_edit_privacy_forwards_sample_message(tr::now));
  895. const auto view = message.get();
  896. auto result = object_ptr<Ui::PaddingWrap<Ui::RpWidget>>(
  897. parent,
  898. object_ptr<Ui::RpWidget>(parent),
  899. style::margins(
  900. 0,
  901. st::defaultVerticalListSkip,
  902. 0,
  903. st::settingsPrivacySkipTop));
  904. const auto widget = result->entity();
  905. struct State {
  906. AdminLog::OwnedItem item;
  907. Option option = {};
  908. base::unique_qptr<Ui::RpWidget> tooltip;
  909. ForwardedTooltip info;
  910. Fn<void()> refreshGeometry;
  911. };
  912. const auto state = widget->lifetime().make_state<State>();
  913. state->item = std::move(message);
  914. state->tooltip = base::make_unique_q<Ui::RpWidget>(outerContainer);
  915. state->tooltip->paintRequest(
  916. ) | rpl::start_with_next([=] {
  917. if (state->info.paint) {
  918. auto p = QPainter(state->tooltip.get());
  919. state->info.paint(p);
  920. }
  921. }, state->tooltip->lifetime());
  922. state->refreshGeometry = [=] {
  923. state->tooltip->show();
  924. state->tooltip->raise();
  925. auto position = state->info.geometry.topLeft();
  926. auto parent = (QWidget*)widget;
  927. while (parent && parent != outerContainer) {
  928. position += parent->pos();
  929. parent = parent->parentWidget();
  930. }
  931. state->tooltip->move(position);
  932. };
  933. const auto watch = [&](QWidget *widget, const auto &self) -> void {
  934. if (!widget) {
  935. return;
  936. }
  937. base::install_event_filter(state->tooltip, widget, [=](
  938. not_null<QEvent*> e) {
  939. if (e->type() == QEvent::Move
  940. || e->type() == QEvent::Show
  941. || e->type() == QEvent::ShowToParent
  942. || e->type() == QEvent::ZOrderChange) {
  943. state->refreshGeometry();
  944. }
  945. return base::EventFilterResult::Continue;
  946. });
  947. if (widget == outerContainer) {
  948. return;
  949. }
  950. self(widget->parentWidget(), self);
  951. };
  952. watch(widget, watch);
  953. const auto padding = st::settingsForwardPrivacyPadding;
  954. widget->widthValue(
  955. ) | rpl::filter(
  956. _1 >= (st::historyMinimalWidth / 2)
  957. ) | rpl::start_with_next([=](int width) {
  958. const auto height = view->resizeGetHeight(width);
  959. const auto top = view->marginTop();
  960. const auto bottom = view->marginBottom();
  961. const auto full = padding + top + height + bottom + padding;
  962. widget->resize(width, full);
  963. }, widget->lifetime());
  964. rpl::combine(
  965. widget->widthValue(),
  966. std::move(optionValue)
  967. ) | rpl::start_with_next([=](int width, Option value) {
  968. state->info = PrepareForwardedTooltip(view, value);
  969. state->tooltip->resize(state->info.geometry.size());
  970. state->refreshGeometry();
  971. state->tooltip->update();
  972. }, state->tooltip->lifetime());
  973. widget->paintRequest(
  974. ) | rpl::start_with_next([=](QRect rect) {
  975. // #TODO themes
  976. Window::SectionWidget::PaintBackground(
  977. controller,
  978. controller->defaultChatTheme().get(), // #TODO themes
  979. widget,
  980. rect);
  981. Painter p(widget);
  982. const auto theme = controller->defaultChatTheme().get();
  983. auto context = theme->preparePaintContext(
  984. _chatStyle.get(),
  985. widget->rect(),
  986. widget->rect(),
  987. controller->isGifPausedAtLeastFor(
  988. Window::GifPauseReason::Layer));
  989. p.translate(padding / 2, padding + view->marginBottom());
  990. context.outbg = view->hasOutLayout();
  991. view->draw(p, context);
  992. }, widget->lifetime());
  993. return result;
  994. }
  995. auto ForwardsPrivacyController::delegate()
  996. -> not_null<HistoryView::ElementDelegate*> {
  997. return static_cast<HistoryView::ElementDelegate*>(this);
  998. }
  999. HistoryView::Context ForwardsPrivacyController::elementContext() {
  1000. return HistoryView::Context::ContactPreview;
  1001. }
  1002. UserPrivacy::Key ProfilePhotoPrivacyController::key() const {
  1003. return Key::ProfilePhoto;
  1004. }
  1005. rpl::producer<QString> ProfilePhotoPrivacyController::title() const {
  1006. return tr::lng_edit_privacy_profile_photo_title();
  1007. }
  1008. auto ProfilePhotoPrivacyController::optionsTitleKey() const
  1009. -> rpl::producer<QString> {
  1010. return tr::lng_edit_privacy_profile_photo_header();
  1011. }
  1012. object_ptr<Ui::RpWidget> ProfilePhotoPrivacyController::setupAboveWidget(
  1013. not_null<Window::SessionController*> controller,
  1014. not_null<QWidget*> parent,
  1015. rpl::producer<Option> optionValue,
  1016. not_null<QWidget*> outerContainer) {
  1017. _option = std::move(optionValue);
  1018. return nullptr;
  1019. }
  1020. object_ptr<Ui::RpWidget> ProfilePhotoPrivacyController::setupMiddleWidget(
  1021. not_null<Window::SessionController*> controller,
  1022. not_null<QWidget*> parent,
  1023. rpl::producer<Option> optionValue) {
  1024. const auto self = controller->session().user();
  1025. auto widget = object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
  1026. parent,
  1027. object_ptr<Ui::VerticalLayout>(parent));
  1028. const auto container = widget->entity();
  1029. struct State {
  1030. void updatePhoto(QImage &&image, bool local) {
  1031. auto result = image.scaled(
  1032. userpicSize * style::DevicePixelRatio(),
  1033. Qt::KeepAspectRatio,
  1034. Qt::SmoothTransformation);
  1035. result = Images::Round(
  1036. std::move(result),
  1037. ImageRoundRadius::Ellipse);
  1038. result.setDevicePixelRatio(style::DevicePixelRatio());
  1039. (local ? localPhoto : photo) = std::move(result);
  1040. if (local) {
  1041. localOriginal = std::move(image);
  1042. }
  1043. hasPhoto.fire(!localPhoto.isNull() || !photo.isNull());
  1044. }
  1045. rpl::event_stream<bool> hasPhoto;
  1046. rpl::variable<bool> hiddenByUser = false;
  1047. rpl::variable<QString> setUserpicButtonText;
  1048. QSize userpicSize;
  1049. QImage photo;
  1050. QImage localPhoto;
  1051. QImage localOriginal;
  1052. };
  1053. const auto state = container->lifetime().make_state<State>();
  1054. state->userpicSize = QSize(
  1055. st::inviteLinkUserpics.size,
  1056. st::inviteLinkUserpics.size);
  1057. Ui::AddSkip(container);
  1058. const auto setUserpicButton = AddButtonWithIcon(
  1059. container,
  1060. state->setUserpicButtonText.value(),
  1061. st::settingsButtonLight,
  1062. { &st::menuBlueIconPhotoSet });
  1063. const auto &stRemoveButton = st::settingsAttentionButtonWithIcon;
  1064. const auto removeButton = container->add(
  1065. object_ptr<Ui::SlideWrap<Ui::SettingsButton>>(
  1066. container,
  1067. object_ptr<Ui::SettingsButton>(
  1068. parent,
  1069. tr::lng_edit_privacy_profile_photo_public_remove(),
  1070. stRemoveButton)));
  1071. Ui::AddSkip(container);
  1072. Ui::AddDividerText(
  1073. container,
  1074. tr::lng_edit_privacy_profile_photo_public_about());
  1075. const auto userpic = Ui::CreateChild<Ui::RpWidget>(
  1076. removeButton->entity());
  1077. userpic->resize(state->userpicSize);
  1078. userpic->paintRequest(
  1079. ) | rpl::start_with_next([=](const QRect &r) {
  1080. auto p = QPainter(userpic);
  1081. p.fillRect(r, Qt::transparent);
  1082. if (!state->localPhoto.isNull()) {
  1083. p.drawImage(0, 0, state->localPhoto);
  1084. } else if (!state->photo.isNull()) {
  1085. p.drawImage(0, 0, state->photo);
  1086. }
  1087. }, userpic->lifetime());
  1088. removeButton->entity()->heightValue(
  1089. ) | rpl::start_with_next([=,
  1090. left = stRemoveButton.iconLeft,
  1091. width = st::menuBlueIconPhotoSet.width()](int height) {
  1092. userpic->moveToLeft(
  1093. left + (width - userpic->width()) / 2,
  1094. (height - userpic->height()) / 2);
  1095. }, userpic->lifetime());
  1096. removeButton->toggleOn(rpl::combine(
  1097. state->hasPhoto.events_starting_with(false),
  1098. state->hiddenByUser.value()
  1099. ) | rpl::map(rpl::mappers::_1 && !rpl::mappers::_2));
  1100. (
  1101. PrepareShortInfoFallbackUserpic(self, st::shortInfoCover).value
  1102. ) | rpl::start_with_next([=](PeerShortInfoUserpic info) {
  1103. state->updatePhoto(base::take(info.photo), false);
  1104. userpic->update();
  1105. }, userpic->lifetime());
  1106. setUserpicButton->setClickedCallback([=] {
  1107. base::call_delayed(
  1108. st::settingsButton.ripple.hideDuration,
  1109. crl::guard(container, [=] {
  1110. using namespace Editor;
  1111. PrepareProfilePhotoFromFile(
  1112. container,
  1113. &controller->window(),
  1114. {
  1115. .confirm = tr::lng_profile_set_photo_button(
  1116. tr::now),
  1117. .cropType = EditorData::CropType::Ellipse,
  1118. .keepAspectRatio = true,
  1119. },
  1120. [=](QImage &&image) {
  1121. state->updatePhoto(std::move(image), true);
  1122. state->hiddenByUser = false;
  1123. userpic->update();
  1124. });
  1125. }));
  1126. });
  1127. removeButton->entity()->setClickedCallback([=] {
  1128. state->hiddenByUser = true;
  1129. });
  1130. state->setUserpicButtonText = removeButton->toggledValue(
  1131. ) | rpl::map([](bool toggled) {
  1132. return !toggled
  1133. ? tr::lng_edit_privacy_profile_photo_public_set()
  1134. : tr::lng_edit_privacy_profile_photo_public_update();
  1135. }) | rpl::flatten_latest();
  1136. _saveAdditional = [=] {
  1137. if (removeButton->isHidden()) {
  1138. if (const auto photoId = SyncUserFallbackPhotoViewer(self)) {
  1139. if (const auto photo = self->owner().photo(*photoId)) {
  1140. controller->session().api().peerPhoto().clear(photo);
  1141. }
  1142. }
  1143. } else if (!state->localOriginal.isNull()) {
  1144. controller->session().api().peerPhoto().uploadFallback(
  1145. self,
  1146. { base::take(state->localOriginal) });
  1147. }
  1148. };
  1149. widget->toggleOn(rpl::combine(
  1150. std::move(optionValue),
  1151. _exceptionsNever.value()
  1152. ) | rpl::map(rpl::mappers::_1 != Option::Everyone || rpl::mappers::_2));
  1153. return widget;
  1154. }
  1155. void ProfilePhotoPrivacyController::saveAdditional() {
  1156. if (_saveAdditional) {
  1157. _saveAdditional();
  1158. }
  1159. }
  1160. auto ProfilePhotoPrivacyController::exceptionButtonTextKey(
  1161. Exception exception) const
  1162. -> rpl::producer<QString> {
  1163. switch (exception) {
  1164. case Exception::Always: {
  1165. return tr::lng_edit_privacy_profile_photo_always_empty();
  1166. };
  1167. case Exception::Never: {
  1168. return tr::lng_edit_privacy_profile_photo_never_empty();
  1169. };
  1170. }
  1171. Unexpected("Invalid exception value.");
  1172. }
  1173. rpl::producer<QString> ProfilePhotoPrivacyController::exceptionBoxTitle(
  1174. Exception exception) const {
  1175. switch (exception) {
  1176. case Exception::Always: {
  1177. return tr::lng_edit_privacy_profile_photo_always_title();
  1178. };
  1179. case Exception::Never: {
  1180. return tr::lng_edit_privacy_profile_photo_never_title();
  1181. };
  1182. }
  1183. Unexpected("Invalid exception value.");
  1184. }
  1185. auto ProfilePhotoPrivacyController::exceptionsDescription() const
  1186. -> rpl::producer<QString> {
  1187. return _option.value(
  1188. ) | rpl::map([](Option option) {
  1189. switch (option) {
  1190. case Option::Everyone:
  1191. return tr::lng_edit_privacy_forwards_exceptions_everyone();
  1192. case Option::Contacts:
  1193. case Option::CloseFriends:
  1194. return tr::lng_edit_privacy_forwards_exceptions();
  1195. case Option::Nobody:
  1196. return tr::lng_edit_privacy_forwards_exceptions_nobody();
  1197. }
  1198. Unexpected("Option value in exceptionsDescription.");
  1199. }) | rpl::flatten_latest();
  1200. }
  1201. void ProfilePhotoPrivacyController::handleExceptionsChange(
  1202. Exception exception,
  1203. rpl::producer<int> value) {
  1204. if (exception == Exception::Never) {
  1205. _exceptionsNever = std::move(value);
  1206. }
  1207. }
  1208. VoicesPrivacyController::VoicesPrivacyController(
  1209. not_null<::Main::Session*> session) {
  1210. Data::AmPremiumValue(
  1211. session
  1212. ) | rpl::start_with_next([=](bool premium) {
  1213. if (!premium) {
  1214. if (const auto box = view()) {
  1215. box->closeBox();
  1216. }
  1217. }
  1218. }, _lifetime);
  1219. }
  1220. UserPrivacy::Key VoicesPrivacyController::key() const {
  1221. return Key::Voices;
  1222. }
  1223. rpl::producer<QString> VoicesPrivacyController::title() const {
  1224. return tr::lng_edit_privacy_voices_title();
  1225. }
  1226. rpl::producer<QString> VoicesPrivacyController::optionsTitleKey() const {
  1227. return tr::lng_edit_privacy_voices_header();
  1228. }
  1229. rpl::producer<QString> VoicesPrivacyController::exceptionButtonTextKey(
  1230. Exception exception) const {
  1231. switch (exception) {
  1232. case Exception::Always:
  1233. return tr::lng_edit_privacy_voices_always_empty();
  1234. case Exception::Never:
  1235. return tr::lng_edit_privacy_voices_never_empty();
  1236. }
  1237. Unexpected("Invalid exception value.");
  1238. }
  1239. rpl::producer<QString> VoicesPrivacyController::exceptionBoxTitle(
  1240. Exception exception) const {
  1241. switch (exception) {
  1242. case Exception::Always:
  1243. return tr::lng_edit_privacy_voices_always_title();
  1244. case Exception::Never: return tr::lng_edit_privacy_voices_never_title();
  1245. }
  1246. Unexpected("Invalid exception value.");
  1247. }
  1248. auto VoicesPrivacyController::exceptionsDescription() const
  1249. -> rpl::producer<QString> {
  1250. return tr::lng_edit_privacy_voices_exceptions();
  1251. }
  1252. object_ptr<Ui::RpWidget> VoicesPrivacyController::setupBelowWidget(
  1253. not_null<Window::SessionController*> controller,
  1254. not_null<QWidget*> parent,
  1255. rpl::producer<Option> option) {
  1256. using namespace rpl::mappers;
  1257. auto result = object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
  1258. parent,
  1259. object_ptr<Ui::VerticalLayout>(parent));
  1260. result->toggleOn(
  1261. Data::AmPremiumValue(&controller->session()) | rpl::map(!_1),
  1262. anim::type::instant);
  1263. const auto content = result->entity();
  1264. Ui::AddSkip(content);
  1265. Settings::AddButtonWithIcon(
  1266. content,
  1267. tr::lng_messages_privacy_premium_button(),
  1268. st::messagePrivacySubscribe,
  1269. { .icon = &st::menuBlueIconPremium }
  1270. )->setClickedCallback([=] {
  1271. Settings::ShowPremium(
  1272. controller,
  1273. u"voice_restrictions_require_premium"_q);
  1274. });
  1275. Ui::AddSkip(content);
  1276. Ui::AddDividerText(content, tr::lng_messages_privacy_premium_about());
  1277. return result;
  1278. }
  1279. Fn<void()> VoicesPrivacyController::premiumClickedCallback(
  1280. Option option,
  1281. not_null<Window::SessionController*> controller) {
  1282. if (option == Option::Everyone) {
  1283. return nullptr;
  1284. }
  1285. const auto showToast = [=] {
  1286. auto link = Ui::Text::Link(
  1287. Ui::Text::Semibold(
  1288. tr::lng_settings_privacy_premium_link(tr::now)));
  1289. _toastInstance = controller->showToast({
  1290. .text = tr::lng_settings_privacy_premium(
  1291. tr::now,
  1292. lt_link,
  1293. link,
  1294. Ui::Text::WithEntities),
  1295. .filter = crl::guard(&controller->session(), [=](
  1296. const ClickHandlerPtr &,
  1297. Qt::MouseButton button) {
  1298. if (button == Qt::LeftButton) {
  1299. if (const auto strong = _toastInstance.get()) {
  1300. strong->hideAnimated();
  1301. _toastInstance = nullptr;
  1302. Settings::ShowPremium(
  1303. controller,
  1304. u"voice_restrictions_require_premium"_q);
  1305. return true;
  1306. }
  1307. }
  1308. return false;
  1309. }),
  1310. .duration = Ui::Toast::kDefaultDuration * 2,
  1311. });
  1312. };
  1313. return showToast;
  1314. }
  1315. UserPrivacy::Key AboutPrivacyController::key() const {
  1316. return Key::About;
  1317. }
  1318. rpl::producer<QString> AboutPrivacyController::title() const {
  1319. return tr::lng_edit_privacy_about_title();
  1320. }
  1321. rpl::producer<QString> AboutPrivacyController::optionsTitleKey() const {
  1322. return tr::lng_edit_privacy_about_header();
  1323. }
  1324. rpl::producer<QString> AboutPrivacyController::exceptionButtonTextKey(
  1325. Exception exception) const {
  1326. switch (exception) {
  1327. case Exception::Always:
  1328. return tr::lng_edit_privacy_about_always_empty();
  1329. case Exception::Never: return tr::lng_edit_privacy_about_never_empty();
  1330. }
  1331. Unexpected("Invalid exception value.");
  1332. }
  1333. rpl::producer<QString> AboutPrivacyController::exceptionBoxTitle(
  1334. Exception exception) const {
  1335. switch (exception) {
  1336. case Exception::Always:
  1337. return tr::lng_edit_privacy_about_always_title();
  1338. case Exception::Never: return tr::lng_edit_privacy_about_never_title();
  1339. }
  1340. Unexpected("Invalid exception value.");
  1341. }
  1342. auto AboutPrivacyController::exceptionsDescription() const
  1343. -> rpl::producer<QString> {
  1344. return tr::lng_edit_privacy_about_exceptions();
  1345. }
  1346. UserPrivacy::Key BirthdayPrivacyController::key() const {
  1347. return Key::Birthday;
  1348. }
  1349. rpl::producer<QString> BirthdayPrivacyController::title() const {
  1350. return tr::lng_edit_privacy_birthday_title();
  1351. }
  1352. rpl::producer<QString> BirthdayPrivacyController::optionsTitleKey() const {
  1353. return tr::lng_edit_privacy_birthday_header();
  1354. }
  1355. rpl::producer<QString> BirthdayPrivacyController::exceptionButtonTextKey(
  1356. Exception exception) const {
  1357. switch (exception) {
  1358. case Exception::Always:
  1359. return tr::lng_edit_privacy_birthday_always_empty();
  1360. case Exception::Never:
  1361. return tr::lng_edit_privacy_birthday_never_empty();
  1362. }
  1363. Unexpected("Invalid exception value.");
  1364. }
  1365. rpl::producer<QString> BirthdayPrivacyController::exceptionBoxTitle(
  1366. Exception exception) const {
  1367. switch (exception) {
  1368. case Exception::Always:
  1369. return tr::lng_edit_privacy_birthday_always_title();
  1370. case Exception::Never:
  1371. return tr::lng_edit_privacy_birthday_never_title();
  1372. }
  1373. Unexpected("Invalid exception value.");
  1374. }
  1375. auto BirthdayPrivacyController::exceptionsDescription() const
  1376. -> rpl::producer<QString> {
  1377. return tr::lng_edit_privacy_birthday_exceptions();
  1378. }
  1379. object_ptr<Ui::RpWidget> BirthdayPrivacyController::setupAboveWidget(
  1380. not_null<Window::SessionController*> controller,
  1381. not_null<QWidget*> parent,
  1382. rpl::producer<Option> optionValue,
  1383. not_null<QWidget*> outerContainer) {
  1384. const auto session = &controller->session();
  1385. const auto user = session->user();
  1386. auto result = object_ptr<Ui::SlideWrap<Ui::DividerLabel>>(
  1387. parent,
  1388. object_ptr<Ui::DividerLabel>(
  1389. parent,
  1390. object_ptr<Ui::FlatLabel>(
  1391. parent,
  1392. tr::lng_edit_privacy_birthday_yet(
  1393. lt_link,
  1394. tr::lng_edit_privacy_birthday_yet_link(
  1395. ) | Ui::Text::ToLink("internal:edit_birthday"),
  1396. Ui::Text::WithEntities),
  1397. st::boxDividerLabel),
  1398. st::defaultBoxDividerLabelPadding));
  1399. result->toggleOn(session->changes().peerFlagsValue(
  1400. user,
  1401. Data::PeerUpdate::Flag::Birthday
  1402. ) | rpl::map([=] {
  1403. return !user->birthday();
  1404. }));
  1405. result->finishAnimating();
  1406. return result;
  1407. }
  1408. UserPrivacy::Key GiftsAutoSavePrivacyController::key() const {
  1409. return Key::GiftsAutoSave;
  1410. }
  1411. rpl::producer<QString> GiftsAutoSavePrivacyController::title() const {
  1412. return tr::lng_edit_privacy_gifts_title();
  1413. }
  1414. auto GiftsAutoSavePrivacyController::optionsTitleKey() const
  1415. -> rpl::producer<QString> {
  1416. return tr::lng_edit_privacy_gifts_header();
  1417. }
  1418. auto GiftsAutoSavePrivacyController::exceptionButtonTextKey(
  1419. Exception exception) const
  1420. -> rpl::producer<QString> {
  1421. switch (exception) {
  1422. case Exception::Always:
  1423. return tr::lng_edit_privacy_gifts_always_empty();
  1424. case Exception::Never:
  1425. return tr::lng_edit_privacy_gifts_never_empty();
  1426. }
  1427. Unexpected("Invalid exception value.");
  1428. }
  1429. rpl::producer<QString> GiftsAutoSavePrivacyController::exceptionBoxTitle(
  1430. Exception exception) const {
  1431. switch (exception) {
  1432. case Exception::Always:
  1433. return tr::lng_edit_privacy_gifts_always_title();
  1434. case Exception::Never: return tr::lng_edit_privacy_gifts_never_title();
  1435. }
  1436. Unexpected("Invalid exception value.");
  1437. }
  1438. auto GiftsAutoSavePrivacyController::exceptionsDescription() const
  1439. -> rpl::producer<QString> {
  1440. return tr::lng_edit_privacy_lastseen_exceptions();
  1441. }
  1442. bool GiftsAutoSavePrivacyController::allowMiniAppsToggle(
  1443. Exception exception) const {
  1444. return true;
  1445. }
  1446. } // namespace Settings