From 2bdd0f587eab3aafde9b63f7f5f479311bcc7e16 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Tue, 4 Jun 2019 17:06:14 -0400
Subject: [PATCH] avmodel: fix media selection

Change-Id: I0c4616142839e155ebbf69182aa3a27b67b14065
---
 src/avmodel.cpp | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/src/avmodel.cpp b/src/avmodel.cpp
index 65b83f78..c597240e 100644
--- a/src/avmodel.cpp
+++ b/src/avmodel.cpp
@@ -721,10 +721,10 @@ AVModelPimpl::getDevice(int type) const
     std::string result = "";
     std::vector<std::string> devices;
     switch (type) {
-        case 0: // INPUT
+        case 1: // INPUT
             devices = linked_.getAudioInputDevices();
             break;
-        case 1: // OUTPUT
+        case 0: // OUTPUT
         case 2: // RINGTONE
             devices = linked_.getAudioOutputDevices();
             break;
@@ -734,17 +734,18 @@ AVModelPimpl::getDevice(int type) const
     QStringList currentDevicesIdx = ConfigurationManager::instance()
         .getCurrentAudioDevicesIndex();
     try {
-        if (currentDevicesIdx.size() < 3
-        || devices.size() != static_cast<size_t>(currentDevicesIdx.size())) {
+        if (currentDevicesIdx.size() < 3) {
             // Should not happen, but cannot retrieve current ringtone device
             return "";
         }
         auto deviceIdx = currentDevicesIdx[type].toUInt();
-        if (deviceIdx >= devices.size()) {
-            // Incorrect device index
-            result = devices[0];
+        for (const auto& dev : devices) {
+            int idx = ConfigurationManager::instance().getAudioInputDeviceIndex(dev.c_str());
+            if (idx == deviceIdx) {
+                return dev;
+            }
         }
-        result = devices[deviceIdx];
+        return "";
     } catch (std::bad_alloc& ba) {
         qWarning() << "bad_alloc caught: " << ba.what();
         return "";
-- 
GitLab