/*
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.
*/

#include "DialogAttach.h"
#include "Types.h"

#include <QMap>
#include <QHeaderView>
#include <QDir>
#include <QTextStream>

struct ProcessInfo {
	edb::pid_t	pid;
	edb::uid_t	uid;
	QString		name;
};

//------------------------------------------------------------------------------
// Name: DialogAttach(QWidget *parent) : QDialog(parent)
// Desc: constructor
//------------------------------------------------------------------------------
DialogAttach::DialogAttach(QWidget *parent) : QDialog(parent) {
	ui.setupUi(this);
	ui.tableWidget->verticalHeader()->hide();
	ui.tableWidget->horizontalHeader()->setStretchLastSection(true);
}

//------------------------------------------------------------------------------
// Name: isNumeric(const QString &s)
// Desc: returns true if the string only contains decimal digits
//------------------------------------------------------------------------------
bool DialogAttach::isNumeric(const QString &s) {
	bool ret = false;
	foreach(QChar ch, s) {
		ret = ch.isDigit();
		if(!ret) {
			break;
		}
	}

	return ret;
}

//------------------------------------------------------------------------------
// Name: on_txtSearch_textChanged(const QString &text)
// Desc: 
//------------------------------------------------------------------------------
void DialogAttach::on_txtSearch_textChanged(const QString &text) {
	updateList(text);
}

//------------------------------------------------------------------------------
// Name: updateList(const QString &filter)
// Desc: 
//------------------------------------------------------------------------------
void DialogAttach::updateList(const QString &filter) {

	QMap<edb::pid_t, ProcessInfo> procs;

	// FILL up procs

	const bool filterUID = ui.chkFilterUID->isChecked();
	const QString lowerFilter = filter.toLower();
	ui.tableWidget->setSortingEnabled(false);
	ui.tableWidget->setRowCount(0);

	foreach(ProcessInfo procInfo, procs) {

		const QString procName = procInfo.name;
		
		if(filter.isEmpty() || procName.toLower().contains(lowerFilter)) {	
			if(!filterUID || procInfo.uid == myUID) {
				const int row = ui.tableWidget->rowCount();
				ui.tableWidget->insertRow(row);
				
				QTableWidgetItem *const itemPID = new QTableWidgetItem;
				itemPID->setData(Qt::DisplayRole, procInfo.pid);
				
				QTableWidgetItem *const itemUID = new QTableWidgetItem;
				itemUID->setData(Qt::DisplayRole, procInfo.uid);
				
				ui.tableWidget->setItem(row, 0, itemPID);
				ui.tableWidget->setItem(row, 1, itemUID);
		        ui.tableWidget->setItem(row, 2, new QTableWidgetItem(procInfo.name));
			}
		}
	}
	ui.tableWidget->setSortingEnabled(true);
}

//------------------------------------------------------------------------------
// Name: showEvent(QShowEvent *event)
// Desc: 
//------------------------------------------------------------------------------
void DialogAttach::showEvent(QShowEvent *event) {
	Q_UNUSED(event);
	updateList(ui.txtSearch->text());
}

//------------------------------------------------------------------------------
// Name: on_chkFilterUID_clicked(bool checked)
// Desc: 
//------------------------------------------------------------------------------
void DialogAttach::on_chkFilterUID_clicked(bool checked) {
	Q_UNUSED(checked);
	updateList(ui.txtSearch->text());
}
