• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kleopatra

  • sources
  • kde-4.12
  • kdepim
  • kleopatra
  • commands
importcertificatefromfilecommand.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  importcertificatefromfilecommand.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2007 Klarälvdalens Datakonsult AB
6 
7  Kleopatra is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  Kleopatra is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  In addition, as a special exception, the copyright holders give
22  permission to link the code of this program with any edition of
23  the Qt library by Trolltech AS, Norway (or with modified versions
24  of Qt that use the same license as Qt), and distribute linked
25  combinations including the two. You must obey the GNU General
26  Public License in all respects for all of the code used other than
27  Qt. If you modify this file, you may extend this exception to
28  your version of the file, but you are not obligated to do so. If
29  you do not wish to do so, delete this exception statement from
30  your version.
31 */
32 
33 #include <config-kleopatra.h>
34 
35 #include "importcertificatefromfilecommand.h"
36 #include "importcertificatescommand_p.h"
37 
38 #include "utils/classify.h"
39 #include "utils/filedialog.h"
40 
41 #include <kleo/cryptobackendfactory.h>
42 #include <kleo/importjob.h>
43 
44 #include <gpgme++/global.h>
45 #include <gpgme++/importresult.h>
46 
47 #include <KLocale>
48 #include <KConfigGroup>
49 
50 #include <QByteArray>
51 #include <QFile>
52 #include <QFileDialog>
53 #include <QPointer>
54 #include <QString>
55 #include <QWidget>
56 #include <QFileInfo>
57 
58 #include <memory>
59 #include <cassert>
60 
61 using namespace GpgME;
62 using namespace Kleo;
63 
64 class ImportCertificateFromFileCommand::Private : public ImportCertificatesCommand::Private {
65  friend class ::ImportCertificateFromFileCommand;
66  ImportCertificateFromFileCommand * q_func() const { return static_cast<ImportCertificateFromFileCommand*>( q ); }
67 public:
68  explicit Private( ImportCertificateFromFileCommand * qq, KeyListController * c );
69  ~Private();
70 
71  bool ensureHaveFile();
72 
73 private:
74  QStringList files;
75 };
76 
77 ImportCertificateFromFileCommand::Private * ImportCertificateFromFileCommand::d_func() { return static_cast<Private*>(d.get()); }
78 const ImportCertificateFromFileCommand::Private * ImportCertificateFromFileCommand::d_func() const { return static_cast<const Private*>(d.get()); }
79 
80 ImportCertificateFromFileCommand::Private::Private( ImportCertificateFromFileCommand * qq, KeyListController * c )
81  : ImportCertificatesCommand::Private( qq, c ),
82  files()
83 {
84 
85 }
86 
87 ImportCertificateFromFileCommand::Private::~Private() {}
88 
89 
90 #define d d_func()
91 #define q q_func()
92 
93 
94 ImportCertificateFromFileCommand::ImportCertificateFromFileCommand( KeyListController * p )
95  : ImportCertificatesCommand( new Private( this, p ) )
96 {
97 
98 }
99 
100 ImportCertificateFromFileCommand::ImportCertificateFromFileCommand( QAbstractItemView * v, KeyListController * p )
101  : ImportCertificatesCommand( v, new Private( this, p ) )
102 {
103 
104 }
105 
106 ImportCertificateFromFileCommand::ImportCertificateFromFileCommand( const QStringList & files, KeyListController * p )
107  : ImportCertificatesCommand( new Private( this, p ) )
108 {
109  d->files = files;
110 }
111 
112 ImportCertificateFromFileCommand::ImportCertificateFromFileCommand( const QStringList & files, QAbstractItemView * v, KeyListController * p )
113  : ImportCertificatesCommand( v, new Private( this, p ) )
114 {
115  d->files = files;
116 }
117 
118 ImportCertificateFromFileCommand::~ImportCertificateFromFileCommand() {}
119 
120 void ImportCertificateFromFileCommand::setFiles( const QStringList & files )
121 {
122  d->files = files;
123 }
124 
125 void ImportCertificateFromFileCommand::doStart()
126 {
127  if ( !d->ensureHaveFile() ) {
128  emit canceled();
129  d->finished();
130  return;
131  }
132 
133  //TODO: use KIO here
134  d->setWaitForMoreJobs( true );
135  Q_FOREACH( const QString & fn, d->files ) {
136  QFile in( fn );
137  if ( !in.open( QIODevice::ReadOnly ) ) {
138  d->error( i18n( "Could not open file %1 for reading: %2", in.fileName(), in.errorString() ), i18n( "Certificate Import Failed" ) );
139  d->importResult( ImportResult(), fn );
140  continue;
141  }
142  const GpgME::Protocol protocol = findProtocol( fn );
143  if ( protocol == GpgME::UnknownProtocol ) { //TODO: might use exceptions here
144  d->error( i18n( "Could not determine certificate type of %1.", in.fileName() ), i18n( "Certificate Import Failed" ) );
145  d->importResult( ImportResult(), fn );
146  continue;
147  }
148  d->startImport( protocol, in.readAll(), fn );
149  }
150  d->setWaitForMoreJobs( false );
151 }
152 
153 static QStringList get_file_name( QWidget * parent ) {
154  const QString certificateFilter = i18n("Certificates") + QLatin1String(" (*.asc *.cer *.cert *.crt *.der *.pem *.gpg *.p7c *.p12 *.pfx *.pgp)");
155  const QString anyFilesFilter = i18n("Any files") + QLatin1String(" (*)");
156  QString previousDir;
157  if ( const KSharedConfig::Ptr config = KGlobal::config() ) {
158  const KConfigGroup group( config, "Import Certificate" );
159  previousDir = group.readPathEntry( "last-open-file-directory", QDir::homePath() );
160  }
161  const QStringList files = Kleo::FileDialog::getOpenFileNames( parent, i18n( "Select Certificate File" ), previousDir, certificateFilter + QLatin1String(";;") + anyFilesFilter );
162  if ( !files.empty() )
163  if ( const KSharedConfig::Ptr config = KGlobal::config() ) {
164  KConfigGroup group( config, "Import Certificate" );
165  group.writePathEntry( "last-open-file-directory", QFileInfo( files.front() ).path() );
166  }
167  return files;
168 }
169 
170 bool ImportCertificateFromFileCommand::Private::ensureHaveFile()
171 {
172  if ( files.empty() )
173  files = get_file_name( parentWidgetOrView() );
174  return !files.empty();
175 }
176 
177 #undef d
178 #undef q
179 
180 #include "moc_importcertificatefromfilecommand.cpp"
181 
182 
Kleo::ImportCertificateFromFileCommand::~ImportCertificateFromFileCommand
~ImportCertificateFromFileCommand()
Definition: importcertificatefromfilecommand.cpp:118
Kleo::ImportCertificatesCommand::Private
Definition: importcertificatescommand_p.h:55
Kleo::KeyListController
Definition: keylistcontroller.h:55
QWidget
classify.h
Kleo::ImportCertificateFromFileCommand
Definition: importcertificatefromfilecommand.h:42
Kleo::ImportCertificateFromFileCommand::ImportCertificateFromFileCommand
ImportCertificateFromFileCommand(KeyListController *parent)
Definition: importcertificatefromfilecommand.cpp:94
importcertificatescommand_p.h
Kleo::ImportCertificatesCommand
Definition: importcertificatescommand.h:42
d
#define d
Definition: importcertificatefromfilecommand.cpp:90
Kleo::Command::d
kdtools::pimpl_ptr< Private > d
Definition: commands/command.h:129
Kleo::ImportCertificatesCommand::Private::Private
Private(ImportCertificatesCommand *qq, KeyListController *c)
Definition: importcertificatescommand.cpp:163
q
#define q
Definition: importcertificatefromfilecommand.cpp:91
Kleo::FileDialog::getOpenFileNames
QStringList getOpenFileNames(QWidget *parent=0, const QString &caption=QString(), const QString &dirID=QString(), const QString &filter=QString())
Definition: filedialog.cpp:115
get_file_name
static QStringList get_file_name(QWidget *parent)
Definition: importcertificatefromfilecommand.cpp:153
Kleo::ImportCertificateFromFileCommand::files
QStringList files() const
Kleo::ImportCertificateFromFileCommand::setFiles
void setFiles(const QStringList &files)
Definition: importcertificatefromfilecommand.cpp:120
Kleo::Command::canceled
void canceled()
importcertificatefromfilecommand.h
Kleo::findProtocol
ProtocolMask FormatMask TypeMask TypeMask TypeMask GpgME::Protocol findProtocol(const unsigned int classifcation)
Definition: classify.h:127
filedialog.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:56:41 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kleopatra

Skip menu "kleopatra"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal