/****************************************************************************
** ui.h extension file, included from the uic-generated form implementation.
**
** If you want to add, delete, or rename functions or slots, use
** Qt Designer to update this file, preserving your code.
**
** You should not define a constructor or destructor in this file.
** Instead, write your code in functions called init() and destroy().
** These will automatically be called by the form's constructor and
** destructor.
*****************************************************************************/
#include <qdir.h>
#include <qstatusbar.h>



//--------------------------------------- init ---------------------------------
void Dlg_Manu::init()
{G_pCManuApp->m_pDlg_Manu    = this;  // a faire ici des le debut
 //.................. cosmetique placement icone titre etc ...........................................................................
 setIcon( QPixmap (G_pCManuApp->m_PathRessources + "manu.png" ) );
 setCaption( QObject::tr(" Manu : documentation pour Medin'Tux" ));
 G_pCManuApp->PlaceWindows();        // place la fenetre selon le infos du fichier manu.ini
 //.................. positionner le repertoire source ...........................................................................
 textBrowserView->mimeSourceFactory()->addFilePath( G_pCManuApp->m_PathDoc );
 GetChapitres();
 GotoTarget( G_pCManuApp->m_StartDoc );
 QPushButton *quitButton = new QPushButton( tr("Quitter"), this,"pushButtonQuitter" );
 if (quitButton)
    {statusBar()->addWidget(quitButton , 0 , TRUE );
     connect( quitButton  , SIGNAL( clicked() ) , G_pCManuApp , SLOT( quit() ) );
    }
}

//--------------------------------------- GotoTarget--------------------------------------------------------
void Dlg_Manu::GotoTarget(const QString &target )
{int pos                 = target.findRev('#');
 QString       file_name = target;
 int              isUtf8 = 0;
 QString            text = 0;
 QString         anchor  = "";
 if (pos !=-1)
    {file_name = file_name.left(pos);
     anchor    = target.mid(pos+1);
    }
 file_name.prepend(G_pCManuApp->m_PathDoc + "/");
 if (  G_pCManuApp->m_curentPage  != file_name )
    {  G_pCManuApp->m_curentPage   = file_name;
       if (CGestIni::Param_UpdateFromDisk( file_name, text , &isUtf8) !=0 )
          {
           text.replace("meta name=\"qrichtext\" content=\"charset=utf-8\"",   // obligé d'etre en content=\"1\" pour que les tabulations fonctionnent !!
                        "meta name=\"qrichtext\" content=\"1\"");
           if (isUtf8) textBrowserView->mimeSourceFactory()->setExtensionType("html", "text/xml;charset=UTF-8");
           else        textBrowserView->mimeSourceFactory()->setExtensionType("html", "text/html;charset=iso8859-1");
           if (G_pCManuApp->m_IsQT_page) {textBrowserView->setText(text);
                                          if (anchor.length()) textBrowserView->scrollToAnchor(anchor);
                                         }
         }
     if (! G_pCManuApp->m_IsQT_page) textBrowserView->setSource( target );
    }
 else if (anchor.length())
    {textBrowserView->scrollToAnchor(anchor);
    }
 statusBar()->message ( target, 10000) ;
}

//--------------------------------------- GetChapitres-----------------------------------------------------
void Dlg_Manu::GetChapitres( )
{//........................ relever toutes les ancres des fichiers presents ..............................

 G_pCManuApp->GotoDebug();
 QStringList lst;
 QString text;
 if (G_pCManuApp->GetListDoc(lst))
    { QStringList::Iterator it;
      for ( it = lst.begin(); it != lst.end(); ++it )
          {QListViewItem* pQListViewItem = new QListViewItem (listView_AllAncres, *it);
           if (pQListViewItem )
	      {QStringList ancresList;
               QString file_ancres = *it;
               int pos = file_ancres.findRev('.');
               if (pos != -1) file_ancres = file_ancres.left(pos)+".acr";
               //................. verifier si cache d'ancres pas deja present ..............................................
               if (CGestIni::Param_UpdateFromDisk(G_pCManuApp->m_PathCache + "/" + file_ancres, text )!=0)
                  { CGestIni::Param_GetList(text, "Cache Ancres", "",  ancresList );
                  }
              //................. sinon charger le fichier html et lister les ancres ........................................
              //                  et creer le cache
               else if (CGestIni::Param_UpdateFromDisk(G_pCManuApp->m_PathDoc + "/" + *it, text )!=0)
                  { CHtmlTools::ListerAncres(text, ancresList);
                    if (ancresList.count())
                       {QString cache  = "[Cache Ancres]";
                        int          n = 1;
                        for ( QStringList::Iterator ut = ancresList.begin(); ut != ancresList.end(); ++ut )
                            {QString toAdd = QString("\r\n A") + QString::number(n++)+ " = " + *ut;
                             cache += toAdd;
                            }
                        CGestIni::Param_UpdateToDisk(G_pCManuApp->m_PathCache + "/" + file_ancres, cache );
                       }
                  }
               if (ancresList.count())
	          { for ( QStringList::Iterator ut = ancresList.begin(); ut != ancresList.end(); ++ut ) new QListViewItem(pQListViewItem, *ut);
                    pQListViewItem->setOpen (TRUE );
                  }
              }
          }
    }
}
//--------------------------------------- pushButtonMake_Index_clicked ----------------------------------------------
void Dlg_Manu::pushButtonMake_Index_clicked()
{QStringList lst;
 QString text;
 if (G_pCManuApp->GetListDoc(lst))
    { QStringList::Iterator it;
      listView_AllAncres->clear();
      for ( it = lst.begin(); it != lst.end(); ++it )
          {QListViewItem* pQListViewItem = new QListViewItem (listView_AllAncres, *it);
           if (pQListViewItem )
	      {QStringList ancresList;
               QString file_ancres = *it;
               int pos = file_ancres.findRev('.');
               if (pos != -1) file_ancres = file_ancres.left(pos)+".acr";
               if (CGestIni::Param_UpdateFromDisk(G_pCManuApp->m_PathDoc + "/" + *it, text )!=0)
                  { CHtmlTools::ListerAncres(text, ancresList);
                    if (ancresList.count())
                       {QString cache  = "[Cache Ancres]";
                        int          n = 1;
                        for ( QStringList::Iterator ut = ancresList.begin(); ut != ancresList.end(); ++ut )
                            {QString toAdd = QString("\r\n A") + QString::number(n++)+ " = " + *ut;
                             cache += toAdd;
                            }
                        CGestIni::Param_UpdateToDisk(G_pCManuApp->m_PathCache + "/" + file_ancres, cache );
                       }
                  }
               if (ancresList.count())
	          { for ( QStringList::Iterator ut = ancresList.begin(); ut != ancresList.end(); ++ut ) new QListViewItem(pQListViewItem, *ut);
                    pQListViewItem->setOpen (TRUE );
                  }
              }
          }
    }
}

//--------------------------------------- listView_AllAncres_doubleClicked ------------------------------------------
void Dlg_Manu::listView_AllAncres_doubleClicked( QListViewItem *pQListViewItem )
{if (pQListViewItem==0) return;
 GotoTarget(MakeLinkString (pQListViewItem));
}

//---------------------------- MakeLinkString -----------------------------------------------------------------------
// ACTION: part de l'item: QListViewItem *qlistViewItem   et remonte de parents en parents jusqu'à la racine
//         tout en constituant le chemin ainsi formé par les nom de ces items
// SORTIE: QString    contenant le chemin de l'item: QListViewItem *qlistViewItem

QString Dlg_Manu::MakeLinkString (QListViewItem *qlistViewItem)
{QString path  = qlistViewItem->text(0);
 qlistViewItem = qlistViewItem->parent();
 while (qlistViewItem!=0)
       {path.insert(0, '#');
        path.insert(0,qlistViewItem->text(0));
	if (qlistViewItem->depth()==0) break;
        qlistViewItem = qlistViewItem->parent();
       }
 return path;
}


//---------------------------- pushButtonBack_clicked -----------------------------------------------------------------------
void Dlg_Manu::pushButtonBack_clicked()
{textBrowserView->backward ();
}


//---------------------------- pushButtonHome_clicked -----------------------------------------------------------------------
void Dlg_Manu::pushButtonHome_clicked()
{textBrowserView->home ();
}


//---------------------------- pushButtonNext_clicked -----------------------------------------------------------------------
void Dlg_Manu::pushButtonNext_clicked()
{ textBrowserView->forward ();
}


//---------------------------- pushButtonSommaire_clicked -----------------------------------------------------------------------
void Dlg_Manu::pushButtonSommaire_clicked()
{//emit  linkClicked   (m_Path+ QDir::separator() + "DrTux.html#Sommaire du DrTux");
 //emit  anchorClicked ("Sommaire du DrTux", m_Path+QDir::separator() + "DrTux.html#Sommaire du DrTux");
 //textBrowserView->setSource(G_pCManuApp->m_PathDoc + "/"+ G_pCManuApp->m_SommaireDoc);
 GotoTarget(G_pCManuApp->m_SommaireDoc);
 //qDebug(m_Path+QDir::separator() + "DrTux.html#Sommaire du DrTux");
}

//---------------------------- pushButtonInfo_clicked -----------------------------------------------------------------------
void Dlg_Manu::pushButtonInfo_clicked()
{G_pCManuApp->AfficherContexte();
}


//---------------------------- listView_AllAncres_currentChanged -----------------------------------------------------------------------
void Dlg_Manu::listView_AllAncres_currentChanged( QListViewItem *pQListViewItem  )
{if (pQListViewItem==0) return;
 GotoTarget(MakeLinkString (pQListViewItem));
}


