mirror of https://github.com/procxx/kepka.git
Moved loader of emoji sets and dictionaries to CloudBlob.
This commit is contained in:
parent
704dcc8d65
commit
8ca0b614d7
|
@ -41,34 +41,19 @@ using Dictionaries = std::vector<int>;
|
||||||
using namespace Storage::CloudBlob;
|
using namespace Storage::CloudBlob;
|
||||||
|
|
||||||
using Loading = MTP::DedicatedLoader::Progress;
|
using Loading = MTP::DedicatedLoader::Progress;
|
||||||
using DictState = base::variant<
|
using DictState = BlobState;
|
||||||
Available,
|
|
||||||
Ready,
|
|
||||||
Active,
|
|
||||||
Failed,
|
|
||||||
Loading>;
|
|
||||||
|
|
||||||
class Loader : public QObject {
|
class Loader : public BlobLoader {
|
||||||
public:
|
public:
|
||||||
Loader(QObject *parent, int id);
|
Loader(
|
||||||
|
QObject *parent,
|
||||||
|
int id,
|
||||||
|
MTP::DedicatedLoader::Location location,
|
||||||
|
const QString &folder,
|
||||||
|
int size);
|
||||||
|
|
||||||
int id() const;
|
void destroy() override;
|
||||||
|
void unpack(const QString &path) override;
|
||||||
rpl::producer<DictState> state() const;
|
|
||||||
void destroy();
|
|
||||||
|
|
||||||
private:
|
|
||||||
void setImplementation(std::unique_ptr<MTP::DedicatedLoader> loader);
|
|
||||||
void unpack(const QString &path);
|
|
||||||
void finalize(const QString &path);
|
|
||||||
void fail();
|
|
||||||
|
|
||||||
int _id = 0;
|
|
||||||
int _size = 0;
|
|
||||||
rpl::variable<DictState> _state;
|
|
||||||
|
|
||||||
MTP::WeakInstance _mtproto;
|
|
||||||
std::unique_ptr<MTP::DedicatedLoader> _implementation;
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -109,10 +94,10 @@ int GetDownloadSize(int id) {
|
||||||
}
|
}
|
||||||
|
|
||||||
MTP::DedicatedLoader::Location GetDownloadLocation(int id) {
|
MTP::DedicatedLoader::Location GetDownloadLocation(int id) {
|
||||||
constexpr auto kUsername = "tdhbcfiles";
|
const auto username = kCloudLocationUsername.utf16();
|
||||||
const auto sets = Spellchecker::Dictionaries();
|
const auto sets = Spellchecker::Dictionaries();
|
||||||
const auto i = ranges::find(sets, id, &Spellchecker::Dict::id);
|
const auto i = ranges::find(sets, id, &Spellchecker::Dict::id);
|
||||||
return MTP::DedicatedLoader::Location{ kUsername, i->postId };
|
return MTP::DedicatedLoader::Location{ username, i->postId };
|
||||||
}
|
}
|
||||||
|
|
||||||
DictState ComputeState(int id) {
|
DictState ComputeState(int id) {
|
||||||
|
@ -147,60 +132,18 @@ QString StateDescription(const DictState &state) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Loader::Loader(QObject *parent, int id)
|
Loader::Loader(
|
||||||
: QObject(parent)
|
QObject *parent,
|
||||||
, _id(id)
|
int id,
|
||||||
, _size(GetDownloadSize(_id))
|
MTP::DedicatedLoader::Location location,
|
||||||
, _state(Loading{ 0, _size })
|
const QString &folder,
|
||||||
, _mtproto(Core::App().activeAccount().mtp()) {
|
int size) : BlobLoader(parent, id, location, folder, size) {
|
||||||
const auto ready = [=](std::unique_ptr<MTP::DedicatedLoader> loader) {
|
|
||||||
if (loader) {
|
|
||||||
setImplementation(std::move(loader));
|
|
||||||
} else {
|
|
||||||
fail();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
const auto location = GetDownloadLocation(id);
|
|
||||||
const auto folder = Spellchecker::DictPathByLangId(id);
|
|
||||||
MTP::StartDedicatedLoader(&_mtproto, location, folder, ready);
|
|
||||||
}
|
|
||||||
|
|
||||||
int Loader::id() const {
|
|
||||||
return _id;
|
|
||||||
}
|
|
||||||
|
|
||||||
rpl::producer<DictState> Loader::state() const {
|
|
||||||
return _state.value();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Loader::setImplementation(
|
|
||||||
std::unique_ptr<MTP::DedicatedLoader> loader) {
|
|
||||||
_implementation = std::move(loader);
|
|
||||||
auto convert = [](auto value) {
|
|
||||||
return DictState(value);
|
|
||||||
};
|
|
||||||
_state = _implementation->progress(
|
|
||||||
) | rpl::map([](const Loading &state) {
|
|
||||||
return DictState(state);
|
|
||||||
});
|
|
||||||
_implementation->failed(
|
|
||||||
) | rpl::start_with_next([=] {
|
|
||||||
fail();
|
|
||||||
}, _implementation->lifetime());
|
|
||||||
|
|
||||||
_implementation->ready(
|
|
||||||
) | rpl::start_with_next([=](const QString &filepath) {
|
|
||||||
unpack(filepath);
|
|
||||||
}, _implementation->lifetime());
|
|
||||||
|
|
||||||
QDir(Spellchecker::DictPathByLangId(_id)).removeRecursively();
|
|
||||||
_implementation->start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Loader::unpack(const QString &path) {
|
void Loader::unpack(const QString &path) {
|
||||||
const auto weak = Ui::MakeWeak(this);
|
const auto weak = Ui::MakeWeak(this);
|
||||||
crl::async([=] {
|
crl::async([=] {
|
||||||
if (Spellchecker::UnpackDictionary(path, _id)) {
|
if (Spellchecker::UnpackDictionary(path, id())) {
|
||||||
QFile(path).remove();
|
QFile(path).remove();
|
||||||
crl::on_main(weak, [=] {
|
crl::on_main(weak, [=] {
|
||||||
destroy();
|
destroy();
|
||||||
|
@ -213,13 +156,6 @@ void Loader::unpack(const QString &path) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void Loader::finalize(const QString &path) {
|
|
||||||
}
|
|
||||||
|
|
||||||
void Loader::fail() {
|
|
||||||
_state = Failed();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Loader::destroy() {
|
void Loader::destroy() {
|
||||||
Expects(GlobalLoader == this);
|
Expects(GlobalLoader == this);
|
||||||
|
|
||||||
|
@ -329,7 +265,12 @@ auto AddButtonWithLoader(
|
||||||
) | rpl::start_with_next([=](bool toggled) {
|
) | rpl::start_with_next([=](bool toggled) {
|
||||||
const auto &state = buttonState->current();
|
const auto &state = buttonState->current();
|
||||||
if (toggled && (state.is<Available>() || state.is<Failed>())) {
|
if (toggled && (state.is<Available>() || state.is<Failed>())) {
|
||||||
SetGlobalLoader(base::make_unique_q<Loader>(App::main(), id));
|
SetGlobalLoader(base::make_unique_q<Loader>(
|
||||||
|
App::main(),
|
||||||
|
id,
|
||||||
|
GetDownloadLocation(id),
|
||||||
|
Spellchecker::DictPathByLangId(id),
|
||||||
|
GetDownloadSize(id)));
|
||||||
} else if (!toggled && state.is<Loading>()) {
|
} else if (!toggled && state.is<Loading>()) {
|
||||||
if (GlobalLoader && GlobalLoader->id() == id) {
|
if (GlobalLoader && GlobalLoader->id() == id) {
|
||||||
GlobalLoader->destroy();
|
GlobalLoader->destroy();
|
||||||
|
|
|
@ -53,34 +53,19 @@ auto Sets() {
|
||||||
}
|
}
|
||||||
|
|
||||||
using Loading = MTP::DedicatedLoader::Progress;
|
using Loading = MTP::DedicatedLoader::Progress;
|
||||||
using SetState = base::variant<
|
using SetState = BlobState;
|
||||||
Available,
|
|
||||||
Ready,
|
|
||||||
Active,
|
|
||||||
Loading,
|
|
||||||
Failed>;
|
|
||||||
|
|
||||||
class Loader : public QObject {
|
class Loader : public BlobLoader {
|
||||||
public:
|
public:
|
||||||
Loader(QObject *parent, int id);
|
Loader(
|
||||||
|
QObject *parent,
|
||||||
|
int id,
|
||||||
|
MTP::DedicatedLoader::Location location,
|
||||||
|
const QString &folder,
|
||||||
|
int size);
|
||||||
|
|
||||||
int id() const;
|
void destroy() override;
|
||||||
|
void unpack(const QString &path) override;
|
||||||
rpl::producer<SetState> state() const;
|
|
||||||
void destroy();
|
|
||||||
|
|
||||||
private:
|
|
||||||
void setImplementation(std::unique_ptr<MTP::DedicatedLoader> loader);
|
|
||||||
void unpack(const QString &path);
|
|
||||||
void finalize(const QString &path);
|
|
||||||
void fail();
|
|
||||||
|
|
||||||
int _id = 0;
|
|
||||||
int _size = 0;
|
|
||||||
rpl::variable<SetState> _state;
|
|
||||||
|
|
||||||
MTP::WeakInstance _mtproto;
|
|
||||||
std::unique_ptr<MTP::DedicatedLoader> _implementation;
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -148,10 +133,10 @@ int GetDownloadSize(int id) {
|
||||||
}
|
}
|
||||||
|
|
||||||
MTP::DedicatedLoader::Location GetDownloadLocation(int id) {
|
MTP::DedicatedLoader::Location GetDownloadLocation(int id) {
|
||||||
constexpr auto kUsername = "tdhbcfiles";
|
const auto username = kCloudLocationUsername.utf16();
|
||||||
const auto sets = Sets();
|
const auto sets = Sets();
|
||||||
const auto i = ranges::find(sets, id, &Set::id);
|
const auto i = ranges::find(sets, id, &Set::id);
|
||||||
return MTP::DedicatedLoader::Location{ kUsername, i->postId };
|
return MTP::DedicatedLoader::Location{ username, i->postId };
|
||||||
}
|
}
|
||||||
|
|
||||||
SetState ComputeState(int id) {
|
SetState ComputeState(int id) {
|
||||||
|
@ -195,63 +180,21 @@ bool UnpackSet(const QString &path, const QString &folder) {
|
||||||
return UnpackBlob(path, folder, GoodSetPartName);
|
return UnpackBlob(path, folder, GoodSetPartName);
|
||||||
}
|
}
|
||||||
|
|
||||||
Loader::Loader(QObject *parent, int id)
|
Loader::Loader(
|
||||||
: QObject(parent)
|
QObject *parent,
|
||||||
, _id(id)
|
int id,
|
||||||
, _size(GetDownloadSize(_id))
|
MTP::DedicatedLoader::Location location,
|
||||||
, _state(Loading{ 0, _size })
|
const QString &folder,
|
||||||
, _mtproto(Core::App().activeAccount().mtp()) {
|
int size) : BlobLoader(parent, id, location, folder, size) {
|
||||||
const auto ready = [=](std::unique_ptr<MTP::DedicatedLoader> loader) {
|
|
||||||
if (loader) {
|
|
||||||
setImplementation(std::move(loader));
|
|
||||||
} else {
|
|
||||||
fail();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
const auto location = GetDownloadLocation(id);
|
|
||||||
const auto folder = internal::SetDataPath(id);
|
|
||||||
MTP::StartDedicatedLoader(&_mtproto, location, folder, ready);
|
|
||||||
}
|
|
||||||
|
|
||||||
int Loader::id() const {
|
|
||||||
return _id;
|
|
||||||
}
|
|
||||||
|
|
||||||
rpl::producer<SetState> Loader::state() const {
|
|
||||||
return _state.value();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Loader::setImplementation(
|
|
||||||
std::unique_ptr<MTP::DedicatedLoader> loader) {
|
|
||||||
_implementation = std::move(loader);
|
|
||||||
auto convert = [](auto value) {
|
|
||||||
return SetState(value);
|
|
||||||
};
|
|
||||||
_state = _implementation->progress(
|
|
||||||
) | rpl::map([](const Loading &state) {
|
|
||||||
return SetState(state);
|
|
||||||
});
|
|
||||||
_implementation->failed(
|
|
||||||
) | rpl::start_with_next([=] {
|
|
||||||
fail();
|
|
||||||
}, _implementation->lifetime());
|
|
||||||
|
|
||||||
_implementation->ready(
|
|
||||||
) | rpl::start_with_next([=](const QString &filepath) {
|
|
||||||
unpack(filepath);
|
|
||||||
}, _implementation->lifetime());
|
|
||||||
|
|
||||||
QDir(internal::SetDataPath(_id)).removeRecursively();
|
|
||||||
_implementation->start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Loader::unpack(const QString &path) {
|
void Loader::unpack(const QString &path) {
|
||||||
const auto folder = internal::SetDataPath(_id);
|
const auto folder = internal::SetDataPath(id());
|
||||||
const auto weak = Ui::MakeWeak(this);
|
const auto weak = Ui::MakeWeak(this);
|
||||||
crl::async([=] {
|
crl::async([=] {
|
||||||
if (UnpackSet(path, folder)) {
|
if (UnpackSet(path, folder)) {
|
||||||
QFile(path).remove();
|
QFile(path).remove();
|
||||||
SwitchToSet(_id, crl::guard(weak, [=](bool success) {
|
SwitchToSet(id(), crl::guard(weak, [=](bool success) {
|
||||||
if (success) {
|
if (success) {
|
||||||
destroy();
|
destroy();
|
||||||
} else {
|
} else {
|
||||||
|
@ -266,13 +209,6 @@ void Loader::unpack(const QString &path) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void Loader::finalize(const QString &path) {
|
|
||||||
}
|
|
||||||
|
|
||||||
void Loader::fail() {
|
|
||||||
_state = Failed();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Loader::destroy() {
|
void Loader::destroy() {
|
||||||
Expects(GlobalLoader == this);
|
Expects(GlobalLoader == this);
|
||||||
|
|
||||||
|
@ -491,7 +427,12 @@ void Row::setupHandler() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Row::load() {
|
void Row::load() {
|
||||||
SetGlobalLoader(base::make_unique_q<Loader>(App::main(), _id));
|
SetGlobalLoader(base::make_unique_q<Loader>(
|
||||||
|
App::main(),
|
||||||
|
_id,
|
||||||
|
GetDownloadLocation(_id),
|
||||||
|
internal::SetDataPath(_id),
|
||||||
|
GetDownloadSize(_id)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Row::setupLabels(const Set &set) {
|
void Row::setupLabels(const Set &set) {
|
||||||
|
|
|
@ -8,6 +8,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
#include "storage/storage_cloud_blob.h"
|
#include "storage/storage_cloud_blob.h"
|
||||||
|
|
||||||
#include "base/zlib_help.h"
|
#include "base/zlib_help.h"
|
||||||
|
#include "core/application.h"
|
||||||
|
#include "main/main_account.h"
|
||||||
|
|
||||||
namespace Storage::CloudBlob {
|
namespace Storage::CloudBlob {
|
||||||
|
|
||||||
|
@ -64,4 +66,61 @@ bool UnpackBlob(
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BlobLoader::BlobLoader(
|
||||||
|
QObject *parent,
|
||||||
|
int id,
|
||||||
|
MTP::DedicatedLoader::Location location,
|
||||||
|
const QString &folder,
|
||||||
|
int size)
|
||||||
|
: QObject(parent)
|
||||||
|
, _folder(folder)
|
||||||
|
, _id(id)
|
||||||
|
, _state(Loading{ 0, size })
|
||||||
|
, _mtproto(Core::App().activeAccount().mtp()) {
|
||||||
|
const auto ready = [=](std::unique_ptr<MTP::DedicatedLoader> loader) {
|
||||||
|
if (loader) {
|
||||||
|
setImplementation(std::move(loader));
|
||||||
|
} else {
|
||||||
|
fail();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
MTP::StartDedicatedLoader(&_mtproto, location, _folder, ready);
|
||||||
|
}
|
||||||
|
|
||||||
|
int BlobLoader::id() const {
|
||||||
|
return _id;
|
||||||
|
}
|
||||||
|
|
||||||
|
rpl::producer<BlobState> BlobLoader::state() const {
|
||||||
|
return _state.value();
|
||||||
|
}
|
||||||
|
|
||||||
|
void BlobLoader::setImplementation(
|
||||||
|
std::unique_ptr<MTP::DedicatedLoader> loader) {
|
||||||
|
_implementation = std::move(loader);
|
||||||
|
auto convert = [](auto value) {
|
||||||
|
return BlobState(value);
|
||||||
|
};
|
||||||
|
_state = _implementation->progress(
|
||||||
|
) | rpl::map([](const Loading &state) {
|
||||||
|
return BlobState(state);
|
||||||
|
});
|
||||||
|
_implementation->failed(
|
||||||
|
) | rpl::start_with_next([=] {
|
||||||
|
fail();
|
||||||
|
}, _implementation->lifetime());
|
||||||
|
|
||||||
|
_implementation->ready(
|
||||||
|
) | rpl::start_with_next([=](const QString &filepath) {
|
||||||
|
unpack(filepath);
|
||||||
|
}, _implementation->lifetime());
|
||||||
|
|
||||||
|
QDir(_folder).removeRecursively();
|
||||||
|
_implementation->start();
|
||||||
|
}
|
||||||
|
|
||||||
|
void BlobLoader::fail() {
|
||||||
|
_state = Failed();
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace Storage::CloudBlob
|
} // namespace Storage::CloudBlob
|
||||||
|
|
|
@ -7,8 +7,12 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "mtproto/dedicated_file_loader.h"
|
||||||
|
|
||||||
namespace Storage::CloudBlob {
|
namespace Storage::CloudBlob {
|
||||||
|
|
||||||
|
constexpr auto kCloudLocationUsername = "tdhbcfiles"_cs;
|
||||||
|
|
||||||
struct Blob {
|
struct Blob {
|
||||||
int id = 0;
|
int id = 0;
|
||||||
int postId = 0;
|
int postId = 0;
|
||||||
|
@ -51,9 +55,48 @@ struct Failed {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
using Loading = MTP::DedicatedLoader::Progress;
|
||||||
|
using BlobState = base::variant<
|
||||||
|
Available,
|
||||||
|
Ready,
|
||||||
|
Active,
|
||||||
|
Failed,
|
||||||
|
Loading>;
|
||||||
|
|
||||||
bool UnpackBlob(
|
bool UnpackBlob(
|
||||||
const QString &path,
|
const QString &path,
|
||||||
const QString &folder,
|
const QString &folder,
|
||||||
Fn<bool(const QString &)> checkNameCallback);
|
Fn<bool(const QString &)> checkNameCallback);
|
||||||
|
|
||||||
|
class BlobLoader : public QObject {
|
||||||
|
public:
|
||||||
|
BlobLoader(
|
||||||
|
QObject *parent,
|
||||||
|
int id,
|
||||||
|
MTP::DedicatedLoader::Location location,
|
||||||
|
const QString &folder,
|
||||||
|
int size);
|
||||||
|
|
||||||
|
int id() const;
|
||||||
|
|
||||||
|
rpl::producer<BlobState> state() const;
|
||||||
|
virtual void destroy() = 0;
|
||||||
|
virtual void unpack(const QString &path) = 0;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void fail();
|
||||||
|
|
||||||
|
const QString _folder;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void setImplementation(std::unique_ptr<MTP::DedicatedLoader> loader);
|
||||||
|
|
||||||
|
int _id = 0;
|
||||||
|
rpl::variable<BlobState> _state;
|
||||||
|
|
||||||
|
MTP::WeakInstance _mtproto;
|
||||||
|
std::unique_ptr<MTP::DedicatedLoader> _implementation;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace Storage::CloudBlob
|
} // namespace Storage::CloudBlob
|
||||||
|
|
Loading…
Reference in New Issue