/*
 *
 * Copyright (C) 2002 George Staikos <staikos@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */


#include <kconfig.h>
#include <kdebug.h>

#include "cfgdata.h"

ConfigData::ConfigData(KConfig* config) 
           : _cfg (config)
{
    defaults();
}

ConfigData::~ConfigData()
{
    save ();
    _cfg->sync ();
}

ConfigData::ConfigData(const ConfigData& cfg)
{
    *this = cfg;
}

ConfigData const &ConfigData::operator=(ConfigData const &other)
{
    if (this != &other) {
        frameRate = other.frameRate;
        landscape = other.landscape;
        mirror = other.mirror;
        maxResolution = other.maxResolution;
        autoStart = other.autoStart;
        videoDesktopEnabled = other.videoDesktopEnabled;
        stayOnTop = other.stayOnTop;
        autoScaleEnabled = other.autoScaleEnabled;
        showSelectedOnly = other.showSelectedOnly;
        nps = other.nps;
        prevDev = other.prevDev;
        prevSrc = other.prevSrc;
        channelFile = other.channelFile;
        lastSource = other.lastSource;
        
        hue = other.hue;
        colour = other.colour;
        brightness = other.brightness;
        whiteness = other.whiteness;
        contrast = other.contrast;
        firstTime = other.firstTime;
        
        lastChannel = other.lastChannel;
        fixAR = other.fixAR;
        ARmode = other.ARmode;
        disableScreenSaver = other.disableScreenSaver;
        screenSaverMode = other.screenSaverMode;
        useListingsURL = other.useListingsURL;
        listingsURL = other.listingsURL;
        volumeRestoreDelay = other.volumeRestoreDelay;
	topLevelPos = other.topLevelPos;
	topLevelSize = other.topLevelSize;
        
        kdDebug() << "ConfigData::operator = : called..." << endl;
        
        _cfg = other._cfg;
    } // if
    return(*this);
} // operator=


void ConfigData::defaults()
{
    defaultsGeneral();
    defaultsDevice();
    defaultsPicture();
    defaultsChannels();
} // defaults

void ConfigData::defaultsGeneral()
{
    frameRate = 22;
    landscape = false;
    mirror = false;
    maxResolution = false;
    autoStart = true;
    videoDesktopEnabled = true;
    stayOnTop = false;
    firstTime = true;
    fixAR = false;
    ARmode = AR_HEIGHT_TO_WIDTH;
    disableScreenSaver = true;
    screenSaverMode = SAVER_RUNNING;
    topLevelPos = QPoint();
    topLevelSize = QSize(400, 300);
    useListingsURL = true;
    listingsURL = QString( "http://www.ananova.com/tv/" );
} // defaultsGeneral

void ConfigData::defaultsDevice()
{
    nps = 0;
    return;
} // defaultsDevice


void ConfigData::defaultsPicture()
{
    brightness = hue = colour = contrast = whiteness = 32768;
    autoScaleEnabled = true;
} // defaultsPicture


void ConfigData::defaultsChannels()
{
    showSelectedOnly = true;
} // defaultsChannels

int ConfigData::load ()
{
    if (!_cfg)
        return -1;
        
    _cfg->setGroup("General");
    firstTime = _cfg->readBoolEntry("First Time", true);
    frameRate = _cfg->readNumEntry("Frame Rate", 22);
    autoStart = _cfg->readBoolEntry("Auto Start", true);
    maxResolution = _cfg->readBoolEntry("Capture at Max Res", false);
    landscape = _cfg->readBoolEntry("Display Landscape", false);
    mirror = _cfg->readBoolEntry("Mirror image", false);
    videoDesktopEnabled = _cfg->readBoolEntry("Enable Video Desktop", true);
    stayOnTop = _cfg->readBoolEntry("Stay on Top", false);
    autoScaleEnabled = _cfg->readBoolEntry("Auto Scale Brightness", true);
    showSelectedOnly = _cfg->readBoolEntry("Show Only Selected Channels", true);
    prevDev = _cfg->readEntry("Previous Device", QString::null);
    prevSrc = _cfg->readEntry("Previous Source", QString::null);
    nps = _cfg->readNumEntry("NTSC PAL SECAM", 0);
    fixAR = _cfg->readBoolEntry("Fix Aspect Ratio", false);
    ARmode = _cfg->readNumEntry("Aspect Ratio Mode", AR_HEIGHT_TO_WIDTH);
    disableScreenSaver = _cfg->readBoolEntry("Disable Screensaver", true);
    screenSaverMode = _cfg->readNumEntry("Screensaver Mode", SAVER_RUNNING);
    topLevelPos = _cfg->readPointEntry("TopLevel Pos");
    QSize defS(400, 300);
    topLevelSize = _cfg->readSizeEntry("TopLevel Size", &defS);
    useListingsURL = _cfg->readBoolEntry("Use Listings URL", true);
    listingsURL = _cfg->readEntry("Listings URL", QString::null);
    volumeRestoreDelay = _cfg->readNumEntry ("Volume Restore Delay", 30);
    
    _cfg->setGroup("Volume");    
    volumeMuted = _cfg->readBoolEntry("Volume Muted", false);
    volumeLeft = _cfg->readNumEntry("Volume Left", 0);     
    volumeRight = _cfg->readNumEntry("Volume Right", 0);

    // Make sure the volume has sane values, otherwise set them
    // to zero.
    if (volumeLeft > 100)
      volumeLeft = 100;

    if (volumeRight > 100)
      volumeRight = 100;

    return 0;
}


int ConfigData::loadTuning( const QString &dev, const QString &src )
{
    if (!_cfg)
      return -1;

    kdDebug() << "Loading Picture Settings for device: " << dev << endl;
    kdDebug() << "  for source: " << src << endl;
    
    _cfg->setGroup(dev);
    
    if (!_cfg->hasKey(src+" Whiteness")) 
      return -1;

    lastSource = _cfg->readEntry("Last Source", "");
    whiteness = _cfg->readNumEntry(src+" Whiteness", 0);
    colour = _cfg->readNumEntry(src+" Colour", 0);
    contrast = _cfg->readNumEntry(src+" Contrast", 0);
    hue = _cfg->readNumEntry(src+" Hue", 0);
    brightness = _cfg->readNumEntry(src+" Brightness", 0);
    lastChannel = _cfg->readNumEntry(src+" Previous Channel", 2);
    channelFile = _cfg->readEntry(src+" Channel File", "channels.xml");


    kdDebug() << "brightness = " << brightness << " colour = " << colour
              << " hue = " << hue << " contrast = " << contrast << endl;
    return 0;
}


int ConfigData::saveTuning( const QString &dev, const QString &src )
{
    if (!_cfg)
        return -1;

    kdDebug() << "Saving Picture settings for device: " << dev << endl;
    kdDebug() << "  for source: " << src << endl;
    kdDebug() << "brightness = " << brightness << " colour = " << colour << " hue = " << hue << " contrast = " << contrast << endl;

    _cfg->setGroup(dev);
    _cfg->writeEntry("Last Source", lastSource);
    _cfg->writeEntry(src+" Whiteness", whiteness);
    _cfg->writeEntry(src+" Colour", colour);
    _cfg->writeEntry(src+" Contrast", contrast);
    _cfg->writeEntry(src+" Hue", hue);
    _cfg->writeEntry(src+" Brightness", brightness);
    _cfg->writeEntry(src+" Previous Channel", lastChannel);
    _cfg->writeEntry(src+" Channel File", channelFile);

    return 0;
}


int ConfigData::save() {
    
    if (!_cfg)
       return -1;

    _cfg->setGroup("General");
    _cfg->writeEntry("First Time", false);
    _cfg->writeEntry("NTSC PAL SECAM", nps);
    _cfg->writeEntry("Frame Rate", frameRate);
    _cfg->writeEntry("Auto Start", autoStart);
    _cfg->writeEntry("Capture at Max Res", maxResolution);
    _cfg->writeEntry("Display Landscape", landscape);
    _cfg->writeEntry("Mirror image", mirror);
    _cfg->writeEntry("Enable Video Desktop", videoDesktopEnabled);
    _cfg->writeEntry("Stay on Top", stayOnTop);
    _cfg->writeEntry("Auto Scale Enabled", autoScaleEnabled);
    _cfg->writeEntry("Show Only Selected Channels", showSelectedOnly);
    _cfg->writeEntry("Previous Device", prevDev);
    _cfg->writeEntry("Previous Source", prevSrc);
    _cfg->writeEntry("Fix Aspect Ratio", fixAR);
    _cfg->writeEntry("Aspect Ratio Mode", ARmode);
    _cfg->writeEntry("Disable Screensaver", disableScreenSaver);
    _cfg->writeEntry("Screensaver Mode", screenSaverMode);
    _cfg->writeEntry("TopLevel Pos", topLevelPos);
    _cfg->writeEntry("TopLevel Size", topLevelSize);
    _cfg->writeEntry("Use Listings URL", useListingsURL);
    _cfg->writeEntry("Listings URL", listingsURL);
    _cfg->writeEntry("Volume Restore Delay", volumeRestoreDelay);
 
    _cfg->setGroup("Volume");
    _cfg->writeEntry("Volume Muted", volumeMuted);
    _cfg->writeEntry("Volume Left", volumeLeft);
    _cfg->writeEntry("Volume Right", volumeRight);
    
    return 0;
}

const QString ConfigData::lastSourceFor(const QString& dev) const {
    if (_cfg) 
      return QString::null;

    _cfg->setGroup(dev);
    return _cfg->readEntry("Last Source", QString::null);
}


