/* -*- mode: c++; tab-width: 4; c-basic-offset: 4 -*- */
/*
 * Copyright (C) 2005 Atmark <atmarkat _AT_ msn _DOT_ com>
 *                    AGAWA Koji <i _AT_ atty _DOT_ jp>
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <qapplication.h>
#include <qpushbutton.h>
#include <qlayout.h>
#include <qwidget.h>
#include <qlabel.h>
#include <qspinbox.h>
#include <qlineedit.h>
#include <qcheckbox.h>
#include <qcombobox.h>
#include <qlistview.h>
#include <qtabbar.h>

#include "configurationdialog.h"
#include "configuration.h"
#include "config/generalconfig.h"
#include "config/mplayerconfig.h"
#include "config/formatsconfig.h"
#include "config/hotkeyconfig.h"
#include "config/uiconfig.h"
#include "config/coverartconfig.h"

ConfigurationDialog::ConfigurationDialog(const Configuration &config, QWidget *parent, const char *name)
	: QTabDialog(parent, name, true),
	  workingConfiguration_(config)
{
	createWidgets();
}

void ConfigurationDialog::createWidgets()
{
	setCaption(tr("Configuration"));
#ifdef QTOPIA
	setWState(WState_Reserved1);
#endif

	// General config
	generalConfig_ = new GeneralConfig(workingConfiguration_, this);
	connect(generalConfig_, SIGNAL(defaultButtonClicked()), this, SLOT(setDefaultSetting()));
	connect(generalConfig_, SIGNAL(nukeMediaCacheButtonClicked()), this, SIGNAL(nukeMediaCacheButtonClicked()));
	
	mplayerConfig_ = new MPlayerConfig(workingConfiguration_, this);

	formatsConfig_ = new FormatsConfig(workingConfiguration_, this);
	
	hotKeyConfig_ = new HotKeyConfig(workingConfiguration_, this);
	
	// UI Config
	uiConfig = new UIConfig(workingConfiguration_, this);
	
	coverArtConfig = new CoverArtConfig(workingConfiguration_, this);

	// Hide buttons
#ifdef QTOPIA
	setOkButton(QString::null);
#endif
	
	// Set tabs
	addTab(generalConfig_, tr("General"));
	addTab(uiConfig, tr("User Interface"));
	addTab(hotKeyConfig_, tr("HotKey"));
	addTab(mplayerConfig_, tr("MPlayer"));
	addTab(formatsConfig_, tr("Formats"));
	addTab(coverArtConfig, tr("Cover Art"));
}

void ConfigurationDialog::showEvent(QShowEvent *)
{
#ifdef QTOPIA
	showMaximized();
#endif;
}

void ConfigurationDialog::setDefaultSetting()
{
	workingConfiguration_ = Configuration();

	generalConfig_->setStatesByConfiguration(workingConfiguration_);
	mplayerConfig_->setStatesByConfiguration(workingConfiguration_);
	formatsConfig_->setStatesByConfiguration(workingConfiguration_);
	hotKeyConfig_->setStatesByConfiguration(workingConfiguration_);
	uiConfig->setStatesByConfiguration(workingConfiguration_);
	coverArtConfig->setStatesByConfiguration(workingConfiguration_);
}

void ConfigurationDialog::showMPlayerConfig()
{
	showPage(mplayerConfig_);
	mplayerConfig_->setFocus();
	mplayerConfig_->focusMPlayerPathWidget();
}

int ConfigurationDialog::exec()
{
	loadWidgetPosition(this, workingConfiguration_.getConfigName(), "ConfigDialog");
	int result = QTabDialog::exec();
	formatsConfig_->saveStatesToConfiguration();
	saveWidgetPosition(this, workingConfiguration_.getConfigName(), "ConfigDialog");	
	return result;
}