From 6ac7ea7c0fa977843e73cbeb5d1efe49bb09187d Mon Sep 17 00:00:00 2001 From: SET Date: Sat, 14 Nov 2020 11:25:00 +0100 Subject: [PATCH] Reorganize code. Move KeyEdit::IsOurKey to Tools::IsOurKey. --- K7Main.cpp | 6 ++++-- KeyEdit.cpp | 16 +++------------- KeyEdit.h | 6 ------ Tools.cpp | 17 +++++++++++++++++ Tools.h | 9 ++++++++- 5 files changed, 32 insertions(+), 22 deletions(-) diff --git a/K7Main.cpp b/K7Main.cpp index 55866a3..529943c 100644 --- a/K7Main.cpp +++ b/K7Main.cpp @@ -253,6 +253,7 @@ void K7Main::DisplayKeys(const vector& kList, const WString& grpLabe { WTreeTableNode * grpNode = new WTreeTableNode(grpLabel); m_ttbKeys->treeRoot()->addChildNode(unique_ptr (grpNode)); + vector ourKeys = m_config->PrivateKeyIds(); for (uint i = 0; i < kList.size(); i++) { const GpgME::Key k = kList.at(i); @@ -272,7 +273,7 @@ void K7Main::DisplayKeys(const vector& kList, const WString& grpLabe * Here we allow the owner trust level of primary keys to be changed anytime. * Kleopatra doesn't do that for primary keys having ultimate trust level. */ - bool isOurKey = m_keyEdit->IsOurKey(k.primaryFingerprint()); + bool isOurKey = Tools::IsOurKey(k.primaryFingerprint(), ourKeys); if (!isOurKey || (isOurKey && k.hasSecret())) { lblOwnerTrust->doubleClicked().connect(std::bind(&KeyEdit::OnOwnerTrustDoubleClicked, m_keyEdit, keyNode, k.hasSecret())); lblOwnerTrust->setToolTip(TR("TTTDoubleCLick")); @@ -386,9 +387,10 @@ void K7Main::DisplaySubKeys(const WString& fullKeyID, bool secret) rootNode->setChildCountPolicy(ChildCountPolicy::Enabled); m_ttbSubKeys->setTreeRoot(unique_ptr (rootNode), TR("SubKeys")); rootNode->expand(); + vector ourKeys = m_config->PrivateKeyIds(); bool canEditExpiry = m_config->CanEditExpiryTime() && Tools::KeyHasSecret(k.primaryFingerprint()) - && m_keyEdit->IsOurKey(k.primaryFingerprint()); + && Tools::IsOurKey(k.primaryFingerprint(), ourKeys); for (uint i = 0; i < k.numSubkeys(); i++) { Subkey sk = k.subkey(i); diff --git a/KeyEdit.cpp b/KeyEdit.cpp index 8506a70..d577fe7 100644 --- a/KeyEdit.cpp +++ b/KeyEdit.cpp @@ -39,7 +39,9 @@ void KeyEdit::OnOwnerTrustDoubleClicked(WTreeTableNode * keyNode, bool keyHasSec * not be editable by anyone. */ WText * lblFpr = static_cast (keyNode->columnWidget(3)); - if (!IsOurKey(lblFpr->text()) && Tools::KeyHasSecret(lblFpr->text())) { + vector ourKeys = m_owner->m_config->PrivateKeyIds(); + if (!Tools::IsOurKey(lblFpr->text(), ourKeys) + && Tools::KeyHasSecret(lblFpr->text())) { m_owner->m_tmwMessage->SetText(TR("OwnerTrustReadOnly")); return; } @@ -118,18 +120,6 @@ void KeyEdit::FillOwnerTrustCombo(WComboBox * cmb, bool keyHasSecret) cmb->setModelColumn(1); } -bool KeyEdit::IsOurKey(const WString& fpr) -{ - vector ourKeys = m_owner->m_config->PrivateKeyIds(); - vector ::iterator it; - for (it = ourKeys.begin(); it != ourKeys.end(); it++) - { - if (*it == fpr) - return true; - } - return false; -} - void KeyEdit::OnUidValidityClicked(WTreeTableNode* uidNode, vector& privateKeys, const WString& targetKeyFpr) { if (targetKeyFpr != m_targetUidValidityKeyFpr) { diff --git a/KeyEdit.h b/KeyEdit.h index 4364758..ac4ad8d 100644 --- a/KeyEdit.h +++ b/KeyEdit.h @@ -41,12 +41,6 @@ public: * @param keyHasSecret */ void OnOwnerTrustBlurred(WTreeTableNode * keyNode, bool keyHasSecret); - /** - * If the fingerprint is that of a private key we manage, returns true. - * @param fpr - * @return - */ - bool IsOurKey(const WString& fpr); /** * Shows a popup with parameters for key certification. * @param uidNode diff --git a/Tools.cpp b/Tools.cpp index 80cb4bc..4fa4f67 100644 --- a/Tools.cpp +++ b/Tools.cpp @@ -104,3 +104,20 @@ bool Tools::KeyHasSecret(const WString& fpr) } return (!k.isNull()); } + +bool Tools::IsFound(const WString& item, vector& items) +{ + // std:find should be more aesthetic. + vector ::iterator it; + for (it = items.begin(); it != items.end(); it++) + { + if ((*it) == item) + return true; + } + return false; +} + +bool Tools::IsOurKey(const WString& fpr, vector& ourPrivKeys) +{ + return IsFound(fpr, ourPrivKeys); +} diff --git a/Tools.h b/Tools.h index f474c3d..b4f3531 100644 --- a/Tools.h +++ b/Tools.h @@ -44,9 +44,16 @@ public: static WString GetUidStatus(const GpgME::UserID& uid); static WString GetSigStatus(const GpgME::UserID::Signature& sig); static bool KeyHasSecret(const WString& fpr); + /** + * If the fingerprint is that of a private key we manage, returns true. + * @param fpr + * @param ourPrivKeys + * @return + */ + static bool IsOurKey(const WString& fpr, vector& ourPrivKeys); private: - + static bool IsFound(const WString& item, vector& items); }; #endif /* TOOLS_H */