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

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

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

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
bool DialogAttach::isNumeric(const std::string &s) {
	for(std::string::const_iterator i = s.begin(); i != s.end(); i++) {
		if(!std::isdigit(*i)) {
			return false;
		}
	}
	return true;
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
void DialogAttach::showEvent(QShowEvent *event) {
	(void)event;
	std::map<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(qPrintable(i->fileName()))) {
			QDir proc(i->absoluteFilePath());
			
			QString status = proc.absolutePath() + "/status";
			
			std::ifstream file(qPrintable(status));
			if(file) {
				std::istringstream ss((std::string(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>())));
				std::string name;					
				ss.ignore(INT_MAX, ':');
				ss >> name;

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

	ui.tableWidget->setSortingEnabled(false);
	ui.tableWidget->setRowCount(0);
	for(std::map<int, std::string>::const_iterator i = procs.begin(); i != procs.end(); ++i) {
		int row = ui.tableWidget->rowCount();
		ui.tableWidget->insertRow(row);
		ui.tableWidget->setItem(row, 0, new QTableWidgetItem(QString().sprintf("%d",i->first)));
        ui.tableWidget->setItem(row, 1, new QTableWidgetItem(i->second.c_str()));
	}
	ui.tableWidget->setSortingEnabled(true);
}
