/* This file is part of the KDE project
   Copyright (C) 2003-2004 Jaroslaw Staniek <js@iidea.pl>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "KexiStartup_p.h"

#include <kstandarddirs.h>
#include <kprogress.h>
#include <tdeprocess.h>
#include <kdebug.h>
#include <tdelocale.h>

#include <tqfileinfo.h>
#include <tqdir.h>
#include <tqapplication.h>

SQLite2ToSQLite3Migration::SQLite2ToSQLite3Migration(const TQString& filePath)
: m_filePath(filePath)
{
	m_process = 0;
	m_dlg = 0;
	result = false;
	m_run = false;
}

SQLite2ToSQLite3Migration::~SQLite2ToSQLite3Migration()
{
	delete m_process;
	m_dlg->close();
	delete m_dlg;
}

tristate SQLite2ToSQLite3Migration::run()
{
	if (m_run)
		return false;
	m_run = true;
	const TQString ksqlite2to3_app = TDEStandardDirs::findExe( "ksqlite2to3" );
	if (ksqlite2to3_app.isEmpty())
		return false;

	TQFileInfo fi(m_filePath);
	if (fi.isSymLink()) {
		m_filePath = fi.readLink();
		fi = TQFileInfo(m_filePath);
	}
	//remember permissions of m_filePath
	m_restoreStat = (0==stat(TQFile::encodeName(m_filePath), &m_st));

	m_process = new TDEProcess(this, "process");
	*m_process << ksqlite2to3_app << m_filePath;
	m_process->setWorkingDirectory( fi.dir(true).absPath() );
	connect( m_process, TQ_SIGNAL(receivedStderr(TDEProcess*,char*,int)),
		this, TQ_SLOT(receivedStderr(TDEProcess*,char*,int)));
	connect( m_process, TQ_SIGNAL(processExited(TDEProcess*)), this, TQ_SLOT(processExited(TDEProcess*)) );
	if (!m_process->start(TDEProcess::NotifyOnExit, TDEProcess::Stderr))
		return false;

	m_dlg = new KProgressDialog(0, 0, TQString(), 
		i18n("Saving \"%1\" project file to a new \"%2\" database format...")
		.arg(TQDir::convertSeparators(TQFileInfo(m_filePath).fileName())).arg("SQLite3")
	);
	m_dlg->setModal(true);
	connect(m_dlg, TQ_SIGNAL(cancelClicked()), this, TQ_SLOT(cancelClicked()));
	m_dlg->setMinimumDuration(1000);
	m_dlg->setAutoClose(true);
	m_dlg->progressBar()->setTotalSteps(100);
	m_dlg->progressBar()->setProgress(0);
	m_dlg->exec();

	if (result!=true)
		return result;

	return result;
}

extern void updateProgressBar(KProgressDialog *pd, char *buffer, int buflen);

void SQLite2ToSQLite3Migration::receivedStderr(TDEProcess *, char *buffer, int buflen)
{
	updateProgressBar(m_dlg, buffer, buflen);
}

void SQLite2ToSQLite3Migration::processExited(TDEProcess* process)
{
	kdDebug() << "EXIT " << process->name() << endl;

	kdDebug() << process->isRunning() << " " << process->exitStatus() << endl;
	m_dlg->close();
	result = !process->isRunning() && 0==process->exitStatus();//m_process->normalExit();
	kdDebug() << result.toString() << endl;
	if (result == true) {
		if (m_restoreStat) {
			//restore permissions for m_filePath
			chmod(TQFile::encodeName(m_filePath), m_st.st_mode);
			chown(TQFile::encodeName(m_filePath), m_st.st_uid, m_st.st_gid);
		}
	}
}

void SQLite2ToSQLite3Migration::cancelClicked()
{
	kdDebug() << result.toString() << " cancelClicked() " <<m_process->isRunning() << " " 
		<< m_process->exitStatus() << endl;
	if (!m_process->isRunning() && 0==m_process->exitStatus())
		return;
	result = cancelled;
	m_process->kill();
}

#include "KexiStartup_p.moc"