~ubuntu-branches/ubuntu/saucy/acetoneiso/saucy

« back to all changes in this revision

Viewing changes to acetoneiso/sources/erase_dvd.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Nick Andrik
  • Date: 2010-01-19 20:16:15 UTC
  • Revision ID: james.westby@ubuntu.com-20100119201615-bdokwbbqhn1fo1iq
Tags: upstream-2.2.1
Import upstream version 2.2.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//This file is part of AcetoneISO. Copyright 2006,2007,2008,2009 Marco Di Antonio and Fabrizio Di Marco (acetoneiso@gmail.com)
 
2
//
 
3
//    AcetoneISO is free software: you can redistribute it and/or modify
 
4
//    it under the terms of the GNU General Public License as published by
 
5
//    the Free Software Foundation, either version 3 of the License, or
 
6
//    (at your option) any later version.
 
7
//
 
8
//    AcetoneISO is distributed in the hope that it will be useful,
 
9
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
//    GNU General Public License for more details.
 
12
//
 
13
//    You should have received a copy of the GNU General Public License
 
14
//    along with AcetoneISO.  If not, see <http://www.gnu.org/licenses/>.
 
15
#include <QtGui>
 
16
#include <QByteArray>
 
17
#include <QProcess>
 
18
#include <QDBusConnection>
 
19
#include <QDBusInterface>
 
20
#include <QDBusMessage>
 
21
#include <QList>
 
22
#include <QVariant>
 
23
#include <fcntl.h>
 
24
#include <iostream>
 
25
#include <QDebug>
 
26
#include <Phonon/MediaObject>
 
27
#include <Phonon/AudioOutput>
 
28
#include "erase_dvd.h"
 
29
 
 
30
/*
 
31
cancellare cd completo: wodim -verbose dev=/dev/sr0  blank=all
 
32
cancellare cd rapido: wodim -verbose dev=/dev/sr0  blank=fast
 
33
dice il tipo di media inserito: hal-get-property "--key volume.disc.type --udi $udi
 
34
*/
 
35
 
 
36
//
 
37
erasedvd::erasedvd( QWidget * parent, Qt::WFlags f) 
 
38
        : QDialog(parent, f)
 
39
{
 
40
 
 
41
    setupUi(this);
 
42
    is_erasing = false;
 
43
    loaded_success = false;
 
44
    QCoreApplication::setApplicationName("AcetoneISO");
 
45
    connect( start, SIGNAL( clicked() ), this, SLOT( start_erase() ) );
 
46
 
 
47
    textBrowser->clear();
 
48
    device_scan();
 
49
   
 
50
 
 
51
}
 
52
 
 
53
 
 
54
 
 
55
 
 
56
void erasedvd::device_scan() {
 
57
 
 
58
 
 
59
/*creo la connessione dbus a hal*/
 
60
QDBusConnection conn = QDBusConnection::systemBus();
 
61
QDBusInterface hal("org.freedesktop.Hal", "/org/freedesktop/Hal/Manager", "org.freedesktop.Hal.Manager", conn);
 
62
/*cerco tutti i dispositivi storage.cdrom con hal via dbus*/
 
63
QDBusMessage msg = hal.call( "FindDeviceByCapability", "storage.cdrom");
 
64
 
 
65
QList<QVariant> devices = msg.arguments(); 
 
66
/*per ogni udi (dispositivo cdrom) che trovo, ricavo vendor, model, velocità e path*/
 
67
 
 
68
//disabilita tutto se non viene trovato almeno 1 periferica
 
69
int count = devices.count();
 
70
if (count < 1) {
 
71
 QMessageBox::warning(this, "AcetoneISO::warning", tr("No CD/DVD device found.\nIf you think this is a bug please report it."));
 
72
 disable_buttons();
 
73
 devices_combo->addItem( tr("No CD/DVD device found") );
 
74
 return;
 
75
}
 
76
 
 
77
 
 
78
QIcon icon_optical_drive( ":/images/drive-optical.png" );
 
79
 
 
80
//scannerizzo tutte le periferiche e le aggiungo nella combobox. questa funzione viene chiamato una sola volta.
 
81
QVariant name(devices);
 
82
int cc = 0;
 
83
int success = -1;
 
84
while (cc < count) {
 
85
                  QString cdrom = name.toStringList()[0]; 
 
86
                  qDebug() << "Found device: " << cdrom; 
 
87
                  QDBusInterface device("org.freedesktop.Hal", cdrom, "org.freedesktop.Hal.Device", conn);
 
88
                  
 
89
                  //vedo se effittivamente il device puo scrivere su dischi dvd-rw
 
90
//controllo in seguito se puo scrivere su dvd+rw dal momento che se puo scrivere su dvd+rw puo anche farlo su dvd-rw ma questo ragionamento non funziona al contrario
 
91
                  msg = device.call("GetProperty", "storage.cdrom.dvdrw");
 
92
                  QVariant vcan_write_cdrw = msg.arguments()[0];
 
93
                  bool can_write_cdrw = vcan_write_cdrw.toBool();
 
94
                  if (!can_write_cdrw) {
 
95
                    cc = cc + 1;
 
96
                    continue;
 
97
                  }
 
98
                  //success si ricorda sempre la posizione in cui aggiungere all'array indipendentemente dal cc del ciclo while
 
99
                  success = success + 1;
 
100
                  //aggiungo id nell'array
 
101
                  id_device.insert(success, cdrom);               
 
102
                  /*ottengo il nome del vendor del cdrom*/
 
103
                  msg = device.call("GetProperty", "storage.vendor");
 
104
                  QVariant var = msg.arguments()[0]; 
 
105
                  QString vendor = var.toStringList()[0];
 
106
                  qDebug() << "VENDOR" << vendor;
 
107
                  /*ottengo il nome del modello del cdrom*/
 
108
                  msg = device.call("GetProperty", "storage.model");
 
109
                  QVariant var2 = msg.arguments()[0]; 
 
110
                  QString model = var2.toStringList()[0];
 
111
                  qDebug() << "MODEL" << model;
 
112
 
 
113
                  /*ottengo il path del dispositivo cdrom*/
 
114
                  msg = device.call("GetProperty", "block.device");
 
115
                  QVariant var3 = msg.arguments()[0]; 
 
116
                  device_path.insert(success, var3.toStringList()[0]);
 
117
                  qDebug() << "PATH" << device_path;
 
118
                  
 
119
                  msg = device.call("GetProperty", "storage.firmware_version");
 
120
                  QVariant firmwarev = msg.arguments()[0];
 
121
                  QString firmware = firmwarev.toString();
 
122
                   /*aggiungo il nome del device nella combobox*/
 
123
                  QString final_device = vendor.append( "\t" + model + " " + firmware );
 
124
                  devices_combo->insertItem(success,icon_optical_drive, " " + final_device );
 
125
                  
 
126
                  cc = cc + 1;
 
127
                  }
 
128
 
 
129
media_available();
 
130
//if < 1 significa che sebbene ci sono 1 o piu periferiche cdrom, nessuna e' capace di scrivere su cd-rw
 
131
if (devices_combo->count() < 1) {
 
132
disable_buttons();
 
133
QMessageBox::warning(this, "AcetoneISO::warning", tr("No CD/DVD device found capable of writing to DVD-RW discs.\nIf you think this is a bug please report it."));
 
134
return; 
 
135
}
 
136
else { //c'e' almeno 1 periferica cdrom in grado di scrivere su dischi cd-rw
 
137
loaded_success = true;
 
138
//crea un timer cosi aggiorna automaticamente la label_info
 
139
timer = new QTimer(this);
 
140
connect(timer, SIGNAL(timeout()), this, SLOT(media_available()));
 
141
timer->start(2000);
 
142
connect( devices_combo, SIGNAL(currentIndexChanged(int) ), this , SLOT( combo_changed(int) ) ); 
 
143
}
 
144
 
 
145
}
 
146
 
 
147
 
 
148
void erasedvd::media_available() {
 
149
//qDebug() << "media_available()";
 
150
//se sto masterizzando deve uscire da qui
 
151
if (is_erasing) {
 
152
start->setEnabled(false);
 
153
label_info->setText(tr("The")+ " " + discotipo + " " + tr("is getting blanked..."));
 
154
return;
 
155
}
 
156
 
 
157
//if < 1 it means there are no devices capaci di scrivere su dischi cd-rw
 
158
if (devices_combo->count() < 1) {
 
159
return; 
 
160
}
 
161
 
 
162
//se non e' visibile deve disconnettere le connessioni
 
163
if (loaded_success) {
 
164
  if (!devices_combo->isVisible()) {
 
165
    disconnect(devices_combo, 0, 0, 0);
 
166
    disconnect(timer, 0, 0, 0);
 
167
    loaded_success = false;
 
168
  }
 
169
}
 
170
//scopro se ho un cd dentro il device
 
171
/*creo la connessione dbus a hal*/
 
172
QDBusConnection conn = QDBusConnection::systemBus();
 
173
QDBusMessage msg;
 
174
//prendo indice corrente della combobox cosi so chi l'id della periferica selezionata
 
175
int current_index = devices_combo->currentIndex();
 
176
QString id = id_device[current_index];
 
177
 
 
178
//mi connetto alla periferica corrente selezionata in combobox
 
179
QDBusInterface device("org.freedesktop.Hal", id, "org.freedesktop.Hal.Device", conn);
 
180
//vedo se e' inserito un cd 
 
181
msg = device.call("GetProperty", "storage.removable.media_available");
 
182
QVariant var5 = msg.arguments()[0];
 
183
bool opticDev = var5.toBool();
 
184
QString plusminus = QString::fromUtf8("\xC2\xB1"); //0xC2 0xB1
 
185
if (!opticDev) {
 
186
  label_info->setText(tr("Insert a DVD") + plusminus + "RW " + tr("disc.")  );
 
187
  start->setEnabled(false);
 
188
  //devices_combo->setItemText(cc,tr("No Media Inserted, insert a media and click on refresh button."));
 
189
  return;
 
190
}
 
191
 
 
192
//vedo il tipo di cd inserito
 
193
QDBusMessage fbp;
 
194
QDBusInterface newh("org.freedesktop.Hal", "/org/freedesktop/Hal/Manager", "org.freedesktop.Hal.Manager", conn);
 
195
fbp = newh.call("FindDeviceStringMatch", "info.parent", id);
 
196
QList<QVariant> var6 = fbp.arguments();
 
197
QString disc_type = var6.at(0).toString();
 
198
QDBusInterface device_type("org.freedesktop.Hal", disc_type, "org.freedesktop.Hal.Device", conn);
 
199
msg = device_type.call("GetProperty", "volume.disc.type");
 
200
QString var7 = msg.arguments()[0].toString();
 
201
QString disc_is = var7;
 
202
//ferma in caso che il disco inserito non sia un disco DVD±RW
 
203
if ( (disc_is != "dvd_rw") and (disc_is != "dvd_plus_rw") ) {
 
204
label_info->setText(tr("The disc isn't a DVD") + plusminus + "RW. " +tr("Please insert a DVD") + plusminus + "RW " + tr("disc."));
 
205
start->setEnabled(false); 
 
206
return;
 
207
}
 
208
 
 
209
if (disc_is == "dvd_rw") {
 
210
discotipo = "DVD-RW";  
 
211
}
 
212
else {
 
213
discotipo = "DVD+RW";
 
214
msg = device.call("GetProperty", "storage.cdrom.dvdplusrw");
 
215
QVariant vcan_write_cdrw = msg.arguments()[0];
 
216
bool discobool = vcan_write_cdrw.toBool();
 
217
  if (!discobool) {
 
218
    label_info->setText( tr("You inserted a DVD+RW disc, however your CD/DVD device is uncapable of writing to DVD+RW discs.") ); 
 
219
    start->setEnabled(false); 
 
220
    return;
 
221
  }
 
222
}
 
223
 
 
224
 
 
225
label_info->setText(discotipo + " " + tr("succesfully found in device."));
 
226
start->setEnabled(true);
 
227
 
 
228
}
 
229
 
 
230
//gestisce quando l'utente cambia periferica nella combobox
 
231
void erasedvd::combo_changed(int n) {
 
232
int a;
 
233
a = n;
 
234
media_available(); 
 
235
}
 
236
 
 
237
 
 
238
void erasedvd::start_erase() {
 
239
  
 
240
QString type = discotipo;
 
241
QMessageBox msgBox;
 
242
msgBox.setText(tr("You decided to blank the") + " " + discotipo + tr("\nAre you sure?"));
 
243
 msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
 
244
 switch (msgBox.exec()) {
 
245
 case QMessageBox::Yes:
 
246
    {
 
247
      //continue below
 
248
     }
 
249
     break;
 
250
 case QMessageBox::No:
 
251
 {
 
252
  return; //exit
 
253
 }
 
254
     break;
 
255
 default:
 
256
     // should never be reached
 
257
     break;
 
258
 }
 
259
 
 
260
QString erase_type;
 
261
if (radioButton->isChecked()) {
 
262
erase_type = "complete";
 
263
}
 
264
else {
 
265
erase_type = "fast";
 
266
}
 
267
 
 
268
 
 
269
disable_buttons();
 
270
QString dev = device_path.at(devices_combo->currentIndex());
 
271
 
 
272
//dev = dev.prepend("dev=");
 
273
textBrowser->clear(); 
 
274
erase = new QProcess();
 
275
erase->setReadChannel(QProcess::StandardOutput);
 
276
erase->setProcessChannelMode(QProcess::MergedChannels); 
 
277
//connection to update the display
 
278
connect(erase, SIGNAL(readyReadStandardOutput()), SLOT(updateEraseDisplay() )); 
 
279
connect(erase, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(printOutErase(int, QProcess::ExitStatus)));
 
280
QDir bin("/usr/bin");
 
281
QDir::setCurrent( bin.absolutePath() );
 
282
is_erasing = true;
 
283
 
 
284
if (erase_type == "fast") {
 
285
erase->start("dvd+rw-format",QStringList()  << dev );
 
286
}
 
287
else {
 
288
erase->start("dvd+rw-format",QStringList()  << "-force" << dev );
 
289
}
 
290
 
 
291
 
 
292
 
 
293
 
 
294
}
 
295
 
 
296
//fa loutput nel display del processo wodim
 
297
void erasedvd::updateEraseDisplay() {
 
298
erase_output = erase->readAllStandardOutput(); 
 
299
//questo evita che prende un output vuoto
 
300
if (erase_output.size() > 2 ) {
 
301
eraseoutput_real = erase_output; //questo si ricorda sempre dell'ultimo output NON vuoto
 
302
textBrowser->setPlainText(erase_output);
 
303
}
 
304
else {
 
305
textBrowser->setPlainText(eraseoutput_real); 
 
306
}
 
307
 
 
308
}
 
309
 
 
310
//gestisce quando il processo dvd+rw-format finisce
 
311
void erasedvd::printOutErase(int, QProcess::ExitStatus) {
 
312
enable_buttons();
 
313
is_erasing = false;
 
314
 
 
315
//controllo se il dvd e' gia formattato o se cmq non necessita di una formattazione secondo il sofware dvd+r-format
 
316
QByteArray str = "already";
 
317
bool contains = eraseoutput_real.contains(str); //prendo l'ultimo output non vuoto
 
318
eraseoutput_real = "";
 
319
if (contains) {
 
320
 QMessageBox::information(this, "AcetoneISO",tr("The") + " " + discotipo + " " + tr("is already formatted.\nThere is no need to format it because you can simply overwrite the media.\nIf you really want to blank it, choose Blank the Entire disc in the below window but we highly discourage you from doing so.\nOverwriting the") + " " + discotipo + " " + tr("is the simplest and safest thing to do."));
 
321
return;
 
322
}
 
323
 
 
324
 
 
325
if (eject->isChecked()) {
 
326
QString dev = device_path.at(devices_combo->currentIndex());
 
327
EJect.start("eject",QStringList()  << dev );
 
328
}
 
329
 
 
330
int valore_uscita = erase->exitCode();
 
331
if(valore_uscita == 0) {
 
332
  if (playcheckbox->isChecked()) {
 
333
    play_success();
 
334
  }
 
335
  QMessageBox::information(this, "AcetoneISO",tr("Process Succesfully Finished!"));
 
336
  textBrowser->clear();
 
337
}
 
338
else {
 
339
  if (playcheckbox->isChecked()) {
 
340
    play_error();
 
341
  }
 
342
  QMessageBox::critical(this, "AcetoneISO","Process Error Code: " + QString::number(valore_uscita) );
 
343
}
 
344
}
 
345
 
 
346
//abilita i bottoni
 
347
void erasedvd::enable_buttons() { 
 
348
start->setEnabled(true);
 
349
devices_combo->setEnabled(true);
 
350
eject->setEnabled(true);
 
351
radioButton->setEnabled(true);
 
352
radioButton_2->setEnabled(true);
 
353
playcheckbox->setEnabled(true);
 
354
}
 
355
//disabilita i bottoni
 
356
void erasedvd::disable_buttons() {
 
357
start->setEnabled(false);
 
358
devices_combo->setEnabled(false);
 
359
eject->setEnabled(false);
 
360
radioButton->setEnabled(false);
 
361
radioButton_2->setEnabled(false); 
 
362
playcheckbox->setEnabled(false);
 
363
}
 
364
 
 
365
 
 
366
void erasedvd::play_success() {
 
367
Phonon::MediaObject *mediaObject;       
 
368
Phonon::AudioOutput *audioOutput; 
 
369
Phonon::Path path;
 
370
mediaObject = new Phonon::MediaObject();
 
371
audioOutput = new Phonon::AudioOutput(Phonon::NoCategory);
 
372
path = Phonon::createPath(mediaObject, audioOutput);
 
373
QString file(":/audio/success.ogg");
 
374
mediaObject->setCurrentSource(Phonon::MediaSource(file));
 
375
mediaObject->play();
 
376
}
 
377
 
 
378
void erasedvd::play_error() {
 
379
Phonon::MediaObject *mediaObject;       
 
380
Phonon::AudioOutput *audioOutput; 
 
381
Phonon::Path path;
 
382
mediaObject = new Phonon::MediaObject();
 
383
audioOutput = new Phonon::AudioOutput(Phonon::NoCategory);
 
384
path = Phonon::createPath(mediaObject, audioOutput);
 
385
QString file(":/audio/error.ogg");
 
386
mediaObject->setCurrentSource(Phonon::MediaSource(file));
 
387
mediaObject->play();
 
388
}
 
389
 
 
390