#include "multiadddialog.h"
//
multiAddDialog::multiAddDialog(QWidget * parent, Qt::WFlags f, QString filename) : QDialog(parent,f)
{
	setupUi(this);
	databaseLineEdit->setText(filename);
	genre << "Action" << "Science Fiction" << "Phantasie" << "Drama" << "Comedy" << "Thriller" << "Music" << "Documentation" << "Education" << "misc";
	selectStartPathButton->setIcon(QIcon(":/src/icons/folder.png"));
	databaseSelectButton->setIcon(QIcon(":/src/icons/folder.png"));
	
QFile file((QString)getenv("HOME")+(QString)"/.MediaBib/genre.txt");
QString line;

	if (file.open(QIODevice::ReadOnly|QIODevice::Text)){
		QTextStream stream(&file);
		while (!stream.atEnd()){
			line = stream.readLine();
			genreComboBox->addItem(line);
		}
		file.close();
	} else {
		genreComboBox->insertItems(0,genre);
	}
	if (genreComboBox->findText("auto") == -1){
		genreComboBox->insertItem(0,"auto");
		genreComboBox->setCurrentIndex(0);
	}
	addProcess = new QProcess(this);
	connect(addProcess,SIGNAL(readyReadStandardOutput()),this,SLOT(slot_readyReadStandardOutput()));
	connect(addProcess,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(slot_processFinished()));
}

void multiAddDialog::slot_startSearchButtonClicked(){
	if (databaseLineEdit->text() != ""){
		if (searchLineEdit->text() != ""){
			processOutput->setAlignment(Qt::AlignLeft);
			elementState(false);
			QString program = (QString)getenv("HOME") + "/bin/createmoviedb.sh";
			QStringList param;
			param << searchLineEdit->text() << genreComboBox->itemText(genreComboBox->currentIndex()) << databaseLineEdit->text();
			addProcess->start(program,param);
			processOutput->setText("searching ...");
		} else {
			QMessageBox::information(this,tr("WARINING"),tr("Please select a path to start the search from."));
		}
	} else {
		QMessageBox::information(this,tr("WARNING"),tr("Please select a database to store the data in"));
	}
}
	
void multiAddDialog::slot_selectDatabaseButtonClicked(){
QString	filename = QFileDialog::getOpenFileName(this, tr("Select Database"),"/",tr("Movie Database (*.mdb)"));
	if (!filename.isNull()){
		databaseLineEdit->setText(filename);
	}}

void multiAddDialog::slot_searchFromButtonClicked(){
QString	dir = QFileDialog::getExistingDirectory(this,tr("Select Directory"),"/",QFileDialog::ShowDirsOnly|QFileDialog::DontResolveSymlinks);
	if (!dir.isNull()){
		searchLineEdit->setText(dir);
	}
}

void multiAddDialog::slot_readyReadStandardOutput(){
QString line;
	line = (QString)addProcess->readAllStandardOutput();
	processOutput->setText((QString)tr("found: ") + line);
}

void multiAddDialog::slot_processFinished(){
	processOutput->setAlignment(Qt::AlignCenter);
	processOutput->setText(tr("* * * * Search Process finished * * * *"));
	elementState(true);
}

void multiAddDialog::elementState(bool state){
	searchLineEdit->setEnabled(state);
	genreComboBox->setEnabled(state);
	selectStartPathButton->setEnabled(state);
	databaseLineEdit->setEnabled(state);
	databaseSelectButton->setEnabled(state);
	startSearchButton->setEnabled(state);
	closeButton->setEnabled(state);
	genreSetupButton->setEnabled(state);
}

void multiAddDialog::slot_genreSetupButtonClicked(){
genreSetup * GenreSetup = new genreSetup();
	GenreSetup->exec();
	
QString basePath = (QString)getenv("HOME")+(QString)"/.MediaBib/genre.txt";
QFile file(basePath);
QString line;

	if (file.open(QIODevice::ReadOnly|QIODevice::Text)){
		QTextStream stream(&file);
		genreComboBox->clear();
		while (!stream.atEnd()){
			line = stream.readLine();
			genreComboBox->addItem(line);
		}
		file.close();
	}
}	

//
