File indexing completed on 2024-05-05 04:49:24

0001 /****************************************************************************************
0002  * Copyright (c) 2010 Téo Mrnjavac <teo@kde.org>                                        *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include "TranscodingAssistantDialog.h"
0018 
0019 #include "TranscodingJob.h"
0020 #include "core/transcoding/TranscodingController.h"
0021 
0022 #include <QIcon>
0023 #include <QPushButton>
0024 
0025 #include <KConfigGroup>
0026 #include <QDialogButtonBox>
0027 #include <QVBoxLayout>
0028 
0029 using namespace Transcoding;
0030 
0031 AssistantDialog::AssistantDialog( const QStringList &playableFileTypes, bool saveSupported,
0032                                   Collections::CollectionLocationDelegate::OperationType operation,
0033                                   const QString &destCollectionName,
0034                                   const Configuration &prevConfiguration,
0035                                   QWidget *parent )
0036     : KPageDialog( parent, Qt::Dialog )
0037     , m_configuration( JUST_COPY )
0038     , m_save( false )
0039     , m_playableFileTypes( playableFileTypes )
0040 {
0041     DEBUG_BLOCK
0042     Q_UNUSED( destCollectionName )  // keep it in signature, may be useful in future
0043 
0044     QWidget *uiBase = new QWidget( this );
0045     ui.setupUi( uiBase );
0046     setModal( true );
0047     setWindowTitle( i18n( "Transcode Tracks" ) );
0048     setMinimumSize( 620, 500 );
0049     setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
0050 
0051     QDialogButtonBox *buttonBox = this->buttonBox();
0052     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0053     okButton->setDefault(true);
0054     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0055     connect(buttonBox, &QDialogButtonBox::accepted, this, &AssistantDialog::accept);
0056     connect(buttonBox, &QDialogButtonBox::rejected, this, &AssistantDialog::reject);
0057     okButton->setText( i18n( "Transc&ode" ) );
0058     okButton->setEnabled( false );
0059 
0060     QString explanatoryText;
0061     QIcon justCopyIcon;
0062     QString justCopyText;
0063     QString justCopyDescription;
0064     switch( operation )
0065     {
0066         case Collections::CollectionLocationDelegate::Copy:
0067             explanatoryText = i18n(
0068                 "While copying, you can choose to transcode your music files into another "
0069                 "format with an encoder (codec). This can be done to save space or to "
0070                 "make your files readable by a portable music player or a particular "
0071                 "software program." );
0072             justCopyIcon = QIcon::fromTheme( QStringLiteral("edit-copy") );
0073             justCopyText = i18n( "&Copy" );
0074             justCopyDescription = i18n( "Just copy the tracks without transcoding them." );
0075             break;
0076         case Collections::CollectionLocationDelegate::Move:
0077             explanatoryText = i18n(
0078                 "While moving, you can choose to transcode your music files into another "
0079                 "format with an encoder (codec). This can be done to save space or to "
0080                 "make your files readable by a portable music player or a particular "
0081                 "software program. Only successfully transcoded files will be removed "
0082                 "from their original location." );
0083             justCopyIcon = QIcon::fromTheme( QStringLiteral("go-jump") ); // Dolphin uses this icon for "move"
0084             justCopyText = i18n( "&Move" );
0085             justCopyDescription = i18n( "Just move the tracks without transcoding them." );
0086             break;
0087     }
0088     ui.explanatoryTextLabel->setText( explanatoryText );
0089 
0090     ui.justCopyButton->setIcon( justCopyIcon );
0091     ui.justCopyButton->setText( justCopyText );
0092     ui.justCopyButton->setDescription( justCopyDescription );
0093     ui.justCopyButton->setMinimumHeight( ui.justCopyButton->iconSize().height() + 2*10 ); //we make room for the pretty icon
0094     connect( ui.justCopyButton, &QAbstractButton::clicked,
0095              this, &AssistantDialog::onJustCopyClicked );
0096 
0097     //Let's set up the codecs page...
0098     populateFormatList();
0099     connect( ui.formatListWidget, &QListWidget::currentItemChanged,
0100              this, &AssistantDialog::onFormatSelect );
0101 
0102     ui.formatIconLabel->hide();
0103     ui.formatNameLabel->hide();
0104     connect( buttonBox->button(QDialogButtonBox::Ok), &QAbstractButton::clicked,
0105              this, &AssistantDialog::onTranscodeClicked );
0106 
0107     ui.rememberCheckBox->setChecked( m_save );
0108     ui.rememberCheckBox->setEnabled( saveSupported );
0109     connect( ui.rememberCheckBox, &QCheckBox::toggled,
0110              this, &AssistantDialog::onRememberToggled );
0111 
0112     switch( prevConfiguration.trackSelection() ) //restore the previously selected TrackSelection radio button
0113     {
0114         case Configuration::TranscodeUnlessSameType:
0115             ui.transcodeUnlessSameTypeRadioButton->setChecked( true );
0116             break;
0117         case Configuration::TranscodeOnlyIfNeeded:
0118             ui.transcodeOnlyIfNeededRadioButton->setChecked( true );
0119             break;
0120         case Configuration::TranscodeAll:
0121             ui.transcodeAllRadioButton->setChecked( true );
0122     }
0123 
0124     ui.transcodeAllRadioButton->setEnabled( false );
0125     ui.transcodeUnlessSameTypeRadioButton->setEnabled( false );
0126     ui.transcodeOnlyIfNeededRadioButton->setEnabled( false );
0127 }
0128 
0129 void
0130 AssistantDialog::populateFormatList()
0131 {
0132     QSet<Encoder> available = Amarok::Components::transcodingController()->availableEncoders();
0133 
0134     // Add a note if no encoder is found
0135     ui.groupBox->setEnabled( !available.isEmpty() );
0136     ui.encoderNotFoundLabel->setVisible( available.isEmpty() );
0137 
0138     foreach( Encoder encoder, Amarok::Components::transcodingController()->allEncoders() )
0139     {
0140         if( encoder == INVALID || encoder == JUST_COPY )
0141             continue; // skip "fake" encoders
0142         Format *format = Amarok::Components::transcodingController()->format( encoder );
0143 
0144         QListWidgetItem *item = new QListWidgetItem( format->icon(), format->prettyName() );
0145         item->setToolTip( format->description() );
0146         item->setData( Qt::UserRole, encoder );
0147 
0148         // can be disabled due to unavailability
0149         bool enabled = available.contains( encoder );
0150         if( !enabled )
0151             item->setToolTip( i18nc( "Tooltip of a disabled transcoding encoder option",
0152                                      "Not currently available on your system." ) );
0153 
0154         // or because not supported by target collection
0155         if( enabled && !m_playableFileTypes.isEmpty() )
0156         {
0157             enabled = m_playableFileTypes.contains( format->fileExtension() );
0158             if( !enabled )
0159                 item->setToolTip( i18n( "Target collection indicates this format would not be playable." ) );
0160         }
0161 
0162         Qt::ItemFlags flags = item->flags();
0163         if( !enabled )
0164             item->setFlags( flags & ~Qt::ItemIsEnabled );
0165         ui.formatListWidget->addItem( item );
0166     }
0167 }
0168 
0169 void
0170 AssistantDialog::onJustCopyClicked() //SLOT
0171 {
0172     QDialog::done( QDialog::Accepted );
0173 }
0174 
0175 void
0176 AssistantDialog::onTranscodeClicked() //SLOT
0177 {
0178     m_configuration = ui.transcodingOptionsStackedWidget->configuration( trackSelection() );
0179     QDialog::done( QDialog::Accepted );
0180 }
0181 
0182 void
0183 AssistantDialog::onFormatSelect( QListWidgetItem *item ) //SLOT
0184 {
0185     if( item )
0186     {
0187         ui.formatIconLabel->show();
0188         ui.formatNameLabel->show();
0189         Encoder encoder = static_cast< Encoder >( item->data( Qt::UserRole ).toInt() );
0190         const Format *format = Amarok::Components::transcodingController()->format( encoder );
0191         ui.formatIconLabel->setPixmap( format->icon().pixmap( 32, 32 ) );
0192         ui.formatNameLabel->setText( format->prettyName() );
0193         ui.formatIconLabel->setToolTip( format->description() );
0194         ui.formatIconLabel->setWhatsThis( format->description() );
0195         ui.formatNameLabel->setToolTip( format->description() );
0196         ui.formatNameLabel->setWhatsThis( format->description() );
0197         ui.transcodingOptionsStackedWidget->switchPage( encoder );
0198 
0199         ui.transcodeAllRadioButton->setEnabled( true );
0200         ui.transcodeUnlessSameTypeRadioButton->setEnabled( true );
0201         ui.transcodeOnlyIfNeededRadioButton->setEnabled( true );
0202 
0203         buttonBox()->button(QDialogButtonBox::Ok)->setEnabled(true);
0204     }
0205 }
0206 
0207 void
0208 AssistantDialog::onRememberToggled( bool checked ) //SLOT
0209 {
0210     m_save = checked;
0211 }
0212 
0213 Configuration::TrackSelection
0214 AssistantDialog::trackSelection() const
0215 {
0216     if( ui.transcodeOnlyIfNeededRadioButton->isChecked() )
0217         return Configuration::TranscodeOnlyIfNeeded;
0218     else if( ui.transcodeUnlessSameTypeRadioButton->isChecked() )
0219         return Configuration::TranscodeUnlessSameType;
0220     else
0221         return Configuration::TranscodeAll;
0222 }
0223