File indexing completed on 2024-04-21 04:48:11

0001 /* AUDEX CDDA EXTRACTOR
0002  * SPDX-FileCopyrightText: Copyright (C) 2007 Marco Nelles
0003  * <https://userbase.kde.org/Audex>
0004  *
0005  * SPDX-License-Identifier: GPL-3.0-or-later
0006  */
0007 
0008 #include "profiledatahashlistdialog.h"
0009 
0010 #include <QDialogButtonBox>
0011 #include <QVBoxLayout>
0012 
0013 ProfileDataHashlistDialog::ProfileDataHashlistDialog(ProfileModel *profile_model, const int profile_row, const bool new_profile_mode, QWidget *parent)
0014     : QDialog(parent)
0015 {
0016     Q_UNUSED(parent);
0017 
0018     this->profile_model = profile_model;
0019     this->profile_row = profile_row;
0020     this->new_profile_mode = new_profile_mode;
0021 
0022     applyButton = nullptr;
0023 
0024     // profile data hashlist data
0025     QString format = profile_model->data(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_HL_FORMAT_INDEX)).toString();
0026     QString scheme = profile_model->data(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_HL_NAME_INDEX)).toString();
0027 
0028     setWindowTitle(i18n("Playlist Settings"));
0029 
0030     auto *mainLayout = new QVBoxLayout;
0031     setLayout(mainLayout);
0032 
0033     QDialogButtonBox::StandardButtons buttons = QDialogButtonBox::Ok | QDialogButtonBox::Cancel;
0034     if (!new_profile_mode)
0035         buttons |= QDialogButtonBox::Apply;
0036 
0037     QDialogButtonBox *buttonBox = new QDialogButtonBox(buttons);
0038     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0039     okButton->setDefault(true);
0040     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0041     if (!new_profile_mode)
0042         applyButton = buttonBox->button(QDialogButtonBox::Apply);
0043     connect(buttonBox, &QDialogButtonBox::accepted, this, &ProfileDataHashlistDialog::slotAccepted);
0044     connect(buttonBox, &QDialogButtonBox::rejected, this, &ProfileDataHashlistDialog::reject);
0045     if (!new_profile_mode)
0046         connect(applyButton, &QPushButton::clicked, this, &ProfileDataHashlistDialog::slotApplied);
0047 
0048     QWidget *widget = new QWidget(this);
0049     mainLayout->addWidget(widget);
0050     mainLayout->addWidget(buttonBox);
0051     ui.setupUi(widget);
0052 
0053     connect(ui.kpushbutton_scheme, SIGNAL(clicked()), this, SLOT(scheme_wizard()));
0054     ui.kpushbutton_scheme->setIcon(QIcon::fromTheme("tools-wizard"));
0055 
0056     ui.kcombobox_format->addItem(i18n("SFV (Simple File Verification)"), "SFV");
0057     ui.kcombobox_format->addItem(i18n("MD5 (Message-Digest Algorithm 5)"), "MD5");
0058     ui.kcombobox_format->addItem(i18n("SHA-256 (Secure Hash Algorithm)"), "SHA-256");
0059     {
0060         int i = ui.kcombobox_format->findData(format);
0061         ui.kcombobox_format->setCurrentIndex(i);
0062     }
0063     connect(ui.kcombobox_format, SIGNAL(currentIndexChanged(int)), this, SLOT(trigger_changed()));
0064 
0065     ui.qlineedit_scheme->setText(scheme);
0066     connect(ui.qlineedit_scheme, SIGNAL(textEdited(const QString &)), this, SLOT(trigger_changed()));
0067 
0068     if (applyButton)
0069         applyButton->setEnabled(false);
0070 }
0071 
0072 ProfileDataHashlistDialog::~ProfileDataHashlistDialog()
0073 {
0074 }
0075 
0076 void ProfileDataHashlistDialog::slotAccepted()
0077 {
0078     if (save())
0079         accept();
0080     else
0081         ErrorDialog::show(this, error.message(), error.details());
0082 }
0083 
0084 void ProfileDataHashlistDialog::slotApplied()
0085 {
0086     if (!save())
0087         ErrorDialog::show(this, error.message(), error.details());
0088 }
0089 
0090 void ProfileDataHashlistDialog::scheme_wizard()
0091 {
0092     QString suffix = ui.kcombobox_format->itemData(ui.kcombobox_format->currentIndex()).toString().toLower();
0093 
0094     FilenameSchemeWizardDialog *dialog = new FilenameSchemeWizardDialog(ui.qlineedit_scheme->text(), suffix, this);
0095 
0096     if (dialog->exec() != QDialog::Accepted) {
0097         delete dialog;
0098         return;
0099     }
0100 
0101     ui.qlineedit_scheme->setText(dialog->scheme);
0102 
0103     delete dialog;
0104 
0105     trigger_changed();
0106 }
0107 
0108 void ProfileDataHashlistDialog::trigger_changed()
0109 {
0110     if (applyButton) {
0111         QString format = profile_model->data(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_HL_FORMAT_INDEX)).toString();
0112         QString scheme = profile_model->data(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_HL_NAME_INDEX)).toString();
0113 
0114         if (ui.kcombobox_format->itemData(ui.kcombobox_format->currentIndex()).toString() != format) {
0115             applyButton->setEnabled(true);
0116             return;
0117         }
0118         if (ui.qlineedit_scheme->text() != scheme) {
0119             applyButton->setEnabled(true);
0120             return;
0121         }
0122         applyButton->setEnabled(false);
0123     }
0124 }
0125 
0126 bool ProfileDataHashlistDialog::save()
0127 {
0128     QString format = ui.kcombobox_format->itemData(ui.kcombobox_format->currentIndex()).toString();
0129     QString scheme = ui.qlineedit_scheme->text();
0130 
0131     error.clear();
0132     bool success = true;
0133 
0134     if (success)
0135         success = profile_model->setData(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_HL_FORMAT_INDEX), format);
0136     if (success)
0137         success = profile_model->setData(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_HL_NAME_INDEX), scheme);
0138 
0139     if (!success)
0140         error = profile_model->lastError();
0141 
0142     if (success) {
0143         profile_model->commit();
0144         if (applyButton)
0145             applyButton->setEnabled(false);
0146         return true;
0147     }
0148 
0149     return false;
0150 }