/***************************************************************************
 *   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 "veneer/device.h"

#include <solid/device.h>
#include <solid/storageaccess.h>
#include <solid/block.h>

#include <QList>
#include <QString>
#include <QDebug>

namespace Veneer
{
	QList<Device> listHotPluggables()
	{
		QList<Device> rval;

		QList<Solid::Device> hotpluggableDrives = Solid::Device::listFromQuery("StorageDrive.hotpluggable == true");

		foreach(const Solid::Device& solidStorage, hotpluggableDrives)
		{
			if (!solidStorage.is<Solid::Block>())
				continue;

			const QString deviceName = solidStorage.as<Solid::Block>()->device();

			const QString predicate = QString("[ StorageVolume.usage == 'FileSystem' AND Block.device == '%1']").arg(deviceName);

			QList<Solid::Device> solidVolumes = Solid::Device::listFromQuery(predicate);

			if (solidVolumes.size() != 1)
			{
// 				qDebug() << "not one but " << solidVolumes.size() << " devices found for " << deviceName;
				continue;
			}

			if (!solidVolumes[0].is<Solid::StorageAccess>())
			{
// 				qDebug() << deviceName << " is not a storage access device";
				continue;
			}

			Solid::StorageAccess* access = solidVolumes[0].as<Solid::StorageAccess>();

			if (!access->isAccessible())
			{
// 				qDebug() << deviceName << " is not mounted.";
				continue;
			}

			rval.append(Device(solidStorage, solidVolumes[0]));
		}

		return rval;
	}

	// ----------------------------------------------------------------------

	Device::Device() :
		m_SolidStorage(),
		m_SolidVolume(),
		m_BlockDevice(NULL),
		m_StorageAccess(NULL)
	{
	}

	Device::Device(const Solid::Device& solidStorage_, const Solid::Device& solidVolume_) :
		m_SolidStorage(solidStorage_),
		m_SolidVolume(solidVolume_),
		m_BlockDevice(NULL),
		m_StorageAccess(NULL)
	{
	}

	QString Device::vendor() const
	{
		return m_SolidStorage.vendor();
	}

	QString Device::product() const
	{
		return m_SolidStorage.product();
	}

	QString Device::icon() const
	{
		return m_SolidVolume.icon();
	}

	QString Device::devicePath() const
	{
		if (m_BlockDevice == NULL)
			m_BlockDevice = m_SolidVolume.as<Solid::Block>();

		if (!m_BlockDevice)
			return QString();

		return m_BlockDevice->device();
	}

	QString Device::mountPoint() const
	{
		if (m_StorageAccess == NULL)
			m_StorageAccess = m_SolidVolume.as<Solid::StorageAccess>();

		if (m_StorageAccess && m_StorageAccess->isAccessible())
			return m_StorageAccess->filePath();

		return QString();
	}

}
