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 "fileusermodel.h"
0019 
0020 #include <elf/elffileset.h>
0021 
0022 
0023 FileUserModel::FileUserModel(QObject* parent): QAbstractListModel(parent)
0024 {
0025 }
0026 
0027 FileUserModel::~FileUserModel() = default;
0028 
0029 void FileUserModel::setFile(ElfFileSet* fileSet, ElfFile *usedFile)
0030 {
0031     beginResetModel();
0032     const auto l = [](FileUserModel* m) { m->endResetModel(); };
0033     const auto endReset = std::unique_ptr<FileUserModel, decltype(l)>(this, l);
0034 
0035     m_fileSet = fileSet;
0036     m_usedFile = usedFile;
0037     m_users.clear();
0038     if (!m_fileSet || !m_usedFile)
0039         return;
0040 
0041     if (!usedFile->dynamicSection())
0042         return;
0043     auto soName = usedFile->dynamicSection()->soName();
0044     if (soName.isEmpty())
0045         soName = usedFile->fileName().toUtf8();
0046 
0047     for (int i = 0; i < fileSet->size(); ++i) {
0048         const auto file = fileSet->file(i);
0049         if (!file->dynamicSection())
0050             continue;
0051         foreach (const auto &dep, file->dynamicSection()->neededLibraries()) {
0052             if (dep == soName)
0053                 m_users.push_back(i);
0054         }
0055     }
0056 }
0057 
0058 ElfFile* FileUserModel::usedFile() const
0059 {
0060     return m_usedFile;
0061 }
0062 
0063 QVariant FileUserModel::data(const QModelIndex& index, int role) const
0064 {
0065     if (!index.isValid() || !m_fileSet)
0066         return {};
0067 
0068     switch (role) {
0069         case Qt::DisplayRole:
0070             return m_fileSet->file(m_users.at(index.row()))->displayName();
0071         case FileRole:
0072             return QVariant::fromValue(m_fileSet->file(m_users.at(index.row())));
0073     }
0074 
0075     return {};
0076 }
0077 
0078 int FileUserModel::rowCount(const QModelIndex& parent) const
0079 {
0080     if (parent.isValid() || !m_fileSet)
0081         return 0;
0082 
0083     return m_users.size();
0084 }
0085 
0086 QVariant FileUserModel::headerData(int section, Qt::Orientation orientation, int role) const
0087 {
0088     if (role == Qt::DisplayRole && orientation == Qt::Horizontal)
0089         return tr("Users");
0090     return QAbstractItemModel::headerData(section, orientation, role);
0091 }