From 178627a65272d2989379eb239eeafc2a45040b42 Mon Sep 17 00:00:00 2001 From: Florian Zeitz Date: Wed, 9 Jan 2013 00:36:21 +0100 Subject: [PATCH] Make status/show/priority of XMPPPresence properties --- src/XMPPPresence.h | 48 ++++++++++++++++++++++++---- src/XMPPPresence.m | 80 +++++++++++++++++++++++++++++++++++++++------- tests/test.m | 10 +++--- 3 files changed, 115 insertions(+), 23 deletions(-) diff --git a/src/XMPPPresence.h b/src/XMPPPresence.h index 54b34e5..3e7cd84 100644 --- a/src/XMPPPresence.h +++ b/src/XMPPPresence.h @@ -27,6 +27,19 @@ * \brief A class describing a presence stanza. */ @interface XMPPPresence: XMPPStanza +{ +/// \cond internal + OFString *status; + OFString *show; + OFNumber *priority; +/// \endcond +} +#ifdef OF_HAVE_PROPERTIES +@property (copy) OFString *status; +@property (copy) OFString *show; +@property (copy) OFNumber *priority; +#endif + /** * \brief Creates a new autoreleased XMPPPresence. * @@ -89,23 +102,44 @@ ID: (OFString*)ID; /** - * \brief Adds a show element to the presence stanza. + * \brief Sets/Adds the show element of the presence stanza. * * \param show The text content of the show element */ -- (void)addShow: (OFString*)show; +- (void)setShow: (OFString*)show; /** - * \brief Adds a status element to the presence stanza. + * \brief Returns the text content of the show element of the presence stanza. + * + * \return The text content of the show element of the presence stanza. + */ +- (OFString*)show; + +/** + * \brief Sets/Adds the status element of the presence stanza. * * \param status The text content of the status element */ -- (void)addStatus: (OFString*)status; +- (void)setStatus: (OFString*)status; /** - * \brief Adds a priority element to the presence stanza. + * \brief Returns the text content of the status element of the presence stanza. * - * \param priority The text content of the priority element + * \return The text content of the status element of the presence stanza. */ -- (void)addPriority: (int8_t)priority; +- (OFString*)status; + +/** + * \brief Sets/Adds the priority element of the presence stanza. + * + * \param priority The numeric content of the priority element + */ +- (void)setPriority: (OFNumber*)priority; + +/** + * \brief Returns the numeric content of the priority element of the presence stanza. + * + * \return The numeric content of the priority element of the presence stanza. + */ +- (OFNumber*)priority; @end diff --git a/src/XMPPPresence.m b/src/XMPPPresence.m index 684edb0..354ad08 100644 --- a/src/XMPPPresence.m +++ b/src/XMPPPresence.m @@ -77,6 +77,15 @@ ID: ID_]; } +- (void)dealloc +{ + [status release]; + [show release]; + [priority release]; + + [super dealloc]; +} + - (OFString*)type { if (type == nil) @@ -85,25 +94,74 @@ return [[type copy] autorelease]; } -- (void)addShow: (OFString*)show +- (void)setShow: (OFString*)show_ { - [self addChild: [OFXMLElement elementWithName: @"show" - namespace: XMPP_NS_CLIENT - stringValue: show]]; + OFXMLElement *oldShow = [self elementForName: @"show" + namespace: XMPP_NS_CLIENT]; + + if (oldShow != nil) + [self removeChild: oldShow]; + + if (show_ != nil) + [self addChild: [OFXMLElement elementWithName: @"show" + namespace: XMPP_NS_CLIENT + stringValue: show_]]; + + OF_SETTER(show, show_, YES, 1); } -- (void)addStatus: (OFString*)status +- (OFString*)show { - [self addChild: [OFXMLElement elementWithName: @"status" - namespace: XMPP_NS_CLIENT - stringValue: status]]; + return [[show copy] autorelease]; } -- (void)addPriority: (int8_t)priority +- (void)setStatus: (OFString*)status_ { - OFString* prio = [OFString stringWithFormat: @"%" @PRId8, priority]; + OFXMLElement *oldStatus = [self elementForName: @"status" + namespace: XMPP_NS_CLIENT]; + + if (oldStatus != nil) + [self removeChild: oldStatus]; + + if (status_ != nil) + [self addChild: [OFXMLElement elementWithName: @"status" + namespace: XMPP_NS_CLIENT + stringValue: status_]]; + + OF_SETTER(status, status_, YES, 1); +} + +- (OFString*)status +{ + return [[status copy] autorelease]; +} + +- (void)setPriority: (OFNumber*)priority_ +{ + intmax_t prio = [priority_ intMaxValue]; + + if ((prio < -128) || (prio > 127)) + @throw [OFInvalidArgumentException + exceptionWithClass: [self class] + selector: _cmd]; + + OFXMLElement *oldPriority = [self elementForName: @"priority" + namespace: XMPP_NS_CLIENT]; + + if (oldPriority != nil) + [self removeChild: oldPriority]; + + OFString* priority_s = + [OFString stringWithFormat: @"%" @PRId8, [priority_ int8Value]]; [self addChild: [OFXMLElement elementWithName: @"priority" namespace: XMPP_NS_CLIENT - stringValue: prio]]; + stringValue: priority_s]]; + + OF_SETTER(priority, priority_, YES, 1); +} + +- (OFString*)priority +{ + return [[priority copy] autorelease]; } @end diff --git a/tests/test.m b/tests/test.m index 70f7d45..453ee2c 100644 --- a/tests/test.m +++ b/tests/test.m @@ -53,9 +53,9 @@ OF_APPLICATION_DELEGATE(AppDelegate) OFArray *arguments = [OFApplication arguments]; XMPPPresence *pres = [XMPPPresence presence]; - [pres addShow: @"chat"]; - [pres addStatus: @"Bored"]; - [pres addPriority: 20]; + [pres setShow: @"chat"]; + [pres setStatus: @"Bored"]; + [pres setPriority: [OFNumber numberWithInt8: 20]]; [pres setTo: [XMPPJID JIDWithString: @"alice@example.com"]]; [pres setFrom: [XMPPJID JIDWithString: @"bob@example.org"]]; assert([[pres XMLString] isEqual: @"