-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTNStropheContact.j
710 lines (576 loc) · 26.2 KB
/
TNStropheContact.j
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/*
* TNStropheContact.j
*
* Copyright (C) 2010 Antoine Mercadal <antoine.mercadal@inframonde.eu>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import <Foundation/Foundation.j>
@import <AppKit/CPImage.j>
@import "TNStropheConnection.j"
@import "TNStropheGroup.j"
@import "TNStropheJID.j"
@import "TNStropheVCard.j"
TNStropheContactStatusAway = @"away";
TNStropheContactStatusBusy = @"xa";
TNStropheContactStatusDND = @"dnd";
TNStropheContactStatusOffline = @"offline";
TNStropheContactStatusOnline = @"online";
TNStropheContactGroupUpdatedNotification = @"TNStropheContactGroupUpdatedNotification";
TNStropheContactMessageActiveNotification = @"TNStropheContactMessageActiveNotification";
TNStropheContactMessageComposingNotification = @"TNStropheContactMessageComposingNotification";
TNStropheContactMessageGoneNotification = @"TNStropheContactMessageGoneNotification";
TNStropheContactMessageInactiveNotification = @"TNStropheContactMessageInactiveNotification";
TNStropheContactMessagePausedNotification = @"TNStropheContactMessagePausedNotification";
TNStropheContactMessageReceivedNotification = @"TNStropheContactMessageReceivedNotification";
TNStropheContactMessageSentNotification = @"TNStropheContactMessageSentNotification";
TNStropheContactMessageTreatedNotification = @"TNStropheContactMessageTreatedNotification";
TNStropheContactNicknameUpdatedNotification = @"TNStropheContactNicknameUpdatedNotification";
TNStropheContactPresenceUpdatedNotification = @"TNStropheContactPresenceUpdatedNotification";
TNStropheContactStanzaSentNotification = @"TNStropheContactStanzaSentNotification"
TNStropheContactSubscriptionUpdatedNotification = @"TNStropheContactSubscriptionUpdatedNotification";
TNStropheContactVCardReceivedNotification = @"TNStropheContactVCardReceivedNotification";
var TNStropheContactDelegate_avatarForContact_ = 1 << 0;
TNStropheContactImageOffline = nil;
TNStropheContactImageOnline = nil;
TNStropheContactImageBusy = nil;
TNStropheContactImageAway = nil;
TNStropheContactImageDND = nil;
TNStropheContactImageNewMessage = nil;
TNStropheContactImageNewError = nil;
/*! @ingroup strophecappuccino
this is an implementation of a XMPP Contact
*/
@implementation TNStropheContact: CPObject
{
CPArray _groups @accessors(property=groups);
CPArray _messagesQueue @accessors(property=messagesQueue);
CPArray _resources @accessors(property=resources);
CPImage _avatar @accessors(property=avatar);
CPImage _statusIcon @accessors(property=statusIcon);
CPNumber _numberOfEvents @accessors(property=numberOfEvents);
CPString _nickname @accessors(property=nickname);
CPString _nodeName @accessors(property=nodeName);
CPString _subscription @accessors(property=subscription);
CPString _type @accessors(property=type);
CPString _XMPPShow @accessors(property=XMPPShow);
CPString _XMPPStatus @accessors(property=XMPPStatus);
id _delegate @accessors(getter=delegate);
TNStropheConnection _connection @accessors(property=connection);
TNStropheJID _JID @accessors(property=JID);
TNStropheVCard _vCard @accessors(property=vCard);
BOOL _askingVCard;
BOOL _isComposing;
CPImage _statusReminder;
int _implementedDelegateMethods;
}
#pragma mark -
#pragma mark Class methods
/*! create a contact using a given connection, JID and group
@param aConnection TNStropheConnection to use
@param aJID the JID of the contact
@param aGroup the group of the contact
@return an allocated and initialized TNStropheContact
*/
+ (TNStropheContact)contactWithConnection:(TNStropheConnection)aConnection JID:(TNStropheJID)aJID group:(TNStropheGroup)aGroup
{
return [[TNStropheContact alloc] initWithConnection:aConnection JID:aJID group:aGroup];
}
#pragma mark -
#pragma mark Initialization
/*! Initialize the class, by creating the images
*/
+ (void)initialize
{
var bundle = [CPBundle bundleForClass:TNStropheContact];
TNStropheContactImageOffline = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"gray.png"] size:CGSizeMake(8.0, 8.0)];
TNStropheContactImageOnline = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"green.png"] size:CGSizeMake(8.0, 8.0)];
TNStropheContactImageBusy = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"red.png"] size:CGSizeMake(8.0, 8.0)];
TNStropheContactImageAway = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"orange.png"] size:CGSizeMake(8.0, 8.0)];
TNStropheContactImageDND = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"black.png"] size:CGSizeMake(8.0, 8.0)];
TNStropheContactImageNewMessage = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"blue.png"] size:CGSizeMake(8.0, 8.0)];
TNStropheContactImageNewError = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"error.png"] size:CGSizeMake(8.0, 8.0)];
}
/*! init a TNStropheContact with a given connection
@param aConnection TNStropheConnection to use
@return an initialized TNStropheContact
*/
- (id)initWithConnection:(TNStropheConnection)aConnection JID:(TNStropheJID)aJID group:(TNStropheGroup)aGroup
{
if (self = [super init])
{
_type = @"contact";
_connection = aConnection;
_messagesQueue = [CPArray array];
_numberOfEvents = 0;
_isComposing = NO;
_askingVCard = NO;
_resources = [CPArray array];
_groups = [CPArray array];
[self setNickname:[_JID bare]];
[self setJID:aJID];
[self setXMPPStatus:@"Offline"];
[self setXMPPShow:TNStropheContactStatusOffline];
[self setStatusIcon:TNStropheContactImageOffline];
if (aGroup)
[_groups addObject:aGroup];
}
return self;
}
#pragma mark -
#pragma mark Setters and Getters
/*! Set the delegate
You should not use this yourself if you are
The contact is managed by a TNStropheRoster
In that case, the delegate methods will be
forwarded to the roster's delegate
Delegate methods
- (CPImage)avatarForContact:(TNStropheContact)aContact
Allows to return an image for teh contact if none is set in the vCard
*/
- (void)setDelegate:(id)aDelegate
{
if (aDelegate == _delegate)
return;
_delegate = aDelegate;
_implementedDelegateMethods = 0;
if ([_delegate respondsToSelector:@selector(avatarForContact:)])
_implementedDelegateMethods |= TNStropheContactDelegate_avatarForContact_;
}
- (CPString)name
{
return [_vCard fullName] || _nickname;
}
#pragma mark -
#pragma mark Status
/*! Processes presence. It populates the status of the contact
and send notifications
You should never have to use this method
@param aStanza the response TNStropheStanza
*/
- (BOOL)_didReceivePresence:(TNStropheStanza)aStanza
{
var resource = [aStanza fromResource],
presenceStatus = [aStanza firstChildWithName:@"status"];
if (resource && (typeof(resource) != "undefined") && resource != @"")
{
if (![_resources containsObject:resource])
[_resources addObject:resource];
[_JID setResource:resource];
}
switch ([aStanza type])
{
case @"error":
var errorCode = [[aStanza firstChildWithName:@"error"] valueForAttribute:@"code"];
[self setXMPPShow:TNStropheContactStatusOffline];
[self setXMPPStatus:@"Error code: " + errorCode];
[self setStatusIcon:TNStropheContactImageNewError];
_statusReminder = TNStropheContactImageNewError;
return NO;
case @"unavailable":
[_resources removeObject:resource];
CPLogConsole(@"contact become unavailable from resource: " + resource + @". Resources left : " + _resources + "("+ [_resources count] + ")");
if ([_resources count] == 0)
{
[self setXMPPShow:TNStropheContactStatusOffline];
[self setStatusIcon:TNStropheContactImageOffline];
_statusReminder = TNStropheContactImageOffline;
if (presenceStatus)
[self setXMPPStatus:[presenceStatus text]];
else
[self setXMPPStatus:@"Offline"];
}
else
[_JID setResource:[_resources lastObject]];
break;
case @"subscribe":
[self setXMPPStatus:@"Asking subscribtion"];
[self setSubscription:[aStanza type]];
break;
case @"subscribed":
[self setSubscription:[aStanza type]];
break;
case @"unsubscribe":
[self setSubscription:[aStanza type]];
break;
case @"unsubscribed":
[self setSubscription:[aStanza type]];
[self setXMPPStatus:@"Unauthorized"];
break;
default:
[self setXMPPShow:TNStropheContactStatusOnline];
[self setStatusIcon:TNStropheContactImageOnline];
[self setSubscription:@"subscribed"];
_statusReminder = TNStropheContactImageOnline;
if ([aStanza firstChildWithName:@"show"])
{
[self setXMPPShow:[[aStanza firstChildWithName:@"show"] text]];
switch (_XMPPShow)
{
case TNStropheContactStatusBusy:
[self setStatusIcon:TNStropheContactImageBusy];
_statusReminder = TNStropheContactImageBusy;
break;
case TNStropheContactStatusAway:
[self setStatusIcon:TNStropheContactImageAway];
_statusReminder = TNStropheContactImageAway;
break;
case TNStropheContactStatusDND:
[self setStatusIcon:TNStropheContactImageDND];
_statusReminder = TNStropheContactImageDND;
break;
}
}
if (_numberOfEvents > 0)
[self setStatusIcon:TNStropheContactImageNewMessage];
if (presenceStatus)
[self setXMPPStatus:[presenceStatus text]];
else
[self setXMPPStatus:@"Online"];
if ([aStanza firstChildWithName:@"x"]
&& [[aStanza firstChildWithName:@"x"] valueForAttribute:@"xmlns"] == @"vcard-temp:x:update"
&& ![aStanza firstChildWithName:@"delay"])
{
[self getVCard];
}
break;
}
if (!([aStanza firstChildWithName:@"x"] && [[aStanza firstChildWithName:@"x"] valueForAttribute:@"xmlns"] == @"vcard-temp:x:update"))
[[CPNotificationCenter defaultCenter] postNotificationName:TNStropheContactPresenceUpdatedNotification object:self];
return YES;
}
- (void)sendStatus:(CPString)aStatus
{
var statusStanza = [TNStropheStanza messageTo:_JID withAttributes:{"type": "chat"}];
[statusStanza addChildWithName:aStatus andAttributes:{"xmlns": "http://jabber.org/protocol/chatstates"}];
[self sendStanza:statusStanza andRegisterSelector:@selector(_didSendStatus:) ofObject:self];
}
/*! @ignore
*/
- (void)_didSendStatus:(TNStropheStanza)aStanza
{
if ([aStanza type] == @"error")
CPLog.error(@"Unable to send status. Resp is: " + aStanza);
}
/*! this allows to send "composing" information to a user. This will never send "paused".
you have to handle a timer if you want to automatically send pause after a while.
*/
- (void)sendComposing
{
if (_isComposing)
return;
[self sendStatus:@"composing"];
_isComposing = YES;
}
/*! this allows to send "paused" information to a user.
*/
- (void)sendComposePaused
{
[self sendStatus:@"paused"];
_isComposing = NO;
}
#pragma mark -
#pragma mark Subscription
/*! subscribe to the contact
*/
- (void)subscribe
{
[_connection send:[TNStropheStanza presenceTo:_JID withAttributes:{@"type": @"subscribed"} bare:YES]];
}
/*! unsubscribe from the contact
*/
- (void)unsubscribe
{
[_connection send:[TNStropheStanza presenceTo:_JID withAttributes:{@"type": @"unsubscribed"} bare:YES]];
}
/*! ask subscribtion to the contact
*/
- (void)askSubscription
{
[_connection send:[TNStropheStanza presenceTo:_JID withAttributes:{@"type": @"subscribe"} bare:YES]];
}
- (void)setSubscription:(CPString)aSubscription
{
[self willChangeValueForKey:@"subscription"];
_subscription = aSubscription;
[[CPNotificationCenter defaultCenter] postNotificationName:TNStropheContactSubscriptionUpdatedNotification object:self];
[self didChangeValueForKey:@"subscription"];
}
#pragma mark -
#pragma mark MetaData
- (CPString)description
{
return _nickname;
}
/*! probe the contact's vCard
you should never have to use this message if you are using TNStropheRoster
*/
- (void)getVCard
{
var uid = [_connection getUniqueId],
vcardStanza = [TNStropheStanza iqTo:_JID withAttributes:{@"type": @"get", @"id": uid} bare:YES],
params = [CPDictionary dictionaryWithObjectsAndKeys: uid, @"id"];
[vcardStanza addChildWithName:@"vCard" andAttributes:{@"xmlns": @"vcard-temp"}];
_askingVCard = YES;
[_connection registerSelector:@selector(_didReceiveVCard:) ofObject:self withDict:params];
[_connection send:vcardStanza];
}
/*! executed on getVCard result. Will post TNStropheContactVCardReceivedNotification
and send notifications. If vCard contains a PHOTO node, it will set the avatar CPImage
property of the TNStropheContact
You should never have to use this method
@param aStanza the response TNStropheStanza
*/
- (BOOL)_didReceiveVCard:(TNStropheStanza)aStanza
{
var aVCard = [aStanza firstChildWithName:@"vCard"];
_askingVCard = NO;
_avatar = nil;
if (aVCard)
{
var VCARD = [[TNStropheVCard alloc] initWithXMLNode:aVCard];
if (!_nickname || (_nickname == [_JID bare]) || (_nickname == [_JID node]))
[self setNickname:[VCARD fullName] || [_JID node]]
if ([VCARD photo])
[self setAvatar:[VCARD photo]];
else if (_implementedDelegateMethods & TNStropheContactDelegate_avatarForContact_)
[self setAvatar:[_delegate avatarForContact:self]];
[self willChangeValueForKey:@"name"];
[self setVCard:VCARD];
[self didChangeValueForKey:@"name"];
[[CPNotificationCenter defaultCenter] postNotificationName:TNStropheContactVCardReceivedNotification object:self];
}
return NO;
}
#pragma mark -
#pragma mark Communicating
- (void)sendStanza:(TNStropheStanza)aStanza
{
[self sendStanza:aStanza withUserInfo:nil];
}
- (void)sendStanza:(TNStropheStanza)aStanza withUserInfo:(CPDictionary)userInfo
{
[aStanza setTo:_JID];
[_connection send:aStanza];
[[CPNotificationCenter defaultCenter] postNotificationName:TNStropheContactStanzaSentNotification object:self userInfo:userInfo];
}
/*! send a TNStropheStanza to the contact. From, ant To value are rewritten. This message uses a given stanza id
in order to use it if you need. You should mostly use the
You should never have to use the method sendStanza:andRegisterSelector:ofObject: in most of the case
@param aStanza the TNStropheStanza to send to the contact
@param aSelector the selector to perform on response
@param anObject the object receiving the selector
@param anId the specific stanza ID to use
@param someUserInfo random information to give to the selector
@param aDelegate the handler id delegate
@return the associated registration id for the selector
*/
- (id)sendStanza:(TNStropheStanza)aStanza andRegisterSelector:(SEL)aSelector ofObject:(id)anObject withSpecificID:(id)anId userInfo:(id)someUserInfo handlerDelegate:(id)aDelegate
{
var params = [CPDictionary dictionaryWithObjectsAndKeys:anId, @"id"],
userInfo = [CPDictionary dictionaryWithObjectsAndKeys:aStanza, @"stanza", anId, @"id"],
ret;
[aStanza setID:anId];
if (aSelector && someUserInfo && aDelegate)
ret = [_connection registerSelector:aSelector ofObject:anObject withDict:params userInfo:someUserInfo handlerDelegate:aDelegate];
else if (aSelector && !someUserInfo && aDelegate)
ret = [_connection registerSelector:aSelector ofObject:anObject withDict:params handlerDelegate:aDelegate];
else if (aSelector && someUserInfo && !aDelegate)
ret = [_connection registerSelector:aSelector ofObject:anObject withDict:params userInfo:someUserInfo];
else if (aSelector && !someUserInfo && !aDelegate)
ret = [_connection registerSelector:aSelector ofObject:anObject withDict:params];
[self sendStanza:aStanza withUserInfo:userInfo];
return ret;
}
/*! send a TNStropheStanza to the contact. From, ant To value are rewritten.
@param aStanza the TNStropheStanza to send to the contact
@param aSelector the selector to perform on response
@param anObject the object receiving the selector
@return the associated registration id for the selector
*/
- (id)sendStanza:(TNStropheStanza)aStanza andRegisterSelector:(SEL)aSelector ofObject:(id)anObject
{
return [self sendStanza:aStanza andRegisterSelector:aSelector ofObject:anObject withSpecificID:[_connection getUniqueId] userInfo:nil handlerDelegate:nil];
}
/*! send a TNStropheStanza to the contact. From, ant To value are rewritten.
@param aStanza the TNStropheStanza to send to the contact
@param aSelector the selector to perform on response
@param anObject the object receiving the selector
@param anId the specific stanza ID to use
@return the associated registration id for the selector
*/
- (id)sendStanza:(TNStropheStanza)aStanza andRegisterSelector:(SEL)aSelector ofObject:(id)anObject withSpecificID:(int)anId
{
return [self sendStanza:aStanza andRegisterSelector:aSelector ofObject:anObject withSpecificID:anId userInfo:nil handlerDelegate:nil];
}
/*! send a TNStropheStanza to the contact. From, ant To value are rewritten.
@param aStanza the TNStropheStanza to send to the contact
@param aSelector the selector to perform on response
@param anObject the object receiving the selector
@param someUserInfo random information to give to the selector
@return the associated registration id for the selector
*/
- (id)sendStanza:(TNStropheStanza)aStanza andRegisterSelector:(SEL)aSelector ofObject:(id)anObject userInfo:(id)someUserInfo
{
return [self sendStanza:aStanza andRegisterSelector:aSelector ofObject:anObject withSpecificID:[_connection getUniqueId] userInfo:someUserInfo handlerDelegate:nil];
}
/*! send a TNStropheStanza to the contact. From, ant To value are rewritten.
@param aStanza the TNStropheStanza to send to the contact
@param aSelector the selector to perform on response
@param anObject the object receiving the selector
@param someUserInfo random information to give to the selector
@param aDelegate the handler id delegate
@return the associated registration id for the selector
*/
- (id)sendStanza:(TNStropheStanza)aStanza andRegisterSelector:(SEL)aSelector ofObject:(id)anObject handlerDelegate:(id)aDelegate
{
return [self sendStanza:aStanza andRegisterSelector:aSelector ofObject:anObject withSpecificID:[_connection getUniqueId] userInfo:nil handlerDelegate:aDelegate];
}
/*! register the contact to listen incoming messages
you should never have to use this message if you use TNStropheRoster
*/
- (void)getMessages
{
var params = [CPDictionary dictionaryWithObjectsAndKeys:@"message", @"name",
[_JID bare], @"from",
{matchBare: true}, @"options"];
[_connection registerSelector:@selector(_didReceiveMessage:) ofObject:self withDict:params];
}
/*! message sent when contact listening its message (using getMessages) and send appropriates notifications
you should never have to use this message.
@param aStanza the response stanza
@return YES in order to listen again
*/
- (BOOL)_didReceiveMessage:(id)aStanza
{
var center = [CPNotificationCenter defaultCenter],
userInfo = [CPDictionary dictionaryWithObjectsAndKeys:aStanza, @"stanza", [CPDate date], @"date"];
if ([aStanza containsChildrenWithName:@"composing"])
[center postNotificationName:TNStropheContactMessageComposingNotification object:self userInfo:userInfo];
if ([aStanza containsChildrenWithName:@"paused"])
[center postNotificationName:TNStropheContactMessagePausedNotification object:self userInfo:userInfo];
if ([aStanza containsChildrenWithName:@"active"])
[center postNotificationName:TNStropheContactMessageActiveNotification object:self userInfo:userInfo];
if ([aStanza containsChildrenWithName:@"inactive"])
[center postNotificationName:TNStropheContactMessageInactiveNotification object:self userInfo:userInfo];
if ([aStanza containsChildrenWithName:@"gone"])
[center postNotificationName:TNStropheContactMessageGoneNotification object:self userInfo:userInfo];
if ([aStanza containsChildrenWithName:@"body"])
{
_statusIcon = TNStropheContactImageNewMessage;
[_messagesQueue addObject:aStanza];
_numberOfEvents++;
[center postNotificationName:TNStropheContactMessageReceivedNotification object:self userInfo:userInfo];
}
return YES;
}
/*! send a message to the contact (of type chat)
@param aMessage CPString containing the message
*/
- (void)sendMessage:(CPString)aMessage
{
[self sendMessage:aMessage withType:@"chat"];
}
/*! send a message to the contact
@param aMessage CPString containing the message
@param aType CPString containing type
*/
- (void)sendMessage:(CPString)aMessage withType:(CPString)aType
{
var messageStanza = [TNStropheStanza messageWithAttributes:{@"type":aType}];
[messageStanza addChildWithName:@"body"];
[messageStanza addTextNode:aMessage];
[self sendStanza:messageStanza andRegisterSelector:@selector(_didSendMessage:) ofObject:self];
}
/*! @ignore
*/
- (void)_didSendMessage:(TNStropheStanza)aStanza
{
if ([aStanza type] == @"error")
CPLog.error(@"Unable to send message. Resp is: " + aStanza);
}
/*! return the last TNStropheStanza message in the message queue and remove it form the queue.
Will post TNStropheContactMessageTreatedNotification.
@return TNStropheStanza the last message in queue
*/
- (TNStropheStanza)popMessagesQueue
{
if ([_messagesQueue count] == 0)
return;
var message = [_messagesQueue objectAtIndex:0];
_numberOfEvents--;
if (_numberOfEvents === 0)
_statusIcon = _statusReminder;
[_messagesQueue removeObjectAtIndex:0];
[[CPNotificationCenter defaultCenter] postNotificationName:TNStropheContactMessageTreatedNotification object:self];
return message;
}
/*! purge all message in queue. Will post TNStropheContactMessageTreatedNotification
*/
- (void)freeMessagesQueue
{
_numberOfEvents = 0;
[self setStatusIcon:_statusReminder];
[_messagesQueue removeAllObjects];
[[CPNotificationCenter defaultCenter] postNotificationName:TNStropheContactMessageTreatedNotification object:self];
}
#pragma mark -
#pragma mark Delegates
/*! this method is called when the avatar image is ready.
@param anImage the image that sent the message
*/
- (void)imageDidLoad:(CPImage)anImage
{
[anImage setDelegate:nil];
[[CPNotificationCenter defaultCenter] postNotificationName:TNStropheContactVCardReceivedNotification object:self];
}
@end
@implementation TNStropheContact (CPCoding)
- (id)initWithCoder:(CPCoder)aCoder
{
if (self = [super init])
{
_JID = [aCoder decodeObjectForKey:@"_JID"];
_nodeName = [aCoder decodeObjectForKey:@"_nodeName"];
_groups = [aCoder decodeObjectForKey:@"_groups"];
_nickname = [aCoder decodeObjectForKey:@"_nickname"];
_XMPPStatus = [aCoder decodeObjectForKey:@"_XMPPStatus"];
_resources = [aCoder decodeObjectForKey:@"_resources"];
_XMPPShow = [aCoder decodeObjectForKey:@"_XMPPShow"];
_statusIcon = [aCoder decodeObjectForKey:@"_statusIcon"];
_type = [aCoder decodeObjectForKey:@"_type"];
_vCard = [aCoder decodeObjectForKey:@"_vCard"];
_numberOfEvents = [aCoder decodeObjectForKey:@"_numberOfEvents"];
}
return self;
}
- (void)encodeWithCoder:(CPCoder)aCoder
{
[aCoder encodeObject:_JID forKey:@"_JID"];
[aCoder encodeObject:_nodeName forKey:@"_nodeName"];
[aCoder encodeObject:_groups forKey:@"_groups"];
[aCoder encodeObject:_nickname forKey:@"_nickname"];
[aCoder encodeObject:_XMPPStatus forKey:@"_XMPPStatus"];
[aCoder encodeObject:_XMPPShow forKey:@"_XMPPShow"];
[aCoder encodeObject:_type forKey:@"_type"];
[aCoder encodeObject:_statusIcon forKey:@"_statusIcon"];
[aCoder encodeObject:_messagesQueue forKey:@"_messagesQueue"];
[aCoder encodeObject:_numberOfEvents forKey:@"_numberOfEvents"];
if (_resources)
[aCoder encodeObject:_resources forKey:@"_resources"];
if (_vCard)
[aCoder encodeObject:_vCard forKey:@"_vCard"];
}
@end