tdesktop/Telegram/SourceFiles/settings/settings_codes.cpp

189 lines
6.2 KiB
C++
Raw Normal View History

2018-09-06 13:48:01 +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 "settings/settings_codes.h"
#include "platform/platform_specific.h"
#include "ui/toast/toast.h"
#include "mainwidget.h"
#include "data/data_session.h"
#include "storage/localstorage.h"
#include "boxes/confirm_box.h"
#include "lang/lang_cloud_manager.h"
#include "lang/lang_instance.h"
#include "core/application.h"
2018-09-06 13:48:01 +00:00
#include "mtproto/mtp_instance.h"
#include "mtproto/dc_options.h"
#include "core/file_utilities.h"
#include "core/update_checker.h"
#include "window/themes/window_theme.h"
#include "window/themes/window_theme_editor.h"
#include "media/audio/media_audio_track.h"
#include "facades.h"
2018-09-06 13:48:01 +00:00
namespace Settings {
auto GenerateCodes() {
2019-07-24 14:00:30 +00:00
auto codes = std::map<QString, Fn<void(::Main::Session*)>>();
codes.emplace(qsl("debugmode"), [](::Main::Session *session) {
2018-09-06 13:48:01 +00:00
QString text = Logs::DebugEnabled()
? qsl("Do you want to disable DEBUG logs?")
: qsl("Do you want to enable DEBUG logs?\n\n"
"All network events will be logged.");
Ui::show(Box<ConfirmBox>(text, [] {
Core::App().switchDebugMode();
2018-09-06 13:48:01 +00:00
}));
});
2019-07-24 14:00:30 +00:00
codes.emplace(qsl("viewlogs"), [](::Main::Session *session) {
2018-09-06 13:48:01 +00:00
File::ShowInFolder(cWorkingDir() + "log.txt");
});
2019-07-24 14:00:30 +00:00
codes.emplace(qsl("testmode"), [](::Main::Session *session) {
2018-09-06 13:48:01 +00:00
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, [] {
Core::App().switchTestMode();
2018-09-06 13:48:01 +00:00
}));
});
if (!Core::UpdaterDisabled()) {
2019-07-24 14:00:30 +00:00
codes.emplace(qsl("testupdate"), [](::Main::Session *session) {
2018-09-06 13:48:01 +00:00
Core::UpdateChecker().test();
});
}
2019-07-24 14:00:30 +00:00
codes.emplace(qsl("loadlang"), [](::Main::Session *session) {
Lang::CurrentCloudManager().switchToLanguage({ qsl("#custom") });
2018-09-06 13:48:01 +00:00
});
2019-07-24 14:00:30 +00:00
codes.emplace(qsl("debugfiles"), [](::Main::Session *session) {
2018-09-06 13:48:01 +00:00
if (!Logs::DebugEnabled()) {
return;
}
if (DebugLogging::FileLoader()) {
Global::RefDebugLoggingFlags() &= ~DebugLogging::FileLoaderFlag;
} else {
Global::RefDebugLoggingFlags() |= DebugLogging::FileLoaderFlag;
}
Ui::show(Box<InformBox>(DebugLogging::FileLoader() ? qsl("Enabled file download logging") : qsl("Disabled file download logging")));
});
2019-07-24 14:00:30 +00:00
codes.emplace(qsl("crashplease"), [](::Main::Session *session) {
2018-09-06 13:48:01 +00:00
Unexpected("Crashed in Settings!");
});
2019-07-24 14:00:30 +00:00
codes.emplace(qsl("moderate"), [](::Main::Session *session) {
2018-09-06 13:48:01 +00:00
auto text = Global::ModerateModeEnabled() ? qsl("Disable moderate mode?") : qsl("Enable moderate mode?");
2019-07-24 14:00:30 +00:00
Ui::show(Box<ConfirmBox>(text, [] {
2018-09-06 13:48:01 +00:00
Global::SetModerateModeEnabled(!Global::ModerateModeEnabled());
Local::writeUserSettings();
Ui::hideLayer();
}));
});
2019-07-24 14:00:30 +00:00
codes.emplace(qsl("getdifference"), [](::Main::Session *session) {
2018-09-06 13:48:01 +00:00
if (auto main = App::main()) {
main->getDifference();
}
});
2019-07-24 14:00:30 +00:00
codes.emplace(qsl("loadcolors"), [](::Main::Session *session) {
FileDialog::GetOpenPath(Core::App().getFileDialogParent(), "Open palette file", "Palette (*.tdesktop-palette)", [](const FileDialog::OpenResult &result) {
2018-09-06 13:48:01 +00:00
if (!result.paths.isEmpty()) {
Window::Theme::Apply(result.paths.front());
}
});
});
2019-07-24 14:00:30 +00:00
codes.emplace(qsl("videoplayer"), [](::Main::Session *session) {
2018-09-06 13:48:01 +00:00
auto text = cUseExternalVideoPlayer() ? qsl("Use internal video player?") : qsl("Use external video player?");
Ui::show(Box<ConfirmBox>(text, [] {
cSetUseExternalVideoPlayer(!cUseExternalVideoPlayer());
Local::writeUserSettings();
Ui::hideLayer();
}));
});
2019-07-24 14:00:30 +00:00
codes.emplace(qsl("endpoints"), [](::Main::Session *session) {
FileDialog::GetOpenPath(Core::App().getFileDialogParent(), "Open DC endpoints", "DC Endpoints (*.tdesktop-endpoints)", [](const FileDialog::OpenResult &result) {
2018-09-06 13:48:01 +00:00
if (!result.paths.isEmpty()) {
2019-07-24 08:46:23 +00:00
if (!Core::App().dcOptions()->loadFromFile(result.paths.front())) {
2018-09-06 13:48:01 +00:00
Ui::show(Box<InformBox>("Could not load endpoints :( Errors in 'log.txt'."));
}
}
});
});
#ifndef TDESKTOP_DISABLE_REGISTER_CUSTOM_SCHEME
2019-07-24 14:00:30 +00:00
codes.emplace(qsl("registertg"), [](::Main::Session *session) {
Platform::RegisterCustomScheme(true);
2018-09-06 13:48:01 +00:00
Ui::Toast::Show("Forced custom scheme register.");
});
#endif // !TDESKTOP_DISABLE_REGISTER_CUSTOM_SCHEME
2019-07-24 14:00:30 +00:00
codes.emplace(qsl("export"), [](::Main::Session *session) {
session->data().startExport();
2018-09-06 13:48:01 +00:00
});
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) {
2019-07-24 14:00:30 +00:00
codes.emplace(key, [=](::Main::Session *session) {
if (!session) {
2018-09-06 13:48:01 +00:00
return;
}
2019-07-24 14:00:30 +00:00
FileDialog::GetOpenPath(Core::App().getFileDialogParent(), "Open audio file", audioFilters, crl::guard(session, [=](const FileDialog::OpenResult &result) {
2019-07-24 11:45:24 +00:00
if (Main::Session::Exists() && !result.paths.isEmpty()) {
2018-09-06 13:48:01 +00:00
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 {
2019-07-24 14:00:30 +00:00
session->settings().setSoundOverride(key, result.paths.front());
2018-09-06 13:48:01 +00:00
Local::writeUserSettings();
}
}
2019-07-24 14:00:30 +00:00
}));
2018-09-06 13:48:01 +00:00
});
}
2019-07-24 14:00:30 +00:00
codes.emplace(qsl("sounds_reset"), [](::Main::Session *session) {
if (session) {
session->settings().clearSoundOverrides();
2018-09-06 13:48:01 +00:00
Local::writeUserSettings();
Ui::show(Box<InformBox>("All sound overrides were reset."));
}
});
2019-09-26 14:26:36 +00:00
2018-09-06 13:48:01 +00:00
return codes;
}
2019-07-24 14:00:30 +00:00
void CodesFeedString(::Main::Session *session, const QString &text) {
2018-09-06 13:48:01 +00:00
static const auto codes = GenerateCodes();
static auto secret = QString();
secret += text.toLower();
int size = secret.size(), from = 0;
while (size > from) {
auto piece = secret.midRef(from);
auto found = false;
for (const auto &[key, method] : codes) {
if (piece == key) {
2019-07-24 14:00:30 +00:00
method(session);
2018-09-06 13:48:01 +00:00
from = size;
found = true;
break;
}
}
if (found) break;
found = ranges::find_if(codes, [&](const auto &pair) {
return pair.first.startsWith(piece);
}) != end(codes);
if (found) break;
++from;
}
secret = (size > from) ? secret.mid(from) : QString();
}
} // namespace Settings