From 71f8bc2d05c980afe0938e24b89a01460411fa2e Mon Sep 17 00:00:00 2001
From: Alexander Golubev <fatzer2@gmail.com>
Date: Tue, 10 Mar 2026 04:41:29 +0300
Subject: kmail: refactor Verify*Memento classes

Move common functionality to a dedicated superclass; no practical
changes were maid.

Signed-off-by: Alexander Golubev <fatzer2@gmail.com>
---
 kmail/objecttreeparser_p.cpp | 195 +++++++++++++++++--------------------------
 kmail/objecttreeparser_p.h   |  65 +++++++++------
 2 files changed, 118 insertions(+), 142 deletions(-)

diff --git a/kmail/objecttreeparser_p.cpp b/kmail/objecttreeparser_p.cpp
index fbd2b4df..c0a5eef4 100644
--- a/kmail/objecttreeparser_p.cpp
+++ b/kmail/objecttreeparser_p.cpp
@@ -70,6 +70,69 @@ void CryptoBodyPartMemento::setRunning( bool running ) {
   m_running = running;
 }
 
+GenericVerifyMemento::GenericVerifyMemento(Kleo::KeyListJob * klj)
+  : m_keylistjob(klj)
+{}
+
+void GenericVerifyMemento::execKeyListJob() {
+  if ( canStartKeyListJob() ) {
+    std::vector<GpgME::Key> keys;
+    m_keylistjob->exec( keyListPattern(), /*secretOnly=*/false, keys );
+    if ( !keys.empty() )
+      m_key = keys.back();
+  }
+  destroyKeyListJob(); // exec'ed jobs don't delete themselves
+}
+
+bool GenericVerifyMemento::startKeyListJob()
+{
+  if ( !canStartKeyListJob() )
+    return false;
+  if ( const GpgME::Error err = m_keylistjob->start( keyListPattern() ) )
+    return false;
+  connect( m_keylistjob, TQ_SIGNAL(done()), this, TQ_SLOT(slotKeyListJobDone()) );
+  connect( m_keylistjob, TQ_SIGNAL(nextKey(const GpgME::Key&)),
+           this, TQ_SLOT(slotNextKey(const GpgME::Key&)) );
+  return true;
+}
+
+void GenericVerifyMemento::destroyKeyListJob() {
+  if ( m_keylistjob )
+    m_keylistjob->deleteLater();
+  m_keylistjob = 0;
+}
+
+bool GenericVerifyMemento::canStartKeyListJob() const {
+  if ( !m_keylistjob )
+    return false;
+  const char * const fpr = m_vr.signature( 0 ).fingerprint();
+  return fpr && *fpr;
+}
+
+void GenericVerifyMemento::slotNextKey( const GpgME::Key & key )
+{
+  m_key = key;
+}
+
+void GenericVerifyMemento::slotKeyListJobDone()
+{
+  m_keylistjob = 0;
+  setRunning( false );
+  notify();
+}
+
+TQStringList GenericVerifyMemento::keyListPattern() const
+{
+  assert( canStartKeyListJob() );
+  return TQStringList( TQString::fromLatin1( m_vr.signature( 0 ).fingerprint() ) );
+}
+
+
+GenericVerifyMemento::~GenericVerifyMemento() {
+  if ( m_keylistjob )
+    m_keylistjob->slotCancel();
+}
+
 DecryptVerifyBodyPartMemento::DecryptVerifyBodyPartMemento( DecryptVerifyJob * job, const TQByteArray & cipherText )
   : CryptoBodyPartMemento(),
     m_cipherText( cipherText ),
@@ -122,22 +185,20 @@ void DecryptVerifyBodyPartMemento::slotResult( const DecryptionResult & dr,
                                                const TQByteArray & plainText )
 {
   saveResult( dr, vr, plainText );
+  setRunning( false );
   m_job = 0;
   notify();
 }
 
 
-
-
 VerifyDetachedBodyPartMemento::VerifyDetachedBodyPartMemento( VerifyDetachedJob * job,
                                                               KeyListJob * klj,
                                                               const TQByteArray & signature,
                                                               const TQByteArray & plainText )
-  : CryptoBodyPartMemento(),
+  : GenericVerifyMemento(klj),
     m_signature( signature ),
     m_plainText( plainText ),
-    m_job( job ),
-    m_keylistjob( klj )
+    m_job( job )
 {
   assert( m_job );
 }
@@ -145,14 +206,12 @@ VerifyDetachedBodyPartMemento::VerifyDetachedBodyPartMemento( VerifyDetachedJob
 VerifyDetachedBodyPartMemento::~VerifyDetachedBodyPartMemento() {
   if ( m_job )
     m_job->slotCancel();
-  if ( m_keylistjob )
-    m_keylistjob->slotCancel();
 }
 
 bool VerifyDetachedBodyPartMemento::start() {
   assert( m_job );
   if ( const GpgME::Error err = m_job->start( m_signature, m_plainText ) ) {
-    m_vr = VerificationResult( err );
+    setVerificationResult( VerificationResult( err ) );
     return false;
   }
   connect( m_job, TQ_SIGNAL(result(const GpgME::VerificationResult&)),
@@ -167,36 +226,14 @@ void VerifyDetachedBodyPartMemento::exec() {
   saveResult( m_job->exec( m_signature, m_plainText ) );
   m_job->deleteLater(); // exec'ed jobs don't delete themselves
   m_job = 0;
-  if ( canStartKeyListJob() ) {
-    std::vector<GpgME::Key> keys;
-    m_keylistjob->exec( keyListPattern(), /*secretOnly=*/false, keys );
-    if ( !keys.empty() )
-      m_key = keys.back();
-  }
-  if ( m_keylistjob )
-    m_keylistjob->deleteLater(); // exec'ed jobs don't delete themselves
-  m_keylistjob = 0;
+  execKeyListJob();
   setRunning( false );
 }
 
-bool VerifyDetachedBodyPartMemento::canStartKeyListJob() const
-{
-  if ( !m_keylistjob )
-    return false;
-  const char * const fpr = m_vr.signature( 0 ).fingerprint();
-  return fpr && *fpr;
-}
-
-TQStringList VerifyDetachedBodyPartMemento::keyListPattern() const
-{
-  assert( canStartKeyListJob() );
-  return TQStringList( TQString::fromLatin1( m_vr.signature( 0 ).fingerprint() ) );
-}
-
 void VerifyDetachedBodyPartMemento::saveResult( const VerificationResult & vr )
 {
   assert( m_job );
-  m_vr = vr;
+  setVerificationResult( vr );
   setAuditLog( m_job->auditLogError(), m_job->auditLogAsHtml() );
 }
 
@@ -204,46 +241,19 @@ void VerifyDetachedBodyPartMemento::slotResult( const VerificationResult & vr )
 {
   saveResult( vr );
   m_job = 0;
-  if ( canStartKeyListJob() && startKeyListJob() )
+  if ( startKeyListJob() )
     return;
-  if ( m_keylistjob )
-    m_keylistjob->deleteLater();
-  m_keylistjob = 0;
-  setRunning( false );
-  notify();
-}
-
-bool VerifyDetachedBodyPartMemento::startKeyListJob()
-{
-  assert( canStartKeyListJob() );
-  if ( const GpgME::Error err = m_keylistjob->start( keyListPattern() ) )
-    return false;
-  connect( m_keylistjob, TQ_SIGNAL(done()), this, TQ_SLOT(slotKeyListJobDone()) );
-  connect( m_keylistjob, TQ_SIGNAL(nextKey(const GpgME::Key&)),
-           this, TQ_SLOT(slotNextKey(const GpgME::Key&)) );
-  return true;
-}
-
-void VerifyDetachedBodyPartMemento::slotNextKey( const GpgME::Key & key )
-{
-  m_key = key;
-}
-
-void VerifyDetachedBodyPartMemento::slotKeyListJobDone()
-{
-  m_keylistjob = 0;
+  destroyKeyListJob();
   setRunning( false );
   notify();
 }
 
-
 VerifyOpaqueBodyPartMemento::VerifyOpaqueBodyPartMemento( VerifyOpaqueJob * job,
                                                           KeyListJob *  klj,
                                                           const TQByteArray & signature )
-  : CryptoBodyPartMemento(),
+  : GenericVerifyMemento(klj),
     m_signature( signature ),
-    m_job( job ),
-    m_keylistjob( klj )
+    m_job( job )
 {
   assert( m_job );
 }
@@ -251,14 +261,12 @@ VerifyOpaqueBodyPartMemento::VerifyOpaqueBodyPartMemento( VerifyOpaqueJob * job,
 VerifyOpaqueBodyPartMemento::~VerifyOpaqueBodyPartMemento() {
   if ( m_job )
     m_job->slotCancel();
-  if ( m_keylistjob )
-    m_keylistjob->slotCancel();
 }
 
 bool VerifyOpaqueBodyPartMemento::start() {
   assert( m_job );
   if ( const GpgME::Error err = m_job->start( m_signature ) ) {
-    m_vr = VerificationResult( err );
+    setVerificationResult( VerificationResult( err ));
     return false;
   }
   connect( m_job, TQ_SIGNAL(result(const GpgME::VerificationResult&,const TQByteArray&)),
@@ -274,37 +282,15 @@ void VerifyOpaqueBodyPartMemento::exec() {
   saveResult( m_job->exec( m_signature, plainText ), plainText );
   m_job->deleteLater(); // exec'ed jobs don't delete themselves
   m_job = 0;
-  if ( canStartKeyListJob() ) {
-    std::vector<GpgME::Key> keys;
-    m_keylistjob->exec( keyListPattern(), /*secretOnly=*/false, keys );
-    if ( !keys.empty() )
-      m_key = keys.back();
-  }
-  if ( m_keylistjob )
-    m_keylistjob->deleteLater(); // exec'ed jobs don't delete themselves
-  m_keylistjob = 0;
+  execKeyListJob();
   setRunning( false );
 }
 
-bool VerifyOpaqueBodyPartMemento::canStartKeyListJob() const
-{
-  if ( !m_keylistjob )
-    return false;
-  const char * const fpr = m_vr.signature( 0 ).fingerprint();
-  return fpr && *fpr;
-}
-
-TQStringList VerifyOpaqueBodyPartMemento::keyListPattern() const
-{
-  assert( canStartKeyListJob() );
-  return TQStringList( TQString::fromLatin1( m_vr.signature( 0 ).fingerprint() ) );
-}
-
 void VerifyOpaqueBodyPartMemento::saveResult( const VerificationResult & vr,
                                               const TQByteArray & plainText )
 {
   assert( m_job );
-  m_vr = vr;
+  setVerificationResult(vr);
   m_plainText = plainText;
   setAuditLog( m_job->auditLogError(), m_job->auditLogAsHtml() );
 }
@@ -314,34 +300,9 @@ void VerifyOpaqueBodyPartMemento::slotResult( const VerificationResult & vr,
 {
   saveResult( vr, plainText );
   m_job = 0;
-  if ( canStartKeyListJob() && startKeyListJob() )
+  if ( startKeyListJob() )
     return;
-  if ( m_keylistjob )
-    m_keylistjob->deleteLater();
-  m_keylistjob = 0;
-  setRunning( false );
-  notify();
-}
-
-bool VerifyOpaqueBodyPartMemento::startKeyListJob()
-{
-  assert( canStartKeyListJob() );
-  if ( const GpgME::Error err = m_keylistjob->start( keyListPattern() ) )
-    return false;
-  connect( m_keylistjob, TQ_SIGNAL(done()), this, TQ_SLOT(slotKeyListJobDone()) );
-  connect( m_keylistjob, TQ_SIGNAL(nextKey(const GpgME::Key&)),
-           this, TQ_SLOT(slotNextKey(const GpgME::Key&)) );
-  return true;
-}
-
-void VerifyOpaqueBodyPartMemento::slotNextKey( const GpgME::Key & key )
-{
-  m_key = key;
-}
-
-void VerifyOpaqueBodyPartMemento::slotKeyListJobDone()
-{
-  m_keylistjob = 0;
+  destroyKeyListJob();
   setRunning( false );
   notify();
 }
diff --git a/kmail/objecttreeparser_p.h b/kmail/objecttreeparser_p.h
index 961fbfdf..943402d9 100644
--- a/kmail/objecttreeparser_p.h
+++ b/kmail/objecttreeparser_p.h
@@ -84,11 +84,48 @@ namespace KMail {
     GpgME::Error m_auditLogError;
   };
 
+  /**
+   * A memento encapsulating some common logic for other verify subclasses
+   */
+  class GenericVerifyMemento
+    : public CryptoBodyPartMemento
+  {
+    TQ_OBJECT
+
+  public:
+    ~GenericVerifyMemento();
+
+    const GpgME::VerificationResult & verifyResult() const { return m_vr; }
+    const GpgME::Key & signingKey() const { return m_key; }
+
+  private slots:
+    void slotKeyListJobDone();
+    void slotNextKey( const GpgME::Key & );
+
+  protected:
+    GenericVerifyMemento ( Kleo::KeyListJob * klj );
+
+    void execKeyListJob();
+    bool startKeyListJob();
+    void destroyKeyListJob();
+
+    bool canStartKeyListJob() const;
+    void setVerificationResult( const GpgME::VerificationResult & vr) { m_vr = vr; };
+
+    TQStringList keyListPattern() const;
+  private:
+    // input:
+    TQGuardedPtr<Kleo::KeyListJob> m_keylistjob;
+    // output:
+    GpgME::VerificationResult m_vr;
+    GpgME::Key m_key;
+  };
+
   class DecryptVerifyBodyPartMemento
     : public CryptoBodyPartMemento
   {
     TQ_OBJECT
-  
+
   public:
     DecryptVerifyBodyPartMemento( Kleo::DecryptVerifyJob * job, const TQByteArray & cipherText );
     ~DecryptVerifyBodyPartMemento();
@@ -121,7 +158,7 @@ namespace KMail {
 
 
   class VerifyDetachedBodyPartMemento
-    : public CryptoBodyPartMemento
+    : public GenericVerifyMemento
   {
     TQ_OBJECT
   
@@ -135,33 +172,22 @@ namespace KMail {
     bool start();
     void exec();
 
-    const GpgME::VerificationResult & verifyResult() const { return m_vr; }
-    const GpgME::Key & signingKey() const { return m_key; }
-
   private slots:
     void slotResult( const GpgME::VerificationResult & vr );
-    void slotKeyListJobDone();
-    void slotNextKey( const GpgME::Key & );
 
   private:
     void saveResult( const GpgME::VerificationResult & );
-    bool canStartKeyListJob() const;
-    TQStringList keyListPattern() const;
-    bool startKeyListJob();
   private:
     // input:
     const TQByteArray m_signature;
     const TQByteArray m_plainText;
     TQGuardedPtr<Kleo::VerifyDetachedJob> m_job;
-    TQGuardedPtr<Kleo::KeyListJob> m_keylistjob;
     // output:
-    GpgME::VerificationResult m_vr;
-    GpgME::Key m_key;
   };
 
 
   class VerifyOpaqueBodyPartMemento
-    : public CryptoBodyPartMemento
+    : public GenericVerifyMemento
   {
     TQ_OBJECT
   
@@ -175,30 +201,19 @@ namespace KMail {
     void exec();
 
     const TQByteArray & plainText() const { return m_plainText; }    
-    const GpgME::VerificationResult & verifyResult() const { return m_vr; }
-    const GpgME::Key & signingKey() const { return m_key; }
 
   private slots:
     void slotResult( const GpgME::VerificationResult & vr,
                      const TQByteArray & plainText );
-    void slotKeyListJobDone();
-    void slotNextKey( const GpgME::Key & );
-
   private:
     void saveResult( const GpgME::VerificationResult &,
                      const TQByteArray & );
-    bool canStartKeyListJob() const;
-    TQStringList keyListPattern() const;
-    bool startKeyListJob();
   private:
     // input:
     const TQByteArray m_signature;
     TQGuardedPtr<Kleo::VerifyOpaqueJob> m_job;
-    TQGuardedPtr<Kleo::KeyListJob> m_keylistjob;
     // output:
-    GpgME::VerificationResult m_vr;
     TQByteArray m_plainText;
-    GpgME::Key m_key;
   };
 
 
-- 
cgit v1.2.3

