/****************************************************************************
**
** Copyright (C) 2014 Jolla Ltd.
** Contact: http://jolla.com/
**
** This file is part of Qt Creator.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Digia.
**
****************************************************************************/

#include "nxtgeneraloptionswidget.h"
#include "ui_nxtgeneraloptionswidget.h"

#include <nxtconstants.h>
#include <steps/deploy/nxtdeployconfiguration.h>
#include <steps/deploy/nxtdeploysteps.h>
#include <nxtsettings.h>

using namespace Utils;

namespace Nxt {
namespace Internal {

NxtGeneralOptionsWidget::NxtGeneralOptionsWidget(QWidget *parent)
    : QWidget(parent)
    , m_ui(new Ui::NxtGeneralOptionsWidget)
{
    m_ui->setupUi(this);

    m_ui->environmentFilterTextEdit->setPlainText(NxtSettings::environmentFilter());
    m_ui->environmentFilterTextEdit->setEnabled(!NxtSettings::isEnvironmentFilterFromEnvironment());
    m_ui->environmentFilterTextEdit->setToolTip(m_ui->environmentFilterTextEdit->toolTip().arg(Constants::NXT_SDK_ENVIRONMENT_FILTER));
    m_ui->environmentFilterOverriddenLabel->setVisible(NxtSettings::isEnvironmentFilterFromEnvironment());
    m_ui->environmentFilterOverriddenLabel->setText(m_ui->environmentFilterOverriddenLabel->text().arg(Constants::NXT_SDK_ENVIRONMENT_FILTER));

    m_ui->askBeforeStartingVmCheckBox->setChecked(NxtSettings::isAskBeforeStartingVmEnabled());
    m_ui->askBeforeClosingVmCheckBox->setChecked(NxtSettings::isAskBeforeClosingVmEnabled());
}

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

void NxtGeneralOptionsWidget::store()
{
    if (!NxtSettings::isEnvironmentFilterFromEnvironment())
        NxtSettings::setEnvironmentFilter(m_ui->environmentFilterTextEdit->toPlainText());
    NxtSettings::setAskBeforeStartingVmEnabled(m_ui->askBeforeStartingVmCheckBox->isChecked());
    NxtSettings::setAskBeforeClosingVmEnabled(m_ui->askBeforeClosingVmCheckBox->isChecked());
}

QString NxtGeneralOptionsWidget::searchKeywords() const
{
    QString keywords;
    const QLatin1Char sep(' ');
    QTextStream(&keywords) << sep << m_ui->environmentFilterLabel->text()
                           << sep << m_ui->askBeforeStartingVmCheckBox->text()
                           ;
    keywords.remove(QLatin1Char('&'));
    return keywords;
}

} // namespace Internal
} // namespace Nxt
