diff --git a/src/XMPPCallback.h b/src/XMPPCallback.h index fd590c1..09f3d67 100644 --- a/src/XMPPCallback.h +++ b/src/XMPPCallback.h @@ -31,19 +31,22 @@ typedef void(^xmpp_callback_block_t)(XMPPConnection*, XMPPIQ*); @interface XMPPCallback: OFObject { - id object; + id target; SEL selector; +#ifdef OF_HAVE_BLOCKS + xmpp_callback_block_t block; +#endif } #ifdef OF_HAVE_BLOCKS -+ callbackWithCallbackBlock: (xmpp_callback_block_t)callback; -- initWithCallbackBlock: (xmpp_callback_block_t)callback; ++ callbackWithBlock: (xmpp_callback_block_t)callback; +- initWithBlock: (xmpp_callback_block_t)callback; #endif -+ callbackWithCallbackObject: (id)object - selector: (SEL)selector; -- initWithCallbackObject: (id)object - selector: (SEL)selector; ++ callbackWithTarget: (id)target + selector: (SEL)selector; +- initWithTarget: (id)target + selector: (SEL)selector; - (void)runWithIQ: (XMPPIQ*)iq connection: (XMPPConnection*)connection; diff --git a/src/XMPPCallback.m b/src/XMPPCallback.m index d18f3e5..db71cea 100644 --- a/src/XMPPCallback.m +++ b/src/XMPPCallback.m @@ -28,34 +28,39 @@ @implementation XMPPCallback #ifdef OF_HAVE_BLOCKS -+ callbackWithCallbackBlock: (xmpp_callback_block_t)callback ++ callbackWithBlock: (xmpp_callback_block_t)block { - return [[[self alloc] initWithCallbackBlock: callback] autorelease]; + return [[(XMPPCallback*)[self alloc] initWithBlock: block] autorelease]; } -- initWithCallbackBlock: (xmpp_callback_block_t)callback +- initWithBlock: (xmpp_callback_block_t)block_ { self = [super init]; - object = [callback copy]; + @try { + block = [block_ copy]; + } @catch (id e) { + [self release]; + @throw e; + } return self; } #endif -+ callbackWithCallbackObject: (id)object_ - selector: (SEL)selector_ ++ callbackWithTarget: (id)target + selector: (SEL)selector { - return [[[self alloc] initWithCallbackObject: object_ - selector: selector_] autorelease]; + return [[[self alloc] initWithTarget: target + selector: selector] autorelease]; } -- initWithCallbackObject: (id)object_ - selector: (SEL)selector_ +- initWithTarget: (id)target_ + selector: (SEL)selector_ { self = [super init]; - object = object_; + target = [target_ retain]; selector = selector_; return self; @@ -63,6 +68,11 @@ - (void)dealloc { + [target release]; +#ifdef OF_HAVE_BLOCKS + [block release]; +#endif + [super dealloc]; } @@ -70,11 +80,11 @@ connection: (XMPPConnection*)connection { #ifdef OF_HAVE_BLOCKS - if ([object isKindOfClass: [OFBlock class]]) - ((xmpp_callback_block_t)object)(connection, iq); + if (block != NULL) + block(connection, iq); else #endif - [object performSelector: selector + [target performSelector: selector withObject: connection withObject: iq]; } diff --git a/src/XMPPConnection.h b/src/XMPPConnection.h index 7fc11df..b566620 100644 --- a/src/XMPPConnection.h +++ b/src/XMPPConnection.h @@ -315,9 +315,9 @@ * \param selector The selector of the callback method, * must take exactly one parameter of type XMPPIQ* */ -- (void)sendIQ: (XMPPIQ*)iq - withCallbackObject: (id)object - selector: (SEL)selector; +- (void)sendIQ: (XMPPIQ*)iq + callbackTarget: (id)target + selector: (SEL)selector; #ifdef OF_HAVE_BLOCKS /** @@ -325,8 +325,8 @@ * * \param callback The callback block */ -- (void)sendIQ: (XMPPIQ*)iq - withCallbackBlock: (xmpp_callback_block_t)block; +- (void)sendIQ: (XMPPIQ*)iq + callbackBlock: (xmpp_callback_block_t)block; #endif /** @@ -370,10 +370,10 @@ - (void)XMPP_handlePresence: (XMPPPresence*)presence; - (void)XMPP_handleFeatures: (OFXMLElement*)element; - (void)XMPP_handleResourceBindForConnection: (XMPPConnection*)connection - withIQ: (XMPPIQ*)iq; + IQ: (XMPPIQ*)iq; - (void)XMPP_sendSession; - (void)XMPP_handleSessionForConnection: (XMPPConnection*)connection - withIQ: (XMPPIQ*)iq; + IQ: (XMPPIQ*)iq; - (OFString*)XMPP_IDNAToASCII: (OFString*)domain; - (XMPPMulticastDelegate*)XMPP_delegates; /// \endcond diff --git a/src/XMPPConnection.m b/src/XMPPConnection.m index 764afc7..08e82a3 100644 --- a/src/XMPPConnection.m +++ b/src/XMPPConnection.m @@ -469,9 +469,9 @@ [sock writeString: [element XMLString]]; } -- (void)sendIQ: (XMPPIQ*)iq - withCallbackObject: (id)object - selector: (SEL)selector +- (void)sendIQ: (XMPPIQ*)iq + callbackTarget: (id)target + selector: (SEL)selector { OFAutoreleasePool *pool; XMPPCallback *callback; @@ -480,8 +480,8 @@ [iq setID: [self generateStanzaID]]; pool = [[OFAutoreleasePool alloc] init]; - callback = [XMPPCallback callbackWithCallbackObject: object - selector: selector]; + callback = [XMPPCallback callbackWithTarget: target + selector: selector]; [callbacks setObject: callback forKey: [iq ID]]; [pool release]; @@ -490,8 +490,8 @@ } #ifdef OF_HAVE_BLOCKS -- (void)sendIQ: (XMPPIQ*)iq - withCallbackBlock: (xmpp_callback_block_t)block; +- (void)sendIQ: (XMPPIQ*)iq + callbackBlock: (xmpp_callback_block_t)block { OFAutoreleasePool *pool; XMPPCallback *callback; @@ -500,7 +500,7 @@ [iq setID: [self generateStanzaID]]; pool = [[OFAutoreleasePool alloc] init]; - callback = [XMPPCallback callbackWithCallbackBlock: block]; + callback = [XMPPCallback callbackWithBlock: block]; [callbacks setObject: callback forKey: [iq ID]]; [pool release]; @@ -1038,10 +1038,10 @@ [iq addChild: bind]; - [self sendIQ: iq - withCallbackObject: self - selector: @selector(XMPP_handleResourceBindForConnection: - withIQ:)]; + [self sendIQ: iq + callbackTarget: self + selector: @selector(XMPP_handleResourceBindForConnection: + IQ:)]; } - (void)XMPP_sendStreamError: (OFString*)condition @@ -1065,7 +1065,7 @@ } - (void)XMPP_handleResourceBindForConnection: (XMPPConnection*)connection - withIQ: (XMPPIQ*)iq + IQ: (XMPPIQ*)iq { OFXMLElement *bindElement; OFXMLElement *jidElement; @@ -1099,14 +1099,13 @@ ID: [self generateStanzaID]]; [iq addChild: [OFXMLElement elementWithName: @"session" namespace: XMPP_NS_SESSION]]; - [self sendIQ: iq - withCallbackObject: self - selector: @selector( - XMPP_handleSessionForConnection:withIQ:)]; + [self sendIQ: iq + callbackTarget: self + selector: @selector(XMPP_handleSessionForConnection:IQ:)]; } - (void)XMPP_handleSessionForConnection: (XMPPConnection*)connection - withIQ: (XMPPIQ*)iq + IQ: (XMPPIQ*)iq { if (![[iq type] isEqual: @"result"]) assert(0); diff --git a/src/XMPPRoster.h b/src/XMPPRoster.h index 702b052..5e00daf 100644 --- a/src/XMPPRoster.h +++ b/src/XMPPRoster.h @@ -156,7 +156,7 @@ /// \cond internal - (void)XMPP_updateRosterItem: (XMPPRosterItem*)rosterItem; - (void)XMPP_handleInitialRosterForConnection: (XMPPConnection*)connection - withIQ: (XMPPIQ*)iq; + IQ: (XMPPIQ*)iq; - (XMPPRosterItem*)XMPP_rosterItemWithXMLElement: (OFXMLElement*)element; /// \endcond @end diff --git a/src/XMPPRoster.m b/src/XMPPRoster.m index 70b9477..4a8594c 100644 --- a/src/XMPPRoster.m +++ b/src/XMPPRoster.m @@ -97,10 +97,10 @@ [iq addChild: query]; - [connection sendIQ: iq - withCallbackObject: self - selector: @selector(XMPP_handleInitialRosterForConnection: - withIQ:)]; + [connection sendIQ: iq + callbackTarget: self + selector: @selector(XMPP_handleInitialRosterForConnection: + IQ:)]; } - (BOOL)connection: (XMPPConnection*)connection_ @@ -306,7 +306,7 @@ } - (void)XMPP_handleInitialRosterForConnection: (XMPPConnection*)connection_ - withIQ: (XMPPIQ*)iq + IQ: (XMPPIQ*)iq { OFXMLElement *rosterElement; OFEnumerator *enumerator; diff --git a/tests/test.m b/tests/test.m index 2b32236..bdc43f1 100644 --- a/tests/test.m +++ b/tests/test.m @@ -169,8 +169,8 @@ OF_APPLICATION_DELEGATE(AppDelegate) ID: [conn generateStanzaID]]; [iq addChild: [OFXMLElement elementWithName: @"ping" namespace: @"urn:xmpp:ping"]]; - [conn sendIQ: iq - withCallbackBlock: ^ (XMPPConnection *c, XMPPIQ *resp) { + [conn sendIQ: iq + callbackBlock: ^ (XMPPConnection *c, XMPPIQ *resp) { of_log(@"Ping response: %@", resp); }]; #endif