File indexing completed on 2024-05-19 16:49:48

0001 // SPDX-FileCopyrightText: 2020 Simon Persson <simon.persson@mykolab.com>
0002 //
0003 // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 
0005 #include "mergedvfsmodel.h"
0006 #include "mergedvfs.h"
0007 #include "vfshelpers.h"
0008 
0009 #include <KIO/Global>
0010 
0011 #include <QIcon>
0012 #include <QMimeDatabase>
0013 #include <QMimeType>
0014 #include <QPixmap>
0015 
0016 MergedVfsModel::MergedVfsModel(MergedRepository *pRoot, QObject *pParent) :
0017    QAbstractItemModel(pParent), mRoot(pRoot)
0018 {
0019 }
0020 
0021 MergedVfsModel::~MergedVfsModel() {
0022     delete mRoot;
0023 }
0024 
0025 int MergedVfsModel::columnCount(const QModelIndex &pParent) const {
0026     Q_UNUSED(pParent)
0027     return 1;
0028 }
0029 
0030 QVariant MergedVfsModel::data(const QModelIndex &pIndex, int pRole) const {
0031     if(!pIndex.isValid()) {
0032         return QVariant();
0033     }
0034     auto *lNode = static_cast<MergedNode *>(pIndex.internalPointer());
0035     switch (pRole) {
0036     case Qt::DisplayRole:
0037         return lNode->objectName();
0038     case Qt::DecorationRole: {
0039         QString lIconName = KIO::iconNameForUrl(QUrl::fromLocalFile(lNode->objectName()));
0040         if(lNode->isDirectory()) {
0041             QMimeDatabase db;
0042             lIconName = db.mimeTypeForName(QStringLiteral("inode/directory")).iconName();
0043         }
0044         return QIcon::fromTheme(lIconName);
0045     }
0046     default:
0047         return QVariant();
0048     }
0049 }
0050 
0051 QModelIndex MergedVfsModel::index(int pRow, int pColumn, const QModelIndex &pParent) const {
0052     if(pColumn != 0 || pRow < 0) {
0053         return {}; // invalid
0054     }
0055     if(!pParent.isValid()) {
0056         if(pRow >= mRoot->subNodes().count()) {
0057             return {}; // invalid
0058         }
0059         return createIndex(pRow, 0, mRoot->subNodes().at(pRow));
0060     }
0061     auto lParentNode = static_cast<MergedNode *>(pParent.internalPointer());
0062     if(pRow >= lParentNode->subNodes().count()) {
0063         return {}; // invalid
0064     }
0065     return createIndex(pRow, 0, lParentNode->subNodes().at(pRow));
0066 }
0067 
0068 QModelIndex MergedVfsModel::parent(const QModelIndex &pChild) const {
0069     if(!pChild.isValid()) {
0070         return {};
0071     }
0072     auto lChild = static_cast<MergedNode *>(pChild.internalPointer());
0073     auto lParent = qobject_cast<MergedNode *>(lChild->parent());
0074     if(lParent == nullptr || lParent == mRoot) {
0075         return {}; //invalid
0076     }
0077     auto lGrandParent = qobject_cast<MergedNode *>(lParent->parent());
0078     if(lGrandParent == nullptr) {
0079         return {}; //invalid
0080     }
0081     return createIndex(lGrandParent->subNodes().indexOf(lParent), 0, lParent);
0082 }
0083 
0084 int MergedVfsModel::rowCount(const QModelIndex &pParent) const {
0085     if(!pParent.isValid()) {
0086         return mRoot->subNodes().count();
0087     }
0088     auto lParent = static_cast<MergedNode *>(pParent.internalPointer());
0089     if(lParent == nullptr) {
0090         return 0;
0091     }
0092     return lParent->subNodes().count();
0093 }
0094 
0095 const VersionList *MergedVfsModel::versionList(const QModelIndex &pIndex) {
0096     auto lNode = static_cast<MergedNode *>(pIndex.internalPointer());
0097     return lNode->versionList();
0098 }
0099 
0100 const MergedNode *MergedVfsModel::node(const QModelIndex &pIndex) {
0101     return static_cast<MergedNode *>(pIndex.internalPointer());
0102 }
0103