Merge XMPP*Callback into a single class

This commit is contained in:
Florian Zeitz 2012-01-06 20:18:33 +01:00
parent b94a321cfa
commit a433727bd5
4 changed files with 33 additions and 37 deletions

View file

@ -24,29 +24,23 @@
@class XMPPIQ;
@protocol XMPPCallback <OFObject>
- (void)runWithIQ: (XMPPIQ*)iq;
@end
#ifdef OF_HAVE_BLOCKS
typedef void(^xmpp_callback_block)(XMPPIQ*);
@interface XMPPBlockCallback: OFObject <XMPPCallback>
{
xmpp_callback_block callback;
}
+ callbackWithCallbackBlock: (xmpp_callback_block)callback;
- initWithCallbackBlock: (xmpp_callback_block)callback;
@end
#endif
@interface XMPPObjectCallback: OFObject <XMPPCallback>
@interface XMPPCallback: OFObject
{
id object;
SEL selector;
}
#ifdef OF_HAVE_BLOCKS
+ callbackWithCallbackBlock: (xmpp_callback_block)callback;
- initWithCallbackBlock: (xmpp_callback_block)callback;
#endif
+ callbackWithCallbackObject: (id)object
selector: (SEL)selector;
- initWithCallbackObject: (id)object
selector: (SEL)selector;
- (void)runWithIQ: (XMPPIQ*)iq;
@end

View file

@ -26,37 +26,23 @@
#import "XMPPCallback.h"
@implementation XMPPCallback
#ifdef OF_HAVE_BLOCKS
@implementation XMPPBlockCallback
+ callbackWithCallbackBlock: (xmpp_callback_block)callback_
+ callbackWithCallbackBlock: (xmpp_callback_block)callback
{
return [[[self alloc] initWithCallbackBlock: callback_] autorelease];
return [[[self alloc] initWithCallbackBlock: callback] autorelease];
}
- initWithCallbackBlock: (xmpp_callback_block)callback_
- initWithCallbackBlock: (xmpp_callback_block)callback
{
self = [super init];
callback = [callback_ copy];
object = [callback copy];
return self;
}
- (void)dealloc
{
[callback release];
[super dealloc];
}
- (void)runWithIQ: (XMPPIQ*)iq
{
callback(iq);
}
@end
#endif
@implementation XMPPObjectCallback
+ callbackWithCallbackObject: (id)object_
selector: (SEL)selector_
{
@ -85,6 +71,11 @@
- (void)runWithIQ: (XMPPIQ*)iq
{
#ifdef OF_HAVE_BLOCKS
if ([object isKindOfClass: [OFBlock class]])
((xmpp_callback_block)object)(iq);
else
#endif
[object performSelector: selector
withObject: iq];
}

View file

@ -362,7 +362,7 @@ withCallbackObject: (id)object
if (![iq ID])
[iq setID: [self generateStanzaID]];
[callbacks setObject: [XMPPObjectCallback
[callbacks setObject: [XMPPCallback
callbackWithCallbackObject: object
selector: selector]
forKey: [iq ID]];
@ -382,7 +382,7 @@ withCallbackBlock: (xmpp_callback_block)callback;
if (![iq ID])
[iq setID: [self generateStanzaID]];
[callbacks setObject: [XMPPBlockCallback
[callbacks setObject: [XMPPCallback
callbackWithCallbackBlock: callback]
forKey: [iq ID]];
} @finally {
@ -697,7 +697,7 @@ withCallbackBlock: (xmpp_callback_block)callback;
- (void)XMPP_handleIQ: (XMPPIQ*)iq
{
BOOL handled = NO;
id <XMPPCallback> callback;
XMPPCallback *callback;
if ((callback = [callbacks objectForKey: [iq ID]])) {
[callback runWithIQ: iq];

View file

@ -147,6 +147,17 @@ OF_APPLICATION_DELEGATE(AppDelegate)
[pres addStatus: @"ObjXMPP test is working!"];
[conn sendStanza: pres];
#ifdef OF_HAVE_BLOCKS
XMPPIQ *iq = [XMPPIQ IQWithType: @"get"
ID: [conn generateStanzaID]];
[iq addChild: [OFXMLElement elementWithName: @"ping"
namespace: @"urn:xmpp:ping"]];
[conn sendIQ: iq
withCallbackBlock: ^(XMPPIQ* resp) {
of_log(@"Ping response: %@", resp);
}];
#endif
}
- (void)connectionDidUpgradeToTLS: (XMPPConnection*)conn