summaryrefslogtreecommitdiffstats
path: root/src/sources/soundsourceproviderregistry.cpp
blob: f128d0187e7f3688a7487c78cc6f288bd216bcf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "sources/soundsourceproviderregistry.h"

#include "util/logger.h"

namespace mixxx {

namespace {

const Logger kLogger("SoundSourceProviderRegistry");

void insertRegistration(
        QList<SoundSourceProviderRegistration>* pRegistrations,
        SoundSourceProviderRegistration&& registration) {
    DEBUG_ASSERT(pRegistrations);
    QList<SoundSourceProviderRegistration>::iterator listIter(
            pRegistrations->begin());
    // Perform a linear search through the list & insert
    while (pRegistrations->end() != listIter) {
        // Priority comparison with <=: New registrations will be inserted
        // before existing registrations with equal priority, but after
        // existing registrations with higher priority.
        if (listIter->getProviderPriority() <= registration.getProviderPriority()) {
            listIter = pRegistrations->insert(listIter, std::move(registration));
            DEBUG_ASSERT(pRegistrations->end() != listIter);
            return; // done
        } else {
            ++listIter; // continue loop
        }
    }
    if (pRegistrations->end() == listIter) {
        // List was empty or registration has the lowest priority
        pRegistrations->append(std::move(registration));
    }
}

} // anonymous namespace

int SoundSourceProviderRegistry::registerProvider(
        const SoundSourceProviderPointer& pProvider) {
    VERIFY_OR_DEBUG_ASSERT(pProvider) {
        return 0;
    }
    const QString displayName = pProvider->getDisplayName();
    VERIFY_OR_DEBUG_ASSERT(!displayName.isEmpty()) {
        return 0;
    }
    kLogger.debug()
            << "Registering provider"
            << displayName;
    {
        // Due to the debug assertion this code is not testable
        const auto pProviderByDisplayName = m_providersByDisplayName.value(displayName);
        VERIFY_OR_DEBUG_ASSERT(!pProviderByDisplayName) {
            if (pProviderByDisplayName == pProvider) {
                kLogger.info()
                        << "Ignoring repeated registration of the same provider"
                        << displayName;
            } else {
                kLogger.warning()
                        << "Cannot register different providers with the same display name"
                        << displayName;
            }
            return 0;
        }
    }
    const QStringList supportedFileExtensions(
            pProvider->getSupportedFileExtensions());
    if (supportedFileExtensions.isEmpty()) {
        kLogger.warning()
                << "SoundSource provider"
                << displayName
                << "does not support any file extensions";
        return 0; // abort registration
    }
    for (const auto& fileExtension : supportedFileExtensions) {
        const auto priority =
                pProvider->getPriorityHint(fileExtension);
        kLogger.debug()
                << "Registering file extension"
                << fileExtension
                << "for provider"
                << displayName
                << "with priority"
                << priority;
        SoundSourceProviderRegistration registration(pProvider, priority);
        QList<SoundSourceProviderRegistration>& registrationsForFileExtension =
                m_registrationListsByFileExtension[fileExtension];
        insertRegistration(
                &registrationsForFileExtension,
                std::move(registration));
    }
    m_providersByDisplayName.insert(displayName, pProvider);
    DEBUG_ASSERT(m_providersByDisplayName.count(displayName) == 1);
    return supportedFileExtensions.size();
}

QList<SoundSourceProviderRegistration>
SoundSourceProviderRegistry::getRegistrationsForFileExtension(
        const QString& fileExtension) const {
    auto i = m_registrationListsByFileExtension.constFind(fileExtension);
    if (m_registrationListsByFileExtension.constEnd() != i) {
        DEBUG_ASSERT(!i.value().isEmpty());
        return i.value();
    } else {
        kLogger.debug()
                << "No provider(s) registered for file extension"
                << fileExtension;
        return QList<SoundSourceProviderRegistration>();
    }
}

} // namespace mixxx