From 1cea8b4c47e8b03aea7e452fb49b8dea3233eb36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Wed, 8 Jun 2011 23:47:16 +0200 Subject: Put XEP-0082 datetime parsing into Base. diff --git a/Swiften/Base/DateTime.cpp b/Swiften/Base/DateTime.cpp new file mode 100644 index 0000000..1120938 --- /dev/null +++ b/Swiften/Base/DateTime.cpp @@ -0,0 +1,25 @@ +/* + * 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 + +namespace Swift { + +boost::posix_time::ptime stringToDateTime(const std::string& string) { + static std::locale locale(std::locale::classic(), new boost::posix_time::time_input_facet("%Y-%m-%d %H:%M:%S%F%Q")); + std::istringstream stream(string); + stream.imbue(locale); + boost::posix_time::ptime result(boost::posix_time::not_a_date_time); + stream >> result; + return result; +} + +} diff --git a/Swiften/Base/DateTime.h b/Swiften/Base/DateTime.h new file mode 100644 index 0000000..309ee56 --- /dev/null +++ b/Swiften/Base/DateTime.h @@ -0,0 +1,17 @@ +/* + * 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 + +namespace Swift { + /** + * Converts a date formatted according to XEP-0082 into a time + * object. + */ + boost::posix_time::ptime stringToDateTime(const std::string& string); +} diff --git a/Swiften/Base/SConscript b/Swiften/Base/SConscript index 3279c22..8752ea8 100644 --- a/Swiften/Base/SConscript +++ b/Swiften/Base/SConscript @@ -2,6 +2,7 @@ Import("swiften_env") objects = swiften_env.SwiftenObject([ "ByteArray.cpp", + "DateTime.cpp", "SafeByteArray.cpp", "Error.cpp", "Log.cpp", diff --git a/Swiften/Parser/PayloadParsers/DelayParser.cpp b/Swiften/Parser/PayloadParsers/DelayParser.cpp index e2a6bad..e18d09d 100644 --- a/Swiften/Parser/PayloadParsers/DelayParser.cpp +++ b/Swiften/Parser/PayloadParsers/DelayParser.cpp @@ -6,27 +6,16 @@ #include -#include - -#include -#include +#include namespace Swift { -DelayParser::DelayParser(const std::locale& locale) : locale(locale), level_(0) { -} - -boost::posix_time::ptime DelayParser::dateFromString(const std::string& string) { - std::istringstream stream(string); - stream.imbue(locale); - boost::posix_time::ptime result(boost::posix_time::not_a_date_time); - stream >> result; - return result; +DelayParser::DelayParser() : level_(0) { } void DelayParser::handleStartElement(const std::string& /*element*/, const std::string& /*ns*/, const AttributeMap& attributes) { if (level_ == 0) { - boost::posix_time::ptime stamp = dateFromString(attributes.getAttribute("stamp")); + boost::posix_time::ptime stamp = stringToDateTime(attributes.getAttribute("stamp")); getPayloadInternal()->setStamp(stamp); if (!attributes.getAttribute("from").empty()) { std::string from = attributes.getAttribute("from"); diff --git a/Swiften/Parser/PayloadParsers/DelayParser.h b/Swiften/Parser/PayloadParsers/DelayParser.h index 02103d9..144220a 100644 --- a/Swiften/Parser/PayloadParsers/DelayParser.h +++ b/Swiften/Parser/PayloadParsers/DelayParser.h @@ -12,17 +12,13 @@ namespace Swift { class DelayParser : public GenericPayloadParser { public: - DelayParser(const std::locale& locale); + DelayParser(); virtual void handleStartElement(const std::string& element, const std::string&, const AttributeMap& attributes); virtual void handleEndElement(const std::string& element, const std::string&); virtual void handleCharacterData(const std::string& data); private: - boost::posix_time::ptime dateFromString(const std::string& string); - - private: - std::locale locale; int level_; }; } diff --git a/Swiften/Parser/PayloadParsers/DelayParserFactory.cpp b/Swiften/Parser/PayloadParsers/DelayParserFactory.cpp deleted file mode 100644 index 48841d2..0000000 --- a/Swiften/Parser/PayloadParsers/DelayParserFactory.cpp +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#include - -#include -#include - -namespace Swift { - -DelayParserFactory::DelayParserFactory() { - boost::posix_time::time_input_facet* facet = new boost::posix_time::time_input_facet("%Y-%m-%d %H:%M:%S%F%Q"); - locale = std::locale(std::locale::classic(), facet); -} - -} diff --git a/Swiften/Parser/PayloadParsers/DelayParserFactory.h b/Swiften/Parser/PayloadParsers/DelayParserFactory.h deleted file mode 100644 index 7dde35e..0000000 --- a/Swiften/Parser/PayloadParsers/DelayParserFactory.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#pragma once - -#include -#include - -namespace Swift { - class PayloadParserFactoryCollection; - - class DelayParserFactory : public PayloadParserFactory { - public: - DelayParserFactory(); - - virtual bool canParse(const std::string& /*element*/, const std::string& ns, const AttributeMap&) const { - return ns == "urn:xmpp:delay"; - } - - virtual PayloadParser* createPayloadParser() { - return new DelayParser(locale); - } - - private: - std::locale locale; - }; -} diff --git a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp index 88a7bb6..4b143fd 100644 --- a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp +++ b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp @@ -38,7 +38,7 @@ #include #include #include -#include +#include #include #include #include @@ -58,6 +58,7 @@ FullPayloadParserFactoryCollection::FullPayloadParserFactoryCollection() { factories_.push_back(shared_ptr(new GenericPayloadParserFactory("subject"))); factories_.push_back(shared_ptr(new GenericPayloadParserFactory("priority"))); factories_.push_back(shared_ptr(new ErrorParserFactory(this))); + factories_.push_back(shared_ptr(new GenericPayloadParserFactory("delay", "urn:xmpp:delay"))); factories_.push_back(shared_ptr(new GenericPayloadParserFactory("query", "jabber:iq:version"))); factories_.push_back(shared_ptr(new GenericPayloadParserFactory("storage", "storage:bookmarks"))); factories_.push_back(shared_ptr(new GenericPayloadParserFactory("x", "http://jabber.org/protocol/rosterx"))); @@ -79,7 +80,6 @@ FullPayloadParserFactoryCollection::FullPayloadParserFactoryCollection() { factories_.push_back(shared_ptr(new VCardParserFactory())); factories_.push_back(shared_ptr(new PrivateStorageParserFactory(this))); factories_.push_back(shared_ptr(new ChatStateParserFactory())); - factories_.push_back(shared_ptr(new DelayParserFactory())); factories_.push_back(shared_ptr(new MUCUserPayloadParserFactory())); factories_.push_back(shared_ptr(new NicknameParserFactory())); foreach(shared_ptr factory, factories_) { diff --git a/Swiften/Parser/SConscript b/Swiften/Parser/SConscript index daa21be..17505f1 100644 --- a/Swiften/Parser/SConscript +++ b/Swiften/Parser/SConscript @@ -26,7 +26,6 @@ sources = [ "PayloadParsers/CapsInfoParser.cpp", "PayloadParsers/DiscoInfoParser.cpp", "PayloadParsers/DiscoItemsParser.cpp", - "PayloadParsers/DelayParserFactory.cpp", "PayloadParsers/ErrorParser.cpp", "PayloadParsers/FormParser.cpp", "PayloadParsers/IBBParser.cpp", -- cgit v0.10.2-6-g49f6