#include <qvgroupbox.h>
#include <qhgroupbox.h>
#include <qhbuttongroup.h>
#include <qradiobutton.h>
#include <qvbox.h>
#include <qpushbutton.h>
#include <qtextcodec.h>
#include <qfileinfo.h>
#include "qtkfiledialog.h"
#include "coverartconfig.h"

CoverArtConfig::CoverArtConfig(Configuration& configuration, QWidget *parent, const char *name)
	: QScrollView(parent, name),
	  config_(configuration)
{
	createWidgets();
	setStatesByConfiguration(config_);
}

void CoverArtConfig::setStatesByConfiguration(const Configuration& config)
{
	if (&config != &config_)
		config_ = config;

	coverArtDirectory_->setText(config.coverArtDirectory);
}

void CoverArtConfig::createWidgets()
{
	setFrameStyle(NoFrame);
	setMargin(4);
	setResizePolicy(AutoOneFit);

	QVBox *bigbox = new QVBox(viewport());
	bigbox->setSpacing(4);
	addChild(bigbox);

	QHGroupBox *pathGroup = new QHGroupBox(tr("Cover Art location"), bigbox);
	coverArtDirectory_ = new QLineEdit(pathGroup);
	connect(coverArtDirectory_, SIGNAL(textChanged(const QString&)),
			this, SLOT(setCoverArtDirectory(const QString&)));
	QPushButton *openButton = new QPushButton(tr("..."), pathGroup);
	connect(openButton, SIGNAL(clicked()), this, SLOT(execCoverArtDirectoryDialog()));
}

void CoverArtConfig::execCoverArtDirectoryDialog()
{
	QTKFileDialog dialog(this);

	if (!coverArtDirectory_->text().isEmpty())
		dialog.setDirectory(QFileInfo(coverArtDirectory_->text()).filePath());

	dialog.setHomeDirectoryCdUpEnable(true);
	dialog.setSystemFile(true);
	dialog.setHiddenFile(true);
	dialog.setDirectorySelectMode(true);

	loadWidgetPosition(&dialog, config_.getConfigName(), "FileDialog");	
	
	if (dialog.exec() == QDialog::Accepted)
		coverArtDirectory_->setText(dialog.getCurrentDir());
	
	saveWidgetPosition(&dialog, config_.getConfigName(), "FileDialog");	
}
