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 #ifndef DEPENDENCYMODEL_H
0019 #define DEPENDENCYMODEL_H
0020 
0021 #include <QAbstractItemModel>
0022 #include <QHash>
0023 #include <QVector>
0024 
0025 class ElfFileSet;
0026 class ElfFile;
0027 
0028 /** Model showing full hierarchical dependencies of a file set. */
0029 class DependencyModel : public QAbstractItemModel
0030 {
0031     Q_OBJECT
0032 public:
0033     explicit DependencyModel(QObject* parent = nullptr);
0034     ~DependencyModel();
0035 
0036     enum Role {
0037         UserFileRole = Qt::UserRole + 1,
0038         ProviderFileRole
0039     };
0040 
0041     ElfFileSet* fileSet() const;
0042     void setFileSet(ElfFileSet *fileSet);
0043 
0044     QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const final override;
0045     int columnCount(const QModelIndex& parent = QModelIndex()) const final override;
0046     int rowCount(const QModelIndex& parent = QModelIndex()) const final override;
0047     QModelIndex parent(const QModelIndex& child) const final override;
0048     QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const final override;
0049     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const final override;
0050 
0051 private:
0052     // we use an sequential int for the unique node index, the second have of the QMI internalId is the index of the file
0053     uint64_t makeId(uint32_t id, int32_t fileIndex) const;
0054     int32_t fileIndex(uint64_t qmiId) const;
0055     int32_t fileIndex(const QByteArray &needed) const;
0056     uint32_t nodeId(uint64_t qmiId) const;
0057     bool hasCycle(const QModelIndex &index) const;
0058 
0059     ElfFileSet *m_fileSet = nullptr;
0060     QHash<QByteArray, int32_t> m_fileIndex;
0061     mutable QVector<uint64_t> m_parentMap;
0062     mutable QVector<QVector<uint64_t>> m_childMap;
0063     mutable uint32_t m_uniqueIndex = 0; // 0 is the invisible root
0064     static const int32_t InvalidFile = -1; // marker for dependencies we could not find
0065 
0066     int usedSymbolCount(int parentId, int fileId) const;
0067     mutable QVector<QVector<int>> m_symbolCountTable;
0068 };
0069 
0070 #endif // DEPENDENCYMODEL_H