File indexing completed on 2025-01-19 05:11:30

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 "entities/submodule.h"
0009 #include "gitmanager.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     beginResetModel();
0084     qDeleteAll(mData);
0085     mData.clear();
0086     auto append = [this](Submodule *s) {
0087         mData << s;
0088     };
0089     mGit->forEachSubmodules(append);
0090     endResetModel();
0091 
0092     //    const auto modulesList = mGit->readAllNonEmptyOutput({QStringLiteral("submodule"), QStringLiteral("status")});
0093     //    for (const auto &line : modulesList) {
0094     //        auto m = new Submodule;
0095     //        m->setCommitHash(line.mid(0, 40));
0096     //        auto n = line.lastIndexOf(QLatin1Char(' '));
0097     //        if (line.count(QLatin1Char(' ')) == 1)
0098     //            n = line.size();
0099     //        m->setPath(line.mid(41, n - 41));
0100 
0101     //        if (line.count(QLatin1Char(' ')) == 2)
0102     //            m->setRefName(line.mid(n));
0103     //        mData.append(m);
0104     //    }
0105 }
0106 
0107 } // namespace Git
0108 
0109 #include "moc_submodulesmodel.cpp"