#include "preferencesdialog.h"

QSettings settings("Slackpack");
QProcess *src2pkgproc;
PreferencesDialog::PreferencesDialog(QDialog *parent) :
    QDialog(parent)
{
    setupUi(this);

    QFile configFile(QDir::homePath().append("/.config/Slackpack.conf"));

    if(!configFile.exists())
    {
       defaultsbtn_clicked();
       return;
    }

     connect(createpkgComboBox, SIGNAL(currentIndexChanged(int)),this,SLOT(scriptChanged()));
     connect(rpm2tgzComboBox, SIGNAL(currentIndexChanged(int)),this,SLOT(scriptChanged()));
     connect(deb2tgzComboBox, SIGNAL(currentIndexChanged(int)),this,SLOT(scriptChanged()));
     //read settings
     createpkgComboBox->setCurrentIndex(settings.value("createpkgComboBoxLastState").toInt());
     createpkgArgsBox->setText(settings.value("createpkgArgsBoxLastState").toString());
     rpm2tgzComboBox->setCurrentIndex(settings.value("rpm2tgzComboBoxLastState").toInt());
     rpm2tgzArgsBox->setText(settings.value("rpm2tgzArgsBoxLastState").toString());
     deb2tgzComboBox->setCurrentIndex(settings.value("deb2tgzComboBoxLastState").toInt());
     deb2tgzArgsBox->setText(settings.value("deb2tgzArgsBoxLastState").toString());

}

PreferencesDialog::~PreferencesDialog()
{
    delete m_ui;
}

void PreferencesDialog::scriptChanged()
{
    //check if  user selected 'src2pkg'
    if(createpkgComboBox->currentIndex() == 1)
    {
        src2pkgproc = new QProcess(this);
        src2pkgproc->start("src2pkg");
        connect( src2pkgproc, SIGNAL(error(QProcess::ProcessError)),this,SLOT(src2pkgError()));
    }

    if(rpm2tgzComboBox->currentIndex() == 1)
    {
        src2pkgproc = new QProcess(this);
        src2pkgproc->start("src2pkg");
        connect( src2pkgproc, SIGNAL(error(QProcess::ProcessError)),this,SLOT(src2pkgError()));
    }

    if(deb2tgzComboBox->currentIndex() == 1)
    {
        src2pkgproc = new QProcess(this);
        src2pkgproc->start("src2pkg");
        connect( src2pkgproc, SIGNAL(error(QProcess::ProcessError)),this,SLOT(src2pkgError()));
    }

    createpkgArgsBox->clear();
    rpm2tgzArgsBox->clear();
    deb2tgzArgsBox->clear();
}

void PreferencesDialog::src2pkgError()
{
         QString src2pkgOut;
         src2pkgproc->readAll();
         QMessageBox errorBox;
         errorBox.setText("Be sure to have src2pkg installed on the system!\n"
                          "if src2pkg is installed, run 'src2pkg --setup' as root to configure it\n");
         errorBox.setIcon(QMessageBox::Critical);
         errorBox.exec();

       createpkgComboBox->setCurrentIndex(0);
       createpkgArgsBox->setText(settings.value("createpkgArgsBoxLastState").toString());
       rpm2tgzComboBox->setCurrentIndex(0);
       rpm2tgzArgsBox->setText(settings.value("rpm2tgzArgsBoxLastState").toString());
       deb2tgzComboBox->setCurrentIndex(0);
       deb2tgzArgsBox->setText(settings.value("deb2tgzArgsBoxLastState").toString());
}
void PreferencesDialog::defaultsbtn_clicked()
{
    //set default settings values
    settings.setValue("createpkgComboBoxLastState","0");
    settings.setValue("createpkgArgsBoxLastState","-l y -c y");
    settings.setValue("rpm2tgzComboBoxLastState","0");
    settings.setValue("rpm2tgzArgsBoxLastState","");
    settings.setValue("deb2tgzComboBoxLastState","0");
    settings.setValue("deb2tgzArgsBoxLastState","");


    createpkgComboBox->setCurrentIndex(settings.value("createpkgComboBoxLastState").toInt());
    createpkgArgsBox->setText(settings.value("createpkgArgsBoxLastState").toString());
    rpm2tgzComboBox->setCurrentIndex(settings.value("rpm2tgzComboBoxLastState").toInt());
    rpm2tgzArgsBox->setText(settings.value("rpm2tgzArgsBoxLastState").toString());
    deb2tgzComboBox->setCurrentIndex(settings.value("deb2tgzComboBoxLastState").toInt());
    deb2tgzArgsBox->setText(settings.value("deb2tgzArgsBoxLastState").toString());
}

void PreferencesDialog::applybtn_clicked()
{
    //write settings
    settings.setValue("createpkgComboBoxLastState",createpkgComboBox->currentIndex());
    settings.setValue("createpkgArgsBoxLastState",createpkgArgsBox->text());
    settings.setValue("rpm2tgzComboBoxLastState",rpm2tgzComboBox->currentIndex());
    settings.setValue("rpm2tgzArgsBoxLastState",rpm2tgzArgsBox->text());
    settings.setValue("deb2tgzComboBoxLastState",rpm2tgzComboBox->currentIndex());
    settings.setValue("deb2tgzArgsBoxLastState",deb2tgzArgsBox->text());
    PreferencesDialog::close();
}

