diff options
author | Remko Tronçon <git@el-tramo.be> | 2011-04-09 07:54:44 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2011-04-18 19:11:41 (GMT) |
commit | e3d2137622cea23298f203801bc698eff08e0ea1 (patch) | |
tree | e8d02f3334a6422e25ca5f30606850d960b27cc7 | |
parent | 01385d84f07466550fff702079555c65963463a8 (diff) | |
download | swift-contrib-e3d2137622cea23298f203801bc698eff08e0ea1.zip swift-contrib-e3d2137622cea23298f203801bc698eff08e0ea1.tar.bz2 |
Factor out payload persisting into utility classes.
-rw-r--r-- | Swiften/Disco/CapsFileStorage.cpp | 40 | ||||
-rw-r--r-- | Swiften/Entity/GenericPayloadPersister.h | 36 | ||||
-rw-r--r-- | Swiften/Entity/PayloadPersister.cpp | 52 | ||||
-rw-r--r-- | Swiften/Entity/PayloadPersister.h | 30 | ||||
-rw-r--r-- | Swiften/Roster/RosterFileStorage.cpp | 18 | ||||
-rw-r--r-- | Swiften/Roster/RosterFileStorage.h | 7 | ||||
-rw-r--r-- | Swiften/SConscript | 1 | ||||
-rw-r--r-- | Swiften/VCards/VCardFileStorage.cpp | 36 |
8 files changed, 142 insertions, 78 deletions
diff --git a/Swiften/Disco/CapsFileStorage.cpp b/Swiften/Disco/CapsFileStorage.cpp index 17fe37b..81e9551 100644 --- a/Swiften/Disco/CapsFileStorage.cpp +++ b/Swiften/Disco/CapsFileStorage.cpp @@ -6,57 +6,29 @@ #include "Swiften/Disco/CapsFileStorage.h" -#include <iostream> -#include <boost/filesystem/fstream.hpp> -#include <boost/filesystem.hpp> - -#include "Swiften/Base/ByteArray.h" +#include <Swiften/Entity/GenericPayloadPersister.h> #include "Swiften/Serializer/PayloadSerializers/DiscoInfoSerializer.h" -#include "Swiften/Parser/PayloadParsers/UnitTest/PayloadParserTester.h" #include "Swiften/Parser/PayloadParsers/DiscoInfoParser.h" #include "Swiften/StringCodecs/Hexify.h" #include "Swiften/StringCodecs/Base64.h" -namespace Swift { +using namespace Swift; + +typedef GenericPayloadPersister<DiscoInfo, DiscoInfoParser, DiscoInfoSerializer> DiscoInfoPersister; CapsFileStorage::CapsFileStorage(const boost::filesystem::path& path) : path(path) { } DiscoInfo::ref CapsFileStorage::getDiscoInfo(const std::string& hash) const { - boost::filesystem::path capsPath(getCapsPath(hash)); - if (boost::filesystem::exists(capsPath)) { - ByteArray data; - data.readFromFile(capsPath.string()); - - DiscoInfoParser parser; - PayloadParserTester tester(&parser); - tester.parse(data.toString()); - return boost::dynamic_pointer_cast<DiscoInfo>(parser.getPayload()); - } - else { - return DiscoInfo::ref(); - } + return DiscoInfoPersister().loadPayloadGeneric(getCapsPath(hash)); } void CapsFileStorage::setDiscoInfo(const std::string& hash, DiscoInfo::ref discoInfo) { - boost::filesystem::path capsPath(getCapsPath(hash)); - if (!boost::filesystem::exists(capsPath.parent_path())) { - try { - boost::filesystem::create_directories(capsPath.parent_path()); - } - catch (const boost::filesystem::filesystem_error& e) { - std::cerr << "ERROR: " << e.what() << std::endl; - } - } DiscoInfo::ref bareDiscoInfo(new DiscoInfo(*discoInfo.get())); bareDiscoInfo->setNode(""); - boost::filesystem::ofstream file(capsPath); - file << DiscoInfoSerializer().serializePayload(bareDiscoInfo); - file.close(); + DiscoInfoPersister().savePayload(bareDiscoInfo, getCapsPath(hash)); } boost::filesystem::path CapsFileStorage::getCapsPath(const std::string& hash) const { return path / (Hexify::hexify(Base64::decode(hash)) + ".xml"); } - -} diff --git a/Swiften/Entity/GenericPayloadPersister.h b/Swiften/Entity/GenericPayloadPersister.h new file mode 100644 index 0000000..63553de --- /dev/null +++ b/Swiften/Entity/GenericPayloadPersister.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2011 Remko Tronçon + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#pragma once + +#include <Swiften/Entity/PayloadPersister.h> +#include <Swiften/Parser/GenericPayloadParserFactory.h> + +namespace Swift { + template<typename PAYLOAD, typename PARSER, typename SERIALIZER> + class GenericPayloadPersister : public PayloadPersister { + public: + GenericPayloadPersister() { + } + + public: + boost::shared_ptr<PAYLOAD> loadPayloadGeneric(const boost::filesystem::path& path) { + return boost::dynamic_pointer_cast<PAYLOAD>(loadPayload(path)); + } + + protected: + virtual const PayloadSerializer* getSerializer() const { + return &serializer; + } + + virtual PayloadParser* createParser() const { + return new PARSER(); + } + + private: + SERIALIZER serializer; + }; +} diff --git a/Swiften/Entity/PayloadPersister.cpp b/Swiften/Entity/PayloadPersister.cpp new file mode 100644 index 0000000..2899edc --- /dev/null +++ b/Swiften/Entity/PayloadPersister.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2011 Remko Tronçon + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#include <Swiften/Entity/PayloadPersister.h> + +#include <boost/filesystem/fstream.hpp> +#include <boost/filesystem.hpp> + +#include <Swiften/Parser/PayloadParsers/UnitTest/PayloadParserTester.h> +#include <Swiften/Base/ByteArray.h> +#include <Swiften/Parser/PayloadParser.h> +#include <Swiften/Parser/PayloadParserFactory.h> +#include <Swiften/Serializer/PayloadSerializer.h> + +using namespace Swift; + +PayloadPersister::PayloadPersister() { +} + +PayloadPersister::~PayloadPersister() { +} + +void PayloadPersister::savePayload(boost::shared_ptr<Payload> payload, const boost::filesystem::path& path) { + if (!boost::filesystem::exists(path.parent_path())) { + try { + boost::filesystem::create_directories(path.parent_path()); + } + catch (const boost::filesystem::filesystem_error& e) { + std::cerr << "ERROR: " << e.what() << std::endl; + } + } + boost::filesystem::ofstream file(path); + file << getSerializer()->serialize(payload); + file.close(); +} + +boost::shared_ptr<Payload> PayloadPersister::loadPayload(const boost::filesystem::path& path) { + if (boost::filesystem::exists(path)) { + ByteArray data; + data.readFromFile(path.string()); + std::auto_ptr<PayloadParser> parser(createParser()); + PayloadParserTester tester(parser.get()); + tester.parse(data.toString()); + return parser->getPayload(); + } + else { + return boost::shared_ptr<Payload>(); + } +} diff --git a/Swiften/Entity/PayloadPersister.h b/Swiften/Entity/PayloadPersister.h new file mode 100644 index 0000000..ea7c74c --- /dev/null +++ b/Swiften/Entity/PayloadPersister.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2011 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> + +namespace Swift { + class Payload; + class PayloadSerializer; + class PayloadParser; + + class PayloadPersister { + public: + PayloadPersister(); + virtual ~PayloadPersister(); + + void savePayload(boost::shared_ptr<Payload>, const boost::filesystem::path&); + boost::shared_ptr<Payload> loadPayload(const boost::filesystem::path&); + + protected: + + virtual const PayloadSerializer* getSerializer() const = 0; + virtual PayloadParser* createParser() const = 0; + }; +} diff --git a/Swiften/Roster/RosterFileStorage.cpp b/Swiften/Roster/RosterFileStorage.cpp index de7b442..31b6c5e 100644 --- a/Swiften/Roster/RosterFileStorage.cpp +++ b/Swiften/Roster/RosterFileStorage.cpp @@ -6,19 +6,21 @@ #include <Swiften/Roster/RosterFileStorage.h> -#include <boost/smart_ptr/make_shared.hpp> +#include <Swiften/Entity/GenericPayloadPersister.h> +#include <Swiften/Serializer/PayloadSerializers/RosterSerializer.h> +#include <Swiften/Parser/PayloadParsers/RosterParser.h> -namespace Swift { +using namespace Swift; + +typedef GenericPayloadPersister<RosterPayload, RosterParser, RosterSerializer> RosterPersister; RosterFileStorage::RosterFileStorage(const boost::filesystem::path& path) : path(path) { } -// FIXME -void RosterFileStorage::setRoster(boost::shared_ptr<RosterPayload> r) { - roster.reset(); - if (r) { - roster = boost::make_shared<RosterPayload>(*r); - } +boost::shared_ptr<RosterPayload> RosterFileStorage::getRoster() const { + return RosterPersister().loadPayloadGeneric(path); } +void RosterFileStorage::setRoster(boost::shared_ptr<RosterPayload> roster) { + RosterPersister().savePayload(roster, path); } diff --git a/Swiften/Roster/RosterFileStorage.h b/Swiften/Roster/RosterFileStorage.h index dc91c27..cb00969 100644 --- a/Swiften/Roster/RosterFileStorage.h +++ b/Swiften/Roster/RosterFileStorage.h @@ -15,15 +15,10 @@ namespace Swift { public: RosterFileStorage(const boost::filesystem::path& path); - // FIXME - virtual boost::shared_ptr<RosterPayload> getRoster() const { - return roster; - } - + virtual boost::shared_ptr<RosterPayload> getRoster() const; virtual void setRoster(boost::shared_ptr<RosterPayload>); private: boost::filesystem::path path; - boost::shared_ptr<RosterPayload> roster; }; } diff --git a/Swiften/SConscript b/Swiften/SConscript index 7f27ea1..12714ae 100644 --- a/Swiften/SConscript +++ b/Swiften/SConscript @@ -78,6 +78,7 @@ if env["SCONS_STAGE"] == "build" : "Elements/VCard.cpp", "Elements/MUCOccupant.cpp", "Entity/Entity.cpp", + "Entity/PayloadPersister.cpp", "MUC/MUC.cpp", "MUC/MUCManager.cpp", "MUC/MUCRegistry.cpp", diff --git a/Swiften/VCards/VCardFileStorage.cpp b/Swiften/VCards/VCardFileStorage.cpp index ec676e1..1b23635 100644 --- a/Swiften/VCards/VCardFileStorage.cpp +++ b/Swiften/VCards/VCardFileStorage.cpp @@ -10,18 +10,20 @@ #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/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 { +using namespace Swift; + +typedef GenericPayloadPersister<VCard, VCardParser, VCardSerializer> VCardPersister; VCardFileStorage::VCardFileStorage(boost::filesystem::path dir) : vcardsPath(dir) { cacheFile = vcardsPath / "phashes"; @@ -50,34 +52,11 @@ 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(data.toString()); - return boost::dynamic_pointer_cast<VCard>(parser.getPayload()); - } - else { - return boost::shared_ptr<VCard>(); - } + return VCardPersister().loadPayloadGeneric(getVCardPath(jid)); } 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(); + VCardPersister().savePayload(v, getVCardPath(jid)); getAndUpdatePhotoHash(jid, v); } @@ -126,6 +105,3 @@ void VCardFileStorage::savePhotoHashes() const { std::cerr << "Error writing vcards file" << std::endl; } } - - -} |