/*
 * eventdb.h - asynchronous I/O event database
 * Copyright (C) 2001, 2002  Justin Karneges
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#ifndef EVENTDB_H
#define EVENTDB_H

#include<qobject.h>
#include<qtimer.h>
#include<qfile.h>
#include<qptrlist.h>
#include"psievent.h"


class EDBItem
{
public:
	EDBItem(PsiEvent *, const QString &id, const QString &nextId, const QString &prevId);
	~EDBItem();

	PsiEvent *event() const;
	const QString & id() const;
	const QString & nextId() const;
	const QString & prevId() const;

private:
	QString v_id, v_prevId, v_nextId;
	PsiEvent *e;
};

class EDBResult : public QPtrList<EDBItem>
{
public:
	EDBResult() {}
};

class EDB;
class EDBHandle : public QObject
{
	Q_OBJECT
public:
	enum { Read, Write };
	EDBHandle(EDB *);
	~EDBHandle();

	// operations
	void getLatest(const Jid &, int len);
	void getOldest(const Jid &, int len);
	void get(const Jid &jid, const QString &id, int direction, int len);
	void find(const QString &, const Jid &, const QString &id, int direction);
	void append(const Jid &, PsiEvent *);

	bool busy() const;
	const EDBResult *result() const;
	bool writeSuccess() const;
	int lastRequestType() const;

signals:
	void finished();

private:
	class Private;
	Private *d;

	friend class EDB;
	void edb_resultReady(EDBResult *);
	void edb_writeFinished(bool);
	int listeningFor() const;
};

class EDB : public QObject
{
	Q_OBJECT
public:
	enum { Forward, Backward };
	EDB();
	virtual ~EDB()=0;

protected:
	int genUniqueId() const;
	virtual int getLatest(const Jid &, int len)=0;
	virtual int getOldest(const Jid &, int len)=0;
	virtual int get(const Jid &jid, const QString &id, int direction, int len)=0;
	virtual int append(const Jid &, PsiEvent *)=0;
	virtual int find(const QString &, const Jid &, const QString &id, int direction)=0;
	void resultReady(int, EDBResult *);
	void writeFinished(int, bool);

private:
	class Private;
	Private *d;

	friend class EDBHandle;
	void reg(EDBHandle *);
	void unreg(EDBHandle *);

	int op_getLatest(const Jid &, int len);
	int op_getOldest(const Jid &, int len);
	int op_get(const Jid &, const QString &id, int direction, int len);
	int op_find(const QString &, const Jid &, const QString &id, int direction);
	int op_append(const Jid &, PsiEvent *);
};

class EDBFlatFile : public EDB
{
	Q_OBJECT
public:
	EDBFlatFile();
	~EDBFlatFile();

	int getLatest(const Jid &, int len);
	int getOldest(const Jid &, int len);
	int get(const Jid &jid, const QString &id, int direction, int len);
	int find(const QString &, const Jid &, const QString &id, int direction);
	int append(const Jid &, PsiEvent *);

	class File;

private slots:
	void performRequests();
	void file_timeout();

private:
	class Private;
	Private *d;

	File *findFile(const Jid &) const;
	File *ensureFile(const Jid &);
};

class EDBFlatFile::File : public QObject
{
	Q_OBJECT
public:
	File(const Jid &_j);
	~File();

	int total() const;
	void touch();
	PsiEvent *get(int);
	bool append(PsiEvent *);

signals:
	void timeout();

private slots:
	void timer_timeout();

public:
	Jid j;
	QString fname;
	QFile f;
	bool valid;
	QTimer *t;

	class Private;
	Private *d;

private:
	PsiEvent *lineToEvent(const QString &);
	QString eventToLine(PsiEvent *);
	void ensureIndex();
};

#endif
