diff options
author | Remko Tronçon <git@el-tramo.be> | 2010-04-07 19:57:03 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2010-08-24 17:10:23 (GMT) |
commit | 6e4b357141a6d09632f1e96d0eaf54f79daf52c9 (patch) | |
tree | 7627e6309e9b7048e0da0482fff389a6648b2aae /Swiften/VCards | |
parent | 1586683c148825cd24863aec6ef940c8c0fec5ea (diff) | |
download | swift-6e4b357141a6d09632f1e96d0eaf54f79daf52c9.zip swift-6e4b357141a6d09632f1e96d0eaf54f79daf52c9.tar.bz2 |
Added VCardStorage.
Diffstat (limited to 'Swiften/VCards')
-rw-r--r-- | Swiften/VCards/SConscript | 8 | ||||
-rw-r--r-- | Swiften/VCards/VCardFileStorage.cpp | 53 | ||||
-rw-r--r-- | Swiften/VCards/VCardFileStorage.h | 28 | ||||
-rw-r--r-- | Swiften/VCards/VCardFileStorageFactory.h | 27 | ||||
-rw-r--r-- | Swiften/VCards/VCardStorage.cpp | 14 | ||||
-rw-r--r-- | Swiften/VCards/VCardStorage.h | 23 | ||||
-rw-r--r-- | Swiften/VCards/VCardStorageFactory.cpp | 14 | ||||
-rw-r--r-- | Swiften/VCards/VCardStorageFactory.h | 19 |
8 files changed, 186 insertions, 0 deletions
diff --git a/Swiften/VCards/SConscript b/Swiften/VCards/SConscript new file mode 100644 index 0000000..538eb4a --- /dev/null +++ b/Swiften/VCards/SConscript @@ -0,0 +1,8 @@ +Import("swiften_env") + +objects = swiften_env.StaticObject([ + "VCardStorage.cpp", + "VCardFileStorage.cpp", + "VCardStorageFactory.cpp", + ]) +swiften_env.Append(SWIFTEN_OBJECTS = [objects]) diff --git a/Swiften/VCards/VCardFileStorage.cpp b/Swiften/VCards/VCardFileStorage.cpp new file mode 100644 index 0000000..d3163fb --- /dev/null +++ b/Swiften/VCards/VCardFileStorage.cpp @@ -0,0 +1,53 @@ +/* + * 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 "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) { +} + +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(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, boost::shared_ptr<VCard> v) { + boost::filesystem::path vcardPath(getVCardPath(jid)); + if (!boost::filesystem::exists(vcardPath.parent_path())) { + boost::filesystem::create_directories(vcardPath.parent_path()); + } + boost::filesystem::ofstream file(getVCardPath(jid)); + file << VCardSerializer().serializePayload(v); + file.close(); +} + +boost::filesystem::path VCardFileStorage::getVCardPath(const JID& jid) const { + return boost::filesystem::path(vcardsPath / (jid.toBare().toString().getUTF8String() + ".xml")); +} + +} diff --git a/Swiften/VCards/VCardFileStorage.h b/Swiften/VCards/VCardFileStorage.h new file mode 100644 index 0000000..d75ac92 --- /dev/null +++ b/Swiften/VCards/VCardFileStorage.h @@ -0,0 +1,28 @@ +/* + * 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.hpp> + +#include "Swiften/VCards/VCardStorage.h" + +namespace Swift { + class VCardFileStorage : public VCardStorage { + public: + VCardFileStorage(boost::filesystem::path dir); + + virtual boost::shared_ptr<VCard> getVCard(const JID& jid) const; + virtual void setVCard(const JID& jid, boost::shared_ptr<VCard> v); + + private: + boost::filesystem::path getVCardPath(const JID&) const; + + private: + boost::filesystem::path vcardsPath; + }; +} diff --git a/Swiften/VCards/VCardFileStorageFactory.h b/Swiften/VCards/VCardFileStorageFactory.h new file mode 100644 index 0000000..136c6a7 --- /dev/null +++ b/Swiften/VCards/VCardFileStorageFactory.h @@ -0,0 +1,27 @@ +/* + * 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/filesystem.hpp> + +#include "Swiften/VCards/VCardStorageFactory.h" +#include "Swiften/VCards/VCardFileStorage.h" + +namespace Swift { + class VCardFileStorageFactory : public VCardStorageFactory { + public: + VCardFileStorageFactory(boost::filesystem::path base) : base(base) { + } + + virtual VCardStorage* createVCardStorage(const String& profile) { + return new VCardFileStorage(base / profile.getUTF8String()); + } + + private: + boost::filesystem::path base; + }; +} diff --git a/Swiften/VCards/VCardStorage.cpp b/Swiften/VCards/VCardStorage.cpp new file mode 100644 index 0000000..cacd083 --- /dev/null +++ b/Swiften/VCards/VCardStorage.cpp @@ -0,0 +1,14 @@ +/* + * 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/VCardStorage.h" + +namespace Swift { + +VCardStorage::~VCardStorage() { +} + +} diff --git a/Swiften/VCards/VCardStorage.h b/Swiften/VCards/VCardStorage.h new file mode 100644 index 0000000..2a9044c --- /dev/null +++ b/Swiften/VCards/VCardStorage.h @@ -0,0 +1,23 @@ +/* + * 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 "Swiften/Elements/VCard.h" + +namespace Swift { + class JID; + + class VCardStorage { + public: + virtual ~VCardStorage(); + + virtual boost::shared_ptr<VCard> getVCard(const JID& jid) const = 0; + virtual void setVCard(const JID&, boost::shared_ptr<VCard>) = 0; + }; +} diff --git a/Swiften/VCards/VCardStorageFactory.cpp b/Swiften/VCards/VCardStorageFactory.cpp new file mode 100644 index 0000000..64a3e88 --- /dev/null +++ b/Swiften/VCards/VCardStorageFactory.cpp @@ -0,0 +1,14 @@ +/* + * 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/VCardStorageFactory.h" + +namespace Swift { + +VCardStorageFactory::~VCardStorageFactory() { +} + +} diff --git a/Swiften/VCards/VCardStorageFactory.h b/Swiften/VCards/VCardStorageFactory.h new file mode 100644 index 0000000..7bef87b --- /dev/null +++ b/Swiften/VCards/VCardStorageFactory.h @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2010 Remko Tronçon + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#pragma once + +namespace Swift { + class VCardStorage; + class String; + + class VCardStorageFactory { + public: + virtual ~VCardStorageFactory(); + + virtual VCardStorage* createVCardStorage(const String& profile) = 0; + }; +} |