/*
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 "DialogPlugins.h"
#include "Debugger.h"
#include "DebuggerPluginInterface.h"

#include <QHeaderView>
#include <QMetaClassInfo>

DialogPlugins::DialogPlugins(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f) {
	ui.setupUi(this);
	ui.tableWidget->verticalHeader()->hide();
}

void DialogPlugins::showEvent(QShowEvent *) {
	
	QMap<QString, QObject *> plugins = edb::v1::pluginList();
	
	ui.tableWidget->setSortingEnabled(false);
	ui.tableWidget->setRowCount(0);
	
	for(QMap<QString, QObject *>::iterator it = plugins.begin(); it != plugins.end(); ++it) {
		const int row = ui.tableWidget->rowCount();
		ui.tableWidget->insertRow(row);
	
		QString filename = it.key();
		QString pluginname;
		QString author;
		QString url;
		
		// get a QObject from the plugin
		QObject *const p = it.value();
		if(p != 0) {
			const QMetaObject *const meta = p->metaObject();
			pluginname = meta->className();
			int author_index = meta->indexOfClassInfo("author");
			if(author_index != -1) {
				author = meta->classInfo(author_index).value();
			}
			
			int url_index = meta->indexOfClassInfo("url");
			if(url_index != -1) {
				url = meta->classInfo(url_index).value();
			}
		}
				
		ui.tableWidget->setItem(row, 0, new QTableWidgetItem(filename));
		ui.tableWidget->setItem(row, 1, new QTableWidgetItem(pluginname));
		ui.tableWidget->setItem(row, 2, new QTableWidgetItem(author));
		ui.tableWidget->setItem(row, 3, new QTableWidgetItem(url));
	}
	
	ui.tableWidget->resizeRowsToContents();
	ui.tableWidget->resizeColumnsToContents();
	ui.tableWidget->setSortingEnabled(true);	
}
