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 "stashesmodel.h" 0008 0009 #include "entities/commit.h" 0010 #include "entities/signature.h" 0011 #include "gitmanager.h" 0012 #include <KLocalizedString> 0013 0014 #include <git2/stash.h> 0015 0016 namespace Git 0017 { 0018 0019 StashesModel::StashesModel(Manager *git, QObject *parent) 0020 : AbstractGitItemsModel(git, parent) 0021 { 0022 } 0023 0024 int StashesModel::rowCount(const QModelIndex &parent) const 0025 { 0026 Q_UNUSED(parent) 0027 return mData.size(); 0028 } 0029 0030 int StashesModel::columnCount(const QModelIndex &parent) const 0031 { 0032 Q_UNUSED(parent) 0033 return static_cast<int>(StashesModel::LastColumn) + 1; 0034 } 0035 0036 QVariant StashesModel::data(const QModelIndex &index, int role) const 0037 { 0038 if (role != Qt::DisplayRole || !index.isValid() || index.row() < 0 || index.row() >= mData.size()) 0039 return {}; 0040 0041 auto remote = mData.at(index.row()); 0042 0043 switch (index.column()) { 0044 case Subject: 0045 return remote->subject(); 0046 case CommitHash: 0047 return remote->commitHash(); 0048 case AuthorName: 0049 return remote->commit()->author()->name(); 0050 case AuthorEmail: 0051 return remote->commit()->author()->email(); 0052 case Time: 0053 return remote->commit()->author()->time(); 0054 } 0055 return {}; 0056 } 0057 0058 QVariant StashesModel::headerData(int section, Qt::Orientation orientation, int role) const 0059 { 0060 if (role != Qt::DisplayRole) 0061 return {}; 0062 0063 if (orientation == Qt::Horizontal) 0064 switch (section) { 0065 case Subject: 0066 return i18n("Subject"); 0067 case CommitHash: 0068 return i18n("Commit"); 0069 case AuthorName: 0070 return i18n("Author name"); 0071 case AuthorEmail: 0072 return i18n("Author email"); 0073 case Time: 0074 return i18n("Time"); 0075 } 0076 0077 return {}; 0078 } 0079 0080 QSharedPointer<Stash> StashesModel::fromIndex(const QModelIndex &index) const 0081 { 0082 if (!index.isValid() || index.row() < 0 || index.row() >= mData.size()) 0083 return {}; 0084 0085 return mData.at(index.row()); 0086 } 0087 0088 void StashesModel::fill() 0089 { 0090 mData.clear(); 0091 0092 mGit->forEachStash([this](QSharedPointer<Stash> stash) { 0093 mData << stash; 0094 }); 0095 0096 // const auto list = mGit->readAllNonEmptyOutput({QStringLiteral("stash"), QStringLiteral("list"), QStringLiteral("--format=format:%s%m%an%m%ae%m%aD")}); 0097 // int id{0}; 0098 // for (const auto &item : std::as_const(list)) { 0099 // const auto parts = item.split(QLatin1Char('>')); 0100 // if (parts.size() != 4) 0101 // continue; 0102 0103 // const auto subject = parts.first(); 0104 // auto stash = new Stash(mGit, QStringLiteral("stash@{%1}").arg(id)); 0105 0106 // stash->mSubject = subject; 0107 // stash->mAuthorName = parts.at(1); 0108 // stash->mAuthorEmail = parts.at(2); 0109 // stash->mPushTime = QDateTime::fromString(parts.at(3), Qt::RFC2822Date); 0110 0111 // mData.append(stash); 0112 // id++; 0113 // } 0114 } 0115 } 0116 0117 #include "moc_stashesmodel.cpp"