##// END OF EJS Templates
Adds method for registering a data provider in the data source controller
Alexandre Leroux -
r119:95d739a33d44
parent child
Show More
@@ -1,61 +1,73
1 #ifndef SCIQLOP_DATASOURCECONTROLLER_H
1 #ifndef SCIQLOP_DATASOURCECONTROLLER_H
2 #define SCIQLOP_DATASOURCECONTROLLER_H
2 #define SCIQLOP_DATASOURCECONTROLLER_H
3
3
4 #include <QLoggingCategory>
4 #include <QLoggingCategory>
5 #include <QObject>
5 #include <QObject>
6 #include <QUuid>
6 #include <QUuid>
7
7
8 #include <Common/spimpl.h>
8 #include <Common/spimpl.h>
9
9
10 Q_DECLARE_LOGGING_CATEGORY(LOG_DataSourceController)
10 Q_DECLARE_LOGGING_CATEGORY(LOG_DataSourceController)
11
11
12 class DataSourceItem;
12 class DataSourceItem;
13 class IDataProvider;
13
14
14 /**
15 /**
15 * @brief The DataSourceController class aims to make the link between SciQlop and its plugins. This
16 * @brief The DataSourceController class aims to make the link between SciQlop and its plugins. This
16 * is the intermediate class that SciQlop has to use in the way to connect a data source. Please
17 * is the intermediate class that SciQlop has to use in the way to connect a data source. Please
17 * first use register method to initialize a plugin specified by its metadata name (JSON plugin
18 * first use register method to initialize a plugin specified by its metadata name (JSON plugin
18 * source) then others specifics method will be able to access it. You can load a data source driver
19 * source) then others specifics method will be able to access it. You can load a data source driver
19 * plugin then create a data source.
20 * plugin then create a data source.
20 */
21 */
21 class DataSourceController : public QObject {
22 class DataSourceController : public QObject {
22 Q_OBJECT
23 Q_OBJECT
23 public:
24 public:
24 explicit DataSourceController(QObject *parent = 0);
25 explicit DataSourceController(QObject *parent = 0);
25 virtual ~DataSourceController();
26 virtual ~DataSourceController();
26
27
27 /**
28 /**
28 * Registers a data source. The method delivers a unique id that can be used afterwards to
29 * Registers a data source. The method delivers a unique id that can be used afterwards to
29 * access to the data source properties (structure, connection parameters, data provider, etc.)
30 * access to the data source properties (structure, connection parameters, data provider, etc.)
30 * @param dataSourceName the name of the data source
31 * @param dataSourceName the name of the data source
31 * @return the unique id with which the data source has been registered
32 * @return the unique id with which the data source has been registered
32 */
33 */
33 QUuid registerDataSource(const QString &dataSourceName) noexcept;
34 QUuid registerDataSource(const QString &dataSourceName) noexcept;
34
35
35 /**
36 /**
36 * Sets the structure of a data source. The controller takes ownership of the structure.
37 * Sets the structure of a data source. The controller takes ownership of the structure.
37 * @param dataSourceUid the unique id with which the data source has been registered into the
38 * @param dataSourceUid the unique id with which the data source has been registered into the
38 * controller. If it is invalid, the method has no effect.
39 * controller. If it is invalid, the method has no effect.
39 * @param dataSourceItem the structure of the data source
40 * @param dataSourceItem the structure of the data source
40 * @sa registerDataSource()
41 * @sa registerDataSource()
41 */
42 */
42 void setDataSourceItem(const QUuid &dataSourceUid,
43 void setDataSourceItem(const QUuid &dataSourceUid,
43 std::unique_ptr<DataSourceItem> dataSourceItem) noexcept;
44 std::unique_ptr<DataSourceItem> dataSourceItem) noexcept;
44
45
46 /**
47 * Sets the data provider used to retrieve data from of a data source. The controller takes
48 * ownership of the provider.
49 * @param dataSourceUid the unique id with which the data source has been registered into the
50 * controller. If it is invalid, the method has no effect.
51 * @param dataProvider the provider of the data source
52 * @sa registerDataSource()
53 */
54 void setDataProvider(const QUuid &dataSourceUid,
55 std::unique_ptr<IDataProvider> dataProvider) noexcept;
56
45 public slots:
57 public slots:
46 /// Manage init/end of the controller
58 /// Manage init/end of the controller
47 void initialize();
59 void initialize();
48 void finalize();
60 void finalize();
49
61
50 signals:
62 signals:
51 /// Signal emitted when a structure has been set for a data source
63 /// Signal emitted when a structure has been set for a data source
52 void dataSourceItemSet(DataSourceItem *dataSourceItem);
64 void dataSourceItemSet(DataSourceItem *dataSourceItem);
53
65
54 private:
66 private:
55 void waitForFinish();
67 void waitForFinish();
56
68
57 class DataSourceControllerPrivate;
69 class DataSourceControllerPrivate;
58 spimpl::unique_impl_ptr<DataSourceControllerPrivate> impl;
70 spimpl::unique_impl_ptr<DataSourceControllerPrivate> impl;
59 };
71 };
60
72
61 #endif // SCIQLOP_DATASOURCECONTROLLER_H
73 #endif // SCIQLOP_DATASOURCECONTROLLER_H
@@ -1,78 +1,95
1 #include <DataSource/DataSourceController.h>
1 #include <DataSource/DataSourceController.h>
2 #include <DataSource/DataSourceItem.h>
2 #include <DataSource/DataSourceItem.h>
3
3
4 #include <Data/IDataProvider.h>
5
4 #include <QMutex>
6 #include <QMutex>
5 #include <QThread>
7 #include <QThread>
6
8
7 #include <QDir>
9 #include <QDir>
8 #include <QStandardPaths>
10 #include <QStandardPaths>
9
11
10 Q_LOGGING_CATEGORY(LOG_DataSourceController, "DataSourceController")
12 Q_LOGGING_CATEGORY(LOG_DataSourceController, "DataSourceController")
11
13
12 class DataSourceController::DataSourceControllerPrivate {
14 class DataSourceController::DataSourceControllerPrivate {
13 public:
15 public:
14 QMutex m_WorkingMutex;
16 QMutex m_WorkingMutex;
15 /// Data sources registered
17 /// Data sources registered
16 QHash<QUuid, QString> m_DataSources;
18 QHash<QUuid, QString> m_DataSources;
17 /// Data sources structures
19 /// Data sources structures
18 std::map<QUuid, std::unique_ptr<DataSourceItem> > m_DataSourceItems;
20 std::map<QUuid, std::unique_ptr<DataSourceItem> > m_DataSourceItems;
21 /// Data providers registered
22 std::map<QUuid, std::unique_ptr<IDataProvider> > m_DataProviders;
19 };
23 };
20
24
21 DataSourceController::DataSourceController(QObject *parent)
25 DataSourceController::DataSourceController(QObject *parent)
22 : impl{spimpl::make_unique_impl<DataSourceControllerPrivate>()}
26 : impl{spimpl::make_unique_impl<DataSourceControllerPrivate>()}
23 {
27 {
24 qCDebug(LOG_DataSourceController()) << tr("DataSourceController construction")
28 qCDebug(LOG_DataSourceController()) << tr("DataSourceController construction")
25 << QThread::currentThread();
29 << QThread::currentThread();
26 }
30 }
27
31
28 DataSourceController::~DataSourceController()
32 DataSourceController::~DataSourceController()
29 {
33 {
30 qCDebug(LOG_DataSourceController()) << tr("DataSourceController destruction")
34 qCDebug(LOG_DataSourceController()) << tr("DataSourceController destruction")
31 << QThread::currentThread();
35 << QThread::currentThread();
32 this->waitForFinish();
36 this->waitForFinish();
33 }
37 }
34
38
35 QUuid DataSourceController::registerDataSource(const QString &dataSourceName) noexcept
39 QUuid DataSourceController::registerDataSource(const QString &dataSourceName) noexcept
36 {
40 {
37 auto dataSourceUid = QUuid::createUuid();
41 auto dataSourceUid = QUuid::createUuid();
38 impl->m_DataSources.insert(dataSourceUid, dataSourceName);
42 impl->m_DataSources.insert(dataSourceUid, dataSourceName);
39
43
40 return dataSourceUid;
44 return dataSourceUid;
41 }
45 }
42
46
43 void DataSourceController::setDataSourceItem(
47 void DataSourceController::setDataSourceItem(
44 const QUuid &dataSourceUid, std::unique_ptr<DataSourceItem> dataSourceItem) noexcept
48 const QUuid &dataSourceUid, std::unique_ptr<DataSourceItem> dataSourceItem) noexcept
45 {
49 {
46 if (impl->m_DataSources.contains(dataSourceUid)) {
50 if (impl->m_DataSources.contains(dataSourceUid)) {
47 impl->m_DataSourceItems.insert(std::make_pair(dataSourceUid, std::move(dataSourceItem)));
51 impl->m_DataSourceItems.insert(std::make_pair(dataSourceUid, std::move(dataSourceItem)));
48
52
49 // Retrieves the data source item to emit the signal with it
53 // Retrieves the data source item to emit the signal with it
50 auto it = impl->m_DataSourceItems.find(dataSourceUid);
54 auto it = impl->m_DataSourceItems.find(dataSourceUid);
51 if (it != impl->m_DataSourceItems.end()) {
55 if (it != impl->m_DataSourceItems.end()) {
52 emit dataSourceItemSet(it->second.get());
56 emit dataSourceItemSet(it->second.get());
53 }
57 }
54 }
58 }
55 else {
59 else {
56 qCWarning(LOG_DataSourceController()) << tr("Can't set data source item for uid %1 : no "
60 qCWarning(LOG_DataSourceController()) << tr("Can't set data source item for uid %1 : no "
57 "data source has been registered with the uid")
61 "data source has been registered with the uid")
58 .arg(dataSourceUid.toString());
62 .arg(dataSourceUid.toString());
59 }
63 }
60 }
64 }
61
65
66 void DataSourceController::setDataProvider(const QUuid &dataSourceUid,
67 std::unique_ptr<IDataProvider> dataProvider) noexcept
68 {
69 if (impl->m_DataSources.contains(dataSourceUid)) {
70 impl->m_DataProviders.insert(std::make_pair(dataSourceUid, std::move(dataProvider)));
71 }
72 else {
73 qCWarning(LOG_DataSourceController()) << tr("Can't set data provider for uid %1 : no data "
74 "source has been registered with the uid")
75 .arg(dataSourceUid.toString());
76 }
77 }
78
62 void DataSourceController::initialize()
79 void DataSourceController::initialize()
63 {
80 {
64 qCDebug(LOG_DataSourceController()) << tr("DataSourceController init")
81 qCDebug(LOG_DataSourceController()) << tr("DataSourceController init")
65 << QThread::currentThread();
82 << QThread::currentThread();
66 impl->m_WorkingMutex.lock();
83 impl->m_WorkingMutex.lock();
67 qCDebug(LOG_DataSourceController()) << tr("DataSourceController init END");
84 qCDebug(LOG_DataSourceController()) << tr("DataSourceController init END");
68 }
85 }
69
86
70 void DataSourceController::finalize()
87 void DataSourceController::finalize()
71 {
88 {
72 impl->m_WorkingMutex.unlock();
89 impl->m_WorkingMutex.unlock();
73 }
90 }
74
91
75 void DataSourceController::waitForFinish()
92 void DataSourceController::waitForFinish()
76 {
93 {
77 QMutexLocker locker{&impl->m_WorkingMutex};
94 QMutexLocker locker{&impl->m_WorkingMutex};
78 }
95 }
General Comments 0
You need to be logged in to leave comments. Login now