File indexing completed on 2023-12-03 08:26:47
0001 /*************************************************************************** 0002 * Copyright (C) 2009 Matthias Fuchs <mat69@gmx.net> * 0003 * * 0004 * This program is free software; you can redistribute it and/or modify * 0005 * it under the terms of the GNU General Public License as published by * 0006 * the Free Software Foundation; either version 2 of the License, or * 0007 * (at your option) any later version. * 0008 * * 0009 * This program is distributed in the hope that it will be useful, * 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0012 * GNU General Public License for more details. * 0013 * * 0014 * You should have received a copy of the GNU General Public License * 0015 * along with this program; if not, write to the * 0016 * Free Software Foundation, Inc., * 0017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * 0018 ***************************************************************************/ 0019 0020 #include "filedlg.h" 0021 #include "../verificationdialog.h" 0022 #include "metalinker.h" 0023 #include "urlwidget.h" 0024 0025 #include "../../core/verificationdelegate.h" 0026 #include "../../core/verificationmodel.h" 0027 #include "../../core/verifier.h" 0028 0029 #include <KStandardGuiItem> 0030 #include <QSortFilterProxyModel> 0031 0032 #include <KLocalizedString> 0033 0034 FileDlg::FileDlg(KGetMetalink::File *file, 0035 const QStringList ¤tFileNames, 0036 QSortFilterProxyModel *countrySort, 0037 QSortFilterProxyModel *languageSort, 0038 QWidget *parent, 0039 bool edit) 0040 : KGetSaveSizeDialog("FileDlg", parent) 0041 , m_file(file) 0042 , m_initialFileName(m_file->name) 0043 , m_currentFileNames(currentFileNames) 0044 , m_edit(edit) 0045 { 0046 // remove the initial name, to see later if the chosen name is still free 0047 m_currentFileNames.removeAll(m_initialFileName); 0048 0049 ui.setupUi(this); 0050 0051 QValidator *validator = new QIntValidator(); 0052 ui.size->setValidator(validator); 0053 0054 m_urlWidget = new UrlWidget(this); 0055 m_urlWidget->init(&m_file->resources, countrySort); 0056 ui.urlLayout->addWidget(m_urlWidget->widget()); 0057 connect(m_urlWidget, &UrlWidget::urlsChanged, this, &FileDlg::slotUpdateOkButton); 0058 0059 auto *data = new QWidget(this); 0060 uiData.setupUi(data); 0061 ui.dataLayout->addWidget(data); 0062 0063 ui.infoWidget->setCloseButtonVisible(false); 0064 ui.infoWidget->setMessageType(KMessageWidget::Information); 0065 0066 // set the file data 0067 ui.name->setText(m_file->name); 0068 uiData.identity->setText(m_file->data.identity); 0069 uiData.version->setText(m_file->data.version); 0070 uiData.description->setText(m_file->data.description); 0071 uiData.logo->setUrl(m_file->data.logo); 0072 if (m_file->data.oses.count()) { 0073 uiData.os->setText(m_file->data.oses.join(i18nc("comma, to separate members of a list", ","))); 0074 } 0075 uiData.copyright->setText(m_file->data.copyright); 0076 uiData.pub_name->setText(m_file->data.publisher.name); 0077 uiData.pub_url->setUrl(m_file->data.publisher.url); 0078 0079 if (m_file->size) { 0080 ui.size->setText(QString::number(m_file->size)); 0081 } 0082 0083 // create the language selection 0084 uiData.language->setModel(languageSort); 0085 if (m_file->data.languages.count()) { // TODO 4.5 support multiple languages 0086 const int index = uiData.language->findData(m_file->data.languages.first()); 0087 uiData.language->setCurrentIndex(index); 0088 } else { 0089 // Do not select a language of a file if none was defined. 0090 uiData.language->setCurrentIndex(-1); 0091 } 0092 0093 // create the verification stuff 0094 m_verificationModel = new VerificationModel(this); 0095 QHash<QString, QString>::const_iterator it; 0096 QHash<QString, QString>::const_iterator itEnd = m_file->verification.hashes.constEnd(); 0097 for (it = m_file->verification.hashes.constBegin(); it != itEnd; ++it) { 0098 m_verificationModel->addChecksum(it.key(), it.value()); 0099 } 0100 KGuiItem::assign(ui.add_hash, KStandardGuiItem::add()); 0101 KGuiItem::assign(ui.remove_hash, KStandardGuiItem::remove()); 0102 m_verificationProxy = new QSortFilterProxyModel(this); 0103 m_verificationProxy->setSourceModel(m_verificationModel); 0104 ui.used_hashes->setSortingEnabled(true); 0105 ui.used_hashes->hideColumn(VerificationModel::Verified); 0106 ui.used_hashes->setModel(m_verificationProxy); 0107 ui.used_hashes->setItemDelegate(new VerificationDelegate(this)); 0108 slotUpdateVerificationButtons(); 0109 0110 connect(m_verificationModel, &VerificationModel::dataChanged, this, &FileDlg::slotUpdateVerificationButtons); 0111 connect(m_verificationModel, &VerificationModel::rowsRemoved, this, &FileDlg::slotUpdateVerificationButtons); 0112 connect(ui.used_hashes, &QTreeView::clicked, this, &FileDlg::slotUpdateVerificationButtons); 0113 connect(ui.add_hash, &QPushButton::clicked, this, &FileDlg::slotAddHash); 0114 connect(ui.remove_hash, &QPushButton::clicked, this, &FileDlg::slotRemoveHash); 0115 connect(ui.name, &KLineEdit::textEdited, this, &FileDlg::slotUpdateOkButton); 0116 connect(this, &QDialog::accepted, this, &FileDlg::slotOkClicked); 0117 connect(ui.buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); 0118 connect(ui.buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); 0119 0120 slotUpdateOkButton(); 0121 0122 setWindowTitle(i18n("File Properties")); 0123 } 0124 0125 void FileDlg::slotUpdateOkButton() 0126 { 0127 bool hasName = !ui.name->text().isEmpty(); 0128 bool hasUrls = m_urlWidget->hasUrls(); 0129 bool isDuplicate = (m_currentFileNames.indexOf(ui.name->text()) > -1); 0130 0131 QStringList information; 0132 0133 if (!hasName) { 0134 information << i18n("Enter a filename."); 0135 } 0136 if (isDuplicate) { 0137 information << i18n("The filename exists already, choose a different one."); 0138 } 0139 if (!hasUrls) { 0140 information << i18n("Enter at least one URL."); 0141 } 0142 0143 ui.infoWidget->setText(information.join(" ")); 0144 ui.infoWidget->setVisible(!information.isEmpty()); 0145 0146 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(hasName && hasUrls && !isDuplicate); 0147 } 0148 0149 void FileDlg::slotUpdateVerificationButtons() 0150 { 0151 ui.remove_hash->setEnabled(ui.used_hashes->selectionModel()->hasSelection()); 0152 } 0153 0154 void FileDlg::slotRemoveHash() 0155 { 0156 while (ui.used_hashes->selectionModel()->hasSelection()) { 0157 const QModelIndex index = ui.used_hashes->selectionModel()->selectedRows().first(); 0158 m_verificationModel->removeRow(m_verificationProxy->mapToSource(index).row()); 0159 } 0160 } 0161 0162 void FileDlg::slotAddHash() 0163 { 0164 auto *dialog = new VerificationAddDlg(m_verificationModel, this); 0165 dialog->show(); 0166 } 0167 0168 void FileDlg::slotOkClicked() 0169 { 0170 QList<KGetMetalink::Metaurl> metaurls = m_file->resources.metaurls; // TODO remove once metaurls are also shown 0171 QList<KGetMetalink::Pieces> pieces = m_file->verification.pieces; // TODO remove once the partial hashes are also shown 0172 m_file->clear(); 0173 0174 m_file->name = ui.name->text(); 0175 m_file->size = ui.size->text().toLongLong(); 0176 m_file->data.identity = uiData.identity->text(); 0177 m_file->data.version = uiData.version->text(); 0178 m_file->data.description = uiData.description->text(); 0179 m_file->data.logo = QUrl(uiData.logo->text()); 0180 if (!uiData.os->text().isEmpty()) { 0181 m_file->data.oses = uiData.os->text().split(i18nc("comma, to separate members of a list", ",")); 0182 } 0183 m_file->data.copyright = uiData.copyright->text(); 0184 m_file->data.publisher.name = uiData.pub_name->text(); 0185 m_file->data.publisher.url = QUrl(uiData.pub_url->text()); 0186 m_file->data.languages << uiData.language->itemData(uiData.language->currentIndex()).toString(); 0187 0188 m_urlWidget->save(); 0189 m_file->resources.metaurls = metaurls; 0190 0191 // store the verification data 0192 for (int i = 0; i < m_verificationModel->rowCount(); ++i) { 0193 const QString type = m_verificationModel->index(i, VerificationModel::Type).data().toString(); 0194 const QString hash = m_verificationModel->index(i, VerificationModel::Checksum).data().toString(); 0195 m_file->verification.hashes[type] = hash; 0196 } 0197 m_file->verification.pieces = pieces; 0198 0199 if (m_edit) { 0200 // the file has been edited 0201 Q_EMIT fileEdited(m_initialFileName, m_file->name); 0202 } else { 0203 // a new file should be added, not edited 0204 Q_EMIT addFile(); 0205 } 0206 } 0207 0208 #include "moc_filedlg.cpp"