diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-07-12 16:08:14 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-07-12 16:08:14 +0200 |
commit | b948ffa771054e8a7780ccfc38de11762bcdfcd6 (patch) | |
tree | c80d412f04e4c09133d7567e7cbc184e62ab1ef5 /common/store.cpp | |
parent | a867e3cdc35a4a3efc65c6ba292b62f3e1cbeb72 (diff) | |
download | sink-b948ffa771054e8a7780ccfc38de11762bcdfcd6.tar.gz sink-b948ffa771054e8a7780ccfc38de11762bcdfcd6.zip |
React to new resources
Diffstat (limited to 'common/store.cpp')
-rw-r--r-- | common/store.cpp | 68 |
1 files changed, 54 insertions, 14 deletions
diff --git a/common/store.cpp b/common/store.cpp index 848afae..363878c 100644 --- a/common/store.cpp +++ b/common/store.cpp | |||
@@ -38,6 +38,8 @@ | |||
38 | 38 | ||
39 | SINK_DEBUG_AREA("store") | 39 | SINK_DEBUG_AREA("store") |
40 | 40 | ||
41 | Q_DECLARE_METATYPE(QSharedPointer<Sink::ResultEmitter<Sink::ApplicationDomain::SinkResource::Ptr>>) | ||
42 | |||
41 | namespace Sink { | 43 | namespace Sink { |
42 | 44 | ||
43 | QString Store::storageLocation() | 45 | QString Store::storageLocation() |
@@ -95,6 +97,27 @@ static QMap<QByteArray, QByteArray> getResources(const QList<QByteArray> &resour | |||
95 | return resources; | 97 | return resources; |
96 | } | 98 | } |
97 | 99 | ||
100 | |||
101 | template <class DomainType> | ||
102 | KAsync::Job<void> queryResource(const QByteArray resourceType, const QByteArray &resourceInstanceIdentifier, const Query &query, typename AggregatingResultEmitter<typename DomainType::Ptr>::Ptr aggregatingEmitter) | ||
103 | { | ||
104 | auto facade = FacadeFactory::instance().getFacade<DomainType>(resourceType, resourceInstanceIdentifier); | ||
105 | if (facade) { | ||
106 | SinkTrace() << "Trying to fetch from resource " << resourceInstanceIdentifier; | ||
107 | auto result = facade->load(query); | ||
108 | if (result.second) { | ||
109 | aggregatingEmitter->addEmitter(result.second); | ||
110 | } else { | ||
111 | SinkWarning() << "Null emitter for resource " << resourceInstanceIdentifier; | ||
112 | } | ||
113 | return result.first; | ||
114 | } else { | ||
115 | SinkTrace() << "Couldn' find a facade for " << resourceInstanceIdentifier; | ||
116 | // Ignore the error and carry on | ||
117 | return KAsync::null<void>(); | ||
118 | } | ||
119 | } | ||
120 | |||
98 | template <class DomainType> | 121 | template <class DomainType> |
99 | QSharedPointer<QAbstractItemModel> Store::loadModel(Query query) | 122 | QSharedPointer<QAbstractItemModel> Store::loadModel(Query query) |
100 | { | 123 | { |
@@ -117,24 +140,41 @@ QSharedPointer<QAbstractItemModel> Store::loadModel(Query query) | |||
117 | auto resources = getResources(query.resources, query.accounts, ApplicationDomain::getTypeName<DomainType>()); | 140 | auto resources = getResources(query.resources, query.accounts, ApplicationDomain::getTypeName<DomainType>()); |
118 | auto aggregatingEmitter = AggregatingResultEmitter<typename DomainType::Ptr>::Ptr::create(); | 141 | auto aggregatingEmitter = AggregatingResultEmitter<typename DomainType::Ptr>::Ptr::create(); |
119 | model->setEmitter(aggregatingEmitter); | 142 | model->setEmitter(aggregatingEmitter); |
143 | |||
144 | if (query.liveQuery && query.resources.isEmpty() && !ApplicationDomain::isGlobalType(ApplicationDomain::getTypeName<DomainType>())) { | ||
145 | SinkTrace() << "Listening for new resources"; | ||
146 | auto facade = FacadeFactory::instance().getFacade<ApplicationDomain::SinkResource>("", ""); | ||
147 | Q_ASSERT(facade); | ||
148 | Sink::Query resourceQuery; | ||
149 | resourceQuery.liveQuery = query.liveQuery; | ||
150 | auto result = facade->load(resourceQuery); | ||
151 | auto emitter = result.second; | ||
152 | emitter->onAdded([query, aggregatingEmitter](const ApplicationDomain::SinkResource::Ptr &resource) { | ||
153 | SinkTrace() << "Found new resources: " << resource->identifier(); | ||
154 | const auto resourceType = ResourceConfig::getResourceType(resource->identifier()); | ||
155 | Q_ASSERT(!resourceType.isEmpty()); | ||
156 | queryResource<DomainType>(resourceType, resource->identifier(), query, aggregatingEmitter).exec(); | ||
157 | }); | ||
158 | emitter->onModified([](const ApplicationDomain::SinkResource::Ptr &) { | ||
159 | }); | ||
160 | emitter->onRemoved([](const ApplicationDomain::SinkResource::Ptr &) { | ||
161 | }); | ||
162 | emitter->onInitialResultSetComplete([](const ApplicationDomain::SinkResource::Ptr &) { | ||
163 | }); | ||
164 | emitter->onComplete([query, aggregatingEmitter]() { | ||
165 | SinkTrace() << "Resource query complete"; | ||
166 | |||
167 | }); | ||
168 | model->setProperty("resourceEmitter", QVariant::fromValue(emitter)); | ||
169 | result.first.exec(); | ||
170 | } | ||
171 | |||
120 | KAsync::iterate(resources.keys()) | 172 | KAsync::iterate(resources.keys()) |
121 | .template each<void, QByteArray>([query, aggregatingEmitter, resources](const QByteArray &resourceInstanceIdentifier, KAsync::Future<void> &future) { | 173 | .template each<void, QByteArray>([query, aggregatingEmitter, resources](const QByteArray &resourceInstanceIdentifier, KAsync::Future<void> &future) { |
122 | const auto resourceType = resources.value(resourceInstanceIdentifier); | 174 | const auto resourceType = resources.value(resourceInstanceIdentifier); |
123 | auto facade = FacadeFactory::instance().getFacade<DomainType>(resourceType, resourceInstanceIdentifier); | 175 | queryResource<DomainType>(resourceType, resourceInstanceIdentifier, query, aggregatingEmitter).template then<void>([&future]() { |
124 | if (facade) { | ||
125 | SinkTrace() << "Trying to fetch from resource " << resourceInstanceIdentifier; | ||
126 | auto result = facade->load(query); | ||
127 | if (result.second) { | ||
128 | aggregatingEmitter->addEmitter(result.second); | ||
129 | } else { | ||
130 | SinkWarning() << "Null emitter for resource " << resourceInstanceIdentifier; | ||
131 | } | ||
132 | result.first.template then<void>([&future]() { future.setFinished(); }).exec(); | ||
133 | } else { | ||
134 | SinkTrace() << "Couldn' find a facade for " << resourceInstanceIdentifier; | ||
135 | // Ignore the error and carry on | ||
136 | future.setFinished(); | 176 | future.setFinished(); |
137 | } | 177 | }).exec(); |
138 | }) | 178 | }) |
139 | .exec(); | 179 | .exec(); |
140 | model->fetchMore(QModelIndex()); | 180 | model->fetchMore(QModelIndex()); |