Page Menu
Home
GnuPG
Search
Configure Global Search
Log In
Files
F23020608
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Size
37 KB
Subscribers
None
View Options
diff --git a/crypto/gui/verifychecksumsdialog.cpp b/crypto/gui/verifychecksumsdialog.cpp
index 605f25681..8f4f7cbf3 100644
--- a/crypto/gui/verifychecksumsdialog.cpp
+++ b/crypto/gui/verifychecksumsdialog.cpp
@@ -1,311 +1,364 @@
/* -*- mode: c++; c-basic-offset:4 -*-
crypto/gui/verifychecksumsdialog.cpp
This file is part of Kleopatra, the KDE keymanager
Copyright (c) 2010 Klarälvdalens Datakonsult AB
Kleopatra is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Kleopatra is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
In addition, as a special exception, the copyright holders give
permission to link the code of this program with any edition of
the Qt library by Trolltech AS, Norway (or with modified versions
of Qt that use the same license as Qt), and distribute linked
combinations including the two. You must obey the GNU General
Public License in all respects for all of the code used other than
Qt. If you modify this file, you may extend this exception to
your version of the file, but you are not obligated to do so. If
you do not wish to do so, delete this exception statement from
your version.
*/
#include <config-kleopatra.h>
#include "verifychecksumsdialog.h"
#include <KDebug>
#include <KLocalizedString>
-#include <KPushButton>
-#include <KStandardGuiItem>
-#include <KUrl>
+#include <KMessageBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QStringList>
#include <QVBoxLayout>
#include <QHash>
#include <QTreeView>
#include <QSortFilterProxyModel>
#include <QDirModel>
#include <QProgressBar>
#include <QDialogButtonBox>
+#include <QPushButton>
#include <boost/static_assert.hpp>
#include <cassert>
using namespace Kleo;
using namespace Kleo::Crypto;
using namespace Kleo::Crypto::Gui;
using namespace boost;
namespace {
static Qt::GlobalColor statusColor[] = {
Qt::color0, // Unknown - nothing
Qt::green, // OK
Qt::red, // Failed
Qt::darkRed, // Error
};
BOOST_STATIC_ASSERT((sizeof statusColor/sizeof *statusColor == VerifyChecksumsDialog::NumStatii));
class ColorizedFileSystemModel : public QDirModel {
Q_OBJECT
public:
explicit ColorizedFileSystemModel( QObject * parent=0 )
: QDirModel( parent ),
statusMap()
{
}
/* reimp */ QVariant data( const QModelIndex & mi, int role=Qt::DisplayRole ) const {
if ( mi.isValid() && role == Qt::BackgroundRole ) {
const QHash<QString,VerifyChecksumsDialog::Status>::const_iterator
it = statusMap.find( filePath( mi ) );
if ( it != statusMap.end() )
if ( const Qt::GlobalColor c = statusColor[*it] )
return c;
}
return QDirModel::data( mi, role );
}
public Q_SLOTS:
void setStatus( const QString & file, VerifyChecksumsDialog::Status status ) {
if ( status >= VerifyChecksumsDialog::NumStatii || file.isEmpty() )
return;
// canonicalize filename:
const QModelIndex mi = index( file );
const QString canonical = filePath( mi );
if ( canonical.isEmpty() ) {
qDebug() << Q_FUNC_INFO << ": can't locate file" << file;
return;
}
const QHash<QString,VerifyChecksumsDialog::Status>::iterator
it = statusMap.find( canonical );
if ( it != statusMap.end() )
if ( *it == status )
return; // nothing to do
else
*it = status;
else
statusMap[canonical] = status;
emitDataChangedFor( mi );
}
void clearStatusInformation() {
using std::swap;
QHash<QString,VerifyChecksumsDialog::Status> oldStatusMap;
swap( statusMap, oldStatusMap );
for ( QHash<QString,VerifyChecksumsDialog::Status>::const_iterator it = oldStatusMap.begin(), end = oldStatusMap.end() ; it != end ; ++it )
emitDataChangedFor( it.key() );
}
private:
void emitDataChangedFor( const QString & file ) {
emitDataChangedFor( index( file ) );
}
void emitDataChangedFor( const QModelIndex & mi ) {
const QModelIndex p = parent( mi );
emit dataChanged( index( mi.row(), 0, p ), index( mi.row(), columnCount( p ) - 1, p ) );
}
private:
QHash<QString,VerifyChecksumsDialog::Status> statusMap;
};
static int find_layout_item( const QBoxLayout & blay ) {
for ( int i = 0, end = blay.count() ; i < end ; ++i )
if ( QLayoutItem * item = blay.itemAt( i ) )
if ( item->layout() )
return i;
return 0;
}
struct BaseWidget {
QSortFilterProxyModel proxy;
QLabel label;
QTreeView view;
BaseWidget( QDirModel * model, QWidget * parent, QVBoxLayout * vlay )
: proxy(),
label( parent ),
view( parent )
{
KDAB_SET_OBJECT_NAME( proxy );
KDAB_SET_OBJECT_NAME( label );
KDAB_SET_OBJECT_NAME( view );
const int row = find_layout_item( *vlay );
vlay->insertWidget( row, &label );
vlay->insertWidget( row+1, &view, 1 );
proxy.setSourceModel( model );
view.setModel( &proxy );
}
void setBase( const QString & base ) {
label.setText( base );
if ( QDirModel * fsm = qobject_cast<QDirModel*>( proxy.sourceModel() ) )
view.setRootIndex( proxy.mapFromSource( fsm->index( base ) ) );
else
qWarning( "%s: expect a QDirModel-derived class as proxy.sourceModel(), got %s",
Q_FUNC_INFO, proxy.sourceModel() ? proxy.sourceModel()->metaObject()->className() : "null pointer" );
}
};
} // anon namespace
class VerifyChecksumsDialog::Private {
friend class ::Kleo::Crypto::Gui::VerifyChecksumsDialog;
VerifyChecksumsDialog * const q;
public:
explicit Private( VerifyChecksumsDialog * qq )
: q( qq ),
bases(),
+ errors(),
model(),
ui( q )
{
qRegisterMetaType<Status>( "Kleo::Crypto::Gui::VerifyChecksumsDialog::Status" );
}
+private:
+ void slotErrorButtonClicked() {
+ KMessageBox::errorList( q, i18n("The following errors and warnings were recorded:"),
+ errors, i18n("Checksum Verification Errors") );
+ }
+
+private:
+ void updateErrors() {
+ const bool active = ui.isProgressBarActive();
+ ui.progressLabel.setVisible( active );
+ ui.progressBar. setVisible( active );
+ ui.errorLabel. setVisible( !active );
+ ui.errorButton. setVisible( !active && !errors.empty() );
+ if ( errors.empty() )
+ ui.errorLabel.setText( i18n("No errors occurred" ) );
+ else
+ ui.errorLabel.setText( i18np( "One error occurred", "%1 errors occurred", errors.size() ) );
+ }
+
private:
QStringList bases;
+ QStringList errors;
ColorizedFileSystemModel model;
struct UI {
std::vector<BaseWidget*> baseWidgets;
QLabel progressLabel;
QProgressBar progressBar;
+ QLabel errorLabel;
+ QPushButton errorButton;
QDialogButtonBox buttonBox;
QVBoxLayout vlay;
- QHBoxLayout hlay;
+ QHBoxLayout hlay[2];
explicit UI( VerifyChecksumsDialog * q )
: baseWidgets(),
progressLabel( i18n("Progress:"), q ),
progressBar( q ),
+ errorLabel( i18n("No errors occurred"), q ),
+ errorButton( i18nc("Show Errors","Show"), q ),
buttonBox( QDialogButtonBox::Close, Qt::Horizontal, q ),
- vlay( q ),
- hlay()
+ vlay( q )
{
KDAB_SET_OBJECT_NAME( progressLabel );
KDAB_SET_OBJECT_NAME( progressBar );
+ KDAB_SET_OBJECT_NAME( errorLabel );
+ KDAB_SET_OBJECT_NAME( errorButton );
KDAB_SET_OBJECT_NAME( buttonBox );
KDAB_SET_OBJECT_NAME( vlay );
- KDAB_SET_OBJECT_NAME( hlay );
+ KDAB_SET_OBJECT_NAME( hlay[0] );
+ KDAB_SET_OBJECT_NAME( hlay[1] );
+
+ errorButton.setAutoDefault( false );
- hlay.addWidget( &progressLabel );
- hlay.addWidget( &progressBar, 1 );
+ hlay[0].addWidget( &progressLabel );
+ hlay[0].addWidget( &progressBar, 1 );
- vlay.addLayout( &hlay );
+ hlay[1].addWidget( &errorLabel, 1 );
+ hlay[1].addWidget( &errorButton );
+
+ vlay.addLayout( &hlay[0] );
+ vlay.addLayout( &hlay[1] );
vlay.addWidget( &buttonBox );
+ errorLabel.hide();
+ errorButton.hide();
+
QPushButton * close = closeButton();
connect( close, SIGNAL(clicked()), q, SIGNAL(canceled()) );
connect( close, SIGNAL(clicked()), q, SLOT(accept()) );
+
+ connect( &errorButton, SIGNAL(clicked()), q, SLOT(slotErrorButtonClicked()) );
}
~UI() {
qDeleteAll( baseWidgets );
}
QPushButton * closeButton() const {
return buttonBox.button( QDialogButtonBox::Close );
}
void setBases( const QStringList & bases, QDirModel * model ) {
// create new BaseWidgets:
for ( unsigned int i = baseWidgets.size(), end = bases.size() ; i < end ; ++i )
baseWidgets.push_back( new BaseWidget( model, vlay.parentWidget(), &vlay ) );
// shed surplus BaseWidgets:
for ( unsigned int i = bases.size(), end = baseWidgets.size() ; i < end ; ++i ) {
delete baseWidgets.back();
baseWidgets.pop_back();
}
assert( static_cast<unsigned>( bases.size() ) == baseWidgets.size() );
// update bases:
for ( unsigned int i = 0 ; i < baseWidgets.size() ; ++i )
baseWidgets[i]->setBase( bases[i] );
}
void setProgress( int cur, int tot ) {
progressBar.setMaximum( tot );
progressBar.setValue( cur );
- progressBar.setVisible( !tot || cur != tot );
- progressLabel.setVisible( !tot || cur != tot );
+ }
+
+ bool isProgressBarActive() const {
+ const int tot = progressBar.maximum();
+ const int cur = progressBar.value();
+ return !tot || cur != tot ;
}
} ui;
};
VerifyChecksumsDialog::VerifyChecksumsDialog( QWidget * parent, Qt::WindowFlags flags )
: QDialog( parent, flags ),
d( new Private( this ) )
{
}
VerifyChecksumsDialog::~VerifyChecksumsDialog() {}
+// slot
void VerifyChecksumsDialog::setBaseDirectories( const QStringList & bases ) {
if ( d->bases == bases )
return;
d->bases = bases;
d->ui.setBases( bases, &d->model );
}
+// slot
+void VerifyChecksumsDialog::setErrors( const QStringList & errors ) {
+ if ( d->errors == errors )
+ return;
+ d->errors = errors;
+ d->updateErrors();
+}
+
// slot
void VerifyChecksumsDialog::setProgress( int cur, int tot ) {
d->ui.setProgress( cur, tot );
+ d->updateErrors();
}
// slot
void VerifyChecksumsDialog::setStatus( const QString & file, Status status ) {
d->model.setStatus( file, status );
}
// slot
void VerifyChecksumsDialog::clearStatusInformation() {
+ d->errors.clear();
+ d->updateErrors();
d->model.clearStatusInformation();
}
#include "verifychecksumsdialog.moc"
#include "moc_verifychecksumsdialog.cpp"
diff --git a/crypto/gui/verifychecksumsdialog.h b/crypto/gui/verifychecksumsdialog.h
index fc2c4f05a..4da34f9c3 100644
--- a/crypto/gui/verifychecksumsdialog.h
+++ b/crypto/gui/verifychecksumsdialog.h
@@ -1,79 +1,81 @@
/* -*- mode: c++; c-basic-offset:4 -*-
crypto/gui/verifychecksumsdialog.h
This file is part of Kleopatra, the KDE keymanager
Copyright (c) 2010 Klarälvdalens Datakonsult AB
Kleopatra is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Kleopatra is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
In addition, as a special exception, the copyright holders give
permission to link the code of this program with any edition of
the Qt library by Trolltech AS, Norway (or with modified versions
of Qt that use the same license as Qt), and distribute linked
combinations including the two. You must obey the GNU General
Public License in all respects for all of the code used other than
Qt. If you modify this file, you may extend this exception to
your version of the file, but you are not obligated to do so. If
you do not wish to do so, delete this exception statement from
your version.
*/
#ifndef __KLEOPATRA_CRYPTO_GUI_VERIFYCHECKSUMSDIALOG_H__
#define __KLEOPATRA_CRYPTO_GUI_VERIFYCHECKSUMSDIALOG_H__
#include <QDialog>
#include <QMetaType>
#include <utils/pimpl_ptr.h>
namespace Kleo {
namespace Crypto {
namespace Gui {
class VerifyChecksumsDialog : public QDialog {
Q_OBJECT
Q_ENUMS( Status )
public:
explicit VerifyChecksumsDialog( QWidget * parent=0, Qt::WindowFlags flags=0 );
~VerifyChecksumsDialog();
enum Status {
Unknown,
OK,
Failed,
Error,
NumStatii
};
public Q_SLOTS:
void setBaseDirectories( const QStringList & bases );
void setProgress( int current, int total );
void setStatus( const QString & file, Kleo::Crypto::Gui::VerifyChecksumsDialog::Status status );
+ void setErrors( const QStringList & errors );
void clearStatusInformation();
Q_SIGNALS:
void canceled();
private:
+ Q_PRIVATE_SLOT( d, void slotErrorButtonClicked() )
class Private;
kdtools::pimpl_ptr<Private> d;
};
}
}
}
Q_DECLARE_METATYPE( Kleo::Crypto::Gui::VerifyChecksumsDialog::Status )
#endif // __KLEOPATRA_CRYPTO_GUI_RESULTITEMWIDGET_H__
diff --git a/crypto/verifychecksumscontroller.cpp b/crypto/verifychecksumscontroller.cpp
index a3294f00e..eaf99a4d4 100644
--- a/crypto/verifychecksumscontroller.cpp
+++ b/crypto/verifychecksumscontroller.cpp
@@ -1,617 +1,619 @@
/* -*- mode: c++; c-basic-offset:4 -*-
crypto/verifychecksumscontroller.cpp
This file is part of Kleopatra, the KDE keymanager
Copyright (c) 2010 Klarälvdalens Datakonsult AB
Kleopatra is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Kleopatra is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
In addition, as a special exception, the copyright holders give
permission to link the code of this program with any edition of
the Qt library by Trolltech AS, Norway (or with modified versions
of Qt that use the same license as Qt), and distribute linked
combinations including the two. You must obey the GNU General
Public License in all respects for all of the code used other than
Qt. If you modify this file, you may extend this exception to
your version of the file, but you are not obligated to do so. If
you do not wish to do so, delete this exception statement from
your version.
*/
#include <config-kleopatra.h>
#include "verifychecksumscontroller.h"
#include <crypto/gui/verifychecksumsdialog.h>
#include <utils/checksumdefinition.h>
#include <utils/input.h>
#include <utils/output.h>
#include <utils/classify.h>
#include <utils/stl_util.h>
#include <utils/kleo_assert.h>
#include <KLocale>
#include <kdebug.h>
#include <KSaveFile>
#include <KGlobal>
#include <KConfigGroup>
#include <QPointer>
#include <QFileInfo>
#include <QThread>
#include <QMutex>
#include <QProgressDialog>
#include <QDir>
#include <QProcess>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <gpg-error.h>
#include <deque>
#include <limits>
#include <set>
using namespace Kleo;
using namespace Kleo::Crypto;
using namespace Kleo::Crypto::Gui;
using namespace boost;
#ifdef Q_OS_UNIX
static const bool HAVE_UNIX = true;
#else
static const bool HAVE_UNIX = false;
#endif
static const QLatin1String CHECKSUM_DEFINITION_ID_ENTRY( "checksum-definition-id" );
static const Qt::CaseSensitivity fs_cs = HAVE_UNIX ? Qt::CaseSensitive : Qt::CaseInsensitive ; // can we use QAbstractFileEngine::caseSensitive()?
#if 0
static QStringList fs_sort( QStringList l ) {
int (*QString_compare)(const QString&,const QString&,Qt::CaseSensitivity) = &QString::compare;
kdtools::sort( l, bind( QString_compare, _1, _2, fs_cs ) < 0 );
return l;
}
static QStringList fs_intersect( QStringList l1, QStringList l2 ) {
int (*QString_compare)(const QString&,const QString&,Qt::CaseSensitivity) = &QString::compare;
fs_sort( l1 );
fs_sort( l2 );
QStringList result;
std::set_intersection( l1.begin(), l1.end(),
l2.begin(), l2.end(),
std::back_inserter( result ),
bind( QString_compare, _1, _2, fs_cs ) < 0 );
return result;
}
#endif
static QList<QRegExp> get_patterns( const std::vector< shared_ptr<ChecksumDefinition> > & checksumDefinitions )
{
QList<QRegExp> result;
Q_FOREACH( const shared_ptr<ChecksumDefinition> & cd, checksumDefinitions )
if ( cd )
Q_FOREACH( const QString & pattern, cd->patterns() )
result.push_back( QRegExp( pattern, fs_cs ) );
return result;
}
namespace {
struct matches_any : std::unary_function<QString,bool> {
const QList<QRegExp> m_regexps;
explicit matches_any( const QList<QRegExp> & regexps ) : m_regexps( regexps ) {}
bool operator()( const QString & s ) const {
return kdtools::any( m_regexps, bind( &QRegExp::exactMatch, _1, s ) );
}
};
struct matches_none_of : std::unary_function<QString,bool> {
const QList<QRegExp> m_regexps;
explicit matches_none_of( const QList<QRegExp> & regexps ) : m_regexps( regexps ) {}
bool operator()( const QString & s ) const {
return kdtools::none_of( m_regexps, bind( &QRegExp::exactMatch, _1, s ) );
}
};
}
class VerifyChecksumsController::Private : public QThread {
Q_OBJECT
friend class ::Kleo::Crypto::VerifyChecksumsController;
VerifyChecksumsController * const q;
public:
explicit Private( VerifyChecksumsController * qq );
~Private();
Q_SIGNALS:
void baseDirectories( const QStringList & );
void progress( int, int, const QString & );
void status( const QString & file, Kleo::Crypto::Gui::VerifyChecksumsDialog::Status );
private:
void slotOperationFinished() {
- if ( dialog )
+ if ( dialog ) {
dialog->setProgress( 100, 100 );
+ dialog->setErrors( errors );
+ }
if ( !errors.empty() )
q->setLastError( gpg_error( GPG_ERR_GENERAL ),
errors.join( "\n" ) );
q->emitDoneOrError();
}
private:
/* reimp */ void run();
private:
QPointer<VerifyChecksumsDialog> dialog;
mutable QMutex mutex;
const std::vector< shared_ptr<ChecksumDefinition> > checksumDefinitions;
QStringList files;
QStringList errors;
volatile bool canceled;
};
VerifyChecksumsController::Private::Private( VerifyChecksumsController * qq )
: q( qq ),
dialog(),
mutex(),
checksumDefinitions( ChecksumDefinition::getChecksumDefinitions() ),
files(),
errors(),
canceled( false )
{
connect( this, SIGNAL(progress(int,int,QString)),
q, SIGNAL(progress(int,int,QString)) );
connect( this, SIGNAL(finished()),
q, SLOT(slotOperationFinished()) );
}
VerifyChecksumsController::Private::~Private() { kDebug(); }
VerifyChecksumsController::VerifyChecksumsController( QObject * p )
: Controller( p ), d( new Private( this ) )
{
}
VerifyChecksumsController::VerifyChecksumsController( const shared_ptr<const ExecutionContext> & ctx, QObject * p )
: Controller( ctx, p ), d( new Private( this ) )
{
}
VerifyChecksumsController::~VerifyChecksumsController() {
kDebug();
}
void VerifyChecksumsController::setFiles( const QStringList & files ) {
kleo_assert( !d->isRunning() );
kleo_assert( !files.empty() );
const QMutexLocker locker( &d->mutex );
d->files = files;
}
void VerifyChecksumsController::start() {
{
const QMutexLocker locker( &d->mutex );
d->dialog = new VerifyChecksumsDialog;
d->dialog->setAttribute( Qt::WA_DeleteOnClose );
d->dialog->setWindowTitle( i18nc("@title:window","Verify Checksum Results") );
connect( d->dialog, SIGNAL(canceled()),
this, SLOT(cancel()) );
connect( d.get(), SIGNAL(baseDirectories(QStringList)),
d->dialog, SLOT(setBaseDirectories(QStringList)) );
connect( d.get(), SIGNAL(progress(int,int,QString)),
d->dialog, SLOT(setProgress(int,int)) );
connect( d.get(), SIGNAL(status(QString,Kleo::Crypto::Gui::VerifyChecksumsDialog::Status)),
d->dialog, SLOT(setStatus(QString,Kleo::Crypto::Gui::VerifyChecksumsDialog::Status)) );
d->canceled = false;
d->errors.clear();
}
d->start();
d->dialog->show();
}
void VerifyChecksumsController::cancel() {
kDebug();
const QMutexLocker locker( &d->mutex );
d->canceled = true;
}
namespace {
struct SumFile {
QDir dir;
QString sumFile;
quint64 totalSize;
shared_ptr<ChecksumDefinition> checksumDefinition;
};
}
static QStringList filter_checksum_files( QStringList l, const QList<QRegExp> & rxs ) {
l.erase( std::remove_if( l.begin(), l.end(),
matches_none_of( rxs ) ),
l.end() );
return l;
}
namespace {
struct File {
QString name;
QByteArray checksum;
bool binary;
};
}
static std::vector<File> parse_sum_file( const QString & fileName ) {
std::vector<File> files;
QFile f( fileName );
if ( f.open( QIODevice::ReadOnly ) ) {
QTextStream s( &f );
QRegExp rx( "([a-f0-9A-F]+) ([ *])([^ *].*)[\n\r]*" );
while ( !s.atEnd() ) {
const QString line = s.readLine();
if ( rx.exactMatch( line ) ) {
assert( !rx.cap(3).endsWith( QLatin1Char('\n') ) );
assert( !rx.cap(3).endsWith( QLatin1Char('\r') ) );
const File file = {
rx.cap( 3 ),
rx.cap( 1 ).toLatin1(),
rx.cap( 2 ) == QLatin1String("*"),
};
files.push_back( file );
}
}
}
return files;
}
static quint64 aggregate_size( const QDir & dir, const QStringList & files ) {
quint64 n = 0;
Q_FOREACH( const QString & file, files )
n += QFileInfo( dir.absoluteFilePath( file ) ).size();
return n;
}
static shared_ptr<ChecksumDefinition> filename2definition( const QString & fileName,
const std::vector< shared_ptr<ChecksumDefinition> > & checksumDefinitions )
{
Q_FOREACH( const shared_ptr<ChecksumDefinition> & cd, checksumDefinitions )
if ( cd )
Q_FOREACH( const QString & pattern, cd->patterns() )
if ( QRegExp( pattern, fs_cs ).exactMatch( fileName ) )
return cd;
return shared_ptr<ChecksumDefinition>();
}
namespace {
struct less_dir : std::binary_function<QDir,QDir,bool> {
bool operator()( const QDir & lhs, const QDir & rhs ) const {
return QString::compare( lhs.absolutePath(), rhs.absolutePath(), fs_cs ) < 0 ;
}
};
struct less_file : std::binary_function<QString,QString,bool> {
bool operator()( const QString & lhs, const QString & rhs ) const {
return QString::compare( lhs, rhs, fs_cs ) < 0 ;
}
};
struct sumfile_contains_file : std::unary_function<QString,bool> {
const QDir dir;
const QString fileName;
sumfile_contains_file( const QDir & dir_, const QString & fileName_ )
: dir( dir_ ), fileName( fileName_ ) {}
bool operator()( const QString & sumFile ) const {
const std::vector<File> files = parse_sum_file( dir.absoluteFilePath( sumFile ) );
qDebug( "find_sums_by_input_files: found %zd files listed in %s", files.size(), qPrintable( dir.absoluteFilePath( sumFile ) ) );
Q_FOREACH( const File & file, files ) {
qDebug( "find_sums_by_input_files: %s == %s ? %s", qPrintable( file.name ), qPrintable( fileName ),
QString::compare( file.name, fileName, fs_cs ) == 0 ? "yes" : "no" );
if ( QString::compare( file.name, fileName, fs_cs ) == 0 )
return true;
}
return false;
}
};
}
// IF is_dir(file)
// add all sumfiles \in dir(file)
// inputs.prepend( all dirs \in dir(file) )
// ELSE IF is_sum_file(file)
// add
// ELSE IF \exists sumfile in dir(file) \where sumfile \contains file
// add sumfile
// ELSE
// error: no checksum found for "file"
static QStringList find_base_directiories( const QStringList & files ) {
// Step 1: find base dirs:
std::set<QDir,less_dir> dirs;
Q_FOREACH( const QString & file, files ) {
const QFileInfo fi( file );
const QDir dir = fi.isDir() ? QDir( file ) : fi.dir() ;
dirs.insert( dir );
}
// Step 1a: collapse direct child directories
bool changed;
do {
changed = false;
std::set<QDir,less_dir>::iterator it = dirs.begin();
while ( it != dirs.end() ) {
QDir dir = *it;
if ( dir.cdUp() && dirs.count( dir ) ) {
dirs.erase( it++ );
changed = true;
} else {
++it;
}
}
} while ( changed );
return kdtools::transform<QStringList>( dirs, mem_fn( &QDir::absolutePath ) );
}
static std::vector<SumFile> find_sums_by_input_files( const QStringList & files, QStringList & errors,
const function<void(int)> & progress,
const std::vector< shared_ptr<ChecksumDefinition> > & checksumDefinitions )
{
const QList<QRegExp> patterns = get_patterns( checksumDefinitions );
const matches_any is_sum_file( patterns );
std::map<QDir,std::set<QString,less_file>,less_dir> dirs2sums;
// Step 1: find the sumfiles we need to check:
std::deque<QString> inputs( files.begin(), files.end() );
int i = 0;
while ( !inputs.empty() ) {
const QString file = inputs.front();
qDebug( "find_sums_by_input_files: considering %s", qPrintable( file ) );
inputs.pop_front();
const QFileInfo fi( file );
const QString fileName = fi.fileName();
if ( fi.isDir() ) {
qDebug( "find_sums_by_input_files: it's a directory" );
QDir dir( file );
const QStringList sumfiles = filter_checksum_files( dir.entryList( QDir::Files ), patterns );
qDebug( "find_sums_by_input_files: found %d sum files: %s", sumfiles.size(), qPrintable( sumfiles.join(", ") ) );
dirs2sums[ dir ].insert( sumfiles.begin(), sumfiles.end() );
const QStringList dirs = dir.entryList( QDir::Dirs|QDir::NoDotAndDotDot );
qDebug( "find_sums_by_input_files: found %d subdirs, prepending", dirs.size() );
kdtools::transform( dirs,
std::inserter( inputs, inputs.begin() ),
bind( &QDir::absoluteFilePath, cref(dir), _1 ) );
} else if ( is_sum_file( fileName ) ) {
qDebug( "find_sums_by_input_files: it's a sum file" );
dirs2sums[fi.dir()].insert( fileName );
} else {
qDebug( "find_sums_by_input_files: it's something else; checking whether we'll find a sumfile for it..." );
const QDir dir = fi.dir();
const QStringList sumfiles = filter_checksum_files( dir.entryList( QDir::Files ), patterns );
qDebug( "find_sums_by_input_files: found %d potential sumfiles: %s", sumfiles.size(), qPrintable( sumfiles.join(", ") ) );
const QStringList::const_iterator it = kdtools::find_if( sumfiles, sumfile_contains_file( dir, fileName ) );
if ( it == sumfiles.end() )
errors.push_back( i18n( "Cannot find checksums file for file %1", file ) );
else
dirs2sums[dir].insert( *it );
}
if ( !progress.empty() )
progress( ++i );
}
// Step 2: convert into vector<SumFile>:
std::vector<SumFile> sumfiles;
sumfiles.reserve( dirs2sums.size() );
for ( std::map<QDir,std::set<QString,less_file>,less_dir>::const_iterator it = dirs2sums.begin(), end = dirs2sums.end() ; it != end ; ++it ) {
if ( it->second.empty() )
continue;
const QDir & dir = it->first;
Q_FOREACH( const QString & sumFileName, it->second ) {
const std::vector<File> summedfiles = parse_sum_file( dir.absoluteFilePath( sumFileName ) );
const SumFile sumFile = {
it->first,
sumFileName,
aggregate_size( it->first, kdtools::transform<QStringList>( summedfiles, mem_fn( &File::name ) ) ),
filename2definition( sumFileName, checksumDefinitions ),
};
sumfiles.push_back( sumFile );
}
if ( !progress.empty() )
progress( ++i );
}
return sumfiles;
}
static QStringList c_lang_environment() {
QStringList env = QProcess::systemEnvironment();
env.erase( std::remove_if( env.begin(), env.end(),
bind( &QRegExp::exactMatch,
QRegExp( "^LANG=.*", fs_cs ), _1 ) ),
env.end() );
env.push_back( "LANG=C" );
return env;
}
static const struct {
const char * string;
VerifyChecksumsDialog::Status status;
} statusStrings[] = {
{ "OK", VerifyChecksumsDialog::OK },
{ "FAILED", VerifyChecksumsDialog::Failed },
};
static const size_t numStatusStrings = sizeof statusStrings / sizeof *statusStrings ;
static VerifyChecksumsDialog::Status string2status( const QByteArray & str ) {
for ( unsigned int i = 0 ; i < numStatusStrings ; ++i )
if ( str == statusStrings[i].string )
return statusStrings[i].status;
return VerifyChecksumsDialog::Unknown;
}
static QString process( const SumFile & sumFile, bool * fatal, const QStringList & env,
const function<void(const QString&,VerifyChecksumsDialog::Status)> & status )
{
QProcess p;
p.setEnvironment( env );
p.setWorkingDirectory( sumFile.dir.absolutePath() );
p.setReadChannel( QProcess::StandardOutput );
const QString absFilePath = sumFile.dir.absoluteFilePath( sumFile.sumFile );
const QString program = sumFile.checksumDefinition->verifyCommand();
const QStringList arguments = sumFile.checksumDefinition->verifyCommandArguments( QStringList( absFilePath ) );
qDebug( "[%p] Starting %s %s", &p, qPrintable( program ), qPrintable( arguments.join(" ") ) );
p.start( program, arguments );
while ( p.state() != QProcess::NotRunning ) {
p.waitForReadyRead();
while ( p.canReadLine() ) {
const QByteArray line = p.readLine();
const int colonIdx = line.lastIndexOf( ':' );
if ( colonIdx < 0 ) {
qDebug( "%s: failed to parse line '%s'", Q_FUNC_INFO, line.data() );
continue;
}
const QString file = QFile::decodeName( line.left( colonIdx ) );
const VerifyChecksumsDialog::Status result = string2status( line.mid( colonIdx+1 ).trimmed() );
status( sumFile.dir.absoluteFilePath( file ), result );
}
}
qDebug( "[%p] Exit code %d.", &p, p.exitCode() );
if ( p.exitStatus() != QProcess::NormalExit || p.exitCode() != 0 ) {
if ( fatal && p.error() == QProcess::FailedToStart )
*fatal = true;
if ( p.error() == QProcess::UnknownError )
return i18n( "Error while running %1: %2", program,
QString::fromLocal8Bit( p.readAllStandardError().trimmed().constData() ) );
else
return i18n( "Failed to execute %1: %2", program, p.errorString() );
}
return QString();
}
namespace {
static QDebug operator<<( QDebug s, const SumFile & sum ) {
return s << "SumFile(" << sum.dir << "->" << sum.sumFile << "<-(" << sum.totalSize << ')' << ")\n";
}
}
void VerifyChecksumsController::Private::run() {
QMutexLocker locker( &mutex );
const QStringList files = this->files;
const std::vector< shared_ptr<ChecksumDefinition> > checksumDefinitions = this->checksumDefinitions;
locker.unlock();
QStringList errors;
//
// Step 0: find base directories:
//
emit baseDirectories( find_base_directiories( files ) );
//
// Step 1: build a list of work to do (no progress):
//
const QString scanning = i18n("Scanning directories...");
emit progress( 0, 0, scanning );
const function<void(int)> progressCb = bind( &Private::progress, this, _1, 0, scanning );
const function<void(const QString&,VerifyChecksumsDialog::Status)>
statusCb = bind( &Private::status, this, _1, _2 );
const std::vector<SumFile> sumfiles = find_sums_by_input_files( files, errors, progressCb, checksumDefinitions );
Q_FOREACH( const SumFile & sumfile, sumfiles )
qDebug() << sumfile;
if ( !canceled ) {
emit progress( 0, 0, i18n("Calculating total size...") );
const quint64 total
= kdtools::accumulate_transform( sumfiles, mem_fn( &SumFile::totalSize ), Q_UINT64_C(0) );
if ( !canceled ) {
//
// Step 2: perform work (with progress reporting):
//
const QStringList env = c_lang_environment();
// re-scale 'total' to fit into ints (wish QProgressDialog would use quint64...)
const quint64 factor = total / std::numeric_limits<int>::max() + 1 ;
quint64 done = 0;
Q_FOREACH( const SumFile & sumFile, sumfiles ) {
emit progress( done/factor, total/factor,
i18n("Verifying checksums (%2) in %1", sumFile.checksumDefinition->label(), sumFile.dir.path() ) );
bool fatal = false;
const QString error = process( sumFile, &fatal, env, statusCb );
if ( !error.isEmpty() )
errors.push_back( error );
done += sumFile.totalSize;
if ( fatal || canceled )
break;
}
emit progress( done/factor, total/factor, i18n("Done.") );
}
}
locker.relock();
this->errors = errors;
// mutex unlocked by QMutexLocker
}
#include "moc_verifychecksumscontroller.cpp"
#include "verifychecksumscontroller.moc"
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Mon, May 12, 6:27 PM (1 d, 16 h)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
89/7b/14907d518a2f52ae614bf1039053
Attached To
rKLEOPATRA Kleopatra
Event Timeline
Log In to Comment