#include "setupdialog.h"
//
setupDialog::setupDialog(QDialog * parent,Qt::WFlags f  ) : QDialog(parent,f)
{
	setupUi(this);
	readSetup();
}


void setupDialog::readSetup(){
QString homePath=getenv("HOME");
QFile setupFile(homePath+"/.MediaBib/setup");

		if (setupFile.open(QIODevice::ReadOnly|QIODevice::Text)){
			QTextStream stream(&setupFile);
			QString line;
			int lineCount=0;
			while (!stream.atEnd()){
				line = stream.readLine();
				switch (lineCount){
				case 0  : basePath = line;
							break;
				case 1	: defaultDatabaseEdit->setText(line);
							break;
				case 2	: for (int i=0;i<externalPlayerComboBox->count();i++){
							if (externalPlayerComboBox->itemText(i) == line){
								externalPlayerComboBox->setCurrentIndex(i);
							}
						  }
						  	break;
				case 3	: for (int i=0;i<webLookupComboBox->count();i++){
							if (webLookupComboBox->itemText(i) == line){
								webLookupComboBox->setCurrentIndex(i);
							}
						  }
						  	break;
				case 4	: for (int i=0;i<browserComboBox->count();i++){
							if (browserComboBox->itemText(i) == line){
								browserComboBox->setCurrentIndex(i);
							}
						  }
						  	break;
				}
				lineCount++;
			}
		}
		setupFile.close();

}


void setupDialog::slot_closeButtonClicked(){
QString homePath=getenv("HOME");
QFile setupFile(homePath+"/.MediaBib/setup");

	if (setupFile.open(QIODevice::WriteOnly|QIODevice::Text)){
		QTextStream stream(&setupFile);
		stream << basePath << "\n" << defaultDatabaseEdit->text() << "\n" << externalPlayerComboBox->itemText(externalPlayerComboBox->currentIndex()) << "\n" << webLookupComboBox->itemText(webLookupComboBox->currentIndex()) << "\n" << browserComboBox->itemText(browserComboBox->currentIndex()) << endl;
	}
	setupFile.close();
	this->accept();
}

void setupDialog::slot_selectDefaultDatabaseButtonClicked(){
QString filename = QFileDialog::getOpenFileName(this, tr("Default Database"),"/",tr("Movie Database (*.mdb)"));
	if (!filename.isNull()) defaultDatabaseEdit->setText(filename);
}

//
