Commit 0858b9de authored by Vitaly Fanaskov's avatar Vitaly Fanaskov Committed by Volker Krause

Data source can be obtained by ID

Summary: It allows to perform fast look up when needed

Reviewers: vkrause

Reviewed By: vkrause

Differential Revision: https://phabricator.kde.org/D19758
parent 1be6e139
......@@ -348,6 +348,25 @@ private slots:
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)
......
......@@ -504,6 +504,7 @@ void Provider::addDataSource(AbstractDataSource *source)
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 @@ QVector<AbstractDataSource*> Provider::dataSources() const
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;
......
......@@ -182,6 +182,12 @@ public:
*/
QVector<AbstractDataSource*> 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).
*/
......
......@@ -100,6 +100,7 @@ public:
int encouragementInterval;
QVector<AbstractDataSource*> dataSources;
QHash<QString, AbstractDataSource*> dataSourcesById;
};
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment