From 061f676041e6df0db2e550acf903ae7315d9ad07 Mon Sep 17 00:00:00 2001 From: Christian Hohnstaedt Date: Tue, 30 May 2023 14:38:55 +0200 Subject: [PATCH] Improve/simplify item import Unify import functions for some (on_butImport_clicked) or all (on_butOk_clicked) items. They both call "importIndexes(const QModelIndexList &indexes)" containing some or all indexes to import. db_x509.cpp: Only need to search the treeItem (containing all items without issuer, not all in the rootItem (containing also certificates with known issuer) Re-use "remFromCont()" when stealing items from an other CA. --- lib/db_base.cpp | 11 ++--------- lib/db_base.h | 4 ++-- lib/db_x509.cpp | 2 +- widgets/ImportMulti.cpp | 25 +++++++++++++------------ widgets/ImportMulti.h | 2 +- 5 files changed, 19 insertions(+), 25 deletions(-) diff --git a/lib/db_base.cpp b/lib/db_base.cpp index 16e9ea62..9b85d97c 100644 --- a/lib/db_base.cpp +++ b/lib/db_base.cpp @@ -300,15 +300,8 @@ void db_base::insertChild(pki_base *child, pki_base *parent) if (!parent || parent == child) parent = treeItem; - if (curr_parent) { - /* Need to take it */ - if (curr_parent != treeItem && treeview) - idx = index(curr_parent); - int row = rownumber(child); - beginRemoveRows(idx, row, row); - curr_parent->takeChild(child); - endRemoveRows(); - } + if (curr_parent) + remFromCont(index(child)); if (parent != treeItem && treeview) idx = index(parent); diff --git a/lib/db_base.h b/lib/db_base.h index 8d95a567..676b55eb 100644 --- a/lib/db_base.h +++ b/lib/db_base.h @@ -75,10 +75,10 @@ class db_base: public QAbstractItemModel void dump(const QString &dirname) const; QModelIndex index(int row, int column, - const QModelIndex &parent) const; + const QModelIndex &parent = QModelIndex()) const; QModelIndex index(pki_base *pki) const; QModelIndex parent(const QModelIndex &index) const; - int rowCount(const QModelIndex &parent) const; + int rowCount(const QModelIndex &parent = QModelIndex()) const; int allItemsCount() const { return rootItem->childCount(); diff --git a/lib/db_x509.cpp b/lib/db_x509.cpp index 7be323ba..5c686477 100644 --- a/lib/db_x509.cpp +++ b/lib/db_x509.cpp @@ -192,7 +192,7 @@ void db_x509::inToCont(pki_base *pki) revList.merge(other->getRevList()); } /* Search rootItem childs, whether they are ours */ - foreach(pki_base *b, rootItem->getChildItems()) { + foreach(pki_base *b, treeItem->getChildItems()) { pki_x509 *child = dynamic_cast(b); if (!child || child == cert || child->getSigner() == child) continue; diff --git a/widgets/ImportMulti.cpp b/widgets/ImportMulti.cpp index 03e135a1..ed7183e6 100644 --- a/widgets/ImportMulti.cpp +++ b/widgets/ImportMulti.cpp @@ -163,34 +163,32 @@ void ImportMulti::on_butRemove_clicked() void ImportMulti::on_butOk_clicked() { - if (!openDB()) - return; + QModelIndexList indexes; - Transaction; - if (!TransBegin()) - return; + for (int i = 0; i < mcont->rowCount(); i++) + indexes << mcont->index(i, 0, QModelIndex()); - while (mcont->rowCount(QModelIndex())) - import(mcont->index(0, 0, QModelIndex())); - - TransCommit(); + importIndexes(indexes); accept(); } void ImportMulti::on_butImport_clicked() { QItemSelectionModel *selectionModel = listView->selectionModel(); - QModelIndexList indexes = selectionModel->selectedIndexes(); + importIndexes(selectionModel->selectedIndexes()); +} + +void ImportMulti::importIndexes(const QModelIndexList &indexes) +{ if (!openDB()) return; Transaction; if (!TransBegin()) return; + foreach(QModelIndex index, indexes) { - if (index.column() != 0) - continue; import(index); } TransCommit(); @@ -237,6 +235,9 @@ pki_base *ImportMulti::import(const QModelIndex &idx) { pki_base *pki = mcont->fromIndex(idx); + if (idx.column() != 0) + return NULL; + for (int i = 0; i < mcont->rowCount(idx); i++) import(mcont->index(i, 0, idx)); diff --git a/widgets/ImportMulti.h b/widgets/ImportMulti.h index 9c3c4553..965f4107 100644 --- a/widgets/ImportMulti.h +++ b/widgets/ImportMulti.h @@ -31,6 +31,7 @@ class ImportMulti: public QDialog, private Ui::ImportMulti void addItem(pki_base *pki); pki_base *getSelected(); pki_base *import(const QModelIndex &idx); + void importIndexes(const QModelIndexList &indexes); void execute(int force=0, QStringList failed = QStringList()); int entries(); void tokenInfo(const slotid &s); @@ -45,7 +46,6 @@ class ImportMulti: public QDialog, private Ui::ImportMulti void on_butOk_clicked(); void on_deleteToken_clicked(); void on_renameToken_clicked(); - }; #endif