/*
Copyright (C) 2006 Evan Teran
                   eteran@alum.rit.edu

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 program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#ifndef MEMORY_REGIONS_20060501_H_
#define MEMORY_REGIONS_20060501_H_

#include <QAbstractItemModel>
#include <QVector>
#include <QString>
#include "Types.h"
#include "DebugEventHandlerInterface.h"

class BackupInfo;

class MemoryRegions : public QAbstractItemModel {
	Q_OBJECT
public:
	virtual QVariant data(const QModelIndex &index, int role) const;
	virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
	virtual QModelIndex parent(const QModelIndex &index) const;
	virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
	virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
	virtual QVariant headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
	
public:
	MemoryRegions();
	virtual ~MemoryRegions();
	
	void setPID(edb::pid_t pid);
	void clear();

	class Region : public DebugEventHandlerInterface {
	
		friend class MemoryRegions;
	public:
		Region() : start(0), end(0), base(0), m_BackupInfo(0), m_Lock(0), m_Permissions(0) {
		}
		
		virtual ~Region();

	public:
		Region(const Region &o) : DebugEventHandlerInterface(), start(o.start), end(o.end), base(o.base), name(o.name), m_BackupInfo(0), m_Lock(0), m_Permissions(o.m_Permissions) {
		}
		
		Region &operator=(const Region &o) {
			if(&o != this) {
				start			= o.start;
				end				= o.end;
				base			= o.base;
				name			= o.name;
				m_Permissions	= o.m_Permissions;
				m_BackupInfo	= 0;
				m_Lock			= 0;
			}
			return *this;
		}
		
		bool operator==(const Region &o) const {
			return 
				start			== o.start &&
				end				== o.end &&
				base			== o.base &&
				name			== o.name &&
				m_Permissions	== o.m_Permissions;
		}
		
		bool operator!=(const Region &o) const {
			return !(operator==(o));
		}

	public:
		bool accessible() const	{ return readable() || writable() || executable(); }
		bool readable() const	{ return (m_Permissions & READ) != 0; }
		bool writable() const	{ return (m_Permissions & WRITE) != 0; }
		bool executable() const { return (m_Permissions & EXEC) != 0; }
		int size() const		{ return end - start; }
		
		void setPermissions(bool read, bool write, bool execute);
		
	public:
		virtual void handleEvent(DebugEvent event);
		
	private:
		void setPermissions(bool read, bool write, bool execute, edb::address_t tempAddress);
		
	public:
		edb::address_t	start;
		edb::address_t	end;
		edb::address_t	base;
		QString			name;
		
	private:
		BackupInfo 	*m_BackupInfo;
		int			m_Lock;
		quint8		m_Permissions;
		
	private:
		static const unsigned int READ;
		static const unsigned int WRITE;
		static const unsigned int EXEC;
	};
	
	const QVector<Region> &regions() const { return m_Regions; }
	
	void sync();
	
	bool findRegion(edb::address_t address, Region &region) const;
	bool findRegion(edb::address_t address) const;
	
private:
	static bool proccessMapLine(const QString &line, Region &region);

private:
	edb::pid_t		m_PID;
	QVector<Region>	m_Regions;
};

#endif
