From 16f4df4191caeaafb0efffeae72d25fd5899ca1b Mon Sep 17 00:00:00 2001
From: Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com>
Date: Mon, 6 Oct 2014 20:42:13 +0200
Subject: [PATCH] [ #55823 ] Fix certificate handling regression

---
 src/account.cpp     | 38 +++++++++++++++++++++++++++++++++++---
 src/account.h       |  1 +
 src/certificate.cpp |  1 +
 src/certificate.h   |  3 +++
 4 files changed, 40 insertions(+), 3 deletions(-)

diff --git a/src/account.cpp b/src/account.cpp
index a1be8ca5..f6af5e45 100644
--- a/src/account.cpp
+++ b/src/account.cpp
@@ -137,6 +137,29 @@ void Account::slotPresenceMessageChanged(const QString& message)
    emit changed(this);
 }
 
+void Account::slotUpdateCertificate()
+{
+   Certificate* cert = qobject_cast<Certificate*>(sender());
+   if (cert) {
+      switch (cert->type()) {
+         case Certificate::Type::AUTHORITY:
+            if (accountDetail(Account::MapField::TLS::CA_LIST_FILE) != cert->path().toString())
+               setAccountDetail(Account::MapField::TLS::CA_LIST_FILE, cert->path().toString());
+            break;
+         case Certificate::Type::USER:
+            if (accountDetail(Account::MapField::TLS::CERTIFICATE_FILE) != cert->path().toString())
+               setAccountDetail(Account::MapField::TLS::CERTIFICATE_FILE, cert->path().toString());
+            break;
+         case Certificate::Type::PRIVATE_KEY:
+            if (accountDetail(Account::MapField::TLS::PRIVATE_KEY_FILE) != cert->path().toString())
+               setAccountDetail(Account::MapField::TLS::PRIVATE_KEY_FILE, cert->path().toString());
+            break;
+         case Certificate::Type::NONE:
+            break;
+      };
+   }
+}
+
 /*****************************************************************************
  *                                                                           *
  *                                  Getters                                  *
@@ -467,8 +490,10 @@ int Account::tlsListenerPort() const
 ///Return the account TLS certificate authority list file
 Certificate* Account::tlsCaListCertificate() const
 {
-   if (!m_pCaCert)
+   if (!m_pCaCert) {
       const_cast<Account*>(this)->m_pCaCert = new Certificate(Certificate::Type::AUTHORITY,this);
+      connect(m_pCaCert,SIGNAL(changed()),this,SLOT(slotUpdateCertificate()));
+   }
    const_cast<Account*>(this)->m_pCaCert->setPath(accountDetail(Account::MapField::TLS::CA_LIST_FILE));
    return m_pCaCert;
 }
@@ -476,8 +501,10 @@ Certificate* Account::tlsCaListCertificate() const
 ///Return the account TLS certificate
 Certificate* Account::tlsCertificate() const
 {
-   if (!m_pTlsCert)
+   if (!m_pTlsCert) {
       const_cast<Account*>(this)->m_pTlsCert = new Certificate(Certificate::Type::USER,this);
+      connect(m_pTlsCert,SIGNAL(changed()),this,SLOT(slotUpdateCertificate()));
+   }
    const_cast<Account*>(this)->m_pTlsCert->setPath(accountDetail(Account::MapField::TLS::CERTIFICATE_FILE));
    return m_pTlsCert;
 }
@@ -485,8 +512,10 @@ Certificate* Account::tlsCertificate() const
 ///Return the account private key
 Certificate* Account::tlsPrivateKeyCertificate() const
 {
-   if (!m_pPrivateKey)
+   if (!m_pPrivateKey) {
       const_cast<Account*>(this)->m_pPrivateKey = new Certificate(Certificate::Type::PRIVATE_KEY,this);
+      connect(m_pPrivateKey,SIGNAL(changed()),this,SLOT(slotUpdateCertificate()));
+   }
    const_cast<Account*>(this)->m_pPrivateKey->setPath(accountDetail(Account::MapField::TLS::PRIVATE_KEY_FILE));
    return m_pPrivateKey;
 }
@@ -887,18 +916,21 @@ void Account::setTlsPassword(const QString& detail)
 ///Set the certificate authority list file
 void Account::setTlsCaListCertificate(Certificate* cert)
 {
+   m_pCaCert = cert; //FIXME memory leak
    setAccountDetail(Account::MapField::TLS::CA_LIST_FILE, cert?cert->path().toLocalFile():QString());
 }
 
 ///Set the certificate
 void Account::setTlsCertificate(Certificate* cert)
 {
+   m_pTlsCert = cert; //FIXME memory leak
    setAccountDetail(Account::MapField::TLS::CERTIFICATE_FILE, cert?cert->path().toLocalFile():QString());
 }
 
 ///Set the private key
 void Account::setTlsPrivateKeyCertificate(Certificate* cert)
 {
+   m_pPrivateKey = cert; //FIXME memory leak
    setAccountDetail(Account::MapField::TLS::PRIVATE_KEY_FILE, cert?cert->path().toLocalFile():QString());
 }
 
diff --git a/src/account.h b/src/account.h
index 81f83d12..0c9aca6d 100644
--- a/src/account.h
+++ b/src/account.h
@@ -466,6 +466,7 @@ class LIB_EXPORT Account : public QObject {
    private Q_SLOTS:
       void slotPresentChanged        (bool  present  );
       void slotPresenceMessageChanged(const QString& );
+      void slotUpdateCertificate     (               );
 
    private:
       //Constructors
diff --git a/src/certificate.cpp b/src/certificate.cpp
index b40f76c3..4570537b 100644
--- a/src/certificate.cpp
+++ b/src/certificate.cpp
@@ -38,6 +38,7 @@ QUrl Certificate::path() const
 void Certificate::setPath(const QUrl& path)
 {
    m_Path = path;
+   emit changed();
 }
 
 bool Certificate::isExpired() const
diff --git a/src/certificate.h b/src/certificate.h
index 14b68509..753ae7d4 100644
--- a/src/certificate.h
+++ b/src/certificate.h
@@ -58,6 +58,9 @@ private:
    QUrl m_Path;
    Certificate::Type m_Type;
    //TODO
+
+Q_SIGNALS:
+   void changed();
 };
 Q_DECLARE_METATYPE(Certificate*)
 
-- 
GitLab