summaryrefslogtreecommitdiffstats
blob: 3f846781e6308a6c8095c3fffff5e4fbc9c689b5 (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
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
/*
 * Copyright (c) 2010 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include "Swiften/VCards/VCardFileStorage.h"

#include <boost/filesystem/fstream.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>

#include <Swiften/Base/String.h>
#include <Swiften/StringCodecs/Hexify.h>
#include <Swiften/StringCodecs/SHA1.h>
#include <Swiften/Base/foreach.h>
#include "Swiften/JID/JID.h"
#include "Swiften/Base/ByteArray.h"
#include "Swiften/Elements/VCard.h"
#include "Swiften/Serializer/PayloadSerializers/VCardSerializer.h"
#include "Swiften/Parser/PayloadParsers/UnitTest/PayloadParserTester.h"
#include "Swiften/Parser/PayloadParsers/VCardParser.h"

namespace Swift {

VCardFileStorage::VCardFileStorage(boost::filesystem::path dir) : vcardsPath(dir) {
	cacheFile = vcardsPath / "vcards.xml";
	if (boost::filesystem::exists(cacheFile)) {
		boost::property_tree::ptree tree;
		try {
			boost::property_tree::xml_parser::read_xml(cacheFile.string(), tree);
		}
		catch (const boost::property_tree::xml_parser::xml_parser_error& e) {
			std::cerr << "Error reading vcards file: " << e.filename() << ":" << e.line() << ": " << e.message() << std::endl;
		}
		foreach(const boost::property_tree::ptree::value_type &v, tree.get_child("vcards")) {
			try {
				JID jid(v.second.get<std::string>("jid"));
				std::string hash(v.second.get<std::string>("phash"));
				if (jid.isValid()) {
					photoHashes.insert(std::make_pair(jid, hash));
				}
			}
			catch (const boost::property_tree::ptree_error& e) {
				std::cerr << "Invalid vcard value: " << e.what() << std::endl;
			}
		}
	}
}

boost::shared_ptr<VCard> VCardFileStorage::getVCard(const JID& jid) const {
	boost::filesystem::path vcardPath(getVCardPath(jid));
	if (boost::filesystem::exists(vcardPath)) {
		ByteArray data;
		data.readFromFile(vcardPath.string());

		VCardParser parser;
		PayloadParserTester tester(&parser);
		tester.parse(std::string(data.getData(), data.getSize()));
		return boost::dynamic_pointer_cast<VCard>(parser.getPayload());
	}
	else {
		return boost::shared_ptr<VCard>();
	}
}

void VCardFileStorage::setVCard(const JID& jid, VCard::ref v) {
	boost::filesystem::path vcardPath(getVCardPath(jid));
	if (!boost::filesystem::exists(vcardPath.parent_path())) {
		try {
			boost::filesystem::create_directories(vcardPath.parent_path());
		}
		catch (const boost::filesystem::filesystem_error& e) {
			std::cerr << "ERROR: " << e.what() << std::endl;
		}
	}
	boost::filesystem::ofstream file(getVCardPath(jid));
	file << VCardSerializer().serializePayload(v);
	file.close();
	getAndUpdatePhotoHash(jid, v);
}

boost::filesystem::path VCardFileStorage::getVCardPath(const JID& jid) const {
	std::string file(jid.toString());
	String::replaceAll(file, '/', "%2f");
	return boost::filesystem::path(vcardsPath / (file + ".xml"));
}

std::string VCardFileStorage::getPhotoHash(const JID& jid) const {
	PhotoHashMap::const_iterator i = photoHashes.find(jid);
	if (i != photoHashes.end()) {
		return i->second;
	}
	else {
		VCard::ref vCard = getVCard(jid);
		return getAndUpdatePhotoHash(jid, vCard);
	}
}

std::string VCardFileStorage::getAndUpdatePhotoHash(const JID& jid, VCard::ref vCard) const {
	std::string hash;
	if (vCard && !vCard->getPhoto().isEmpty()) {
		hash = Hexify::hexify(SHA1::getHash(vCard->getPhoto()));
	}
	std::pair<PhotoHashMap::iterator, bool> r = photoHashes.insert(std::make_pair(jid, hash));
	if (r.second) {
		savePhotoHashes();
	}
	else if (r.first->second != hash) {
		r.first->second = hash;
		savePhotoHashes();
	}
	return hash;
}

void VCardFileStorage::savePhotoHashes() const {
	boost::property_tree::ptree tree;
	for (PhotoHashMap::const_iterator i = photoHashes.begin(); i != photoHashes.end(); ++i) {
		boost::property_tree::ptree entry;
		entry.put("jid", i->first.toString());
		entry.put("phash", i->second);
		tree.add_child("vcards.vcard", entry);
	}
	try {
		boost::property_tree::xml_parser::write_xml(cacheFile.string(), tree);
	}
	catch (const boost::property_tree::xml_parser::xml_parser_error& e) {
		std::cerr << "Error writing vcards file: " << e.filename() << ": " << e.message() << std::endl;
	}
}


}