@@ -1,104 +1,111 | |||||
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> |
|
4 | #include <Data/IDataProvider.h> | |
5 |
|
5 | |||
6 | #include <QMutex> |
|
6 | #include <QMutex> | |
7 | #include <QThread> |
|
7 | #include <QThread> | |
8 |
|
8 | |||
9 | #include <QDir> |
|
9 | #include <QDir> | |
10 | #include <QStandardPaths> |
|
10 | #include <QStandardPaths> | |
11 |
|
11 | |||
12 | Q_LOGGING_CATEGORY(LOG_DataSourceController, "DataSourceController") |
|
12 | Q_LOGGING_CATEGORY(LOG_DataSourceController, "DataSourceController") | |
13 |
|
13 | |||
14 | class DataSourceController::DataSourceControllerPrivate { |
|
14 | class DataSourceController::DataSourceControllerPrivate { | |
15 | public: |
|
15 | public: | |
16 | QMutex m_WorkingMutex; |
|
16 | QMutex m_WorkingMutex; | |
17 | /// Data sources registered |
|
17 | /// Data sources registered | |
18 | QHash<QUuid, QString> m_DataSources; |
|
18 | QHash<QUuid, QString> m_DataSources; | |
19 | /// Data sources structures |
|
19 | /// Data sources structures | |
20 | std::map<QUuid, std::unique_ptr<DataSourceItem> > m_DataSourceItems; |
|
20 | std::map<QUuid, std::unique_ptr<DataSourceItem> > m_DataSourceItems; | |
21 | /// Data providers registered |
|
21 | /// Data providers registered | |
22 | /// @remarks Data providers are stored as shared_ptr as they can be sent to a variable and |
|
22 | /// @remarks Data providers are stored as shared_ptr as they can be sent to a variable and | |
23 | /// continue to live without necessarily the data source controller |
|
23 | /// continue to live without necessarily the data source controller | |
24 | std::map<QUuid, std::shared_ptr<IDataProvider> > m_DataProviders; |
|
24 | std::map<QUuid, std::shared_ptr<IDataProvider> > m_DataProviders; | |
25 | }; |
|
25 | }; | |
26 |
|
26 | |||
27 | DataSourceController::DataSourceController(QObject *parent) |
|
27 | DataSourceController::DataSourceController(QObject *parent) | |
28 | : impl{spimpl::make_unique_impl<DataSourceControllerPrivate>()} |
|
28 | : impl{spimpl::make_unique_impl<DataSourceControllerPrivate>()} | |
29 | { |
|
29 | { | |
30 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController construction") |
|
30 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController construction") | |
31 | << QThread::currentThread(); |
|
31 | << QThread::currentThread(); | |
32 | } |
|
32 | } | |
33 |
|
33 | |||
34 | DataSourceController::~DataSourceController() |
|
34 | DataSourceController::~DataSourceController() | |
35 | { |
|
35 | { | |
36 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController destruction") |
|
36 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController destruction") | |
37 | << QThread::currentThread(); |
|
37 | << QThread::currentThread(); | |
38 | this->waitForFinish(); |
|
38 | this->waitForFinish(); | |
39 | } |
|
39 | } | |
40 |
|
40 | |||
41 | QUuid DataSourceController::registerDataSource(const QString &dataSourceName) noexcept |
|
41 | QUuid DataSourceController::registerDataSource(const QString &dataSourceName) noexcept | |
42 | { |
|
42 | { | |
43 | auto dataSourceUid = QUuid::createUuid(); |
|
43 | auto dataSourceUid = QUuid::createUuid(); | |
44 | impl->m_DataSources.insert(dataSourceUid, dataSourceName); |
|
44 | impl->m_DataSources.insert(dataSourceUid, dataSourceName); | |
45 |
|
45 | |||
46 | return dataSourceUid; |
|
46 | return dataSourceUid; | |
47 | } |
|
47 | } | |
48 |
|
48 | |||
49 | void DataSourceController::setDataSourceItem( |
|
49 | void DataSourceController::setDataSourceItem( | |
50 | const QUuid &dataSourceUid, std::unique_ptr<DataSourceItem> dataSourceItem) noexcept |
|
50 | const QUuid &dataSourceUid, std::unique_ptr<DataSourceItem> dataSourceItem) noexcept | |
51 | { |
|
51 | { | |
52 | if (impl->m_DataSources.contains(dataSourceUid)) { |
|
52 | if (impl->m_DataSources.contains(dataSourceUid)) { | |
53 | // The data provider is implicitly converted to a shared_ptr |
|
53 | // The data provider is implicitly converted to a shared_ptr | |
54 | impl->m_DataSourceItems.insert(std::make_pair(dataSourceUid, std::move(dataSourceItem))); |
|
54 | impl->m_DataSourceItems.insert(std::make_pair(dataSourceUid, std::move(dataSourceItem))); | |
55 |
|
55 | |||
56 | // Retrieves the data source item to emit the signal with it |
|
56 | // Retrieves the data source item to emit the signal with it | |
57 | auto it = impl->m_DataSourceItems.find(dataSourceUid); |
|
57 | auto it = impl->m_DataSourceItems.find(dataSourceUid); | |
58 | if (it != impl->m_DataSourceItems.end()) { |
|
58 | if (it != impl->m_DataSourceItems.end()) { | |
59 | emit dataSourceItemSet(it->second.get()); |
|
59 | emit dataSourceItemSet(it->second.get()); | |
60 | } |
|
60 | } | |
61 | } |
|
61 | } | |
62 | else { |
|
62 | else { | |
63 | qCWarning(LOG_DataSourceController()) << tr("Can't set data source item for uid %1 : no " |
|
63 | qCWarning(LOG_DataSourceController()) << tr("Can't set data source item for uid %1 : no " | |
64 | "data source has been registered with the uid") |
|
64 | "data source has been registered with the uid") | |
65 | .arg(dataSourceUid.toString()); |
|
65 | .arg(dataSourceUid.toString()); | |
66 | } |
|
66 | } | |
67 | } |
|
67 | } | |
68 |
|
68 | |||
69 | void DataSourceController::setDataProvider(const QUuid &dataSourceUid, |
|
69 | void DataSourceController::setDataProvider(const QUuid &dataSourceUid, | |
70 | std::unique_ptr<IDataProvider> dataProvider) noexcept |
|
70 | std::unique_ptr<IDataProvider> dataProvider) noexcept | |
71 | { |
|
71 | { | |
72 | if (impl->m_DataSources.contains(dataSourceUid)) { |
|
72 | if (impl->m_DataSources.contains(dataSourceUid)) { | |
73 | impl->m_DataProviders.insert(std::make_pair(dataSourceUid, std::move(dataProvider))); |
|
73 | impl->m_DataProviders.insert(std::make_pair(dataSourceUid, std::move(dataProvider))); | |
74 | } |
|
74 | } | |
75 | else { |
|
75 | else { | |
76 | qCWarning(LOG_DataSourceController()) << tr("Can't set data provider for uid %1 : no data " |
|
76 | qCWarning(LOG_DataSourceController()) << tr("Can't set data provider for uid %1 : no data " | |
77 | "source has been registered with the uid") |
|
77 | "source has been registered with the uid") | |
78 | .arg(dataSourceUid.toString()); |
|
78 | .arg(dataSourceUid.toString()); | |
79 | } |
|
79 | } | |
80 | } |
|
80 | } | |
81 |
|
81 | |||
82 | void DataSourceController::loadProductItem(const QUuid &dataSourceUid, |
|
82 | void DataSourceController::loadProductItem(const QUuid &dataSourceUid, | |
83 | const DataSourceItem &productItem) noexcept |
|
83 | const DataSourceItem &productItem) noexcept | |
84 | { |
|
84 | { | |
85 | /// @todo ALX |
|
85 | if (productItem.type() == DataSourceItemType::PRODUCT) { | |
|
86 | /// Retrieves the data provider of the data source (if any) | |||
|
87 | auto it = impl->m_DataProviders.find(dataSourceUid); | |||
|
88 | auto dataProvider = (it != impl->m_DataProviders.end()) ? it->second : nullptr; | |||
|
89 | } | |||
|
90 | else { | |||
|
91 | qCWarning(LOG_DataSourceController()) << tr("Can't load an item that is not a product"); | |||
|
92 | } | |||
86 | } |
|
93 | } | |
87 |
|
94 | |||
88 | void DataSourceController::initialize() |
|
95 | void DataSourceController::initialize() | |
89 | { |
|
96 | { | |
90 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController init") |
|
97 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController init") | |
91 | << QThread::currentThread(); |
|
98 | << QThread::currentThread(); | |
92 | impl->m_WorkingMutex.lock(); |
|
99 | impl->m_WorkingMutex.lock(); | |
93 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController init END"); |
|
100 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController init END"); | |
94 | } |
|
101 | } | |
95 |
|
102 | |||
96 | void DataSourceController::finalize() |
|
103 | void DataSourceController::finalize() | |
97 | { |
|
104 | { | |
98 | impl->m_WorkingMutex.unlock(); |
|
105 | impl->m_WorkingMutex.unlock(); | |
99 | } |
|
106 | } | |
100 |
|
107 | |||
101 | void DataSourceController::waitForFinish() |
|
108 | void DataSourceController::waitForFinish() | |
102 | { |
|
109 | { | |
103 | QMutexLocker locker{&impl->m_WorkingMutex}; |
|
110 | QMutexLocker locker{&impl->m_WorkingMutex}; | |
104 | } |
|
111 | } |
General Comments 0
You need to be logged in to leave comments.
Login now