/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2012 - 2013                                                                        *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
	Description:
	Cette classe vérifie constamment la présence de la clef USB et permet d'obtenir
	le répertoire visé par la clef.
	
*/

/* ************************************************************************** */
/* Revision:
### YYYMMDD JFM
	Verision d'origine.
	
### YYYYMMDD Description du besoin ou du bug
	Description du changement.
 */

/* ************************************************************************** */

#include "USBDriveInterface.h"
#ifndef WINDOWS_OS
#include <mntent.h>
#endif
#include "EngLog.h"

CUSBDriveInterface::CUSBDriveInterface()
{

	mDriveDetected = false;
	mDriveEmulationEnabled = false;

	mDrivePresenceUpdateTimer = new QTimer();
	mDrivePresenceUpdateTimer->setSingleShot(false);
	mDrivePresenceUpdateTimer->setInterval(1000);
	connect(mDrivePresenceUpdateTimer,SIGNAL(timeout()),this,SLOT(CheckDrivePresence()));

}

CUSBDriveInterface::~CUSBDriveInterface()
{
	mDrivePresenceUpdateTimer->stop();
	delete mDrivePresenceUpdateTimer;
}

unsigned int CUSBDriveInterface::Start()
{
	mDrivePresenceUpdateTimer->start();
	return RET_OK;
}

void CUSBDriveInterface::CheckDrivePresence()
{
	if(mDriveEmulationEnabled == true)
	{

	}
	else
	{
#ifdef WINDOWS_OS
		mDriveDetected = false;
#else
		FILE* mtab = setmntent("/etc/mtab", "r");
		struct mntent* m;
		struct mntent mnt;
		char strings[4096];

		//parse the mounted drives list
		while ((m = getmntent_r(mtab, &mnt, strings, sizeof(strings))))
		{
			if ((mnt.mnt_dir != NULL) /*&& (statfs(mnt.mnt_dir, &fs) == 0)*/)
			{
				QString type = (mnt.mnt_type);

				//filter the "vfat" drive type (usb flash drive is vfat)
				if(type == "vfat")
				{

					QString temp(mnt.mnt_dir);
					if(temp.contains("/media/usb"))
					{
						if(mDriveDetected == false)
						{
							mDriveDetected = true;
							mDrivePath = temp;
							qDebug("Flash Detected: %s",mDrivePath.toAscii().data());
							CEngLog::instance()->AddLogString("Clef USB détectée");
						}
						endmntent(mtab);

						return;
					}

				}
			}
		}

		if(mDriveDetected == true)
		{
			mDriveDetected = false;
			mDrivePath.clear();
			qDebug("Flash drive disconnected");
			CEngLog::instance()->AddLogString("Clef USB débranchée");
		}
		endmntent(mtab);

#endif
	 }
}

QString CUSBDriveInterface::GetDrivePath()
{
	return mDrivePath;
}

bool CUSBDriveInterface::IsDriveDetected()
{
	return mDriveDetected;
}

unsigned int CUSBDriveInterface::EmulateDrivePresence(bool DrivePresent)
{
	if(mDriveEmulationEnabled == false)
		return RET_ERROR;

	mDriveDetected = DrivePresent;

	return RET_OK;
}

unsigned int CUSBDriveInterface::EnableDriveEmulation(bool Enabled)
{
	mDriveEmulationEnabled = Enabled;

	if(Enabled == true)
	{
		mDrivePath = ".";
	}
	else
	{
		mDriveDetected = false;
	}
	return RET_OK;

}
