/***************************************************************************
                          doctreeview.cpp  -  description
                             -------------------
    begin                : Sat Mar 4 2000
    copyright            : (C) 2000 by Yacovlev Alexander & Dmitry Poplavsky <pdima@mail.univ.kiev.ua>
                           (C) 2002, 2004 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; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

// QT clases
#include <tqstrlist.h>
#include <tqheader.h>
#include <tqpixmap.h>
#include <tqdir.h>

// KDE clases
#include <tdeconfig.h>
#include <tdeapplication.h>
#include <tdelocale.h>
#include <tdepopupmenu.h>
#include <kstandarddirs.h>
#include <kiconloader.h>
#include <kurl.h>

// application clases
#include "doctreeview.h"
#include "docfolder.h"
#include "docitem.h"

DocTreeView::DocTreeView(TQWidget *parent, const char *name )
  : TDEListView(parent,name)
{

  contextHelpDict = new TQDict<TQString>( 101, false );

  setRootIsDecorated( true );
  header()->hide();
  setSorting(-1,false);

  setFrameStyle( Panel | Sunken );
  setLineWidth( 2 );
  addColumn(i18n("Name"), -1);
  addColumn("");
  setFullWidth(true);

  projectDocFolder = new TDEListViewItem(this, i18n("Project Documentation"));
  projectDocFolder->setOpen(true);
  slotRefreshTree();
  setFocusPolicy(TQ_ClickFocus);

  connect(this, TQT_SIGNAL(executed(TQListViewItem *)), TQT_SLOT(clickItem(TQListViewItem *)) );
  connect(this, TQT_SIGNAL(returnPressed(TQListViewItem *)), TQT_SLOT(clickItem(TQListViewItem *)));
  connect(this, TQT_SIGNAL(doubleClicked(TQListViewItem *)), TQT_SLOT(slotDoubleClicked(TQListViewItem *)));

  m_contextMenu  = new TDEPopupMenu(this);
  m_menuReload = m_contextMenu->insertItem(i18n("&Reload"), this, TQT_SLOT(slotReloadProjectDocs()));
  m_contextMenu->insertItem(SmallIcon("network"), i18n("&Download Documentation..."), this, TQT_SIGNAL(downloadDoc()));
  connect(this, TQT_SIGNAL(contextMenu(TDEListView*, TQListViewItem*, const TQPoint&)),
          this, TQT_SLOT(slotMenu(TDEListView*, TQListViewItem*, const TQPoint&)));
}


DocTreeView::~DocTreeView(){
  contextHelpDict->setAutoDelete(true);
  delete contextHelpDict;
}

void DocTreeView::slotRefreshTree()
{
  for (TQValueList<DocFolder *>::Iterator it = m_folderList.begin(); it != m_folderList.end(); ++it)
  {
    delete *it;
  }
  m_folderList.clear();
  TQStringList docDirs = TDEGlobal::instance()->dirs()->findDirs("appdata", "doc");

  for ( TQStringList::Iterator it = docDirs.begin(); it != docDirs.end(); ++it )
  {
    TQString docDir = *it;
    TQDir dir(docDir, "*.docrc");
    TQStringList files = dir.entryList();

    for ( TQStringList::Iterator it_f = files.begin(); it_f != files.end(); ++it_f )
    {
      TDEConfig config( docDir + *it_f );
      config.setGroup("Tree");

      TQString relDocDir = config.readEntry("Doc dir");
      TQString name = config.readEntry("Name").lower();

      DocFolder *folder = new DocFolder(this, config.readEntry("Top Element"), &config , TQDir::cleanDirPath(docDir+relDocDir)+"/");
      folder->setPixmap( 0, SmallIcon("folder_open") );
      folder->topLevel = true;
      folder->setOpen(true);
      m_folderList.append(folder);

      config.setGroup("Context");
      TQStrList list;
      config.readListEntry("ContextList", list );

      for ( unsigned int i=0; i<list.count(); i++ )
      {
        TQString keyword = list.at(i);
        TQString *url = new TQString(TQDir::cleanDirPath(docDir + relDocDir + "/" + config.readEntry( list.at(i) )));
        contextHelpDict->insert( name + "|" + keyword, url );
      }
    }
  }

}

void DocTreeView::clickItem( TQListViewItem *)
{
  TQListViewItem *it = currentItem();
  if ( !it )
    return;
  DocItem *dit = dynamic_cast< DocItem *>(it);
  if ( dit )
    if ( ! dit->url.isEmpty() )
        emit openURL( dit->url);

  DocFolder *dfol = dynamic_cast< DocFolder *>(it);
  if ( dfol )
    if ( ! dfol->url.isEmpty() )
      emit openURL( dfol->url );
  //else
  //  emit openURL( locate("appdata","doc/documentation.html") );
}


TQString * DocTreeView::contextHelp(const TQString &keyword)
{
  TQString word = keyword.mid(keyword.find("|"));
  if (contextHelpDict->find(keyword))
    return contextHelpDict->find(keyword);
  else
    return contextHelpDict->find(word); //to support old documentation packages
}

void DocTreeView::slotDoubleClicked(TQListViewItem *item )
{
  if (item)
  {
    item->setOpen(!item->isOpen());
  }
}

void DocTreeView::slotAddProjectDoc(const KURL& url)
{
  TQString path = url.path();
  int pos = path.find("/doc/");
  path = path.mid(pos + 5);
  new DocItem(projectDocFolder, path, url.url());
}

void DocTreeView::slotMenu(TDEListView *, TQListViewItem *item, const TQPoint &point)
{
  m_contextMenu->setItemVisible(m_menuReload, false);
  if (item)
  {
    setSelected(item, true);
    if (currentItem() == projectDocFolder)
    {
      m_contextMenu->setItemVisible(m_menuReload, true);
    }
  }
  m_contextMenu->popup(point);
}

void DocTreeView::slotNewProjectLoaded(const TQString &, const KURL &, const KURL &)
{
  slotReloadProjectDocs();
}

void DocTreeView::slotReloadProjectDocs()
{
  TQListViewItem *child = projectDocFolder->firstChild();
  while (child) {
      TQListViewItem *c = child;
      child = child->nextSibling();
      delete c;
  }
  emit reloadProjectDocs();
}

#include "doctreeview.moc"
