Added initial manager of countries.

This commit is contained in:
23rd 2021-08-27 12:01:44 +03:00
parent 3e80c04da7
commit cf523953ad
4 changed files with 185 additions and 0 deletions

View File

@ -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

View File

@ -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<Countries::Manager>(
_domain.get());
countries->lifetime().add([=] {
[[maybe_unused]] const auto countriesCopy = countries;
});
}
}
void Application::showOpenGLCrashNotification() {

View File

@ -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 <QtCore/QFile>
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<Main::Domain*> 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<not_null<MTP::Instance*>>());
}) | rpl::flatten_latest(
) | rpl::start_with_next([=](bool, not_null<MTP::Instance*> 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<Info>();
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<Info>();
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

View File

@ -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<Main::Domain*> domain);
~Manager();
void read();
void write() const;
rpl::lifetime &lifetime();
private:
void request();
std::optional<MTP::Sender> _api;
const QString _path;
int _hash = 0;
rpl::lifetime _lifetime;
};
} // namespace Countries