/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2006-2014  Georg Rudoy
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **********************************************************************/

#include "lifereaimportpage.h"
#include <QDomDocument>
#include <QDir>
#include <QFileDialog>
#include <QMessageBox>
#include <QSettings>
#include <QUrl>
#include <util/xpc/util.h>

namespace LeechCraft
{
namespace NewLife
{
namespace Importers
{
	LifereaImportPage::LifereaImportPage (QWidget *parent)
	: QWizardPage (parent)
	{
		Ui_.setupUi (this);
		Ui_.ImportSettings_->setText (Ui_.ImportSettings_->text ().arg ("Liferea"));

		setTitle (tr ("Liferea's feeds import"));
		setSubTitle (tr ("Select Liferea's base location and options"));
	}

	bool LifereaImportPage::CheckValidity (const QString& directory) const
	{
		if (!QFile::exists (directory + "/liferea.db"))
			return false;

		QString filename = directory + "/feedlist.opml";
		QFile file (filename);
		if (!file.exists () ||
				!file.open (QIODevice::ReadOnly))
			return false;

		QDomDocument document;
		if (!document.setContent (&file, true))
			return false;

		QDomElement root = document.documentElement ();
		if (root.tagName () != "opml")
			return false;

		QDomNodeList heads = root.elementsByTagName ("head");
		if (heads.size () != 1 || !heads.at (0).isElement ())
			return false;

		QDomNodeList bodies = root.elementsByTagName ("body");
		if (bodies.size () != 1 || !bodies.at (0).isElement ())
			return false;

		if (!bodies.at (0).toElement ().elementsByTagName ("outline").size ())
			return false;

		return true;
	}

	bool LifereaImportPage::isComplete () const
	{
		return CheckValidity (Ui_.FileLocation_->text ());
	}

	int LifereaImportPage::nextId () const
	{
		return -1;
	}

	void LifereaImportPage::initializePage ()
	{
		connect (wizard (),
				SIGNAL (accepted ()),
				this,
				SLOT (handleAccepted ()));

		connect (this,
				SIGNAL (gotEntity (const LeechCraft::Entity&)),
				wizard (),
				SIGNAL (gotEntity (const LeechCraft::Entity&)));

		QString defaultFile = GetSuggestion ();

		if (CheckValidity (defaultFile))
			Ui_.FileLocation_->setText (defaultFile);
	}

	void LifereaImportPage::on_Browse__released ()
	{
		QString filename = QFileDialog::getExistingDirectory (this,
				tr ("Select Liferea's directory"),
				GetSuggestion ());

		if (filename.isEmpty ())
			return;

		if (!CheckValidity (filename))
			QMessageBox::critical (this,
					"LeechCraft",
					tr ("The directory you've selected is not Liferea's directory."));
		else
			Ui_.FileLocation_->setText (filename);

		emit completeChanged ();
	}

	void LifereaImportPage::on_FileLocation__textEdited (const QString&)
	{
		emit completeChanged ();
	}

	void LifereaImportPage::handleAccepted ()
	{
		QString filename = Ui_.FileLocation_->text ();
		if (!CheckValidity (filename))
			return;

		Entity e = Util::MakeEntity (QUrl::fromLocalFile (filename + "/feedlist.opml"),
				QString (),
				FromUserInitiated,
				"text/x-opml");

		/*
		if (Ui_.ImportSettings_->checkState () == Qt::Checked)
		{
			QSettings settings (QDir::homePath () + "/.kde/share/config/akregatorrc",
					QSettings::IniFormat);
			if (settings.status () == QSettings::NoError)
			{
				if (settings.contains ("Show Tray Icon"))
					e.Additional_ ["ShowTrayIcon"] = settings.value ("Show Tray Icon");
				if (settings.contains ("Fetch On Startup"))
					e.Additional_ ["UpdateOnStartup"] = settings.value ("Fetch On Startup");
				if (settings.contains ("Auto Fetch Interval"))
					e.Additional_ ["UpdateTimeout"] = settings.value ("Auto Fetch Interval");

				settings.beginGroup ("Archive");
				if (settings.contains ("Max Article Number"))
					e.Additional_ ["MaxArticles"] = settings.value ("Max Article Number");
				if (settings.contains ("Max Article Age"))
					e.Additional_ ["MaxAge"] = settings.value ("Max Article Age");
				settings.endGroup ();

				e.Additional_ ["UserVisibleName"] = tr ("Akregator settings");
			}
			else
				QMessageBox::critical (0,
						"LeechCraft",
						tr ("Could not access or parse Akregator settings."));
		}
		*/

		emit gotEntity (e);
	}

	QString LifereaImportPage::GetSuggestion () const
	{
		QDir home = QDir::home ();
		QStringList entries = home.entryList (QStringList (".liferea_*"),
				QDir::Dirs | QDir::Hidden,
				QDir::Name);

		QString defaultFile;
		if (entries.size ())
			defaultFile = QDir::homePath () + "/" + entries.last ();

		return defaultFile;
	}
}
}
}
