<commit_before>/* -*- mode: c++; c-basic-offset:4 -*-
    importcertificatecommand.cpp

    This file is part of Kleopatra, the KDE keymanager
    Copyright (c) 2007 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 "importcertificatecommand.h"
#include "command_p.h"

#include "utils/classify.h"

#include <kleo/cryptobackendfactory.h>
#include <kleo/importjob.h>

#include <gpgme++/global.h>
#include <gpgme++/importresult.h>

#include <KLocale>
#include <KMessageBox>
#include <KJob>

#include <QByteArray>
#include <QFile>
#include <QFileDialog>
#include <QPointer>
#include <QString>
#include <QWidget>

#include <memory>
#include <cassert>

using namespace GpgME;
using namespace Kleo;

class ImportCertificateCommand::Private : public Command::Private {
    friend class ::ImportCertificateCommand;
    ImportCertificateCommand * q_func() const { return static_cast<ImportCertificateCommand*>( q ); }
public:
    explicit Private( ImportCertificateCommand * qq, KeyListController * c );
    ~Private();

    bool ensureHaveFile();
    void startImport( const QByteArray& data );
    void importResult( const GpgME::ImportResult& );
    GpgME::Protocol checkProtocol( const QByteArray& data, const QString& filename ) const;
    void showError( const GpgME::Error& error );
    void showDetails( const ImportResult& result );

private:
    QPointer<ImportJob> importJob;
    QString filename;
};

ImportCertificateCommand::Private * ImportCertificateCommand::d_func() { return static_cast<Private*>(d.get()); }
const ImportCertificateCommand::Private * ImportCertificateCommand::d_func() const { return static_cast<const Private*>(d.get()); }

ImportCertificateCommand::Private::Private( ImportCertificateCommand * qq, KeyListController * c )
    : Command::Private( qq, c ), importJob( 0 )
{
    
}

ImportCertificateCommand::Private::~Private() {}



ImportCertificateCommand::ImportCertificateCommand( KeyListController * p )
    : Command( new Private( this, p ) )
{
    
}

ImportCertificateCommand::ImportCertificateCommand( QAbstractItemView * v, KeyListController * p )
    : Command( v, new Private( this, p ) )
{
    
}

ImportCertificateCommand::~ImportCertificateCommand() {}

#define d d_func()
#define q q_func()

QString ImportCertificateCommand::fileName() const
{
    return d->filename;
}

void ImportCertificateCommand::setFileName( const QString& fileName )
{
    d->filename = fileName;
}

void ImportCertificateCommand::doStart()
{
    if ( !d->ensureHaveFile() ) {
        emit canceled();
        d->finished();
        return;
    }
    //TODO: use KIO here
    QFile in( d->filename );
    if ( !in.open( QIODevice::ReadOnly ) ) {
        KMessageBox::error( d->view(), i18n( "Could not open file %1 for reading", d->filename ), i18n( "Certificate Import Failed" ) );
        d->finished();
        return;
    }
    d->startImport( in.readAll() ); 
    emit info( i18n( "Importing certificate..." ) );
}

bool ImportCertificateCommand::Private::ensureHaveFile()
{
   if ( filename.isNull() )
       filename = QFileDialog::getOpenFileName( view(), i18n( "Select Certificate File" ), QString(), i18n( "Certificates (*.asc *.pem *.der *.p7c *.p12)" )  );
   return !filename.isNull();<commit_msg>Allow to look for non-certificates, too. Remember last selected path.<commit_after>/* -*- mode: c++; c-basic-offset:4 -*-
    importcertificatecommand.cpp

    This file is part of Kleopatra, the KDE keymanager
    Copyright (c) 2007 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 "importcertificatecommand.h"
#include "command_p.h"

#include "utils/classify.h"

#include <kleo/cryptobackendfactory.h>
#include <kleo/importjob.h>

#include <gpgme++/global.h>
#include <gpgme++/importresult.h>

#include <KLocale>
#include <KMessageBox>
#include <KConfigGroup>

#include <QByteArray>
#include <QFile>
#include <QFileDialog>
#include <QPointer>
#include <QString>
#include <QWidget>
#include <QFileInfo>

#include <memory>
#include <cassert>

using namespace GpgME;
using namespace Kleo;

class ImportCertificateCommand::Private : public Command::Private {
    friend class ::ImportCertificateCommand;
    ImportCertificateCommand * q_func() const { return static_cast<ImportCertificateCommand*>( q ); }
public:
    explicit Private( ImportCertificateCommand * qq, KeyListController * c );
    ~Private();

    bool ensureHaveFile();
    void startImport( const QByteArray& data );
    void importResult( const GpgME::ImportResult& );
    GpgME::Protocol checkProtocol( const QByteArray& data, const QString& filename ) const;
    void showError( const GpgME::Error& error );
    void showDetails( const ImportResult& result );

private:
    QPointer<ImportJob> importJob;
    QString filename;
};

ImportCertificateCommand::Private * ImportCertificateCommand::d_func() { return static_cast<Private*>(d.get()); }
const ImportCertificateCommand::Private * ImportCertificateCommand::d_func() const { return static_cast<const Private*>(d.get()); }

ImportCertificateCommand::Private::Private( ImportCertificateCommand * qq, KeyListController * c )
    : Command::Private( qq, c ), importJob( 0 )
{
    
}

ImportCertificateCommand::Private::~Private() {}



ImportCertificateCommand::ImportCertificateCommand( KeyListController * p )
    : Command( new Private( this, p ) )
{
    
}

ImportCertificateCommand::ImportCertificateCommand( QAbstractItemView * v, KeyListController * p )
    : Command( v, new Private( this, p ) )
{
    
}

ImportCertificateCommand::~ImportCertificateCommand() {}

#define d d_func()
#define q q_func()

QString ImportCertificateCommand::fileName() const
{
    return d->filename;
}

void ImportCertificateCommand::setFileName( const QString& fileName )
{
    d->filename = fileName;
}

void ImportCertificateCommand::doStart()
{
    if ( !d->ensureHaveFile() ) {
        emit canceled();
        d->finished();
        return;
    }
    //TODO: use KIO here
    QFile in( d->filename );
    if ( !in.open( QIODevice::ReadOnly ) ) {
        KMessageBox::error( d->view(), i18n( "Could not open file %1 for reading", d->filename ), i18n( "Certificate Import Failed" ) );
        d->finished();
        return;
    }
    d->startImport( in.readAll() ); 
    emit info( i18n( "Importing certificate..." ) );
}

static QString get_file_name( QWidget * parent ) {
    const QString certificateFilter = i18n("Certificates (*.asc *.pem *.der *.p7c *.p12)");
    const QString anyFilesFilter = i18n("Any files (*)" );
    QString previousDir;
    if ( const KSharedConfig::Ptr config = KGlobal::config() ) {
        const KConfigGroup group( config, "Import Certificate" );
        previousDir = group.readPathEntry( "last-open-file-directory", QString() );
    }
    const QString fn = QFileDialog::getOpenFileName( parent, i18n( "Select Certificate File" ), previousDir, certificateFilter + ";;" + anyFilesFilter );
    if ( fn.isEmpty() )
        return QString();
    if ( const KSharedConfig::Ptr config = KGlobal::config() ) {
        KConfigGroup group( config, "Import Certificate" );
        group.writePathEntry( "last-open-file-directory", QFileInfo( fn ).path() );
    }
    return fn;
}

bool ImportCertificateCommand::Private::ensureHaveFile()
{
    if ( filename.isEmpty() )
        filename = get_file_name( view() );
    return !filename.isEmpty();
{'fraction_non_alphanumeric': 0.10344827586206896, 'fraction_numerical': 0.006558680314021664, 'mean_word_length': 4.001988071570577, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2359867', 'n_tokens_mistral': 2876, 'n_tokens_neox': 2823, 'n_words': 991}