From e3d2137622cea23298f203801bc698eff08e0ea1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Sat, 9 Apr 2011 09:54:44 +0200 Subject: Factor out payload persisting into utility classes. 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 -#include -#include - -#include "Swiften/Base/ByteArray.h" +#include #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 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(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 +#include + +namespace Swift { + template + class GenericPayloadPersister : public PayloadPersister { + public: + GenericPayloadPersister() { + } + + public: + boost::shared_ptr loadPayloadGeneric(const boost::filesystem::path& path) { + return boost::dynamic_pointer_cast(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 + +#include +#include + +#include +#include +#include +#include +#include + +using namespace Swift; + +PayloadPersister::PayloadPersister() { +} + +PayloadPersister::~PayloadPersister() { +} + +void PayloadPersister::savePayload(boost::shared_ptr 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 PayloadPersister::loadPayload(const boost::filesystem::path& path) { + if (boost::filesystem::exists(path)) { + ByteArray data; + data.readFromFile(path.string()); + std::auto_ptr parser(createParser()); + PayloadParserTester tester(parser.get()); + tester.parse(data.toString()); + return parser->getPayload(); + } + else { + return boost::shared_ptr(); + } +} 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 +#include + +namespace Swift { + class Payload; + class PayloadSerializer; + class PayloadParser; + + class PayloadPersister { + public: + PayloadPersister(); + virtual ~PayloadPersister(); + + void savePayload(boost::shared_ptr, const boost::filesystem::path&); + boost::shared_ptr 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 -#include +#include +#include +#include -namespace Swift { +using namespace Swift; + +typedef GenericPayloadPersister RosterPersister; RosterFileStorage::RosterFileStorage(const boost::filesystem::path& path) : path(path) { } -// FIXME -void RosterFileStorage::setRoster(boost::shared_ptr r) { - roster.reset(); - if (r) { - roster = boost::make_shared(*r); - } +boost::shared_ptr RosterFileStorage::getRoster() const { + return RosterPersister().loadPayloadGeneric(path); } +void RosterFileStorage::setRoster(boost::shared_ptr 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 getRoster() const { - return roster; - } - + virtual boost::shared_ptr getRoster() const; virtual void setRoster(boost::shared_ptr); private: boost::filesystem::path path; - boost::shared_ptr 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 #include +#include #include #include #include #include #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 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 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(parser.getPayload()); - } - else { - return boost::shared_ptr(); - } + 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; } } - - -} -- cgit v0.10.2-6-g49f6