File indexing completed on 2024-12-01 07:38:58
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 "verificationdialog.h" 0021 0022 #include <QSortFilterProxyModel> 0023 0024 #include <KLocalizedString> 0025 #include <KMessageBox> 0026 0027 #include "core/filemodel.h" 0028 #include "core/transferhandler.h" 0029 #include "core/verificationdelegate.h" 0030 #include "core/verificationmodel.h" 0031 #include "core/verifier.h" 0032 #include "settings.h" 0033 0034 VerificationAddDlg::VerificationAddDlg(VerificationModel *model, QWidget *parent, Qt::WindowFlags flags) 0035 : QDialog(parent, flags) 0036 , m_model(model) 0037 { 0038 setWindowTitle(i18n("Add checksum")); 0039 ui.setupUi(this); 0040 0041 QStringList supportedTypes = Verifier::supportedVerficationTypes(); 0042 supportedTypes.sort(); 0043 ui.hashTypes->addItems(supportedTypes); 0044 0045 KGuiItem::assign(ui.buttonBox->button(QDialogButtonBox::Ok), KStandardGuiItem::add()); 0046 0047 updateButton(); 0048 0049 connect(ui.newHash, &KLineEdit::textChanged, this, &VerificationAddDlg::updateButton); 0050 connect(ui.hashTypes, static_cast<void (KComboBox::*)(int)>(&KComboBox::currentIndexChanged), this, &VerificationAddDlg::updateButton); 0051 connect(this, &QDialog::accepted, this, &VerificationAddDlg::addChecksum); 0052 0053 connect(ui.buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); 0054 connect(ui.buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); 0055 } 0056 0057 QSize VerificationAddDlg::sizeHint() const 0058 { 0059 QSize sh = QDialog::sizeHint(); 0060 sh.setHeight(minimumSize().height()); 0061 sh.setWidth(sh.width() * 1.5); 0062 return sh; 0063 } 0064 0065 void VerificationAddDlg::updateButton() 0066 { 0067 const QString type = ui.hashTypes->currentText(); 0068 const QString hash = ui.newHash->text(); 0069 const bool enabled = Verifier::isChecksum(type, hash); 0070 0071 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(enabled); 0072 } 0073 0074 void VerificationAddDlg::addChecksum() 0075 { 0076 if (m_model) { 0077 m_model->addChecksum(ui.hashTypes->currentText(), ui.newHash->text()); 0078 } 0079 } 0080 0081 VerificationDialog::VerificationDialog(QWidget *parent, TransferHandler *transfer, const QUrl &file) 0082 : KGetSaveSizeDialog("VerificationDialog", parent) 0083 , m_transfer(transfer) 0084 , m_verifier(transfer->verifier(file)) 0085 , m_model(nullptr) 0086 , m_proxy(nullptr) 0087 , m_fileModel(nullptr) 0088 { 0089 if (m_verifier) { 0090 m_model = m_verifier->model(); 0091 connect(m_verifier, &Verifier::verified, this, &VerificationDialog::slotVerified); 0092 } 0093 0094 setWindowTitle(i18n("Transfer Verification for %1", file.fileName())); 0095 0096 ui.setupUi(this); 0097 0098 KGuiItem::assign(ui.add, KStandardGuiItem::add()); 0099 KGuiItem::assign(ui.remove, KStandardGuiItem::remove()); 0100 KGuiItem::assign(ui.closeButton, KStandardGuiItem::close()); 0101 ui.verifying->hide(); 0102 0103 if (m_model) { 0104 m_proxy = new QSortFilterProxyModel(this); 0105 m_proxy->setSourceModel(m_model); 0106 ui.usedHashes->setModel(m_proxy); 0107 ui.usedHashes->setItemDelegate(new VerificationDelegate(this)); 0108 0109 QByteArray loadedState = QByteArray::fromBase64(Settings::verificationHeaderState().toLatin1()); 0110 if (!loadedState.isEmpty()) { 0111 ui.usedHashes->header()->restoreState(loadedState); 0112 } 0113 0114 m_fileModel = m_transfer->fileModel(); 0115 if (m_fileModel) { 0116 m_file = m_fileModel->index(file, FileItem::File); 0117 connect(m_fileModel, &FileModel::fileFinished, this, &VerificationDialog::fileFinished); 0118 } 0119 0120 updateButtons(); 0121 0122 connect(m_model, &VerificationModel::dataChanged, this, &VerificationDialog::updateButtons); 0123 connect(m_model, &VerificationModel::rowsRemoved, this, &VerificationDialog::updateButtons); 0124 connect(ui.usedHashes, &QTreeView::clicked, this, &VerificationDialog::updateButtons); 0125 connect(ui.add, &QPushButton::clicked, this, &VerificationDialog::addClicked); 0126 connect(ui.remove, &QPushButton::clicked, this, &VerificationDialog::removeClicked); 0127 connect(ui.verify, &QPushButton::clicked, this, &VerificationDialog::verifyClicked); 0128 } 0129 0130 connect(this, &VerificationDialog::finished, this, &VerificationDialog::slotFinished); 0131 connect(ui.closeButton, &QPushButton::clicked, this, &QDialog::reject); 0132 } 0133 0134 QSize VerificationDialog::sizeHint() const 0135 { 0136 QSize sh = QDialog::sizeHint(); 0137 sh.setWidth(sh.width() * 1.2); 0138 return sh; 0139 } 0140 0141 void VerificationDialog::slotFinished() 0142 { 0143 if (m_model) { 0144 Settings::setVerificationHeaderState(ui.usedHashes->header()->saveState().toBase64()); 0145 } 0146 } 0147 0148 void VerificationDialog::fileFinished(const QUrl &file) 0149 { 0150 if (m_fileModel && (m_fileModel->getUrl(m_file) == file)) { 0151 updateButtons(); 0152 } 0153 } 0154 0155 void VerificationDialog::updateButtons() 0156 { 0157 ui.remove->setEnabled(m_model && ui.usedHashes->selectionModel()->hasSelection()); 0158 0159 // check if the download finished and if the selected index is verifyable 0160 bool verifyEnabled = false; 0161 if (m_fileModel && m_fileModel->downloadFinished(m_fileModel->getUrl(m_file))) { 0162 const QModelIndexList indexes = ui.usedHashes->selectionModel()->selectedRows(); 0163 if (indexes.count() == 1) { 0164 verifyEnabled = m_verifier->isVerifyable(indexes.first()); 0165 } 0166 } 0167 ui.verify->setEnabled(verifyEnabled); 0168 } 0169 0170 void VerificationDialog::removeClicked() 0171 { 0172 while (ui.usedHashes->selectionModel()->hasSelection()) { 0173 const QModelIndex index = ui.usedHashes->selectionModel()->selectedRows().first(); 0174 m_model->removeRow(m_proxy->mapToSource(index).row()); 0175 } 0176 } 0177 0178 void VerificationDialog::addClicked() 0179 { 0180 auto *dialog = new VerificationAddDlg(m_model, this); 0181 dialog->show(); 0182 } 0183 0184 void VerificationDialog::verifyClicked() 0185 { 0186 const QModelIndex index = m_proxy->mapToSource(ui.usedHashes->selectionModel()->selectedRows().first()); 0187 if (index.isValid()) { 0188 m_verifier->verify(index); 0189 ui.progressBar->setMaximum(0); 0190 ui.verifying->show(); 0191 } 0192 } 0193 0194 void VerificationDialog::slotVerified(bool verified) 0195 { 0196 ui.progressBar->setMaximum(1); 0197 ui.verifying->hide(); 0198 0199 if (verified) { 0200 QString fileName; 0201 if (m_fileModel) { 0202 fileName = m_fileModel->getUrl(m_file).fileName(); 0203 } 0204 0205 KMessageBox::information(this, i18n("%1 was successfully verified.", fileName), i18n("Verification successful")); 0206 } 0207 } 0208 0209 #include "moc_verificationdialog.cpp"