From 4e2280a2bafce97af45edc38cb24a76c5a1bdbad Mon Sep 17 00:00:00 2001
From: Emmanuel Lepage Vallee <elv1313@gmail.com>
Date: Mon, 21 Dec 2015 07:07:00 -0500
Subject: [PATCH] audio: Fix invalid QModelIndex for current devices

If ->reload() is called on one of the model, the selection will
be lost. This cause the wrong device to be selected in the
clients ComboBox.

Tuleap: #216
Change-Id: Ifa0821e6b40f279d1908d012f1c0fd510eaa0703
---
 src/audio/alsapluginmodel.cpp     | 15 +++++++--------
 src/audio/inputdevicemodel.cpp    | 13 +++++--------
 src/audio/outputdevicemodel.cpp   | 13 ++++++-------
 src/audio/ringtonedevicemodel.cpp | 13 ++++++-------
 4 files changed, 24 insertions(+), 30 deletions(-)

diff --git a/src/audio/alsapluginmodel.cpp b/src/audio/alsapluginmodel.cpp
index e2da2b52..b99af3a9 100644
--- a/src/audio/alsapluginmodel.cpp
+++ b/src/audio/alsapluginmodel.cpp
@@ -36,7 +36,6 @@ private:
 
 public Q_SLOTS:
    void setCurrentPlugin(const QModelIndex& idx);
-   void setCurrentPlugin(int idx);
 };
 
 AlsaPluginModelPrivate::AlsaPluginModelPrivate(Audio::AlsaPluginModel* parent) : q_ptr(parent),
@@ -113,7 +112,7 @@ QItemSelectionModel* Audio::AlsaPluginModel::selectionModel() const
 
       d_ptr->m_pSelectionModel->setCurrentIndex(currentPlugin(), QItemSelectionModel::ClearAndSelect);
 
-      connect(d_ptr->m_pSelectionModel, SIGNAL(currentChanged(QModelIndex,QModelIndex)), d_ptr.data(), SLOT(setCurrentPlugin(QModelIndex)));
+      connect(d_ptr->m_pSelectionModel, &QItemSelectionModel::currentChanged, d_ptr.data(), &AlsaPluginModelPrivate::setCurrentPlugin);
    }
 
    return d_ptr->m_pSelectionModel;
@@ -141,21 +140,21 @@ void AlsaPluginModelPrivate::setCurrentPlugin(const QModelIndex& idx)
    configurationManager.setAudioPlugin(m_lDeviceList[idx.row()]);
 }
 
-///Set the current index (qcombobox compatibility shim)
-void AlsaPluginModelPrivate::setCurrentPlugin(int idx)
-{
-   setCurrentPlugin(q_ptr->index(idx,0));
-}
-
 ///Reload to current daemon state
 void Audio::AlsaPluginModel::reload()
 {
+   const int currentRow = selectionModel()->currentIndex().row();
+
    ConfigurationManagerInterface& configurationManager = ConfigurationManager::instance();
    beginResetModel();
    d_ptr->m_lDeviceList = configurationManager.getAudioPluginList();
    endResetModel();
    emit layoutChanged();
    emit dataChanged(index(0,0),index(d_ptr->m_lDeviceList.size()-1,0));
+
+   // Restore the selection
+   d_ptr->m_pSelectionModel->setCurrentIndex(index(currentRow,0), QItemSelectionModel::ClearAndSelect);
+
 }
 
 #include <alsapluginmodel.moc>
diff --git a/src/audio/inputdevicemodel.cpp b/src/audio/inputdevicemodel.cpp
index 60dea5b5..decbcf0e 100644
--- a/src/audio/inputdevicemodel.cpp
+++ b/src/audio/inputdevicemodel.cpp
@@ -37,7 +37,6 @@ private:
 
 public Q_SLOTS:
    void setCurrentDevice(const QModelIndex& index);
-   void setCurrentDevice(int idx);
 };
 
 InputDeviceModelPrivate::InputDeviceModelPrivate(Audio::InputDeviceModel* parent) : q_ptr(parent),
@@ -118,7 +117,7 @@ QItemSelectionModel* Audio::InputDeviceModel::selectionModel() const
       if (!(idx >= d_ptr->m_lDeviceList.size()))
          d_ptr->m_pSelectionModel->setCurrentIndex(index(idx,0), QItemSelectionModel::ClearAndSelect);
 
-      connect(d_ptr->m_pSelectionModel, SIGNAL(currentChanged(QModelIndex,QModelIndex)), d_ptr.data(), SLOT(setCurrentDevice(QModelIndex)));
+      connect(d_ptr->m_pSelectionModel, &QItemSelectionModel::currentChanged, d_ptr.data(), &InputDeviceModelPrivate::setCurrentDevice);
    }
 
    return d_ptr->m_pSelectionModel;
@@ -133,21 +132,19 @@ void InputDeviceModelPrivate::setCurrentDevice(const QModelIndex& index)
    }
 }
 
-///QCombobox signals -> QModelIndex shim
-void InputDeviceModelPrivate::setCurrentDevice(int idx)
-{
-   setCurrentDevice(q_ptr->index(idx,0));
-}
-
 ///Reload input device list
 void Audio::InputDeviceModel::reload()
 {
+   const int currentRow = selectionModel()->currentIndex().row();
    ConfigurationManagerInterface& configurationManager = ConfigurationManager::instance();
    beginResetModel();
    d_ptr->m_lDeviceList = configurationManager.getAudioInputDeviceList  ();
    endResetModel();
    emit layoutChanged();
    emit dataChanged(index(0,0),index(d_ptr->m_lDeviceList.size()-1,0));
+
+   // Restore the selection
+   d_ptr->m_pSelectionModel->setCurrentIndex(index(currentRow,0), QItemSelectionModel::ClearAndSelect);
 }
 
 #include <inputdevicemodel.moc>
diff --git a/src/audio/outputdevicemodel.cpp b/src/audio/outputdevicemodel.cpp
index 59ec452d..6498af4e 100644
--- a/src/audio/outputdevicemodel.cpp
+++ b/src/audio/outputdevicemodel.cpp
@@ -38,7 +38,6 @@ private:
 
 public Q_SLOTS:
    void setCurrentDevice(const QModelIndex& index);
-   void setCurrentDevice(int idx);
 };
 
 
@@ -135,21 +134,21 @@ void OutputDeviceModelPrivate::setCurrentDevice(const QModelIndex& index)
    }
 }
 
-///QCombobox index -> QModelIndex shim
-void OutputDeviceModelPrivate::setCurrentDevice(int idx)
-{
-   setCurrentDevice(q_ptr->index(idx,0));
-}
-
 ///reload output devices list
 void Audio::OutputDeviceModel::reload()
 {
+   const int currentRow = selectionModel()->currentIndex().row();
+
    ConfigurationManagerInterface& configurationManager = ConfigurationManager::instance();
    beginResetModel();
    d_ptr->m_lDeviceList = configurationManager.getAudioOutputDeviceList();
    endResetModel();
    emit layoutChanged();
    emit dataChanged(index(0,0),index(d_ptr->m_lDeviceList.size()-1,0));
+
+   // Restore the selection
+   d_ptr->m_pSelectionModel->setCurrentIndex(index(currentRow,0), QItemSelectionModel::ClearAndSelect);
+
 }
 
 void Audio::OutputDeviceModel::playDTMF(const QString& str)
diff --git a/src/audio/ringtonedevicemodel.cpp b/src/audio/ringtonedevicemodel.cpp
index 71f38987..4b096c12 100644
--- a/src/audio/ringtonedevicemodel.cpp
+++ b/src/audio/ringtonedevicemodel.cpp
@@ -37,7 +37,6 @@ private:
 
 public Q_SLOTS:
    void setCurrentDevice(const QModelIndex& index);
-   void setCurrentDevice(int idx);
 };
 
 RingtoneDeviceModelPrivate::RingtoneDeviceModelPrivate(Audio::RingtoneDeviceModel* parent) : q_ptr(parent),
@@ -144,21 +143,21 @@ void RingtoneDeviceModelPrivate::setCurrentDevice(const QModelIndex& index)
    }
 }
 
-///QCombobox -> QModelIndex shim
-void RingtoneDeviceModelPrivate::setCurrentDevice(int idx)
-{
-   setCurrentDevice(q_ptr->index(idx,0));
-}
-
 ///Reload ringtone device list
 void Audio::RingtoneDeviceModel::reload()
 {
+   const int currentRow = selectionModel()->currentIndex().row();
+
    ConfigurationManagerInterface& configurationManager = ConfigurationManager::instance();
    beginResetModel();
    d_ptr->m_lDeviceList = configurationManager.getAudioOutputDeviceList();
    endResetModel();
    emit layoutChanged();
    emit dataChanged(index(0,0),index(d_ptr->m_lDeviceList.size()-1,0));
+
+   // Restore the selection
+   d_ptr->m_pSelectionModel->setCurrentIndex(index(currentRow,0), QItemSelectionModel::ClearAndSelect);
+
 }
 
 #include <ringtonedevicemodel.moc>
-- 
GitLab