kleopatra
encryptclipboardcommand.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 "encryptclipboardcommand.h"
00036
00037 #include "command_p.h"
00038
00039 #include <crypto/encryptemailcontroller.h>
00040
00041 #include <utils/input.h>
00042 #include <utils/output.h>
00043
00044 #include <KLocale>
00045 #include <KMessageBox>
00046 #include <kdebug.h>
00047
00048 #include <QApplication>
00049 #include <QClipboard>
00050 #include <QMimeData>
00051
00052 #include <exception>
00053
00054 using namespace Kleo;
00055 using namespace Kleo::Commands;
00056 using namespace Kleo::Crypto;
00057 using namespace boost;
00058
00059 namespace {
00060 struct nodelete {
00061 template <typename T>
00062 void operator()( const T * ) const {}
00063 };
00064 }
00065
00066 class EncryptClipboardCommand::Private : public Command::Private {
00067 friend class ::Kleo::Commands::EncryptClipboardCommand;
00068 EncryptClipboardCommand * q_func() const { return static_cast<EncryptClipboardCommand*>( q ); }
00069 public:
00070 explicit Private( EncryptClipboardCommand * qq, KeyListController * c );
00071 ~Private();
00072
00073 void init();
00074
00075 private:
00076 void slotRecipientsResolved();
00077 void slotControllerDone() {
00078 finished();
00079 }
00080 void slotControllerError( int, const QString & ) {
00081 finished();
00082 }
00083
00084 private:
00085 shared_ptr<const ExecutionContext> shared_qq;
00086 shared_ptr<Input> input;
00087 EncryptEMailController controller;
00088 };
00089
00090
00091 EncryptClipboardCommand::Private * EncryptClipboardCommand::d_func() { return static_cast<Private*>( d.get() ); }
00092 const EncryptClipboardCommand::Private * EncryptClipboardCommand::d_func() const { return static_cast<const Private*>( d.get() ); }
00093
00094 #define d d_func()
00095 #define q q_func()
00096
00097 EncryptClipboardCommand::Private::Private( EncryptClipboardCommand * qq, KeyListController * c )
00098 : Command::Private( qq, c ),
00099 shared_qq( qq, nodelete() ),
00100 input(),
00101 controller( EncryptEMailController::ClipboardMode )
00102 {
00103
00104 }
00105
00106 EncryptClipboardCommand::Private::~Private() { kDebug(); }
00107
00108 EncryptClipboardCommand::EncryptClipboardCommand( KeyListController * c )
00109 : Command( new Private( this, c ) )
00110 {
00111 d->init();
00112 }
00113
00114 EncryptClipboardCommand::EncryptClipboardCommand( QAbstractItemView * v, KeyListController * c )
00115 : Command( v, new Private( this, c ) )
00116 {
00117 d->init();
00118 }
00119
00120 void EncryptClipboardCommand::Private::init() {
00121 controller.setExecutionContext( shared_qq );
00122 connect( &controller, SIGNAL(done()), q, SLOT(slotControllerDone()) );
00123 connect( &controller, SIGNAL(error(int,QString)), q, SLOT(slotControllerError(int,QString)) );
00124 }
00125
00126 EncryptClipboardCommand::~EncryptClipboardCommand() { kDebug(); }
00127
00128
00129 bool EncryptClipboardCommand::canEncryptCurrentClipboard() {
00130 if ( const QClipboard * clip = QApplication::clipboard() )
00131 if ( const QMimeData * mime = clip->mimeData() )
00132 return mime->hasText();
00133 return false;
00134 }
00135
00136 void EncryptClipboardCommand::doStart() {
00137
00138 try {
00139
00140
00141 d->input = Input::createFromClipboard();
00142
00143 connect( &d->controller, SIGNAL(recipientsResolved()),
00144 this, SLOT(slotRecipientsResolved()) );
00145
00146 d->controller.startResolveRecipients();
00147
00148 } catch ( const std::exception & e ) {
00149 KMessageBox::information( d->view(),
00150 i18n("An error occurred: %1",
00151 QString::fromLocal8Bit( e.what() ) ),
00152 i18n("Encrypt Clipboard Error") );
00153 d->finished();
00154 }
00155 }
00156
00157 void EncryptClipboardCommand::Private::slotRecipientsResolved() {
00158 try {
00159 controller.setInputAndOutput( input, Output::createFromClipboard() );
00160 input.reset();
00161 controller.start();
00162 } catch ( const std::exception & e ) {
00163 KMessageBox::information( view(),
00164 i18n("An error occurred: %1",
00165 QString::fromLocal8Bit( e.what() ) ),
00166 i18n("Encrypt Clipboard Error") );
00167 finished();
00168 }
00169 }
00170
00171 void EncryptClipboardCommand::doCancel() {
00172 kDebug();
00173 d->controller.cancel();
00174 }
00175
00176 void EncryptClipboardCommand::applyWindowID( QWidget * wid ) const {
00177 if ( wid )
00178 wid->setParent( d->view(), wid->windowFlags() );
00179 }
00180
00181 #undef d
00182 #undef q
00183
00184 #include "moc_encryptclipboardcommand.cpp"