// list_hostvars.cpp
//
// List Rivendell Host Variables
//
//   (C) Copyright 2002-2004,2016-2018 Fred Gleason <fredg@paravelsystems.com>
//
//   This program is free software; you can redistribute it and/or modify
//   it under the terms of the GNU General Public License version 2 as
//   published by the Free Software Foundation.
//
//   This program 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 General Public License for more details.
//
//   You should have received a copy of the GNU General Public
//   License along with this program; if not, write to the Free Software
//   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//

#include <qdialog.h>
#include <qstring.h>
#include <qpushbutton.h>
#include <q3listbox.h>
#include <q3textedit.h>
#include <qlabel.h>
#include <qpainter.h>
#include <qevent.h>
#include <qmessagebox.h>
#include <q3buttongroup.h>

#include <rddb.h>
#include <rdescape_string.h>
#include <rdstation.h>

#include "add_hostvar.h"
#include "edit_hostvar.h"
#include "globals.h"
#include "list_hostvars.h"

ListHostvars::ListHostvars(QString station,QWidget *parent)
  : QDialog(parent,"",true)
{
  QString str;

  //
  // Fix the Window Size
  //
  setMinimumWidth(sizeHint().width());
  setMaximumWidth(sizeHint().width());
  setMinimumHeight(sizeHint().height());
  setMaximumHeight(sizeHint().height());

  list_station=station;
  str=QString(tr("Host Variables for"));
  setCaption(tr("Host Variables for")+" "+station);

  //
  // Create Fonts
  //
  QFont font=QFont("Helvetica",12,QFont::Bold);
  font.setPixelSize(12);
  QFont small_font=QFont("Helvetica",10,QFont::Bold);
  small_font.setPixelSize(10);

  //
  // Matrix List Box
  //
  list_view=new Q3ListView(this);
  list_view->setGeometry(10,24,sizeHint().width()-20,sizeHint().height()-114);
  QLabel *label=new QLabel(list_view,tr("Host Variables"),this);
  label->setFont(font);
  label->setGeometry(14,5,sizeHint().width()-28,19);
  list_view->setAllColumnsShowFocus(true);
  list_view->setItemMargin(5);
  list_view->addColumn(tr("NAME"));
  list_view->setColumnAlignment(0,Qt::AlignHCenter);
  list_view->addColumn(tr("VALUE"));
  list_view->setColumnAlignment(1,Qt::AlignLeft);
  list_view->addColumn(tr("REMARK"));
  list_view->setColumnAlignment(2,Qt::AlignLeft);
  connect(list_view,SIGNAL(doubleClicked(Q3ListViewItem *,const QPoint &,int)),
	  this,SLOT(doubleClickedData(Q3ListViewItem *,const QPoint &,int)));

  RefreshList();

  //
  //  Add Button
  //
  QPushButton *add_button=new QPushButton(this);
  add_button->setGeometry(10,sizeHint().height()-80,80,50);
  add_button->setFont(font);
  add_button->setText(tr("&Add"));
  connect(add_button,SIGNAL(clicked()),this,SLOT(addData()));

  //
  //  Edit Button
  //
  QPushButton *edit_button=new QPushButton(this);
  edit_button->setGeometry(100,sizeHint().height()-80,80,50);
  edit_button->setFont(font);
  edit_button->setText(tr("&Edit"));
  connect(edit_button,SIGNAL(clicked()),this,SLOT(editData()));

  //
  //  Delete Button
  //
  QPushButton *delete_button=new QPushButton(this);
  delete_button->setGeometry(190,sizeHint().height()-80,80,50);
  delete_button->setFont(font);
  delete_button->setText(tr("&Delete"));
  connect(delete_button,SIGNAL(clicked()),this,SLOT(deleteData()));

  //
  //  OK Button
  //
  QPushButton *button=new QPushButton(this);
  button->setGeometry(sizeHint().width()-180,sizeHint().height()-60,
			    80,50);
  button->setDefault(true);
  button->setFont(font);
  button->setText(tr("&OK"));
  connect(button,SIGNAL(clicked()),this,SLOT(okData()));

  //
  //  Cancel Button
  //
  button=new QPushButton(this);
  button->setGeometry(sizeHint().width()-90,sizeHint().height()-60,
			    80,50);
  button->setDefault(true);
  button->setFont(font);
  button->setText(tr("&Cancel"));
  connect(button,SIGNAL(clicked()),this,SLOT(cancelData()));
}


ListHostvars::~ListHostvars()
{
  delete list_view;
}


QSize ListHostvars::sizeHint() const
{
  return QSize(490,320);
} 


QSizePolicy ListHostvars::sizePolicy() const
{
  return QSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
}


void ListHostvars::addData()
{
  QString varname;
  QString varvalue;
  QString varremark;
  AddHostvar *var_dialog=
    new AddHostvar(list_station,&varname,&varvalue,&varremark,this);
  if(var_dialog->exec()==0) {
    Q3ListViewItem *item=new Q3ListViewItem(list_view);
    item->setText(0,varname);
    item->setText(1,varvalue);
    item->setText(2,varremark);
  }
  delete var_dialog;
}


void ListHostvars::editData()
{
  Q3ListViewItem *item=list_view->selectedItem();
  if(item==NULL) {
    return;
  }
  QString varvalue=item->text(1);
  QString varremark=item->text(2);
  EditHostvar *var_dialog=
    new EditHostvar(list_station,item->text(0),&varvalue,&varremark,this);
  if(var_dialog->exec()==0) {
    item->setText(1,varvalue);
    item->setText(2,varremark);
  }
  delete var_dialog;
}


void ListHostvars::deleteData()
{
  Q3ListViewItem *item=list_view->selectedItem();
  if(item==NULL) {
    return;
  }
  if(QMessageBox::question(this,"RDAdmin - "+tr("Delete Host Variable"),
			   tr("Are you sure you want to delete the variable")+
			   " \""+item->text(0)+"\"?",
		      QMessageBox::Yes,QMessageBox::No)!=QMessageBox::Yes) {
    return;
  }
  delete item;
}


void ListHostvars::doubleClickedData(Q3ListViewItem *,const QPoint &,int)
{
  editData();
}


void ListHostvars::okData()
{
  QString sql;
  RDSqlQuery *q;

  sql=QString("delete from HOSTVARS where ")+
    "STATION_NAME=\""+RDEscapeString(list_station)+"\"";
  q=new RDSqlQuery(sql);
  delete q;
  Q3ListViewItem *item=list_view->firstChild();
  while(item!=NULL) {
    sql=QString("insert into HOSTVARS set ")+
      "STATION_NAME=\""+RDEscapeString(list_station)+"\","+
      "NAME=\""+RDEscapeString(item->text(0))+"\","+
      "VARVALUE=\""+RDEscapeString(item->text(1))+"\","+
      "REMARK=\""+RDEscapeString(item->text(2))+"\"";
    q=new RDSqlQuery(sql);
    delete q;
    item=item->nextSibling();
  }
  done(0);
}


void ListHostvars::cancelData()
{
  done(-1);
}


void ListHostvars::RefreshList()
{
  Q3ListViewItem *l;

  list_view->clear();
  QString sql=QString("select ")+
    "NAME,"+      // 00
    "VARVALUE,"+  // 01
    "REMARK "     // 02
    "from HOSTVARS where "+
    "STATION_NAME=\""+RDEscapeString(list_station)+"\" "+
    "order by NAME";
  RDSqlQuery *q=new RDSqlQuery(sql);
  while(q->next()) {
    l=new Q3ListViewItem(list_view);
    l->setText(0,q->value(0).toString());
    l->setText(1,q->value(1).toString());
    l->setText(2,q->value(2).toString());
  }
  delete q;
}
