From 7b307a9e542174a192b1acc910f64d35381ab631 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Wed, 6 Apr 2022 14:43:39 +0300 Subject: [PATCH] Added ability to save audio from chat as custom notification sound. --- Telegram/Resources/icons/menu/sound_add.png | Bin 0 -> 675 bytes .../Resources/icons/menu/sound_add@2x.png | Bin 0 -> 1254 bytes .../Resources/icons/menu/sound_add@3x.png | Bin 0 -> 1853 bytes Telegram/Resources/langs/lang.strings | 2 + Telegram/SourceFiles/boxes/ringtones_box.cpp | 20 +++++++-- .../history/history_inner_widget.cpp | 4 ++ .../view/history_view_context_menu.cpp | 42 ++++++++++++++++++ .../history/view/history_view_context_menu.h | 4 ++ Telegram/SourceFiles/ui/menu_icons.style | 1 + 9 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 Telegram/Resources/icons/menu/sound_add.png create mode 100644 Telegram/Resources/icons/menu/sound_add@2x.png create mode 100644 Telegram/Resources/icons/menu/sound_add@3x.png diff --git a/Telegram/Resources/icons/menu/sound_add.png b/Telegram/Resources/icons/menu/sound_add.png new file mode 100644 index 0000000000000000000000000000000000000000..f2d02e3d3ae10c48c5ce9de932ee5b534c28ca82 GIT binary patch literal 675 zcmeAS@N?(olHy`uVBq!ia0vp^5+KaM1SIoCSFHz9jKx9jP7LeL$-D$|Tv8)E(|mmy zw18|52FCVG1{RPKAeI7R1_tH@j10^`nh_+nfC(-uv49!D1}S`GTDq2jfr;1C#WBP} z@M-Yg*BcW>{{2jIJo2P?TI(T&*`Gqix;-V2q*Q38e#%b}K6&KOBXbWn#gp!A+@4cZ zBq#KLfA#&!D zq&1a=i81ll-+y=WmIr8XeEw=K)oUijJB$D3!i|?-uDSlY#;)FKE>j~zud{i)8k47n zNZ9JolHC3Gqt}KRy~*9|$;Kk?%DgbcuWb_5RvO=pUL_lck{|6 z@`d~FZ;#Nq^85Z;4q*wt_PKuQP1####pq4uHYJ;z+e3AB=s;eX-m;gnJ@SAwtxQFbAQtL^;feB`?ss#$l5BEW*||+7F<2WYbg&~bKLsl zLY6Xov(KtcKFOgq`J{{D_19AkKB~`XKG-npMd$6eW@eMO%s%_9Xs62AqMbZ@@9*Pd zaGO(dk#QSil#7y}j7FW^eD%qmlUN>ZjCk|;^#b=NT@zc>4`?q~@%W>~T)&MmN0TNV zO|rea--$=7@nAxNRfyKr6eCH#_K@rUd-gL=?!5J@xGFmtlt4UP{an^LB{Ts5!*LoL literal 0 HcmV?d00001 diff --git a/Telegram/Resources/icons/menu/sound_add@2x.png b/Telegram/Resources/icons/menu/sound_add@2x.png new file mode 100644 index 0000000000000000000000000000000000000000..a310c1bd810452f53ec5996faf5ee6f011e9db1b GIT binary patch literal 1254 zcmVPx#L}ge>W=%~1DgXcg2mk?xX#fNO00031000^Q000001E2u_0{{R30RRC20H6W@ z1ONa40RR91FrWhf1ONa40RR91FaQ7m0NXcg3;+NGP)S5VR9Fe^m`f<_Ulhl$_cMir zydMcsxJcxc0m6)d$H0I*N(@Lw@)$6nh>226h+-^{5rvS#z@wBm6Xg+k-%tNt`=0yz z9p@b9f3C?rd+=-Rwbowid)8ii?X}O3ALe0>fH?xjM<6OHs=3@bK`-$;pX{3B#?K22fB?P+VMG ze0)4GAl%*EC4$Jv$nOATZEc;Mot>JRihDq4-G!^GtBs9~-rimb&Je-=r!%@>#L~) zXci6(3`nMxm6ey5mz+WvbL<@*9a(vDa-wNSO*b?&pff)|@9OHRmck+5_xJZtPft@) zQlx@0faGG`1i?TdD=TYtbu}z3Ore~Ule4_M92^{MX=(ZT`l^tAZF+ckh_jB44&BQv zEG#I>Xfm#^uR$iatgfyO5bz%!9tsN!b=%P|H4u<6R8ms%`1pwLmzS5Lqa%o|tu4L` zjv*yOzdyYiUjl@+q@*M&f&d}EM9as=NA2q`=8$x@va*txm`GqGXL@>?qH0{Bl16?L zUyYDh>Fn&>*VnhXxmjOdPkQ|}_a|jcVf@pAp|kt@d!Xpm{*Eov3qn)jy8+qQ*l4;} z)87h+{`KbOCOZrtM@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Z-xU;>zkK zA3u($o}iNDb7zRbZLg`RAy#m3pFDZ;^5x49A3i{7gwqh!NJ>gFF)_iXMEZh-Arl5s z-o(Vj!otGJ%1UIhN=r+#va)dKxz3DR5oZYrW!iO}ot@s^-eU21^CR_4FccJRMe%@| zYp@?4J$i&VJ6{r&p(x&kjKspi!l)yxpN)-;W|2llh#VrgPv9Rver$%+vL1n<>FH?_ zuvy{7ix<=+5v3|FE+!ZvQ_?!&0Y+d1q@=;hT5sIAAr3!a>2S}GNO3`eSbcpxhyV8N z8;dFwAi(pP1|O*maNXn1(IY&U*@ zT4Kd;eSP}$iAF}O#?jGHJUAlwbQ%GMk3#pJ7_6-3!Gi}R9!{!kXC!F}FF_3BoIiga z2aLcl4=|OuFkl05r{QWYDk_4hAOLow^nkLu{QeMMskl%G0 zjV*~26mH(U`Mam?rReBr5|EddCp{nnfC!Iqc%<;8!CWhnS}?ODLStcKVgiifSn_8c47UIJ15PMeP$E`A1x;*2NxRkr2-;~h~NuS^^C*H!w@AWN!axF@81_& zd>k13Zv(;L(Q&y+`O$fZ)2B~gy?XWb?b~}dI2^Fi zA3uJanwr9W0G|k#IJF|Ii4;4m2-O&F;o!l82%6J^NEF~ABqSslE)%h4IBW$41^fi5 zm>zC$jjs>!Xa-k<8#6aI7g=ro6@;fmRaF(s^}Wq}WX_mIZQudG{e_1GTTH2bjYzdp rodI$6b literal 0 HcmV?d00001 diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index 8e05e06fa..9d1c4e6c5 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -1891,6 +1891,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_context_set_as_quick" = "Set As Quick"; "lng_context_delete_from_disk" = "Delete from disk"; "lng_context_delete_all_files" = "Delete all files"; +"lng_context_save_custom_sound" = "Save for notifications"; "lng_downloads_section" = "Downloads"; "lng_downloads_view_in_chat" = "View in chat"; @@ -3128,6 +3129,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_ringtones_box_default" = "Default"; "lng_ringtones_box_no_sound" = "No sound"; "lng_ringtones_box_error" = "Sorry, but your file is too big."; +"lng_ringtones_toast_added" = "Sound added!"; // Wnd specific diff --git a/Telegram/SourceFiles/boxes/ringtones_box.cpp b/Telegram/SourceFiles/boxes/ringtones_box.cpp index 6139feafa..e7d65fb0d 100644 --- a/Telegram/SourceFiles/boxes/ringtones_box.cpp +++ b/Telegram/SourceFiles/boxes/ringtones_box.cpp @@ -12,6 +12,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "base/base_file_utilities.h" #include "base/call_delayed.h" #include "base/event_filter.h" +#include "base/unixtime.h" #include "core/file_utilities.h" #include "core/mime_type.h" #include "data/data_document.h" @@ -135,12 +136,12 @@ void RingtonesBox( addToGroup( container, kDefaultValue, - tr::lng_ringtones_box_default({}), + tr::lng_ringtones_box_default(tr::now), false); addToGroup( container, kNoSoundValue, - tr::lng_ringtones_box_no_sound({}), + tr::lng_ringtones_box_no_sound(tr::now), noSound); const auto custom = container->add( @@ -157,7 +158,20 @@ void RingtonesBox( for (const auto &id : peer->session().api().ringtones().list()) { const auto chosen = (checkedId.id && checkedId.id == id); const auto document = peer->session().data().document(id); - addToGroup(custom, value++, document->filename(), chosen); + const auto text = [&] { + if (!document->filename().isEmpty()) { + return document->filename(); + } + const auto date = langDateTime( + base::unixtime::parse(document->date)); + const auto base = document->isVoiceMessage() + ? (tr::lng_in_dlg_audio(tr::now) + ' ') + : document->isAudioFile() + ? (tr::lng_in_dlg_audio_file(tr::now) + ' ') + : QString(); + return base + date; + }(); + addToGroup(custom, value++, text, chosen); state->documentIds.push_back(id); } diff --git a/Telegram/SourceFiles/history/history_inner_widget.cpp b/Telegram/SourceFiles/history/history_inner_widget.cpp index 5fcc14d3f..a8065e779 100644 --- a/Telegram/SourceFiles/history/history_inner_widget.cpp +++ b/Telegram/SourceFiles/history/history_inner_widget.cpp @@ -2051,6 +2051,10 @@ void HistoryInner::showContextMenu(QContextMenuEvent *e, bool showFromTouch) { }, &st::menuIconShowInFolder); } if (!hasCopyRestriction(item)) { + HistoryView::AddSaveSoundForNotifications( + _menu, + document, + controller); _menu->addAction(lnkIsVideo ? tr::lng_context_save_video(tr::now) : (lnkIsVoice ? tr::lng_context_save_audio(tr::now) : (lnkIsAudio ? tr::lng_context_save_audio_file(tr::now) : tr::lng_context_save_file(tr::now))), App::LambdaDelayed(st::defaultDropdownMenu.menu.ripple.hideDuration, this, [=] { saveDocumentToFile(itemId, document); }), &st::menuIconDownload); diff --git a/Telegram/SourceFiles/history/view/history_view_context_menu.cpp b/Telegram/SourceFiles/history/view/history_view_context_menu.cpp index 32f93e15c..2f46f5967 100644 --- a/Telegram/SourceFiles/history/view/history_view_context_menu.cpp +++ b/Telegram/SourceFiles/history/view/history_view_context_menu.cpp @@ -10,6 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "api/api_attached_stickers.h" #include "api/api_editing.h" #include "api/api_polls.h" +#include "api/api_ringtones.h" #include "api/api_who_reacted.h" #include "api/api_toggling_media.h" // Api::ToggleFavedSticker #include "base/unixtime.h" @@ -68,6 +69,8 @@ namespace HistoryView { namespace { constexpr auto kRescheduleLimit = 20; +constexpr auto kMaxDurationForRingtone = 10; +constexpr auto kMaxSizeForRingtone = 1024 * 500; bool HasEditMessageAction( const ContextMenuRequest &request, @@ -293,6 +296,9 @@ void AddDocumentActions( std::move(callback), &st::menuIconStickers); } + if (!list->hasCopyRestriction(item)) { + AddSaveSoundForNotifications(menu, document, list->controller()); + } AddSaveDocumentAction(menu, item, document, list); } @@ -1070,6 +1076,42 @@ void AddPollActions( } } +void AddSaveSoundForNotifications( + not_null menu, + not_null document, + not_null controller) { + const auto &ringtones = document->session().api().ringtones(); + if (document->size > kMaxSizeForRingtone) { + return; + } else if (ranges::contains(ringtones.list(), document->id)) { + return; + } else if (const auto song = document->song()) { + if (song->duration > kMaxDurationForRingtone) { + return; + } + } else if (const auto voice = document->voice()) { + if (voice->duration > kMaxDurationForRingtone) { + return; + } + } else { + return; + } + const auto toastParent = Window::Show(controller).toastParent(); + menu->addAction(tr::lng_context_save_custom_sound(tr::now), [=] { + document->session().api().request(MTPaccount_SaveRingtone( + document->mtpInput(), + MTP_bool(false) + )).done([=] { + Ui::Toast::Show( + toastParent, + tr::lng_ringtones_toast_added(tr::now)); + }).fail([](const MTP::Error &error) { + LOG(("API Error: Saving ringtone failed with %1 message." + ).arg(error.type())); + }).send(); + }, &st::menuIconSoundAdd); +} + void AddWhoReactedAction( not_null menu, not_null context, diff --git a/Telegram/SourceFiles/history/view/history_view_context_menu.h b/Telegram/SourceFiles/history/view/history_view_context_menu.h index 0861ebb27..814eb60e3 100644 --- a/Telegram/SourceFiles/history/view/history_view_context_menu.h +++ b/Telegram/SourceFiles/history/view/history_view_context_menu.h @@ -60,6 +60,10 @@ void AddPollActions( not_null poll, not_null item, Context context); +void AddSaveSoundForNotifications( + not_null menu, + not_null document, + not_null controller); void AddWhoReactedAction( not_null menu, not_null context, diff --git a/Telegram/SourceFiles/ui/menu_icons.style b/Telegram/SourceFiles/ui/menu_icons.style index fc8f15fc3..3636500ee 100644 --- a/Telegram/SourceFiles/ui/menu_icons.style +++ b/Telegram/SourceFiles/ui/menu_icons.style @@ -108,6 +108,7 @@ menuIconCustomize: icon {{ "menu/customize", menuIconColor }}; menuIconSoundOn: icon {{ "menu/sound_enable", menuIconColor }}; menuIconSoundOff: icon {{ "menu/sound_disable", menuIconColor }}; menuIconSoundSelect: icon {{ "menu/sound_select", menuIconColor }}; +menuIconSoundAdd: icon {{ "menu/sound_add", menuIconColor }}; menuIconFile: icon {{ "menu/file", menuIconColor }}; menuIconPhoto: icon {{ "menu/image", menuIconColor }};