//
// C++ Implementation: k9config
//
// Description:
//
//
// Author: Jean-Michel PETIT <k9copy@free.fr>, (C) 2007
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "k9config2.h"
#include <QDir>
#include <QLocale>

k9Config::k9Config() {
    m_config=NULL;
    read();
}

void k9Config::read() {
    // if (! m_config->hasGroup( "options"))
    // 	readOldConfig();
    if (m_config!=NULL)
        delete m_config;
    m_config=new QSettings("K9Copy","backlite",0);

    m_config->beginGroup("dir");
    m_prefOutput=m_config->value("output",QDir::tempPath()+"/k9copy/").toString();
    m_prefOutputMpeg4=m_config->value("outputMpeg4",QDir::homePath() ).toString();
    m_prefOutputMpeg2=m_config->value("outputMpeg2",QDir::homePath()).toString();
    m_prefOutputIso=m_config->value("outputIso",QDir::homePath()).toString();
    m_prefOutputAudio=m_config->value("outputAudio",QDir::homePath()).toString();
    m_config->endGroup();

    m_config->beginGroup("options");
    m_prefK3b=m_config->value("usek3b",false).toBool();
    m_prefAutoBurn=m_config->value("autoburn",false).toBool();
    m_quickScan=m_config->value("quickscan",true).toBool();
    m_prefSize=m_config->value("dvdsize",4400).toInt();
    m_useDvdAuthor=m_config->value("useDvdAuthor",true).toBool();
    m_keepMenus=m_config->value("keepMenus",true).toBool();
    m_prefDelTmpFiles=m_config->value("deltmpfiles",false).toBool();
    m_prefReadAhead=m_config->value("readahead",-1).toInt();
    m_prefUserOps=m_config->value("userops",false).toBool();
    m_prefShowOutput=m_config->value("showoutput",true).toBool();
    QString lang=QLocale::system().name();
    m_prefAudioLang=m_config->value("audiolang",lang.left(2)).toString();
    m_prefSubtitleLang=m_config->value("subtitlelang",lang.left(2)).toString();
    m_config->endGroup();

    m_config->beginGroup("preview");
    m_useMplayer=m_config->value("useMplayer",false).toBool();
    m_usePhonon=m_config->value("usePhonon",false).toBool();
    m_MplayerVout=m_config->value("MplayerVout",0).toInt();
    m_MplayerAout=m_config->value("MplayerAout",0).toInt();
    m_useXine=m_config->value("useXine",false).toBool();
    m_XineVout=m_config->value("XineVout",0).toInt();
    m_XineAout=m_config->value("XineAout",0).toInt();
    m_config->endGroup();

    m_config->beginGroup("mp4");
    m_prefMp4Codec=m_config->value("codec",0).toInt();
    m_prefMp4Encoder=m_config->value("encoder","mencoder").toString();
    m_prefMp4Size=m_config->value("size",700).toInt();
    m_prefMp4NumberCD=m_config->value("numberCD",1).toInt();
    m_prefMp4Width=m_config->value("width","720").toString();
    m_prefMp4Height=m_config->value("height","").toString();
    m_prefMp4AspectRatio=m_config->value("aspectRatio",true).toBool();
    m_prefMp42Passes=m_config->value("2passes",false).toBool();
    m_prefMp4AudioBitrate=m_config->value("audiobitrate","128").toString();
    m_prefMp4VideoBitrate=m_config->value("videobitrate" "").toString();
    m_prefMp4AudioCodec=m_config->value("audiocodec",0).toInt();
    m_prefMp4AudioGain=m_config->value("audiogain",7).toInt();
    m_prefUseCellCache=m_config->value("usecellcache",true).toBool();
    m_prefMpegChapters=m_config->value("mpegchapters",true).toBool();
    m_config->endGroup();

    m_config->beginGroup("authoring");
    m_prefAudioFormat=m_config->value("audioFormat","AC3").toString();
    m_prefAudioBitrate=m_config->value("audioBitrate",192).toInt();
    m_prefButtonWidth=m_config->value("buttonWidth",50).toInt();
    m_prefButtonHeight=m_config->value("buttonHeight",40).toInt();
    QColor c=m_config->value("buttonHiliteColor").value<QColor>();
    c=c.isValid()?c:QColor(Qt::yellow);
    m_prefButtonHiliteColor =c;

    c=m_config->value("buttonTextColor").value<QColor>();
    c=c.isValid()?c:QColor(Qt::yellow);
    m_prefButtonTextColor=c;
    QVariant v=m_config->value("buttonFont");
    if (v.isNull())
        m_prefButtonFont=QFont();
    else
        m_prefButtonFont=v.value<QFont>();

    m_config->endGroup();

    m_config->beginGroup("programs");
    m_prefMencoderPath=m_config->value("Mencoder",k9Tools::findExe("mencoder")).toString();
    m_prefMplayerPath=m_config->value("Mplayer",k9Tools::findExe("mplayer")).toString();
    m_prefk3bPath=m_config->value("k3b",k9Tools::findExe("k3b")).toString();
    m_prefDvdauthorPath=m_config->value("Dvdauthor",k9Tools::findExe("dvdauthor")).toString();
    m_prefFFmpegPath=m_config->value("FFMpeg",k9Tools::findExe("ffmpeg")).toString();
    m_prefWodimPath=m_config->value("Wodim",k9Tools::findExe("wodim")).toString();
    m_prefGenisoimagePath=m_config->value("Genisoimage",k9Tools::findExe("genisoimage")).toString();
    m_config->endGroup();


    m_config->beginGroup("devices");
    m_devices=m_config->value("dev",QStringList()).toStringList();
    m_devicesLabels=m_config->value("labels",QStringList()).toStringList();
    m_devicesIO=m_config->value("IO",QStringList()).toStringList();
    m_InputDev=m_config->value("input",0).toInt();
    m_OutputDev=m_config->value("output",0).toInt();

    m_config->endGroup();


    m_config->beginGroup("gui");
    m_prefStyle=m_config->value("style","").toString();
    QList<bool> defCol;
    defCol << true << true <<true << true;
    m_prefColumns=defCol;
    int size=m_config->beginReadArray("columns");

    for (int i=0; i< size;i++) {
        m_config->setArrayIndex(i);
        m_prefColumns[i]=m_config->value("col").toBool();
    }
    m_config->endArray();
    m_config->endGroup();
}

void k9Config::save() {
    m_config->beginGroup("options");
    m_config->setValue("usek3b",m_prefK3b);
    m_config->setValue("autoburn",m_prefAutoBurn);
    m_config->setValue("quickscan",m_quickScan);
    m_config->setValue("dvdsize",m_prefSize);
    m_config->setValue("useDvdAuthor",m_useDvdAuthor);
    m_config->setValue("keepMenus",m_keepMenus);
    m_config->setValue("deltmpfiles",m_prefDelTmpFiles);
    m_config->setValue("readahead",m_prefReadAhead);
    m_config->setValue("userops",m_prefUserOps);
    m_config->setValue("showoutput",m_prefShowOutput);
    m_config->setValue("audiolang",m_prefAudioLang);
    m_config->setValue("subtitlelang",m_prefSubtitleLang);
    m_config->endGroup();

    m_config->beginGroup("preview");
    m_config->setValue("useMplayer",m_useMplayer);
    m_config->setValue("usePhonon",m_usePhonon);
    m_config->setValue("MplayerVout",m_MplayerVout);
    m_config->setValue("MplayerAout",m_MplayerAout);
    m_config->setValue("useXine",m_useXine);
    m_config->setValue("XineVout",m_XineVout);
    m_config->setValue("XineAout",m_XineAout);
    m_config->endGroup();

    m_config->beginGroup("mp4");
    m_config->setValue("codec",m_prefMp4Codec);
    m_config->setValue("encoder",m_prefMp4Encoder);
    m_config->setValue("size",m_prefMp4Size);
    m_config->setValue("numberCD",m_prefMp4NumberCD);
    m_config->setValue("width",m_prefMp4Width);
    m_config->setValue("height",m_prefMp4Height);
    m_config->setValue("aspectRatio",m_prefMp4AspectRatio);
    m_config->setValue("2passes",m_prefMp42Passes);
    m_config->setValue("audiobitrate",m_prefMp4AudioBitrate);
    m_config->setValue("videobitrate",m_prefMp4VideoBitrate);
    m_config->setValue("audiocodec",m_prefMp4AudioCodec);
    m_config->setValue("audiogain",m_prefMp4AudioGain);
    m_config->setValue("usecellcache",m_prefUseCellCache);
    m_config->setValue("mpegchapters",m_prefMpegChapters);
    m_config->endGroup();

    m_config->beginGroup("authoring");
    m_config->setValue("audioFormat",m_prefAudioFormat);
    m_config->setValue("audioBitrate",m_prefAudioBitrate);
    m_config->setValue("buttonWidth",m_prefButtonWidth);
    m_config->setValue("buttonHeight",m_prefButtonHeight);
    m_config->setValue("buttonHiliteColor",m_prefButtonHiliteColor);
    m_config->setValue("buttonTextColor",m_prefButtonTextColor);
    m_config->setValue("buttonFont",m_prefButtonFont);
    m_config->endGroup();

    m_config->beginGroup("dir");
    m_config->setValue("output",m_prefOutput);
    m_config->setValue("outputMpeg4",m_prefOutputMpeg4);
    m_config->setValue("outputMpeg2",m_prefOutputMpeg2);
    m_config->setValue("outputAudio",m_prefOutputAudio);
    m_config->setValue("outputIso",m_prefOutputIso);
    m_config->endGroup();

    m_config->beginGroup("programs");
    m_config->setValue("Mencoder",m_prefMencoderPath);
    m_config->setValue("Mplayer",m_prefMplayerPath);
    m_config->setValue("k3b",m_prefk3bPath);
    m_config->setValue("Dvdauthor",m_prefDvdauthorPath);
    m_config->setValue("FFmpeg",m_prefFFmpegPath);
    m_config->setValue("Wodim",m_prefWodimPath);
    m_config->setValue("Genisoimage",m_prefGenisoimagePath);
    m_config->endGroup();

    m_config->beginGroup("devices");
    m_config->setValue("dev",m_devices);
    m_config->setValue("labels",m_devicesLabels);
    m_config->setValue("IO",m_devicesIO);
    m_config->setValue("input",m_InputDev);
    m_config->setValue("output",m_OutputDev);
    m_config->endGroup();


    m_config->beginGroup("gui");
    m_config->setValue("style",m_prefStyle);
    m_config->beginWriteArray("columns",m_prefColumns.count());
    for (int i=0; i < m_prefColumns.count();i++) {
        m_config->setArrayIndex(i);
        m_config->setValue("col",m_prefColumns.at(i));
    }
    m_config->endArray();
    m_config->sync();
}


k9Config::~k9Config() {
    delete m_config;

}


void k9Config::setPrefGeometry(QByteArray _value,const QString &name ) {
    m_config->beginGroup(name);
    m_config->setValue("geometry",_value);

    m_config->endGroup();
    m_config->sync();
}


QByteArray k9Config::getPrefGeometry(const QString &name) {
    m_config->beginGroup(name);
    return m_config->value("geometry").toByteArray();
}

QSettings* k9Config::getConfig()  {
    if (m_config ==NULL)
	read();
    return m_config;
}
