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; @class XMPPIQ;
@protocol XMPPCallback <OFObject>
- (void)runWithIQ: (XMPPIQ*)iq;
@end
#ifdef OF_HAVE_BLOCKS #ifdef OF_HAVE_BLOCKS
typedef void(^xmpp_callback_block)(XMPPIQ*); 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 #endif
@interface XMPPObjectCallback: OFObject <XMPPCallback> @interface XMPPCallback: OFObject
{ {
id object; id object;
SEL selector; SEL selector;
} }
#ifdef OF_HAVE_BLOCKS
+ callbackWithCallbackBlock: (xmpp_callback_block)callback;
- initWithCallbackBlock: (xmpp_callback_block)callback;
#endif
+ callbackWithCallbackObject: (id)object + callbackWithCallbackObject: (id)object
selector: (SEL)selector; selector: (SEL)selector;
- initWithCallbackObject: (id)object - initWithCallbackObject: (id)object
selector: (SEL)selector; selector: (SEL)selector;
- (void)runWithIQ: (XMPPIQ*)iq;
@end @end

View file

@ -26,37 +26,23 @@
#import "XMPPCallback.h" #import "XMPPCallback.h"
@implementation XMPPCallback
#ifdef OF_HAVE_BLOCKS #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]; self = [super init];
callback = [callback_ copy]; object = [callback copy];
return self; return self;
} }
- (void)dealloc
{
[callback release];
[super dealloc];
}
- (void)runWithIQ: (XMPPIQ*)iq
{
callback(iq);
}
@end
#endif #endif
@implementation XMPPObjectCallback
+ callbackWithCallbackObject: (id)object_ + callbackWithCallbackObject: (id)object_
selector: (SEL)selector_ selector: (SEL)selector_
{ {
@ -85,7 +71,12 @@
- (void)runWithIQ: (XMPPIQ*)iq - (void)runWithIQ: (XMPPIQ*)iq
{ {
[object performSelector: selector #ifdef OF_HAVE_BLOCKS
withObject: iq]; if ([object isKindOfClass: [OFBlock class]])
((xmpp_callback_block)object)(iq);
else
#endif
[object performSelector: selector
withObject: iq];
} }
@end @end

View file

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

View file

@ -147,6 +147,17 @@ OF_APPLICATION_DELEGATE(AppDelegate)
[pres addStatus: @"ObjXMPP test is working!"]; [pres addStatus: @"ObjXMPP test is working!"];
[conn sendStanza: pres]; [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 - (void)connectionDidUpgradeToTLS: (XMPPConnection*)conn