4 #include "scanwidget.h"
6 #define HAL_SERVICE "org.freedesktop.Hal"
7 #define HAL_PATH_MANAGER "/org/freedesktop/Hal/Manager"
8 #define HAL_PATH_DEVICE "/org/freedesktop/Hal/Device"
9 #define HAL_OBJECT_MANAGER "org.freedesktop.Hal.Manager"
10 #define HAL_OBJECT_DEVICE "org.freedesktop.Hal.Device"
12 ClickedField::ClickedField(QRadioButton *b, QObject *parent)
18 bool ClickedField::eventFilter(QObject *o, QEvent *e)
20 if (e->type() == QEvent::FocusIn) {
21 rb->setChecked( true );
24 return QObject::eventFilter(o, e);
27 ScanWidget::ScanWidget( QWidget *parent )
32 deviceRadioButton->setChecked( true );
33 devices = new QMap<QString, QString>;
35 deviceComboBox->installEventFilter( new ClickedField(deviceRadioButton, this) );
36 folderLineEdit->installEventFilter( new ClickedField(folderRadioButton, this) );
37 folderPushButton->installEventFilter( new ClickedField(folderRadioButton, this) );
39 progress->setMaximum( 1 );
41 updateVolumeList("bar");
43 connect( scanButton, SIGNAL( clicked() ), this, SLOT( goScan() ) );
44 connect( folderPushButton, SIGNAL( clicked() ), this, SLOT( setFolder() ) );
47 QMap<QString, QString> *ScanWidget::volumeList()
49 if( devices->count() > 0 )
54 if( !QDBusConnection::systemBus().isConnected() ) {
55 qDebug("Cannot connect to D-BUS session bus.");
59 QDBusInterface hal( HAL_SERVICE, HAL_PATH_MANAGER , HAL_OBJECT_MANAGER, QDBusConnection::systemBus() );
60 if (!hal.isValid() ) {
61 qDebug( "Couldn't find HAL. Is HAL running?" );
65 QDBusReply<QStringList> reply = hal.call( "FindDeviceStringMatch", "volume.disc.type", "dvd_rom" );
66 if( !reply.isValid() ) {
67 qDebug( "Couldn't call FindDeviceStringMatch." );
71 if( reply.value().count() > 0 ) {
72 foreach ( QString udi, reply.value() ) {
73 QDBusInterface halDev( HAL_SERVICE, udi, HAL_OBJECT_DEVICE, QDBusConnection::systemBus() );
74 QDBusReply<bool> isVideo = halDev.call( "GetProperty", "volume.disc.is_videodvd" );
75 if( !isVideo.value() ) {
76 qDebug("This is a DVD, but not video");
79 QDBusReply<QString> d = halDev.call( "GetProperty", "block.device" );
80 QDBusReply<QString> v = halDev.call( "GetProperty", "volume.label" );
81 QRegExp rx("_S(\\d+)_D(\\d+)");
82 QString label = v.value();
84 if (rx.numCaptures() > 0) {
85 label = label.replace(rx, QString(" Season %1 Disc %2").arg(rx.cap(1)).arg(rx.cap(2)));
87 label = label.replace("_", " ");
89 devices->insert( d.value(), label );
94 devices->insert( "none detected", "Insert a DVD..." );
100 void ScanWidget::updateVolumeList(QString)
102 if( devices->count() > 0 )
105 deviceComboBox->clear();
108 QMapIterator<QString, QString> i( *volumeList() );
109 while (i.hasNext()) {
111 d << QString("%1 (%2)").arg(i.value()).arg(i.key());
113 deviceComboBox->addItems(d);
116 void ScanWidget::goScan()
118 scanButton->setDisabled( true );
120 if( deviceRadioButton->isChecked() )
122 QRegExp rx(".*\\((.*)\\)");
123 rx.indexIn(deviceComboBox->currentText());
124 emit scanDVD( rx.cap(1) );
126 else if( folderRadioButton->isChecked() )
128 emit scanDVD( folderLineEdit->text() );
132 void ScanWidget::setProgress( int value, int maximum )
134 if( progress->maximum() == 1 )
136 progress->setMaximum( maximum );
139 progress->setValue( value );
141 if( value == maximum )
147 void ScanWidget::setFolder()
149 QString folderPath = QFileDialog::getExistingDirectory();
150 folderLineEdit->setText( folderPath );