2020-06-15 16:25:02 +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
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
#include "base/timer.h"
|
|
|
|
|
2020-06-15 16:25:02 +00:00
|
|
|
namespace Storage {
|
2020-06-18 18:04:16 +00:00
|
|
|
class Domain;
|
2020-06-15 16:25:02 +00:00
|
|
|
enum class StartResult : uchar;
|
|
|
|
} // namespace Storage
|
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
namespace MTP {
|
|
|
|
enum class Environment : uchar;
|
|
|
|
} // namespace MTP
|
|
|
|
|
2020-06-15 16:25:02 +00:00
|
|
|
namespace Main {
|
|
|
|
|
|
|
|
class Account;
|
|
|
|
class Session;
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
class Domain final {
|
2020-06-15 16:25:02 +00:00
|
|
|
public:
|
2020-06-19 12:59:31 +00:00
|
|
|
struct AccountWithIndex {
|
|
|
|
int index = 0;
|
|
|
|
std::unique_ptr<Account> account;
|
|
|
|
};
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
static constexpr auto kMaxAccounts = 3;
|
|
|
|
|
|
|
|
explicit Domain(const QString &dataName);
|
|
|
|
~Domain();
|
2020-06-15 16:25:02 +00:00
|
|
|
|
|
|
|
[[nodiscard]] bool started() const;
|
2020-06-16 09:40:43 +00:00
|
|
|
[[nodiscard]] Storage::StartResult start(const QByteArray &passcode);
|
|
|
|
void resetWithForgottenPasscode();
|
2020-06-17 09:36:25 +00:00
|
|
|
void finish();
|
2020-06-15 16:25:02 +00:00
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
[[nodiscard]] Storage::Domain &local() const {
|
2020-06-15 16:25:02 +00:00
|
|
|
return *_local;
|
|
|
|
}
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
[[nodiscard]] auto accounts() const
|
2020-06-19 12:59:31 +00:00
|
|
|
-> const std::vector<AccountWithIndex> &;
|
2020-06-15 16:25:02 +00:00
|
|
|
[[nodiscard]] rpl::producer<Account*> activeValue() const;
|
2020-06-22 13:01:41 +00:00
|
|
|
[[nodiscard]] rpl::producer<> accountsChanges() const;
|
2020-06-23 14:55:08 +00:00
|
|
|
[[nodiscard]] Account *maybeLastOrSomeAuthedAccount();
|
2020-06-15 16:25:02 +00:00
|
|
|
|
|
|
|
// Expects(started());
|
|
|
|
[[nodiscard]] Account &active() const;
|
|
|
|
[[nodiscard]] rpl::producer<not_null<Account*>> activeChanges() const;
|
|
|
|
|
|
|
|
[[nodiscard]] rpl::producer<Session*> activeSessionValue() const;
|
|
|
|
[[nodiscard]] rpl::producer<Session*> activeSessionChanges() const;
|
|
|
|
|
2020-06-18 11:17:58 +00:00
|
|
|
[[nodiscard]] int unreadBadge() const;
|
|
|
|
[[nodiscard]] bool unreadBadgeMuted() const;
|
|
|
|
[[nodiscard]] rpl::producer<> unreadBadgeChanges() const;
|
|
|
|
void notifyUnreadBadgeChanged();
|
|
|
|
|
2020-06-19 12:59:31 +00:00
|
|
|
[[nodiscard]] not_null<Main::Account*> add(MTP::Environment environment);
|
2020-12-13 13:08:16 +00:00
|
|
|
void maybeActivate(not_null<Main::Account*> account);
|
2020-06-19 12:59:31 +00:00
|
|
|
void activate(not_null<Main::Account*> account);
|
2020-06-30 17:16:05 +00:00
|
|
|
void addActivated(MTP::Environment environment);
|
2020-06-15 16:25:02 +00:00
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
// Interface for Storage::Domain.
|
2020-06-19 12:59:31 +00:00
|
|
|
void accountAddedInStorage(AccountWithIndex accountWithIndex);
|
|
|
|
void activateFromStorage(int index);
|
2020-06-19 17:13:43 +00:00
|
|
|
[[nodiscard]] int activeForStorage() const;
|
2020-06-16 09:40:43 +00:00
|
|
|
|
2020-06-15 16:25:02 +00:00
|
|
|
private:
|
2020-06-16 09:40:43 +00:00
|
|
|
void activateAfterStarting();
|
|
|
|
void activateAuthedAccount();
|
2020-06-16 14:19:23 +00:00
|
|
|
bool removePasscodeIfEmpty();
|
2020-06-16 09:40:43 +00:00
|
|
|
void removeRedundantAccounts();
|
|
|
|
void watchSession(not_null<Account*> account);
|
|
|
|
void scheduleWriteAccounts();
|
2020-06-17 09:36:25 +00:00
|
|
|
void checkForLastProductionConfig(not_null<Main::Account*> account);
|
2020-06-18 11:17:58 +00:00
|
|
|
void updateUnreadBadge();
|
|
|
|
void scheduleUpdateUnreadBadge();
|
2020-06-29 16:07:56 +00:00
|
|
|
void suggestExportIfNeeded();
|
2020-06-16 09:40:43 +00:00
|
|
|
|
2020-06-15 16:25:02 +00:00
|
|
|
const QString _dataName;
|
2020-06-18 18:04:16 +00:00
|
|
|
const std::unique_ptr<Storage::Domain> _local;
|
2020-06-15 16:25:02 +00:00
|
|
|
|
2020-06-19 12:59:31 +00:00
|
|
|
std::vector<AccountWithIndex> _accounts;
|
2020-06-22 13:01:41 +00:00
|
|
|
rpl::event_stream<> _accountsChanges;
|
2020-06-15 16:25:02 +00:00
|
|
|
rpl::variable<Account*> _active = nullptr;
|
2020-06-19 12:59:31 +00:00
|
|
|
int _accountToActivate = -1;
|
2020-06-23 14:55:08 +00:00
|
|
|
int _lastActiveIndex = -1;
|
2020-06-16 09:40:43 +00:00
|
|
|
bool _writeAccountsScheduled = false;
|
2020-06-15 16:25:02 +00:00
|
|
|
|
|
|
|
rpl::event_stream<Session*> _activeSessions;
|
|
|
|
|
2020-06-18 11:17:58 +00:00
|
|
|
rpl::event_stream<> _unreadBadgeChanges;
|
|
|
|
int _unreadBadge = 0;
|
|
|
|
bool _unreadBadgeMuted = true;
|
|
|
|
bool _unreadBadgeUpdateScheduled = false;
|
|
|
|
|
2020-06-15 16:25:02 +00:00
|
|
|
rpl::lifetime _activeLifetime;
|
|
|
|
rpl::lifetime _lifetime;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Main
|