File indexing completed on 2025-01-05 05:14:50
0001 /* 0002 SPDX-FileCopyrightText: 2021 Hamed Masafi <hamed.masfi@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-3.0-or-later 0005 */ 0006 0007 #include "searchdialog.h" 0008 #include "fileviewerdialog.h" 0009 0010 #include <KLocalizedString> 0011 #include <QStandardItemModel> 0012 #include <QtConcurrent> 0013 #include <entities/commit.h> 0014 #include <gitmanager.h> 0015 0016 SearchDialog::SearchDialog(const QString &path, Git::Manager *git, QWidget *parent) 0017 : AppDialog(git, parent) 0018 { 0019 setupUi(this); 0020 initModel(); 0021 lineEditPath->setText(path); 0022 0023 connect(pushButtonSearch, &QPushButton::clicked, this, &SearchDialog::slotPushButtonSearchClicked); 0024 connect(treeView, &QTreeView::doubleClicked, this, &SearchDialog::slotTreeViewDoubleClicked); 0025 } 0026 0027 void SearchDialog::initModel() 0028 { 0029 if (!mModel) { 0030 mModel = new QStandardItemModel(this); 0031 treeView->setModel(mModel); 0032 } 0033 0034 mModel->setColumnCount(3); 0035 mModel->setHeaderData(0, Qt::Horizontal, i18n("File name")); 0036 mModel->setHeaderData(1, Qt::Horizontal, i18n("Branch")); 0037 mModel->setHeaderData(2, Qt::Horizontal, i18n("Commit")); 0038 } 0039 0040 SearchDialog::SearchDialog(Git::Manager *git, QWidget *parent) 0041 : AppDialog(git, parent) 0042 { 0043 setupUi(this); 0044 initModel(); 0045 0046 connect(pushButtonSearch, &QPushButton::clicked, this, &SearchDialog::slotPushButtonSearchClicked); 0047 connect(treeView, &QTreeView::doubleClicked, this, &SearchDialog::slotTreeViewDoubleClicked); 0048 } 0049 0050 void SearchDialog::slotPushButtonSearchClicked() 0051 { 0052 mModel->clear(); 0053 initModel(); 0054 startTimer(500); 0055 pushButtonSearch->setEnabled(false); 0056 mProgress.total = mProgress.value = 0; 0057 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0058 auto f = QtConcurrent::run(this, &SearchDialog::beginSearch); 0059 #else 0060 auto f = QtConcurrent::run(&SearchDialog::beginSearch, this); 0061 #endif 0062 } 0063 0064 void SearchDialog::slotTreeViewDoubleClicked(const QModelIndex &index) 0065 { 0066 if (!index.isValid()) 0067 return; 0068 const auto file = mModel->data(mModel->index(index.row(), 0)).toString(); 0069 const auto branch = mModel->data(mModel->index(index.row(), 1)).toString(); 0070 const auto commit = mModel->data(mModel->index(index.row(), 2)).toString(); 0071 0072 QString place; 0073 if (!commit.isEmpty() && !branch.isEmpty()) 0074 place = branch + QLatin1Char(':') + commit; 0075 else 0076 place = branch.isEmpty() ? commit : branch; 0077 0078 auto d = new FileViewerDialog(mGit, place, file); 0079 d->setWindowModality(Qt::ApplicationModal); 0080 d->setAttribute(Qt::WA_DeleteOnClose, true); 0081 d->show(); 0082 } 0083 0084 void SearchDialog::beginSearch() 0085 { 0086 if (radioButtonSearchBranches->isChecked()) { 0087 const auto branchesList = mGit->branchesNames(Git::Manager::BranchType::LocalBranch); 0088 mProgress.total = branchesList.size(); 0089 for (const auto &branch : branchesList) { 0090 searchOnPlace(branch, QString()); 0091 mProgress.value++; 0092 } 0093 } else { 0094 Git::LogList list; 0095 list.load(mGit); 0096 0097 mProgress.total = list.size(); 0098 for (const auto &branch : std::as_const(list)) { 0099 searchOnPlace(QString(), branch->commitHash()); 0100 mProgress.value++; 0101 } 0102 } 0103 0104 pushButtonSearch->setEnabled(true); 0105 } 0106 0107 void SearchDialog::searchOnPlace(const QString &branch, const QString &commit) 0108 { 0109 const QString place = branch.isEmpty() ? commit : branch; 0110 const auto files = mGit->ls(place); 0111 0112 for (const auto &file : std::as_const(files)) { 0113 if (!lineEditPath->text().isEmpty() && !file.contains(lineEditPath->text())) 0114 continue; 0115 0116 bool ok = mGit->fileContent(place, file).contains(lineEditText->text(), checkBoxCaseSensetive->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive); 0117 if (ok) { 0118 mModel->appendRow({new QStandardItem(file), new QStandardItem(branch), new QStandardItem(commit)}); 0119 } 0120 } 0121 } 0122 0123 void SearchDialog::timerEvent(QTimerEvent *event) 0124 { 0125 Q_UNUSED(event) 0126 progressBar->setMaximum(mProgress.total); 0127 progressBar->setValue(mProgress.value); 0128 } 0129 0130 #include "moc_searchdialog.cpp"