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