 /********************************* .cpp **********************************
 \file .cpp
 \brief (put some short descripion here).

     #include ".h"
     Project: MedinTux
     Copyright (C) 2003 2004 2005 2006 2007
     by Sevin Roland  and Data Medical Design
     E-Mail: medintux@medintux.org

 *   This program is free software; you can redistribute it and/or modify         *
 *   it under the terms of the CeCILL Version 1 du 21/06/2004                     *
 *   License (GPL compatible)                                                     *
 *                              http://www.cecill.info/                           *
 *   as published by :                                                            *
 *                                                                                *
 *   Commissariat a l'Energie Atomique                                            *
 *   - CEA,                                                                       *
 *                            31-33 rue de la Federation, 75752 PARIS cedex 15.   *
 *                            FRANCE                                              *
 *   Centre National de la Recherche Scientifique                                 *
 *   - CNRS,                                                                      *
 *                            3 rue Michel-Ange, 75794 Paris cedex 16.            *
 *                            FRANCE                                              *
 *   Institut National de Recherche en Informatique et en Automatique             *
 *   - INRIA,                                                                     *
 *                            Domaine de Voluceau, Rocquencourt, BP 105, 78153    *
 *                            FRANCE                                              *
 *                                                                                *
 *         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.                   *
 *         CeCILL  License (GPL compatible) for more details.                     *
 *                                                                                *
 **********************************************************************************/
#include <qfiledialog.h> 
#include <qdir.h> 
#include <qfile.h> 
#include <qfileinfo.h> 
#include <qpushbutton.h> 
#include <qlineedit.h> 
#include <qapplication.h> 
#include <qcursor.h> 

#include "C_Dlg_MainDialog.h"

C_Dlg_MainDialog::C_Dlg_MainDialog(QWidget* parent, const char* name, bool modal, WFlags fl)
: Dlg_MainDialog(parent,name, modal,fl)
{// lineEdit_repToErase
 // lineEdit_repWhereErase
 // pushButton_Quitter
 // pushButton_Effacer
 // pushButton_find
 lineEdit_repToErase->setText("cvs");
 lineEdit_fileToErase->setText("~");
 connect( pushButton_Quitter,               SIGNAL( clicked()),         this,    SLOT  ( Slot_On_pushButton_Quitter())  );
 connect( pushButton_Effacer,               SIGNAL( clicked()),         this,    SLOT  ( Slot_On_pushButton_Effacer()) );
 connect( pushButton_find,                  SIGNAL( clicked()),         this,    SLOT  ( Slot_On_pushButton_find())  );
}

C_Dlg_MainDialog::~C_Dlg_MainDialog()
{
}

/*$SPECIALIZATION$*/


//----------------------------------------- Slot_On_pushButton_find ---------------------------------------------
void C_Dlg_MainDialog::Slot_On_pushButton_find()
{ // QString QFileDialog::getExistingDirectory ( const QString & dir = QString::null, QWidget * parent = 0, const char * name = 0, const QString & caption = QString::null, bool dirOnly = TRUE, bool resolveSymlinks = TRUE ) [static] 
  // This is a convenience static function that will return an existing directory selected by the user. 
    QString s = QFileDialog::getExistingDirectory(
                    "/home",
                    this,
                    "get existing directory",
                    tr("choisissez le répertoir cible"),
                    TRUE );
  if (s.length()) lineEdit_repWhereErase->setText(s);
}

//----------------------------------------- Slot_On_pushButton_Effacer ---------------------------------------------
void C_Dlg_MainDialog::Slot_On_pushButton_Effacer()
{ QApplication::setOverrideCursor( QCursor( Qt::WaitCursor ) );
  Test_And_RemoveDir(lineEdit_repWhereErase->text(),  lineEdit_repToErase->text() );
  QApplication::restoreOverrideCursor();
}

//----------------------------------------- Slot_On_pushButton_Quitter ---------------------------------------------
void C_Dlg_MainDialog::Slot_On_pushButton_Quitter()
{accept();
}


//----------------------------------------- Test_And_RemoveDir ---------------------------------------------
void C_Dlg_MainDialog::Test_And_RemoveDir(QString src_Dir,  QString dir_To_remove)
{
    //........................ explorer la source .......................................
    QDir cur_dir( src_Dir );
    cur_dir.setFilter( QDir::All | QDir::NoSymLinks | QDir::Hidden );
    QString motifMusBeInFileToErase = lineEdit_fileToErase->text();

    //............ exploration de la liste ......................
    const QFileInfoList * pQFileInfoList = cur_dir.entryInfoList();
    if (pQFileInfoList==0) return;
    QFileInfoListIterator it( *pQFileInfoList );
    QFileInfo            *fi;
    while ( (fi = it.current()) != 0 )
    {
        QString fname = fi->fileName().latin1();
        if      (fname == "..")
                {
                }
        else if (fi->isDir() && fname != "." )
                {if (fname == dir_To_remove)
                     EraseDirectory(src_Dir + "/" + fname);
                 else
                     Test_And_RemoveDir( src_Dir + "/" + fname, dir_To_remove);
                }
        else if (fi->isFile() && motifMusBeInFileToErase.length())
                {if (fname.findRev(motifMusBeInFileToErase) != -1) cur_dir.remove(src_Dir + "/" + fname);
                }
        ++it;
    }
}

//----------------------------------------- EraseDirectory ---------------------------------------------
void C_Dlg_MainDialog::EraseDirectory( QString dir_to_erase)
{   //........................ petit message .......................................
    //G_pCApp->Datasemp_OutMessage(textEdit_Message, tr ("----------- Effacement de ce répertoire en cours : ") + dir_to_erase + "-----------");
    QDir cur_dir( dir_to_erase );
    cur_dir.setFilter( QDir::All | QDir::NoSymLinks | QDir::Hidden );
    //............ exploration de la liste ......................
    const QFileInfoList * pQFileInfoList = cur_dir.entryInfoList();
    if (pQFileInfoList==0) return;
    QFileInfoListIterator it( *pQFileInfoList );
    QFileInfo            *fi;
    while ( (fi = it.current()) != 0 )
    {
        QString fname = fi->fileName().latin1();
        if      (fname == "..")
        {
        }
        else if (fi->isDir() && fname != "." )
        {EraseDirectory(dir_to_erase + "/" + fname);
        }
        else if (fi->isFile())
        {   //G_pCApp->Datasemp_OutMessage(textEdit_Message, QString (tr("Fichier en cours d'effacement : ")) + dir_to_erase + "/" + fname);
            cur_dir.remove(dir_to_erase + "/" + fname);
        }
        ++it;
    }
    cur_dir.rmdir(dir_to_erase );
}

