/***************************************************************************
                          abbreviation.cpp  -  description
                             -------------------
    copyright            : (C) 2003 by Andras Mantia <amantia@kde.org>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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; version 2 of the License.               *
 *                                                                         *
 ***************************************************************************/

//qt includes
#include <tqdom.h>
#include <tqfile.h>
#include <tqfileinfo.h>
#include <tqlineedit.h>
#include <tqtextedit.h>
#include <tqtextstream.h>
#include <tqtimer.h>

//kde includes
#include <kcombobox.h>
#include <kdebug.h>
#include <kdialogbase.h>
#include <tdeglobal.h>
#include <kinputdialog.h>
#include <tdelistbox.h>
#include <tdelistview.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <kstandarddirs.h>

//app includes
#include "abbreviation.h"
#include "codetemplatedlgs.h"
#include "resource.h"
#include "quantacommon.h"
#include "dtds.h"


AbbreviationDlg::AbbreviationDlg(TQWidget *parent, const char *name):
    AbbreviationDlgS(parent, name)
{
  m_oldItem = 0L;
  m_currentAbbrev = 0L;

  TQMapConstIterator<TQString, Abbreviation> it;
  for (it = qConfig.abbreviations.constBegin(); it != qConfig.abbreviations.constEnd(); ++it)
  {
    groupCombo->insertItem(it.key());
  }
  slotGroupChanged(groupCombo->currentText());
}

AbbreviationDlg::~AbbreviationDlg()
{
}

void AbbreviationDlg::slotGroupChanged(const TQString& newGroupName)
{
  //save the current template
  TQListViewItem *item = templatesList->currentItem();
  if (item && m_currentAbbrev)
     m_currentAbbrev->abbreviations.insert(item->text(0) + " " + item->text(1), codeEdit->text());
  dtepList->clear();
  templatesList->clear();
  codeEdit->clear();
  m_oldItem = 0L;
  m_currentAbbrev = &qConfig.abbreviations[newGroupName];
  TQString templateStr;
  TQString templateName;
  TQMap<TQString, TQString>::ConstIterator it;
  for ( it = m_currentAbbrev->abbreviations.constBegin(); it != m_currentAbbrev->abbreviations.constEnd(); ++it )
  {
    templateStr = it.key();
    templateName = templateStr.mid(templateStr.find(' ') + 1);
    templateStr = templateStr.left(templateStr.find(' '));
    new TDEListViewItem(templatesList, templateStr, templateName);
  }
  for ( TQStringList::ConstIterator dtepit = m_currentAbbrev->dteps.constBegin(); dtepit != m_currentAbbrev->dteps.constEnd(); ++dtepit )
  {
      dtepList->insertItem(DTDs::ref()->getDTDNickNameFromName(*dtepit));
  }
  templatesList->sort();
  templatesList->setCurrentItem(templatesList->firstChild());
  templatesList->setSelected(templatesList->firstChild(), true);
}

void AbbreviationDlg::slotNewGroup()
{
   bool ok;
   TQString groupName = KInputDialog::getText(i18n("New Abbreviation Group"), i18n("Group name:"), "", &ok, this);
   if (ok && !groupName.isEmpty())
   {
      if (qConfig.abbreviations.contains(groupName))
      {
          KMessageBox::error(this, i18n("<qt>There is already an abbreviation group called <b>%1</b>. Choose an unique name for the new group.</qt>").arg(groupName), i18n("Group already exists"));
          TQTimer::singleShot(0, this, TQT_SLOT(slotNewGroup()));
      } else
      {
         groupCombo->insertItem(groupName);
         groupCombo->setCurrentItem(groupCombo->count()-1);
         slotGroupChanged(groupName);
      }
   }
}

void AbbreviationDlg::slotAddDTEP()
{
  TQStringList lst = DTDs::ref()->nickNameList(false);
  for (uint i = 0; i < dtepList->count(); i++)
     lst.remove(dtepList->text(i));
  bool ok = false;
  TQStringList res = KInputDialog::getItemList(
                  i18n( "Add DTEP" ),
                  i18n( "Select a DTEP:" ), lst, 0, true, &ok, this );
  if (ok)
  {
    dtepList->insertStringList(res);
    for (TQStringList::ConstIterator it = res.constBegin(); it != res.constEnd(); ++it)
    {
      m_currentAbbrev->dteps.append(DTDs::ref()->getDTDNameFromNickName(*it));
    }
  }
}

void AbbreviationDlg::slotRemoveDTEP()
{
  bool hasSelected = false;
  for (uint i = 0; i < dtepList->count(); i++)
  {
    if (dtepList->isSelected(i))
    {
      m_currentAbbrev->dteps.remove(DTDs::ref()->getDTDNameFromNickName(dtepList->text(i)));
      dtepList->removeItem(i);
      i--;
      hasSelected = true;
    }
  }
  if (!hasSelected)
  {
    KMessageBox::error(this, i18n("<qt>Select a DTEP from the list before using <b>Remove</b>.</qt>"), i18n("No DTEP Selected"));
  }
}

void AbbreviationDlg::slotTemplateSelectionChanged(TQListViewItem* item)
{
  if (!item)
      return;
  //TQListViewItem *m_oldItem = templatesList->currentItem();
  if (m_oldItem)
  {
    TQString key = m_oldItem->text(0)+" " +m_oldItem->text(1);
    m_currentAbbrev->abbreviations.insert(key, codeEdit->text());
  }

  TQString code = m_currentAbbrev->abbreviations[item->text(0)+" " +item->text(1)];
  codeEdit->setText(code);
  m_oldItem = item;
}

void AbbreviationDlg::slotAddTemplate()
{
  KDialogBase dlg(this, 0, true, i18n("Add Code Template"), KDialogBase::Ok | KDialogBase::Cancel);
  CodeTemplateDlgS w(&dlg);
  dlg.setMainWidget(&w);
  w.templateEdit->setFocus();
  if (dlg.exec())
  {
    TQString templateName = w.templateEdit->text();
    TQString description = w.descriptionEdit->text();
    if (!templateName.isEmpty())
    {
      TQListViewItem *item = new TDEListViewItem(templatesList, templateName, description);
      templatesList->setCurrentItem(item);
      templatesList->setSelected(item, true);
      templatesList->ensureItemVisible(item);
      codeEdit->setFocus();
    }
  }
}

void AbbreviationDlg::slotRemoveTemplate()
{
  TQListViewItem *item = templatesList->currentItem();
  if (item &&
      KMessageBox::warningContinueCancel(this, i18n("<qt>Do you really want to remove the <b>%1</b> template?</qt>").arg(item->text(1)),TQString(),KStdGuiItem::del()) == KMessageBox::Continue)
  {
    m_currentAbbrev->abbreviations.remove(item->text(0)+" "+item->text(1));
    delete item;
    m_oldItem = 0L;
    slotTemplateSelectionChanged(templatesList->currentItem());
  }
}

void AbbreviationDlg::slotEditTemplate()
{
  TQListViewItem *item = templatesList->currentItem();
  if (!item)
      return;
  KDialogBase dlg(this, 0, true, i18n("Edit Code Template"), KDialogBase::Ok | KDialogBase::Cancel);
  CodeTemplateDlgS w(&dlg);
  dlg.setMainWidget(&w);
  w.templateEdit->setText(item->text(0));
  w.descriptionEdit->setText(item->text(1));
  if (dlg.exec())
  {
    m_currentAbbrev->abbreviations.remove(item->text(0)+" "+item->text(1));
    item->setText(0, w.templateEdit->text());
    item->setText(1, w.descriptionEdit->text());
    m_currentAbbrev->abbreviations.insert(item->text(0) + " " + item->text(1), codeEdit->text());
  }
}


void AbbreviationDlg::saveTemplates()
{
  TQListViewItem *item = templatesList->currentItem();
  if (item && m_currentAbbrev)
     m_currentAbbrev->abbreviations.insert(item->text(0) + " " + item->text(1), codeEdit->text());
  TQString abbrevFile = TDEGlobal::dirs()->saveLocation("data") + resourceDir + "abbreviations.xml";
  TQString s = "<!DOCTYPE Templates>\n<Templates>\n</Templates>\n";
  TQString s2;
  TQDomDocument doc;
  doc.setContent(s);
  TQDomNode firstNode = doc.firstChild();
  Abbreviation abbrev;
  TQMap<TQString, Abbreviation>::ConstIterator abbrevIt;
  for (abbrevIt = qConfig.abbreviations.constBegin(); abbrevIt != qConfig.abbreviations.constEnd(); ++abbrevIt)
  {
    TQDomElement groupEl = doc.createElement("Group");
    groupEl.setAttribute("name", abbrevIt.key());
    abbrev = abbrevIt.data();
    for (TQStringList::ConstIterator dtepIt = abbrev.dteps.constBegin(); dtepIt != abbrev.dteps.constEnd(); ++dtepIt)
    {
      TQDomElement el = doc.createElement("DTEP");
      el.setAttribute("name", *dtepIt);
      groupEl.appendChild(el);
    }
    int pos;
    TQMap<TQString,TQString>::ConstIterator it;
    for (it = abbrev.abbreviations.constBegin(); it != abbrev.abbreviations.constEnd(); ++it)
    {
      TQDomElement el = doc.createElement("Template");
      el.setAttribute("code", it.data());
      s = it.key();
      s2 = "";
      pos = s.find(' ');
      if (pos != -1)
      {
        s2 = s.mid(pos+1);
        s = s.left(pos);
      }
      el.setAttribute("name", s);
      el.setAttribute("description", s2);
      groupEl.appendChild(el);
    }
    firstNode.appendChild(groupEl);
  }

  TQFile f(abbrevFile);
  if (f.open(IO_WriteOnly | IO_Truncate))
  {
    TQTextStream str(&f);
    str.setEncoding(TQTextStream::UnicodeUTF8);
    str << doc.toString();
    f.close();
  } else
  {
    KMessageBox::error(this, i18n("<qt>Cannot open the file <b>%1</b> for writing.\nModified abbreviations will be lost when you quit Quanta.</qt>").arg(s));
  }
}

#include "abbreviation.moc"
