diff --git a/autotests/providertest.cpp b/autotests/providertest.cpp --- a/autotests/providertest.cpp +++ b/autotests/providertest.cpp @@ -348,6 +348,25 @@ QVERIFY(!spy.wait(100)); } } + + void testDataSourceLookup() + { + Provider p; + + auto screenInfoDataSource = new ScreenInfoSource; + auto screenInfoDataSourceId = screenInfoDataSource->id(); + + auto platformInfoDataSource = new PlatformInfoSource; + auto platformInfoDataSourceId = platformInfoDataSource->id(); + + p.addDataSource(screenInfoDataSource); + p.addDataSource(platformInfoDataSource); + + QCOMPARE(screenInfoDataSource, p.dataSource(screenInfoDataSourceId)); + QCOMPARE(platformInfoDataSource, p.dataSource(platformInfoDataSourceId)); + + QVERIFY(!p.dataSource(QStringLiteral("SomeInvalidId"))); + } }; QTEST_MAIN(ProviderTest) diff --git a/src/provider/core/provider.h b/src/provider/core/provider.h --- a/src/provider/core/provider.h +++ b/src/provider/core/provider.h @@ -182,6 +182,12 @@ */ QVector dataSources() const; + /*! Returns a data source with matched @p id + * @param id data source unique identifier + * @return pointer to found data source or nullptr if data source is not found + */ + AbstractDataSource *dataSource(const QString &id) const; + /*! Returns the minimum time between two surveys in days. * The default is -1 (no surveys enabled). */ diff --git a/src/provider/core/provider.cpp b/src/provider/core/provider.cpp --- a/src/provider/core/provider.cpp +++ b/src/provider/core/provider.cpp @@ -504,6 +504,7 @@ timeSrc->setProvider(d); d->dataSources.push_back(source); + d->dataSourcesById[source->id()] = source; auto s = d->makeSettings(); s->beginGroup(QStringLiteral("Source-") + source->id()); @@ -515,6 +516,12 @@ return d->dataSources; } +AbstractDataSource *Provider::dataSource(const QString &id) const +{ + auto it = d->dataSourcesById.find(id); + return it != std::end(d->dataSourcesById) ? *it : nullptr; +} + int Provider::surveyInterval() const { return d->surveyInterval; diff --git a/src/provider/core/provider_p.h b/src/provider/core/provider_p.h --- a/src/provider/core/provider_p.h +++ b/src/provider/core/provider_p.h @@ -100,6 +100,7 @@ int encouragementInterval; QVector dataSources; + QHash dataSourcesById; }; }