/*
 * This file is part of DesktopBSD, see the README file.
 *
 * Author: Peter Hofer <ph@desktopbsd.net>
 *			(C) 2004, 2005
 *
 * Copyright: BSD, see the COPYING file included in this distribution
 *
 */

#include "device.h"

#include <qregexp.h>

#include <sysctl.h>

/** This changes the device this object represents.
  *
  * @attention Should only be used for operator=().
  *
  * @param name A device, e.g. 'fxp1'.
  */
void  Device::setDevice(QString name) {
	this->name = name;
	if(name.isNull()) {
		desc = QString::null;
		return;
	}

	/* Separate device name and number, e.g. bfe2 -> "bfe", num = "2" */
	uint  pos = name.find(QRegExp("[0-9]+$"));
	QString  num = name.mid(pos);
	name.truncate(pos);

	desc = SysCtl::getStrCtl("dev." + name + "." + num + ".%desc");

	driver = SysCtl::getStrCtl("dev." + name + "." + num + ".%driver");
	/* This should be correct in most cases */
	if(driver.isEmpty())
		driver = name;

	/* If reading the sysctl failed, use the driver name */
	if(desc.isEmpty())
		desc = driver;
}


Device&  Device::operator= (const Device& dev) {
	if(this != &dev) {
		name = dev.name;
		desc = dev.desc;
		driver = dev.driver;
	}

	return  *this;
}


Device::Type  Device::getDeviceType()  const {
	Type  devType;

	if(driver == "acd")		devType = ATAPI_CD;
	else if(driver == "ad")  devType = ATA_Disk;
	else if(driver == "ar")  devType = ATA_RAID;
	else if(driver == "cd")  devType = SCSI_CD;
	else if(driver == "da")  devType = SCSI_DirectAccess;
	else if(driver == "wd")  devType = IDE_WD100;
	else if(driver == "fd")  devType = Floppy;
	else devType = Unknown;

	return  devType;
}

