mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-06-05 06:33:57 +02:00
Use common code for local sound disk cache.
This commit is contained in:
parent
df7dc1583d
commit
2e74ad6fbe
4 changed files with 61 additions and 47 deletions
|
@ -312,12 +312,6 @@ constexpr auto kFrameSize = 4096;
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
LocalCache::~LocalCache() {
|
|
||||||
for (const auto &[id, path] : _cache) {
|
|
||||||
QFile::remove(path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
LocalSound LocalCache::sound(
|
LocalSound LocalCache::sound(
|
||||||
DocumentId id,
|
DocumentId id,
|
||||||
Fn<QByteArray()> resolveOriginalBytes,
|
Fn<QByteArray()> resolveOriginalBytes,
|
||||||
|
@ -334,4 +328,33 @@ LocalSound LocalCache::sound(
|
||||||
: LocalSound();
|
: LocalSound();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LocalDiskCache::LocalDiskCache(const QString &folder)
|
||||||
|
: _base(folder + '/') {
|
||||||
|
QDir().mkpath(_base);
|
||||||
|
}
|
||||||
|
|
||||||
|
QString LocalDiskCache::path(const LocalSound &sound) {
|
||||||
|
if (!sound) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
const auto i = _paths.find(sound.id);
|
||||||
|
if (i != end(_paths)) {
|
||||||
|
return i->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto result = u"TDesktop-%1"_q.arg(sound.id
|
||||||
|
? QString::number(sound.id, 16).toUpper()
|
||||||
|
: u"Default"_q);
|
||||||
|
const auto path = _base + u"%1.wav"_q.arg(result);
|
||||||
|
|
||||||
|
auto f = QFile(path);
|
||||||
|
if (f.open(QIODevice::WriteOnly)) {
|
||||||
|
f.write(sound.wav);
|
||||||
|
f.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
_paths.emplace(sound.id, result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace Media::Audio
|
} // namespace Media::Audio
|
||||||
|
|
|
@ -20,9 +20,6 @@ struct LocalSound {
|
||||||
|
|
||||||
class LocalCache final {
|
class LocalCache final {
|
||||||
public:
|
public:
|
||||||
LocalCache() = default;
|
|
||||||
~LocalCache();
|
|
||||||
|
|
||||||
[[nodiscard]] LocalSound sound(
|
[[nodiscard]] LocalSound sound(
|
||||||
DocumentId id,
|
DocumentId id,
|
||||||
Fn<QByteArray()> resolveOriginalBytes,
|
Fn<QByteArray()> resolveOriginalBytes,
|
||||||
|
@ -33,4 +30,16 @@ private:
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class LocalDiskCache final {
|
||||||
|
public:
|
||||||
|
explicit LocalDiskCache(const QString &folder);
|
||||||
|
|
||||||
|
[[nodiscard]] QString path(const LocalSound &sound);
|
||||||
|
|
||||||
|
private:
|
||||||
|
const QString _base;
|
||||||
|
base::flat_map<DocumentId, QString> _paths;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace Media::Audio
|
} // namespace Media::Audio
|
||||||
|
|
|
@ -18,6 +18,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
#include "history/history.h"
|
#include "history/history.h"
|
||||||
#include "history/history_item.h"
|
#include "history/history_item.h"
|
||||||
#include "main/main_session.h"
|
#include "main/main_session.h"
|
||||||
|
#include "media/audio/media_audio_local_cache.h"
|
||||||
#include "lang/lang_keys.h"
|
#include "lang/lang_keys.h"
|
||||||
#include "base/weak_ptr.h"
|
#include "base/weak_ptr.h"
|
||||||
#include "window/notifications_utilities.h"
|
#include "window/notifications_utilities.h"
|
||||||
|
@ -191,6 +192,8 @@ private:
|
||||||
const not_null<Manager*> _manager;
|
const not_null<Manager*> _manager;
|
||||||
NotificationId _id;
|
NotificationId _id;
|
||||||
|
|
||||||
|
//Media::Audio::LocalDiskCache _sounds;
|
||||||
|
|
||||||
Gio::Application _application;
|
Gio::Application _application;
|
||||||
Gio::Notification _notification;
|
Gio::Notification _notification;
|
||||||
const std::string _guid;
|
const std::string _guid;
|
||||||
|
@ -219,6 +222,7 @@ NotificationData::NotificationData(
|
||||||
NotificationId id)
|
NotificationId id)
|
||||||
: _manager(manager)
|
: _manager(manager)
|
||||||
, _id(id)
|
, _id(id)
|
||||||
|
//, _sounds(cWorkingDir() + u"tdata/audio_cache"_q)
|
||||||
, _application(UseGNotification()
|
, _application(UseGNotification()
|
||||||
? Gio::Application::get_default()
|
? Gio::Application::get_default()
|
||||||
: nullptr)
|
: nullptr)
|
||||||
|
@ -232,7 +236,8 @@ NotificationData::NotificationData(
|
||||||
bool NotificationData::init(const Info &info) {
|
bool NotificationData::init(const Info &info) {
|
||||||
const auto &title = info.title;
|
const auto &title = info.title;
|
||||||
const auto &subtitle = info.subtitle;
|
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) {
|
if (_application) {
|
||||||
_notification = Gio::Notification::new_(
|
_notification = Gio::Notification::new_(
|
||||||
subtitle.isEmpty()
|
subtitle.isEmpty()
|
||||||
|
|
|
@ -226,8 +226,6 @@ private:
|
||||||
|
|
||||||
void clearingThreadLoop();
|
void clearingThreadLoop();
|
||||||
|
|
||||||
[[nodiscard]] QString cacheSound(const Media::Audio::LocalSound &sound);
|
|
||||||
|
|
||||||
const uint64 _managerId = 0;
|
const uint64 _managerId = 0;
|
||||||
QString _managerIdString;
|
QString _managerIdString;
|
||||||
|
|
||||||
|
@ -262,17 +260,27 @@ private:
|
||||||
ClearFinish>;
|
ClearFinish>;
|
||||||
std::vector<ClearTask> _clearingTasks;
|
std::vector<ClearTask> _clearingTasks;
|
||||||
|
|
||||||
std::vector<Fn<void()>> _focusedCallbacks;
|
Media::Audio::LocalDiskCache _sounds;
|
||||||
base::flat_map<DocumentId, QString> _cachedSounds;
|
|
||||||
|
|
||||||
rpl::lifetime _lifetime;
|
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)
|
Manager::Private::Private(Manager *manager)
|
||||||
: _managerId(base::RandomValue<uint64>())
|
: _managerId(base::RandomValue<uint64>())
|
||||||
, _managerIdString(QString::number(_managerId))
|
, _managerIdString(QString::number(_managerId))
|
||||||
, _delegate([[NotificationDelegate alloc] initWithManager:manager managerId:_managerId]) {
|
, _delegate([[NotificationDelegate alloc] initWithManager:manager managerId:_managerId])
|
||||||
|
, _sounds(ResolveSoundsFolder()) {
|
||||||
Core::App().settings().workModeValue(
|
Core::App().settings().workModeValue(
|
||||||
) | rpl::start_with_next([=](Core::Settings::WorkMode mode) {
|
) | rpl::start_with_next([=](Core::Settings::WorkMode mode) {
|
||||||
// We need to update the delegate _after_ the tray icon change was done in Qt.
|
// 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);
|
}, _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(
|
void Manager::Private::showNotification(
|
||||||
NotificationInfo &&info,
|
NotificationInfo &&info,
|
||||||
Ui::PeerUserpicView &userpicView) {
|
Ui::PeerUserpicView &userpicView) {
|
||||||
|
@ -361,7 +338,7 @@ void Manager::Private::showNotification(
|
||||||
|
|
||||||
const auto sound = info.sound ? info.sound() : Media::Audio::LocalSound();
|
const auto sound = info.sound ? info.sound() : Media::Audio::LocalSound();
|
||||||
if (sound) {
|
if (sound) {
|
||||||
[notification setSoundName:Q2NSString(cacheSound(sound))];
|
[notification setSoundName:Q2NSString(_sounds.path(sound))];
|
||||||
} else {
|
} else {
|
||||||
[notification setSoundName:nil];
|
[notification setSoundName:nil];
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue