From cf523953ad477b3356f71ac1ef31980a10472483 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Fri, 27 Aug 2021 12:01:44 +0300 Subject: [PATCH] Added initial manager of countries. --- Telegram/CMakeLists.txt | 2 + Telegram/SourceFiles/core/application.cpp | 9 ++ .../countries/countries_manager.cpp | 136 ++++++++++++++++++ .../SourceFiles/countries/countries_manager.h | 38 +++++ 4 files changed, 185 insertions(+) create mode 100644 Telegram/SourceFiles/countries/countries_manager.cpp create mode 100644 Telegram/SourceFiles/countries/countries_manager.h diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index 708386fdb..936f3002b 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -345,6 +345,8 @@ PRIVATE core/utils.cpp core/utils.h core/version.h + countries/countries_manager.cpp + countries/countries_manager.h data/stickers/data_stickers_set.cpp data/stickers/data_stickers_set.h data/stickers/data_stickers.cpp diff --git a/Telegram/SourceFiles/core/application.cpp b/Telegram/SourceFiles/core/application.cpp index cd6636a9f..11577f81b 100644 --- a/Telegram/SourceFiles/core/application.cpp +++ b/Telegram/SourceFiles/core/application.cpp @@ -35,6 +35,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "apiwrap.h" #include "api/api_updates.h" #include "calls/calls_instance.h" +#include "countries/countries_manager.h" #include "lang/lang_file_parser.h" #include "lang/lang_translator.h" #include "lang/lang_cloud_manager.h" @@ -320,6 +321,14 @@ void Application::run() { _mediaView->show(std::move(request)); } }, _window->lifetime()); + + { + const auto countries = std::make_shared( + _domain.get()); + countries->lifetime().add([=] { + [[maybe_unused]] const auto countriesCopy = countries; + }); + } } void Application::showOpenGLCrashNotification() { diff --git a/Telegram/SourceFiles/countries/countries_manager.cpp b/Telegram/SourceFiles/countries/countries_manager.cpp new file mode 100644 index 000000000..3fc0d4ec5 --- /dev/null +++ b/Telegram/SourceFiles/countries/countries_manager.cpp @@ -0,0 +1,136 @@ +/* +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 "countries/countries_manager.h" + +#include "core/application.h" +#include "countries/countries_instance.h" +#include "main/main_app_config.h" +#include "main/main_account.h" +#include "main/main_domain.h" +#include "mtproto/mtp_instance.h" + +#include + +namespace Countries { +namespace { + +auto ProcessAlternativeName(Info &&info) { + if (info.name == u"USA"_q) { + info.alternativeName = u"United States of America"_q; + } + return std::move(info); +} + +} // namespace + +Manager::Manager(not_null domain) +: _path(cWorkingDir() + "tdata/countries") { + read(); + domain->activeValue( + ) | rpl::map([=](Main::Account *account) { + if (!account) { + _api.reset(); + } + return rpl::combine( + account + ? account->appConfig().refreshed() | rpl::map_to(true) + : rpl::never<>() | rpl::map_to(false), + account + ? account->mtpValue() + : rpl::never>()); + }) | rpl::flatten_latest( + ) | rpl::start_with_next([=](bool, not_null instance) { + _api.emplace(instance); + request(); + }, _lifetime); +} + +void Manager::read() { + auto file = QFile(_path); + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { + return; + } + + _hash = QString(file.readLine()).toInt(); + + auto infos = std::vector(); + while (!file.atEnd()) { + const auto parts = QString(file.readLine()).split(";"); + if (parts.size() != 3) { + continue; + } + infos.push_back(ProcessAlternativeName({ + .name = parts.at(0), + .iso2 = parts.at(1), + .code = parts.at(2), + })); + } + Instance().setList(std::move(infos)); +} + +void Manager::write() const { + auto file = QFile(_path); + if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { + return; + } + + auto s = QTextStream(&file); + s << QString::number(_hash) << "\n"; + + for (const auto &info : Instance().list()) { + s << info.name << ";" << info.iso2 << ";" << info.code << "\n"; + } +} + +void Manager::request() { + Expects(_api); + + _api->request(MTPhelp_GetCountriesList( + MTP_string(), + MTP_int(_hash) + )).done([=](const MTPhelp_CountriesList &result) { + result.match([&](const MTPDhelp_countriesList &data) { + _hash = data.vhash().v; + + auto infos = std::vector(); + + for (const auto &country : data.vcountries().v) { + + const auto &countryData = country.c_help_country(); + const auto &first = countryData.vcountry_codes().v.front() + .c_help_countryCode(); + + const auto name = countryData.vdefault_name().v; + + infos.push_back(ProcessAlternativeName({ + .name = name, + .iso2 = countryData.viso2().v, + .code = first.vcountry_code().v, + })); + } + + Instance().setList(std::move(infos)); + write(); + }, [](const MTPDhelp_countriesListNotModified &data) { + }); + _lifetime.destroy(); + }).fail([=](const MTP::Error &error) { + LOG(("API Error: getting countries failed with error %1" + ).arg(error.type())); + _lifetime.destroy(); + }).send(); +} + +rpl::lifetime &Manager::lifetime() { + return _lifetime; +} + +Manager::~Manager() { +} + +} // namespace Countries diff --git a/Telegram/SourceFiles/countries/countries_manager.h b/Telegram/SourceFiles/countries/countries_manager.h new file mode 100644 index 000000000..1ce913078 --- /dev/null +++ b/Telegram/SourceFiles/countries/countries_manager.h @@ -0,0 +1,38 @@ +/* +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 +*/ +#pragma once + +#include "mtproto/sender.h" + +namespace Main { +class Domain; +} // namespace Main + +namespace Countries { + +class Manager final { +public: + Manager(not_null domain); + ~Manager(); + + void read(); + void write() const; + + rpl::lifetime &lifetime(); + +private: + void request(); + + std::optional _api; + const QString _path; + int _hash = 0; + + rpl::lifetime _lifetime; +}; + +} // namespace Countries