From 27d7cb1c99a55bfb45658b4f20f901926e526cae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Fri, 13 May 2011 23:57:48 +0200 Subject: Refactored OutgoingAdHocCommandSession a bit. 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 #include +#include #include +#include 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 payl if (error) { onError(error); } else { - const std::vector actions = payload->getAvailableActions(); + const std::vector& actions = payload->getAvailableActions(); actionStates_.clear(); if (payload->getStatus() == Command::Executing ) { actionStates_[Command::Cancel] = EnabledAndPresent; @@ -50,53 +52,44 @@ void OutgoingAdHocCommandSession::handleResponse(boost::shared_ptr payl } } -bool OutgoingAdHocCommandSession::getIsMultiStage() { +bool OutgoingAdHocCommandSession::getIsMultiStage() const { return isMultiStage_; } void OutgoingAdHocCommandSession::start() { - boost::shared_ptr commandPayload(new Command(command_.getNode())); - boost::shared_ptr > commandRequest(new GenericRequest(IQ::Set, command_.getJID(), commandPayload, iqRouter_)); + boost::shared_ptr > commandRequest = boost::make_shared< GenericRequest >(IQ::Set, command_.getJID(), boost::make_shared(command_.getNode()), iqRouter_); commandRequest->onResponse.connect(boost::bind(&OutgoingAdHocCommandSession::handleResponse, this, _1, _2)); commandRequest->send(); } void OutgoingAdHocCommandSession::cancel() { if (!sessionID_.empty()) { - boost::shared_ptr commandPayload(new Command(command_.getNode(), sessionID_, Command::Cancel)); - boost::shared_ptr > commandRequest(new GenericRequest(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 commandPayload(new Command(command_.getNode(), sessionID_, Command::Prev)); - boost::shared_ptr > commandRequest(new GenericRequest(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 commandPayload(command); - command->setForm(form); - boost::shared_ptr > commandRequest(new GenericRequest(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 commandPayload(command); + submitForm(form, Command::Next); +} + +void OutgoingAdHocCommandSession::submitForm(Form::ref form, Command::Action action) { + boost::shared_ptr command(boost::make_shared(command_.getNode(), sessionID_, action)); command->setForm(form); - boost::shared_ptr > commandRequest(new GenericRequest(IQ::Set, command_.getJID(), commandPayload, iqRouter_)); + boost::shared_ptr > commandRequest = boost::make_shared< GenericRequest >(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 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 + B get(const std::map& map, const A& key, const B& defaultValue) { + typename std::map::const_iterator i = map.find(key); + if (i != map.end()) { + return i->second; + } + else { + return defaultValue; + } + } + /* * Functors */ -- cgit v0.10.2-6-g49f6