/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "resources.h"
#include "kdevmonconfig.h"

#include <kapplication.h>
#include <kglobal.h>
#include <kstandarddirs.h>
#include <kconfiggroup.h>
#include <kdebug.h>
#include <qcolor.h>

KdevmonConfig *KdevmonConfig::m_config = 0L;

KdevmonConfig *KdevmonConfig::getConfig()
{
    if( m_config == 0L )
        m_config = new KdevmonConfig();
    return m_config;
}

KdevmonConfig::KdevmonConfig() {	
	commonConfig = new CommonConfig();
	graphConfig = new GraphConfig();
	mainFrameConfig = new MainFrameConfig();
	dockFrameConfig = new DockFrameConfig();
// 	popupFrameConfig = new PopupFrameConfig();
	statsFrameConfig = new StatsFrameConfig();
	
	commonConfig->configFile = QString::null;
	m_bIsInitialized = false;
}

void KdevmonConfig::readSettings()
{
//     if( commonConfig->configFile == QString::null )
//         commonConfig->configFile = readNameOfConfigFile();
//     KConfig config( commonConfig->configFile );

	KConfigGroup config = KGlobal::config()->group("General");
	commonConfig->colorIn = QVariant(config.readEntry( "colorIn", QColor("red") )).value<QColor>();
	commonConfig->colorOut = QVariant(config.readEntry( "colorOut", QColor("white"))).value<QColor>();
	commonConfig->refreshTime = QVariant(config.readEntry( "refreshTime", 1000 )).toInt();
	commonConfig->unit = (Common::Unit) QVariant(config.readEntry( "unit", (int)Common::UNIT_BYTE )).toInt();
	commonConfig->smoothFactor = QVariant(config.readEntry( "smoothFactor", 8 )).toInt();
	
	config = KGlobal::config()->group( "Diagram" );
	graphConfig->showGrid = QVariant(config.readEntry( "showGrid", true )).toBool();
	graphConfig->scalingMode = (Common::ScalingMode) QVariant(config.readEntry( "scalingMode", (int)Common::MODE_NORMAL )).toInt();
	graphConfig->graphPosition = (Common::GraphPosition) QVariant(config.readEntry( "graphPosition", (int)Common::POS_TOP_BOTTOM )).toInt();
	graphConfig->graphStyle = (Common::GraphStyle) QVariant(config.readEntry( "graphStyle", (int)Common::STYLE_SHADED )).toInt();
	graphConfig->scaling = QVariant(config.readEntry( "scaling", 56000 )).toInt();
	graphConfig->device = QVariant(config.readEntry( "device", "eth0" )).toString();
	
	config = KGlobal::config()->group( "MainFrame" );
	mainFrameConfig->bgColor = QVariant(config.readEntry( "bgColor", QColor("black"))).value<QColor>();
	mainFrameConfig->gridColor = QVariant(config.readEntry( "gridColor", QColor("gray"))).value<QColor>();
	mainFrameConfig->transparent = QVariant(config.readEntry( "transparent", false )).toBool();
	mainFrameConfig->showTooltips = QVariant(config.readEntry( "showTooltips", true )).toBool();
	mainFrameConfig->showStatsBar = QVariant(config.readEntry( "showStatsBar", true )).toBool();
	mainFrameConfig->showDecorations = QVariant(config.readEntry( "showDecorations", true )).toBool();
	mainFrameConfig->geometry = QVariant(config.readEntry( "geometry", QRect(0, 0, 300, 100) )).toRect();
	mainFrameConfig->isVisible = QVariant(config.readEntry( "isVisible", true )).toBool();
	
	config = KGlobal::config()->group( "DockFrame" );
	dockFrameConfig->bgColor = QVariant(config.readEntry( "bgColor", QColor("black"))).value<QColor>();
	dockFrameConfig->isVisible = QVariant(config.readEntry( "isVisible", true )).toBool();
	dockFrameConfig->showText = QVariant(config.readEntry( "showText", false )).toBool();
	dockFrameConfig->TextSize = QVariant(config.readEntry( "TextSize", 5 )).toInt();
	
//	config = KGlobal::config()->group("PopupFrame" );
//	popupFrameConfig->position = QVariant(config.readPointEntry( "position", new QPoint(100, 100) );
//	popupFrameConfig->bgColor = QVariant(config.readColorEntry( "bgColor", new QColor("black").value<QColor>()) );
//	popupFrameConfig->fgColor = QVariant(config.readColorEntry( "fgColor", new QColor("lightGray").value<QColor>()) );
//
	config = KGlobal::config()->group( "StatsFrame" );
	statsFrameConfig->position = QVariant(config.readEntry( "position", QPoint(100,100) )).toPoint();
	statsFrameConfig->bgColor = QVariant(config.readEntry( "bgColor", QColor("black"))).value<QColor>();
	
    m_bIsInitialized = true;
}

void KdevmonConfig::writeSettings()
{
//     if( commonConfig->configFile == QString::null )
//         commonConfig->configFile = readNameOfConfigFile();
//     KConfig config( commonConfig->configFile );
	KConfigGroup configgroup = KGlobal::config()->group( "General" );
	configgroup.writeEntry( "colorIn", commonConfig->colorIn );
	configgroup.writeEntry( "colorOut", commonConfig->colorOut );
	configgroup.writeEntry( "refreshTime", commonConfig->refreshTime );
	configgroup.writeEntry( "unit", (int) commonConfig->unit );
	configgroup.writeEntry( "smoothFactor", commonConfig->smoothFactor );
		
	configgroup = KGlobal::config()->group( "Diagram" );
	configgroup.writeEntry( "showGrid", graphConfig->showGrid );
	configgroup.writeEntry( "scalingMode", (int) graphConfig->scalingMode );
	configgroup.writeEntry( "graphPosition", (int) graphConfig->graphPosition );
	configgroup.writeEntry( "graphStyle", (int) graphConfig->graphStyle );
	configgroup.writeEntry( "scaling", int(graphConfig->scaling) );
	configgroup.writeEntry( "device", graphConfig->device );
	
	configgroup = KGlobal::config()->group( "MainFrame" );
	configgroup.writeEntry( "bgColor", mainFrameConfig->bgColor );
	configgroup.writeEntry( "gridColor", mainFrameConfig->gridColor );
	configgroup.writeEntry( "showTooltips", mainFrameConfig->showTooltips );
	configgroup.writeEntry( "showStatsBar", mainFrameConfig->showStatsBar );
	configgroup.writeEntry( "showDecorations", mainFrameConfig->showDecorations );
	configgroup.writeEntry( "transparent", mainFrameConfig->transparent );
	configgroup.writeEntry( "geometry", mainFrameConfig->geometry );
	configgroup.writeEntry( "isVisible", mainFrameConfig->isVisible );
	
	configgroup = KGlobal::config()->group( "DockFrame" );
	configgroup.writeEntry( "bgColor", dockFrameConfig->bgColor );
	configgroup.writeEntry( "isVisible", dockFrameConfig->isVisible );
	configgroup.writeEntry( "showText", dockFrameConfig->showText );
	configgroup.writeEntry( "TextSize", dockFrameConfig->TextSize );
	
//	config = KGlobal::config()->group( "PopupFrame" );
//	configgroup.writeEntry( "position", popupFrameConfig->position );
//	configgroup.writeEntry( "bgColor", popupFrameConfig->bgColor );
//	configgroup.writeEntry( "fgColor", popupFrameConfig->fgColor );
//
	configgroup = KGlobal::config()->group( "StatsFrame" );
	configgroup.writeEntry( "position", statsFrameConfig->position );
	configgroup.writeEntry( "bgColor", statsFrameConfig->bgColor );
	
	configgroup.sync();

// 	writeNameOfConfigFile( commonConfig->configFile );
}

struct CommonConfig *KdevmonConfig::getCommonConfig()
{
	if( ! m_bIsInitialized )
	    readSettings();
	return commonConfig;
}

struct GraphConfig *KdevmonConfig::getGraphConfig()
{
	if( ! m_bIsInitialized )
	    readSettings();
	return graphConfig;
}

struct MainFrameConfig *KdevmonConfig::getMainFrameConfig()
{
	if( ! m_bIsInitialized )
	    readSettings();
	return mainFrameConfig;
}

struct DockFrameConfig *KdevmonConfig::getDockFrameConfig()
{
	if( ! m_bIsInitialized )
	    readSettings();
	return dockFrameConfig;
}

//struct PopupFrameConfig *KdevmonConfig::getPopupFrameConfig()
//{
//	if( ! m_bIsInitialized )
//	    readSettings();
//	return popupFrameConfig;
//}
//
struct StatsFrameConfig *KdevmonConfig::getStatsFrameConfig()
{
	if( ! m_bIsInitialized )
		readSettings();
	return statsFrameConfig;
}
	
QString KdevmonConfig::readNameOfConfigFile()
{
// 	QString defaultConfigFile = KGlobal::dirs()->localkdedir()
// 	    + "share/apps/kdevmon/defaultrc";
// 	KConfig *dummyConfig = KApplication::kApplication()->config();
// 	dummyConfig->setGroup( "Common" );
//     return dummyConfig->readEntry( "configFile", defaultConfigFile );
    return QString();
}

void KdevmonConfig::writeNameOfConfigFile( const QString& configFile )
{
// 	KConfig *dummyConfig = KApplication::kApplication()->config();
// 	dummyConfig->setGroup( "Common" );
//     dummyConfig->writeEntry( "configFile", configFile );
//     dummyConfig->sync();
}

