File indexing completed on 2024-04-28 04:49:50

0001 /*
0002     SPDX-FileCopyrightText: 1998-2008 Sebastian Trueg <trueg@k3b.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "k3bdevicemodel.h"
0007 #include "k3bmedium.h"
0008 #include "k3bmediacache.h"
0009 #include "k3bcore.h"
0010 #include "k3bdevice.h"
0011 #include "k3b_i18n.h"
0012 
0013 
0014 class K3b::DeviceModel::Private
0015 {
0016 public:
0017     QList<K3b::Device::Device*> devices;
0018     QHash<K3b::Device::Device*, bool> devicesValid;
0019 };
0020 
0021 
0022 K3b::DeviceModel::DeviceModel( QObject* parent )
0023     : QAbstractItemModel( parent ),
0024       d( new Private() )
0025 {
0026     connect( k3bcore->mediaCache(), SIGNAL(mediumChanged(K3b::Device::Device*)),
0027              this, SLOT(slotMediumChanged(K3b::Device::Device*)) );
0028     connect( k3bcore->mediaCache(), SIGNAL(checkingMedium(K3b::Device::Device*,QString)),
0029              this, SLOT(slotCheckingMedium(K3b::Device::Device*,QString)) );
0030 }
0031 
0032 
0033 K3b::DeviceModel::~DeviceModel()
0034 {
0035     delete d;
0036 }
0037 
0038 
0039 void K3b::DeviceModel::setDevices( const QList<K3b::Device::Device*>& devices )
0040 {
0041     beginResetModel();
0042     d->devices = devices;
0043     foreach( K3b::Device::Device* dev, devices ) {
0044         d->devicesValid[dev] = true;
0045     }
0046     endResetModel();
0047 }
0048 
0049 
0050 void K3b::DeviceModel::addDevice( K3b::Device::Device* dev )
0051 {
0052     if ( !d->devices.contains( dev ) ) {
0053         beginResetModel();
0054         d->devices.append( dev );
0055         endResetModel(); // hardcore reset since entries might change
0056     }
0057 }
0058 
0059 
0060 void K3b::DeviceModel::removeDevice( K3b::Device::Device* dev )
0061 {
0062     if ( d->devices.contains( dev ) ) {
0063         beginResetModel();
0064         d->devices.removeOne( dev );
0065         endResetModel();
0066     }
0067 }
0068 
0069 
0070 void K3b::DeviceModel::addDevices( const QList<K3b::Device::Device*>& devs )
0071 {
0072     beginResetModel();
0073     Q_FOREACH( K3b::Device::Device* dev, devs ) {
0074         if ( !d->devices.contains( dev ) ) {
0075             d->devices.append( dev );
0076         }
0077     }
0078     endResetModel();
0079 }
0080 
0081 
0082 void K3b::DeviceModel::clear()
0083 {
0084     beginResetModel();
0085     d->devices.clear();
0086     endResetModel();
0087 }
0088 
0089 
0090 QList<K3b::Device::Device*> K3b::DeviceModel::devices() const
0091 {
0092     return d->devices;
0093 }
0094 
0095 
0096 K3b::Device::Device* K3b::DeviceModel::deviceForIndex( const QModelIndex& index ) const
0097 {
0098     return static_cast<K3b::Device::Device*>( index.internalPointer() );
0099 }
0100 
0101 
0102 QModelIndex K3b::DeviceModel::indexForDevice( K3b::Device::Device* dev ) const
0103 {
0104     for ( int i = 0; i < d->devices.count(); ++i ) {
0105         if ( d->devices[i] == dev ) {
0106             return createIndex( i, 0, dev );
0107         }
0108     }
0109     return QModelIndex();
0110 }
0111 
0112 
0113 int K3b::DeviceModel::columnCount( const QModelIndex& parent ) const
0114 {
0115     Q_UNUSED( parent );
0116     // TODO: allow multiple columns at some point (so far we do not need it)
0117     return 1;
0118 }
0119 
0120 
0121 QVariant K3b::DeviceModel::data( const QModelIndex& index, int role ) const
0122 {
0123     K3b::Device::Device* dev = deviceForIndex( index );
0124     K3b::Medium medium = k3bcore->mediaCache()->medium( dev );
0125 
0126     switch( role ) {
0127     case Qt::DisplayRole:
0128         if ( d->devicesValid[dev] ) {
0129             return medium.shortString();
0130         }
0131         else {
0132             return i18n( "Analyzing medium..." );
0133         }
0134 
0135     case Qt::DecorationRole:
0136         return medium.icon();
0137 
0138     case IsDevice:
0139         return true;
0140 
0141     case Vendor:
0142         return dev->vendor();
0143 
0144     case Description:
0145         return dev->description();
0146 
0147     case BlockDevice:
0148         return dev->blockDeviceName();
0149 
0150     case Valid:
0151         return d->devicesValid[dev];
0152 
0153     default:
0154         return QVariant();
0155     }
0156 }
0157 
0158 
0159 QModelIndex K3b::DeviceModel::index( int row, int column, const QModelIndex& parent ) const
0160 {
0161     Q_UNUSED( parent );
0162     return row < d->devices.count() ? createIndex( row, column, d->devices[row] ) : QModelIndex();
0163 }
0164 
0165 
0166 QModelIndex K3b::DeviceModel::parent( const QModelIndex& index ) const
0167 {
0168     Q_UNUSED( index );
0169     return QModelIndex();
0170 }
0171 
0172 
0173 int K3b::DeviceModel::rowCount( const QModelIndex& parent ) const
0174 {
0175     if ( !parent.isValid() ) {
0176         return d->devices.count();
0177     }
0178     else {
0179         return 0;
0180     }
0181 }
0182 
0183 
0184 void K3b::DeviceModel::slotMediumChanged( K3b::Device::Device* dev )
0185 {
0186     QModelIndex index = indexForDevice( dev );
0187     if ( index.isValid() ) {
0188         d->devicesValid[dev] = true;
0189         emit dataChanged( index, index );
0190     }
0191 }
0192 
0193 
0194 void K3b::DeviceModel::slotCheckingMedium( K3b::Device::Device* dev, const QString& /*message*/ )
0195 {
0196     QModelIndex index = indexForDevice( dev );
0197     if ( index.isValid() ) {
0198         d->devicesValid[dev] = false;
0199         emit dataChanged( index, index );
0200     }
0201 }
0202 
0203 #include "moc_k3bdevicemodel.cpp"