File indexing completed on 2024-10-06 04:25:57

0001 /*
0002     SPDX-FileCopyrightText: 2010 Michal Malek <michalm@jabster.pl>
0003     SPDX-FileCopyrightText: 1998-2008 Sebastian Trueg <trueg@k3b.org>
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "k3bappdevicemanager.h"
0008 
0009 #include "k3baction.h"
0010 #include "k3bdevice.h"
0011 #include "k3bdevicehandler.h"
0012 #include "k3bglobals.h"
0013 #include "k3bapplication.h"
0014 #include "k3bmediacache.h"
0015 
0016 #include <KLocalizedString>
0017 #include <KIO/Job>
0018 #include <KMessageBox>
0019 #include <KActionCollection>
0020 #include <Solid/Block>
0021 #include <Solid/Device>
0022 #include <Solid/StorageAccess>
0023 
0024 #include <QAction>
0025 #include <QInputDialog>
0026 
0027 class K3b::AppDeviceManager::Private
0028 {
0029 public:
0030     QAction* actionDiskInfo;
0031     QAction* actionUnmount;
0032     QAction* actionMount;
0033     QAction* actionEject;
0034     QAction* actionLoad;
0035     QAction* actionSetReadSpeed;
0036 
0037     K3b::Device::Device* currentDevice;
0038 };
0039 
0040 K3b::AppDeviceManager::AppDeviceManager( QObject* parent )
0041     : K3b::Device::DeviceManager( parent ),
0042       d( new Private )
0043 {
0044     // setup actions
0045     d->actionDiskInfo = K3b::createAction(this, i18n("Media &Info"), "document-properties", 0, this, SLOT(diskInfo()),
0046                                          actionCollection(), "device_diskinfo");
0047     d->actionUnmount = K3b::createAction(this,  i18n("&Unmount"), "media-optical", 0, this, SLOT(unmountDisk()),
0048                                         actionCollection(), "device_unmount");
0049     d->actionMount = K3b::createAction(this, i18n("&Mount"), "media-optical", 0, this, SLOT(mountDisk()),
0050                                       actionCollection(), "device_mount");
0051     d->actionEject = K3b::createAction(this, i18n("&Eject"), "media-eject", 0, this, SLOT(ejectDisk()),
0052                                       actionCollection(), "device_eject");
0053     d->actionLoad = K3b::createAction(this, i18n("L&oad"), 0, 0, this, SLOT(loadDisk()),
0054                                      actionCollection(), "device_load");
0055 //   QAction* actionUnlock = new QAction( i18n("Un&lock"), "", 0, this, SLOT(unlockDevice()),
0056 //                     actionCollection(), "device_unlock" );
0057 //   QAction* actionlock = new QAction( i18n("Loc&k"), "", 0, this, SLOT(lockDevice()),
0058 //                   actionCollection(), "device_lock" );
0059     d->actionSetReadSpeed = K3b::createAction(this, i18n("Set Read Speed..."), 0, 0, this, SLOT(setReadSpeed()),
0060                                              actionCollection(), "device_set_read_speed" );
0061 
0062     d->actionDiskInfo->setToolTip( i18n("Display generic medium information") );
0063     d->actionDiskInfo->setStatusTip( d->actionDiskInfo->toolTip() );
0064     d->actionUnmount->setToolTip( i18n("Unmount the medium") );
0065     d->actionUnmount->setStatusTip( d->actionUnmount->toolTip() );
0066     d->actionMount->setToolTip( i18n("Mount the medium") );
0067     d->actionMount->setStatusTip( d->actionMount->toolTip() );
0068     d->actionEject->setToolTip( i18n("Eject the medium") );
0069     d->actionEject->setStatusTip( d->actionEject->toolTip() );
0070     d->actionLoad->setToolTip( i18n("(Re)Load the medium") );
0071     d->actionLoad->setStatusTip( d->actionLoad->toolTip() );
0072     d->actionSetReadSpeed->setToolTip( i18n("Force the drive's read speed") );
0073     d->actionSetReadSpeed->setStatusTip( d->actionSetReadSpeed->toolTip() );
0074 
0075     setXMLFile( "k3bdeviceui.rc", true );
0076 
0077     d->currentDevice = 0;
0078     slotMediumChanged( 0 );
0079 }
0080 
0081 
0082 K3b::AppDeviceManager::~AppDeviceManager()
0083 {
0084     delete d;
0085 }
0086 
0087 
0088 void K3b::AppDeviceManager::setMediaCache( K3b::MediaCache* c )
0089 {
0090     connect( c, SIGNAL(mediumChanged(K3b::Device::Device*)),
0091              this, SLOT(slotMediumChanged(K3b::Device::Device*)) );
0092 }
0093 
0094 
0095 K3b::Device::Device* K3b::AppDeviceManager::currentDevice() const
0096 {
0097     return d->currentDevice;
0098 }
0099 
0100 
0101 void K3b::AppDeviceManager::clear()
0102 {
0103     // make sure we do not use a deleted device
0104     setCurrentDevice( 0 );
0105     K3b::Device::DeviceManager::clear();
0106 }
0107 
0108 K3b::Device::Device* K3b::AppDeviceManager::addDevice( const Solid::Device& solidDev )
0109 {
0110     K3b::Device::Device* dev = K3b::Device::DeviceManager::addDevice( solidDev );
0111     if( dev && currentDevice() == 0 ) {
0112         setCurrentDevice( dev );
0113     }
0114     return dev;
0115 }
0116 
0117 void K3b::AppDeviceManager::removeDevice( const Solid::Device& solidDev )
0118 {
0119     if( const Solid::Block* blockDevice = solidDev.as<Solid::Block>() ) {
0120         if( findDevice( blockDevice->device() ) == currentDevice() )
0121             setCurrentDevice( 0 );
0122 
0123         K3b::Device::DeviceManager::removeDevice( solidDev );
0124 
0125         if( currentDevice() == 0 && !allDevices().isEmpty() )
0126             setCurrentDevice( allDevices().first() );
0127     }
0128 }
0129 
0130 
0131 void K3b::AppDeviceManager::slotMediumChanged( K3b::Device::Device* dev )
0132 {
0133     if( dev == currentDevice() ) {
0134 
0135         d->actionDiskInfo->setEnabled( dev != 0 );
0136         d->actionEject->setEnabled( dev != 0 );
0137         d->actionLoad->setEnabled( dev != 0 );
0138         d->actionSetReadSpeed->setEnabled( dev != 0 );
0139 
0140         if( dev ) {
0141             bool mediumMountable = k3bappcore->mediaCache()->medium( dev ).content() & K3b::Medium::ContentData;
0142             d->actionMount->setEnabled( mediumMountable );
0143             d->actionUnmount->setEnabled( mediumMountable );
0144 
0145             disconnect( this, SLOT(slotMountChanged(bool,QString)) );
0146             disconnect( this, SLOT(slotMountFinished(Solid::ErrorType,QVariant,QString)) );
0147             disconnect( this, SLOT(slotUnmountFinished(Solid::ErrorType,QVariant,QString)) );
0148 
0149             Solid::StorageAccess* storage = dev->solidStorage();
0150             if( storage != 0 ) {
0151                 connect( storage, SIGNAL(accessibilityChanged(bool,QString)),
0152                         this, SLOT(slotMountChanged(bool,QString)) );
0153                 connect( storage, SIGNAL(setupDone(Solid::ErrorType,QVariant,QString)),
0154                         this, SLOT(slotMountFinished(Solid::ErrorType,QVariant,QString)) );
0155                 connect( storage, SIGNAL(teardownDone(Solid::ErrorType,QVariant,QString)),
0156                         this, SLOT(slotUnmountFinished(Solid::ErrorType,QVariant,QString)) );
0157                 d->actionMount->setVisible( !storage->isAccessible() );
0158                 d->actionUnmount->setVisible( storage->isAccessible() );
0159             }
0160             else {
0161                 d->actionMount->setVisible( true );
0162                 d->actionUnmount->setVisible( false );
0163             }
0164         }
0165         else {
0166             d->actionMount->setVisible( true );
0167             d->actionUnmount->setVisible( false );
0168             d->actionMount->setEnabled( false );
0169             d->actionUnmount->setEnabled( false );
0170         }
0171     }
0172 }
0173 
0174 
0175 void K3b::AppDeviceManager::slotMountChanged( bool accessible, const QString& )
0176 {
0177     d->actionMount->setVisible( !accessible );
0178     d->actionUnmount->setVisible( accessible );
0179 }
0180 
0181 
0182 void K3b::AppDeviceManager::slotMountFinished( Solid::ErrorType error, QVariant, const QString& )
0183 {
0184     if( currentDevice() != 0 ) {
0185         Solid::StorageAccess* storage = currentDevice()->solidStorage();
0186         if( error == Solid::NoError && storage != 0 ) {
0187             qDebug() << "Device mounted at " << storage->filePath();
0188             emit mountFinished( storage->filePath() );
0189         }
0190     }
0191 }
0192 
0193 void K3b::AppDeviceManager::slotUnmountFinished( Solid::ErrorType error, QVariant, const QString& )
0194 {
0195     emit unmountFinished( error == Solid::NoError );
0196 }
0197 
0198 
0199 void K3b::AppDeviceManager::setCurrentDevice( K3b::Device::Device* dev )
0200 {
0201     if( dev != currentDevice() ) {
0202         d->currentDevice = dev;
0203 
0204         emit currentDeviceChanged( currentDevice() );
0205         slotMediumChanged( currentDevice() );
0206     }
0207 }
0208 
0209 
0210 void K3b::AppDeviceManager::diskInfo()
0211 {
0212     if( currentDevice() ) {
0213         emit detectingDiskInfo( currentDevice() );
0214     }
0215 }
0216 
0217 
0218 void K3b::AppDeviceManager::unlockDevice()
0219 {
0220     if( currentDevice() )
0221         K3b::Device::unblock( currentDevice() );
0222 }
0223 
0224 
0225 void K3b::AppDeviceManager::lockDevice()
0226 {
0227     if( currentDevice() )
0228         K3b::Device::block( currentDevice() );
0229 }
0230 
0231 
0232 void K3b::AppDeviceManager::mountDisk()
0233 {
0234     if( currentDevice() ) {
0235         Solid::StorageAccess* storage = currentDevice()->solidStorage();
0236         if( storage != 0 ) {
0237             if( storage->isAccessible() )
0238                 emit mountFinished( storage->filePath() );
0239             else
0240                 storage->setup();
0241         }
0242     }
0243 }
0244 
0245 
0246 void K3b::AppDeviceManager::unmountDisk()
0247 {
0248     if ( currentDevice() ) {
0249         Solid::StorageAccess* storage = currentDevice()->solidStorage();
0250         if( storage != 0 ) {
0251             if( storage->isAccessible() )
0252                 storage->teardown();
0253             else
0254                 emit unmountFinished( true );
0255         }
0256     }
0257 }
0258 
0259 
0260 void K3b::AppDeviceManager::ejectDisk()
0261 {
0262     if ( currentDevice() )
0263         K3b::Device::eject( currentDevice() );
0264 }
0265 
0266 
0267 void K3b::AppDeviceManager::loadDisk()
0268 {
0269     if( currentDevice() )
0270         K3b::Device::reload( currentDevice() );
0271 }
0272 
0273 
0274 void K3b::AppDeviceManager::setReadSpeed()
0275 {
0276     if( currentDevice() ) {
0277         bool ok = false;
0278         int s = QInputDialog::getInt( nullptr,
0279                                       i18n("CD Read Speed"),
0280                                       i18n("<p>Please enter the preferred read speed for <b>%1</b>. "
0281                                            "This speed will be used for the currently mounted "
0282                                            "medium."
0283                                            "<p>This is especially useful to slow down the drive when "
0284                                            "watching movies which are read directly from the drive "
0285                                            "and the spinning noise is intrusive."
0286                                            "<p>Be aware that this has no influence on K3b since it will "
0287                                            "change the reading speed again when copying CDs or DVDs."
0288                                            ,currentDevice()->vendor() + ' ' + currentDevice()->description()),
0289                                       12,
0290                                       1,
0291                                       currentDevice()->maxReadSpeed(),
0292                                       1,
0293                                       &ok );
0294         if( ok ) {
0295             if( !currentDevice()->setSpeed( s*175, 0xFFFF ) )
0296                 KMessageBox::error( 0, i18n("Setting the read speed failed.") );
0297         }
0298     }
0299 }
0300 
0301 
0302 void K3b::AppDeviceManager::diskInfo( K3b::Device::Device* dev )
0303 {
0304     setCurrentDevice( dev );
0305     diskInfo();
0306 }
0307 
0308 
0309 void K3b::AppDeviceManager::unlockDevice( K3b::Device::Device* dev )
0310 {
0311     setCurrentDevice( dev );
0312     unlockDevice();
0313 }
0314 
0315 
0316 void K3b::AppDeviceManager::lockDevice( K3b::Device::Device* dev )
0317 {
0318     setCurrentDevice( dev );
0319     lockDevice();
0320 }
0321 
0322 
0323 void K3b::AppDeviceManager::mountDisk( K3b::Device::Device* dev )
0324 {
0325     setCurrentDevice( dev );
0326     mountDisk();
0327 }
0328 
0329 
0330 void K3b::AppDeviceManager::unmountDisk( K3b::Device::Device* dev )
0331 {
0332     setCurrentDevice( dev );
0333     unmountDisk();
0334 }
0335 
0336 
0337 void K3b::AppDeviceManager::ejectDisk( K3b::Device::Device* dev )
0338 {
0339     setCurrentDevice( dev );
0340     ejectDisk();
0341 }
0342 
0343 
0344 void K3b::AppDeviceManager::loadDisk( K3b::Device::Device* dev )
0345 {
0346     setCurrentDevice( dev );
0347     loadDisk();
0348 }
0349 
0350 
0351 void K3b::AppDeviceManager::setReadSpeed( K3b::Device::Device* dev )
0352 {
0353     setCurrentDevice( dev );
0354     setReadSpeed();
0355 }
0356 
0357 #include "moc_k3bappdevicemanager.cpp"