ObjXMPP  Check-in [c06d9a53ff]

Overview
Comment:Make XMPPPresence comparable
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: c06d9a53ffad866f74f059aedfc0d6b9d22c193becb044d55bbd488f0d0badc9
User & Date: florob@babelmonkeys.de on 2013-01-14 20:18:09
Other Links: manifest | tags
Context
2013-01-18
23:38
Fix Makefile check-in: 0bee074775 user: florob@babelmonkeys.de tags: trunk
2013-01-14
20:18
Make XMPPPresence comparable check-in: c06d9a53ff user: florob@babelmonkeys.de tags: trunk
2013-01-12
22:47
Adjust to recent ObjFW changes. check-in: 9d0f61744d user: florob@babelmonkeys.de tags: trunk
Changes

Modified src/XMPPPresence.h from [308cf467b3] to [cd5efafbb3].

22
23
24
25
26
27
28
29

30
31
32
33
34
35
36
22
23
24
25
26
27
28

29
30
31
32
33
34
35
36







-
+







 */

#import "XMPPStanza.h"

/**
 * \brief A class describing a presence stanza.
 */
@interface XMPPPresence: XMPPStanza
@interface XMPPPresence: XMPPStanza <OFComparing>
{
/// \cond internal
	OFString *status;
	OFString *show;
	OFNumber *priority;
/// \endcond
}

Modified src/XMPPPresence.m from [b2990bd4cd] to [c812c30fb1].

20
21
22
23
24
25
26


27
28
29












30
31
32
33
34
35
36
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50







+
+



+
+
+
+
+
+
+
+
+
+
+
+







 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <assert.h>

#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
{
	return [[[self alloc] init] autorelease];
}

160
161
162
163
164
165
166































167
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

	OF_SETTER(priority, priority_, YES, 1);
}

- (OFString*)priority
{
	return [[priority copy] autorelease];
}

- (of_comparison_result_t)compare: (id <OFComparing>)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

Modified tests/test.m from [f57acb5efb] to [80ad4b43b9].

49
50
51
52
53
54
55
56

57
58
59
60
61
62

63
64
65









66
67
68
69
70
71
72
49
50
51
52
53
54
55

56
57
58
59
60
61

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81







-
+





-
+



+
+
+
+
+
+
+
+
+








@implementation AppDelegate
- (void)applicationDidFinishLaunching
{
	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: @"<presence to='alice@example.com' "
	    @"from='bob@example.org'><show>chat</show>"
	    @"from='bob@example.org'><show>xa</show>"
	    @"<status>Bored</status><priority>20</priority>"
	    @"</presence>"]);

	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"]];
	[msg setFrom: [XMPPJID JIDWithString: @"alice@example.com"]];
	assert([[msg XMLString] isEqual: @"<message type='chat' "
	    @"to='jdev@conference.jabber.org' "
	    @"from='alice@example.com'><body>Hello everyone</body>"