Redirect to the main page if we have an account.

Add API to account list to check usable account.
On removed, redirect to login page if there is no more account(TODO: need SDK update for core callback)
This commit is contained in:
Julien Wadel 2023-11-29 10:49:32 +01:00
parent bdf1d197ec
commit 9e1e797d8c
6 changed files with 47 additions and 23 deletions

View file

@ -40,6 +40,7 @@ AccountList::AccountList(QObject *parent) : ListProxy(parent) {
qDebug() << "[AccountList] new" << this; qDebug() << "[AccountList] new" << this;
mustBeInMainThread(getClassName()); mustBeInMainThread(getClassName());
connect(CoreModel::getInstance().get(), &CoreModel::accountAdded, this, &AccountList::lUpdate); connect(CoreModel::getInstance().get(), &CoreModel::accountAdded, this, &AccountList::lUpdate);
connect(CoreModel::getInstance().get(), &CoreModel::accountRemoved, this, &AccountList::lUpdate);
} }
AccountList::~AccountList() { AccountList::~AccountList() {
@ -65,6 +66,7 @@ void AccountList::setSelf(QSharedPointer<AccountList> me) {
mustBeInMainThread(getClassName()); mustBeInMainThread(getClassName());
resetData(); resetData();
add(*accounts); add(*accounts);
setHaveAccount(accounts->size() > 0);
delete accounts; delete accounts;
}); });
}); });
@ -73,33 +75,29 @@ void AccountList::setSelf(QSharedPointer<AccountList> me) {
lUpdate(); lUpdate();
} }
AccountGui *AccountList::getDefaultAccount() const { QSharedPointer<AccountCore> AccountList::getDefaultAccountCore() const {
for (auto it : mList) { for (auto it : mList) {
auto account = it.objectCast<AccountCore>(); auto account = it.objectCast<AccountCore>();
if (account->getIsDefaultAccount()) return new AccountGui(account); if (account->getIsDefaultAccount()) return account;
} }
return nullptr; return nullptr;
} }
/*
void AccountList::update() { AccountGui *AccountList::getDefaultAccount() const {
App::postModelAsync([=]() { auto account = getDefaultAccountCore();
QList<QSharedPointer<AccountCore>> accounts; if (account) return new AccountGui(account);
// Model thread. else return nullptr;
mustBeInLinphoneThread(getClassName());
auto linphoneAccounts = CoreModel::getInstance()->getCore()->getAccountList();
for (auto it : linphoneAccounts) {
auto model = AccountCore::create(it);
accounts.push_back(model);
}
// Invoke for adding stuffs in caller thread
QMetaObject::invokeMethod(this, [this, accounts]() {
mustBeInMainThread(getClassName());
clearData();
add(accounts);
});
});
} }
*/ bool AccountList::getHaveAccount() const {
return mHaveAccount;
}
void AccountList::setHaveAccount(bool haveAccount) {
if (mHaveAccount != haveAccount) {
mHaveAccount = haveAccount;
emit haveAccountChanged();
}
}
QVariant AccountList::data(const QModelIndex &index, int role) const { QVariant AccountList::data(const QModelIndex &index, int role) const {
int row = index.row(); int row = index.row();
if (!index.isValid() || row < 0 || row >= mList.count()) return QVariant(); if (!index.isValid() || row < 0 || row >= mList.count()) return QVariant();

View file

@ -27,6 +27,7 @@
#include <QLocale> #include <QLocale>
class AccountGui; class AccountGui;
class AccountCore;
// ============================================================================= // =============================================================================
class AccountList : public ListProxy, public AbstractObject { class AccountList : public ListProxy, public AbstractObject {
@ -39,11 +40,18 @@ public:
void setSelf(QSharedPointer<AccountList> me); void setSelf(QSharedPointer<AccountList> me);
AccountGui *getDefaultAccount() const; AccountGui *getDefaultAccount() const;
QSharedPointer<AccountCore> getDefaultAccountCore() const;
bool getHaveAccount() const;
void setHaveAccount(bool haveAccount);
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
signals: signals:
void lUpdate(); void lUpdate();
void haveAccountChanged();
private: private:
bool mHaveAccount = false;
QSharedPointer<SafeConnection> mModelConnection; QSharedPointer<SafeConnection> mModelConnection;
DECLARE_ABSTRACT_OBJECT DECLARE_ABSTRACT_OBJECT
}; };

View file

@ -26,6 +26,7 @@ AccountProxy::AccountProxy(QObject *parent) : SortFilterProxy(parent) {
qDebug() << "[AccountProxy] new" << this; qDebug() << "[AccountProxy] new" << this;
mList = AccountList::create(); mList = AccountList::create();
connect(mList.get(), &AccountList::countChanged, this, &AccountProxy::defaultAccountChanged); connect(mList.get(), &AccountList::countChanged, this, &AccountProxy::defaultAccountChanged);
connect(mList.get(), &AccountList::haveAccountChanged, this, &AccountProxy::haveAccountChanged);
setSourceModel(mList.get()); setSourceModel(mList.get());
sort(0); sort(0);
} }
@ -53,6 +54,10 @@ AccountGui *AccountProxy::getDefaultAccount() const {
void AccountProxy::setDefaultAccount(AccountGui *account) { void AccountProxy::setDefaultAccount(AccountGui *account) {
} }
bool AccountProxy::getHaveAccount() const {
return dynamic_cast<AccountList *>(sourceModel())->getHaveAccount();
}
bool AccountProxy::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const { bool AccountProxy::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const {
bool show = (mFilterText.isEmpty() || mFilterText == "*"); bool show = (mFilterText.isEmpty() || mFilterText == "*");
if (!show) { if (!show) {

View file

@ -32,6 +32,7 @@ class AccountProxy : public SortFilterProxy {
Q_PROPERTY(QString filterText READ getFilterText WRITE setFilterText NOTIFY filterTextChanged) Q_PROPERTY(QString filterText READ getFilterText WRITE setFilterText NOTIFY filterTextChanged)
Q_PROPERTY(AccountGui *defaultAccount READ getDefaultAccount WRITE setDefaultAccount NOTIFY defaultAccountChanged) Q_PROPERTY(AccountGui *defaultAccount READ getDefaultAccount WRITE setDefaultAccount NOTIFY defaultAccountChanged)
Q_PROPERTY(bool haveAccount READ getHaveAccount NOTIFY haveAccountChanged)
public: public:
AccountProxy(QObject *parent = Q_NULLPTR); AccountProxy(QObject *parent = Q_NULLPTR);
@ -43,9 +44,12 @@ public:
AccountGui *getDefaultAccount() const; AccountGui *getDefaultAccount() const;
void setDefaultAccount(AccountGui *account); void setDefaultAccount(AccountGui *account);
bool getHaveAccount() const;
signals: signals:
void filterTextChanged(); void filterTextChanged();
void defaultAccountChanged(); void defaultAccountChanged();
void haveAccountChanged();
protected: protected:
virtual bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override; virtual bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;

View file

@ -159,6 +159,7 @@ private:
signals: signals:
void accountAdded(const std::shared_ptr<linphone::Core> &core, const std::shared_ptr<linphone::Account> &account); void accountAdded(const std::shared_ptr<linphone::Core> &core, const std::shared_ptr<linphone::Account> &account);
void accountRemoved();
void accountRegistrationStateChanged(const std::shared_ptr<linphone::Core> &core, void accountRegistrationStateChanged(const std::shared_ptr<linphone::Core> &core,
const std::shared_ptr<linphone::Account> &account, const std::shared_ptr<linphone::Account> &account,
linphone::RegistrationState state, linphone::RegistrationState state,

View file

@ -11,11 +11,19 @@ Window {
visible: true visible: true
title: qsTr("Linphone") title: qsTr("Linphone")
property bool firstConnection: true property bool firstConnection: true
AccountProxy{
id: accountProxy
onHaveAccountChanged: {
if(haveAccount)
mainWindowStackView.replace(mainPage)
else
mainWindowStackView.replace(loginPage)
}
}
StackView { StackView {
id: mainWindowStackView id: mainWindowStackView
anchors.fill: parent anchors.fill: parent
initialItem: welcomePage initialItem: accountProxy.haveAccount ? mainPage : welcomePage
} }
Component { Component {
id: welcomePage id: welcomePage