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