Skip to content

Commit 3505178

Browse files
Fixed crashed caused by an unauthorized project matching search terms.
1 parent 88ad912 commit 3505178

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

FEBioStudio/RepositoryPanel.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,9 @@ void CRepositoryPanel::GetFileMetaDataForUpload(QVariantList& fileInfoList, QStr
855855

856856
void CRepositoryPanel::SearchDatabase(QString searchTerm)
857857
{
858+
// Here we split the search term into a list of pairs, where the first item in the pair is the data type
859+
// to be searched (e.g. "all", "material", etc.) and the second item is a list of search terms to be
860+
// searched for within that data type.
858861
vector<pair<QString, QStringList>> termList;
859862

860863
QRegularExpression regex("\\w*:.*?(?=(?:\\w*:)|$)");
@@ -926,6 +929,9 @@ void CRepositoryPanel::SearchDatabase(QString searchTerm)
926929
}
927930
}
928931

932+
// This will store the number of matches for each item that is found in the search, and whether the item
933+
// was found by searching the project data or the file data (the bool in the pair is true if the item was
934+
// found by searching the project data and false if it was found by searching the file data)
929935
map<pair<int, bool>,int> IDs;
930936

931937
ui->searchTree->blockSignals(true);
@@ -940,6 +946,11 @@ void CRepositoryPanel::SearchDatabase(QString searchTerm)
940946

941947
map<int,int> itemIDs;
942948

949+
// Search the project data for the current term and update the number of matches for each item
950+
// that is found. If an item is found that was not found in a previous term, we add it to the
951+
// map with the number of matches for the current term. If an item is found that was found in a
952+
// previous term, we update the number of matches for that item by adding the number of matches
953+
// for the current term to the number of matches that were previously found for that item.
943954
for(auto term : terms)
944955
{
945956
set<int> tempIDs = dbHandler->ProjectSearch(dataType, term);
@@ -959,6 +970,9 @@ void CRepositoryPanel::SearchDatabase(QString searchTerm)
959970
}
960971
}
961972

973+
// If this is the first term, we just add all of the items that were found to the map. If this
974+
// is not the first term, then we only keep items that were found in previous terms and in the
975+
// current term, and we update the number of matches for those items.
962976
if(firstTerm)
963977
{
964978
for(auto& item : itemIDs)
@@ -993,6 +1007,8 @@ void CRepositoryPanel::SearchDatabase(QString searchTerm)
9931007
}
9941008
}
9951009

1010+
// Now we do the same thing for the file data, except that if an item is found in the file data,
1011+
// we set the bool in the pair to false
9961012
itemIDs.clear();
9971013

9981014
for(auto term : terms)
@@ -1051,6 +1067,9 @@ void CRepositoryPanel::SearchDatabase(QString searchTerm)
10511067
firstTerm = false;
10521068
}
10531069

1070+
// Now we have a map of all of the items that were found in the search, along with the number of
1071+
// matches for each item and whether the item was found by searching the project data or the file
1072+
//data. We want to sort these items by the number of matches, so we put them in a vector and sort the vector.
10541073
vector<pair<pair<int,bool>,int>> results;
10551074

10561075
for(auto item : IDs)
@@ -1066,15 +1085,21 @@ void CRepositoryPanel::SearchDatabase(QString searchTerm)
10661085
}
10671086
);
10681087

1088+
// Finally, we loop over the sorted items and add them to the search results tree, with the items that have
1089+
// the most matches at the top
10691090
SearchItem* searchItem;
10701091
for(auto item : results)
10711092
{
10721093
if(item.first.second)
10731094
{
1095+
// If a matched item is from an unauthorized project, it won't be in the projectItemsByID map, so we skip it
1096+
if(ui->projectItemsByID.count(item.first.first) == 0) continue;
10741097
searchItem = new SearchItem(ui->projectItemsByID[item.first.first]);
10751098
}
10761099
else
10771100
{
1101+
// If a matched item is from an unauthorized project, it won't be in the fileItemsByID map, so we skip it
1102+
if(ui->fileItemsByID.count(item.first.first) == 0) continue;
10781103
searchItem = new SearchItem(ui->fileItemsByID[item.first.first]);
10791104
}
10801105

0 commit comments

Comments
 (0)