From e951bacb3b683479af53990374644871d2fb93fe Mon Sep 17 00:00:00 2001 From: Florian Zeitz Date: Mon, 14 Jan 2013 21:11:29 +0100 Subject: [PATCH] Make XMPPPresence comparable --- src/XMPPPresence.h | 2 +- src/XMPPPresence.m | 45 +++++++++++++++++++++++++++++++++++++++++++++ tests/test.m | 13 +++++++++++-- 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/XMPPPresence.h b/src/XMPPPresence.h index 3e7cd84..b7c3627 100644 --- a/src/XMPPPresence.h +++ b/src/XMPPPresence.h @@ -26,7 +26,7 @@ /** * \brief A class describing a presence stanza. */ -@interface XMPPPresence: XMPPStanza +@interface XMPPPresence: XMPPStanza { /// \cond internal OFString *status; diff --git a/src/XMPPPresence.m b/src/XMPPPresence.m index 354ad08..7c5850d 100644 --- a/src/XMPPPresence.m +++ b/src/XMPPPresence.m @@ -25,9 +25,23 @@ # include "config.h" #endif +#include + #import "XMPPPresence.h" #import "namespaces.h" +// This provides us with sortable values for show values +static int show_to_int(OFString *show) +{ + if ([show isEqual: @"chat"]) return 0; + if (show == nil) return 1; // available + if ([show isEqual: @"away"]) return 2; + if ([show isEqual: @"dnd"]) return 3; + if ([show isEqual: @"xa"]) return 4; + + assert(0); +} + @implementation XMPPPresence + presence { @@ -164,4 +178,35 @@ { return [[priority copy] autorelease]; } + +- (of_comparison_result_t)compare: (id )object +{ + XMPPPresence *otherPresence; + OFString *otherShow; + of_comparison_result_t priorityOrder; + + if (object == self) + return OF_ORDERED_SAME; + + if (![object isKindOfClass: [XMPPPresence class]]) + @throw [OFInvalidArgumentException + exceptionWithClass: [self class] + selector: _cmd]; + + otherPresence = (XMPPPresence*)object; + + priorityOrder = [priority compare: [otherPresence priority]]; + + if (priorityOrder != OF_ORDERED_SAME) + return priorityOrder; + + otherShow = [otherPresence show]; + if ([show isEqual: otherShow]) + return OF_ORDERED_SAME; + + if (show_to_int(show) < show_to_int(otherShow)) + return OF_ORDERED_ASCENDING; + + return OF_ORDERED_DESCENDING; +} @end diff --git a/tests/test.m b/tests/test.m index 453ee2c..e38f2e3 100644 --- a/tests/test.m +++ b/tests/test.m @@ -53,16 +53,25 @@ OF_APPLICATION_DELEGATE(AppDelegate) OFArray *arguments = [OFApplication arguments]; XMPPPresence *pres = [XMPPPresence presence]; - [pres setShow: @"chat"]; + [pres setShow: @"xa"]; [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: @"chat" + @"from='bob@example.org'>xa" @"Bored20" @""]); + XMPPPresence *pres2 = [XMPPPresence presence]; + [pres2 setShow: @"away"]; + [pres2 setStatus: @"Bored"]; + [pres2 setPriority: [OFNumber numberWithInt8: 23]]; + [pres2 setTo: [XMPPJID JIDWithString: @"alice@example.com"]]; + [pres2 setFrom: [XMPPJID JIDWithString: @"bob@example.org"]]; + + assert([pres compare: pres2] == OF_ORDERED_ASCENDING); + XMPPMessage *msg = [XMPPMessage messageWithType: @"chat"]; [msg setBody: @"Hello everyone"]; [msg setTo: [XMPPJID JIDWithString: @"jdev@conference.jabber.org"]];