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

0001 /*
0002     SPDX-FileCopyrightText: 2003 Christian Kvasny <chris@k3b.org>
0003     SPDX-FileCopyrightText: 1998-2008 Sebastian Trueg <trueg@k3b.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "k3bvideocdrippingdialog.h"
0009 #include "k3bvideocdrip.h"
0010 #include "k3bjobprogressdialog.h"
0011 #include "k3bcore.h"
0012 #include "k3bglobals.h"
0013 #include "k3bstdguiitems.h"
0014 
0015 #include <KColorScheme>
0016 #include <KConfig>
0017 #include <KLocalizedString>
0018 #include <KUrlRequester>
0019 #include <KMessageBox>
0020 
0021 #include <QDebug>
0022 #include <QDir>
0023 #include <QFileInfo>
0024 #include <QStringList>
0025 #include <QTimer>
0026 #include <QGridLayout>
0027 #include <QLabel>
0028 #include <QLayout>
0029 #include <QCheckBox>
0030 #include <QGroupBox>
0031 #include <QHBoxLayout>
0032 #include <QToolTip>
0033 
0034 
0035 K3b::VideoCdRippingDialog::VideoCdRippingDialog( K3b::VideoCdRippingOptions* options, QWidget* parent )
0036     : K3b::InteractionDialog( parent,
0037                             i18n( "Video CD Ripping" ),
0038                             QString(),
0039                             START_BUTTON|CANCEL_BUTTON,
0040                             START_BUTTON,
0041                             "Video CD Ripping" ), // config group
0042       m_videooptions( options )
0043 {
0044     setupGui();
0045     setupContextHelp();
0046 }
0047 
0048 
0049 K3b::VideoCdRippingDialog::~VideoCdRippingDialog()
0050 {
0051 }
0052 
0053 
0054 void K3b::VideoCdRippingDialog::setupGui()
0055 {
0056     QWidget * frame = mainWidget();
0057     QGridLayout* MainLayout = new QGridLayout( frame );
0058     MainLayout->setContentsMargins( 0, 0, 0, 0 );
0059 
0060     // ---------------------------------------------------- Directory group ---
0061     QGroupBox* groupDirectory = new QGroupBox( i18n( "Destination Folder" ), frame );
0062 
0063     QGridLayout* groupDirectoryLayout = new QGridLayout( groupDirectory );
0064     groupDirectoryLayout->setAlignment( Qt::AlignTop );
0065 
0066     QLabel* rippathLabel = new QLabel( i18n( "Rip files to:" ), groupDirectory );
0067     m_editDirectory = new KUrlRequester( groupDirectory );
0068     m_editDirectory->setMode( KFile::Directory | KFile::ExistingOnly | KFile::LocalOnly );
0069 
0070     rippathLabel->setBuddy( m_editDirectory );
0071 
0072     QHBoxLayout* freeSpaceBox = new QHBoxLayout;
0073     freeSpaceBox->addWidget( new QLabel( i18n( "Free space in folder:" ), groupDirectory ) );
0074     m_labelFreeSpace = new QLabel( "                       ", groupDirectory );
0075     m_labelFreeSpace->setAlignment( Qt::AlignVCenter | Qt::AlignRight );
0076     freeSpaceBox->addWidget( m_labelFreeSpace );
0077 
0078     QHBoxLayout* necessarySizeBox = new QHBoxLayout;
0079     necessarySizeBox->addWidget( new QLabel( i18n( "Necessary storage size:" ), groupDirectory ) );
0080     m_labelNecessarySize = new QLabel( "                        ", groupDirectory );
0081     m_labelNecessarySize->setAlignment( Qt::AlignVCenter | Qt::AlignRight );
0082     necessarySizeBox->addWidget( m_labelNecessarySize );
0083 
0084     groupDirectoryLayout->addWidget( rippathLabel, 0, 0 );
0085     groupDirectoryLayout->addWidget( m_editDirectory, 0, 1 );
0086     groupDirectoryLayout->addLayout( freeSpaceBox, 1, 1 );
0087     groupDirectoryLayout->addLayout( necessarySizeBox, 2, 1 );
0088 
0089     // ---------------------------------------------------- Options group ---
0090     QGroupBox* groupOptions = new QGroupBox( i18n( "Settings" ), frame );
0091     m_ignoreExt = new QCheckBox( i18n( "Ignore /EXT/PSD_X.VCD" ), groupOptions );
0092     m_sector2336 = new QCheckBox( i18n( "Use 2336 byte sector mode for image file" ), groupOptions );
0093     // Only available for image file ripping
0094     m_sector2336->setEnabled( false );
0095     m_sector2336->setChecked( false );
0096     m_extractXML = new QCheckBox( i18n( "Extract XML structure" ), groupOptions );
0097     QVBoxLayout* groupOptionsLayout = new QVBoxLayout( groupOptions );
0098     groupOptionsLayout->addWidget( m_ignoreExt );
0099     groupOptionsLayout->addWidget( m_sector2336 );
0100     groupOptionsLayout->addWidget( m_extractXML );
0101 
0102     MainLayout->addWidget( groupDirectory, 0, 0 );
0103     MainLayout->addWidget( groupOptions, 1, 0 );
0104     MainLayout->setRowStretch( 0, 1 );
0105 
0106     setStartButtonText( i18n( "Start Ripping" ), i18n( "Starts extracting the selected VideoCd tracks" ) );
0107     // ----------------------------------------------------------------------------------
0108 
0109     connect( m_editDirectory, SIGNAL(textChanged(QString)), this, SLOT(slotUpdateFreeSpace()) );
0110 
0111     m_labelNecessarySize ->setText( KIO::convertSize( m_videooptions ->getVideoCdSize() ) );
0112 }
0113 
0114 
0115 void K3b::VideoCdRippingDialog::setupContextHelp()
0116 {
0117     m_labelFreeSpace->setToolTip( i18n("Free space in destination folder: %1", m_editDirectory ->url().url() ) );
0118 
0119     m_labelNecessarySize->setToolTip( i18n("Necessary space for extracted files") );
0120 
0121     m_ignoreExt->setToolTip( i18n("Ignore extended PSD") );
0122     m_ignoreExt->setWhatsThis( i18n("<p>Ignore extended PSD (located in the ISO 9660 filesystem under `/EXT/PSD_X.VCD') and use the <em>standard</em> PSD.</p>") );
0123 
0124     m_sector2336->setToolTip( i18n("Assume a 2336-byte sector mode") );
0125     m_sector2336->setWhatsThis( i18n("<p>This option only makes sense if you are reading from a BIN CD disk image. This indicates to `vcdxrip' to assume a 2336-byte sector mode for image file.</p>"
0126                                      "<b>Note: This option is slated to disappear.</b>") );
0127 
0128     m_extractXML->setToolTip( i18n("Create XML description file.") );
0129     m_extractXML->setWhatsThis( i18n("<p>This option creates an XML description file with all video CD information.</p>"
0130                                      "<p>This file will always contain all of the information.</p>"
0131                                      "<p>Example: If you only extract sequences, the description file will also hold the information for files and segments.</p>"
0132                                      "<p>The filename is the same as the video CD name, with a .xml extension. The default is VIDEOCD.xml.</p>") );
0133 }
0134 
0135 void K3b::VideoCdRippingDialog::slotStartClicked()
0136 {
0137 
0138     QStringList filesExists;
0139     QDir d;
0140     d.setPath( m_editDirectory ->url().toLocalFile() );
0141     if( !d.exists() ) {
0142         if( KMessageBox::warningTwoActions( this,
0143                                             i18n("Image folder '%1' does not exist. Do you want K3b to create it?", m_editDirectory->url().toLocalFile() ),
0144                                             i18n("Create folder?"),
0145                                             KGuiItem(i18n("Create")),
0146                                             KStandardGuiItem::cancel()) == KMessageBox::PrimaryAction ) {
0147             if( !QDir().mkpath( m_editDirectory->url().toLocalFile() ) ) {
0148                 KMessageBox::error( this, i18n("Failed to create folder '%1'.", m_editDirectory->url().toLocalFile() ) );
0149                 return;
0150             }
0151         }
0152         else {
0153             return;
0154         }
0155     }
0156     foreach( const QFileInfo& fi, d.entryInfoList() )
0157     {
0158         if ( fi.fileName() != "." && fi .fileName() != ".." )
0159             filesExists.append( QString( "%1 (%2)" ).arg( QString(QFile::encodeName( fi.fileName() )) ).arg( KIO::convertSize( fi.size() ) ) );
0160     }
0161 
0162     if( !filesExists.isEmpty() )
0163         if( KMessageBox::questionTwoActionsList( this,
0164                                                  i18n("Continue although the folder is not empty?"),
0165                                                  filesExists,
0166                                                  i18n("Files Exist"),KStandardGuiItem::cont(),KStandardGuiItem::cancel() ) == KMessageBox::SecondaryAction )
0167             return;
0168 
0169     m_videooptions ->setVideoCdIgnoreExt( m_ignoreExt ->isChecked() );
0170     m_videooptions ->setVideoCdSector2336( m_sector2336 ->isChecked() );
0171     m_videooptions ->setVideoCdExtractXml( m_extractXML ->isChecked() );
0172     m_videooptions ->setVideoCdDestination( m_editDirectory ->url().toLocalFile() );
0173 
0174     K3b::JobProgressDialog dlg( parentWidget(), "Ripping" );
0175     K3b::VideoCdRip* job = new K3b::VideoCdRip( &dlg, m_videooptions, &dlg );
0176 
0177     hide();
0178     dlg.startJob( job );
0179     close();
0180 }
0181 
0182 void K3b::VideoCdRippingDialog::slotFreeSpace(const QString&,
0183                                             unsigned long,
0184                                             unsigned long,
0185                                             unsigned long kbAvail)
0186 {
0187     m_labelFreeSpace->setText( KIO::convertSizeFromKiB(kbAvail) );
0188 
0189     m_freeSpace = kbAvail;
0190 
0191     const KColorScheme colorScheme( isEnabled() ? QPalette::Normal : QPalette::Disabled, KColorScheme::Window );
0192     QColor textColor;
0193     if( m_freeSpace < m_videooptions ->getVideoCdSize() / 1024 )
0194         textColor = colorScheme.foreground( KColorScheme::NegativeText ).color();
0195     else
0196         textColor = colorScheme.foreground( KColorScheme::NormalText ).color();
0197 
0198     QPalette pal( m_labelNecessarySize->palette() );
0199     pal.setColor( m_labelNecessarySize->foregroundRole(), textColor );
0200     m_labelNecessarySize->setPalette( pal );
0201 
0202     QTimer::singleShot( 1000, this, SLOT(slotUpdateFreeSpace()) );
0203 }
0204 
0205 
0206 void K3b::VideoCdRippingDialog::slotUpdateFreeSpace()
0207 {
0208     QString path = m_editDirectory->url().toLocalFile();
0209 
0210     if( !QFile::exists( path ) )
0211         path.truncate( path.lastIndexOf('/') );
0212 
0213     unsigned long size, avail;
0214     if( K3b::kbFreeOnFs( path, size, avail ) )
0215         slotFreeSpace( path, size, 0, avail );
0216     else
0217         m_labelFreeSpace->setText("-");
0218 }
0219 
0220 void K3b::VideoCdRippingDialog::loadSettings( const KConfigGroup& c )
0221 {
0222     m_editDirectory ->setUrl( QUrl::fromLocalFile( c.readEntry( "last ripping directory", K3b::defaultTempPath() ) ) );
0223     m_ignoreExt ->setChecked( c.readEntry( "ignore ext", false ) );
0224     m_sector2336 ->setChecked( c.readEntry( "sector 2336", false ) );
0225     m_extractXML ->setChecked( c.readEntry( "extract xml", false ) );
0226 
0227     slotUpdateFreeSpace();
0228 }
0229 
0230 void K3b::VideoCdRippingDialog::saveSettings( KConfigGroup c )
0231 {
0232     c.writeEntry( "last ripping directory", m_editDirectory->url() );
0233     c.writeEntry( "ignore ext", m_ignoreExt ->isChecked( ) );
0234     c.writeEntry( "sector 2336", m_sector2336 ->isChecked( ) );
0235     c.writeEntry( "extract xml", m_extractXML ->isChecked( ) );
0236 }
0237 
0238 #include "moc_k3bvideocdrippingdialog.cpp"