Add sound override support by codes in Settings.

This commit is contained in:
John Preston 2017-05-08 12:08:24 +03:00
parent 23874a0a26
commit 530a385d4e
7 changed files with 100 additions and 26 deletions

View File

@ -24,6 +24,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "messenger.h"
#include "storage/file_download.h"
#include "storage/localstorage.h"
#include "storage/serialize_common.h"
#include "window/notifications_manager.h"
#include "platform/platform_specific.h"
#include "calls/calls_instance.h"
@ -35,7 +36,10 @@ constexpr auto kAutoLockTimeoutLateMs = TimeMs(3000);
} // namespace
QByteArray AuthSessionData::serialize() const {
auto size = sizeof(qint32) * 2;
auto size = sizeof(qint32) * 4;
for (auto i = _variables.soundOverrides.cbegin(), e = _variables.soundOverrides.cend(); i != e; ++i) {
size += Serialize::stringSize(i.key()) + Serialize::stringSize(i.value());
}
auto result = QByteArray();
result.reserve(size);
@ -50,6 +54,10 @@ QByteArray AuthSessionData::serialize() const {
stream << static_cast<qint32>(_variables.emojiPanelTab);
stream << qint32(_variables.lastSeenWarningSeen ? 1 : 0);
stream << qint32(_variables.tabbedSelectorSectionEnabled ? 1 : 0);
stream << qint32(_variables.soundOverrides.size());
for (auto i = _variables.soundOverrides.cbegin(), e = _variables.soundOverrides.cend(); i != e; ++i) {
stream << i.key() << i.value();
}
}
return result;
}
@ -69,11 +77,23 @@ void AuthSessionData::constructFromSerialized(const QByteArray &serialized) {
qint32 emojiPanTab = static_cast<qint32>(EmojiPanelTab::Emoji);
qint32 lastSeenWarningSeen = 0;
qint32 tabbedSelectorSectionEnabled = 1;
QMap<QString, QString> soundOverrides;
stream >> emojiPanTab;
stream >> lastSeenWarningSeen;
if (!stream.atEnd()) {
stream >> tabbedSelectorSectionEnabled;
}
if (!stream.atEnd()) {
auto count = qint32(0);
stream >> count;
if (stream.status() == QDataStream::Ok) {
for (auto i = 0; i != count; ++i) {
QString key, value;
stream >> key >> value;
soundOverrides[key] = value;
}
}
}
if (stream.status() != QDataStream::Ok) {
LOG(("App Error: Bad data for AuthSessionData::constructFromSerialized()"));
return;
@ -87,6 +107,15 @@ void AuthSessionData::constructFromSerialized(const QByteArray &serialized) {
}
_variables.lastSeenWarningSeen = (lastSeenWarningSeen == 1);
_variables.tabbedSelectorSectionEnabled = (tabbedSelectorSectionEnabled == 1);
_variables.soundOverrides = std::move(soundOverrides);
}
QString AuthSessionData::getSoundPath(const QString &key) const {
auto it = _variables.soundOverrides.constFind(key);
if (it != _variables.soundOverrides.end()) {
return it.value();
}
return qsl(":/sounds/") + key + qsl(".mp3");
}
AuthSession::AuthSession(UserId userId)

View File

@ -89,12 +89,20 @@ public:
TimeMs lastTimeVideoPlayedAt() const {
return _lastTimeVideoPlayedAt;
}
void setSoundOverride(const QString &key, const QString &path) {
_variables.soundOverrides.insert(key, path);
}
void clearSoundOverrides() {
_variables.soundOverrides.clear();
}
QString getSoundPath(const QString &key) const;
private:
struct Variables {
bool lastSeenWarningSeen = false;
EmojiPanelTab emojiPanelTab = EmojiPanelTab::Emoji;
bool tabbedSelectorSectionEnabled = true;
QMap<QString, QString> soundOverrides;
};
base::Variable<bool> _contactsLoaded = { false };

View File

@ -164,12 +164,12 @@ public:
return failed() ? 0 : BN_num_bytes(raw());
}
std::vector<gsl::byte> getBytes() const {
base::byte_vector getBytes() const {
if (failed()) {
return std::vector<gsl::byte>();
return base::byte_vector();
}
auto length = BN_num_bytes(raw());
auto result = std::vector<gsl::byte>(length, gsl::byte());
auto result = base::byte_vector(length, gsl::byte());
auto resultSize = BN_bn2bin(raw(), reinterpret_cast<unsigned char*>(result.data()));
t_assert(resultSize == length);
return result;
@ -204,14 +204,14 @@ inline BigNum operator-(const BigNum &a, const BigNum &b) {
return result;
}
inline std::array<gsl::byte, SHA256_DIGEST_LENGTH> Sha256(base::const_byte_span bytes) {
auto result = std::array<gsl::byte, SHA256_DIGEST_LENGTH>();
inline base::byte_array<SHA256_DIGEST_LENGTH> Sha256(base::const_byte_span bytes) {
auto result = base::byte_array<SHA256_DIGEST_LENGTH>();
SHA256(reinterpret_cast<const unsigned char*>(bytes.data()), bytes.size(), reinterpret_cast<unsigned char*>(result.data()));
return result;
}
inline std::array<gsl::byte, SHA_DIGEST_LENGTH> Sha1(base::const_byte_span bytes) {
auto result = std::array<gsl::byte, SHA_DIGEST_LENGTH>();
inline base::byte_array<SHA_DIGEST_LENGTH> Sha1(base::const_byte_span bytes) {
auto result = base::byte_array<SHA_DIGEST_LENGTH>();
SHA1(reinterpret_cast<const unsigned char*>(bytes.data()), bytes.size(), reinterpret_cast<unsigned char*>(result.data()));
return result;
}

View File

@ -237,7 +237,7 @@ QString Call::getDebugLog() const {
void Call::startWaitingTrack() {
_waitingTrack = Media::Audio::Current().createTrack();
auto trackFileName = (_type == Type::Outgoing) ? qsl(":/sounds/call_outgoing.mp3") : qsl(":/sounds/call_incoming.mp3");
auto trackFileName = AuthSession::Current().data().getSoundPath((_type == Type::Outgoing) ? qsl("call_outgoing") : qsl("call_incoming"));
_waitingTrack->samplePeakEach(kSoundSampleMs);
_waitingTrack->fillFromFile(trackFileName);
_waitingTrack->playInLoop();
@ -255,7 +255,7 @@ bool Call::isKeyShaForFingerprintReady() const {
return (_keyFingerprint != 0);
}
std::array<gsl::byte, Call::kSha256Size> Call::getKeyShaForFingerprint() const {
base::byte_array<Call::kSha256Size> Call::getKeyShaForFingerprint() const {
Expects(isKeyShaForFingerprintReady());
Expects(!_ga.empty());
auto encryptedChatAuthKey = base::byte_vector(_authKey.size() + _ga.size(), gsl::byte {});

View File

@ -73,7 +73,7 @@ void Instance::playSound(Sound sound) {
case Sound::Busy: {
if (!_callBusyTrack) {
_callBusyTrack = Media::Audio::Current().createTrack();
_callBusyTrack->fillFromFile(qsl(":/sounds/call_busy.mp3"));
_callBusyTrack->fillFromFile(AuthSession::Current().data().getSoundPath(qsl("call_busy")));
}
_callBusyTrack->playOnce();
} break;
@ -81,7 +81,7 @@ void Instance::playSound(Sound sound) {
case Sound::Ended: {
if (!_callEndedTrack) {
_callEndedTrack = Media::Audio::Current().createTrack();
_callEndedTrack->fillFromFile(qsl(":/sounds/call_end.mp3"));
_callEndedTrack->fillFromFile(AuthSession::Current().data().getSoundPath(qsl("call_end")));
}
_callEndedTrack->playOnce();
} break;
@ -89,7 +89,7 @@ void Instance::playSound(Sound sound) {
case Sound::Connecting: {
if (!_callConnectingTrack) {
_callConnectingTrack = Media::Audio::Current().createTrack();
_callConnectingTrack->fillFromFile(qsl(":/sounds/call_connect.mp3"));
_callConnectingTrack->fillFromFile(AuthSession::Current().data().getSoundPath(qsl("call_connect")));
}
_callConnectingTrack->playOnce();
} break;

View File

@ -39,6 +39,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "core/file_utilities.h"
#include "window/themes/window_theme.h"
#include "window/themes/window_theme_editor.h"
#include "media/media_audio_track.h"
namespace Settings {
namespace {
@ -47,22 +48,22 @@ QString SecretText;
QMap<QString, base::lambda<void()>> Codes;
void fillCodes() {
Codes.insert(qsl("debugmode"), []() {
Codes.insert(qsl("debugmode"), [] {
QString text = cDebug() ? qsl("Do you want to disable DEBUG logs?") : qsl("Do you want to enable DEBUG logs?\n\nAll network events will be logged.");
Ui::show(Box<ConfirmBox>(text, [] {
App::app()->onSwitchDebugMode();
}));
});
Codes.insert(qsl("testmode"), []() {
Codes.insert(qsl("testmode"), [] {
auto text = cTestMode() ? qsl("Do you want to disable TEST mode?") : qsl("Do you want to enable TEST mode?\n\nYou will be switched to test cloud.");
Ui::show(Box<ConfirmBox>(text, [] {
App::app()->onSwitchTestMode();
}));
});
Codes.insert(qsl("loadlang"), []() {
Codes.insert(qsl("loadlang"), [] {
Global::RefChooseCustomLang().notify();
});
Codes.insert(qsl("debugfiles"), []() {
Codes.insert(qsl("debugfiles"), [] {
if (!cDebug()) return;
if (DebugLogging::FileLoader()) {
Global::RefDebugLoggingFlags() &= ~DebugLogging::FileLoaderFlag;
@ -71,16 +72,16 @@ void fillCodes() {
}
Ui::show(Box<InformBox>(DebugLogging::FileLoader() ? qsl("Enabled file download logging") : qsl("Disabled file download logging")));
});
Codes.insert(qsl("crashplease"), []() {
Codes.insert(qsl("crashplease"), [] {
Unexpected("Crashed in Settings!");
});
Codes.insert(qsl("workmode"), []() {
Codes.insert(qsl("workmode"), [] {
auto text = Global::DialogsModeEnabled() ? qsl("Disable work mode?") : qsl("Enable work mode?");
Ui::show(Box<ConfirmBox>(text, [] {
App::app()->onSwitchWorkMode();
}));
});
Codes.insert(qsl("moderate"), []() {
Codes.insert(qsl("moderate"), [] {
auto text = Global::ModerateModeEnabled() ? qsl("Disable moderate mode?") : qsl("Enable moderate mode?");
Ui::show(Box<ConfirmBox>(text, []() {
Global::SetModerateModeEnabled(!Global::ModerateModeEnabled());
@ -88,22 +89,22 @@ void fillCodes() {
Ui::hideLayer();
}));
});
Codes.insert(qsl("getdifference"), []() {
Codes.insert(qsl("getdifference"), [] {
if (auto main = App::main()) {
main->getDifference();
}
});
Codes.insert(qsl("loadcolors"), []() {
Codes.insert(qsl("loadcolors"), [] {
FileDialog::GetOpenPath("Open palette file", "Palette (*.tdesktop-palette)", [](const FileDialog::OpenResult &result) {
if (!result.paths.isEmpty()) {
Window::Theme::Apply(result.paths.front());
}
});
});
Codes.insert(qsl("edittheme"), []() {
Codes.insert(qsl("edittheme"), [] {
Window::Theme::Editor::Start();
});
Codes.insert(qsl("videoplayer"), []() {
Codes.insert(qsl("videoplayer"), [] {
auto text = cUseExternalVideoPlayer() ? qsl("Use internal video player?") : qsl("Use external video player?");
Ui::show(Box<ConfirmBox>(text, [] {
cSetUseExternalVideoPlayer(!cUseExternalVideoPlayer());
@ -111,7 +112,7 @@ void fillCodes() {
Ui::hideLayer();
}));
});
Codes.insert(qsl("endpoints"), []() {
Codes.insert(qsl("endpoints"), [] {
FileDialog::GetOpenPath("Open DC endpoints", "DC Endpoints (*.tdesktop-endpoints)", [](const FileDialog::OpenResult &result) {
if (!result.paths.isEmpty()) {
if (!Messenger::Instance().mtp()->dcOptions()->loadFromFile(result.paths.front())) {
@ -120,6 +121,42 @@ void fillCodes() {
}
});
});
auto audioFilters = qsl("Audio files (*.wav *.mp3);;") + FileDialog::AllFilesFilter();
auto audioKeys = {
qsl("msg_incoming"),
qsl("call_incoming"),
qsl("call_outgoing"),
qsl("call_busy"),
qsl("call_connect"),
qsl("call_end"),
};
for (auto &key : audioKeys) {
Codes.insert(key, [audioFilters, key] {
if (!AuthSession::Exists()) {
return;
}
FileDialog::GetOpenPath("Open audio file", audioFilters, [key](const FileDialog::OpenResult &result) {
if (AuthSession::Exists() && !result.paths.isEmpty()) {
auto track = Media::Audio::Current().createTrack();
track->fillFromFile(result.paths.front());
if (track->failed()) {
Ui::show(Box<InformBox>("Could not audio :( Errors in 'log.txt'."));
} else {
AuthSession::Current().data().setSoundOverride(key, result.paths.front());
Local::writeUserSettings();
}
}
});
});
}
Codes.insert(qsl("sounds_reset"), [] {
if (AuthSession::Exists()) {
AuthSession::Current().data().clearSoundOverrides();
Local::writeUserSettings();
Ui::show(Box<InformBox>("All sound overrides were reset."));
}
});
}
void codesFeedString(const QString &text) {

View File

@ -362,7 +362,7 @@ void System::ensureSoundCreated() {
}
_soundTrack = Media::Audio::Current().createTrack();
_soundTrack->fillFromFile(qsl(":/sounds/msg_incoming.mp3"));
_soundTrack->fillFromFile(AuthSession::Current().data().getSoundPath(qsl("msg_incoming")));
}
void System::updateAll() {