OSDLCDROMDriveHandler.cc

Go to the documentation of this file.
00001 #include "OSDLCDROMDriveHandler.h"
00002 
00003 #include "OSDLCDROMDrive.h"  // for CDROMDrive
00004 
00005 #include "OSDLBasic.h"       // for CommonModule
00006 #include "OSDLUtils.h"       // for getBackendLastError
00007 
00008 
00009 #include "SDL.h"             // for CD-ROM primitives
00010 
00011 #include <list>  
00012 
00013 using std::list ;
00014 using std::string ;
00015 using std::map ;
00016 
00017 
00018 using namespace OSDL ;
00019 
00020 
00021 
00022 CDROMDriveException::CDROMDriveException( const std::string & message ) 
00023         throw() :
00024     OSDL::Exception( message )
00025 {
00026 
00027 }
00028 
00029 
00030 CDROMDriveException::~CDROMDriveException() throw()
00031 {
00032 
00033 }
00034 
00035 
00036 
00037 
00038 CDROMDriveHandler::CDROMDriveHandler() throw( CDROMDriveException ) :
00039      Object(),
00040      _drives()
00041 {
00042     
00043     send( "Initializing CD-ROM subsystem" ) ;
00044         
00045     if ( SDL_InitSubSystem( CommonModule::UseCDROM ) 
00046             != CommonModule::BackendSuccess ) 
00047         throw CDROMDriveException( "CDROMDriveHandler constructor : "
00048                 "unable to initialize CD-ROM subsystem : " 
00049                 + Utils::getBackendLastError() ) ;
00050                 
00051     send( "CD-ROM subsystem initialized" ) ;                
00052 
00053 }
00054 
00055 
00056 CDROMDriveHandler::~CDROMDriveHandler() throw()
00057 {
00058 
00059     send( "Stopping CD-ROM subsystem." ) ;
00060 
00061     for ( map<CDROMDriveNumber, CDROMDrive *>::const_iterator it 
00062         = _drives.begin() ; it != _drives.end(); it++ )
00063     {
00064         delete (*it).second ;
00065     }
00066     
00067     // Useless but deemed safer :
00068     _drives.clear() ;
00069         
00070     SDL_QuitSubSystem( CommonModule::UseCDROM ) ;
00071 
00072     send( "CD-ROM subsystem stopped." ) ;
00073 
00074 }
00075             
00076 
00077 
00078 CDROMDriveNumber CDROMDriveHandler::GetAvailableCDROMDrivesCount() throw()
00079 {
00080     return SDL_CDNumDrives() ;
00081 }
00082 
00083 
00084 CDROMDrive & CDROMDriveHandler::getCDROMDrive( CDROMDriveNumber number ) 
00085     throw( CDROMDriveException )
00086 {
00087     
00088     map<CDROMDriveNumber, CDROMDrive *>::const_iterator it 
00089         = _drives.find( number ) ;
00090     
00091     if ( it != _drives.end() )
00092     {
00093         
00094         // Drive found :
00095         return * (*it).second ;
00096     
00097     }
00098         
00099     // Else : drive not already created, let's try to create it :
00100     CDROMDrive * newDrive = new CDROMDrive( number ) ;
00101     
00102     /*
00103      * Here the creation succeeded (otherwise an exception is propagated 
00104      * as expected) :
00105      *
00106      */
00107     _drives[ number ] = newDrive ;
00108     
00109     return *newDrive ;
00110     
00111 }
00112 
00113                 
00114 const std::string CDROMDriveHandler::toString( Ceylan::VerbosityLevels level )
00115     const throw()
00116 {
00117         
00118     CDROMDriveNumber driveNumber = GetAvailableCDROMDrivesCount() ;
00119     
00120     if ( driveNumber == 0 )
00121         return "No available CD-ROM drive found" ;
00122     
00123     if ( _drives.empty() )
00124         return "None of the " + Ceylan::toString( driveNumber ) 
00125             + " available drive(s) is currently opened." ;
00126     
00127     // At least a drive is available, at least one is opened :
00128             
00129     list<string> descriptions ;
00130     
00131     for ( map<CDROMDriveNumber, CDROMDrive *>::const_iterator it 
00132         = _drives.begin() ; it != _drives.end(); it++ )
00133     {
00134         descriptions.push_back( (*it).second->toString() ) ;
00135     }
00136     
00137     return "Out of " + Ceylan::toString( driveNumber ) 
00138         + " available CD-ROM drive(s), " 
00139         + Ceylan::toString( _drives.size() ) + " is/are opened : " 
00140         + Ceylan::formatStringList( descriptions ) ;    
00141 
00142 }               
00143             

Generated on Fri Mar 30 14:46:59 2007 for OSDL by  doxygen 1.5.1