File indexing completed on 2025-01-19 04:22:44
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 "submodulesmodel.h" 0008 #include "gitmanager.h" 0009 #include "gitsubmodule.h" 0010 #include <KLocalizedString> 0011 0012 namespace Git 0013 { 0014 0015 SubmodulesModel::SubmodulesModel(Git::Manager *git, QObject *parent) 0016 : AbstractGitItemsModel{git, parent} 0017 { 0018 } 0019 0020 int SubmodulesModel::rowCount(const QModelIndex &parent) const 0021 { 0022 Q_UNUSED(parent) 0023 return mData.size(); 0024 } 0025 0026 int SubmodulesModel::columnCount(const QModelIndex &parent) const 0027 { 0028 Q_UNUSED(parent) 0029 return 2; 0030 } 0031 0032 QVariant SubmodulesModel::data(const QModelIndex &index, int role) const 0033 { 0034 if (role != Qt::DisplayRole || !index.isValid() || index.row() < 0 || index.row() >= mData.size()) 0035 return {}; 0036 0037 auto submodule = mData.at(index.row()); 0038 0039 switch (index.column()) { 0040 case 0: 0041 return submodule->path(); 0042 case 1: 0043 return submodule->refName(); 0044 } 0045 return {}; 0046 } 0047 0048 QVariant SubmodulesModel::headerData(int section, Qt::Orientation orientation, int role) const 0049 { 0050 if (orientation != Qt::Horizontal || role != Qt::DisplayRole) 0051 return {}; 0052 0053 switch (section) { 0054 case 0: 0055 return i18n("Path"); 0056 case 1: 0057 return i18n("Head"); 0058 case 2: 0059 return i18n("Status"); 0060 } 0061 0062 return {}; 0063 } 0064 0065 bool SubmodulesModel::append(Submodule *module) 0066 { 0067 beginInsertRows(QModelIndex(), mData.size(), mData.size()); 0068 mData.append(module); 0069 endInsertRows(); 0070 return true; 0071 } 0072 0073 Submodule *SubmodulesModel::fromIndex(const QModelIndex &index) 0074 { 0075 if (!index.isValid() || index.row() < 0 || index.row() >= mData.size()) 0076 return nullptr; 0077 0078 return mData.at(index.row()); 0079 } 0080 0081 void SubmodulesModel::fill() 0082 { 0083 qDeleteAll(mData); 0084 mData.clear(); 0085 const auto modulesList = mGit->readAllNonEmptyOutput({QStringLiteral("submodule"), QStringLiteral("status")}); 0086 for (const auto &line : modulesList) { 0087 auto m = new Submodule; 0088 m->setCommitHash(line.mid(0, 40)); 0089 auto n = line.lastIndexOf(QLatin1Char(' ')); 0090 if (line.count(QLatin1Char(' ')) == 1) 0091 n = line.size(); 0092 m->setPath(line.mid(41, n - 41)); 0093 0094 if (line.count(QLatin1Char(' ')) == 2) 0095 m->setRefName(line.mid(n)); 0096 mData.append(m); 0097 } 0098 } 0099 0100 } // namespace Git