/*
 * 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 <dbsdglobal.h>

#include <variables.h>
#include <disk.h>

/* Qt */
#include <qpixmap.h>

void modDiskSel::apply() {
	int i = lstDisks->currentItem();

	if(devNames.count() > i)
		Variables::setValue("disk.activeDisk", devNames[i]);
}

void  modDiskSel::initModule() {
	DBSDIconLoader  *iloader = DBSDGlobal::getIconLoader();
	QPixmap  pixRAID(iloader->loadIcon("raid", DBSDIconLoader::LargeListItem));
	QPixmap  pixDisk(iloader->loadIcon("hdd_unmount", DBSDIconLoader::LargeListItem));

	 QPtrList<Disk>  lst = Disk::getAllDevices();
	 lst.setAutoDelete(true);
	 for(Disk  *dsk = lst.last();  dsk != NULL;  dsk = lst.prev()) {
		  QString	desc;
		  QPixmap  *pix = NULL;

		  switch(dsk->getDeviceType()) {
		  case Device::ATA_RAID:
				// desc = "RAID, ";
				desc = dsk->getDesc();
				pix = &pixRAID;
				break;

		  case Device::ATA_Disk:
		  case Device::IDE_WD100:
		  case Device::SCSI_DirectAccess:
				desc = dsk->getDesc();
				pix = &pixDisk;
				break;

		  default:
				break;
		  }

		  if(!desc.isNull() && pix != NULL) {
				Size::Unit unit = (dsk->size().get(Size::GB) < 10) ? Size::MB : Size::GB;
				lstDisks->insertItem(*pix, tr("%1 [%2]").arg(desc)
																	 .arg(dsk->size().toString(unit)));
				devNames.append(dsk->getName());
		  }
	 }

	 lstDisks->setSelected(0, true);
}

bool  modDiskSel::exitReady() {
	apply();

	return  true;
}
