//
// C++ Implementation: eaddelementcmd
//
// Description:
//
//
// Author: Hans Oischinger <hans.oischinger@kdemail.net>, (C) 2005
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "eaddelementcmd.h"

// Core
#include "core/direktorinstance.h"
#include "core/elementdatabase.h"

// Elements
#include "elements/elementnode.h"

// KDE
#include <klocale.h>
#include <kdebug.h>


EAddElementCMD::EAddElementCMD( ElementNode * newElement, ElementNode * parent, ElementNode * neighbour, bool before )
    : ECommand( i18n( "Add Element(s)" )),
    m_remove(false),
    m_element(newElement),
    m_parent(parent),
    m_neighbour(neighbour),
    m_before(before)
{
  if (!m_parent)
    kdWarning() << "EAddElementCMD::EAddElementCMD() - Null parent given" << endl;
}


EAddElementCMD::~EAddElementCMD()
{
  if ( m_remove )
  {
    Kast::self()->elementDB().deleteEntry( m_element );
  }
}

void EAddElementCMD::execute()
{
  ECommand::execute();
  m_remove = false;

  if (isRedoing())
  {
    Kast::self()->elementDB().reviveEntry( m_element );
  }
  else
  {
    if (!m_neighbour)
      m_parent->appendChild(m_element);
    else if (m_neighbour && m_before)
      m_parent->insertBefore(m_element, m_neighbour);
    else if (m_neighbour && !m_before)
      m_parent->insertAfter(m_element, m_neighbour);

    Kast::self()->elementDB().insertEntry( m_element );
  }
}

void EAddElementCMD::unexecute()
{
  ECommand::unexecute();
  m_remove = true;

  Kast::self()->elementDB().removeEntry( m_element );
}
