File indexing completed on 2024-04-28 04:48:28

0001 /*
0002    Copyright (C) 2001 Carsten Duvenhorst <duvenhorst@m2.uni-hannover.de>
0003    Copyright (C) 2005 Benjamin Meyer <ben at meyerhome dot net>
0004 
0005    This program is free software; you can redistribute it and/or modify
0006    it under the terms of the GNU General Public License as published by
0007    the Free Software Foundation; either version 2 of the License, or
0008    (at your option) any later version.
0009 
0010    This program is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0013    GNU General Public License for more details.
0014 
0015    You should have received a copy of the GNU General Public License
0016    along with this program; if not, write to the Free Software
0017    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
0018    USA.
0019 */
0020 
0021 #include "kcmaudiocd.h"
0022 #include "audiocdplugins_version.h"
0023 
0024 #include <QCheckBox>
0025 #include <QRegularExpression>
0026 #include <QSlider>
0027 #include <QVBoxLayout>
0028 
0029 #include <KAboutData>
0030 #include <KConfigDialogManager>
0031 #include <KConfigGroup>
0032 #include <KConfigSkeleton>
0033 #include <KLocalizedString>
0034 #include <KPluginFactory>
0035 
0036 #include "audiocdencoder.h"
0037 
0038 K_PLUGIN_CLASS_WITH_JSON(KAudiocdModule, "kcm_audiocd.json")
0039 
0040 KAudiocdModule::KAudiocdModule(QObject *parent, const KPluginMetaData &md)
0041     : KCModule(parent, md)
0042 {
0043     auto box = new QVBoxLayout(widget());
0044 
0045     audioConfig = new AudiocdConfig(widget());
0046 
0047     box->addWidget(audioConfig);
0048     setButtons(Default | Apply | Help);
0049 
0050     config = new KConfig(QStringLiteral("kcmaudiocdrc"));
0051 
0052     QList<AudioCDEncoder *> encoders;
0053     AudioCDEncoder::findAllPlugins(nullptr, encoders);
0054     for (AudioCDEncoder *encoder : std::as_const(encoders)) {
0055         if (encoder->init()) {
0056             KConfigSkeleton *config = nullptr;
0057             QWidget *widget = encoder->getConfigureWidget(&config);
0058             if (widget && config) {
0059                 audioConfig->tabWidget->addTab(widget, i18n("%1 Encoder", encoder->type()));
0060                 auto configManager = new KConfigDialogManager(widget, config);
0061                 encoderSettings.append(configManager);
0062             }
0063         }
0064     }
0065 
0066     qDeleteAll(encoders);
0067     encoders.clear();
0068 
0069     for (int i = 0; i < encoderSettings.size(); ++i) {
0070         connect(encoderSettings.at(i), &KConfigDialogManager::widgetModified, this, &KAudiocdModule::slotModuleChanged);
0071     }
0072 
0073     // CDDA Options
0074     connect(audioConfig->ec_enable_check, &QCheckBox::clicked, this, &KAudiocdModule::slotEcEnable);
0075     connect(audioConfig->ec_skip_check, &QCheckBox::clicked, this, &KAudiocdModule::slotConfigChanged);
0076     connect(audioConfig->niceLevel, &QSlider::valueChanged, this, &KAudiocdModule::slotConfigChanged);
0077 
0078     // File Name
0079     connect(audioConfig->fileNameLineEdit, &QLineEdit::textChanged, this, &KAudiocdModule::slotConfigChanged);
0080     connect(audioConfig->albumNameLineEdit, &QLineEdit::textChanged, this, &KAudiocdModule::slotConfigChanged);
0081     connect(audioConfig->fileLocationLineEdit, &QLineEdit::textChanged, this, &KAudiocdModule::slotConfigChanged);
0082     connect(audioConfig->fileLocationGroupBox, &QGroupBox::clicked, this, &KAudiocdModule::slotConfigChanged);
0083     connect(audioConfig->kcfg_replaceInput, &QLineEdit::textChanged, this, &KAudiocdModule::updateExample);
0084     connect(audioConfig->kcfg_replaceOutput, &QLineEdit::textChanged, this, &KAudiocdModule::updateExample);
0085     connect(audioConfig->example, &QLineEdit::textChanged, this, &KAudiocdModule::updateExample);
0086     connect(audioConfig->kcfg_replaceInput, &QLineEdit::textChanged, this, &KAudiocdModule::slotConfigChanged);
0087     connect(audioConfig->kcfg_replaceOutput, &QLineEdit::textChanged, this, &KAudiocdModule::slotConfigChanged);
0088     connect(audioConfig->example, &QLineEdit::textChanged, this, &KAudiocdModule::slotConfigChanged);
0089 }
0090 
0091 KAudiocdModule::~KAudiocdModule()
0092 {
0093     delete config;
0094 }
0095 
0096 QString removeQoutes(const QString &text)
0097 {
0098     QString deqoutedString = text;
0099     QRegularExpression qoutedStringRegExp(QStringLiteral("^\".*\"$"));
0100     QRegularExpressionMatch hasQuotes = qoutedStringRegExp.match(text);
0101     if (hasQuotes.hasMatch()) {
0102         deqoutedString = text.mid(1, text.length() - 2);
0103     }
0104     return deqoutedString;
0105 }
0106 
0107 bool needsQoutes(const QString &text)
0108 {
0109     QRegularExpression spaceAtTheBeginning(QStringLiteral("^\\s+.*$"));
0110     QRegularExpression spaceAtTheEnd(QStringLiteral("^.*\\s+$"));
0111     QRegularExpressionMatch hasSpaceAtTheBeginning = spaceAtTheBeginning.match(text);
0112     QRegularExpressionMatch hasSpaceAtTheEnd = spaceAtTheEnd.match(text);
0113     return (hasSpaceAtTheBeginning.hasMatch() || hasSpaceAtTheEnd.hasMatch());
0114 }
0115 
0116 void KAudiocdModule::updateExample()
0117 {
0118     QString text = audioConfig->example->text();
0119 
0120     const QString deqoutedReplaceInput = removeQoutes(audioConfig->kcfg_replaceInput->text());
0121     const QString deqoutedReplaceOutput = removeQoutes(audioConfig->kcfg_replaceOutput->text());
0122     text.replace(QRegularExpression(deqoutedReplaceInput), deqoutedReplaceOutput);
0123     audioConfig->exampleOutput->setText(text);
0124 }
0125 
0126 void KAudiocdModule::defaults() {
0127     audioConfig->ec_enable_check->setChecked(true);
0128     audioConfig->ec_skip_check->setChecked(false);
0129     audioConfig->niceLevel->setValue(0);
0130 
0131     audioConfig->kcfg_replaceInput->setText(QString());
0132     audioConfig->kcfg_replaceOutput->setText(QString());
0133     audioConfig->example->setText(i18n("Cool artist - example audio file.wav"));
0134     for (int i = 0; i < encoderSettings.size(); ++i) {
0135         encoderSettings.at(i)->updateWidgetsDefault();
0136     }
0137 
0138     audioConfig->fileNameLineEdit->setText(QStringLiteral("%{trackartist} - %{number} - %{title}"));
0139     audioConfig->albumNameLineEdit->setText(QStringLiteral("%{albumartist} - %{albumtitle}"));
0140 }
0141 
0142 void KAudiocdModule::save() {
0143     if (!configChanged)
0144         return;
0145 
0146     {
0147         KConfigGroup cg(config, QStringLiteral("CDDA"));
0148 
0149         cg.writeEntry("disable_paranoia", !(audioConfig->ec_enable_check->isChecked()));
0150         cg.writeEntry("never_skip", !(audioConfig->ec_skip_check->isChecked()));
0151         cg.writeEntry("niceLevel", audioConfig->niceLevel->value());
0152     }
0153 
0154   {
0155       KConfigGroup cg(config, QStringLiteral("FileName"));
0156       cg.writeEntry("file_name_template", audioConfig->fileNameLineEdit->text());
0157       cg.writeEntry("album_name_template", audioConfig->albumNameLineEdit->text());
0158       cg.writeEntry("show_file_location", audioConfig->fileLocationGroupBox->isChecked());
0159       cg.writeEntry("file_location_template", audioConfig->fileLocationLineEdit->text());
0160       cg.writeEntry("regexp_example", audioConfig->example->text());
0161 
0162       // save quoted if required
0163       QString replaceInput = audioConfig->kcfg_replaceInput->text();
0164       QString replaceOutput = audioConfig->kcfg_replaceOutput->text();
0165       if (needsQoutes(replaceInput)) {
0166           replaceInput = QLatin1Char('\"') + replaceInput + QLatin1Char('\"');
0167       }
0168     if (needsQoutes(replaceOutput)) {
0169         replaceOutput = QLatin1Char('\"') + replaceOutput + QLatin1Char('\"');
0170     }
0171     cg.writeEntry("regexp_search", replaceInput);
0172     cg.writeEntry("regexp_replace", replaceOutput);
0173   }
0174 
0175   for (int i = 0; i < encoderSettings.size(); ++i) {
0176       encoderSettings.at(i)->updateSettings();
0177   }
0178 
0179   config->sync();
0180 
0181   configChanged = false;
0182 }
0183 
0184 void KAudiocdModule::load() {
0185 
0186   {
0187       KConfigGroup cg(config, QStringLiteral("CDDA"));
0188 
0189       audioConfig->ec_enable_check->setChecked(!(cg.readEntry("disable_paranoia", false)));
0190       audioConfig->ec_skip_check->setChecked(!(cg.readEntry("never_skip", true)));
0191       audioConfig->niceLevel->setValue(cg.readEntry("niceLevel", 0));
0192   }
0193 
0194   {
0195       KConfigGroup cg(config, QStringLiteral("FileName"));
0196       audioConfig->fileNameLineEdit->setText(cg.readEntry("file_name_template", "%{trackartist} - %{number} - %{title}"));
0197       audioConfig->albumNameLineEdit->setText(cg.readEntry("album_name_template", "%{albumartist} - %{albumtitle}"));
0198       audioConfig->fileLocationGroupBox->setChecked(cg.readEntry("show_file_location", false));
0199       audioConfig->fileLocationLineEdit->setText(cg.readEntry("file_location_template", QString()));
0200       audioConfig->kcfg_replaceInput->setText(cg.readEntry("regexp_search"));
0201       audioConfig->kcfg_replaceOutput->setText(cg.readEntry("regexp_replace"));
0202       audioConfig->example->setText(cg.readEntry("example", i18n("Cool artist - example audio file.wav")));
0203   }
0204 
0205   for (int i = 0; i < encoderSettings.size(); ++i) {
0206       encoderSettings.at(i)->updateWidgets();
0207   }
0208 }
0209 
0210 void KAudiocdModule::slotModuleChanged() {
0211     for (int i = 0; i < encoderSettings.size(); ++i) {
0212         KConfigDialogManager *widget = encoderSettings.at(i);
0213         if (widget->hasChanged()) {
0214             slotConfigChanged();
0215             break;
0216         }
0217     }
0218 }
0219 
0220 void KAudiocdModule::slotConfigChanged() {
0221     configChanged = true;
0222     setNeedsSave(true);
0223 }
0224 
0225 /*
0226 #    slot for the error correction settings
0227 */
0228 void KAudiocdModule::slotEcEnable() {
0229   if (!(audioConfig->ec_skip_check->isChecked())) {
0230     audioConfig->ec_skip_check->setChecked(true);
0231   } else {
0232     if (audioConfig->ec_skip_check->isEnabled()) {
0233       audioConfig->ec_skip_check->setChecked(false);
0234     }
0235   }
0236 
0237   slotConfigChanged();
0238 }
0239 
0240 #include "kcmaudiocd.moc"
0241 #include "moc_kcmaudiocd.cpp"