2020-06-09 16:57:05 +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/timer.h"
|
2021-04-02 12:59:54 +00:00
|
|
|
#include "base/flags.h"
|
2020-06-09 16:57:05 +00:00
|
|
|
#include "storage/cache/storage_cache_database.h"
|
|
|
|
#include "data/stickers/data_stickers_set.h"
|
2020-11-13 17:27:08 +00:00
|
|
|
#include "data/data_drafts.h"
|
2020-06-09 16:57:05 +00:00
|
|
|
|
|
|
|
class History;
|
2020-10-13 16:43:18 +00:00
|
|
|
|
|
|
|
namespace Core {
|
2020-06-09 16:57:05 +00:00
|
|
|
class FileLocation;
|
2020-10-13 16:43:18 +00:00
|
|
|
} // namespace Core
|
2020-06-09 16:57:05 +00:00
|
|
|
|
|
|
|
namespace Export {
|
|
|
|
struct Settings;
|
|
|
|
} // namespace Export
|
|
|
|
|
|
|
|
namespace Main {
|
|
|
|
class Account;
|
2020-06-18 12:47:09 +00:00
|
|
|
class SessionSettings;
|
2020-06-09 16:57:05 +00:00
|
|
|
} // namespace Main
|
|
|
|
|
|
|
|
namespace Data {
|
|
|
|
class WallPaper;
|
|
|
|
} // namespace Data
|
|
|
|
|
|
|
|
namespace MTP {
|
2020-06-17 09:36:25 +00:00
|
|
|
class Config;
|
2020-06-09 16:57:05 +00:00
|
|
|
class AuthKey;
|
|
|
|
using AuthKeyPtr = std::shared_ptr<AuthKey>;
|
|
|
|
} // namespace MTP
|
|
|
|
|
|
|
|
namespace Storage {
|
|
|
|
namespace details {
|
|
|
|
struct ReadSettingsContext;
|
2020-11-13 17:27:08 +00:00
|
|
|
struct FileReadDescriptor;
|
2020-06-09 16:57:05 +00:00
|
|
|
} // namespace details
|
|
|
|
|
|
|
|
class EncryptionKey;
|
|
|
|
|
|
|
|
using FileKey = quint64;
|
|
|
|
|
2020-06-15 16:25:02 +00:00
|
|
|
enum class StartResult : uchar;
|
2020-06-09 16:57:05 +00:00
|
|
|
|
|
|
|
struct MessageDraft {
|
|
|
|
MsgId msgId = 0;
|
|
|
|
TextWithTags textWithTags;
|
2021-01-29 11:27:17 +00:00
|
|
|
Data::PreviewState previewState = Data::PreviewState::Allowed;
|
2020-06-09 16:57:05 +00:00
|
|
|
};
|
|
|
|
|
2021-08-30 15:31:01 +00:00
|
|
|
struct MessageDraftSource {
|
|
|
|
Fn<MessageDraft()> draft;
|
|
|
|
Fn<MessageCursor()> cursor;
|
|
|
|
};
|
|
|
|
|
2020-06-09 16:57:05 +00:00
|
|
|
class Account final {
|
|
|
|
public:
|
|
|
|
Account(not_null<Main::Account*> owner, const QString &dataName);
|
|
|
|
~Account();
|
|
|
|
|
2020-06-15 16:25:02 +00:00
|
|
|
[[nodiscard]] StartResult legacyStart(const QByteArray &passcode);
|
2021-07-13 17:51:07 +00:00
|
|
|
[[nodiscard]] std::unique_ptr<MTP::Config> start(
|
2020-06-17 09:36:25 +00:00
|
|
|
MTP::AuthKeyPtr localKey);
|
2020-06-16 11:04:04 +00:00
|
|
|
void startAdded(MTP::AuthKeyPtr localKey);
|
2020-06-09 16:57:05 +00:00
|
|
|
[[nodiscard]] int oldMapVersion() const {
|
|
|
|
return _oldMapVersion;
|
|
|
|
}
|
|
|
|
|
2020-06-23 16:02:15 +00:00
|
|
|
[[nodiscard]] QString tempDirectory() const;
|
|
|
|
|
|
|
|
[[nodiscard]] MTP::AuthKeyPtr peekLegacyLocalKey() const {
|
2020-06-15 16:25:02 +00:00
|
|
|
return _localKey;
|
|
|
|
}
|
2020-06-09 16:57:05 +00:00
|
|
|
|
2020-06-18 12:47:09 +00:00
|
|
|
void writeSessionSettings();
|
2020-06-09 16:57:05 +00:00
|
|
|
void writeMtpData();
|
2020-06-17 09:36:25 +00:00
|
|
|
void writeMtpConfig();
|
2020-06-09 16:57:05 +00:00
|
|
|
|
2021-08-30 15:31:01 +00:00
|
|
|
void registerDraftSource(
|
2020-11-13 17:27:08 +00:00
|
|
|
not_null<History*> history,
|
2021-08-30 15:31:01 +00:00
|
|
|
Data::DraftKey key,
|
|
|
|
MessageDraftSource source);
|
|
|
|
void unregisterDraftSource(
|
2020-11-13 17:27:08 +00:00
|
|
|
not_null<History*> history,
|
2021-08-30 15:31:01 +00:00
|
|
|
Data::DraftKey key);
|
|
|
|
void writeDrafts(not_null<History*> history);
|
|
|
|
void readDraftsWithCursors(not_null<History*> history);
|
|
|
|
void writeDraftCursors(not_null<History*> history);
|
2020-11-13 17:27:08 +00:00
|
|
|
[[nodiscard]] bool hasDraftCursors(PeerId peerId);
|
|
|
|
[[nodiscard]] bool hasDraft(PeerId peerId);
|
2020-06-09 16:57:05 +00:00
|
|
|
|
2020-10-13 16:43:18 +00:00
|
|
|
void writeFileLocation(MediaKey location, const Core::FileLocation &local);
|
|
|
|
[[nodiscard]] Core::FileLocation readFileLocation(MediaKey location);
|
2020-06-09 16:57:05 +00:00
|
|
|
void removeFileLocation(MediaKey location);
|
|
|
|
|
|
|
|
[[nodiscard]] EncryptionKey cacheKey() const;
|
|
|
|
[[nodiscard]] QString cachePath() const;
|
|
|
|
[[nodiscard]] Cache::Database::Settings cacheSettings() const;
|
|
|
|
void updateCacheSettings(
|
|
|
|
Cache::Database::SettingsUpdate &update,
|
|
|
|
Cache::Database::SettingsUpdate &updateBig);
|
|
|
|
|
|
|
|
[[nodiscard]] EncryptionKey cacheBigFileKey() const;
|
|
|
|
[[nodiscard]] QString cacheBigFilePath() const;
|
|
|
|
[[nodiscard]] Cache::Database::Settings cacheBigFileSettings() const;
|
|
|
|
|
|
|
|
void writeInstalledStickers();
|
|
|
|
void writeFeaturedStickers();
|
|
|
|
void writeRecentStickers();
|
|
|
|
void writeFavedStickers();
|
|
|
|
void writeArchivedStickers();
|
2021-03-30 15:51:45 +00:00
|
|
|
void writeArchivedMasks();
|
2020-06-09 16:57:05 +00:00
|
|
|
void readInstalledStickers();
|
|
|
|
void readFeaturedStickers();
|
|
|
|
void readRecentStickers();
|
|
|
|
void readFavedStickers();
|
|
|
|
void readArchivedStickers();
|
2021-03-30 15:51:45 +00:00
|
|
|
void readArchivedMasks();
|
2020-06-09 16:57:05 +00:00
|
|
|
void writeSavedGifs();
|
|
|
|
void readSavedGifs();
|
2021-03-25 11:05:33 +00:00
|
|
|
void writeInstalledMasks();
|
|
|
|
void writeRecentMasks();
|
|
|
|
void readInstalledMasks();
|
|
|
|
void readRecentMasks();
|
2020-06-09 16:57:05 +00:00
|
|
|
|
|
|
|
void writeRecentHashtagsAndBots();
|
|
|
|
void readRecentHashtagsAndBots();
|
|
|
|
void saveRecentSentHashtags(const QString &text);
|
|
|
|
void saveRecentSearchHashtags(const QString &text);
|
|
|
|
|
|
|
|
void writeExportSettings(const Export::Settings &settings);
|
|
|
|
[[nodiscard]] Export::Settings readExportSettings();
|
|
|
|
|
|
|
|
void writeSelf();
|
2020-06-10 13:24:41 +00:00
|
|
|
|
|
|
|
// Read self is special, it can't get session from account, because
|
|
|
|
// it is not really there yet - it is still being constructed.
|
|
|
|
void readSelf(
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
const QByteArray& serialized,
|
|
|
|
int32 streamVersion);
|
2020-06-09 16:57:05 +00:00
|
|
|
|
2021-04-02 12:59:54 +00:00
|
|
|
void markBotTrustedOpenGame(PeerId botId);
|
|
|
|
[[nodiscard]] bool isBotTrustedOpenGame(PeerId botId);
|
|
|
|
void markBotTrustedPayment(PeerId botId);
|
|
|
|
[[nodiscard]] bool isBotTrustedPayment(PeerId botId);
|
2020-06-09 16:57:05 +00:00
|
|
|
|
|
|
|
[[nodiscard]] bool encrypt(
|
|
|
|
const void *src,
|
|
|
|
void *dst,
|
|
|
|
uint32 len,
|
|
|
|
const void *key128) const;
|
|
|
|
[[nodiscard]] bool decrypt(
|
|
|
|
const void *src,
|
|
|
|
void *dst,
|
|
|
|
uint32 len,
|
|
|
|
const void *key128) const;
|
|
|
|
|
|
|
|
void reset();
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum class ReadMapResult {
|
|
|
|
Success,
|
|
|
|
IncorrectPasscode,
|
|
|
|
Failed,
|
|
|
|
};
|
2021-04-02 12:59:54 +00:00
|
|
|
enum class BotTrustFlag : uchar {
|
|
|
|
NoOpenGame = (1 << 0),
|
|
|
|
Payment = (1 << 1),
|
|
|
|
};
|
|
|
|
friend inline constexpr bool is_flag_type(BotTrustFlag) { return true; };
|
2020-06-09 16:57:05 +00:00
|
|
|
|
|
|
|
[[nodiscard]] base::flat_set<QString> collectGoodNames() const;
|
|
|
|
[[nodiscard]] auto prepareReadSettingsContext() const
|
|
|
|
-> details::ReadSettingsContext;
|
|
|
|
|
2020-06-15 16:25:02 +00:00
|
|
|
ReadMapResult readMapWith(
|
|
|
|
MTP::AuthKeyPtr localKey,
|
|
|
|
const QByteArray &legacyPasscode = QByteArray());
|
|
|
|
void clearLegacyFiles();
|
2020-06-09 16:57:05 +00:00
|
|
|
void writeMapDelayed();
|
|
|
|
void writeMapQueued();
|
|
|
|
void writeMap();
|
|
|
|
|
|
|
|
void readLocations();
|
|
|
|
void writeLocations();
|
|
|
|
void writeLocationsQueued();
|
|
|
|
void writeLocationsDelayed();
|
|
|
|
|
2020-06-18 12:47:09 +00:00
|
|
|
std::unique_ptr<Main::SessionSettings> readSessionSettings();
|
|
|
|
void writeSessionSettings(Main::SessionSettings *stored);
|
2020-06-09 16:57:05 +00:00
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
std::unique_ptr<MTP::Config> readMtpConfig();
|
2020-06-09 16:57:05 +00:00
|
|
|
void readMtpData();
|
2020-06-18 12:47:09 +00:00
|
|
|
std::unique_ptr<Main::SessionSettings> applyReadContext(
|
2020-06-09 16:57:05 +00:00
|
|
|
details::ReadSettingsContext &&context);
|
|
|
|
|
2020-11-13 17:27:08 +00:00
|
|
|
void readDraftCursors(PeerId peerId, Data::HistoryDrafts &map);
|
|
|
|
void readDraftCursorsLegacy(
|
|
|
|
PeerId peerId,
|
|
|
|
details::FileReadDescriptor &draft,
|
2021-04-01 21:04:10 +00:00
|
|
|
quint64 draftPeerSerialized,
|
2020-11-13 17:27:08 +00:00
|
|
|
Data::HistoryDrafts &map);
|
|
|
|
void clearDraftCursors(PeerId peerId);
|
|
|
|
void readDraftsWithCursorsLegacy(
|
|
|
|
not_null<History*> history,
|
|
|
|
details::FileReadDescriptor &draft,
|
2021-04-01 21:04:10 +00:00
|
|
|
quint64 draftPeerSerialized);
|
2020-06-09 16:57:05 +00:00
|
|
|
|
|
|
|
void writeStickerSet(
|
|
|
|
QDataStream &stream,
|
|
|
|
const Data::StickersSet &set);
|
|
|
|
template <typename CheckSet>
|
|
|
|
void writeStickerSets(
|
|
|
|
FileKey &stickersKey,
|
|
|
|
CheckSet checkSet,
|
|
|
|
const Data::StickersSetsOrder &order);
|
|
|
|
void readStickerSets(
|
|
|
|
FileKey &stickersKey,
|
|
|
|
Data::StickersSetsOrder *outOrder = nullptr,
|
2021-07-09 06:08:58 +00:00
|
|
|
Data::StickersSetFlags readingFlags = 0);
|
2020-06-09 16:57:05 +00:00
|
|
|
void importOldRecentStickers();
|
|
|
|
|
|
|
|
void readTrustedBots();
|
|
|
|
void writeTrustedBots();
|
|
|
|
|
|
|
|
std::optional<RecentHashtagPack> saveRecentHashtags(
|
|
|
|
Fn<RecentHashtagPack()> getPack,
|
|
|
|
const QString &text);
|
|
|
|
|
|
|
|
const not_null<Main::Account*> _owner;
|
|
|
|
const QString _dataName;
|
|
|
|
const FileKey _dataNameKey = 0;
|
|
|
|
const QString _basePath;
|
2020-06-23 16:02:15 +00:00
|
|
|
const QString _tempPath;
|
2020-06-09 16:57:05 +00:00
|
|
|
const QString _databasePath;
|
|
|
|
|
|
|
|
MTP::AuthKeyPtr _localKey;
|
|
|
|
|
|
|
|
base::flat_map<PeerId, FileKey> _draftsMap;
|
|
|
|
base::flat_map<PeerId, FileKey> _draftCursorsMap;
|
|
|
|
base::flat_map<PeerId, bool> _draftsNotReadMap;
|
2021-08-30 15:31:01 +00:00
|
|
|
base::flat_map<
|
|
|
|
not_null<History*>,
|
|
|
|
base::flat_map<Data::DraftKey, MessageDraftSource>> _draftSources;
|
2020-06-09 16:57:05 +00:00
|
|
|
|
2020-10-13 16:43:18 +00:00
|
|
|
QMultiMap<MediaKey, Core::FileLocation> _fileLocations;
|
|
|
|
QMap<QString, QPair<MediaKey, Core::FileLocation>> _fileLocationPairs;
|
2020-06-09 16:57:05 +00:00
|
|
|
QMap<MediaKey, MediaKey> _fileLocationAliases;
|
|
|
|
|
|
|
|
FileKey _locationsKey = 0;
|
|
|
|
FileKey _trustedBotsKey = 0;
|
|
|
|
FileKey _installedStickersKey = 0;
|
|
|
|
FileKey _featuredStickersKey = 0;
|
|
|
|
FileKey _recentStickersKey = 0;
|
|
|
|
FileKey _favedStickersKey = 0;
|
|
|
|
FileKey _archivedStickersKey = 0;
|
2021-03-30 15:51:45 +00:00
|
|
|
FileKey _archivedMasksKey = 0;
|
2020-06-09 16:57:05 +00:00
|
|
|
FileKey _savedGifsKey = 0;
|
|
|
|
FileKey _recentStickersKeyOld = 0;
|
2020-06-19 15:14:55 +00:00
|
|
|
FileKey _legacyBackgroundKeyDay = 0;
|
|
|
|
FileKey _legacyBackgroundKeyNight = 0;
|
2020-06-09 16:57:05 +00:00
|
|
|
FileKey _settingsKey = 0;
|
|
|
|
FileKey _recentHashtagsAndBotsKey = 0;
|
|
|
|
FileKey _exportSettingsKey = 0;
|
2021-03-25 11:05:33 +00:00
|
|
|
FileKey _installedMasksKey = 0;
|
|
|
|
FileKey _recentMasksKey = 0;
|
2020-06-09 16:57:05 +00:00
|
|
|
|
|
|
|
qint64 _cacheTotalSizeLimit = 0;
|
|
|
|
qint64 _cacheBigFileTotalSizeLimit = 0;
|
|
|
|
qint32 _cacheTotalTimeLimit = 0;
|
|
|
|
qint32 _cacheBigFileTotalTimeLimit = 0;
|
|
|
|
|
2021-04-02 12:59:54 +00:00
|
|
|
base::flat_map<PeerId, base::flags<BotTrustFlag>> _trustedBots;
|
2020-06-09 16:57:05 +00:00
|
|
|
bool _trustedBotsRead = false;
|
|
|
|
bool _readingUserSettings = false;
|
|
|
|
bool _recentHashtagsAndBotsWereRead = false;
|
|
|
|
|
|
|
|
int _oldMapVersion = 0;
|
|
|
|
|
|
|
|
base::Timer _writeMapTimer;
|
|
|
|
base::Timer _writeLocationsTimer;
|
|
|
|
bool _mapChanged = false;
|
|
|
|
bool _locationsChanged = false;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Storage
|