Check running thread and factorize debug class names.

This commit is contained in:
Julien Wadel 2023-10-30 09:18:29 +01:00
parent f4e8ec0f26
commit 978c57f1ed
21 changed files with 164 additions and 19 deletions

View file

@ -34,6 +34,7 @@
#include "core/phone-number/PhoneNumberProxy.hpp"
#include "core/singleapplication/singleapplication.h"
#include "tool/Constants.hpp"
#include "tool/thread/Thread.hpp"
#include "tool/providers/ImageProvider.hpp"

View file

@ -23,9 +23,10 @@
#include <QSharedPointer>
#include "core/singleapplication/singleapplication.h"
#include "core/thread/Thread.hpp"
#include "model/core/CoreModel.hpp"
class Thread;
class App : public SingleApplication {
public:
App(int &argc, char *argv[]);

View file

@ -11,7 +11,6 @@ list(APPEND _LINPHONEAPP_SOURCES
core/phone-number/PhoneNumberProxy.cpp
core/setting/Settings.cpp
core/thread/Thread.cpp
core/proxy/ListProxy.cpp
core/proxy/Proxy.cpp

View file

@ -21,8 +21,11 @@
#include "Account.hpp"
#include "tool/Utils.hpp"
DEFINE_ABSTRACT_OBJECT(Account)
Account::Account(const std::shared_ptr<linphone::Account> &account) : QObject(nullptr) {
// Should be call from model Thread
mustBeInLinphoneThread(getClassName());
auto address = account->getContactAddress();
mContactAddress = address ? Utils::coreStringToAppString(account->getContactAddress()->asString()) : "";
auto params = account->getParams();
@ -40,6 +43,7 @@ Account::Account(const std::shared_ptr<linphone::Account> &account) : QObject(nu
}
Account::~Account() {
mustBeInMainThread("~" + getClassName());
emit mAccountModel->removeListener();
}

View file

@ -27,7 +27,7 @@
#include <QSharedPointer>
#include <linphone++/linphone.hh>
class Account : public QObject {
class Account : public QObject, public AbstractObject {
Q_OBJECT
Q_PROPERTY(QString contactAddress READ getContactAddress CONSTANT)
@ -66,6 +66,8 @@ private:
QString mPictureUri;
LinphoneEnums::RegistrationState mRegistrationState;
std::shared_ptr<AccountModel> mAccountModel;
DECLARE_ABSTRACT_OBJECT
};
#endif

View file

@ -26,11 +26,14 @@
// =============================================================================
AccountList::AccountList(QObject *parent) : ListProxy(parent) {
DEFINE_ABSTRACT_OBJECT(AccountList)
AccountList::AccountList(QObject *parent) : ListProxy(parent) {
mustBeInMainThread(getClassName());
App::postModelAsync([=]() {
QList<QSharedPointer<Account>> accounts;
// Model thread.
mustBeInLinphoneThread(getClassName());
auto linphoneAccounts = CoreModel::getInstance()->getCore()->getAccountList();
for (auto it : linphoneAccounts) {
auto model = QSharedPointer<Account>(new Account(it), &QObject::deleteLater);
@ -38,9 +41,13 @@ AccountList::AccountList(QObject *parent) : ListProxy(parent) {
accounts.push_back(model);
}
// Invoke for adding stuffs in caller thread
QMetaObject::invokeMethod(this, [this, accounts]() { add(accounts); });
QMetaObject::invokeMethod(this, [this, accounts]() {
mustBeInMainThread(getClassName());
add(accounts);
});
});
}
AccountList::~AccountList() {
mustBeInMainThread("~" + getClassName());
}

View file

@ -22,14 +22,17 @@
#define ACCOUNT_LIST_H_
#include "../proxy/ListProxy.hpp"
#include "tool/AbstractObject.hpp"
#include <QLocale>
// =============================================================================
class AccountList : public ListProxy {
class AccountList : public ListProxy, public AbstractObject {
Q_OBJECT
public:
AccountList(QObject *parent = Q_NULLPTR);
~AccountList();
DECLARE_ABSTRACT_OBJECT
};
#endif

View file

@ -21,9 +21,11 @@
#include "PhoneNumber.hpp"
#include "tool/Utils.hpp"
#include <QApplication>
DEFINE_ABSTRACT_OBJECT(PhoneNumber)
PhoneNumber::PhoneNumber(const std::shared_ptr<linphone::DialPlan> &dialPlan) : QObject(nullptr) {
// Should be call from model Thread
mustBeInLinphoneThread(getClassName());
mFlag = Utils::coreStringToAppString(dialPlan->getFlag());
mNationalNumberLength = dialPlan->getNationalNumberLength();
mCountryCallingCode = Utils::coreStringToAppString(dialPlan->getCountryCallingCode());
@ -31,3 +33,7 @@ PhoneNumber::PhoneNumber(const std::shared_ptr<linphone::DialPlan> &dialPlan) :
mInternationalCallPrefix = Utils::coreStringToAppString(dialPlan->getInternationalCallPrefix());
mCountry = Utils::coreStringToAppString(dialPlan->getCountry());
}
PhoneNumber::~PhoneNumber() {
mustBeInMainThread(getClassName());
}

View file

@ -21,10 +21,11 @@
#ifndef PHONE_NUMBER_H_
#define PHONE_NUMBER_H_
#include "tool/AbstractObject.hpp"
#include <QObject>
#include <linphone++/linphone.hh>
class PhoneNumber : public QObject {
class PhoneNumber : public QObject, public AbstractObject {
Q_OBJECT
Q_PROPERTY(QString flag MEMBER mFlag CONSTANT)
@ -37,6 +38,7 @@ class PhoneNumber : public QObject {
public:
// Should be call from model Thread. Will be automatically in App thread after initialization
PhoneNumber(const std::shared_ptr<linphone::DialPlan> &dialPlan);
~PhoneNumber();
QString mFlag;
int mNationalNumberLength;
@ -44,6 +46,8 @@ public:
QString mIsoCountryCode;
QString mInternationalCallPrefix;
QString mCountry;
DECLARE_ABSTRACT_OBJECT
};
#endif

View file

@ -22,12 +22,14 @@
#include "PhoneNumber.hpp"
#include "core/App.hpp"
#include <QSharedPointer>
#include <QString>
#include <linphone++/linphone.hh>
// =============================================================================
PhoneNumberList::PhoneNumberList(QObject *parent) : ListProxy(parent) {
DEFINE_ABSTRACT_OBJECT(PhoneNumberList)
PhoneNumberList::PhoneNumberList(QObject *parent) : ListProxy(parent) {
mustBeInMainThread(getClassName());
App::postModelAsync([=]() {
// Model thread.
auto dialPlans = linphone::Factory::get()->getDialPlans();
@ -39,6 +41,13 @@ PhoneNumberList::PhoneNumberList(QObject *parent) : ListProxy(parent) {
numbers.push_back(numberModel);
}
// Invoke for adding stuffs in caller thread
QMetaObject::invokeMethod(this, [this, numbers]() { add(numbers); });
QMetaObject::invokeMethod(this, [this, numbers]() {
mustBeInMainThread(this->log().arg(Q_FUNC_INFO));
add(numbers);
});
});
}
PhoneNumberList::~PhoneNumberList() {
mustBeInMainThread("~" + getClassName());
}

View file

@ -22,13 +22,17 @@
#define PHONE_NUMBER_LIST_H_
#include "../proxy/ListProxy.hpp"
#include "tool/AbstractObject.hpp"
#include <QLocale>
// =============================================================================
class PhoneNumberList : public ListProxy {
class PhoneNumberList : public ListProxy, public AbstractObject {
Q_OBJECT
public:
PhoneNumberList(QObject *parent = Q_NULLPTR);
~PhoneNumberList();
DECLARE_ABSTRACT_OBJECT
};
#endif

View file

@ -27,13 +27,21 @@
#include "model/core/CoreModel.hpp"
#include "tool/Utils.hpp"
DEFINE_ABSTRACT_OBJECT(AccountManager)
AccountManager::AccountManager(QObject *parent) : QObject(parent) {
mustBeInLinphoneThread(getClassName());
}
AccountManager::~AccountManager() {
mustBeInLinphoneThread("~" + getClassName());
}
std::shared_ptr<linphone::Account> AccountManager::createAccount(const QString &assistantFile) {
mustBeInLinphoneThread(log().arg(Q_FUNC_INFO));
auto core = CoreModel::getInstance()->getCore();
QString assistantPath = "://data/assistant/" + assistantFile;
qInfo() << QStringLiteral("[AccountManager] Set config on assistant: `%1`.").arg(assistantPath);
qInfo() << log().arg(QStringLiteral("Set config on assistant: `%1`.")).arg(assistantPath);
QFile resource(assistantPath);
auto file = QTemporaryFile::createNativeFile(resource);
core->getConfig()->loadFromXmlFile(Utils::appStringToCoreString(file->fileName()));
@ -41,6 +49,7 @@ std::shared_ptr<linphone::Account> AccountManager::createAccount(const QString &
}
bool AccountManager::login(QString username, QString password) {
mustBeInLinphoneThread(log().arg(Q_FUNC_INFO));
auto core = CoreModel::getInstance()->getCore();
auto factory = linphone::Factory::get();
auto account = createAccount("use-app-sip-account.rc");
@ -51,7 +60,8 @@ bool AccountManager::login(QString username, QString password) {
identity->setUsername(Utils::appStringToCoreString(username));
if (params->setIdentityAddress(identity)) {
qWarning() << QStringLiteral("[AccountManager] Unable to set identity address: `%1`.")
qWarning() << log()
.arg(QStringLiteral("Unable to set identity address: `%1`."))
.arg(Utils::coreStringToAppString(identity->asStringUriOnly()));
return false;
}

View file

@ -25,11 +25,13 @@
#include <linphone++/linphone.hh>
#include "AccountModel.hpp"
#include "tool/AbstractObject.hpp"
class AccountManager : public QObject {
class AccountManager : public QObject, public AbstractObject {
Q_OBJECT
public:
AccountManager(QObject *parent = nullptr);
~AccountManager();
bool login(QString username, QString password);
@ -43,6 +45,7 @@ signals:
private:
std::shared_ptr<AccountModel> mAccountModel;
DECLARE_ABSTRACT_OBJECT
};
#endif

View file

@ -22,11 +22,15 @@
#include <QDebug>
DEFINE_ABSTRACT_OBJECT(AccountModel)
AccountModel::AccountModel(const std::shared_ptr<linphone::Account> &account, QObject *parent)
: ::Listener<linphone::Account, linphone::AccountListener>(account, parent) {
mustBeInLinphoneThread(getClassName());
}
AccountModel::~AccountModel() {
mustBeInLinphoneThread("~" + getClassName());
}
void AccountModel::onRegistrationStateChanged(const std::shared_ptr<linphone::Account> &account,
@ -36,6 +40,7 @@ void AccountModel::onRegistrationStateChanged(const std::shared_ptr<linphone::Ac
}
void AccountModel::setPictureUri(std::string uri) {
mustBeInLinphoneThread(log().arg(Q_FUNC_INFO));
auto account = std::dynamic_pointer_cast<linphone::Account>(mMonitor);
auto params = account->getParams()->clone();
params->setPictureUri(uri);

View file

@ -22,11 +22,14 @@
#define ACCOUNT_MODEL_H_
#include "model/listener/Listener.hpp"
#include "tool/AbstractObject.hpp"
#include <QObject>
#include <linphone++/linphone.hh>
class AccountModel : public ::Listener<linphone::Account, linphone::AccountListener>, public linphone::AccountListener {
class AccountModel : public ::Listener<linphone::Account, linphone::AccountListener>,
public linphone::AccountListener,
public AbstractObject {
Q_OBJECT
public:
AccountModel(const std::shared_ptr<linphone::Account> &account, QObject *parent = nullptr);
@ -44,6 +47,9 @@ signals:
const std::string &message);
void pictureUriChanged(std::string uri);
private:
DECLARE_ABSTRACT_OBJECT
};
#endif

View file

@ -19,15 +19,18 @@
*/
#include "SettingsModel.hpp"
// =============================================================================
DEFINE_ABSTRACT_OBJECT(SettingsModel)
const std::string SettingsModel::UiSection("ui");
SettingsModel::SettingsModel(QObject *parent) : QObject(parent) {
mustBeInLinphoneThread(getClassName());
}
SettingsModel::~SettingsModel() {
mustBeInLinphoneThread("~" + getClassName());
}
bool SettingsModel::isReadOnly(const std::string &section, const std::string &name) const {
@ -35,5 +38,6 @@ bool SettingsModel::isReadOnly(const std::string &section, const std::string &na
}
std::string SettingsModel::getEntryFullName(const std::string &section, const std::string &name) const {
mustBeInLinphoneThread(log().arg(Q_FUNC_INFO));
return isReadOnly(section, name) ? name + "/readonly" : name;
}

View file

@ -26,7 +26,9 @@
#include <QVariantMap>
#include <linphone++/linphone.hh>
class SettingsModel : public QObject {
#include "tool/AbstractObject.hpp"
class SettingsModel : public QObject, public AbstractObject {
Q_OBJECT
public:
SettingsModel(QObject *parent = Q_NULLPTR);
@ -40,5 +42,8 @@ public:
static const std::string UiSection;
std::shared_ptr<linphone::Config> mConfig;
private:
DECLARE_ABSTRACT_OBJECT
};
#endif // SETTINGS_MODEL_H_

View file

@ -0,0 +1,52 @@
/*
* Copyright (c) 2010-2024 Belledonne Communications SARL.
*
* This file is part of linphone-desktop
* (see https://www.linphone.org).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ABSTRACT_OBJECT_H_
#define ABSTRACT_OBJECT_H_
#include <QString>
#include "thread/Thread.hpp"
#define DECLARE_ABSTRACT_OBJECT \
virtual QString getClassName() const override; \
static const char *gClassName;
#define DEFINE_ABSTRACT_OBJECT(CLASS_NAME) \
const char *CLASS_NAME::gClassName = #CLASS_NAME; \
QString CLASS_NAME::getClassName() const { \
return gClassName; \
}
class AbstractObject {
public:
virtual QString getClassName() const = 0;
// return "[ClassName]: %1"
inline QString log() const {
return QStringLiteral("[%1]: %2").arg(getClassName()).arg("%1");
}
inline static bool mustBeInLinphoneThread(const QString &context) { // For convenience : Alias to Thread
return Thread::mustBeInLinphoneThread(context);
}
inline static bool mustBeInMainThread(const QString &context) { // For convenience : Alias to Thread
return Thread::mustBeInMainThread(context);
}
};
#endif

View file

@ -2,7 +2,7 @@ list(APPEND _LINPHONEAPP_SOURCES
tool/Constants.cpp
tool/Utils.cpp
tool/LinphoneEnums.cpp
tool/thread/Thread.cpp
tool/providers/ImageProvider.cpp
)

View file

@ -19,6 +19,9 @@
*/
#include "Thread.hpp"
#include "core/App.hpp"
#include "model/core/CoreModel.hpp"
#include <QDebug>
Thread::Thread(QObject *parent) : QThread(parent) {
}
@ -30,3 +33,14 @@ void Thread::run() {
if (result <= 0) toExit = true;
}
}
bool Thread::mustBeInLinphoneThread(const QString &context) {
bool isLinphoneThread = QThread::currentThread() == CoreModel::getInstance()->thread();
if (!isLinphoneThread) qCritical() << "[Thread] Not processing in Linphone thread from " << context;
return isLinphoneThread;
}
bool Thread::mustBeInMainThread(const QString &context) {
bool isMainThread = QThread::currentThread() == App::getInstance()->thread();
if (!isMainThread) qCritical() << "[Thread] Not processing in Main thread from " << context;
return isMainThread;
}

View file

@ -18,11 +18,17 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef THREAD_H_
#define THREAD_H_
#include <QThread>
class Thread : public QThread {
public:
Thread(QObject *parent = nullptr);
static bool mustBeInLinphoneThread(const QString &context);
static bool mustBeInMainThread(const QString &context);
virtual void run();
};
};
#endif