Make status/show/priority of XMPPPresence properties

This commit is contained in:
Florian Zeitz 2013-01-09 00:36:21 +01:00
parent b976d0280c
commit 178627a652
3 changed files with 115 additions and 23 deletions

View file

@ -27,6 +27,19 @@
* \brief A class describing a presence stanza. * \brief A class describing a presence stanza.
*/ */
@interface XMPPPresence: XMPPStanza @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. * \brief Creates a new autoreleased XMPPPresence.
* *
@ -89,23 +102,44 @@
ID: (OFString*)ID; 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 * \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 * \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 @end

View file

@ -77,6 +77,15 @@
ID: ID_]; ID: ID_];
} }
- (void)dealloc
{
[status release];
[show release];
[priority release];
[super dealloc];
}
- (OFString*)type - (OFString*)type
{ {
if (type == nil) if (type == nil)
@ -85,25 +94,74 @@
return [[type copy] autorelease]; return [[type copy] autorelease];
} }
- (void)addShow: (OFString*)show - (void)setShow: (OFString*)show_
{ {
[self addChild: [OFXMLElement elementWithName: @"show" OFXMLElement *oldShow = [self elementForName: @"show"
namespace: XMPP_NS_CLIENT namespace: XMPP_NS_CLIENT];
stringValue: show]];
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" return [[show copy] autorelease];
namespace: XMPP_NS_CLIENT
stringValue: status]];
} }
- (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" [self addChild: [OFXMLElement elementWithName: @"priority"
namespace: XMPP_NS_CLIENT namespace: XMPP_NS_CLIENT
stringValue: prio]]; stringValue: priority_s]];
OF_SETTER(priority, priority_, YES, 1);
}
- (OFString*)priority
{
return [[priority copy] autorelease];
} }
@end @end

View file

@ -53,9 +53,9 @@ OF_APPLICATION_DELEGATE(AppDelegate)
OFArray *arguments = [OFApplication arguments]; OFArray *arguments = [OFApplication arguments];
XMPPPresence *pres = [XMPPPresence presence]; XMPPPresence *pres = [XMPPPresence presence];
[pres addShow: @"chat"]; [pres setShow: @"chat"];
[pres addStatus: @"Bored"]; [pres setStatus: @"Bored"];
[pres addPriority: 20]; [pres setPriority: [OFNumber numberWithInt8: 20]];
[pres setTo: [XMPPJID JIDWithString: @"alice@example.com"]]; [pres setTo: [XMPPJID JIDWithString: @"alice@example.com"]];
[pres setFrom: [XMPPJID JIDWithString: @"bob@example.org"]]; [pres setFrom: [XMPPJID JIDWithString: @"bob@example.org"]];
assert([[pres XMLString] isEqual: @"<presence to='alice@example.com' " assert([[pres XMLString] isEqual: @"<presence to='alice@example.com' "
@ -154,8 +154,8 @@ OF_APPLICATION_DELEGATE(AppDelegate)
of_log(@"Got roster: %@", [roster_ rosterItems]); of_log(@"Got roster: %@", [roster_ rosterItems]);
pres = [XMPPPresence presence]; pres = [XMPPPresence presence];
[pres addPriority: 10]; [pres setPriority: [OFNumber numberWithInt8: 10]];
[pres addStatus: @"ObjXMPP test is working!"]; [pres setStatus: @"ObjXMPP test is working!"];
[conn sendStanza: pres]; [conn sendStanza: pres];