summaryrefslogtreecommitdiffstats
blob: 769fc844e32851069f84897e2781d871576f60f5 (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
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
/*
 * Copyright (c) 2012 Tobias Markmann
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

#include "JingleFileTransferFileInfoParser.h"

#include <boost/optional.hpp>
#include <boost/lexical_cast.hpp>

#include <Swiften/Base/DateTime.h>
#include <Swiften/Base/Log.h>

#include <cstdio>

namespace Swift {

JingleFileTransferFileInfoParser::JingleFileTransferFileInfoParser() : level(0) {
	
}

void JingleFileTransferFileInfoParser::handleStartElement(const std::string& element, const std::string&, const AttributeMap& attributes) {
	if (level == 0) {

	} else if (level == 1) {
		if (element == "date" || element == "desc" || element == "name" || element == "size") {
			cdataBuffer.clear();
		} else if (element == "hash") {
			cdataBuffer.clear();
			getPayloadInternal()->setAlgo(attributes.getAttributeValue("algo").get_value_or("md5"));
		} else {
			if (element == "range") {
				int offset = 0;
				try {
					offset = boost::lexical_cast<boost::uintmax_t>(attributes.getAttributeValue("offset").get_value_or("0"));
				} catch (boost::bad_lexical_cast &) {
					offset = 0;
				}
				if (offset == 0) {
					getPayloadInternal()->setSupportsRangeRequests(true);
				} else {
					getPayloadInternal()->setRangeOffset(offset);
				}
			}
		}
	}
	++level;
}

void JingleFileTransferFileInfoParser::handleEndElement(const std::string& element, const std::string&) {
	--level;
	if (element == "date") {
		getPayloadInternal()->setDate(stringToDateTime(cdataBuffer));
	} else if (element == "desc") {
		getPayloadInternal()->setDescription(cdataBuffer);
	} else if (element == "name") {
		getPayloadInternal()->setName(cdataBuffer);
	} else if (element == "size") {
		try {
			getPayloadInternal()->setSize(boost::lexical_cast<boost::uintmax_t>(cdataBuffer));
		} catch (boost::bad_lexical_cast &) {
			getPayloadInternal()->setSize(0);
		}
	} else if (element == "hash") {
		getPayloadInternal()->setHash(cdataBuffer);
	}
}

void JingleFileTransferFileInfoParser::handleCharacterData(const std::string& data) {
	cdataBuffer += data;
}

}