2018-10-02 20:39:54 +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
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "base/binary_guard.h"
|
|
|
|
|
2019-09-04 07:19:15 +00:00
|
|
|
#include <QtNetwork/QNetworkReply>
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
namespace Main {
|
|
|
|
class Session;
|
|
|
|
} // namespace Main
|
2018-10-02 20:39:54 +00:00
|
|
|
|
|
|
|
namespace Support {
|
|
|
|
namespace details {
|
|
|
|
|
|
|
|
struct TemplatesQuestion {
|
|
|
|
QString question;
|
2018-11-12 08:52:44 +00:00
|
|
|
QStringList originalKeys;
|
|
|
|
QStringList normalizedKeys;
|
2018-10-02 20:39:54 +00:00
|
|
|
QString value;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TemplatesFile {
|
|
|
|
QString url;
|
|
|
|
std::map<QString, TemplatesQuestion> questions;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TemplatesData {
|
|
|
|
std::map<QString, TemplatesFile> files;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TemplatesIndex {
|
|
|
|
using Id = std::pair<QString, QString>; // filename, normalized question
|
|
|
|
using Term = std::pair<QString, int>; // search term, weight
|
|
|
|
|
|
|
|
std::map<QChar, std::vector<Id>> first;
|
|
|
|
std::map<Id, std::vector<Term>> full;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace details
|
|
|
|
|
|
|
|
class Templates : public base::has_weak_ptr {
|
|
|
|
public:
|
2019-07-24 11:45:24 +00:00
|
|
|
explicit Templates(not_null<Main::Session*> session);
|
2018-10-02 20:39:54 +00:00
|
|
|
|
|
|
|
void reload();
|
|
|
|
|
|
|
|
using Question = details::TemplatesQuestion;
|
|
|
|
std::vector<Question> query(const QString &text) const;
|
|
|
|
|
|
|
|
auto errors() const {
|
|
|
|
return _errors.events();
|
|
|
|
}
|
|
|
|
|
2018-10-11 11:17:51 +00:00
|
|
|
struct QuestionByKey {
|
|
|
|
Question question;
|
|
|
|
QString key;
|
|
|
|
};
|
|
|
|
std::optional<QuestionByKey> matchExact(QString text) const;
|
|
|
|
std::optional<QuestionByKey> matchFromEnd(QString text) const;
|
|
|
|
int maxKeyLength() const {
|
|
|
|
return _maxKeyLength;
|
|
|
|
}
|
|
|
|
|
2018-10-05 08:14:00 +00:00
|
|
|
~Templates();
|
|
|
|
|
2018-10-02 20:39:54 +00:00
|
|
|
private:
|
2018-10-05 08:14:00 +00:00
|
|
|
struct Updates;
|
|
|
|
|
2018-11-16 05:51:47 +00:00
|
|
|
void load();
|
2018-10-02 20:39:54 +00:00
|
|
|
void update();
|
2018-10-05 08:14:00 +00:00
|
|
|
void ensureUpdatesCreated();
|
|
|
|
void updateRequestFinished(QNetworkReply *reply);
|
|
|
|
void checkUpdateFinished();
|
2018-10-11 11:17:51 +00:00
|
|
|
void setData(details::TemplatesData &&data);
|
2018-10-02 20:39:54 +00:00
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
not_null<Main::Session*> _session;
|
2018-10-02 20:39:54 +00:00
|
|
|
|
|
|
|
details::TemplatesData _data;
|
|
|
|
details::TemplatesIndex _index;
|
|
|
|
rpl::event_stream<QStringList> _errors;
|
|
|
|
base::binary_guard _reading;
|
|
|
|
bool _reloadAfterRead = false;
|
2018-11-16 05:51:47 +00:00
|
|
|
rpl::lifetime _reloadToastSubscription;
|
2018-10-02 20:39:54 +00:00
|
|
|
|
2018-10-11 11:17:51 +00:00
|
|
|
int _maxKeyLength = 0;
|
|
|
|
|
2018-10-05 08:14:00 +00:00
|
|
|
std::unique_ptr<Updates> _updates;
|
|
|
|
|
2018-11-16 13:36:42 +00:00
|
|
|
rpl::lifetime _lifetime;
|
|
|
|
|
2018-10-02 20:39:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Support
|