/*
Copyright (C) 2006 - 2009 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 "DialogAttachUNIX.h"
#include "Types.h"

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

#include <sys/types.h>
#include <unistd.h>

//------------------------------------------------------------------------------
// Name: DialogAttachUNIX(QWidget *parent)
// Desc: constructor
//------------------------------------------------------------------------------
DialogAttachUNIX::DialogAttachUNIX(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 DialogAttachUNIX::isNumeric(const QString &s) {
	Q_FOREACH(QChar ch, s) {
		if(!ch.isDigit()) {
			return false;
		}
	}

	return true;
}

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

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

	QMap<edb::pid_t, ProcessInfo> procs = findProcesses();

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

	Q_FOREACH(const ProcessInfo &procInfo, procs) {

		const QString procName = procInfo.name;
		
		if(filter.isEmpty() || procName.toLower().contains(lowerFilter)) {	
			if(!filterUID || procInfo.uid == user_id) {
				const int row = ui.tableWidget->rowCount();
				ui.tableWidget->insertRow(row);
				
				QTableWidgetItem *const itemPID = new QTableWidgetItem;
				itemPID->setData(Qt::DisplayRole, procInfo.pid);
				
				QTableWidgetItem *itemUID;
				if(!procInfo.user.isEmpty()) {
					itemUID = new QTableWidgetItem(procInfo.user);
				} else {
					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 DialogAttachUNIX::showEvent(QShowEvent *event) {
	Q_UNUSED(event);
	updateList(ui.txtSearch->text());
}

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

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
edb::pid_t DialogAttachUNIX::selectedPID(bool &ok) const {
	const QList<QTableWidgetItem *> sel = ui.tableWidget->selectedItems();
	if(sel.size() != 0) {
		ok = true;
		return sel.first()->text().toUInt();
	} else {
		ok = false;
		return edb::pid_t();
	}
}
