tdesktop/Telegram/SourceFiles/settings/settings_codes.cpp

268 lines
8.0 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"
2020-03-26 10:29:27 +00:00
#include "mainwindow.h"
2018-09-06 13:48:01 +00:00
#include "data/data_session.h"
#include "main/main_session.h"
#include "main/main_account.h"
2020-06-16 06:42:47 +00:00
#include "main/main_accounts.h"
2018-09-06 13:48:01 +00:00
#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/mtproto_dc_options.h"
2018-09-06 13:48:01 +00:00
#include "core/file_utilities.h"
#include "core/update_checker.h"
#include "window/themes/window_theme.h"
#include "window/themes/window_theme_editor.h"
2020-03-26 10:29:27 +00:00
#include "window/window_session_controller.h"
#include "media/audio/media_audio_track.h"
2020-03-26 10:29:27 +00:00
#include "settings/settings_common.h"
#include "api/api_updates.h"
#include "facades.h"
2018-09-06 13:48:01 +00:00
namespace Settings {
2020-03-26 10:29:27 +00:00
namespace {
using SessionController = Window::SessionController;
2018-09-06 13:48:01 +00:00
auto GenerateCodes() {
2020-03-26 10:29:27 +00:00
auto codes = std::map<QString, Fn<void(SessionController*)>>();
codes.emplace(qsl("debugmode"), [](SessionController *window) {
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
}));
});
2020-03-26 10:29:27 +00:00
codes.emplace(qsl("viewlogs"), [](SessionController *window) {
2018-09-06 13:48:01 +00:00
File::ShowInFolder(cWorkingDir() + "log.txt");
});
if (!Core::UpdaterDisabled()) {
2020-03-26 10:29:27 +00:00
codes.emplace(qsl("testupdate"), [](SessionController *window) {
2018-09-06 13:48:01 +00:00
Core::UpdateChecker().test();
});
}
2020-03-26 10:29:27 +00:00
codes.emplace(qsl("loadlang"), [](SessionController *window) {
Lang::CurrentCloudManager().switchToLanguage({ qsl("#custom") });
2018-09-06 13:48:01 +00:00
});
2020-03-26 10:29:27 +00:00
codes.emplace(qsl("crashplease"), [](SessionController *window) {
2018-09-06 13:48:01 +00:00
Unexpected("Crashed in Settings!");
});
2020-03-26 10:29:27 +00:00
codes.emplace(qsl("moderate"), [](SessionController *window) {
if (!window) {
return;
}
2018-09-06 13:48:01 +00:00
auto text = Global::ModerateModeEnabled() ? qsl("Disable moderate mode?") : qsl("Enable moderate mode?");
Ui::show(Box<ConfirmBox>(text, [=] {
2018-09-06 13:48:01 +00:00
Global::SetModerateModeEnabled(!Global::ModerateModeEnabled());
window->session().saveSettingsDelayed();
2018-09-06 13:48:01 +00:00
Ui::hideLayer();
}));
});
2020-03-26 10:29:27 +00:00
codes.emplace(qsl("getdifference"), [](SessionController *window) {
if (window) {
window->session().updates().getDifference();
2018-09-06 13:48:01 +00:00
}
});
2020-03-26 10:29:27 +00:00
codes.emplace(qsl("loadcolors"), [](SessionController *window) {
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());
}
});
});
2020-03-26 10:29:27 +00:00
codes.emplace(qsl("videoplayer"), [](SessionController *window) {
if (!window) {
return;
}
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, [=] {
2018-09-06 13:48:01 +00:00
cSetUseExternalVideoPlayer(!cUseExternalVideoPlayer());
window->session().saveSettingsDelayed();
2018-09-06 13:48:01 +00:00
Ui::hideLayer();
}));
});
2020-03-26 10:29:27 +00:00
codes.emplace(qsl("endpoints"), [](SessionController *window) {
if (!Core::App().accounts().started()) {
return;
}
const auto weak = window
? base::make_weak(&window->session().account())
: nullptr;
FileDialog::GetOpenPath(Core::App().getFileDialogParent(), "Open DC endpoints", "DC Endpoints (*.tdesktop-endpoints)", [weak](const FileDialog::OpenResult &result) {
2018-09-06 13:48:01 +00:00
if (!result.paths.isEmpty()) {
const auto loadFor = [&](not_null<Main::Account*> account) {
if (!account->mtp().dcOptions().loadFromFile(result.paths.front())) {
Ui::show(Box<InformBox>("Could not load endpoints :( Errors in 'log.txt'."));
}
};
if (const auto strong = weak.get()) {
loadFor(strong);
} else {
for (const auto &[index, account] : Core::App().accounts().list()) {
loadFor(account.get());
}
2018-09-06 13:48:01 +00:00
}
}
});
});
2020-03-26 10:29:27 +00:00
codes.emplace(qsl("folders"), [](SessionController *window) {
if (window) {
window->showSettings(Settings::Type::Folders);
}
});
2020-06-16 06:42:47 +00:00
codes.emplace(qsl("accadd"), [](SessionController *window) {
crl::on_main(&Core::App(), [=] {
if (window
&& !Core::App().locked()
&& Core::App().accounts().started()
&& Core::App().accounts().list().size() < 3) {
Core::App().accounts().activate(
Core::App().accounts().add(MTP::Environment::Production));
}
});
});
codes.emplace(qsl("acctest"), [](SessionController *window) {
crl::on_main(&Core::App(), [=] {
if (window
&& !Core::App().locked()
&& Core::App().accounts().started()
2020-06-16 06:42:47 +00:00
&& Core::App().accounts().list().size() < 3) {
Core::App().accounts().activate(
Core::App().accounts().add(MTP::Environment::Test));
2020-06-16 06:42:47 +00:00
}
});
});
2020-06-16 06:42:47 +00:00
for (auto i = 0; i != 3; ++i) {
codes.emplace(qsl("account%1").arg(i + 1), [=](
SessionController *window) {
crl::on_main(&Core::App(), [=] {
const auto &list = Core::App().accounts().list();
const auto j = list.find(i);
if (j != list.end() && !Core::App().locked()) {
if (&Core::App().activeAccount() != j->second.get()) {
Core::App().accounts().activate(i);
}
}
});
});
}
#ifndef TDESKTOP_DISABLE_REGISTER_CUSTOM_SCHEME
2020-03-26 10:29:27 +00:00
codes.emplace(qsl("registertg"), [](SessionController *window) {
Platform::RegisterCustomScheme(true);
2018-09-06 13:48:01 +00:00
Ui::Toast::Show("Forced custom scheme register.");
});
#endif // !TDESKTOP_DISABLE_REGISTER_CUSTOM_SCHEME
2020-03-26 10:29:27 +00:00
codes.emplace(qsl("export"), [](SessionController *window) {
window->session().data().startExport();
2018-09-06 13:48:01 +00:00
});
#if defined Q_OS_WIN || defined Q_OS_MAC
codes.emplace(qsl("freetype"), [](SessionController *window) {
auto text = cUseFreeType()
#ifdef Q_OS_WIN
? qsl("Switch font engine to GDI?")
#else // Q_OS_WIN
? qsl("Switch font engine to Cocoa?")
#endif // !Q_OS_WIN
: qsl("Switch font engine to FreeType?");
Ui::show(Box<ConfirmBox>(text, [] {
Core::App().switchFreeType();
}));
});
#endif // Q_OS_WIN || Q_OS_MAC
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) {
2020-03-26 10:29:27 +00:00
codes.emplace(key, [=](SessionController *window) {
if (!window) {
2018-09-06 13:48:01 +00:00
return;
}
const auto weak = base::make_weak(&window->session());
2020-03-26 10:29:27 +00:00
FileDialog::GetOpenPath(Core::App().getFileDialogParent(), "Open audio file", audioFilters, crl::guard(&window->session(), [=](const FileDialog::OpenResult &result) {
if (weak && !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()) {
2020-03-26 10:29:27 +00:00
Ui::show(Box<InformBox>(
"Could not audio :( Errors in 'log.txt'."));
2018-09-06 13:48:01 +00:00
} else {
weak->settings().setSoundOverride(
2020-03-26 10:29:27 +00:00
key,
result.paths.front());
weak->saveSettingsDelayed();
2018-09-06 13:48:01 +00:00
}
}
2019-07-24 14:00:30 +00:00
}));
2018-09-06 13:48:01 +00:00
});
}
2020-03-26 10:29:27 +00:00
codes.emplace(qsl("sounds_reset"), [](SessionController *window) {
if (window) {
window->session().settings().clearSoundOverrides();
window->session().saveSettingsDelayed();
2018-09-06 13:48:01 +00:00
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;
}
2020-03-26 10:29:27 +00:00
} // namespace
void CodesFeedString(SessionController *window, 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) {
2020-03-26 10:29:27 +00:00
method(window);
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