diff options
author | Remko Tronçon <git@el-tramo.be> | 2011-04-25 07:26:20 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2011-04-25 07:48:15 (GMT) |
commit | d80935c165ac2709b1dfba1f38c06f2cc1d91ee9 (patch) | |
tree | c3e2b7dd23c89d07b17802686bd0a6db80b7b5d4 /Swiften/VCards | |
parent | 20f9d6330351b373d40b40222433e11a2a0f2713 (diff) | |
download | swift-d80935c165ac2709b1dfba1f38c06f2cc1d91ee9.zip swift-d80935c165ac2709b1dfba1f38c06f2cc1d91ee9.tar.bz2 |
Moved file storages to Swift.
Diffstat (limited to 'Swiften/VCards')
-rw-r--r-- | Swiften/VCards/SConscript | 1 | ||||
-rw-r--r-- | Swiften/VCards/VCardFileStorage.cpp | 107 | ||||
-rw-r--r-- | Swiften/VCards/VCardFileStorage.h | 38 |
3 files changed, 0 insertions, 146 deletions
diff --git a/Swiften/VCards/SConscript b/Swiften/VCards/SConscript index e980ba3..c20c17d 100644 --- a/Swiften/VCards/SConscript +++ b/Swiften/VCards/SConscript @@ -3,6 +3,5 @@ Import("swiften_env") objects = swiften_env.SwiftenObject([ "VCardManager.cpp", "VCardStorage.cpp", - "VCardFileStorage.cpp", ]) swiften_env.Append(SWIFTEN_OBJECTS = [objects]) diff --git a/Swiften/VCards/VCardFileStorage.cpp b/Swiften/VCards/VCardFileStorage.cpp deleted file mode 100644 index 1b23635..0000000 --- a/Swiften/VCards/VCardFileStorage.cpp +++ /dev/null @@ -1,107 +0,0 @@ -/* - * 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/filesystem.hpp> -#include <iostream> - -#include <Swiften/Entity/GenericPayloadPersister.h> -#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/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) : vcardsPath(dir) { - 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 { - return VCardPersister().loadPayloadGeneric(getVCardPath(jid)); -} - -void VCardFileStorage::setVCard(const JID& jid, VCard::ref v) { - VCardPersister().savePayload(v, getVCardPath(jid)); - 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 { - try { - boost::filesystem::ofstream file(cacheFile); - for (PhotoHashMap::const_iterator i = photoHashes.begin(); i != photoHashes.end(); ++i) { - file << i->second << " " << i->first.toString() << std::endl; - } - file.close(); - } - catch (...) { - std::cerr << "Error writing vcards file" << std::endl; - } -} diff --git a/Swiften/VCards/VCardFileStorage.h b/Swiften/VCards/VCardFileStorage.h deleted file mode 100644 index ba422f4..0000000 --- a/Swiften/VCards/VCardFileStorage.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2010 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 VCardFileStorage : public VCardStorage { - public: - VCardFileStorage(boost::filesystem::path dir); - - virtual VCard::ref getVCard(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; - boost::filesystem::path cacheFile; - typedef std::map<JID, std::string> PhotoHashMap; - mutable PhotoHashMap photoHashes; - }; -} |