/*
 *  Copyright (C) 2007-2008 CMM Sigma Andrzej Chmielowiec <cmmsigma@cmmsigma.eu>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 3
 *  as published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */

#ifndef CRD_UT10XX_THREADS
#define CRD_UT10XX_THREADS

#include "crd_ut10xx.h"

#include <QThread>
#include <QString>
#include <QStringList>
#include <QCursor>

class CRD_UT10XX_thread: public QThread
{
	Q_OBJECT;
	
public:
	CRD_UT10XX_thread(CRD_UT10XX_t *ut = 0);
	
	int execWithCursor(Qt::CursorShape shape = Qt::WaitCursor);
	int status();
	QString errorString();
	
protected:
	void setStatus(int status);
	CRD_UT10XX_t *mUt;
	
private:
	int mStatus;
	QString mErrorString;
	
};

//
// CRD_UT10XX_lib_find_devices and CRD_UT10XX_lib_actualize_devices
//
class CRD_UT10XX_thread_lib_refresh: public CRD_UT10XX_thread
{
	Q_OBJECT;
	
public:
	CRD_UT10XX_thread_lib_refresh();
	
protected:
	void run();
	
};


//
// CRD_UT10XX_cmd_dev_init
//
class CRD_UT10XX_thread_cmd_dev_init: public CRD_UT10XX_thread
{
	Q_OBJECT;
	
public:
	CRD_UT10XX_thread_cmd_dev_init(CRD_UT10XX_t *ut = 0);
	
protected:
	void run();
	
};


//
// CRD_UT10XX_cmd_dev_conf
//
class CRD_UT10XX_thread_cmd_dev_conf: public CRD_UT10XX_thread
{
	Q_OBJECT;
	
public:
	CRD_UT10XX_thread_cmd_dev_conf(CRD_UT10XX_t *ut = 0);
	
protected:
	void run();
	
};


//
// CRD_UT10XX_cmd_read_dev_info
//
class CRD_UT10XX_thread_cmd_read_dev_info: public CRD_UT10XX_thread
{
	Q_OBJECT;
	
public:
	CRD_UT10XX_thread_cmd_read_dev_info(CRD_UT10XX_t *ut = 0);
	
protected:
	void run();
	
};


//
// CRD_UT10XX_cmd_read_key_info
//
class CRD_UT10XX_thread_cmd_read_key_info: public CRD_UT10XX_thread
{
	Q_OBJECT;
	
public:
	CRD_UT10XX_thread_cmd_read_key_info(CRD_UT10XX_t *ut = 0, int keyNum = 0);
	
protected:
	void run();
	
private:
	int mKeyNum;
	
};


//
// CRD_UT10XX_cmd_read_public_obj
//
class CRD_UT10XX_thread_cmd_read_public_obj: public CRD_UT10XX_thread
{
	Q_OBJECT;
	
public:
	CRD_UT10XX_thread_cmd_read_public_obj(CRD_UT10XX_t *ut = 0, int objNum = 0);
	
	QByteArray obj();
	
protected:
	void run();
	
private:
	int mObjNum;
	QByteArray mObj;
	
};


//
// CRD_UT10XX_cmd_rbg_generate_bits
//
class CRD_UT10XX_thread_cmd_rbg_generate_bits: public CRD_UT10XX_thread
{
	Q_OBJECT;
	
public:
	CRD_UT10XX_thread_cmd_rbg_generate_bits(CRD_UT10XX_t *ut = 0, int octets = 0);
	
	QByteArray buffer();
	void copyBuffer(char *buffer);
	
protected:
	void run();
	
private:
	int mOctets;
	QByteArray mBuffer;
	
};


//
// CRD_UT10XX_cmd_dev_unlock
//
class CRD_UT10XX_thread_cmd_dev_unlock: public CRD_UT10XX_thread
{
	Q_OBJECT;
	
public:
	CRD_UT10XX_thread_cmd_dev_unlock(CRD_UT10XX_t *ut = 0, const char *pin = 0);
	
protected:
	void run();
	
private:
	const char *mPin;
	
};


//
// CRD_UT10XX_cmd_dev_erase
//
class CRD_UT10XX_thread_cmd_dev_erase: public CRD_UT10XX_thread
{
	Q_OBJECT;
	
public:
	CRD_UT10XX_thread_cmd_dev_erase(CRD_UT10XX_t *ut = 0, const char *pin = 0);
	
protected:
	void run();
	
private:
	const char *mPin;
	
};


//
// CRD_UT10XX_cmd_change_so_pin
//
class CRD_UT10XX_thread_cmd_change_so_pin: public CRD_UT10XX_thread
{
	Q_OBJECT;
	
public:
	CRD_UT10XX_thread_cmd_change_so_pin(CRD_UT10XX_t *ut = 0, const char *pin = 0, 
		const char *newPin = 0);
	
protected:
	void run();
	
private:
	const char *mPin;
	const char *mNewPin;
	
};


//
// CRD_UT10XX_cmd_ecdsa_sign
//
class CRD_UT10XX_thread_cmd_ecdsa_sign: public CRD_UT10XX_thread
{
	Q_OBJECT;
	
public:
	CRD_UT10XX_thread_cmd_ecdsa_sign(CRD_UT10XX_t *ut = 0, const char *pin = 0, 
		int key_num = -1, const char *digest = 0, int digestLen = 0);
	
	QByteArray signature();
	
protected:
	void run();
	
private:
	int mKeyNum;
	int mDigestLen;
	const char *mPin;
	const char *mDigest;
	QByteArray mSignature;
	
};


//
// CRD_UT10XX_cmd_change_user_pin
//
class CRD_UT10XX_thread_cmd_change_user_pin: public CRD_UT10XX_thread
{
	Q_OBJECT;
	
public:
	CRD_UT10XX_thread_cmd_change_user_pin(CRD_UT10XX_t *ut = 0, const char *pin = 0, 
		int pinId = -1, const char *newPin = 0);
	
protected:
	void run();
	
private:
	int mPinId;
	const char *mPin;
	const char *mNewPin;
	
};


//
// CRD_UT10XX_cmd_generate_key
//
class CRD_UT10XX_thread_cmd_generate_key: public CRD_UT10XX_thread
{
	Q_OBJECT;
	
public:
	CRD_UT10XX_thread_cmd_generate_key(CRD_UT10XX_t *ut = 0, const char *pin = 0, 
		int pinId = -1, const char *keyName = 0, int keyNum = -1);
	
protected:
	void run();
	
private:
	int mPinId;
	int mKeyNum;
	const char *mPin;
	const char *mKeyName;
	
};


//
// CRD_UT10XX_cmd_delete_key
//
class CRD_UT10XX_thread_cmd_delete_key: public CRD_UT10XX_thread
{
	Q_OBJECT;
	
public:
	CRD_UT10XX_thread_cmd_delete_key(CRD_UT10XX_t *ut = 0, const char *pin = 0, 
		int pinId = -1, int keyNum = -1);
	
protected:
	void run();
	
private:
	int mPinId;
	int mKeyNum;
	const char *mPin;
	
};


//
// CRD_UT10XX_cmd_write_public_obj
//
class CRD_UT10XX_thread_cmd_write_public_obj: public CRD_UT10XX_thread
{
	Q_OBJECT;
	
public:
	CRD_UT10XX_thread_cmd_write_public_obj(CRD_UT10XX_t *ut = 0, const char *pin = 0, 
		int pinId = -1, int objNum = -1, const char *obj = 0);
	
protected:
	void run();
	
private:
	int mPinId;
	int mObjNum;
	const char *mPin;
	const char *mObj;
	
};


//
// CRD_UT10XX_cmd_try_pin
//
class CRD_UT10XX_thread_cmd_try_pin: public CRD_UT10XX_thread
{
	Q_OBJECT;
	
public:
	CRD_UT10XX_thread_cmd_try_pin(CRD_UT10XX_t *ut = 0, const char *pin = 0, 
		int pinId = -1);
	
protected:
	void run();
	
private:
	int mPinId;
	const char *mPin;
	
};


//
// CRD_UT10XX_fs_get_file_list
//
class CRD_UT10XX_thread_fs_get_file_list: public CRD_UT10XX_thread
{
	Q_OBJECT;
	
public:
	CRD_UT10XX_thread_fs_get_file_list(CRD_UT10XX_t *ut = 0);
	
protected:
	void run();
	
};


//
// CRD_UT10XX_file_open
//
class CRD_UT10XX_thread_file_open: public CRD_UT10XX_thread
{
	Q_OBJECT;
	
public:
	CRD_UT10XX_thread_file_open(CRD_UT10XX_t *ut = 0, const char *fileName = 0);
	
	CRD_UT10XX_file_t* fileHandle();
	
protected:
	void run();
	
private:
	const char *mFileName;
	CRD_UT10XX_file_t *mFileHandle;
	
};


//
// CRD_UT10XX_file_flush
//
class CRD_UT10XX_thread_file_flush: public CRD_UT10XX_thread
{
	Q_OBJECT;
	
public:
	CRD_UT10XX_thread_file_flush(CRD_UT10XX_t *ut = 0, CRD_UT10XX_file_t *fd = 0, 
		const char *pin = 0, int pinId = -1);
	
protected:
	void run();
	
private:
	int mPinId;
	const char *mPin;
	CRD_UT10XX_file_t *mFileHandle;
	
};


//
// CRD_UT10XX_file_delete
//
class CRD_UT10XX_thread_file_delete: public CRD_UT10XX_thread
{
	Q_OBJECT;
	
public:
	CRD_UT10XX_thread_file_delete(CRD_UT10XX_t *ut = 0, CRD_UT10XX_file_t *fd = 0, 
		const char *pin = 0, int pinId = -1);
	
protected:
	void run();
	
private:
	int mPinId;
	const char *mPin;
	CRD_UT10XX_file_t *mFileHandle;
	
};


//
// Generation of certificate request
//
class CRD_UT10XX_thread_generate_cert_request: public CRD_UT10XX_thread
{
	Q_OBJECT;
	
public:
	CRD_UT10XX_thread_generate_cert_request(CRD_UT10XX_t *ut = 0, const char *pin = 0, 
		int keyId = -1, QString fileName = QString(), QStringList dn = QStringList(), QString mail = QString());

signals:
	void genFinished(const QString &fileName, const QString &mail);
	
protected:
	void run();
	
private:
	int mKeyNum;
	const char *mPin;
	QString mFileName;
	QStringList mSubject;
	QString mMail;
};

#endif
