/*
Copyright (C) 2006 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 "Configuration.h"
#include <QSettings>
#include <QDir>
#include <QFont>

//------------------------------------------------------------------------------
// Name: Configuration()
// Desc: constructor
//------------------------------------------------------------------------------
Configuration::Configuration() {
	readSettingsFromFile();
}

//------------------------------------------------------------------------------
// Name: ~Configuration()
// Desc: destructor
//------------------------------------------------------------------------------
Configuration::~Configuration() {
	writeSettingsToFile();
}

//------------------------------------------------------------------------------
// Name: readSettingsFromFile()
// Desc: read in the options from the file
//------------------------------------------------------------------------------
void Configuration::readSettingsFromFile() {

	QSettings settings;
	
	settings.beginGroup("General");
	closeBehaviour		= static_cast<CloseBehaviour>(settings.value("close_behaviour").toUInt());
	settings.endGroup();
	
	settings.beginGroup("Appearance");
	StackViewFont		= settings.value("stack_view_font", QFont("Monospace", 10).toString()).toString();
	MemoryDumpViewFont	= settings.value("memory_view_font", QFont("Monospace", 10).toString()).toString();
	settings.endGroup();
	
	settings.beginGroup("Debugging");
	initialBreakPoint	= static_cast<InitialBreakPoint>(settings.value("initial_breakpoint").toUInt());
	warnOnNoExecBP		= settings.value("warn_on_setting_BP_in_no_exec", true).toBool();
	settings.endGroup();
	
	settings.beginGroup("Disassembly");
	syntax				= static_cast<Syntax>(settings.value("disassembly_syntax").toUInt());
	settings.endGroup();
	
	settings.beginGroup("Directories");
	symbolPath			= settings.value("symbol_path", QDir().absolutePath()).toString();
	pluginPath			= settings.value("plugin_path", QDir().absolutePath()).toString();
	settings.endGroup();
}

//------------------------------------------------------------------------------
// Name: writeSettingsToFile()
// Desc: writes the options to the file
//------------------------------------------------------------------------------
void Configuration::writeSettingsToFile() {

	QSettings settings;
	
	settings.beginGroup("General");
	settings.setValue("close_behaviour", closeBehaviour);
	settings.endGroup();
	
	settings.beginGroup("Appearance");
	settings.setValue("stack_view_font", StackViewFont);
	settings.setValue("memory_view_font", MemoryDumpViewFont);
	settings.endGroup();
	
	settings.beginGroup("Debugging");
	settings.setValue("initial_breakpoint", initialBreakPoint);
	settings.setValue("warn_on_setting_BP_in_no_exec", warnOnNoExecBP);
	settings.endGroup();
	
	settings.beginGroup("Disassembly");
	settings.setValue("disassembly_syntax", syntax);
	settings.endGroup();
	
	settings.beginGroup("Directories");
	settings.setValue("symbol_path", symbolPath);
	settings.setValue("plugin_path", pluginPath);
	settings.endGroup();
}
