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 //
0008 // Created by hamed on 25.03.22.
0009 //
0010 
0011 #include "remotesmodel.h"
0012 #include "gitmanager.h"
0013 #include "gitremote.h"
0014 
0015 namespace Git
0016 {
0017 
0018 RemotesModel::RemotesModel(Manager *git, QObject *parent)
0019     : AbstractGitItemsModel(git, parent)
0020 {
0021 }
0022 
0023 int RemotesModel::rowCount(const QModelIndex &parent) const
0024 {
0025     Q_UNUSED(parent)
0026     return mData.count();
0027 }
0028 
0029 QVariant RemotesModel::data(const QModelIndex &index, int role) const
0030 {
0031     if (!index.isValid())
0032         return {};
0033 
0034     auto remote = mData.at(index.row());
0035 
0036     switch (role) {
0037     case Name:
0038         return remote->name;
0039     case HeadBranch:
0040         return remote->headBranch;
0041     case FetchUrl:
0042         return remote->fetchUrl;
0043     case PushUrl:
0044         return remote->pushUrl;
0045     }
0046     return {};
0047 }
0048 
0049 Remote *RemotesModel::fromIndex(const QModelIndex &index)
0050 {
0051     if (!index.isValid() || index.row() < 0 || index.row() >= mData.size())
0052         return nullptr;
0053 
0054     return mData.at(index.row());
0055 }
0056 
0057 Remote *RemotesModel::findByName(const QString &name)
0058 {
0059     for (const auto &d : std::as_const(mData))
0060         if (d->name == name)
0061             return d;
0062     return nullptr;
0063 }
0064 
0065 void RemotesModel::rename(const QString &oldName, const QString &newName)
0066 {
0067     mGit->runGit({QStringLiteral("remote"), QStringLiteral("rename"), oldName, newName});
0068     load();
0069 }
0070 
0071 void RemotesModel::setUrl(const QString &remoteName, const QString &newUrl)
0072 {
0073     mGit->runGit({QStringLiteral("remote"), QStringLiteral("set-url"), remoteName, newUrl});
0074     load();
0075 }
0076 
0077 void RemotesModel::fill()
0078 {
0079     qDeleteAll(mData.begin(), mData.end());
0080     mData.clear();
0081 
0082     if (!mGit)
0083         return;
0084 
0085     const auto remotes = mGit->remotes();
0086     for (const auto &remote : remotes) {
0087         auto r = new Remote;
0088         r->name = remote;
0089         auto ret = QString(mGit->runGit({QStringLiteral("remote"), QStringLiteral("show"), remote}));
0090         r->parse(ret);
0091         mData.append(r);
0092     }
0093 }
0094 
0095 }
0096 
0097 
0098 QHash<int, QByteArray> Git::RemotesModel::roleNames() const
0099 {
0100     return {{Name, "name"},
0101         {HeadBranch, "headBranch"},
0102         {FetchUrl, "fetchUrl"},
0103         {PushUrl, "pushUrl"}};
0104 }