Fix crash at exit:

Do not use QSharedPointer on object managed by GUI.
Use Proxies to propagate list.
This commit is contained in:
Julien Wadel 2024-09-30 10:49:49 +02:00
parent 860f0cd297
commit 228b922d3e
6 changed files with 31 additions and 33 deletions

View file

@ -37,13 +37,6 @@ QSharedPointer<MagicSearchList> MagicSearchList::create() {
return model; return model;
} }
QSharedPointer<MagicSearchList> MagicSearchList::create(MagicSearchList *magicSearchList) {
auto model = QSharedPointer<MagicSearchList>(magicSearchList, &QObject::deleteLater);
model->moveToThread(App::getInstance()->thread());
model->setSelf(model);
return model;
}
MagicSearchList::MagicSearchList(QObject *parent) : ListProxy(parent) { MagicSearchList::MagicSearchList(QObject *parent) : ListProxy(parent) {
mustBeInMainThread(getClassName()); mustBeInMainThread(getClassName());
mSourceFlags = (int)linphone::MagicSearch::Source::Friends | (int)linphone::MagicSearch::Source::LdapServers; mSourceFlags = (int)linphone::MagicSearch::Source::Friends | (int)linphone::MagicSearch::Source::LdapServers;
@ -130,6 +123,7 @@ void MagicSearchList::setSelf(QSharedPointer<MagicSearchList> me) {
delete contacts; delete contacts;
}); });
}); });
qDebug() << log().arg("Initialized");
emit initialized(); emit initialized();
}); });
}); });

View file

@ -36,7 +36,6 @@ class MagicSearchList : public ListProxy, public AbstractObject {
Q_OBJECT Q_OBJECT
public: public:
static QSharedPointer<MagicSearchList> create(); static QSharedPointer<MagicSearchList> create();
static QSharedPointer<MagicSearchList> create(MagicSearchList *magicSearchList);
MagicSearchList(QObject *parent = Q_NULLPTR); MagicSearchList(QObject *parent = Q_NULLPTR);
~MagicSearchList(); ~MagicSearchList();

View file

@ -25,11 +25,10 @@
MagicSearchProxy::MagicSearchProxy(QObject *parent) : SortFilterProxy(parent) { MagicSearchProxy::MagicSearchProxy(QObject *parent) : SortFilterProxy(parent) {
mSourceFlags = (int)LinphoneEnums::MagicSearchSource::Friends | (int)LinphoneEnums::MagicSearchSource::LdapServers; mSourceFlags = (int)LinphoneEnums::MagicSearchSource::Friends | (int)LinphoneEnums::MagicSearchSource::LdapServers;
mAggregationFlag = LinphoneEnums::MagicSearchAggregation::Friend; mAggregationFlag = LinphoneEnums::MagicSearchAggregation::Friend;
setSourceModel(new MagicSearchList()); setList(MagicSearchList::create());
sort(0); sort(0);
connect(this, &MagicSearchProxy::forceUpdate, [this] { connect(this, &MagicSearchProxy::forceUpdate, [this] {
auto magicSearchList = qobject_cast<MagicSearchList *>(sourceModel()); if (mList) emit mList->lSearch(mSearchText);
if (magicSearchList) emit magicSearchList->lSearch(mSearchText);
}); });
} }
@ -37,28 +36,26 @@ MagicSearchProxy::~MagicSearchProxy() {
setSourceModel(nullptr); setSourceModel(nullptr);
} }
void MagicSearchProxy::setSourceModel(QAbstractItemModel *model) { void MagicSearchProxy::setList(QSharedPointer<MagicSearchList> newList) {
auto oldMagicSearchList = dynamic_cast<MagicSearchList *>(sourceModel()); if (mList == newList) return;
if (oldMagicSearchList) { if (mList) {
disconnect(oldMagicSearchList); disconnect(mList.get());
} }
auto newMagicSearchList = dynamic_cast<MagicSearchList *>(model); mList = newList;
if (newMagicSearchList) { if (mList) {
mList = MagicSearchList::create(newMagicSearchList); connect(mList.get(), &MagicSearchList::sourceFlagsChanged, this, &MagicSearchProxy::sourceFlagsChanged);
connect(newMagicSearchList, &MagicSearchList::sourceFlagsChanged, this, &MagicSearchProxy::sourceFlagsChanged); connect(mList.get(), &MagicSearchList::aggregationFlagChanged, this, &MagicSearchProxy::aggregationFlagChanged);
connect(newMagicSearchList, &MagicSearchList::aggregationFlagChanged, this, connect(mList.get(), &MagicSearchList::friendCreated, this, [this](int index) {
&MagicSearchProxy::aggregationFlagChanged);
connect(newMagicSearchList, &MagicSearchList::friendCreated, this, [this](int index) {
auto proxyIndex = mapFromSource(sourceModel()->index(index, 0)); auto proxyIndex = mapFromSource(sourceModel()->index(index, 0));
emit friendCreated(proxyIndex.row()); emit friendCreated(proxyIndex.row());
}); });
connect(newMagicSearchList, &MagicSearchList::initialized, this, [this, newMagicSearchList] { connect(mList.get(), &MagicSearchList::initialized, this, [this, newList = mList.get()] {
emit newMagicSearchList->lSetSourceFlags(mSourceFlags); emit newList->lSetSourceFlags(mSourceFlags);
emit newMagicSearchList->lSetAggregationFlag(mAggregationFlag); emit newList->lSetAggregationFlag(mAggregationFlag);
emit initialized(); emit initialized();
}); });
} }
QSortFilterProxyModel::setSourceModel(model); setSourceModel(mList.get());
} }
int MagicSearchProxy::findFriendIndexByAddress(const QString &address) { int MagicSearchProxy::findFriendIndexByAddress(const QString &address) {
@ -101,6 +98,11 @@ void MagicSearchProxy::setShowFavoritesOnly(bool show) {
} }
} }
void MagicSearchProxy::setParentProxy(MagicSearchProxy *proxy) {
setList(proxy->mList);
emit parentProxyChanged();
}
LinphoneEnums::MagicSearchAggregation MagicSearchProxy::getAggregationFlag() const { LinphoneEnums::MagicSearchAggregation MagicSearchProxy::getAggregationFlag() const {
return mAggregationFlag; return mAggregationFlag;
} }

View file

@ -35,6 +35,7 @@ class MagicSearchProxy : public SortFilterProxy {
Q_PROPERTY(LinphoneEnums::MagicSearchAggregation aggregationFlag READ getAggregationFlag WRITE setAggregationFlag Q_PROPERTY(LinphoneEnums::MagicSearchAggregation aggregationFlag READ getAggregationFlag WRITE setAggregationFlag
NOTIFY aggregationFlagChanged) NOTIFY aggregationFlagChanged)
Q_PROPERTY(bool showFavoritesOnly READ showFavoritesOnly WRITE setShowFavoritesOnly NOTIFY showFavoriteOnlyChanged) Q_PROPERTY(bool showFavoritesOnly READ showFavoritesOnly WRITE setShowFavoritesOnly NOTIFY showFavoriteOnlyChanged)
Q_PROPERTY(MagicSearchProxy *parentProxy WRITE setParentProxy NOTIFY parentProxyChanged)
public: public:
MagicSearchProxy(QObject *parent = Q_NULLPTR); MagicSearchProxy(QObject *parent = Q_NULLPTR);
@ -52,7 +53,8 @@ public:
bool showFavoritesOnly() const; bool showFavoritesOnly() const;
void setShowFavoritesOnly(bool show); void setShowFavoritesOnly(bool show);
void setSourceModel(QAbstractItemModel *sourceModel) override; void setList(QSharedPointer<MagicSearchList> list);
Q_INVOKABLE void setParentProxy(MagicSearchProxy *proxy);
// Q_INVOKABLE forceUpdate(); // Q_INVOKABLE forceUpdate();
Q_INVOKABLE int findFriendIndexByAddress(const QString &address); Q_INVOKABLE int findFriendIndexByAddress(const QString &address);
@ -64,6 +66,7 @@ signals:
void forceUpdate(); void forceUpdate();
void friendCreated(int index); void friendCreated(int index);
void showFavoriteOnlyChanged(); void showFavoriteOnlyChanged();
void parentProxyChanged();
void initialized(); void initialized();
protected: protected:

View file

@ -27,7 +27,7 @@ ListView {
property bool showFavoritesOnly: false property bool showFavoritesOnly: false
property bool showDefaultAddress: false property bool showDefaultAddress: false
property var sourceModel: MagicSearchList{} property var listProxy: MagicSearchProxy{}
// Model properties // Model properties
// set searchBarText without specifying a model to bold // set searchBarText without specifying a model to bold
@ -97,7 +97,7 @@ ListView {
mainItem.currentIndex = index mainItem.currentIndex = index
} }
aggregationFlag: mainItem.aggregationFlag aggregationFlag: mainItem.aggregationFlag
sourceModel: mainItem.sourceModel parentProxy: mainItem.listProxy
sourceFlags: LinphoneEnums.MagicSearchSource.Friends | ((mainItem.searchText.length > 0 && mainItem.searchText != "*") || SettingsCpp.syncLdapContacts ? LinphoneEnums.MagicSearchSource.LdapServers : 0) sourceFlags: LinphoneEnums.MagicSearchSource.Friends | ((mainItem.searchText.length > 0 && mainItem.searchText != "*") || SettingsCpp.syncLdapContacts ? LinphoneEnums.MagicSearchSource.LdapServers : 0)
onInitialized: { onInitialized: {
magicSearchProxy.forceUpdate() magicSearchProxy.forceUpdate()

View file

@ -59,7 +59,7 @@ AbstractMainPage {
showDefaultItem: contactList.model.sourceModel.count === 0 showDefaultItem: contactList.model.sourceModel.count === 0
MagicSearchList { MagicSearchProxy {
id: allFriends id: allFriends
} }
@ -281,7 +281,7 @@ AbstractMainPage {
showFavoritesOnly: true showFavoritesOnly: true
contactMenuVisible: true contactMenuVisible: true
searchBarText: searchBar.text searchBarText: searchBar.text
sourceModel: allFriends listProxy: allFriends
onSelectedContactChanged: { onSelectedContactChanged: {
if (selectedContact) { if (selectedContact) {
contactList.currentIndex = -1 contactList.currentIndex = -1
@ -338,7 +338,7 @@ AbstractMainPage {
contactMenuVisible: true contactMenuVisible: true
highlightFollowsCurrentItem: true highlightFollowsCurrentItem: true
searchBarText: searchBar.text searchBarText: searchBar.text
sourceModel: allFriends listProxy: allFriends
Connections { Connections {
target: contactList.model target: contactList.model
function onFriendCreated(index) { function onFriendCreated(index) {