/***************************************************************************
                            KSSLCertificate.m -  description
                             -------------------
    begin                : Fri Mar  8 13:55:08 2002
    copyright            : (C) 2000-2002 Lost Highway Ltd. All rights reserved.
    email                : Lost_Highway@tipitina.demon.co.uk
    generated by         : duke@tipitina on Fri Mar  8 13:55:08 2002, using kalyptus 0.9.
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This library is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 ***************************************************************************/

#include <qtc/qt_QString.h>
#include <qtc/qt_QCString.h>
#include <kdec/kde_KSSLCertificate.h>
#include <kdeobjc/KDESupport.h>
#include <kdeobjc/KSSLCertificate.h>

@implementation  KSSLCertificate

- (void *) qt
{
	return _qt;
}

- setQt: (void *) qt
{
	_qt = qt;
	return self;
}

- (BOOL) allocatedInObjcWorld
{
	return _allocatedInObjcWorld;
}

- setAllocatedInObjcWorld: (BOOL) yn
{
	_allocatedInObjcWorld = yn;
	return self;
}


- (void) dealloc
{
	if ([self qt] != 0L) {
		if ([self allocatedInObjcWorld]) {
			kde_del_KSSLCertificate((kde_KSSLCertificate *)[self qt]);
		}
		[QtSupport removeQtKey: [self qt]];
		[self setQt: 0L];
	}
	[super dealloc];
}

- (NSString *) toString
{
static qt_QString * _qstring = 0L;
	_qstring = kde_KSSLCertificate_toString((kde_KSSLCertificate *)[self qt]);
	return [NSString fromQString: _qstring];
}

- (NSString *) getSubject
{
static qt_QString * _qstring = 0L;
	_qstring = kde_KSSLCertificate_getSubject((kde_KSSLCertificate *)[self qt]);
	return [NSString fromQString: _qstring];
}

- (NSString *) getIssuer
{
static qt_QString * _qstring = 0L;
	_qstring = kde_KSSLCertificate_getIssuer((kde_KSSLCertificate *)[self qt]);
	return [NSString fromQString: _qstring];
}

- (NSString *) getNotBefore
{
static qt_QString * _qstring = 0L;
	_qstring = kde_KSSLCertificate_getNotBefore((kde_KSSLCertificate *)[self qt]);
	return [NSString fromQString: _qstring];
}

- (NSString *) getNotAfter
{
static qt_QString * _qstring = 0L;
	_qstring = kde_KSSLCertificate_getNotAfter((kde_KSSLCertificate *)[self qt]);
	return [NSString fromQString: _qstring];
}

- (NSDate *) getQDTNotBefore
{
static qt_QDateTime * _qdate = 0L;
	_qdate = kde_KSSLCertificate_getQDTNotBefore((kde_KSSLCertificate *)[self qt]);
	return (NSDate *) [[NSDate alloc] initWithQDateTime: _qdate];
}

- (NSDate *) getQDTNotAfter
{
static qt_QDateTime * _qdate = 0L;
	_qdate = kde_KSSLCertificate_getQDTNotAfter((kde_KSSLCertificate *)[self qt]);
	return (NSDate *) [[NSDate alloc] initWithQDateTime: _qdate];
}

- (QByteArray*) toDer
{
	return (QByteArray*) [QtSupport objectForQtKey: kde_KSSLCertificate_toDer((kde_KSSLCertificate *)[self qt]) withClass: [QByteArray class] allocatedInObjcWorld: YES];
}

- (QByteArray*) toPem
{
	return (QByteArray*) [QtSupport objectForQtKey: kde_KSSLCertificate_toPem((kde_KSSLCertificate *)[self qt]) withClass: [QByteArray class] allocatedInObjcWorld: YES];
}

- (QByteArray*) toNetscape
{
	return (QByteArray*) [QtSupport objectForQtKey: kde_KSSLCertificate_toNetscape((kde_KSSLCertificate *)[self qt]) withClass: [QByteArray class] allocatedInObjcWorld: YES];
}

- (NSString *) toText
{
static qt_QString * _qstring = 0L;
	_qstring = kde_KSSLCertificate_toText((kde_KSSLCertificate *)[self qt]);
	return [NSString fromQString: _qstring];
}

- (NSString *) getSerialNumber
{
static qt_QString * _qstring = 0L;
	_qstring = kde_KSSLCertificate_getSerialNumber((kde_KSSLCertificate *)[self qt]);
	return [NSString fromQString: _qstring];
}

- (NSString *) getKeyType
{
static qt_QString * _qstring = 0L;
	_qstring = kde_KSSLCertificate_getKeyType((kde_KSSLCertificate *)[self qt]);
	return [NSString fromQString: _qstring];
}

- (NSString *) getPublicKeyText
{
static qt_QString * _qstring = 0L;
	_qstring = kde_KSSLCertificate_getPublicKeyText((kde_KSSLCertificate *)[self qt]);
	return [NSString fromQString: _qstring];
}

- (NSString *) getMD5DigestText
{
static qt_QString * _qstring = 0L;
	_qstring = kde_KSSLCertificate_getMD5DigestText((kde_KSSLCertificate *)[self qt]);
	return [NSString fromQString: _qstring];
}

- (NSString *) getSignatureText
{
static qt_QString * _qstring = 0L;
	_qstring = kde_KSSLCertificate_getSignatureText((kde_KSSLCertificate *)[self qt]);
	return [NSString fromQString: _qstring];
}

- (BOOL) isValid
{
	return (BOOL) kde_KSSLCertificate_isValid((kde_KSSLCertificate *)[self qt]);
}

- (int) validate
{
	return (int) kde_KSSLCertificate_validate((kde_KSSLCertificate *)[self qt]);
}

- (int) revalidate
{
	return (int) kde_KSSLCertificate_revalidate((kde_KSSLCertificate *)[self qt]);
}

//- (KSSLCertChain*) chain
//{
//	return (KSSLCertChain*) [QtSupport objectForQtKey: kde_KSSLCertificate_chain((kde_KSSLCertificate *)[self qt]) withClass: [KSSLCertChain class] allocatedInObjcWorld: YES];
//}

- (KSSLCertificate*) replicate
{
	return (KSSLCertificate*) [QtSupport objectForQtKey: kde_KSSLCertificate_replicate((kde_KSSLCertificate *)[self qt]) withClass: [KSSLCertificate class] allocatedInObjcWorld: YES];
}

- copyWithZone: (NSZone *) zone
{
	KSSLCertificate * aCopy;
	aCopy = [[(KSSLCertificate *)[[self class] allocWithZone: zone] setQt: kde_new_KSSLCertificate([self qt])] init];
	[aCopy setAllocatedInObjcWorld: YES];
	[QtSupport setObject: aCopy forQtKey: [aCopy qt]];
	return aCopy;
}

- (BOOL) setCertWithName: (NSString *)cert
{
static qt_QString * _qstring_cert = 0L;
	return (BOOL) kde_KSSLCertificate_setCert((kde_KSSLCertificate *)[self qt], (qt_QString *) [cert toQString: &_qstring_cert]);
}

//- (KSSLX509V3*) x509V3Extensions
//{
//	return (KSSLX509V3*) [QtSupport objectForQtKey: kde_KSSLCertificate_x509V3Extensions((kde_KSSLCertificate *)[self qt]) withClass: [KSSLX509V3 class] allocatedInObjcWorld: YES];
//}

- (BOOL) isSigner
{
	return (BOOL) kde_KSSLCertificate_isSigner((kde_KSSLCertificate *)[self qt]);
}

+ (KSSLCertificate*) fromString: (NSString *)cert
{
static qt_QCString * _qcstring_cert = 0L;
	return (KSSLCertificate*) [QtSupport objectForQtKey: kde_KSSLCertificate_fromString((qt_QCString*) [cert toQCString: &_qcstring_cert]) withClass: [KSSLCertificate class] allocatedInObjcWorld: YES];
}

//+ (KSSLCertificate*) fromX509: (X509*)x5
//{
//	return (KSSLCertificate*) [QtSupport objectForQtKey: kde_KSSLCertificate_fromX509((X509*) x5) withClass: [KSSLCertificate class] allocatedInObjcWorld: YES];
//}

+ (NSString *) verifyText: (int)x
{
static qt_QString * _qstring = 0L;
	_qstring = kde_KSSLCertificate_verifyText((int)x);
	return [NSString fromQString: _qstring];
}

- init
{
	if ([self qt] == 0L) {
		[self setQt: kde_new_KSSLCertificate1()];
		[self setAllocatedInObjcWorld: YES];
		[QtSupport setObject: self forQtKey: [self qt]];
	}
	[super init];
	return self;
}

//- setCert: (X509*)c
//{
//	kde_KSSLCertificate_setCert1((kde_KSSLCertificate *)[self qt], (X509*) c);
//	return self;
//}

- setChain: (void*)c
{
	kde_KSSLCertificate_setChain((kde_KSSLCertificate *)[self qt], (void*)c);
	return self;
}

//- (X509*) getCert
//{
//	return (X509*) kde_KSSLCertificate_getCert((kde_KSSLCertificate *)[self qt]);
//}

- (int) processError: (int)ec
{
	return (int) kde_KSSLCertificate_processError((kde_KSSLCertificate *)[self qt], (int)ec);
}

+ (int) Unknown
{
	return (int) KSSLCERTIFICATE_Unknown;
}

+ (int) Ok
{
	return (int) KSSLCERTIFICATE_Ok;
}

+ (int) NoCARoot
{
	return (int) KSSLCERTIFICATE_NoCARoot;
}

+ (int) InvalidPurpose
{
	return (int) KSSLCERTIFICATE_InvalidPurpose;
}

+ (int) PathLengthExceeded
{
	return (int) KSSLCERTIFICATE_PathLengthExceeded;
}

+ (int) InvalidCA
{
	return (int) KSSLCERTIFICATE_InvalidCA;
}

+ (int) Expired
{
	return (int) KSSLCERTIFICATE_Expired;
}

+ (int) SelfSigned
{
	return (int) KSSLCERTIFICATE_SelfSigned;
}

+ (int) ErrorReadingRoot
{
	return (int) KSSLCERTIFICATE_ErrorReadingRoot;
}

+ (int) NoSSL
{
	return (int) KSSLCERTIFICATE_NoSSL;
}

+ (int) Revoked
{
	return (int) KSSLCERTIFICATE_Revoked;
}

+ (int) Untrusted
{
	return (int) KSSLCERTIFICATE_Untrusted;
}

+ (int) SignatureFailed
{
	return (int) KSSLCERTIFICATE_SignatureFailed;
}

+ (int) Rejected
{
	return (int) KSSLCERTIFICATE_Rejected;
}

+ (int) PrivateKeyFailed
{
	return (int) KSSLCERTIFICATE_PrivateKeyFailed;
}

@end
