// This file was generated by the Gtk# code generator.
// Any changes made will be lost if regenerated.

namespace GLib {

	using System;
	using System.Runtime.InteropServices;

#region Autogenerated code
	public partial class ProxyAdapter : GLib.GInterfaceAdapter, GLib.IProxy {

		[StructLayout (LayoutKind.Sequential)]
		struct GProxyInterface {
			public ConnectNativeDelegate Connect;
			public ConnectAsyncNativeDelegate ConnectAsync;
			public ConnectFinishNativeDelegate ConnectFinish;
			public SupportsHostnameNativeDelegate SupportsHostname;
		}

		static GProxyInterface iface;

		static ProxyAdapter ()
		{
			GLib.GType.Register (_gtype, typeof (ProxyAdapter));
			iface.Connect = new ConnectNativeDelegate (Connect_cb);
			iface.ConnectAsync = new ConnectAsyncNativeDelegate (ConnectAsync_cb);
			iface.ConnectFinish = new ConnectFinishNativeDelegate (ConnectFinish_cb);
			iface.SupportsHostname = new SupportsHostnameNativeDelegate (SupportsHostname_cb);
		}

		[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
		delegate IntPtr ConnectNativeDelegate (IntPtr inst, IntPtr connection, IntPtr proxy_address, IntPtr cancellable, out IntPtr error);

		static IntPtr Connect_cb (IntPtr inst, IntPtr connection, IntPtr proxy_address, IntPtr cancellable, out IntPtr error)
		{
			error = IntPtr.Zero;

			try {
				IProxyImplementor __obj = GLib.Object.GetObject (inst, false) as IProxyImplementor;
				GLib.IOStream __result;
				__result = __obj.Connect (GLib.Object.GetObject(connection) as GLib.IOStream, GLib.Object.GetObject(proxy_address) as GLib.ProxyAddress, GLib.Object.GetObject(cancellable) as GLib.Cancellable);
				return __result == null ? IntPtr.Zero : __result.Handle;
			} catch (Exception e) {
				GLib.ExceptionManager.RaiseUnhandledException (e, true);
				// NOTREACHED: above call does not return.
				throw;
			}
		}

		[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
		delegate void ConnectAsyncNativeDelegate (IntPtr inst, IntPtr connection, IntPtr proxy_address, IntPtr cancellable, GLibSharp.AsyncReadyCallbackNative cb, IntPtr user_data);

		static void ConnectAsync_cb (IntPtr inst, IntPtr connection, IntPtr proxy_address, IntPtr cancellable, GLibSharp.AsyncReadyCallbackNative cb, IntPtr user_data)
		{
			try {
				IProxyImplementor __obj = GLib.Object.GetObject (inst, false) as IProxyImplementor;
				GLibSharp.AsyncReadyCallbackInvoker cb_invoker = new GLibSharp.AsyncReadyCallbackInvoker (cb, user_data);
				__obj.ConnectAsync (GLib.Object.GetObject(connection) as GLib.IOStream, GLib.Object.GetObject(proxy_address) as GLib.ProxyAddress, GLib.Object.GetObject(cancellable) as GLib.Cancellable, cb_invoker.Handler);
			} catch (Exception e) {
				GLib.ExceptionManager.RaiseUnhandledException (e, false);
			}
		}

		[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
		delegate IntPtr ConnectFinishNativeDelegate (IntPtr inst, IntPtr result, out IntPtr error);

		static IntPtr ConnectFinish_cb (IntPtr inst, IntPtr result, out IntPtr error)
		{
			error = IntPtr.Zero;

			try {
				IProxyImplementor __obj = GLib.Object.GetObject (inst, false) as IProxyImplementor;
				GLib.IOStream __result;
				__result = __obj.ConnectFinish (GLib.AsyncResultAdapter.GetObject (result, false));
				return __result == null ? IntPtr.Zero : __result.Handle;
			} catch (Exception e) {
				GLib.ExceptionManager.RaiseUnhandledException (e, true);
				// NOTREACHED: above call does not return.
				throw;
			}
		}

		[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
		delegate bool SupportsHostnameNativeDelegate (IntPtr inst);

		static bool SupportsHostname_cb (IntPtr inst)
		{
			try {
				IProxyImplementor __obj = GLib.Object.GetObject (inst, false) as IProxyImplementor;
				bool __result;
				__result = __obj.SupportsHostname ();
				return __result;
			} catch (Exception e) {
				GLib.ExceptionManager.RaiseUnhandledException (e, true);
				// NOTREACHED: above call does not return.
				throw;
			}
		}

		static int class_offset = 2 * IntPtr.Size;

		static void Initialize (IntPtr ptr, IntPtr data)
		{
			IntPtr ifaceptr = new IntPtr (ptr.ToInt64 () + class_offset);
			GProxyInterface native_iface = (GProxyInterface) Marshal.PtrToStructure (ifaceptr, typeof (GProxyInterface));
			native_iface.Connect = iface.Connect;
			native_iface.ConnectAsync = iface.ConnectAsync;
			native_iface.ConnectFinish = iface.ConnectFinish;
			native_iface.SupportsHostname = iface.SupportsHostname;
			Marshal.StructureToPtr (native_iface, ifaceptr, false);
		}

		GLib.Object implementor;

		public ProxyAdapter ()
		{
			InitHandler = new GLib.GInterfaceInitHandler (Initialize);
		}

		public ProxyAdapter (IProxyImplementor implementor)
		{
			if (implementor == null)
				throw new ArgumentNullException ("implementor");
			else if (!(implementor is GLib.Object))
				throw new ArgumentException ("implementor must be a subclass of GLib.Object");
			this.implementor = implementor as GLib.Object;
		}

		public ProxyAdapter (IntPtr handle)
		{
			if (!_gtype.IsInstance (handle))
				throw new ArgumentException ("The gobject doesn't implement the GInterface of this adapter", "handle");
			implementor = GLib.Object.GetObject (handle);
		}

		[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
		delegate IntPtr d_g_proxy_get_type();
		static d_g_proxy_get_type g_proxy_get_type = FuncLoader.LoadFunction<d_g_proxy_get_type>(FuncLoader.GetProcAddress(GLibrary.Load(Library.Gio), "g_proxy_get_type"));

		private static GLib.GType _gtype = new GLib.GType (g_proxy_get_type ());

		public static GLib.GType GType {
			get {
				return _gtype;
			}
		}

		public override GLib.GType GInterfaceGType {
			get {
				return _gtype;
			}
		}

		public override IntPtr Handle {
			get {
				return implementor.Handle;
			}
		}

		public IntPtr OwnedHandle {
			get {
				return implementor.OwnedHandle;
			}
		}

		public static IProxy GetObject (IntPtr handle, bool owned)
		{
			GLib.Object obj = GLib.Object.GetObject (handle, owned);
			return GetObject (obj);
		}

		public static IProxy GetObject (GLib.Object obj)
		{
			if (obj == null)
				return null;
			else if (obj is IProxyImplementor)
				return new ProxyAdapter (obj as IProxyImplementor);
			else if (obj as IProxy == null)
				return new ProxyAdapter (obj.Handle);
			else
				return obj as IProxy;
		}

		public IProxyImplementor Implementor {
			get {
				return implementor as IProxyImplementor;
			}
		}

		[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
		delegate IntPtr d_g_proxy_connect(IntPtr raw, IntPtr connection, IntPtr proxy_address, IntPtr cancellable, out IntPtr error);
		static d_g_proxy_connect g_proxy_connect = FuncLoader.LoadFunction<d_g_proxy_connect>(FuncLoader.GetProcAddress(GLibrary.Load(Library.Gio), "g_proxy_connect"));

		public GLib.IOStream Connect(GLib.IOStream connection, GLib.ProxyAddress proxy_address, GLib.Cancellable cancellable) {
			IntPtr error = IntPtr.Zero;
			IntPtr raw_ret = g_proxy_connect(Handle, connection == null ? IntPtr.Zero : connection.Handle, proxy_address == null ? IntPtr.Zero : proxy_address.Handle, cancellable == null ? IntPtr.Zero : cancellable.Handle, out error);
			GLib.IOStream ret = GLib.Object.GetObject(raw_ret) as GLib.IOStream;
			if (error != IntPtr.Zero) throw new GLib.GException (error);
			return ret;
		}

		[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
		delegate void d_g_proxy_connect_async(IntPtr raw, IntPtr connection, IntPtr proxy_address, IntPtr cancellable, GLibSharp.AsyncReadyCallbackNative cb, IntPtr user_data);
		static d_g_proxy_connect_async g_proxy_connect_async = FuncLoader.LoadFunction<d_g_proxy_connect_async>(FuncLoader.GetProcAddress(GLibrary.Load(Library.Gio), "g_proxy_connect_async"));

		public void ConnectAsync(GLib.IOStream connection, GLib.ProxyAddress proxy_address, GLib.Cancellable cancellable, GLib.AsyncReadyCallback cb) {
			GLibSharp.AsyncReadyCallbackWrapper cb_wrapper = new GLibSharp.AsyncReadyCallbackWrapper (cb);
			cb_wrapper.PersistUntilCalled ();
			g_proxy_connect_async(Handle, connection == null ? IntPtr.Zero : connection.Handle, proxy_address == null ? IntPtr.Zero : proxy_address.Handle, cancellable == null ? IntPtr.Zero : cancellable.Handle, cb_wrapper.NativeDelegate, IntPtr.Zero);
		}

		[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
		delegate IntPtr d_g_proxy_connect_finish(IntPtr raw, IntPtr result, out IntPtr error);
		static d_g_proxy_connect_finish g_proxy_connect_finish = FuncLoader.LoadFunction<d_g_proxy_connect_finish>(FuncLoader.GetProcAddress(GLibrary.Load(Library.Gio), "g_proxy_connect_finish"));

		public GLib.IOStream ConnectFinish(GLib.IAsyncResult result) {
			IntPtr error = IntPtr.Zero;
			IntPtr raw_ret = g_proxy_connect_finish(Handle, result == null ? IntPtr.Zero : ((result is GLib.Object) ? (result as GLib.Object).Handle : (result as GLib.AsyncResultAdapter).Handle), out error);
			GLib.IOStream ret = GLib.Object.GetObject(raw_ret) as GLib.IOStream;
			if (error != IntPtr.Zero) throw new GLib.GException (error);
			return ret;
		}

		[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
		delegate IntPtr d_g_proxy_get_default_for_protocol(IntPtr protocol);
		static d_g_proxy_get_default_for_protocol g_proxy_get_default_for_protocol = FuncLoader.LoadFunction<d_g_proxy_get_default_for_protocol>(FuncLoader.GetProcAddress(GLibrary.Load(Library.Gio), "g_proxy_get_default_for_protocol"));

		public static GLib.IProxy GetDefaultForProtocol(string protocol) {
			IntPtr native_protocol = GLib.Marshaller.StringToPtrGStrdup (protocol);
			IntPtr raw_ret = g_proxy_get_default_for_protocol(native_protocol);
			GLib.IProxy ret = GLib.ProxyAdapter.GetObject (raw_ret, false);
			GLib.Marshaller.Free (native_protocol);
			return ret;
		}

		[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
		delegate bool d_g_proxy_supports_hostname(IntPtr raw);
		static d_g_proxy_supports_hostname g_proxy_supports_hostname = FuncLoader.LoadFunction<d_g_proxy_supports_hostname>(FuncLoader.GetProcAddress(GLibrary.Load(Library.Gio), "g_proxy_supports_hostname"));

		public bool SupportsHostname() {
			bool raw_ret = g_proxy_supports_hostname(Handle);
			bool ret = raw_ret;
			return ret;
		}

#endregion
	}
}
