2022-04-04 21:00:58 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "boxes/ringtones_box.h"
|
|
|
|
|
|
|
|
#include "api/api_ringtones.h"
|
|
|
|
#include "apiwrap.h"
|
|
|
|
#include "base/base_file_utilities.h"
|
|
|
|
#include "base/call_delayed.h"
|
|
|
|
#include "base/event_filter.h"
|
2022-04-07 07:34:29 +00:00
|
|
|
#include "base/timer_rpl.h"
|
2022-04-10 19:16:15 +00:00
|
|
|
#include "base/unixtime.h"
|
|
|
|
#include "core/application.h"
|
2022-06-11 00:07:22 +00:00
|
|
|
#include "core/core_settings.h"
|
2022-04-04 21:00:58 +00:00
|
|
|
#include "core/file_utilities.h"
|
|
|
|
#include "core/mime_type.h"
|
|
|
|
#include "data/data_document.h"
|
2022-04-07 07:34:29 +00:00
|
|
|
#include "data/data_document_media.h"
|
2022-04-06 10:15:27 +00:00
|
|
|
#include "data/data_document_resolver.h"
|
2022-10-13 17:34:04 +00:00
|
|
|
#include "data/data_thread.h"
|
2022-04-04 21:00:58 +00:00
|
|
|
#include "data/data_session.h"
|
|
|
|
#include "data/notify/data_notify_settings.h"
|
|
|
|
#include "lang/lang_keys.h"
|
|
|
|
#include "main/main_session.h"
|
2022-04-07 07:34:29 +00:00
|
|
|
#include "media/audio/media_audio.h"
|
2022-04-04 21:00:58 +00:00
|
|
|
#include "settings/settings_common.h"
|
|
|
|
#include "ui/boxes/confirm_box.h"
|
2022-04-10 19:16:15 +00:00
|
|
|
#include "ui/text/format_values.h"
|
2022-04-04 21:00:58 +00:00
|
|
|
#include "ui/widgets/buttons.h"
|
|
|
|
#include "ui/widgets/checkbox.h"
|
|
|
|
#include "ui/widgets/labels.h"
|
|
|
|
#include "ui/widgets/popup_menu.h"
|
|
|
|
#include "ui/widgets/scroll_area.h"
|
|
|
|
#include "ui/wrap/padding_wrap.h"
|
|
|
|
#include "ui/wrap/vertical_layout.h"
|
|
|
|
#include "styles/style_menu_icons.h"
|
|
|
|
#include "styles/style_boxes.h"
|
|
|
|
#include "styles/style_layers.h"
|
2022-05-03 18:11:47 +00:00
|
|
|
#include "styles/style_settings.h"
|
2022-04-04 21:00:58 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kDefaultValue = -1;
|
|
|
|
constexpr auto kNoSoundValue = -2;
|
2022-04-07 07:34:29 +00:00
|
|
|
constexpr auto kNoDetachTimeout = crl::time(250);
|
|
|
|
|
|
|
|
class AudioCreator final {
|
|
|
|
public:
|
|
|
|
AudioCreator();
|
|
|
|
AudioCreator(AudioCreator &&other);
|
|
|
|
~AudioCreator();
|
|
|
|
|
|
|
|
private:
|
|
|
|
rpl::lifetime _lifetime;
|
|
|
|
bool _attached = false;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
AudioCreator::AudioCreator()
|
|
|
|
: _attached(true) {
|
|
|
|
crl::async([] {
|
|
|
|
QMutexLocker lock(Media::Player::internal::audioPlayerMutex());
|
|
|
|
Media::Audio::AttachToDevice();
|
|
|
|
});
|
|
|
|
base::timer_each(
|
|
|
|
kNoDetachTimeout
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
Media::Audio::StopDetachIfNotUsedSafe();
|
|
|
|
}, _lifetime);
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioCreator::AudioCreator(AudioCreator &&other)
|
|
|
|
: _lifetime(base::take(other._lifetime))
|
|
|
|
, _attached(base::take(other._attached)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioCreator::~AudioCreator() {
|
|
|
|
if (_attached) {
|
|
|
|
Media::Audio::ScheduleDetachIfNotUsedSafe();
|
|
|
|
}
|
|
|
|
}
|
2022-04-04 21:00:58 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2022-04-06 15:19:50 +00:00
|
|
|
QString ExtractRingtoneName(not_null<DocumentData*> document) {
|
|
|
|
if (document->isNull()) {
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
const auto name = document->filename();
|
|
|
|
if (!name.isEmpty()) {
|
|
|
|
const auto extension = Data::FileExtension(name);
|
|
|
|
if (extension.isEmpty()) {
|
|
|
|
return name;
|
|
|
|
} else if (name.size() > extension.size() + 1) {
|
|
|
|
return name.mid(0, name.size() - extension.size() - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-04-04 21:00:58 +00:00
|
|
|
void RingtonesBox(
|
|
|
|
not_null<Ui::GenericBox*> box,
|
2022-04-06 15:19:50 +00:00
|
|
|
not_null<Main::Session*> session,
|
|
|
|
Data::NotifySound selected,
|
|
|
|
Fn<void(Data::NotifySound)> save) {
|
2022-04-04 21:00:58 +00:00
|
|
|
box->setTitle(tr::lng_ringtones_box_title());
|
|
|
|
|
|
|
|
const auto container = box->verticalLayout();
|
|
|
|
|
|
|
|
auto padding = st::boxPadding;
|
|
|
|
padding.setTop(padding.bottom());
|
|
|
|
|
|
|
|
struct State {
|
2022-04-07 07:34:29 +00:00
|
|
|
AudioCreator creator;
|
2022-04-04 21:00:58 +00:00
|
|
|
std::shared_ptr<Ui::RadiobuttonGroup> group;
|
2022-04-07 07:34:29 +00:00
|
|
|
std::vector<std::shared_ptr<Data::DocumentMedia>> medias;
|
2022-04-06 15:19:50 +00:00
|
|
|
Data::NotifySound chosen;
|
2022-04-04 21:00:58 +00:00
|
|
|
base::unique_qptr<Ui::PopupMenu> menu;
|
2022-04-05 15:29:43 +00:00
|
|
|
QPointer<Ui::Radiobutton> defaultButton;
|
|
|
|
QPointer<Ui::Radiobutton> chosenButton;
|
|
|
|
std::vector<QPointer<Ui::Radiobutton>> buttons;
|
2022-04-04 21:00:58 +00:00
|
|
|
};
|
|
|
|
const auto state = container->lifetime().make_state<State>(State{
|
2022-04-06 15:19:50 +00:00
|
|
|
.group = std::make_shared<Ui::RadiobuttonGroup>(),
|
|
|
|
.chosen = selected,
|
2022-04-04 21:00:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const auto addToGroup = [=](
|
|
|
|
not_null<Ui::VerticalLayout*> verticalLayout,
|
|
|
|
int value,
|
2022-04-05 15:29:43 +00:00
|
|
|
const QString &text,
|
|
|
|
bool chosen) {
|
|
|
|
if (chosen) {
|
|
|
|
state->group->setValue(value);
|
|
|
|
}
|
2022-04-04 21:00:58 +00:00
|
|
|
const auto button = verticalLayout->add(
|
|
|
|
object_ptr<Ui::Radiobutton>(
|
|
|
|
verticalLayout,
|
|
|
|
state->group,
|
|
|
|
value,
|
2022-04-06 15:19:50 +00:00
|
|
|
text,
|
2022-04-04 21:00:58 +00:00
|
|
|
st::defaultCheckbox),
|
|
|
|
padding);
|
2022-04-05 15:29:43 +00:00
|
|
|
if (chosen) {
|
|
|
|
state->chosenButton = button;
|
|
|
|
}
|
|
|
|
if (value == kDefaultValue) {
|
|
|
|
state->defaultButton = button;
|
2022-04-07 07:34:29 +00:00
|
|
|
button->setClickedCallback([=] {
|
|
|
|
Core::App().notifications().playSound(session, 0);
|
|
|
|
});
|
2022-04-05 15:29:43 +00:00
|
|
|
}
|
2022-04-04 21:00:58 +00:00
|
|
|
if (value < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2022-04-05 15:29:43 +00:00
|
|
|
while (state->buttons.size() <= value) {
|
|
|
|
state->buttons.push_back(nullptr);
|
|
|
|
}
|
2022-04-07 07:34:29 +00:00
|
|
|
button->setClickedCallback([=] {
|
|
|
|
const auto media = state->medias[value].get();
|
|
|
|
if (media->loaded()) {
|
|
|
|
Core::App().notifications().playSound(
|
|
|
|
session,
|
|
|
|
media->owner()->id);
|
|
|
|
}
|
|
|
|
});
|
2022-04-04 21:00:58 +00:00
|
|
|
base::install_event_filter(button, [=](not_null<QEvent*> e) {
|
2022-04-05 15:29:43 +00:00
|
|
|
if (e->type() != QEvent::ContextMenu || state->menu) {
|
2022-04-04 21:00:58 +00:00
|
|
|
return base::EventFilterResult::Continue;
|
|
|
|
}
|
|
|
|
state->menu = base::make_unique_q<Ui::PopupMenu>(
|
|
|
|
button,
|
|
|
|
st::popupMenuWithIcons);
|
|
|
|
auto callback = [=] {
|
2022-04-07 07:34:29 +00:00
|
|
|
const auto id = state->medias[value]->owner()->id;
|
2022-04-06 15:19:50 +00:00
|
|
|
session->api().ringtones().remove(id);
|
2022-04-04 21:00:58 +00:00
|
|
|
};
|
|
|
|
state->menu->addAction(
|
|
|
|
tr::lng_box_delete(tr::now),
|
|
|
|
std::move(callback),
|
|
|
|
&st::menuIconDelete);
|
|
|
|
state->menu->popup(QCursor::pos());
|
|
|
|
return base::EventFilterResult::Cancel;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-04-06 15:19:50 +00:00
|
|
|
session->api().ringtones().uploadFails(
|
2022-04-04 21:00:58 +00:00
|
|
|
) | rpl::start_with_next([=](const QString &error) {
|
2022-04-10 19:16:15 +00:00
|
|
|
if ((error == u"RINGTONE_DURATION_TOO_LONG"_q)) {
|
|
|
|
box->getDelegate()->show(Ui::MakeInformBox(
|
|
|
|
tr::lng_ringtones_error_max_duration(
|
|
|
|
tr::now,
|
|
|
|
lt_duration,
|
|
|
|
Ui::FormatMuteFor(
|
|
|
|
session->api().ringtones().maxDuration()))));
|
|
|
|
} else if ((error == u"RINGTONE_SIZE_TOO_BIG"_q)) {
|
|
|
|
box->getDelegate()->show(Ui::MakeInformBox(
|
|
|
|
tr::lng_ringtones_error_max_size(
|
|
|
|
tr::now,
|
|
|
|
lt_size,
|
|
|
|
Ui::FormatSizeText(
|
|
|
|
session->api().ringtones().maxSize()))));
|
2022-04-04 21:00:58 +00:00
|
|
|
} else if (error == u"RINGTONE_MIME_INVALID"_q) {
|
|
|
|
box->getDelegate()->show(
|
|
|
|
Ui::MakeInformBox(tr::lng_edit_media_invalid_file()));
|
|
|
|
}
|
|
|
|
}, box->lifetime());
|
|
|
|
|
|
|
|
Settings::AddSubsectionTitle(
|
|
|
|
container,
|
|
|
|
tr::lng_ringtones_box_cloud_subtitle());
|
|
|
|
|
2022-04-06 15:19:50 +00:00
|
|
|
const auto noSound = selected.none;
|
2022-04-05 15:29:43 +00:00
|
|
|
addToGroup(
|
|
|
|
container,
|
|
|
|
kDefaultValue,
|
2022-04-06 11:43:39 +00:00
|
|
|
tr::lng_ringtones_box_default(tr::now),
|
2022-04-05 15:29:43 +00:00
|
|
|
false);
|
|
|
|
addToGroup(
|
|
|
|
container,
|
|
|
|
kNoSoundValue,
|
2022-04-06 11:43:39 +00:00
|
|
|
tr::lng_ringtones_box_no_sound(tr::now),
|
2022-04-05 15:29:43 +00:00
|
|
|
noSound);
|
|
|
|
|
|
|
|
const auto custom = container->add(
|
|
|
|
object_ptr<Ui::VerticalLayout>(container));
|
|
|
|
|
|
|
|
const auto rebuild = [=] {
|
2022-04-07 07:34:29 +00:00
|
|
|
const auto old = base::take(state->medias);
|
2022-04-05 15:29:43 +00:00
|
|
|
auto value = 0;
|
|
|
|
while (custom->count()) {
|
|
|
|
delete custom->widgetAt(0);
|
|
|
|
}
|
|
|
|
|
2022-04-06 15:19:50 +00:00
|
|
|
for (const auto &id : session->api().ringtones().list()) {
|
|
|
|
const auto chosen = (state->chosen.id && state->chosen.id == id);
|
|
|
|
const auto document = session->data().document(id);
|
|
|
|
const auto text = ExtractRingtoneName(document);
|
2022-04-06 11:43:39 +00:00
|
|
|
addToGroup(custom, value++, text, chosen);
|
2022-04-07 07:34:29 +00:00
|
|
|
state->medias.push_back(document->createMediaView());
|
|
|
|
document->owner().notifySettings().cacheSound(document);
|
2022-04-05 15:29:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
custom->resizeToWidth(container->width());
|
|
|
|
if (!state->chosenButton) {
|
|
|
|
state->group->setValue(kDefaultValue);
|
|
|
|
state->defaultButton->finishAnimating();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-06 15:19:50 +00:00
|
|
|
session->api().ringtones().listUpdates(
|
2022-04-05 15:29:43 +00:00
|
|
|
) | rpl::start_with_next(rebuild, container->lifetime());
|
2022-04-04 21:00:58 +00:00
|
|
|
|
2022-04-06 15:19:50 +00:00
|
|
|
session->api().ringtones().uploadDones(
|
|
|
|
) | rpl::start_with_next([=](DocumentId id) {
|
|
|
|
state->chosen = Data::NotifySound{ .id = id };
|
|
|
|
rebuild();
|
|
|
|
}, container->lifetime());
|
|
|
|
|
|
|
|
session->api().ringtones().requestList();
|
2022-04-05 15:29:43 +00:00
|
|
|
rebuild();
|
2022-04-04 21:00:58 +00:00
|
|
|
|
|
|
|
const auto upload = box->addRow(
|
|
|
|
Settings::CreateButton(
|
|
|
|
container,
|
|
|
|
tr::lng_ringtones_box_upload_button(),
|
|
|
|
st::ringtonesBoxButton,
|
|
|
|
{
|
2022-05-03 18:11:47 +00:00
|
|
|
&st::settingsIconAdd,
|
2022-04-04 21:00:58 +00:00
|
|
|
0,
|
|
|
|
Settings::IconType::Round,
|
|
|
|
&st::windowBgActive
|
|
|
|
}),
|
|
|
|
style::margins());
|
|
|
|
upload->addClickHandler([=] {
|
|
|
|
const auto delay = st::ringtonesBoxButton.ripple.hideDuration;
|
|
|
|
base::call_delayed(delay, crl::guard(box, [=] {
|
|
|
|
const auto callback = [=](const FileDialog::OpenResult &result) {
|
|
|
|
auto mime = QString();
|
|
|
|
auto name = QString();
|
|
|
|
auto content = result.remoteContent;
|
|
|
|
if (!result.paths.isEmpty()) {
|
|
|
|
auto info = QFileInfo(result.paths.front());
|
|
|
|
mime = Core::MimeTypeForFile(info).name();
|
|
|
|
name = info.fileName();
|
|
|
|
auto f = QFile(result.paths.front());
|
|
|
|
if (f.open(QIODevice::ReadOnly)) {
|
|
|
|
content = f.readAll();
|
|
|
|
f.close();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mime = Core::MimeTypeForData(content).name();
|
|
|
|
name = "audio";
|
|
|
|
}
|
2022-04-10 19:16:15 +00:00
|
|
|
const auto &ringtones = session->api().ringtones();
|
|
|
|
if (int(content.size()) > ringtones.maxSize()) {
|
|
|
|
box->getDelegate()->show(Ui::MakeInformBox(
|
|
|
|
tr::lng_ringtones_error_max_size(
|
|
|
|
tr::now,
|
|
|
|
lt_size,
|
|
|
|
Ui::FormatSizeText(ringtones.maxSize()))));
|
|
|
|
return;
|
|
|
|
}
|
2022-04-04 21:00:58 +00:00
|
|
|
|
2022-04-06 15:19:50 +00:00
|
|
|
session->api().ringtones().upload(name, mime, content);
|
2022-04-04 21:00:58 +00:00
|
|
|
};
|
|
|
|
FileDialog::GetOpenPath(
|
|
|
|
box.get(),
|
|
|
|
tr::lng_ringtones_box_upload_choose(tr::now),
|
|
|
|
"Audio files (*.mp3)",
|
|
|
|
crl::guard(box, callback));
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
box->addSkip(st::ringtonesBoxSkip);
|
2022-04-05 15:29:43 +00:00
|
|
|
Settings::AddDividerText(container, tr::lng_ringtones_box_about());
|
2022-04-04 21:00:58 +00:00
|
|
|
|
|
|
|
box->addSkip(st::ringtonesBoxSkip);
|
|
|
|
|
|
|
|
box->setWidth(st::boxWideWidth);
|
|
|
|
box->addButton(tr::lng_settings_save(), [=] {
|
|
|
|
const auto value = state->group->value();
|
|
|
|
auto sound = (value == kDefaultValue)
|
|
|
|
? Data::NotifySound()
|
|
|
|
: (value == kNoSoundValue)
|
|
|
|
? Data::NotifySound{ .none = true }
|
2022-04-07 07:34:29 +00:00
|
|
|
: Data::NotifySound{ .id = state->medias[value]->owner()->id };
|
2022-04-06 15:19:50 +00:00
|
|
|
save(sound);
|
2022-04-04 21:00:58 +00:00
|
|
|
box->closeBox();
|
|
|
|
});
|
|
|
|
box->addButton(tr::lng_cancel(), [=] { box->closeBox(); });
|
|
|
|
}
|
2022-04-06 15:19:50 +00:00
|
|
|
|
2022-10-13 17:34:04 +00:00
|
|
|
void ThreadRingtonesBox(
|
2022-04-06 15:19:50 +00:00
|
|
|
not_null<Ui::GenericBox*> box,
|
2022-10-13 17:34:04 +00:00
|
|
|
not_null<Data::Thread*> thread) {
|
|
|
|
const auto now = thread->owner().notifySettings().sound(thread);
|
|
|
|
RingtonesBox(box, &thread->session(), now, [=](Data::NotifySound sound) {
|
|
|
|
thread->owner().notifySettings().update(thread, {}, {}, sound);
|
2022-04-06 15:19:50 +00:00
|
|
|
});
|
|
|
|
}
|