1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
/*
* Copyright (c) 2018 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#include <Swift/Controllers/FdpFormSubmitController.h>
#include <Swiften/Disco/GetDiscoItemsRequest.h>
#include <Swiften/Elements/DiscoItems.h>
#include <Swiften/Queries/IQRouter.h>
#include <Swiften/Queries/PubSubRequest.h>
#include <Swift/Controllers/UIEvents/FdpFormSubmitWindowOpenUIEvent.h>
#include <Swift/Controllers/UIEvents/UIEventStream.h>
#include <Swift/Controllers/UIInterfaces/FdpFormSubmitWindow.h>
#include <Swift/Controllers/UIInterfaces/FdpFormSubmitWindowFactory.h>
namespace Swift {
FdpFormSubmitController::FdpFormSubmitController(const JID& self, IQRouter* iqRouter, UIEventStream* uiEventStream, FdpFormSubmitWindowFactory* factory) : selfJID_(self), iqRouter_(iqRouter), uiEventStream_(uiEventStream), factory_(factory), formSubmitWindow_(nullptr) {
fdpFormSubmitWindowOpenUIEventConnection_= uiEventStream_->onUIEvent.connect( [this](const std::shared_ptr<UIEvent>& uiEvent){ handleUIEvent(uiEvent); });
}
FdpFormSubmitController::~FdpFormSubmitController() {
}
void FdpFormSubmitController::handleUIEvent(const std::shared_ptr<UIEvent>& uiEvent) {
if (auto openEvent = std::dynamic_pointer_cast<FdpFormSubmitWindowOpenUIEvent>(uiEvent)) {
if (formSubmitWindow_) {
formSubmitWindow_->raise();
}
else {
createFormSubmitWindow();
}
}
}
void FdpFormSubmitController::createFormSubmitWindow() {
formSubmitWindow_ = factory_->createFdpFormSubmitWindow();
formSubmitWindow_->onCloseEvent.connect([this](){ closeFormSubmitWindow(); });
formSubmitWindow_->onRequestPubSubNodeData.connect([this](const std::string& string){ requestPubSubNodeData(string); });
formSubmitWindow_->onRequestTemplateForm.connect([this](const std::string& fdpTemplateNodeName){ requestTemplateForm(fdpTemplateNodeName); });
formSubmitWindow_->onSubmitForm.connect([this](const std::shared_ptr<Form>& form){ submitForm(form); });
formSubmitWindow_->show();
}
void FdpFormSubmitController::closeFormSubmitWindow() {
formSubmitWindow_.reset();
}
void FdpFormSubmitController::requestPubSubNodeData(const std::string& domainName) {
JID domainJID(domainName);
auto discoItemsRequest = GetDiscoItemsRequest::create(domainJID, iqRouter_);
discoItemsRequest->onResponse.connect(
[this, domainName](std::shared_ptr<DiscoItems> discoItems, ErrorPayload::ref errorPayloadRef) {
if (!discoItems || errorPayloadRef) {
formSubmitWindow_->showNodePlaceholder(FdpFormSubmitWindow::NodeError::DomainNotFound);
return;
}
currentDomain_ = domainName;
bool templateNodeAdded = false;
formSubmitWindow_->clearNodeData();
auto discoItemList = discoItems->getItems();
for (auto discoItem : discoItemList) {
auto node = discoItem.getNode();
if (node.substr(0, 13) != "fdp/template/") {
continue;
}
std::string nodeName = discoItem.getName().empty() ? node : discoItem.getName();
formSubmitWindow_->addNode(node, nodeName);
templateNodeAdded = true;
}
if (!templateNodeAdded) {
formSubmitWindow_->showNodePlaceholder(FdpFormSubmitWindow::NodeError::NoFdpNodesInDomain);
}
}
);
discoItemsRequest->send();
}
void FdpFormSubmitController::requestTemplateForm(const std::string& nodeName) {
auto pubSubItems = std::make_shared<PubSubItems>(nodeName);
pubSubItems->setMaximumItems(1);
auto formRequest = std::make_shared<PubSubRequest<PubSubItems>>(IQ::Type::Get, selfJID_, currentDomain_, pubSubItems, iqRouter_);
formRequest->onResponse.connect(
[this, nodeName](std::shared_ptr<PubSubItems> response, ErrorPayload::ref errorPayload) {
if (!response || errorPayload) {
formSubmitWindow_->showFormPlaceholder(FdpFormSubmitWindow::TemplateError::RequestFailed);
return;
}
auto pubSubItemList = response->getItems();
if (pubSubItemList.empty()) {
formSubmitWindow_->showFormPlaceholder(FdpFormSubmitWindow::TemplateError::CannotLocateForm);
return;
}
auto payloadList = pubSubItemList[0]->getData();
if (payloadList.empty()) {
formSubmitWindow_->showFormPlaceholder(FdpFormSubmitWindow::TemplateError::CannotLocateForm);
return;
}
if (auto form = std::dynamic_pointer_cast<Form>(payloadList[0])) {
currentTemplateNode_ = nodeName;
formSubmitWindow_->setFormData(form);
}
else {
formSubmitWindow_->showFormPlaceholder(FdpFormSubmitWindow::TemplateError::InvalidPayload);
return;
}
}
);
formRequest->send();
}
void FdpFormSubmitController::submitForm(const std::shared_ptr<Form>& form) {
std::string submittedNode = currentTemplateNode_;
submittedNode.replace(submittedNode.find("/template/", 0), 10, "/submitted/");
auto pubSubItem = std::make_shared<PubSubItem>();
auto pubSubItems = std::make_shared<PubSubItems>(submittedNode);
auto pubSubPublish = std::make_shared<PubSubPublish>();
pubSubPublish->setNode(submittedNode);
pubSubPublish->addItem(pubSubItem);
pubSubItem->addData(form);
pubSubItems->addItem(pubSubItem);
auto formRequest = std::make_shared<PubSubRequest<PubSubPublish>>(IQ::Type::Set, selfJID_, currentDomain_, pubSubPublish, iqRouter_);
formRequest->onResponse.connect(
[this](std::shared_ptr<PubSubPublish> response, ErrorPayload::ref errorPayload) {
if (!response || errorPayload) {
formSubmitWindow_->handleSubmitServerResponse(false);
return;
}
formSubmitWindow_->handleSubmitServerResponse(true);
}
);
formRequest->send();
}
}
|