/***************************************************************************
 *   KT environment variable editor window implementation                  *
 *   --------------------------------------------------------------------  *
 *   Copyright (C) 1999, Gary Meyer <gary@meyer.net>                       *
 *   --------------------------------------------------------------------  *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "ktvariable.h"

#include <tqlayout.h>

#include <tdeapplication.h>     // kapp
#include <tdelocale.h>  // i18n()
#include <tdemessagebox.h>
#include <ktextedit.h>

#include "ctvariable.h"

#include "kticon.h"

KTVariable::KTVariable(CTVariable* _ctvar,const TQString &_caption) :
  KDialogBase(0, "ktvariable", true, _caption, Ok|Cancel, Ok, true),
  ctvar( _ctvar)
{
  TQFrame *page = makeMainWidget();
  TQGridLayout *layout = new TQGridLayout( page, 5, 3, 0, spacingHint() );
  layout->setRowStretch(3, 1);
  layout->setColStretch(1, 1);

  setIcon(KTIcon::application(true));

  // variable
  labVariable = new TQLabel(i18n("&Variable:"), page, "labVariable");
  layout->addWidget(labVariable, 1, 0, TQt::AlignLeft | TQt::AlignTop);

  cmbVariable = new TQComboBox(true, page, "cmbVariable");
  layout->addWidget(cmbVariable, 1, 1);

  cmbVariable->insertItem("HOME");
  cmbVariable->insertItem("MAILTO");
  cmbVariable->insertItem("PATH");
  cmbVariable->insertItem("SHELL");

  labVariable->setBuddy(cmbVariable);

  // icon
  labIcon = new TQLabel(page, "labIcon");
  layout->addMultiCellWidget(labIcon, 0, 1, 2, 2);

  // value
  labValue = new TQLabel(i18n("Va&lue:"), page, "labValue");
  layout->addWidget(labValue, 2, 0, TQt::AlignLeft | TQt::AlignTop);

  leValue = new TQLineEdit(page, "leValue");
  layout->addMultiCellWidget(leValue, 2, 2, 1, 2);
  leValue->setMaxLength(255);
  labValue->setBuddy(leValue);

  // comment
  labComment = new TQLabel(i18n("Co&mment:"), page, "labComment");
  layout->addWidget(labComment, 3, 0, TQt::AlignLeft | TQt::AlignTop);

  teComment = new KTextEdit(page, "teComment");
  teComment->setTextFormat(TQt::PlainText);
  layout->addMultiCellWidget(teComment, 3, 3, 1, 2);

  labComment->setBuddy(teComment);

  // enabled
  chkEnabled = new TQCheckBox(i18n("&Enabled"), page, "chkEnabled");
  layout->addWidget(chkEnabled, 4, 0);

  // set starting field values
  cmbVariable->setEditText(TQString::fromLocal8Bit(ctvar->variable.c_str()));
  slotVariableChanged();

  leValue->setText(TQString::fromLocal8Bit(ctvar->value.c_str()));

  teComment->setText(TQString::fromLocal8Bit(ctvar->comment.c_str()));

  chkEnabled->setChecked(ctvar->enabled);

  cmbVariable->setFocus();

  // connect them up
  connect(cmbVariable,TQ_SIGNAL(highlighted(const TQString&)),
    TQ_SLOT(slotVariableChanged()));
  connect(cmbVariable,TQ_SIGNAL(activated(const TQString&)),
    TQ_SLOT(slotVariableChanged()));
}

KTVariable::~KTVariable()
{
}

void KTVariable::slotVariableChanged()
{
  TQString variable = cmbVariable->currentText();
  if (variable == "HOME")
  {
    labIcon->setPixmap(KTIcon::home(false));
    teComment->setText(i18n("Override default home folder."));
  }
  else if (variable == "MAILTO")
  {
    labIcon->setPixmap(KTIcon::mail(false));
    teComment->setText(i18n("Email output to specified account."));
  }
  else if (variable == "SHELL")
  {
    labIcon->setPixmap(KTIcon::shell(false));
    teComment->setText(i18n("Override default shell."));
  }
  else if (variable == "PATH")
  {
    labIcon->setPixmap(KTIcon::path(false));
    teComment->setText(i18n("Folders to search for program files."));
  }
  else
  {
    labIcon->setPixmap(KTIcon::variable(false));
  }
}

void KTVariable::slotOk()
{
  if (cmbVariable->currentText().isEmpty())
  {
    KMessageBox::information(this, i18n("Please enter the variable name."));
    cmbVariable->setFocus();
    return;
  }

  if (leValue->text().isEmpty())
  {
    KMessageBox::information(this, i18n("Please enter the variable value."));
    cmbVariable->setFocus();
    return;
  }

  ctvar->variable = cmbVariable->currentText().local8Bit().data();
  ctvar->value    = leValue->text().local8Bit().data();
  ctvar->comment  = teComment->text().replace('\n',' ').replace('\r',' ').local8Bit().data();
  ctvar->enabled  = chkEnabled->isChecked();
  close();
}

#include "ktvariable.moc"
