From b869a34a488f504049f5a0d7de764afe572bfddf Mon Sep 17 00:00:00 2001
From: Ming Rui Zhang <mingrui.zhang@savoirfairelinux.com>
Date: Thu, 24 Sep 2020 14:02:28 -0400
Subject: [PATCH] misc: fix qml warnings

1. When there is no contact, some properties in smart list view could be null
2. When creating new accounts from no accounts, adding a new contact and
   switch accounts will cause "Contact out of range". This is caused by
   smartlist model account id does not update at that time. Instead,
   use current account id always.

Change-Id: Ic42ec33c4932908caff2ebca2814903f95c12a10
---
 src/accountadapter.cpp                          |  3 ++-
 src/conversationsadapter.cpp                    |  5 -----
 .../ConversationSmartListUserImage.qml          |  2 +-
 .../components/ConversationSmartListView.qml    |  2 ++
 .../ConversationSmartListViewItemDelegate.qml   |  4 ++--
 src/smartlistmodel.cpp                          | 17 +++++------------
 src/smartlistmodel.h                            |  4 ----
 7 files changed, 12 insertions(+), 25 deletions(-)

diff --git a/src/accountadapter.cpp b/src/accountadapter.cpp
index a516f56d8..693f1da2b 100644
--- a/src/accountadapter.cpp
+++ b/src/accountadapter.cpp
@@ -123,12 +123,13 @@ AccountAdapter::createJamiAccount(QString registeredName,
                 emit accountAdded(showBackup,
                                   LRCInstance::accountModel().getAccountList().indexOf(accountId));
             }
+
             // set up avatar pixmap from photobooth
             QImage avatarImg;
             const bool ret = avatarImg.loadFromData(
                 QByteArray::fromBase64(photoBoothImgBase64.toLatin1()));
             if (!ret) {
-                qDebug() << "JAMI account creation BASE64 image loading failed";
+                qDebug() << "No image provided for JAMI account creation";
             } else {
                 LRCInstance::setAvatarForAccount(QPixmap::fromImage(avatarImg), accountId);
             }
diff --git a/src/conversationsadapter.cpp b/src/conversationsadapter.cpp
index 3688345e0..3c3efbb57 100644
--- a/src/conversationsadapter.cpp
+++ b/src/conversationsadapter.cpp
@@ -147,8 +147,6 @@ void
 ConversationsAdapter::onCurrentAccountIdChanged()
 {
     auto accountId = LRCInstance::getCurrAccId();
-    // Should be called when current account is changed.
-    conversationSmartListModel_->setAccount(accountId);
 
     auto& accountInfo = LRCInstance::accountModel().getAccountInfo(accountId);
     currentTypeFilter_ = accountInfo.profileInfo.type;
@@ -276,7 +274,6 @@ ConversationsAdapter::connectConversationModel(bool updateFilter)
                            &lrc::api::ConversationModel::filterChanged,
                            [this]() {
                                conversationSmartListModel_->fillConversationsList();
-                               conversationSmartListModel_->setAccount(LRCInstance::getCurrAccId());
                                updateConversationsFilterWidget();
                                emit updateListViewRequested();
                            });
@@ -286,7 +283,6 @@ ConversationsAdapter::connectConversationModel(bool updateFilter)
                            &lrc::api::ConversationModel::newConversation,
                            [this](const QString& convUid) {
                                conversationSmartListModel_->fillConversationsList();
-                               conversationSmartListModel_->setAccount(LRCInstance::getCurrAccId());
                                updateConversationForNewContact(convUid);
                            });
 
@@ -320,7 +316,6 @@ ConversationsAdapter::connectConversationModel(bool updateFilter)
                            &lrc::api::ConversationModel::searchResultUpdated,
                            [this]() {
                                conversationSmartListModel_->fillConversationsList();
-                               conversationSmartListModel_->setAccount(LRCInstance::getCurrAccId());
                                emit updateListViewRequested();
                            });
 
diff --git a/src/mainview/components/ConversationSmartListUserImage.qml b/src/mainview/components/ConversationSmartListUserImage.qml
index e8034fd3d..2358e165e 100644
--- a/src/mainview/components/ConversationSmartListUserImage.qml
+++ b/src/mainview/components/ConversationSmartListUserImage.qml
@@ -38,7 +38,7 @@ Image {
         anchors.bottom: userImage.bottom
         anchors.bottomMargin: -2
 
-        visible: Presence
+        visible: Presence === undefined ? false : Presence
     }
 
     Rectangle {
diff --git a/src/mainview/components/ConversationSmartListView.qml b/src/mainview/components/ConversationSmartListView.qml
index 5810d3cc2..001bed352 100644
--- a/src/mainview/components/ConversationSmartListView.qml
+++ b/src/mainview/components/ConversationSmartListView.qml
@@ -37,6 +37,8 @@ ListView {
 
     // Refresh all items within the model.
     function updateListView() {
+        if (!root.model)
+            return
         root.model.dataChanged(
                     root.model.index(0, 0),
                     root.model.index(
diff --git a/src/mainview/components/ConversationSmartListViewItemDelegate.qml b/src/mainview/components/ConversationSmartListViewItemDelegate.qml
index d54f7cffd..12cc257d3 100644
--- a/src/mainview/components/ConversationSmartListViewItemDelegate.qml
+++ b/src/mainview/components/ConversationSmartListViewItemDelegate.qml
@@ -99,7 +99,7 @@ ItemDelegate {
                 elide: Text.ElideRight
                 elideWidth: LastInteractionDate ? (smartListItemDelegate.width - lastInteractionPreferredWidth - conversationSmartListUserImage.width-32) :
                                                   smartListItemDelegate.width - lastInteractionPreferredWidth
-                text: DisplayName
+                text: DisplayName === undefined ? "" : DisplayName
             }
             text: textMetricsConversationSmartListUserName.elidedText
             font.pointSize: JamiTheme.menuFontSize
@@ -113,7 +113,7 @@ ItemDelegate {
                 font: conversationSmartListUserLastInteractionDate.font
                 elide: Text.ElideRight
                 elideWidth: lastInteractionPreferredWidth
-                text: LastInteractionDate
+                text: LastInteractionDate === undefined ? "" : LastInteractionDate
             }
 
             text: textMetricsConversationSmartListUserLastInteractionDate.elidedText
diff --git a/src/smartlistmodel.cpp b/src/smartlistmodel.cpp
index f863556a3..f777d488e 100644
--- a/src/smartlistmodel.cpp
+++ b/src/smartlistmodel.cpp
@@ -34,9 +34,7 @@ SmartListModel::SmartListModel(QObject* parent,
                                SmartListModel::Type listModelType,
                                const QString& convUid)
     : QAbstractListModel(parent)
-    , accountId_(accId)
     , listModelType_(listModelType)
-    , convUid_(convUid)
 {
     if (listModelType_ == Type::CONFERENCE) {
         setConferenceableFilter();
@@ -49,7 +47,7 @@ int
 SmartListModel::rowCount(const QModelIndex& parent) const
 {
     if (!parent.isValid()) {
-        auto& accInfo = LRCInstance::accountModel().getAccountInfo(accountId_);
+        auto& accInfo = LRCInstance::accountModel().getAccountInfo(LRCInstance::getCurrAccId());
         auto& convModel = accInfo.conversationModel;
         if (listModelType_ == Type::TRANSFER) {
             auto filterType = accInfo.profileInfo.type;
@@ -85,7 +83,7 @@ SmartListModel::data(const QModelIndex& index, int role) const
     }
 
     try {
-        auto& accountInfo = LRCInstance::accountModel().getAccountInfo(accountId_);
+        auto& accountInfo = LRCInstance::accountModel().getAccountInfo(LRCInstance::getCurrAccId());
         auto& convModel = accountInfo.conversationModel;
         lrc::api::conversation::Info item;
         if (listModelType_ == Type::TRANSFER) {
@@ -172,9 +170,10 @@ void
 SmartListModel::setConferenceableFilter(const QString& filter)
 {
     beginResetModel();
-    auto& accountInfo = LRCInstance::accountModel().getAccountInfo(accountId_);
+    auto& accountInfo = LRCInstance::accountModel().getAccountInfo(LRCInstance::getCurrAccId());
     auto& convModel = accountInfo.conversationModel;
-    conferenceables_ = convModel->getConferenceableConversations(convUid_, filter);
+    conferenceables_ = convModel->getConferenceableConversations(LRCInstance::getCurrentConvUid(),
+                                                                 filter);
     sectionState_[tr("Calls")] = true;
     sectionState_[tr("Contacts")] = true;
     endResetModel();
@@ -406,9 +405,3 @@ SmartListModel::flags(const QModelIndex& index) const
     }
     return flags;
 }
-
-void
-SmartListModel::setAccount(const QString& accountId)
-{
-    accountId_ = accountId;
-}
diff --git a/src/smartlistmodel.h b/src/smartlistmodel.h
index 4f47b44b1..9bb56900c 100644
--- a/src/smartlistmodel.h
+++ b/src/smartlistmodel.h
@@ -79,7 +79,6 @@ public:
     QModelIndex parent(const QModelIndex& child) const;
     Qt::ItemFlags flags(const QModelIndex& index) const;
 
-    Q_INVOKABLE void setAccount(const QString& accId);
     Q_INVOKABLE void setConferenceableFilter(const QString& filter = {});
     Q_INVOKABLE void toggleSection(const QString& section);
     Q_INVOKABLE int currentUidSmartListModelIndex();
@@ -87,15 +86,12 @@ public:
     Q_INVOKABLE void updateConversation(const QString& conv);
 
 private:
-    QString accountId_;
-
     QVariant getConversationItemData(const ConversationInfo& item,
                                      const AccountInfo& accountInfo,
                                      int role) const;
     /*
      * List sectioning.
      */
-    QString convUid_;
     Type listModelType_;
     QMap<QString, bool> sectionState_;
     QMap<ConferenceableItem, ConferenceableValue> conferenceables_;
-- 
GitLab