From 29be65a42bfaa12e604ed068ffef1c9ba86c46a9 Mon Sep 17 00:00:00 2001 From: Dmitry Degtyarev Date: Tue, 14 Jun 2022 14:21:26 +0400 Subject: [PATCH] add single versions of search_items() use it where single results are expected add missing validity check to console_object_move_and_rename() --- .../all_policies_folder_impl.cpp | 10 +-- src/admc/console_impls/find_root_impl.cpp | 8 +-- src/admc/console_impls/object_impl.cpp | 68 +++++++------------ src/admc/console_impls/policy_root_impl.cpp | 8 +-- src/admc/console_impls/query_folder_impl.cpp | 8 +-- src/admc/console_widget/console_widget.cpp | 24 +++++++ src/admc/console_widget/console_widget.h | 6 ++ src/admc/policy_ou_results_widget.cpp | 10 ++- 8 files changed, 66 insertions(+), 76 deletions(-) diff --git a/src/admc/console_impls/all_policies_folder_impl.cpp b/src/admc/console_impls/all_policies_folder_impl.cpp index b24c4736..39d01d66 100644 --- a/src/admc/console_impls/all_policies_folder_impl.cpp +++ b/src/admc/console_impls/all_policies_folder_impl.cpp @@ -141,13 +141,9 @@ void AllPoliciesFolderImpl::create_policy() { QModelIndex get_all_policies_folder_index(ConsoleWidget *console) { const QModelIndex policy_tree_root = get_policy_tree_root(console); - const QList index_list = console->search_items(policy_tree_root, {ItemType_AllPoliciesFolder}); - - if (!index_list.isEmpty()) { - return index_list[0]; - } else { - return QModelIndex(); - } + const QModelIndex out = console->search_item(policy_tree_root, {ItemType_AllPoliciesFolder}); + + return out; } void all_policies_folder_impl_add_objects(ConsoleWidget *console, const QList &object_list, const QModelIndex &parent) { diff --git a/src/admc/console_impls/find_root_impl.cpp b/src/admc/console_impls/find_root_impl.cpp index 61aef940..0f1c0681 100644 --- a/src/admc/console_impls/find_root_impl.cpp +++ b/src/admc/console_impls/find_root_impl.cpp @@ -48,11 +48,7 @@ QList FindRootImpl::default_columns() const { } QModelIndex get_find_tree_root(ConsoleWidget *console) { - const QList index_list = console->search_items(QModelIndex(), {ItemType_FindRoot}); + const QModelIndex out = console->search_item(QModelIndex(), {ItemType_FindRoot}); - if (!index_list.isEmpty()) { - return index_list[0]; - } else { - return QModelIndex(); - } + return out; } diff --git a/src/admc/console_impls/object_impl.cpp b/src/admc/console_impls/object_impl.cpp index 2e429b66..8131b7ce 100644 --- a/src/admc/console_impls/object_impl.cpp +++ b/src/admc/console_impls/object_impl.cpp @@ -583,15 +583,12 @@ void console_object_properties(ConsoleWidget *console, ConsoleWidget *buddy_cons for (const AdObject &object : object_list) { const QString dn = object.get_dn(); - const QList object_index_list = target_console->search_items(root_index, update_dn_role, dn, {item_type}); + const QModelIndex object_index = target_console->search_item(root_index, update_dn_role, dn, {item_type}); - if (object_index_list.isEmpty()) { - continue; + if (object_index.isValid()) { + const QList object_row = target_console->get_row(object_index); + console_object_load(object_row, object); } - - const QModelIndex object_index = object_index_list[0]; - const QList object_row = target_console->get_row(object_index); - console_object_load(object_row, object); } }; @@ -608,17 +605,14 @@ void console_object_properties(ConsoleWidget *console, ConsoleWidget *buddy_cons if (!policy_root.isValid()) { for (const AdObject &object : object_list) { const QString dn = object.get_dn(); - const QList object_index_list = target_console->search_items(policy_root, PolicyOURole_DN, dn, {ItemType_PolicyOU}); + const QModelIndex object_index = target_console->search_item(policy_root, PolicyOURole_DN, dn, {ItemType_PolicyOU}); - if (object_index_list.isEmpty()) { - continue; + if (object_index.isValid()) { + const QList object_row = target_console->get_row(object_index); + console_object_load(object_row, object); + + policy_ou_impl_load_row(object_row, object); } - - const QModelIndex object_index = object_index_list[0]; - const QList object_row = target_console->get_row(object_index); - console_object_load(object_row, object); - - policy_ou_impl_load_row(object_row, object); } } }; @@ -1065,10 +1059,9 @@ void console_object_create(ConsoleWidget *console, ConsoleWidget *buddy_console, auto apply_changes = [&](ConsoleWidget *target_console) { const QModelIndex object_root = get_object_tree_root(target_console); if (object_root.isValid()) { - const QList parent_object_list = target_console->search_items(object_root, ObjectRole_DN, parent_dn, {ItemType_Object}); + const QModelIndex parent_object = target_console->search_item(object_root, ObjectRole_DN, parent_dn, {ItemType_Object}); - if (!parent_object_list.isEmpty()) { - const QModelIndex parent_object = parent_object_list[0]; + if (parent_object.isValid()) { object_impl_add_objects_to_console_from_dns(target_console, ad_inner, {created_dn}, parent_object); } } @@ -1085,11 +1078,9 @@ void console_object_create(ConsoleWidget *console, ConsoleWidget *buddy_console, // Apply changes to policy tree const QModelIndex policy_root = get_policy_tree_root(target_console); if (policy_root.isValid() && object_class == CLASS_OU) { - const QList parent_policy_list = target_console->search_items(policy_root, PolicyOURole_DN, parent_dn, {ItemType_PolicyOU}); - - if (!parent_policy_list.isEmpty()) { - const QModelIndex parent_policy = parent_policy_list[0]; + const QModelIndex parent_policy = target_console->search_item(policy_root, PolicyOURole_DN, parent_dn, {ItemType_PolicyOU}); + if (parent_policy.isValid()) { policy_ou_impl_add_objects_from_dns(target_console, ad_inner, {created_dn}, parent_policy); } } @@ -1137,6 +1128,7 @@ void ObjectImpl::set_disabled(const bool disabled) { for (const QString &dn : changed_objects) { const QList index_list = target_console->search_items(root_index, ObjectRole_DN, dn, {ItemType_Object}); + for (const QModelIndex &index : index_list) { QStandardItem *item = target_console->get_item(index); item->setData(disabled, ObjectRole_AccountDisabled); @@ -1221,18 +1213,10 @@ void console_object_move_and_rename(ConsoleWidget *console, ConsoleWidget *buddy // is that new object is duplicated. const QModelIndex object_root = get_object_tree_root(target_console); if (object_root.isValid()) { - const QModelIndex new_parent_index = [=]() { - const QList results = target_console->search_items(object_root, ObjectRole_DN, new_parent_dn, {ItemType_Object}); + const QModelIndex parent_object = target_console->search_item(object_root, ObjectRole_DN, new_parent_dn, {ItemType_Object}); - if (results.size() == 1) { - return results[0]; - } else { - return QModelIndex(); - } - }(); - - if (new_parent_index.isValid()) { - object_impl_add_objects_to_console(target_console, object_map.values(), new_parent_index); + if (parent_object.isValid()) { + object_impl_add_objects_to_console(target_console, object_map.values(), parent_object); console_object_delete_dn_list(target_console, old_dn_list, object_root, ItemType_Object, ObjectRole_DN); } @@ -1324,19 +1308,13 @@ void console_object_move_and_rename(ConsoleWidget *console, ConsoleWidget *buddy // Apply changes to policy tree const QModelIndex policy_root = get_policy_tree_root(target_console); if (policy_root.isValid()) { - const QModelIndex new_parent_index = [=]() { - const QList results = target_console->search_items(policy_root, PolicyOURole_DN, new_parent_dn, {ItemType_PolicyOU}); + const QModelIndex new_parent_index = target_console->search_item(policy_root, PolicyOURole_DN, new_parent_dn, {ItemType_PolicyOU}); - if (results.size() == 1) { - return results[0]; - } else { - return QModelIndex(); - } - }(); + if (new_parent_index.isValid()) { + policy_ou_impl_add_objects_to_console(target_console, object_map.values(), new_parent_index); - policy_ou_impl_add_objects_to_console(target_console, object_map.values(), new_parent_index); - - console_object_delete_dn_list(target_console, old_dn_list, policy_root, ItemType_PolicyOU, PolicyOURole_DN); + console_object_delete_dn_list(target_console, old_dn_list, policy_root, ItemType_PolicyOU, PolicyOURole_DN); + } } }; diff --git a/src/admc/console_impls/policy_root_impl.cpp b/src/admc/console_impls/policy_root_impl.cpp index 5ee6247e..c4a15607 100644 --- a/src/admc/console_impls/policy_root_impl.cpp +++ b/src/admc/console_impls/policy_root_impl.cpp @@ -91,11 +91,7 @@ void console_policy_tree_init(ConsoleWidget *console) { } QModelIndex get_policy_tree_root(ConsoleWidget *console) { - const QList index_list = console->search_items(QModelIndex(), {ItemType_PolicyRoot}); + const QModelIndex out = console->search_item(QModelIndex(), {ItemType_PolicyRoot}); - if (!index_list.isEmpty()) { - return index_list[0]; - } else { - return QModelIndex(); - } + return out; } diff --git a/src/admc/console_impls/query_folder_impl.cpp b/src/admc/console_impls/query_folder_impl.cpp index d9d87ba7..2650fd5c 100644 --- a/src/admc/console_impls/query_folder_impl.cpp +++ b/src/admc/console_impls/query_folder_impl.cpp @@ -456,13 +456,9 @@ void console_query_tree_save(ConsoleWidget *console) { } QModelIndex get_query_tree_root(ConsoleWidget *console) { - const QList search_results = console->search_items(QModelIndex(), QueryItemRole_IsRoot, true, {ItemType_QueryFolder}); + const QModelIndex out = console->search_item(QModelIndex(), QueryItemRole_IsRoot, true, {ItemType_QueryFolder}); - if (!search_results.isEmpty()) { - return search_results[0]; - } else { - return QModelIndex(); - } + return out; } QList QueryFolderImpl::column_labels() const { diff --git a/src/admc/console_widget/console_widget.cpp b/src/admc/console_widget/console_widget.cpp index b56668c9..6466f6da 100644 --- a/src/admc/console_widget/console_widget.cpp +++ b/src/admc/console_widget/console_widget.cpp @@ -428,6 +428,30 @@ QList ConsoleWidget::search_items(const QModelIndex &parent, const return out; } +QModelIndex ConsoleWidget::search_item(const QModelIndex &parent, int role, const QVariant &value, const QList &type_list) const { + const QList index_list = search_items(parent, role, value, type_list); + + if (index_list.isEmpty()) { + const QModelIndex out = index_list[0]; + + return out; + } else { + return QModelIndex(); + } +} + +QModelIndex ConsoleWidget::search_item(const QModelIndex &parent, const QList &type) const { + const QList index_list = search_items(parent, type); + + if (index_list.isEmpty()) { + const QModelIndex out = index_list[0]; + + return out; + } else { + return QModelIndex(); + } +} + QModelIndex ConsoleWidget::get_current_scope_item() const { const QModelIndex index = d->scope_view->selectionModel()->currentIndex(); diff --git a/src/admc/console_widget/console_widget.h b/src/admc/console_widget/console_widget.h index 1177c7c7..4e1ae6bc 100644 --- a/src/admc/console_widget/console_widget.h +++ b/src/admc/console_widget/console_widget.h @@ -141,6 +141,12 @@ public: QList search_items(const QModelIndex &parent, int role, const QVariant &value, const QList &type = QList()) const; QList search_items(const QModelIndex &parent, const QList &type) const; + // Single index versions of search f-ns. Use when you + // expect only one valid result or none. QModelIndex() + // is returned if no items are found. + QModelIndex search_item(const QModelIndex &parent, int role, const QVariant &value, const QList &type = QList()) const; + QModelIndex search_item(const QModelIndex &parent, const QList &type) const; + QModelIndex get_current_scope_item() const; int get_child_count(const QModelIndex &index) const; diff --git a/src/admc/policy_ou_results_widget.cpp b/src/admc/policy_ou_results_widget.cpp index 5db1170e..40aa640f 100644 --- a/src/admc/policy_ou_results_widget.cpp +++ b/src/admc/policy_ou_results_widget.cpp @@ -263,15 +263,13 @@ void PolicyOUResultsWidget::remove_link() { // Also remove gpo from OU in console const QModelIndex policy_root = get_policy_tree_root(console); - const QList ou_search_results = console->search_items(policy_root, PolicyOURole_DN, ou_dn, {ItemType_PolicyOU}); - if (!ou_search_results.isEmpty()) { - const QModelIndex ou_index = ou_search_results[0]; + const QModelIndex ou_index = console->search_item(policy_root, PolicyOURole_DN, ou_dn, {ItemType_PolicyOU}); + if (ou_index.isValid()) { for (const QString &gpo_dn : gpo_dn_list) { - const QList gpo_search_results = console->search_items(ou_index, PolicyRole_DN, gpo_dn, {ItemType_Policy}); - if (!gpo_search_results.isEmpty()) { - const QModelIndex gpo_index = gpo_search_results[0]; + const QModelIndex gpo_index = console->search_item(ou_index, PolicyRole_DN, gpo_dn, {ItemType_Policy}); + if (!gpo_index.isValid()) { console->delete_item(gpo_index); } }