File indexing completed on 2024-05-12 05:43:31

0001 /*
0002     Copyright (C) 2015 Volker Krause <vkrause@kde.org>
0003 
0004     This program is free software; you can redistribute it and/or modify it
0005     under the terms of the GNU Library General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or (at your
0007     option) any later version.
0008 
0009     This program is distributed in the hope that it will be useful, but WITHOUT
0010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0012     License for more details.
0013 
0014     You should have received a copy of the GNU General Public License
0015     along with this program.  If not, see <https://www.gnu.org/licenses/>.
0016 */
0017 
0018 #include "unuseddependenciesmodel.h"
0019 #include <elf/elffileset.h>
0020 
0021 #include <memory>
0022 
0023 UnusedDependenciesModel::UnusedDependenciesModel(QObject* parent): QAbstractTableModel(parent)
0024 {
0025 }
0026 
0027 UnusedDependenciesModel::~UnusedDependenciesModel() = default;
0028 
0029 void UnusedDependenciesModel::setFileSet(ElfFileSet* fileSet)
0030 {
0031     beginResetModel();
0032     const auto l = [](decltype(this) m) { m->endResetModel(); };
0033     const auto endReset = std::unique_ptr<UnusedDependenciesModel, decltype(l)>(this, l);
0034 
0035     m_fileSet = fileSet;
0036     m_unusedDeps.clear();
0037 }
0038 
0039 void UnusedDependenciesModel::findUnusedDependencies()
0040 {
0041     if (!m_fileSet || !m_unusedDeps.isEmpty())
0042         return;
0043 
0044     beginResetModel();
0045     const auto l = [](decltype(this) m) { m->endResetModel(); };
0046     const auto endReset = std::unique_ptr<UnusedDependenciesModel, decltype(l)>(this, l);
0047 
0048     m_unusedDeps = DependenciesCheck::unusedDependencies(m_fileSet);
0049 }
0050 
0051 QVariant UnusedDependenciesModel::data(const QModelIndex& index, int role) const
0052 {
0053     if (!index.isValid() || !m_fileSet || m_unusedDeps.isEmpty())
0054         return {};
0055 
0056     const auto unusedDep = m_unusedDeps.at(index.row());
0057     switch (role) {
0058         case Qt::DisplayRole:
0059             switch (index.column()) {
0060                 case 0:
0061                     return m_fileSet->file(unusedDep.first)->displayName();
0062                 case 1:
0063                     return m_fileSet->file(unusedDep.second)->displayName();
0064             }
0065             break;
0066     }
0067 
0068     return {};
0069 }
0070 
0071 int UnusedDependenciesModel::columnCount(const QModelIndex& parent) const
0072 {
0073     Q_UNUSED(parent);
0074     return 2;
0075 }
0076 
0077 int UnusedDependenciesModel::rowCount(const QModelIndex& parent) const
0078 {
0079     if (parent.isValid())
0080         return 0;
0081     return m_unusedDeps.size();
0082 }
0083 
0084 QVariant UnusedDependenciesModel::headerData(int section, Qt::Orientation orientation, int role) const
0085 {
0086     if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
0087         switch (section) {
0088             case 0: return tr("ELF File");
0089             case 1: return tr("Unused Dependency");
0090         }
0091     }
0092     return QAbstractItemModel::headerData(section, orientation, role);
0093 }