From 6ddf416e50843f5a85f1df01421652be2513d619 Mon Sep 17 00:00:00 2001 From: Jonathan Schleifer Date: Fri, 3 Feb 2012 17:26:34 +0100 Subject: [PATCH] Add xml:lang support. --- src/XMPPConnection.h | 5 +++++ src/XMPPConnection.m | 23 +++++++++++++++++++++-- src/XMPPStanza.h | 4 ++++ src/XMPPStanza.m | 25 +++++++++++++++++++++++-- 4 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/XMPPConnection.h b/src/XMPPConnection.h index 391fa0d..a9aad2b 100644 --- a/src/XMPPConnection.h +++ b/src/XMPPConnection.h @@ -146,6 +146,7 @@ OFString *domain, *domainToASCII; XMPPJID *JID; uint16_t port; + OFString *language; XMPPMulticastDelegate *delegates; OFMutableDictionary *callbacks; XMPPAuthenticator *authModule; @@ -170,6 +171,8 @@ @property (copy) OFString *domain; /// \brief The resource to request for the connection @property (copy) OFString *resource; +/// \brief The language to request for the connection +@property (copy) OFString *language; /// \brief A private key file to use for authentication @property (copy) OFString *privateKeyFile; /// \brief A certificate file to use for authentication @@ -321,6 +324,8 @@ - (XMPPJID*)JID; - (void)setPort: (uint16_t)port; - (uint16_t)port; +- (void)setLanguage: (OFString*)language; +- (OFString*)language; /// \cond internal - (void)XMPP_startStream; diff --git a/src/XMPPConnection.m b/src/XMPPConnection.m index 7e08ef5..606db9b 100644 --- a/src/XMPPConnection.m +++ b/src/XMPPConnection.m @@ -52,6 +52,8 @@ #import "XMPPXMLElementBuilder.h" #import "namespaces.h" +#import + @implementation XMPPConnection + connection { @@ -528,6 +530,8 @@ - (void)XMPP_startStream { + OFString *langString = @""; + /* Make sure we don't get any old events */ [parser setDelegate: nil]; [elementBuilder setDelegate: nil]; @@ -545,11 +549,16 @@ elementBuilder = [[XMPPXMLElementBuilder alloc] init]; [elementBuilder setDelegate: self]; + if (language != nil) + langString = [OFString stringWithFormat: @"xml:lang='%@' ", + language]; + [sock writeFormat: @"\n" @"", domain]; + @"xmlns:stream='" XMPP_NS_STREAM @"' %@" + @"version='1.0'>", domain, langString]; + streamOpen = YES; } @@ -1042,6 +1051,16 @@ return port; } +- (void)setLanguage: (OFString*)language_ +{ + OF_SETTER(language, language_, YES, YES) +} + +- (OFString*)language +{ + OF_GETTER(language, YES) +} + - (void)addDelegate: (id )delegate { [delegates addDelegate: delegate]; diff --git a/src/XMPPStanza.h b/src/XMPPStanza.h index 4a80918..9bf3f97 100644 --- a/src/XMPPStanza.h +++ b/src/XMPPStanza.h @@ -35,6 +35,7 @@ XMPPJID *to; OFString *type; OFString *ID; + OFString *language; /// \endcond } @@ -47,6 +48,7 @@ @property (copy) OFString *type; /// \brief The value of the stanza's id attribute @property (copy) OFString *ID; +/// \brief The stanza's xml:lang #endif /** @@ -158,4 +160,6 @@ - (OFString*)type; - (void)setID: (OFString*)ID; - (OFString*)ID; +- (void)setLanguage: (OFString*)language; +- (OFString*)language; @end diff --git a/src/XMPPStanza.m b/src/XMPPStanza.m index 4287000..2486cdc 100644 --- a/src/XMPPStanza.m +++ b/src/XMPPStanza.m @@ -211,7 +211,7 @@ - (void)setID: (OFString*)ID_ { - OFString* old = ID; + OFString *old = ID; ID = [ID_ copy]; [old release]; @@ -219,11 +219,32 @@ if (ID_ != nil) [self addAttributeWithName: @"id" - stringValue: ID]; + stringValue: ID_]; } - (OFString*)ID { return [[ID copy] autorelease]; } + +- (void)setLanguage: (OFString*)language_ +{ + OFString *old = language; + language = [language_ copy]; + [old release]; + + [self removeAttributeForName: @"lang" + namespace: @"http://www.w3.org/XML/1998/namespace"]; + + if (language_ != nil) + [self addAttributeWithName: @"lang" + namespace: @"http://www.w3.org/XML/1998/" + @"namespace" + stringValue: language_]; +} + +- (OFString*)language +{ + return [[language copy] autorelease]; +} @end