/*
 * homedirmigration.cpp
 * Copyright (C) 2011  Romanov Ivan <drizt@land.ru>
 *
 * 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.
 *
 * 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.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

#include <QSignalMapper>
#include <QQueue>
#include <QPair>
#include <QMessageBox>
#include <QThread>
#include <QCloseEvent>
#include <QRegExp>

#include "applicationinfo.h"
#include "homedirmigration.h"
#include "ui_homedirmigration.h"
#if defined Q_WS_WIN
#include "psiapplication.h"
#endif

class Thread : public QThread
{
public:
	Thread(HomeDirMigration::Choice choice, QDir oldHomeDir, QDir configDir, QDir dataDir, QObject *parent = NULL) : QThread(parent)
	{
		choice_ = choice;
		oldHomeDir_ = oldHomeDir;
		configDir_ = configDir;
		dataDir_ = dataDir;
		stop_ = false;
	}

	bool result() const
	{
		return result_;
	}

	void stop()
	{
		stop_ = true;
	}

protected:
	void run()
	{
		switch((HomeDirMigration::Choice)choice_) {
		case HomeDirMigration::COPY:
			result_ = copyFolder();
			if (!result_) {
				removeFolder(configDir_.path());
#if defined Q_WS_X11
				removeFolder(dataDir_.path());
#endif
			}
			break;

		case HomeDirMigration::MOVE:
			result_ = copyFolder();
			if (result_) {
				removeFolder(oldHomeDir_.path());
			}
			else {
				removeFolder(configDir_.path());
#if defined Q_WS_X11
				removeFolder(dataDir_.path());
#endif
			}
			break;

		case HomeDirMigration::NOTHING:
			break;
		}
	}

	void removeFolder(const QString& folder)
	{
		QDir dir(folder);
		QFileInfoList infoList = dir.entryInfoList(QDir::Files | QDir::Hidden);
		QStringList files;
		foreach(QFileInfo info, infoList)
		{
			if(info.isFile() && info.isWritable() && info.isReadable())
				files << info.fileName();
		}

		// Remove all file in current directory
		QStringList::Iterator itFile = files.begin();
		while (itFile != files.end()) {
			QFile file(folder + "/" + *itFile);
			file.remove();
			itFile++;
		}

		// Get list of all subdirectories
		infoList = dir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Hidden);
		QStringList dirs;
		foreach(QFileInfo info, infoList)
		{
			if(info.isDir())
				dirs << info.fileName();
		}
		QStringList::Iterator itDir = dirs.begin();
		while (itDir != dirs.end()) {
			if (*itDir != "." && *itDir != "..") {
				// and invoke remove for each
				removeFolder(folder + "/" + *itDir);
			}
			itDir++;
		}

		// Remove myself
		// Any dir always have two subdir as dotdir and dotanddotdir
		if (dir.count() == 2) {
			dir.rmdir(folder);
		}
	}

	bool copyFolder()
	{
		QQueue<QString> queue;

		queue.enqueue("");

		while (!queue.isEmpty()) {
			QString srcDirName = queue.dequeue();
			QDir srcDir(oldHomeDir_.path() + "/" + srcDirName);
			if (!srcDir.exists()) {
				continue;
			}

#if defined Q_WS_X11 || defined Q_WS_MAC
			// Skip cache data
			if (srcDir.dirName() == "tmp-contentdownloader" ||
				srcDir.dirName() == "tmp-sounds" ||
				srcDir.dirName() == "tmp-pics" ||
				srcDir.dirName() == "bob" ||
				srcDir.dirName() == "avatars" ||
				srcDir.dirName() == "vcard") {
				continue;
			}
#endif

			QFileInfoList infoList = srcDir.entryInfoList(QDir::Files | QDir::Hidden);
			QStringList files;
			foreach(QFileInfo info, infoList)
			{
				if(info.isFile() && info.isReadable())
					files << info.fileName();
			}

			for (int i = 0; i < files.count(); i++) {
				if (stop_) {
					return false;
				}
				QString srcFileName;
				srcFileName = srcDirName + "/" + files[i];

#if defined Q_WS_X11 || defined Q_WS_MAC
				// tune is cache.
				if (srcFileName == "/caps.xml" || srcFileName == "/tune") {
					continue;
				}
#endif

				QString dstDirName;
				QRegExp xp("^/psirc$|^/profiles/\\w*/accounts.xml$|^/profiles/\\w*/accounts.xml.backup$|"
						   "^/profiles/\\w*/options.xml$|^/profiles/\\w*/options.xml.backup$|^/profiles/\\w*/mucskipautojoin.txt$");
				if (srcFileName.contains(xp)) {
					dstDirName = configDir_.path() + "/" + srcDirName;
				}
				else {
					dstDirName = dataDir_.path() + "/" + srcDirName;
				}

				QDir dstDir(dstDirName);
				if (!dstDir.exists()) {
					dstDir.mkpath(".");
				}

				QString destFileName = dstDirName + "/" + files[i];
				if (!QFile::copy(oldHomeDir_.path() + srcFileName, destFileName)) {
					qFatal("Critical error while importing Psi/Psi+ profile");
					return false;
				}
			}

			infoList = srcDir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Hidden);
			QStringList dirs;
			foreach(QFileInfo info, infoList)
			{
				if(info.isDir())
					dirs << info.fileName();
			}

			for (int i = 0; i < dirs.count(); i++) {
				queue.enqueue(srcDirName + "/" + dirs[i]);
			}
		}
		return true;
	}

	HomeDirMigration::Choice choice_;
	QDir oldHomeDir_;
	QDir configDir_;
	QDir dataDir_;
	bool result_;
	bool stop_;
};

HomeDirMigration::HomeDirMigration(QWidget *parent) :
	QDialog(parent, Qt::CustomizeWindowHint | Qt::WindowTitleHint |
			Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint | Qt::WindowStaysOnTopHint),
	ui(new Ui::HomeDirMigration),
	choice_(COPY),
	thread_(NULL)
{
	configDir_ = ApplicationInfo::homeDir(ApplicationInfo::ConfigLocation);
	dataDir_ = ApplicationInfo::homeDir(ApplicationInfo::DataLocation);
}

bool HomeDirMigration::checkOldHomeDir()
{
#if defined Q_WS_X11 || defined Q_WS_MAC
	QString base = QDir::homePath();
	// First it need to check ~/.psi-plus folder for Fedora users.
	oldHomeDir_.setPath(base + "/.psi-plus");

	if (!oldHomeDir_.exists()) {
		oldHomeDir_.setPath(base + "/.psi");
	}
#elif defined Q_WS_WIN
	QString base = ((PsiApplication *)qApp)->portableBase();
	if (base.isEmpty()) {
		// Windows 9x
		if(QDir::homePath() == QDir::rootPath()) {
			base = ".";
		}
		// Windows NT/2K/XP variant
		else {
			base = QDir::homePath();
		}
	}
	// no trailing slash
	base = QDir::cleanPath(base);

	oldHomeDir_.setPath(base + "/PsiData");
#endif
	return oldHomeDir_.exists();
}

QString HomeDirMigration::oldHomeDir() const
{
	return oldHomeDir_.path();
}

void HomeDirMigration::threadFinish()
{
	ui->busy->stop();
	QDialog::accept();
}

int HomeDirMigration::exec()
{
	ui->setupUi(this);

	adjustSize();
	setFixedSize(size());
	QSignalMapper *mapper = new QSignalMapper(this);

	// Mapping to copy radio button
	connect(ui->rbCopy, SIGNAL(clicked()), mapper, SLOT(map()));
	mapper->setMapping(ui->rbCopy, COPY);

	// Mapping to move radio button
	connect(ui->rbMove, SIGNAL(clicked()), mapper, SLOT(map()));
	mapper->setMapping(ui->rbMove, MOVE);

	// Mapping to nothing radio button
	connect(ui->rbNothing, SIGNAL(clicked()), mapper, SLOT(map()));
	mapper->setMapping(ui->rbNothing, NOTHING);

	connect(mapper, SIGNAL(mapped(int)), SLOT(setChoice(int)));

	return QDialog::exec();
}

void HomeDirMigration::closeEvent(QCloseEvent *event)
{
	event->ignore();
	if (!thread_) {
		exit(0);
	}
	else {
		Thread *t = (Thread*)thread_;
		t->stop();
		t->wait();
		exit(0);
	}
}

void HomeDirMigration::accept()
{
	if (choice_ == COPY || choice_ == MOVE){
		thread_ = new Thread(choice_, oldHomeDir_, configDir_, dataDir_, this);
		connect(thread_, SIGNAL(finished()), SLOT(threadFinish()));
		thread_->start();
		ui->busy->start();
		setEnabled(false);
	}
	else {
		QDialog::accept();
	}
}

void HomeDirMigration::setChoice(int choice)
{
	choice_ = (Choice)choice;
}

HomeDirMigration::~HomeDirMigration()
{
	delete ui;
}
