kleopatra
adduseridcommand.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <config-kleopatra.h>
00034
00035 #include "adduseridcommand.h"
00036
00037 #include "command_p.h"
00038
00039 #include <dialogs/adduseriddialog.h>
00040
00041 #include <utils/formatting.h>
00042
00043 #include <kleo/cryptobackendfactory.h>
00044 #include <kleo/cryptobackend.h>
00045 #include <kleo/adduseridjob.h>
00046
00047 #include <gpgme++/key.h>
00048
00049 #include <KLocale>
00050 #include <KMessageBox>
00051 #include <kdebug.h>
00052
00053 #include <cassert>
00054
00055 using namespace Kleo;
00056 using namespace Kleo::Commands;
00057 using namespace Kleo::Dialogs;
00058 using namespace GpgME;
00059
00060 class AddUserIDCommand::Private : public Command::Private {
00061 friend class ::Kleo::Commands::AddUserIDCommand;
00062 AddUserIDCommand * q_func() const { return static_cast<AddUserIDCommand*>( q ); }
00063 public:
00064 explicit Private( AddUserIDCommand * qq, KeyListController * c );
00065 ~Private();
00066
00067 void init();
00068
00069 private:
00070 void slotDialogAccepted();
00071 void slotDialogRejected();
00072 void slotResult( const Error & err );
00073
00074 private:
00075 void ensureDialogCreated();
00076 void createJob();
00077 void showErrorDialog( const Error & error );
00078 void showSuccessDialog();
00079
00080 private:
00081 GpgME::Key key;
00082 QPointer<AddUserIDDialog> dialog;
00083 QPointer<AddUserIDJob> job;
00084 };
00085
00086
00087 AddUserIDCommand::Private * AddUserIDCommand::d_func() { return static_cast<Private*>( d.get() ); }
00088 const AddUserIDCommand::Private * AddUserIDCommand::d_func() const { return static_cast<const Private*>( d.get() ); }
00089
00090 #define d d_func()
00091 #define q q_func()
00092
00093 AddUserIDCommand::Private::Private( AddUserIDCommand * qq, KeyListController * c )
00094 : Command::Private( qq, c ),
00095 key(),
00096 dialog(),
00097 job()
00098 {
00099
00100 }
00101
00102 AddUserIDCommand::Private::~Private() { kDebug(); }
00103
00104 AddUserIDCommand::AddUserIDCommand( KeyListController * c )
00105 : Command( new Private( this, c ) )
00106 {
00107 d->init();
00108 }
00109
00110 AddUserIDCommand::AddUserIDCommand( QAbstractItemView * v, KeyListController * c )
00111 : Command( v, new Private( this, c ) )
00112 {
00113 d->init();
00114 }
00115
00116 AddUserIDCommand::AddUserIDCommand( const GpgME::Key & key )
00117 : Command( key, new Private( this, 0 ) )
00118 {
00119 d->init();
00120 }
00121
00122 void AddUserIDCommand::Private::init() {
00123
00124 }
00125
00126 AddUserIDCommand::~AddUserIDCommand() { kDebug(); }
00127
00128 void AddUserIDCommand::doStart() {
00129
00130 const std::vector<Key> keys = d->keys();
00131 if ( keys.size() != 1 ||
00132 keys.front().protocol() != GpgME::OpenPGP ||
00133 !keys.front().hasSecret() ) {
00134 d->finished();
00135 return;
00136 }
00137
00138 d->key = keys.front();
00139
00140 d->ensureDialogCreated();
00141 assert( d->dialog );
00142 d->dialog->show();
00143 }
00144
00145 void AddUserIDCommand::Private::slotDialogAccepted() {
00146 assert( dialog );
00147
00148 createJob();
00149 if ( !job )
00150 finished();
00151
00152 else if ( const Error err = job->start( key, dialog->name(), dialog->email(), dialog->comment() ) ) {
00153 showErrorDialog( err );
00154 finished();
00155 }
00156 }
00157
00158 void AddUserIDCommand::Private::slotDialogRejected() {
00159 emit q->canceled();
00160 finished();
00161 }
00162
00163 void AddUserIDCommand::Private::slotResult( const Error & err ) {
00164 if ( err.isCanceled() )
00165 ;
00166 else if ( err )
00167 showErrorDialog( err );
00168 else
00169 showSuccessDialog();
00170 finished();
00171 }
00172
00173 void AddUserIDCommand::doCancel() {
00174 kDebug();
00175 if ( d->job )
00176 d->job->slotCancel();
00177 }
00178
00179 void AddUserIDCommand::Private::ensureDialogCreated() {
00180 if ( dialog )
00181 return;
00182
00183 dialog = new AddUserIDDialog( parentWidgetOrView() );
00184 dialog->setAttribute( Qt::WA_DeleteOnClose );
00185
00186 connect( dialog, SIGNAL(accepted()), q, SLOT(slotDialogAccepted()) );
00187 connect( dialog, SIGNAL(rejected()), q, SLOT(slotDialogRejected()) );
00188 }
00189
00190 void AddUserIDCommand::Private::createJob() {
00191 assert( !job );
00192
00193 const CryptoBackend::Protocol * const backend = CryptoBackendFactory::instance()->protocol( key.protocol() );
00194 if ( !backend )
00195 return;
00196
00197 AddUserIDJob * const j = backend->addUserIDJob();
00198 if ( !j )
00199 return;
00200
00201 connect( j, SIGNAL(progress(QString,int,int)),
00202 q, SIGNAL(progress(QString,int,int)) );
00203 connect( j, SIGNAL(result(GpgME::Error)),
00204 q, SLOT(slotResult(GpgME::Error)) );
00205
00206 job = j;
00207 }
00208
00209 void AddUserIDCommand::Private::showErrorDialog( const Error & err ) {
00210 KMessageBox::error( parentWidgetOrView(),
00211 i18nc("@info",
00212 "<para>An error occurred while trying to add the user-id: "
00213 "<message>%1</message></para>",
00214 QString::fromLocal8Bit( err.asString() ) ),
00215 i18n("Add User-ID Error") );
00216 }
00217
00218 void AddUserIDCommand::Private::showSuccessDialog() {
00219 KMessageBox::information( parentWidgetOrView(),
00220 i18n("User-ID successfully added."),
00221 i18n("Add User-ID Succeeded") );
00222 }
00223
00224 #undef d
00225 #undef q
00226
00227 #include "moc_adduseridcommand.cpp"