/***************************************************************************
 *   Copyright (C) 2004 by Hans Oischinger                                 *
 *   hans.oischinger@kdemail.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.                                   *
 *                                                                         *
 *   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.,                                       *
 *   51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA.             *
 ***************************************************************************/
#ifndef ELEMENTDATABASE_H
#define ELEMENTDATABASE_H

#include <qobject.h>
#include "coreinterfaces.h"
#include <qptrlist.h>

class KURL;
class DocumentElement;
class ElementNode;
class ElementSelection;
class DefsElement;
/**
This database hold informations about all elements on the document
 
@author Hans Oischinger
*/
class ElementDatabase : public QObject, public ElementDatabaseInterface
{
  Q_OBJECT
public:
  ElementDatabase(QObject *parent = 0, const char *name = 0);
  ~ElementDatabase();

  void addElementListener( ElementListenerInterface* );
  void removeElementListener( ElementListenerInterface* );

  DocumentElement &documentRoot() { return *m_documentElement; }
  void setDocumentRoot( DocumentElement *doc );

  /**
   * @return the root node where all paint servers are hung in
   */
  DefsElement &getPaintServerRoot() { return *m_defs; }

  ElementSelection &selection() { return *m_sel; }

  /**
   * Adds a new item to the database and notifies all listeners
   */
  void ElementDatabase::insertEntry( ElementNode * );

  /**
   * Sets the deleted state for an element and all it's children and notifies all listeners
   */
  void ElementDatabase::removeEntry( ElementNode * );

  /**
   * Sets the state of an element and it's children back to normal and notifies all listeners
   * Usefull after removeEntry() (Undo capability)
   * @param  
   */
  void ElementDatabase::reviveEntry( ElementNode * );

  /**
   * Deletes the element and all it's children... ONLY to be called after a removeEntry
   * Use carefully
   */
  void ElementDatabase::deleteEntry( ElementNode * );

  /**
   * Sets the modified flag on an element and notifies all listeners
   */
  void ElementDatabase::entryModified( ElementNode * );

  /**
   * @return an id that is unique in the current element tree
   */
  QString genUniqueID();

protected:
  void notifyRemove( ElementNode * );
  void notifyInsert( ElementNode * );
  void notifyModified( ElementNode * );

protected slots:
  void establishConnections( ElementNode* );
  void slotOnDestroyed( QObject* );

public slots:
  bool openURL( const KURL& url );

protected:
  QPtrList<ElementListenerInterface> m_listeners;
  DocumentElement *m_documentElement;
  ElementSelection *m_sel;
  DefsElement *m_defs;
};

#endif
