view dstep/foundation/NSURLConnection.d @ 14:89f3c3ef1fd2

Added the Foundation framework
author Jacob Carlborg <doob@me.com>
date Mon, 03 Aug 2009 15:23:15 +0200
parents
children 7ff919f595d5
line wrap: on
line source

/**
 * Copyright: Copyright (c) 2009 Jacob Carlborg.
 * Authors: Jacob Carlborg
 * Version: Initial created: Aug 3, 2009 
 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
 */
module dstep.foundation.NSURLConnection;

import dstep.AvailabilityMacros;
import dstep.foundation.NSCachedURLResponse;
import dstep.foundation.NSData;
import dstep.foundation.NSError;
import dstep.foundation.NSObject;
import dstep.foundation.NSRunLoop;
import dstep.foundation.NSURLAuthenticationChallenge;
import dstep.foundation.NSURLConnectionInternal;
import dstep.foundation.NSURLRequest;
import dstep.foundation.NSURLResponse;
import dstep.objc.bridge.Bridge;
import dstep.objc.objc : id;

class NSURLConnection : NSObject
{
	mixin ObjcWrap;
	mixin TNSURLConnectionSynchronousLoading;

	static bool canHandleRequest (NSURLRequest request)
	{
		return invokeObjcSelfClass!(bool, "canHandleRequest:", NSURLRequest)(request);
	}

	static NSURLConnection connectionWithRequest (NSURLRequest request, Object delegate_)
	{
		return invokeObjcSelfClass!(NSURLConnection, "connectionWithRequest:delegate:", NSURLRequest, Object)(request, delegate_return result is this.objcObject ? this : (result !is null ? new NSURLConnection(result) : null);	}

	Object initWithRequest (NSURLRequest request, Object delegate_)
	{
		return invokeObjcSelf!(Object, "initWithRequest:delegate:", NSURLRequest, Object)(request, delegate_);
	}

	this (NSURLRequest request, Object delegate_)
	{
		objcObject = Bridge.invokeObjcClassMethod!(id, "alloc")(objcClass);
		id result = Bridge.invokeObjcMethod!(id, "initWithRequest:delegate:", NSURLRequest, Object)(objcObject, request, delegate_);

		if (result)
			objcObject = ret;

		dObject = this;
	}

	Object initWithRequest (NSURLRequest request, Object delegate_, bool startImmediately)
	{
		return invokeObjcSelf!(Object, "initWithRequest:delegate:startImmediately:", NSURLRequest, Object, bool)(request, delegate_, startImmediately);
	}

	this (NSURLRequest request, Object delegate_, bool startImmediately)
	{
		objcObject = Bridge.invokeObjcClassMethod!(id, "alloc")(objcClass);
		id result = Bridge.invokeObjcMethod!(id, "initWithRequest:delegate:startImmediately:", NSURLRequest, Object, bool)(objcObject, request, delegate_, startImmediately);

		if (result)
			objcObject = ret;

		dObject = this;
	}

	void start ()
	{
		return invokeObjcSelf!(void, "start");
	}

	void cancel ()
	{
		return invokeObjcSelf!(void, "cancel");
	}

	void scheduleInRunLoop (NSRunLoop aRunLoop, NSString mode)
	{
		return invokeObjcSelf!(void, "scheduleInRunLoop:forMode:", NSRunLoop, NSString)(aRunLoop, mode);
	}

	void unscheduleFromRunLoop (NSRunLoop aRunLoop, NSString mode)
	{
		return invokeObjcSelf!(void, "unscheduleFromRunLoop:forMode:", NSRunLoop, NSString)(aRunLoop, mode);
	}
}

template TNSURLConnectionDelegate ()
{
	NSURLRequest connection (NSURLConnection connection, NSURLRequest request, NSURLResponse response);
	void connection (NSURLConnection connection, NSURLAuthenticationChallenge challenge);
	void connection (NSURLConnection connection, NSURLAuthenticationChallenge challenge);
	void connection (NSURLConnection connection, NSURLResponse response);
	void connection (NSURLConnection connection, NSData data);
	void connectionDidFinishLoading (NSURLConnection connection);
	void connection (NSURLConnection connection, NSError error);
	NSCachedURLResponse connection (NSURLConnection connection, NSCachedURLResponse cachedResponse);
}

template TNSURLConnectionSynchronousLoading ()
{
	static NSData sendSynchronousRequest (NSURLRequest request, NSURLResponse** response, NSError** error)
	{
		return invokeObjcSelfClass!(NSData, "sendSynchronousRequest:returningResponse:error:", NSURLRequest, NSURLResponse**, NSError**)(request, response, error);
	}
}