#ifndef __AVD_Log2UI_H__
#define __AVD_Log2UI_H__

#include <QtCore/QtDebug>
#include <QtCore/QObject>
#include "api/errorcode.h"
#include "api/defines.h"

class MsgHandlerWapper :public QObject
{
	Q_OBJECT
public:
	static MsgHandlerWapper * instance();

signals:
	void message(QtMsgType type, const QString &msg);

private:
	MsgHandlerWapper();
	static MsgHandlerWapper * m_instance;
};

#include <QTextCodec>  
extern QTextCodec *g_textcodec;
void initTextCodec();


//#define Utf8Str(src) g_textcodec->toUnicode(src).toUtf8().data()
#define Utf8Str(src) QString::fromWCharArray(L##src)

QString OutputWithRet(Result ret);
int GetRandomNumber();
std::string GetRandomNumberWithPrefix(const std::string& pre);

class QAbstractButton;
void ButtonChecked(QAbstractButton* btn, bool checked, const QString& text);
class QListWidget;
void QListWidgetAddItem(QListWidget* lst, const char* name, const char* id);
QString GetLogFile();

template<typename HTDObject>
class HTDObjectSet {
public:
	typedef std::vector<HTDObject*> HTDObjectsType;
	typedef typename HTDObjectsType::iterator iterator;
public:
	void add(HTDObject* item) {
		m_objects.push_back(item);
	}
	void remove(AVDHandle handle) {
		iterator it = find(handle);
		if (m_objects.end() != it) {
			delete *it;
			m_objects.erase(it);
		}
	}
	bool hasObject(AVDHandle handle) {
		return (find(handle) != m_objects.end());
	}
	iterator find(AVDHandle handle) {
		iterator it = m_objects.begin();
		for (; m_objects.end() != it; ++it) {
			if ((*it)->Handle() == handle) {
				return it;
			}
		}
		return m_objects.end();
	}
	HTDObject* getItem(AVDHandle handle) {
		iterator it = find(handle);
		return m_objects.end() == it ? NULL : (*it);
	}
protected:
	HTDObjectsType m_objects;
};

#endif // __AVD_Log2UI_H__
