diff options
-rw-r--r-- | Swiften/AdHoc/OutgoingAdHocCommandSession.cpp | 39 | ||||
-rw-r--r-- | Swiften/AdHoc/OutgoingAdHocCommandSession.h | 7 | ||||
-rw-r--r-- | Swiften/Base/Algorithm.h | 11 |
3 files changed, 32 insertions, 25 deletions
diff --git a/Swiften/AdHoc/OutgoingAdHocCommandSession.cpp b/Swiften/AdHoc/OutgoingAdHocCommandSession.cpp index 758b5e7..0632bbf 100644 --- a/Swiften/AdHoc/OutgoingAdHocCommandSession.cpp +++ b/Swiften/AdHoc/OutgoingAdHocCommandSession.cpp @@ -7,8 +7,10 @@ #include <Swiften/AdHoc/OutgoingAdHocCommandSession.h> #include <boost/bind.hpp> +#include <boost/smart_ptr/make_shared.hpp> #include <Swiften/Queries/GenericRequest.h> +#include <Swiften/Base/Algorithm.h> namespace Swift { OutgoingAdHocCommandSession::OutgoingAdHocCommandSession(const DiscoItems::Item& command, IQRouter* iqRouter) : command_(command), iqRouter_(iqRouter), isMultiStage_(false) { @@ -19,7 +21,7 @@ void OutgoingAdHocCommandSession::handleResponse(boost::shared_ptr<Command> payl if (error) { onError(error); } else { - const std::vector<Command::Action> actions = payload->getAvailableActions(); + const std::vector<Command::Action>& actions = payload->getAvailableActions(); actionStates_.clear(); if (payload->getStatus() == Command::Executing ) { actionStates_[Command::Cancel] = EnabledAndPresent; @@ -50,53 +52,44 @@ void OutgoingAdHocCommandSession::handleResponse(boost::shared_ptr<Command> payl } } -bool OutgoingAdHocCommandSession::getIsMultiStage() { +bool OutgoingAdHocCommandSession::getIsMultiStage() const { return isMultiStage_; } void OutgoingAdHocCommandSession::start() { - boost::shared_ptr<Payload> commandPayload(new Command(command_.getNode())); - boost::shared_ptr<GenericRequest<Command> > commandRequest(new GenericRequest<Command>(IQ::Set, command_.getJID(), commandPayload, iqRouter_)); + boost::shared_ptr<GenericRequest<Command> > commandRequest = boost::make_shared< GenericRequest<Command> >(IQ::Set, command_.getJID(), boost::make_shared<Command>(command_.getNode()), iqRouter_); commandRequest->onResponse.connect(boost::bind(&OutgoingAdHocCommandSession::handleResponse, this, _1, _2)); commandRequest->send(); } void OutgoingAdHocCommandSession::cancel() { if (!sessionID_.empty()) { - boost::shared_ptr<Payload> commandPayload(new Command(command_.getNode(), sessionID_, Command::Cancel)); - boost::shared_ptr<GenericRequest<Command> > commandRequest(new GenericRequest<Command>(IQ::Set, command_.getJID(), commandPayload, iqRouter_)); - commandRequest->onResponse.connect(boost::bind(&OutgoingAdHocCommandSession::handleResponse, this, _1, _2)); - commandRequest->send(); + submitForm(Form::ref(), Command::Cancel); } } void OutgoingAdHocCommandSession::goBack() { - boost::shared_ptr<Payload> commandPayload(new Command(command_.getNode(), sessionID_, Command::Prev)); - boost::shared_ptr<GenericRequest<Command> > commandRequest(new GenericRequest<Command>(IQ::Set, command_.getJID(), commandPayload, iqRouter_)); - commandRequest->onResponse.connect(boost::bind(&OutgoingAdHocCommandSession::handleResponse, this, _1, _2)); - commandRequest->send(); + submitForm(Form::ref(), Command::Prev); } void OutgoingAdHocCommandSession::complete(Form::ref form) { - Command* command = new Command(command_.getNode(), sessionID_, Command::Complete); - boost::shared_ptr<Payload> commandPayload(command); - command->setForm(form); - boost::shared_ptr<GenericRequest<Command> > commandRequest(new GenericRequest<Command>(IQ::Set, command_.getJID(), commandPayload, iqRouter_)); - commandRequest->onResponse.connect(boost::bind(&OutgoingAdHocCommandSession::handleResponse, this, _1, _2)); - commandRequest->send(); + submitForm(form, Command::Complete); } void OutgoingAdHocCommandSession::goNext(Form::ref form) { - Command* command = new Command(command_.getNode(), sessionID_, Command::Next); - boost::shared_ptr<Payload> commandPayload(command); + submitForm(form, Command::Next); +} + +void OutgoingAdHocCommandSession::submitForm(Form::ref form, Command::Action action) { + boost::shared_ptr<Command> command(boost::make_shared<Command>(command_.getNode(), sessionID_, action)); command->setForm(form); - boost::shared_ptr<GenericRequest<Command> > commandRequest(new GenericRequest<Command>(IQ::Set, command_.getJID(), commandPayload, iqRouter_)); + boost::shared_ptr<GenericRequest<Command> > commandRequest = boost::make_shared< GenericRequest<Command> >(IQ::Set, command_.getJID(), command, iqRouter_); commandRequest->onResponse.connect(boost::bind(&OutgoingAdHocCommandSession::handleResponse, this, _1, _2)); commandRequest->send(); } -OutgoingAdHocCommandSession::ActionState OutgoingAdHocCommandSession::getActionState(Command::Action action) { - return actionStates_[action]; +OutgoingAdHocCommandSession::ActionState OutgoingAdHocCommandSession::getActionState(Command::Action action) const { + return get(actionStates_, action, Absent); } } diff --git a/Swiften/AdHoc/OutgoingAdHocCommandSession.h b/Swiften/AdHoc/OutgoingAdHocCommandSession.h index 1e0a42d..6b80300 100644 --- a/Swiften/AdHoc/OutgoingAdHocCommandSession.h +++ b/Swiften/AdHoc/OutgoingAdHocCommandSession.h @@ -56,7 +56,7 @@ namespace Swift { /** * Is the form multi-stage? */ - bool getIsMultiStage(); + bool getIsMultiStage() const; /** * Emitted when the form for the next stage is available. @@ -75,9 +75,12 @@ namespace Swift { * Use for Next, Prev, Cancel and Complete only. * If no actions are available, the command has completed. */ - ActionState getActionState(Command::Action action); + ActionState getActionState(Command::Action action) const; + private: void handleResponse(boost::shared_ptr<Command> payload, ErrorPayload::ref error); + void submitForm(Form::ref, Command::Action action); + private: DiscoItems::Item command_; IQRouter* iqRouter_; diff --git a/Swiften/Base/Algorithm.h b/Swiften/Base/Algorithm.h index ea9530b..4d7f1de 100644 --- a/Swiften/Base/Algorithm.h +++ b/Swiften/Base/Algorithm.h @@ -93,6 +93,17 @@ namespace Swift { target.insert(target.end(), source.begin(), source.end()); } + template<typename A, typename B, typename C, typename D> + B get(const std::map<A, B, C, D>& map, const A& key, const B& defaultValue) { + typename std::map<A, B, C, D>::const_iterator i = map.find(key); + if (i != map.end()) { + return i->second; + } + else { + return defaultValue; + } + } + /* * Functors */ |