/*
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 <string>
#include <fstream>
#include <sstream>
#include <iostream>

#include <QMap>
#include <QHeaderView>
#include <QDir>
#include <QStringList>
#include <QString>
#include <QFileInfo>

//------------------------------------------------------------------------------
// 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 = true;
	for(QString::const_iterator i = s.begin(); ret && i != s.end(); i++) {
		ret = i->isDigit();
	}
	
	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<int, std::string> procs;
	
	QDir procDir("/proc/");
	QFileInfoList entries = procDir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
	for(QFileInfoList::const_iterator i = entries.begin(); i != entries.end(); ++i) {
		if(isNumeric(i->fileName())) {
			QDir proc(i->absoluteFilePath());
			
			QString status = proc.absolutePath() + "/status";
			
			const char *const strStatus = qPrintable(status);
			std::ifstream file(strStatus);
			if(file) {
				const std::string strFile = std::string(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
				std::istringstream ss(strFile);
				std::string name;					
				ss.ignore(INT_MAX, ':');
				ss >> name;

				procs.insert(i->fileName().toUInt(), name);
			}
		}
	}

	ui.tableWidget->setSortingEnabled(false);
	ui.tableWidget->setRowCount(0);
	for(QMap<int, std::string>::const_iterator i = procs.begin(); i != procs.end(); ++i) {
		const QString procName = i.value().c_str();
		
		if(filter.isEmpty() || procName.toLower().contains(filter.toLower())) {
			int row = ui.tableWidget->rowCount();
			ui.tableWidget->insertRow(row);
			ui.tableWidget->setItem(row, 0, new QTableWidgetItem(QString().sprintf("%d",i.key())));
	        ui.tableWidget->setItem(row, 1, new QTableWidgetItem(procName));
		}
	}
	ui.tableWidget->setSortingEnabled(true);
}

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