📄 checksumdlg.cpp
字号:
tmpErr->unlink(); delete tmpErr;}// ------------- MatchChecksumDlgQString MatchChecksumDlg::checksumTypesFilter;MatchChecksumDlg::MatchChecksumDlg(const QStringList& files, bool containFolders, const QString& path, const QString& checksumFile): KDialogBase(Plain, i18n("Verify Checksum"), Ok | Cancel, Ok, krApp) { QPtrList<CS_Tool> tools = getTools(containFolders); if (tools.count() == 0) { // nothing was suggested?! QString error = i18n("<qt>Can't verify checksum since no supported tool was found. " "Please check the <b>Dependencies</b> page in Krusader's settings.</qt>"); if (containFolders) error += i18n("<qt><b>Note</b>: you've selected directories, and probably have no recursive checksum tool installed." " Krusader currently supports <i>md5deep, sha1deep, sha256deep, tigerdeep and cfv</i></qt>"); KMessageBox::error(0, error); return; } QGridLayout *layout = new QGridLayout( plainPage(), 1, 1, KDialogBase::marginHint(), KDialogBase::spacingHint()); int row=0; // title (icon+text) QHBoxLayout *hlayout = new QHBoxLayout(layout, KDialogBase::spacingHint()); QLabel *p = new QLabel(plainPage()); p->setPixmap(krLoader->loadIcon("binary", KIcon::Desktop, 32)); hlayout->addWidget(p); QLabel *l1 = new QLabel(i18n("About to verify checksum for the following files") + (containFolders ? i18n(" and folders:") : ":"), plainPage()); hlayout->addWidget(l1); layout->addMultiCellLayout(hlayout, row, row, 0, 1, Qt::AlignLeft); ++row; // file list KListBox *lb = new KListBox(plainPage()); lb->insertStringList(files); layout->addMultiCellWidget(lb, row, row, 0, 1); ++row; // checksum file QHBoxLayout *hlayout2 = new QHBoxLayout(layout, KDialogBase::spacingHint()); QLabel *l2 = new QLabel(i18n("Checksum file:"), plainPage()); hlayout2->addWidget(l2); KURLRequester *checksumFileReq = new KURLRequester( plainPage() ); if (!checksumFile.isEmpty()) checksumFileReq->setURL(checksumFile); checksumFileReq->fileDialog()->setURL(path); checksumFileReq->setFocus(); hlayout2->addWidget(checksumFileReq); layout->addMultiCellLayout(hlayout2, row, row, 0, 1, Qt::AlignLeft); if (exec() != Accepted) return; QString file = checksumFileReq->url(); QString extension; if (!verifyChecksumFile(file, extension)) { KMessageBox::error(0, i18n("<qt>Error reading checksum file <i>%1</i>.<br />Please specify a valid checksum file.</qt>").arg(file)); return; } // do we have a tool for that extension? uint i; CS_Tool *mytool = 0; for ( i=0; i < tools.count(); ++i ) if (cs_typeToText[tools.at(i)->type] == extension.lower()) { mytool = tools.at(i); break; } if (!mytool) { KMessageBox::error(0, i18n("<qt>Krusader can't find a checksum tool that handles %1 on your system. Please check the <b>Dependencies</b> page in Krusader's settings.</qt>").arg(extension)); return; } // else implied: run the process tmpOut = new KTempFile(locateLocal("tmp", "krusader"), ".stdout" ); tmpErr = new KTempFile(locateLocal("tmp", "krusader"), ".stderr" ); KProcess proc; mytool->verify(proc, mytool, KrServices::quote(files), KrServices::quote(file), containFolders, tmpOut->name(), tmpErr->name(), extension); krApp->startWaiting(i18n("Verifying checksums ..."), 0, true); QApplication::setOverrideCursor( KCursor::waitCursor() ); bool r = proc.start(KProcess::NotifyOnExit, KProcess::AllOutput); if (r) while ( proc.isRunning() ) { usleep( 500 ); qApp->processEvents(); if (krApp->wasWaitingCancelled()) { // user cancelled proc.kill(); QApplication::restoreOverrideCursor(); return; } }; if (!r || !proc.normalExit()) { KMessageBox::error(0, i18n("<qt>There was an error while running <b>%1</b>.</qt>").arg(mytool->binary)); return; } QApplication::restoreOverrideCursor(); krApp->stopWait(); // send both stdout and stderr QStringList stdOut,stdErr; if (!KrServices::fileToStringList(tmpOut->textStream(), stdOut) || !KrServices::fileToStringList(tmpErr->textStream(), stdErr)) { KMessageBox::error(krApp, i18n("Error reading stdout or stderr")); return; } VerifyResultDlg dlg(mytool->failed(stdOut, stdErr)); tmpOut->unlink(); delete tmpOut; tmpErr->unlink(); delete tmpErr;}bool MatchChecksumDlg::verifyChecksumFile(QString path, QString& extension) { QFileInfo f(path); if (!f.exists() || f.isDir()) return false; // find the extension extension = path.mid(path.findRev(".")+1); // TODO: do we know the extension? if not, ask the user for one return true;}// ------------- VerifyResultDlgVerifyResultDlg::VerifyResultDlg(const QStringList& failed): KDialogBase(Plain, i18n("Verify Checksum"), Close, Close, krApp) { QGridLayout *layout = new QGridLayout( plainPage(), 1, 1, KDialogBase::marginHint(), KDialogBase::spacingHint()); bool errors = failed.size()>0; int row = 0; // create the icon and title QHBoxLayout *hlayout = new QHBoxLayout(layout, KDialogBase::spacingHint()); QLabel p(plainPage()); p.setPixmap(krLoader->loadIcon(errors ? "messagebox_critical" : "messagebox_info", KIcon::Desktop, 32)); hlayout->addWidget(&p); QLabel *l1 = new QLabel((errors ? i18n("Errors were detected while verifying the checksums") : i18n("Checksums were verified successfully")), plainPage()); hlayout->addWidget(l1); layout->addMultiCellLayout(hlayout,row,row,0,1, Qt::AlignLeft); ++row; if (errors) { QLabel *l3 = new QLabel(i18n("The following files have failed:"), plainPage()); layout->addMultiCellWidget(l3, row, row, 0, 1); ++row; KListBox *lb2 = new KListBox(plainPage()); lb2->insertStringList(failed); layout->addMultiCellWidget(lb2, row, row, 0, 1); ++row; } exec();}// ------------- ChecksumResultsDlgChecksumResultsDlg::ChecksumResultsDlg(const QStringList& stdOut, const QStringList& stdErr, const QString& suggestedFilename, const QString& binary, const QString& /* type */, bool standardFormat): KDialogBase(Plain, i18n("Create Checksum"), Ok | Cancel, Ok, krApp), _binary(binary) { QGridLayout *layout = new QGridLayout( plainPage(), 1, 1, KDialogBase::marginHint(), KDialogBase::spacingHint()); // md5 tools display errors into stderr, so we'll use that to determine the result of the job bool errors = stdErr.size()>0; bool successes = stdOut.size()>0; int row = 0; // create the icon and title QHBoxLayout *hlayout = new QHBoxLayout(layout, KDialogBase::spacingHint()); QLabel p(plainPage()); p.setPixmap(krLoader->loadIcon(errors ? "messagebox_critical" : "messagebox_info", KIcon::Desktop, 32)); hlayout->addWidget(&p); QLabel *l1 = new QLabel((errors ? i18n("Errors were detected while creating the checksums") : i18n("Checksums were created successfully")), plainPage()); hlayout->addWidget(l1); layout->addMultiCellLayout(hlayout,row,row,0,1, Qt::AlignLeft); ++row; if (successes) { if (errors) { QLabel *l2 = new QLabel(i18n("Here are the calculated checksums:"), plainPage()); layout->addMultiCellWidget(l2, row, row, 0, 1); ++row; } KListView *lv = new KListView(plainPage()); if(standardFormat){ lv->addColumn(i18n("Hash")); lv->addColumn(i18n("File")); lv->setAllColumnsShowFocus(true); } else { lv->addColumn(i18n("File and hash")); } for ( QStringList::ConstIterator it = stdOut.begin(); it != stdOut.end(); ++it ) { QString line = (*it); if(standardFormat) { int space = line.find(' '); new KListViewItem(lv, line.left(space), line.mid(space+2)); } else { new KListViewItem(lv, line); } } layout->addMultiCellWidget(lv, row, row, 0, 1); ++row; } if (errors) { QFrame *line1 = new QFrame( plainPage() ); line1->setGeometry( QRect( 60, 210, 501, 20 ) ); line1->setFrameShape( QFrame::HLine ); line1->setFrameShadow( QFrame::Sunken ); layout->addMultiCellWidget(line1, row, row, 0, 1); ++row; QLabel *l3 = new QLabel(i18n("Here are the errors received:"), plainPage()); layout->addMultiCellWidget(l3, row, row, 0, 1); ++row; KListBox *lb = new KListBox(plainPage()); lb->insertStringList(stdErr); layout->addMultiCellWidget(lb, row, row, 0, 1); ++row; } // save checksum to disk, if any hashes are found KURLRequester *checksumFile=0; QCheckBox *saveFileCb=0; if (successes) { QHBoxLayout *hlayout2 = new QHBoxLayout(layout, KDialogBase::spacingHint()); saveFileCb = new QCheckBox(i18n("Save checksum to file:"), plainPage()); saveFileCb->setChecked(true); hlayout2->addWidget(saveFileCb); checksumFile = new KURLRequester( suggestedFilename, plainPage() ); hlayout2->addWidget(checksumFile, Qt::AlignLeft); layout->addMultiCellLayout(hlayout2, row, row,0,1, Qt::AlignLeft); ++row; connect(saveFileCb, SIGNAL(toggled(bool)), checksumFile, SLOT(setEnabled(bool))); checksumFile->setFocus(); } QCheckBox *onePerFile=0; if (stdOut.size() > 1 && standardFormat) { onePerFile = new QCheckBox(i18n("Checksum file for each source file"), plainPage()); onePerFile->setChecked(false); // clicking this, disables the 'save as' part connect(onePerFile, SIGNAL(toggled(bool)), saveFileCb, SLOT(toggle())); connect(onePerFile, SIGNAL(toggled(bool)), saveFileCb, SLOT(setDisabled(bool))); connect(onePerFile, SIGNAL(toggled(bool)), checksumFile, SLOT(setDisabled(bool))); layout->addMultiCellWidget(onePerFile, row, row,0,1, Qt::AlignLeft); ++row; } if (exec() == Accepted && successes) { if (stdOut.size()>1 && standardFormat && onePerFile->isChecked()) { savePerFile(stdOut, suggestedFilename.mid(suggestedFilename.findRev('.'))); } else if (saveFileCb->isEnabled() && saveFileCb->isChecked() && !checksumFile->url().simplifyWhiteSpace().isEmpty()) { saveChecksum(stdOut, checksumFile->url()); } }}bool ChecksumResultsDlg::saveChecksum(const QStringList& data, QString filename) { if (QFile::exists(filename) && KMessageBox::warningContinueCancel(this, i18n("File %1 already exists.\nAre you sure you want to overwrite it?").arg(filename), i18n("Warning"), i18n("Overwrite")) != KMessageBox::Continue) { // find a better name to save to filename = KFileDialog::getSaveFileName(QString::null, "*", 0, i18n("Select a file to save to")); if (filename.simplifyWhiteSpace().isEmpty()) return false; } QFile file(filename); if (!file.open(IO_WriteOnly)) { KMessageBox::detailedError(0, i18n("Error saving file %1").arg(filename), file.errorString()); return false; } QTextStream stream(&file); for ( QStringList::ConstIterator it = data.constBegin(); it != data.constEnd(); ++it) stream << *it << "\n"; file.close(); return true;}void ChecksumResultsDlg::savePerFile(const QStringList& data, const QString& type) { krApp->startWaiting(i18n("Saving checksum files..."), 0); for ( QStringList::ConstIterator it = data.begin(); it != data.end(); ++it ) { QString line = (*it); QString filename = line.mid(line.find(' ')+2)+type; if (!saveChecksum(*it, filename)) { KMessageBox::error(0, i18n("Errors occured while saving multiple checksums. Stopping")); krApp->stopWait(); return; } } krApp->stopWait();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -