diff options
Diffstat (limited to 'Swift/Controllers/Storages')
-rw-r--r-- | Swift/Controllers/Storages/VCardFileStorage.cpp | 12 | ||||
-rw-r--r-- | Swift/Controllers/Storages/VCardFileStorage.h | 4 |
2 files changed, 14 insertions, 2 deletions
diff --git a/Swift/Controllers/Storages/VCardFileStorage.cpp b/Swift/Controllers/Storages/VCardFileStorage.cpp index b22e235..876d642 100644 --- a/Swift/Controllers/Storages/VCardFileStorage.cpp +++ b/Swift/Controllers/Storages/VCardFileStorage.cpp @@ -1,96 +1,106 @@ /* - * Copyright (c) 2010 Remko Tronçon + * Copyright (c) 2010-2014 Remko Tronçon * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. */ #include "Swift/Controllers/Storages/VCardFileStorage.h" #include <boost/filesystem/fstream.hpp> #include <boost/filesystem.hpp> #include <iostream> #include <Swiften/Entity/GenericPayloadPersister.h> #include <Swiften/Base/String.h> #include <Swiften/StringCodecs/Hexify.h> #include <Swiften/Base/foreach.h> #include <Swiften/Base/Path.h> #include <Swiften/Crypto/CryptoProvider.h> #include "Swiften/JID/JID.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" using namespace Swift; typedef GenericPayloadPersister<VCard, VCardParser, VCardSerializer> VCardPersister; VCardFileStorage::VCardFileStorage(boost::filesystem::path dir, CryptoProvider* crypto) : VCardStorage(crypto), vcardsPath(dir), crypto(crypto) { cacheFile = vcardsPath / "phashes"; if (boost::filesystem::exists(cacheFile)) { try { boost::filesystem::ifstream file(cacheFile); std::string line; if (file.is_open()) { while (!file.eof()) { getline(file, line); std::pair<std::string, std::string> r = String::getSplittedAtFirst(line, ' '); JID jid(r.second); if (jid.isValid()) { photoHashes.insert(std::make_pair(jid, r.first)); } else if (!r.first.empty() || !r.second.empty()) { std::cerr << "Invalid entry in phashes file" << std::endl; } } } } catch (...) { std::cerr << "Error reading phashes file" << std::endl; } } } boost::shared_ptr<VCard> VCardFileStorage::getVCard(const JID& jid) const { boost::shared_ptr<VCard> result = VCardPersister().loadPayloadGeneric(getVCardPath(jid)); getAndUpdatePhotoHash(jid, result); return result; } +boost::posix_time::ptime VCardFileStorage::getVCardWriteTime(const JID& jid) const { + if (vcardWriteTimes.find(jid) == vcardWriteTimes.end()) { + return boost::posix_time::ptime(); + } + else { + return vcardWriteTimes.at(jid); + } +} + void VCardFileStorage::setVCard(const JID& jid, VCard::ref v) { + vcardWriteTimes[jid] = boost::posix_time::second_clock::universal_time(); VCardPersister().savePayload(v, getVCardPath(jid)); getAndUpdatePhotoHash(jid, v); } boost::filesystem::path VCardFileStorage::getVCardPath(const JID& jid) const { try { std::string file(jid.toString()); String::replaceAll(file, '/', "%2f"); return boost::filesystem::path(vcardsPath / stringToPath(file + ".xml")); } catch (const boost::filesystem::filesystem_error& e) { std::cerr << "ERROR: " << e.what() << std::endl; return boost::filesystem::path(); } } 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().empty()) { hash = Hexify::hexify(crypto->getSHA1Hash(vCard->getPhoto())); } std::pair<PhotoHashMap::iterator, bool> r = photoHashes.insert(std::make_pair(jid, hash)); if (r.second) { savePhotoHashes(); diff --git a/Swift/Controllers/Storages/VCardFileStorage.h b/Swift/Controllers/Storages/VCardFileStorage.h index 2c3af3d..8ba001e 100644 --- a/Swift/Controllers/Storages/VCardFileStorage.h +++ b/Swift/Controllers/Storages/VCardFileStorage.h @@ -1,41 +1,43 @@ /* - * Copyright (c) 2010-2013 Remko Tronçon + * Copyright (c) 2010-2014 Remko Tronçon * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. */ #pragma once #include <boost/shared_ptr.hpp> #include <boost/filesystem/path.hpp> #include <string> #include <map> #include "Swiften/VCards/VCardStorage.h" namespace Swift { class CryptoProvider; class VCardFileStorage : public VCardStorage { public: VCardFileStorage(boost::filesystem::path dir, CryptoProvider* crypto); virtual VCard::ref getVCard(const JID& jid) const; + virtual boost::posix_time::ptime getVCardWriteTime(const JID& jid) const; virtual void setVCard(const JID& jid, VCard::ref v); virtual std::string getPhotoHash(const JID&) const; private: boost::filesystem::path getVCardPath(const JID&) const; std::string getAndUpdatePhotoHash(const JID& jid, VCard::ref vcard) const; void savePhotoHashes() const; private: boost::filesystem::path vcardsPath; CryptoProvider* crypto; boost::filesystem::path cacheFile; typedef std::map<JID, std::string> PhotoHashMap; mutable PhotoHashMap photoHashes; + std::map<JID, boost::posix_time::ptime> vcardWriteTimes; }; } |