/***************************************************************************
                          copydir.cpp  -  description
                             -------------------
    begin                : Die Jän 18 2005
    copyright            : (C) 2005 by Krippel Harald
    email                : neuro.harald@surfeu.at
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "copydir.h"

#include <qapplication.h>
#include <qstring.h>
#include <qdir.h>
#include <qmessagebox.h>
#include <qurloperator.h>

copydir::copydir(){
}

copydir::~copydir(){
}

int copydir::copy(QString src,QString dest){

QString msg;

 srcDir = new QDir(src);
 destDir = new QDir(dest);

if (!destDir->exists()) {
	if (!destDir->mkdir(destDir->canonicalPath())) {
//		msg.sprintf("Could not create destination directory %s\n",
//    destDir->canonicalPath().latin1());
//		QMessageBox::information(0,"Copy Dir",msg);
		return 0;
	}
}
else if (destDir->entryList(QDir::Files).count() > 0) {
	msg.sprintf("Destination directory %s already exists.\n", destDir->canonicalPath().latin1());
//	switch ( QMessageBox::warning( 0, "Copy Dir", msg, "No, &Cancel","&Yes", 0, 0 ) ) {
		// The user clicked the "No, Cancel" button or pressed Enter
//		case 0:
			// return without doing anything
//			QMessageBox::information( 0, "Copy Dir", "Copy operation cancelled");
			return 0;

		// The user clicked the "Yes" button
//		case 1:
//			break;
//	}
}


// get info on all files in source directory
srcDir->setFilter( QDir::Files | QDir::Readable );
srcDir->setSorting( QDir::Name );
const QFileInfoList *srclist = srcDir->entryInfoList();
QFileInfoListIterator it( *srclist );
QFileInfo *di;

// Use QUrlOperator class to copy files to destination directory
QUrlOperator *op = new QUrlOperator();



while ( (di = it.current()) != 0 ) {

  qApp->processEvents();
	QString tmp = di->absFilePath();
	QString tmp2 = srcDir->canonicalPath();
	if ( !di->isDir() ) {
		// copy the file
		op->copy( di->absFilePath(), destDir->canonicalPath() );
	}
  qApp->processEvents();
	++it;
}
qApp->processEvents();

fflush(NULL);

return 0;
}
