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

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 "profiledatainfodialog.h"
0009 #include "utils/schemeparser.h"
0010 
0011 #include <KConfigGroup>
0012 #include <QDialogButtonBox>
0013 #include <QFileDialog>
0014 
0015 ProfileDataInfoDialog::ProfileDataInfoDialog(ProfileModel *profile_model, const int profile_row, const bool new_profile_mode, QWidget *parent)
0016     : QDialog(parent)
0017 {
0018     Q_UNUSED(parent);
0019 
0020     this->profile_model = profile_model;
0021     this->profile_row = profile_row;
0022     this->new_profile_mode = new_profile_mode;
0023 
0024     applyButton = nullptr;
0025 
0026     // profile data info data
0027     QStringList text = profile_model->data(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_INF_TEXT_INDEX)).toStringList();
0028     QString scheme = profile_model->data(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_INF_NAME_INDEX)).toString();
0029     QString suffix = profile_model->data(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_INF_SUFFIX_INDEX)).toString();
0030 
0031     setWindowTitle(i18n("Info Settings"));
0032 
0033     auto *mainLayout = new QVBoxLayout;
0034     setLayout(mainLayout);
0035 
0036     QDialogButtonBox::StandardButtons buttons = QDialogButtonBox::Ok | QDialogButtonBox::Cancel;
0037     if (!new_profile_mode)
0038         buttons |= QDialogButtonBox::Apply;
0039 
0040     QDialogButtonBox *buttonBox = new QDialogButtonBox(buttons);
0041     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0042     okButton->setDefault(true);
0043     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0044     if (!new_profile_mode)
0045         applyButton = buttonBox->button(QDialogButtonBox::Apply);
0046     connect(buttonBox, &QDialogButtonBox::accepted, this, &ProfileDataInfoDialog::slotAccepted);
0047     connect(buttonBox, &QDialogButtonBox::rejected, this, &ProfileDataInfoDialog::reject);
0048     if (!new_profile_mode)
0049         connect(applyButton, &QPushButton::clicked, this, &ProfileDataInfoDialog::slotApplied);
0050 
0051     QWidget *widget = new QWidget(this);
0052     mainLayout->addWidget(widget);
0053     mainLayout->addWidget(buttonBox);
0054     ui.setupUi(widget);
0055 
0056     help_dialog = new TextViewDialog(SchemeParser::helpHTMLDoc(4), i18n("Text info scheme help"), this);
0057 
0058     connect(ui.kpushbutton_scheme, SIGNAL(clicked()), this, SLOT(scheme_wizard()));
0059     ui.kpushbutton_scheme->setIcon(QIcon::fromTheme("tools-wizard"));
0060 
0061     ui.ktextedit_text->setPlainText(text.join("\n"));
0062     connect(ui.ktextedit_text, SIGNAL(textChanged()), this, SLOT(trigger_changed()));
0063 
0064     ui.qlineedit_scheme->setText(scheme);
0065     connect(ui.qlineedit_scheme, SIGNAL(textEdited(const QString &)), this, SLOT(trigger_changed()));
0066 
0067     ui.qlineedit_suffix->setText(suffix);
0068     connect(ui.qlineedit_suffix, SIGNAL(textEdited(const QString &)), this, SLOT(trigger_changed()));
0069 
0070     ui.kpushbutton_load->setIcon(QIcon::fromTheme("document-open"));
0071     ui.kpushbutton_save->setIcon(QIcon::fromTheme("document-save"));
0072 
0073     connect(ui.kurllabel_help, SIGNAL(leftClickedUrl()), this, SLOT(help()));
0074 
0075     connect(ui.kpushbutton_load, SIGNAL(clicked()), this, SLOT(load_text()));
0076     connect(ui.kpushbutton_save, SIGNAL(clicked()), this, SLOT(save_text()));
0077 
0078     if (applyButton)
0079         applyButton->setEnabled(false);
0080 }
0081 
0082 ProfileDataInfoDialog::~ProfileDataInfoDialog()
0083 {
0084     if (help_dialog != nullptr) {
0085         help_dialog->close();
0086         delete help_dialog;
0087         help_dialog = nullptr;
0088     }
0089 }
0090 
0091 void ProfileDataInfoDialog::slotAccepted()
0092 {
0093     if (save())
0094         accept();
0095     else
0096         ErrorDialog::show(this, error.message(), error.details());
0097 }
0098 
0099 void ProfileDataInfoDialog::slotApplied()
0100 {
0101     if (!save())
0102         ErrorDialog::show(this, error.message(), error.details());
0103 }
0104 
0105 void ProfileDataInfoDialog::scheme_wizard()
0106 {
0107     QString suffix = ui.qlineedit_suffix->text();
0108 
0109     FilenameSchemeWizardDialog *dialog = new FilenameSchemeWizardDialog(ui.qlineedit_scheme->text(), suffix, this);
0110 
0111     if (dialog->exec() != QDialog::Accepted) {
0112         delete dialog;
0113         return;
0114     }
0115 
0116     ui.qlineedit_scheme->setText(dialog->scheme);
0117 
0118     delete dialog;
0119 
0120     trigger_changed();
0121 }
0122 
0123 void ProfileDataInfoDialog::trigger_changed()
0124 {
0125     if (applyButton) {
0126         QStringList text = profile_model->data(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_INF_TEXT_INDEX)).toStringList();
0127         QString scheme = profile_model->data(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_INF_NAME_INDEX)).toString();
0128         QString suffix = profile_model->data(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_INF_SUFFIX_INDEX)).toString();
0129 
0130         if (ui.ktextedit_text->toPlainText().split('\n') != text) {
0131             applyButton->setEnabled(true);
0132             return;
0133         }
0134         if (ui.qlineedit_suffix->text() != suffix) {
0135             applyButton->setEnabled(true);
0136             return;
0137         }
0138         if (ui.qlineedit_scheme->text() != scheme) {
0139             applyButton->setEnabled(true);
0140             return;
0141         }
0142         applyButton->setEnabled(false);
0143     }
0144 }
0145 
0146 void ProfileDataInfoDialog::help()
0147 {
0148     help_dialog->show();
0149 }
0150 
0151 void ProfileDataInfoDialog::load_text()
0152 {
0153     QString filename = QFileDialog::getOpenFileName(this, i18n("Load Text Template"), QDir::homePath(), "*");
0154     if (!filename.isEmpty()) {
0155         QFile file(filename);
0156         if (file.open(QFile::ReadOnly)) {
0157             QTextStream in(&file);
0158             ui.ktextedit_text->setPlainText(in.readAll());
0159             file.close();
0160         }
0161     }
0162 }
0163 
0164 void ProfileDataInfoDialog::save_text()
0165 {
0166     QString filename = QFileDialog::getSaveFileName(this, i18n("Save Text Template"), QDir::homePath(), "*");
0167     if (!filename.isEmpty()) {
0168         QFile file(filename);
0169         if (file.open(QFile::WriteOnly | QFile::Truncate)) {
0170             QTextStream out(&file);
0171             out << ui.ktextedit_text->toPlainText();
0172             file.close();
0173         }
0174     }
0175 }
0176 
0177 bool ProfileDataInfoDialog::save()
0178 {
0179     QStringList text = ui.ktextedit_text->toPlainText().split('\n');
0180     QString suffix = ui.qlineedit_suffix->text();
0181     QString scheme = ui.qlineedit_scheme->text();
0182 
0183     error.clear();
0184     bool success = true;
0185 
0186     if (success)
0187         success = profile_model->setData(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_INF_TEXT_INDEX), text);
0188     if (success)
0189         success = profile_model->setData(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_INF_NAME_INDEX), scheme);
0190     if (success)
0191         success = profile_model->setData(profile_model->index(profile_row, PROFILE_MODEL_COLUMN_INF_SUFFIX_INDEX), suffix);
0192 
0193     if (!success)
0194         error = profile_model->lastError();
0195 
0196     if (success) {
0197         profile_model->commit();
0198         if (applyButton)
0199             applyButton->setEnabled(false);
0200         return true;
0201     }
0202 
0203     return false;
0204 }