#include "k9cddrives.h"

k9CdDrive::k9CdDrive() {
    canReadDVD=false;
    canWriteCDR=false;
    canWriteDVD=false;
    device="";
    name="";
}
k9CdDrive::~k9CdDrive() {}

int k9CdDrives::count() {
    return drives.count();
}

/** No descriptions */
k9CdDrive * k9CdDrives::getDrive(int num) {
    return (k9CdDrive *)drives.at(num);
}


QList< int > k9CdDrive::getWriteSpeeds() const {
    return writeSpeeds;
}


void k9CdDrive::setWriteSpeeds(const QStringList& _value) {
    writeSpeeds.clear();
    foreach(QString s,_value) {
        writeSpeeds << s.toInt();
    }
}

void k9CdDrives::clearDrives() {
    while (!drives.isEmpty()) delete drives.takeFirst();
}

k9CdDrives::k9CdDrives() {
    k9CdDrives(NULL);
}

k9CdDrives::k9CdDrives(QObject *parent):QObject( parent) {
//    drives.setAutoDelete(true);
    m_halObject=new halObject(this);
    scanDrives();
}
void k9CdDrives::scanDrives() {
   clearDrives();
   connect(m_halObject,SIGNAL(deviceAdded(const QString &,const QString&,bool,const QStringList &)),this,SLOT(_deviceAdded(const QString &,const QString&,bool,const QStringList&)));
   m_halObject->getDriveList(false);
   m_halObject->getVolumeList();
   disconnect(m_halObject,SIGNAL(deviceAdded(const QString &,const QString&,bool,const QStringList&)),this,SLOT(_deviceAdded(const QString &,const QString&,bool,const QStringList&)));

}


k9CdDrives::~k9CdDrives() {
    clearDrives();
}

void k9CdDrive::_volumeChanged(const QString &_device,const QString &_volumeName){
    if(_device==device)
        emit volumeChanged(_device,_volumeName);
}

void k9CdDrives::_deviceAdded( const QString &device,const QString &label,bool burnDevice,const QStringList &writeSpeeds) {
    k9CdDrive *drive=new k9CdDrive;
    connect(m_halObject,SIGNAL(discInserted(QString,QString)),drive,SLOT(_volumeChanged(QString,QString)));
    //drive->setDevice( device);
    drive->device=device;
    drive->name=label;
    drive->canReadDVD= true;
    drive->canWriteCDR=burnDevice;
    drive->canWriteDVD=burnDevice;
    drive->setWriteSpeeds(writeSpeeds);

    drive->num=drives.count();
    drives.append(drive);
    emit deviceAdded(drive);

}

void k9CdDrives::eject(const QString & device) {
    k9Process *process =new k9Process();
    if (k9Tools::checkProgram("kdeeject"))
        *process <<"kdeeject" << device;
    else
        *process <<"eject" << device;
    process->start();
    process->wait();
    delete process;
}
