2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-08-31 14:38:15 +00:00

Use common code for local sound disk cache.

This commit is contained in:
John Preston
2025-01-23 11:58:20 +04:00
parent df7dc1583d
commit 2e74ad6fbe
4 changed files with 61 additions and 47 deletions

View File

@@ -18,6 +18,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "history/history.h"
#include "history/history_item.h"
#include "main/main_session.h"
#include "media/audio/media_audio_local_cache.h"
#include "lang/lang_keys.h"
#include "base/weak_ptr.h"
#include "window/notifications_utilities.h"
@@ -191,6 +192,8 @@ private:
const not_null<Manager*> _manager;
NotificationId _id;
//Media::Audio::LocalDiskCache _sounds;
Gio::Application _application;
Gio::Notification _notification;
const std::string _guid;
@@ -219,6 +222,7 @@ NotificationData::NotificationData(
NotificationId id)
: _manager(manager)
, _id(id)
//, _sounds(cWorkingDir() + u"tdata/audio_cache"_q)
, _application(UseGNotification()
? Gio::Application::get_default()
: nullptr)
@@ -232,7 +236,8 @@ NotificationData::NotificationData(
bool NotificationData::init(const Info &info) {
const auto &title = info.title;
const auto &subtitle = info.subtitle;
//const auto sound = info.sound ? info.sound() : QString();
//const auto sound = info.sound ? info.sound() : Media::Audio::LocalSound();
//const auto path = sound ? _sounds.path(sound) : QString();
if (_application) {
_notification = Gio::Notification::new_(
subtitle.isEmpty()

View File

@@ -226,8 +226,6 @@ private:
void clearingThreadLoop();
[[nodiscard]] QString cacheSound(const Media::Audio::LocalSound &sound);
const uint64 _managerId = 0;
QString _managerIdString;
@@ -262,17 +260,27 @@ private:
ClearFinish>;
std::vector<ClearTask> _clearingTasks;
std::vector<Fn<void()>> _focusedCallbacks;
base::flat_map<DocumentId, QString> _cachedSounds;
Media::Audio::LocalDiskCache _sounds;
rpl::lifetime _lifetime;
};
[[nodiscard]] QString ResolveSoundsFolder() {
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSLibraryDirectory,
NSUserDomainMask,
YES);
NSString *library = [paths firstObject];
NSString *sounds = [library stringByAppendingPathComponent : @"Sounds"];
return NS2QString(sounds);
}
Manager::Private::Private(Manager *manager)
: _managerId(base::RandomValue<uint64>())
, _managerIdString(QString::number(_managerId))
, _delegate([[NotificationDelegate alloc] initWithManager:manager managerId:_managerId]) {
, _delegate([[NotificationDelegate alloc] initWithManager:manager managerId:_managerId])
, _sounds(ResolveSoundsFolder()) {
Core::App().settings().workModeValue(
) | rpl::start_with_next([=](Core::Settings::WorkMode mode) {
// We need to update the delegate _after_ the tray icon change was done in Qt.
@@ -283,37 +291,6 @@ Manager::Private::Private(Manager *manager)
}, _lifetime);
}
QString Manager::Private::cacheSound(const Media::Audio::LocalSound &sound) {
const auto i = _cachedSounds.find(sound.id);
if (i != end(_cachedSounds)) {
return i->second;
}
auto result = u"TDesktop-%1"_q.arg(sound.id
? QString::number(sound.id, 16).toUpper()
: u"Default"_q);
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSLibraryDirectory,
NSUserDomainMask,
YES);
NSString *library = [paths firstObject];
NSString *sounds = [library stringByAppendingPathComponent:@"Sounds"];
const auto folder = NS2QString(sounds);
const auto path = folder + u"/%1.wav"_q.arg(result);
QDir().mkpath(folder);
auto f = QFile(path);
if (f.open(QIODevice::WriteOnly)) {
f.write(sound.wav);
f.close();
}
_cachedSounds.emplace(sound.id, result);
return result;
}
void Manager::Private::showNotification(
NotificationInfo &&info,
Ui::PeerUserpicView &userpicView) {
@@ -361,7 +338,7 @@ void Manager::Private::showNotification(
const auto sound = info.sound ? info.sound() : Media::Audio::LocalSound();
if (sound) {
[notification setSoundName:Q2NSString(cacheSound(sound))];
[notification setSoundName:Q2NSString(_sounds.path(sound))];
} else {
[notification setSoundName:nil];
}