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