summaryrefslogtreecommitdiffstats
blob: ac361cc1fe3aef8aac0ec1d784e62a4dfd28d991 (plain)
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
#include "Swiften/Queries/Request.h"
#include "Swiften/Queries/IQRouter.h"
#include "Swiften/EventLoop/MainEventLoop.h"

namespace Swift {

Request::Request(IQ::Type type, const JID& receiver, boost::shared_ptr<Payload> payload, IQRouter* router, AutoDeleteBehavior autoDeleteBehavior) : IQHandler(router), type_(type), receiver_(receiver), payload_(payload), autoDeleteBehavior_(autoDeleteBehavior) {
	id_ = getRouter()->getNewIQID();
}

void Request::send() {
	boost::shared_ptr<IQ> iq(new IQ(type_));
	iq->setTo(receiver_);
	iq->addPayload(payload_);
	iq->setID(id_);
	getRouter()->sendIQ(iq);
}

bool Request::handleIQ(boost::shared_ptr<IQ> iq) {
	if (iq->getID() == id_) {
		if (iq->getType() == IQ::Result) {
			handleResponse(iq->getPayloadOfSameType(payload_), boost::optional<Error>());
			if (autoDeleteBehavior_ == AutoDeleteAfterResponse) {
				MainEventLoop::deleteLater(this);
			}
			return true;
		}
		else {
			// FIXME: Get proper error
			handleResponse(boost::shared_ptr<Payload>(), boost::optional<Error>(Error::UndefinedCondition));
			return true;
		}
	}
	else {
		return false;
	}
}

}