OSDN Git Service

gitattirubes test 2
[handbrake-jp/handbrake-jp.git] / qt4 / scanwidget.cpp
1 #include <QtGui>
2 #include <QtDBus>
3
4 #include "scanwidget.h"
5
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"
11
12 ClickedField::ClickedField(QRadioButton *b, QObject *parent)
13     : QObject(parent)
14 {
15     rb = b;
16 }
17
18 bool ClickedField::eventFilter(QObject *o, QEvent *e)
19 {
20     if (e->type() == QEvent::FocusIn) {
21         rb->setChecked( true );
22         return false;
23     }
24     return QObject::eventFilter(o, e);
25 }
26
27 ScanWidget::ScanWidget( QWidget *parent )
28     : QWidget( parent )
29 {
30     setupUi(this);
31
32     deviceRadioButton->setChecked( true );
33     devices = new QMap<QString, QString>;
34
35     deviceComboBox->installEventFilter( new ClickedField(deviceRadioButton, this) );
36     folderLineEdit->installEventFilter( new ClickedField(folderRadioButton, this) );
37     folderPushButton->installEventFilter( new ClickedField(folderRadioButton, this) );
38
39     progress->setMaximum( 1 );
40
41     updateVolumeList("bar");
42
43     connect( scanButton, SIGNAL( clicked() ), this, SLOT( goScan() ) );
44     connect( folderPushButton, SIGNAL( clicked() ), this, SLOT( setFolder() ) );
45 }
46
47 QMap<QString, QString> *ScanWidget::volumeList()
48 {
49     if( devices->count() > 0 )
50     {
51         return devices;
52     }
53
54     if( !QDBusConnection::systemBus().isConnected() ) {
55         qDebug("Cannot connect to D-BUS session bus.");
56         return devices;
57     }
58
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?" );
62         return devices;
63     }
64
65     QDBusReply<QStringList> reply = hal.call( "FindDeviceStringMatch", "volume.disc.type", "dvd_rom" );
66     if( !reply.isValid() ) {
67         qDebug( "Couldn't call FindDeviceStringMatch." );
68         return devices;
69     }
70
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");
77                 continue;
78             }
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();
83             rx.indexIn(label);
84             if (rx.numCaptures() > 0) {
85                 label = label.replace(rx, QString(" Season %1 Disc %2").arg(rx.cap(1)).arg(rx.cap(2)));
86             } else {
87                 label = label.replace("_", " ");
88             }
89             devices->insert( d.value(), label );
90         }
91     }
92     else
93     {
94         devices->insert( "none detected", "Insert a DVD..." );
95     }
96
97     return devices;
98 }
99
100 void ScanWidget::updateVolumeList(QString)
101 {
102     if( devices->count() > 0 )
103     {
104         devices->clear();
105         deviceComboBox->clear();
106     }
107     QStringList d;
108     QMapIterator<QString, QString> i( *volumeList() );
109     while (i.hasNext()) {
110         i.next();
111         d << QString("%1 (%2)").arg(i.value()).arg(i.key());
112     }
113     deviceComboBox->addItems(d);
114 }
115
116 void ScanWidget::goScan()
117 {
118     scanButton->setDisabled( true );
119
120     if( deviceRadioButton->isChecked() )
121     {
122         QRegExp rx(".*\\((.*)\\)");
123         rx.indexIn(deviceComboBox->currentText());
124         emit scanDVD( rx.cap(1) );
125     }
126     else if( folderRadioButton->isChecked() )
127     {
128         emit scanDVD( folderLineEdit->text() );
129     }
130 }
131
132 void ScanWidget::setProgress( int value, int maximum )
133 {
134     if( progress->maximum() == 1 )
135     {
136         progress->setMaximum( maximum );
137     }
138
139     progress->setValue( value );
140
141     if( value == maximum )
142     {
143         emit scanningDone();
144     }
145 }
146
147 void ScanWidget::setFolder()
148 {
149     QString folderPath = QFileDialog::getExistingDirectory();
150     folderLineEdit->setText( folderPath );
151 }