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 | |
parent | 1586683c148825cd24863aec6ef940c8c0fec5ea (diff) | |
download | swift-6e4b357141a6d09632f1e96d0eaf54f79daf52c9.zip swift-6e4b357141a6d09632f1e96d0eaf54f79daf52c9.tar.bz2 |
Added VCardStorage.
Diffstat (limited to 'Swiften')
-rw-r--r-- | Swiften/Elements/VCard.h | 3 | ||||
-rw-r--r-- | Swiften/QA/SConscript | 1 | ||||
-rw-r--r-- | Swiften/QA/StorageTest/.gitignore | 1 | ||||
-rw-r--r-- | Swiften/QA/StorageTest/SConscript | 17 | ||||
-rw-r--r-- | Swiften/QA/StorageTest/VCardFileStorageTest.cpp | 78 | ||||
-rw-r--r-- | Swiften/SConscript | 1 | ||||
-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 |
14 files changed, 286 insertions, 1 deletions
diff --git a/Swiften/Elements/VCard.h b/Swiften/Elements/VCard.h index 2b2e99f..60f94b1 100644 --- a/Swiften/Elements/VCard.h +++ b/Swiften/Elements/VCard.h @@ -7,11 +7,12 @@ #pragma once #include "Swiften/Base/String.h" +#include "Swiften/Base/Shared.h" #include "Swiften/Base/ByteArray.h" #include "Swiften/Elements/Payload.h" namespace Swift { - class VCard : public Payload { + class VCard : public Payload, public Shared<VCard> { public: VCard() {} diff --git a/Swiften/QA/SConscript b/Swiften/QA/SConscript index 4cc56c9..e9c1546 100644 --- a/Swiften/QA/SConscript +++ b/Swiften/QA/SConscript @@ -3,4 +3,5 @@ SConscript(dirs = [ "ReconnectTest", "ClientTest", "DNSSDTest", + "StorageTest", ]) diff --git a/Swiften/QA/StorageTest/.gitignore b/Swiften/QA/StorageTest/.gitignore new file mode 100644 index 0000000..10d2e31 --- /dev/null +++ b/Swiften/QA/StorageTest/.gitignore @@ -0,0 +1 @@ +StorageTest diff --git a/Swiften/QA/StorageTest/SConscript b/Swiften/QA/StorageTest/SConscript new file mode 100644 index 0000000..c7401e0 --- /dev/null +++ b/Swiften/QA/StorageTest/SConscript @@ -0,0 +1,17 @@ +import os + +Import("env") + +if env["TEST"] : + myenv = env.Clone() + myenv.MergeFlags(myenv["CHECKER_FLAGS"]) + myenv.MergeFlags(myenv["SWIFTEN_FLAGS"]) + myenv.MergeFlags(myenv["CPPUNIT_FLAGS"]) + myenv.MergeFlags(myenv["BOOST_FLAGS"]) + myenv.MergeFlags(myenv["LIBIDN_FLAGS"]) + myenv.MergeFlags(myenv.get("EXPAT_FLAGS", {})) + + tester = myenv.Program("StorageTest", [ + "VCardFileStorageTest.cpp", + ]) + myenv.Test(tester, "system") diff --git a/Swiften/QA/StorageTest/VCardFileStorageTest.cpp b/Swiften/QA/StorageTest/VCardFileStorageTest.cpp new file mode 100644 index 0000000..9704409 --- /dev/null +++ b/Swiften/QA/StorageTest/VCardFileStorageTest.cpp @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2010 Remko Tronçon + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/extensions/TestFactoryRegistry.h> + +#include "Swiften/VCards/VCardFileStorage.h" +#include "Swiften/JID/JID.h" +#include "Swiften/Application/PlatformApplicationPathProvider.h" +#include "Swiften/Elements/VCard.h" + +using namespace Swift; + +class VCardFileStorageTest : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(VCardFileStorageTest); + CPPUNIT_TEST(testSetVCard); + CPPUNIT_TEST(testGetVCard); + CPPUNIT_TEST(testGetVCard_FileDoesNotExist); + CPPUNIT_TEST_SUITE_END(); + + public: + void setUp() { + pathProvider = new PlatformApplicationPathProvider("VCardStorageTest"); + vcardsPath = pathProvider->getExecutableDir() / "vcards"; + boost::filesystem::remove_all(vcardsPath); + } + + void tearDown() { + delete pathProvider; + boost::filesystem::remove_all(vcardsPath); + } + + void testSetVCard() { + std::auto_ptr<VCardFileStorage> testling(createTestling()); + VCard::ref vcard(new VCard()); + vcard->setFullName("Alice In Wonderland"); + vcard->setEMail("alice@wonderland.lit"); + + testling->setVCard(JID("alice@wonderland.lit"), vcard); + + boost::filesystem::path vcardFile(vcardsPath / "alice@wonderland.lit.xml"); + CPPUNIT_ASSERT(boost::filesystem::exists(vcardFile)); + ByteArray data; + data.readFromFile(vcardFile.string()); + CPPUNIT_ASSERT(data.toString().beginsWith("<vCard xmlns=\"vcard-temp\">")); + } + + void testGetVCard() { + std::auto_ptr<VCardFileStorage> testling(createTestling()); + VCard::ref vcard(new VCard()); + vcard->setFullName("Alice In Wonderland"); + vcard->setEMail("alice@wonderland.lit"); + testling->setVCard(JID("alice@wonderland.lit"), vcard); + + VCard::ref result = testling->getVCard(JID("alice@wonderland.lit")); + CPPUNIT_ASSERT_EQUAL(String("Alice In Wonderland"), result->getFullName()); + CPPUNIT_ASSERT_EQUAL(String("alice@wonderland.lit"), result->getEMail()); + } + + void testGetVCard_FileDoesNotExist() { + std::auto_ptr<VCardFileStorage> testling(createTestling()); + VCard::ref result = testling->getVCard(JID("alice@wonderland.lit")); + CPPUNIT_ASSERT(!result); + } + + private: + VCardFileStorage* createTestling() { + return new VCardFileStorage(vcardsPath); + } + + PlatformApplicationPathProvider* pathProvider; + boost::filesystem::path vcardsPath; +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(VCardFileStorageTest); diff --git a/Swiften/SConscript b/Swiften/SConscript index 3d4d5f6..f206dd0 100644 --- a/Swiften/SConscript +++ b/Swiften/SConscript @@ -123,6 +123,7 @@ if env["SCONS_STAGE"] == "build" : "EventLoop", "Parser", "JID", + "VCards", "Network", "History", "StreamStack", 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; + }; +} |