/***************************************************************************
 *   Copyright (C) 2010 by Volker Lanz <vl@fidra.de>                       *
 *                                                                         *
 *   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 program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
 ***************************************************************************/

#include "devicesync/sync.h"
#include "devicesync/syncsource.h"
#include "devicesync/garminedgesync.h"

#include "veneer/device.h"

#include <QXmlStreamReader>
#include <QFile>
#include <QStringList>
#include <QDebug>

Sync::Sync() :
	m_Activities(),
	m_SyncInterfaces(),
	m_RunningInterface(NULL),
	m_ActiveSource(NULL),
	m_Status(Success)
{
	m_SyncInterfaces.append(new GarminEdgeSync());

	foreach(SyncInterface* imp, m_SyncInterfaces)
	{
		connect(imp, SIGNAL(progress(int)), SIGNAL(progress(int)));
		connect(imp, SIGNAL(totalSteps(int)), SIGNAL(totalSteps(int)));
	}

	scan();
}

Sync::~Sync()
{
	qDeleteAll(m_SyncInterfaces);
	qDeleteAll(m_Sources);
}

void Sync::addDevice(const Veneer::Device& device)
{
	if (device.mountPoint().isEmpty())
		return;

	foreach(SyncInterface* imp, m_SyncInterfaces)
		if (imp->canSync(Veneer::Device(device)))
			m_Sources.append(new SyncSource(QString("%1 %2").arg(device.vendor()).arg(device.product()), device.mountPoint(), imp, device.icon()));
}

void Sync::scan()
{
	qDeleteAll(m_Sources);
	m_Sources.clear();

	QList<Veneer::Device> hotpluggableDrives = Veneer::listHotPluggables();

	foreach (const Veneer::Device& d, hotpluggableDrives)
		addDevice(d);
}

void Sync::run()
{
	Q_ASSERT(m_ActiveSource);

	if (m_ActiveSource == NULL)
	{
		qWarning() << "no active source.";
		return;
	}

	m_Status = m_ActiveSource->syncInterface()->run(m_ActiveSource->mountPoint(), m_Activities);
}

const QStringList Sync::errorNames() const
{
	if (m_ActiveSource)
		return m_ActiveSource->syncInterface()->errorNames();

	return QStringList();
}

QString Sync::errorMsg(SyncStatus s)
{
	switch (s)
	{
		case Success:             return tr("Success");
		case ReadError:           return tr("Read error");
	}

	return tr("Unknown error");
}
