File: ringtones_box.cpp

package info (click to toggle)
telegram-desktop 4.6.5%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 53,300 kB
  • sloc: cpp: 605,857; python: 3,978; ansic: 1,636; sh: 965; makefile: 841; objc: 652; javascript: 187; xml: 165
file content (352 lines) | stat: -rw-r--r-- 9,864 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
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"
#include "base/timer_rpl.h"
#include "base/unixtime.h"
#include "core/application.h"
#include "core/core_settings.h"
#include "core/file_utilities.h"
#include "core/mime_type.h"
#include "data/data_document.h"
#include "data/data_document_media.h"
#include "data/data_document_resolver.h"
#include "data/data_thread.h"
#include "data/data_session.h"
#include "data/notify/data_notify_settings.h"
#include "lang/lang_keys.h"
#include "main/main_session.h"
#include "media/audio/media_audio.h"
#include "settings/settings_common.h"
#include "ui/boxes/confirm_box.h"
#include "ui/text/format_values.h"
#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"
#include "styles/style_settings.h"

namespace {

constexpr auto kDefaultValue = -1;
constexpr auto kNoSoundValue = -2;
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();
	}
}

} // namespace

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;
}

void RingtonesBox(
		not_null<Ui::GenericBox*> box,
		not_null<Main::Session*> session,
		Data::NotifySound selected,
		Fn<void(Data::NotifySound)> save) {
	box->setTitle(tr::lng_ringtones_box_title());

	const auto container = box->verticalLayout();

	auto padding = st::boxPadding;
	padding.setTop(padding.bottom());

	struct State {
		AudioCreator creator;
		std::shared_ptr<Ui::RadiobuttonGroup> group;
		std::vector<std::shared_ptr<Data::DocumentMedia>> medias;
		Data::NotifySound chosen;
		base::unique_qptr<Ui::PopupMenu> menu;
		QPointer<Ui::Radiobutton> defaultButton;
		QPointer<Ui::Radiobutton> chosenButton;
		std::vector<QPointer<Ui::Radiobutton>> buttons;
	};
	const auto state = container->lifetime().make_state<State>(State{
		.group = std::make_shared<Ui::RadiobuttonGroup>(),
		.chosen = selected,
	});

	const auto addToGroup = [=](
			not_null<Ui::VerticalLayout*> verticalLayout,
			int value,
			const QString &text,
			bool chosen) {
		if (chosen) {
			state->group->setValue(value);
		}
		const auto button = verticalLayout->add(
			object_ptr<Ui::Radiobutton>(
				verticalLayout,
				state->group,
				value,
				text,
				st::defaultCheckbox),
			padding);
		if (chosen) {
			state->chosenButton = button;
		}
		if (value == kDefaultValue) {
			state->defaultButton = button;
			button->setClickedCallback([=] {
				Core::App().notifications().playSound(session, 0);
			});
		}
		if (value < 0) {
			return;
		}
		while (state->buttons.size() <= value) {
			state->buttons.push_back(nullptr);
		}
		button->setClickedCallback([=] {
			const auto media = state->medias[value].get();
			if (media->loaded()) {
				Core::App().notifications().playSound(
					session,
					media->owner()->id);
			}
		});
		base::install_event_filter(button, [=](not_null<QEvent*> e) {
			if (e->type() != QEvent::ContextMenu || state->menu) {
				return base::EventFilterResult::Continue;
			}
			state->menu = base::make_unique_q<Ui::PopupMenu>(
				button,
				st::popupMenuWithIcons);
			auto callback = [=] {
				const auto id = state->medias[value]->owner()->id;
				session->api().ringtones().remove(id);
			};
			state->menu->addAction(
				tr::lng_box_delete(tr::now),
				std::move(callback),
				&st::menuIconDelete);
			state->menu->popup(QCursor::pos());
			return base::EventFilterResult::Cancel;
		});
	};

	session->api().ringtones().uploadFails(
	) | rpl::start_with_next([=](const QString &error) {
		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()))));
		} 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());

	const auto noSound = selected.none;
	addToGroup(
		container,
		kDefaultValue,
		tr::lng_ringtones_box_default(tr::now),
		false);
	addToGroup(
		container,
		kNoSoundValue,
		tr::lng_ringtones_box_no_sound(tr::now),
		noSound);

	const auto custom = container->add(
		object_ptr<Ui::VerticalLayout>(container));

	const auto rebuild = [=] {
		const auto old = base::take(state->medias);
		auto value = 0;
		while (custom->count()) {
			delete custom->widgetAt(0);
		}

		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);
			addToGroup(custom, value++, text, chosen);
			state->medias.push_back(document->createMediaView());
			document->owner().notifySettings().cacheSound(document);
		}

		custom->resizeToWidth(container->width());
		if (!state->chosenButton) {
			state->group->setValue(kDefaultValue);
			state->defaultButton->finishAnimating();
		}
	};

	session->api().ringtones().listUpdates(
	) | rpl::start_with_next(rebuild, container->lifetime());

	session->api().ringtones().uploadDones(
	) | rpl::start_with_next([=](DocumentId id) {
		state->chosen = Data::NotifySound{ .id = id };
		rebuild();
	}, container->lifetime());

	session->api().ringtones().requestList();
	rebuild();

	const auto upload = box->addRow(
		Settings::CreateButton(
			container,
			tr::lng_ringtones_box_upload_button(),
			st::ringtonesBoxButton,
			{
				&st::settingsIconAdd,
				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";
				}
				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;
				}

				session->api().ringtones().upload(name, mime, content);
			};
			FileDialog::GetOpenPath(
				box.get(),
				tr::lng_ringtones_box_upload_choose(tr::now),
				"Audio files (*.mp3)",
				crl::guard(box, callback));
		}));
	});

	box->addSkip(st::ringtonesBoxSkip);
	Settings::AddDividerText(container, tr::lng_ringtones_box_about());

	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 }
			: Data::NotifySound{ .id = state->medias[value]->owner()->id };
		save(sound);
		box->closeBox();
	});
	box->addButton(tr::lng_cancel(), [=] { box->closeBox(); });
}

void ThreadRingtonesBox(
		not_null<Ui::GenericBox*> box,
		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);
	});
}