/*
 *
 * Copyright (C) 2002 George Staikos <staikos@kde.org>
 *               2003, 2004 Dirk Ziegelmeier <dziegel@gmx.de>
 *
 * 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., 51 Franklin Steet, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <kconfig.h>
#include <kdebug.h>
#include <klocale.h>
#include <kglobal.h>
#include <kconfiggroup.h>

#include <qdir.h>

#include "cfgdata.h"

struct grab_resolutions_s grab_resolutions[] = {
        { 160, 120, "160 x 120 (1/4 NTSC)" },
        { 192, 144, "192 x 144 (1/4 PAL)"  },
        { 320, 240, "320 x 240 (1/2 NTSC)" },
        { 384, 288, "384 x 288 (1/2 PAL)"  },
        { 400, 300, "400 x 300 (default)"  },
        { 640, 480, "640 x 480 (NTSC)"     },
        { 768, 576, "768 x 576 (PAL)"      },
        {  -1,  -1, ""                     },
};

static const ConfigData* defaultCfg = NULL;

const ConfigData& ConfigData::getDefaultConfig()
{
    if(defaultCfg == NULL) {
        defaultCfg = new ConfigData(0L, 0L);
    }
    return *defaultCfg;
}

ConfigData::ConfigData(KConfig* backendCfg, KConfig* clientCfg)
    : _backendCfg(backendCfg),
      _clientCfg(clientCfg)
{
    defaults();
}

ConfigData::~ConfigData()
{
}

ConfigData const &ConfigData::operator=(ConfigData const &other)
{
    if (this != &other) {
        // General
        channelNumberingStart = other.channelNumberingStart;
        firstTime             = other.firstTime;
        showSelectedOnly      = other.showSelectedOnly;
        maxKeypressInterval   = other.maxKeypressInterval;
        mouseWheelUpIsChUp    = other.mouseWheelUpIsChUp;

        // Device
        prevDev = other.prevDev;
        
        // Sound
        autoMute                  = other.autoMute;
        volumeIncrement           = other.volumeIncrement;
        volumeRestoreDelay        = other.volumeRestoreDelay;
        volumeControlViaSoundCard = other.volumeControlViaSoundCard;
        tvCardVolume              = other.tvCardVolume;
        mixerVolume               = other.mixerVolume;

        // Video
        fixAR           = other.fixAR;
        ARmode          = other.ARmode;
        snapshotPath    = other.snapshotPath;
        snapshotFormat  = other.snapshotFormat;
        snapshotQuality = other.snapshotQuality;
        snapshotRes     = other.snapshotRes;

        // Channel
        channelFile        = other.channelFile;
        lastChannel        = other.lastChannel;
        
        kDebug() << "ConfigData::operator = : called...";
    } // if
    return(*this);
} // operator=


void ConfigData::defaults()
{
    // General

    // In us and ca, channel numbering starts at 2. Set an appropriate default.
    QString country = KGlobal::locale()->country();
    if((country == "us")  ||
       (country == "ca"))    {
        channelNumberingStart = 2;
    } else {
        channelNumberingStart = 1;
    }

    firstTime = true;
    showSelectedOnly = true;
    maxKeypressInterval = 2000;
    mouseWheelUpIsChUp = true;

    // Device
    prevDev = QString::null;

    // Channels
    channelFile = QString::null;
    lastChannel = -1;

    // Sound
    autoMute = false;
    volumeIncrement = 5;
    volumeRestoreDelay = 30;
    volumeMuted = false;
    volumeLeft = 50;
    volumeRight = 50;
    volumeControlViaSoundCard = true;
    tvCardVolume = 100;
    mixerVolume = 100;

    // Video
    fixAR = true;
    ARmode = AR_BEST_FIT;
    snapshotPath = QDir::homePath();
    snapshotFormat = "PNG";
    snapshotQuality = -1;
    snapshotRes = 4;
}

int ConfigData::loadBackendSettings()
{
    // none so far
    return 0;
}

int ConfigData::loadClientSettings()
{
    if (!_clientCfg)
        return -1;

    const ConfigData& def = getDefaultConfig();

    KConfigGroup gen_group = KConfigGroup(_clientCfg, "General");

    firstTime                 = gen_group.readEntry("First Time", def.firstTime);
    channelNumberingStart     = gen_group.readEntry("Channel Numbering Start", def.channelNumberingStart);
    showSelectedOnly          = gen_group.readEntry("Show Only Selected Channels", def.showSelectedOnly);
    maxKeypressInterval       = gen_group.readEntry("Max Keypress Interval", def.maxKeypressInterval);
    mouseWheelUpIsChUp        = gen_group.readEntry("MouseWheelUpIsChUp", def.mouseWheelUpIsChUp);
    snapshotPath              = gen_group.readEntry("Snapshot Path", def.snapshotPath);
    snapshotFormat            = gen_group.readEntry("Snapshot Format", def.snapshotFormat);
    snapshotQuality           = gen_group.readEntry("Snapshot Quality", def.snapshotQuality);
    snapshotRes               = gen_group.readEntry("Snapshot Resolution", def.snapshotRes);
    autoMute                  = gen_group.readEntry("Auto Mute", def.autoMute);
    volumeRestoreDelay        = gen_group.readEntry("Volume Restore Delay", def.volumeRestoreDelay);
    volumeControlViaSoundCard = gen_group.readEntry("Volume Control Via Sound Card", def.volumeControlViaSoundCard);
    tvCardVolume              = gen_group.readEntry("TV Card Volume", def.tvCardVolume);
    mixerVolume               = gen_group.readEntry("Mixer Volume", def.mixerVolume);
    prevDev                   = gen_group.readEntry("Previous Device", def.prevDev);
    fixAR                     = gen_group.readEntry("Fix Aspect Ratio", def.fixAR);
    ARmode                    = gen_group.readEntry("Aspect Ratio Mode", def.ARmode);
    volumeIncrement           = gen_group.readEntry ("Volume Increment", def.volumeIncrement);
    
    KConfigGroup volume_group = KConfigGroup(_clientCfg, "General");
    volumeMuted = volume_group.readEntry("Volume Muted", def.volumeMuted);
    volumeLeft  = volume_group.readEntry("Volume Left", def.volumeLeft);
    volumeRight = volume_group.readEntry("Volume Right", def.volumeRight);

    // Make sure the volume has sane values (the user may still need his ears)
    if ( (volumeLeft > 100) || (volumeLeft < 0) )
        volumeLeft = def.volumeLeft;

    if ( (volumeRight > 100) || (volumeRight < 0) )
        volumeRight = def.volumeRight;

    return 0;
}

int ConfigData::load()
{
    int rc1, rc2;

    kDebug();

    rc1 = loadBackendSettings();
    rc2 = loadClientSettings();

    if (rc1 || rc2) {
        return -1;
    } else {
        return 0;
    }
}

int ConfigData::loadDeviceSettings(const QString& dev)
{
    if (!_clientCfg || dev.isEmpty())
        return -1;

    const ConfigData& def = getDefaultConfig();

    kDebug() << "Loading settings for device: " << dev;

    KConfigGroup dev_group = KConfigGroup(_clientCfg, dev);
    lastChannel  = dev_group.readEntry("Previous Channel", def.lastChannel);
    channelFile  = dev_group.readEntry("Channel File", def.channelFile);
    
    return 0;
}

int ConfigData::saveDeviceSettings(const QString& dev)
{
    if (!_clientCfg || dev.isEmpty())
        return -1;

    kDebug() << "Saving settings for device: " << dev;

    KConfigGroup dev_group = KConfigGroup(_clientCfg, dev);
    dev_group.writeEntry("Previous Channel", lastChannel);
    dev_group.writeEntry("Channel File", channelFile);

    return 0;
}

int ConfigData::saveBackendSettings()
{
    // none so far
    return 0;
}

int ConfigData::saveClientSettings()
{
    if (!_clientCfg)
        return -1;

    KConfigGroup gen_group = KConfigGroup(_clientCfg, "General");
    gen_group.writeEntry("First Time", false);
    gen_group.writeEntry("Auto Mute", autoMute);
    // gen_group.writeEntry("Capture at Max Res", maxResolution);
    gen_group.writeEntry("Max Keypress Interval", maxKeypressInterval);
    gen_group.writeEntry("MouseWheelUpIsChUp", mouseWheelUpIsChUp);
    gen_group.writeEntry("Show Only Selected Channels", showSelectedOnly);
    gen_group.writePathEntry("Snapshot Path", snapshotPath);
    gen_group.writeEntry("Snapshot Format", snapshotFormat);
    gen_group.writeEntry("Snapshot Quality", snapshotQuality);
    gen_group.writeEntry("Snapshot Resolution", snapshotRes);
    gen_group.writeEntry("Channel Numbering Start", channelNumberingStart);
    gen_group.writeEntry("Volume Restore Delay", volumeRestoreDelay);
    gen_group.writeEntry("Volume Control Via Sound Card", volumeControlViaSoundCard);
    gen_group.writeEntry("TV Card Volume", tvCardVolume);
    gen_group.writeEntry("Mixer Volume", mixerVolume);
    gen_group.writeEntry("Previous Device", prevDev);
    gen_group.writeEntry("Fix Aspect Ratio", fixAR);
    gen_group.writeEntry("Aspect Ratio Mode", ARmode);
    gen_group.writeEntry("Volume Increment", volumeIncrement);
    gen_group.sync();

    KConfigGroup volume_group = KConfigGroup(_clientCfg, "Volume");
    volume_group.writeEntry("Volume Muted", volumeMuted);
    volume_group.writeEntry("Volume Left", volumeLeft);
    volume_group.writeEntry("Volume Right", volumeRight);
    volume_group.sync();

    return 0;
}

int ConfigData::save()
{
    int rc1, rc2;

    kDebug();

    rc1 = saveBackendSettings();
    rc2 = saveClientSettings();

    if (rc1 || rc2) {
        return -1;
    } else {
        return 0;
    }
}
