File indexing completed on 2024-05-12 04:38:54

0001 /*
0002     SPDX-FileCopyrightText: 2007 Andreas Pakulat <apaku@gmx.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "vcsitemeventmodel.h"
0008 
0009 #include <QIcon>
0010 #include <QModelIndex>
0011 #include <QVariant>
0012 #include <QList>
0013 #include <QUrl>
0014 #include <QMimeType>
0015 #include <QMimeDatabase>
0016 
0017 #include <KLocalizedString>
0018 
0019 #include "../vcsrevision.h"
0020 #include "../vcsevent.h"
0021 
0022 namespace KDevelop
0023 {
0024 
0025 VcsItemEventModel::VcsItemEventModel( QObject* parent )
0026     : QStandardItemModel( parent )
0027 {
0028     setColumnCount(2);
0029 }
0030 
0031 VcsItemEventModel::~VcsItemEventModel()
0032 {}
0033 
0034 void VcsItemEventModel::addItemEvents( const QList<KDevelop::VcsItemEvent>& list )
0035 {
0036     if(rowCount()==0)
0037         setColumnCount(2);
0038 
0039     bool copySource = false;
0040     QMimeDatabase mimeDataBase;
0041     for (const KDevelop::VcsItemEvent& ev : list) {
0042 
0043         KDevelop::VcsItemEvent::Actions act = ev.actions();
0044         QStringList actionStrings;
0045         if( act & KDevelop::VcsItemEvent::Added )
0046             actionStrings << i18nc("@item", "Added");
0047         else if( act & KDevelop::VcsItemEvent::Deleted )
0048             actionStrings << i18nc("@item", "Deleted");
0049         else if( act & KDevelop::VcsItemEvent::Modified )
0050             actionStrings << i18nc("@item", "Modified");
0051         else if( act & KDevelop::VcsItemEvent::Copied )
0052             actionStrings << i18nc("@item", "Copied");
0053         else if( act & KDevelop::VcsItemEvent::Replaced )
0054             actionStrings << i18nc("@item", "Replaced");
0055         QUrl repoUrl = QUrl::fromLocalFile(ev.repositoryLocation());
0056         QMimeType mime = repoUrl.isLocalFile()
0057                 ? mimeDataBase.mimeTypeForFile(repoUrl.toLocalFile(), QMimeDatabase::MatchExtension)
0058                 : mimeDataBase.mimeTypeForUrl(repoUrl);
0059         QList<QStandardItem*> rowItems{
0060             new QStandardItem(QIcon::fromTheme(mime.iconName()), ev.repositoryLocation()),
0061             new QStandardItem(actionStrings.join(i18nc("separates an action list", ", "))),
0062         };
0063         QString loc = ev.repositoryCopySourceLocation();
0064         if(!loc.isEmpty()) { //according to the documentation, those are optional. don't force them on the UI
0065             rowItems << new QStandardItem(ev.repositoryCopySourceLocation());
0066             VcsRevision rev = ev.repositoryCopySourceRevision();
0067             if(rev.revisionType()!=VcsRevision::Invalid) {
0068                 rowItems << new QStandardItem(ev.repositoryCopySourceRevision().revisionValue().toString());
0069             }
0070             copySource = true;
0071         }
0072 
0073         rowItems.first()->setData(QVariant::fromValue(ev));
0074         appendRow(rowItems);
0075     }
0076     if(copySource)
0077         setColumnCount(4);
0078 }
0079 
0080 QVariant VcsItemEventModel::headerData(int section, Qt::Orientation orientation, int role) const
0081 {
0082     if(orientation == Qt::Horizontal && role==Qt::DisplayRole) {
0083         switch(section)
0084         {
0085             case 0: return i18nc("@title:column", "Location");
0086             case 1: return i18nc("@title:column", "Actions");
0087             case 2: return i18nc("@title:column", "Source Location");
0088             case 3: return i18nc("@title:column", "Source Revision");
0089         }
0090     }
0091     return QStandardItemModel::headerData(section, orientation, role);
0092 }
0093 
0094 
0095 KDevelop::VcsItemEvent VcsItemEventModel::itemEventForIndex( const QModelIndex& idx ) const
0096 {
0097     return itemFromIndex(idx)->data().value<VcsItemEvent>();
0098 }
0099 
0100 }
0101 
0102 #include "moc_vcsitemeventmodel.cpp"