Better wording
[kdepim.git] / kleopatra / commands / signencryptfilescommand.cpp
blob728379940d333cc05c0b05d2bf76a2544449397b
1 /* -*- mode: c++; c-basic-offset:4 -*-
2 commands/signencryptfilescommand.cpp
4 This file is part of Kleopatra, the KDE keymanager
5 Copyright (c) 2008 Klarälvdalens Datakonsult AB
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.
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.
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
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.
33 #include <config-kleopatra.h>
35 #include "signencryptfilescommand.h"
37 #include "command_p.h"
39 #include <crypto/signencryptfilescontroller.h>
41 #include <utils/filedialog.h>
43 #include <kleo/stl_util.h>
45 #include <KLocale>
46 #include <KMessageBox>
47 #include <kdebug.h>
49 #include <QStringList>
51 #include <exception>
53 using namespace Kleo;
54 using namespace Kleo::Commands;
55 using namespace Kleo::Crypto;
56 using namespace boost;
58 class SignEncryptFilesCommand::Private : public Command::Private {
59 friend class ::Kleo::Commands::SignEncryptFilesCommand;
60 SignEncryptFilesCommand * q_func() const { return static_cast<SignEncryptFilesCommand*>( q ); }
61 public:
62 explicit Private( SignEncryptFilesCommand * qq, KeyListController * c );
63 ~Private();
65 QStringList selectFiles() const;
67 void init();
69 private:
70 void slotControllerDone() {
71 finished();
73 void slotControllerError( int, const QString & ) {
74 finished();
77 private:
78 QStringList files;
79 shared_ptr<const ExecutionContext> shared_qq;
80 SignEncryptFilesController controller;
84 SignEncryptFilesCommand::Private * SignEncryptFilesCommand::d_func() { return static_cast<Private*>( d.get() ); }
85 const SignEncryptFilesCommand::Private * SignEncryptFilesCommand::d_func() const { return static_cast<const Private*>( d.get() ); }
87 #define d d_func()
88 #define q q_func()
90 SignEncryptFilesCommand::Private::Private( SignEncryptFilesCommand * qq, KeyListController * c )
91 : Command::Private( qq, c ),
92 files(),
93 shared_qq( qq, kdtools::nodelete() ),
94 controller()
96 controller.setOperationMode( SignEncryptFilesController::SignAllowed | SignEncryptFilesController::EncryptAllowed | SignEncryptFilesController::ArchiveAllowed );
99 SignEncryptFilesCommand::Private::~Private() { kDebug(); }
101 SignEncryptFilesCommand::SignEncryptFilesCommand( KeyListController * c )
102 : Command( new Private( this, c ) )
104 d->init();
107 SignEncryptFilesCommand::SignEncryptFilesCommand( QAbstractItemView * v, KeyListController * c )
108 : Command( v, new Private( this, c ) )
110 d->init();
113 SignEncryptFilesCommand::SignEncryptFilesCommand( const QStringList & files, KeyListController * c )
114 : Command( new Private( this, c ) )
116 d->init();
117 d->files = files;
120 SignEncryptFilesCommand::SignEncryptFilesCommand( const QStringList & files, QAbstractItemView * v, KeyListController * c )
121 : Command( v, new Private( this, c ) )
123 d->init();
124 d->files = files;
127 void SignEncryptFilesCommand::Private::init() {
128 controller.setExecutionContext( shared_qq );
129 connect( &controller, SIGNAL(done()), q, SLOT(slotControllerDone()) );
130 connect( &controller, SIGNAL(error(int,QString)), q, SLOT(slotControllerError(int,QString)) );
133 SignEncryptFilesCommand::~SignEncryptFilesCommand() { kDebug(); }
135 void SignEncryptFilesCommand::setFiles( const QStringList & files ) {
136 d->files = files;
139 void SignEncryptFilesCommand::setSigningPolicy( Policy policy ) {
140 unsigned int mode = d->controller.operationMode();
141 mode &= ~SignEncryptFilesController::SignMask;
142 switch ( policy ) {
143 case NoPolicy:
144 case Allow:
145 mode |= SignEncryptFilesController::SignAllowed ;
146 break;
147 case Deny:
148 mode |= SignEncryptFilesController::SignDisallowed ;
149 break;
150 case Force:
151 mode |= SignEncryptFilesController::SignForced ;
152 break;
154 try {
155 d->controller.setOperationMode( mode );
156 } catch ( ... ) {}
159 Policy SignEncryptFilesCommand::signingPolicy() const {
160 const unsigned int mode = d->controller.operationMode();
161 switch ( mode & SignEncryptFilesController::SignMask ) {
162 default:
163 assert( !"This should not happen!" );
164 return NoPolicy;
165 case SignEncryptFilesController::SignAllowed:
166 return Allow;
167 case SignEncryptFilesController::SignForced:
168 return Force;
169 case SignEncryptFilesController::SignDisallowed:
170 return Deny;
174 void SignEncryptFilesCommand::setEncryptionPolicy( Policy policy ) {
175 unsigned int mode = d->controller.operationMode();
176 mode &= ~SignEncryptFilesController::EncryptMask;
177 switch ( policy ) {
178 case NoPolicy:
179 case Allow:
180 mode |= SignEncryptFilesController::EncryptAllowed ;
181 break;
182 case Deny:
183 mode |= SignEncryptFilesController::EncryptDisallowed ;
184 break;
185 case Force:
186 mode |= SignEncryptFilesController::EncryptForced ;
187 break;
189 try {
190 d->controller.setOperationMode( mode );
191 } catch ( ... ) {}
194 Policy SignEncryptFilesCommand::encryptionPolicy() const {
195 const unsigned int mode = d->controller.operationMode();
196 switch ( mode & SignEncryptFilesController::EncryptMask ) {
197 default:
198 assert( !"This should not happen!" );
199 return NoPolicy;
200 case SignEncryptFilesController::EncryptAllowed:
201 return Allow;
202 case SignEncryptFilesController::EncryptForced:
203 return Force;
204 case SignEncryptFilesController::EncryptDisallowed:
205 return Deny;
209 void SignEncryptFilesCommand::setProtocol( GpgME::Protocol proto ) {
210 d->controller.setProtocol( proto );
213 GpgME::Protocol SignEncryptFilesCommand::protocol() const {
214 return d->controller.protocol();
217 void SignEncryptFilesCommand::doStart() {
219 try {
221 if ( d->files.empty() )
222 d->files = d->selectFiles();
223 if ( d->files.empty() ) {
224 d->finished();
225 return;
228 d->controller.setFiles( d->files );
229 d->controller.start();
231 } catch ( const std::exception & e ) {
232 d->information( i18n("An error occurred: %1",
233 QString::fromLocal8Bit( e.what() ) ),
234 i18n("Sign/Encrypt Files Error") );
235 d->finished();
239 void SignEncryptFilesCommand::doCancel() {
240 kDebug();
241 d->controller.cancel();
244 QStringList SignEncryptFilesCommand::Private::selectFiles() const {
245 return FileDialog::getOpenFileNames( parentWidgetOrView(), i18n( "Select One or More Files to Sign and/or Encrypt" ), "enc" );
248 #undef d
249 #undef q
251 #include "moc_signencryptfilescommand.cpp"