File indexing completed on 2024-11-03 05:18:23
0001 /* 0002 0003 SPDX-License-Identifier: GPL-2.0-or-later 0004 0005 */ 0006 0007 #include "kgpgmd5widget.h" 0008 0009 #include <QApplication> 0010 #include <QCryptographicHash> 0011 #include <QFile> 0012 #include <QClipboard> 0013 #include <QHBoxLayout> 0014 #include <QLabel> 0015 #include <QLineEdit> 0016 0017 #include <KMessageBox> 0018 #include <KLocalizedString> 0019 #include <KLed> 0020 #include <KConfigGroup> 0021 #include <QDialogButtonBox> 0022 #include <QPushButton> 0023 #include <QVBoxLayout> 0024 0025 0026 Md5Widget::Md5Widget(QWidget *parent, const QUrl &url) 0027 : QDialog(parent) 0028 { 0029 setWindowTitle(i18n("MD5 Checksum")); 0030 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Close|QDialogButtonBox::Apply); 0031 QWidget *mainWidget = new QWidget(this); 0032 QVBoxLayout *mainLayout = new QVBoxLayout(this); 0033 setLayout(mainLayout); 0034 mainLayout->addWidget(mainWidget); 0035 connect(buttonBox, &QDialogButtonBox::accepted, this, &Md5Widget::accept); 0036 connect(buttonBox, &QDialogButtonBox::rejected, this, &Md5Widget::reject); 0037 buttonBox->button(QDialogButtonBox::Close)->setDefault(true); 0038 buttonBox->button(QDialogButtonBox::Apply)->setText(i18n("Compare MD5 with Clipboard")); 0039 0040 QFile f(url.path()); 0041 QCryptographicHash checkfile(QCryptographicHash::Md5); 0042 0043 if (f.open(QIODevice::ReadOnly)) { 0044 checkfile.addData(&f); 0045 f.close(); 0046 } 0047 0048 m_md5sum = QLatin1String( checkfile.result() ); 0049 0050 QWidget *page = new QWidget(this); 0051 0052 QLabel *firstlabel = new QLabel(page); 0053 firstlabel->setText(i18n("MD5 sum for <b>%1</b> is:", url.fileName())); 0054 0055 QLineEdit *md5lineedit = new QLineEdit(m_md5sum, page); 0056 md5lineedit->setReadOnly(true); 0057 0058 m_led = new KLed(QColor(80, 80, 80), KLed::Off, KLed::Sunken, KLed::Circular, page); 0059 QSizePolicy policy(QSizePolicy::Fixed, QSizePolicy::Fixed); 0060 policy.setVerticalStretch(0); 0061 policy.setHorizontalStretch(0); 0062 policy.setHeightForWidth(m_led->sizePolicy().hasHeightForWidth()); 0063 m_led->setSizePolicy(policy); 0064 0065 m_label = new QLabel(page); 0066 m_label->setText(i18n("<b>Unknown status</b>")); 0067 0068 QHBoxLayout *ledlayout = new QHBoxLayout(); 0069 ledlayout->addWidget(m_led); 0070 ledlayout->addWidget(m_label); 0071 0072 QVBoxLayout *dialoglayout = new QVBoxLayout(page); 0073 dialoglayout->addWidget(firstlabel); 0074 dialoglayout->addWidget(md5lineedit); 0075 dialoglayout->addLayout(ledlayout); 0076 dialoglayout->addStretch(); 0077 0078 mainLayout->addWidget(page); 0079 mainLayout->addWidget(buttonBox); 0080 0081 connect(buttonBox->button(QDialogButtonBox::Apply), &QPushButton::clicked, this, &Md5Widget::slotApply); 0082 } 0083 0084 void Md5Widget::slotApply() 0085 { 0086 QString text = qApp->clipboard()->text().remove(QLatin1Char( ' ' )); 0087 if (!text.isEmpty()) 0088 { 0089 if (text.length() != m_md5sum.length()) 0090 KMessageBox::error(this, i18n("Clipboard content is not a MD5 sum.")); 0091 else 0092 if (text == m_md5sum) 0093 { 0094 m_label->setText(i18n("<b>Correct checksum</b>, file is ok.")); 0095 m_led->setColor(QColor(Qt::green)); 0096 m_led->on(); 0097 } 0098 else 0099 { 0100 m_label->setText(i18n("<b>Wrong checksum, <em>file corrupted</em></b>")); 0101 m_led->setColor(QColor(Qt::red)); 0102 m_led->on(); 0103 } 0104 } 0105 }