File indexing completed on 2024-06-16 08:29:44

0001 // SPDX-FileCopyrightText: 2020 Simon Persson <simon.persson@mykolab.com>
0002 //
0003 // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 
0005 #include "versionlistmodel.h"
0006 #include "vfshelpers.h"
0007 
0008 #include <KFormat>
0009 #include <KLocalizedString>
0010 
0011 #include <QDateTime>
0012 #include <QLocale>
0013 #include <QMimeDatabase>
0014 #include <QMimeType>
0015 
0016 VersionListModel::VersionListModel(QObject *parent) :
0017    QAbstractListModel(parent)
0018 {
0019     mVersionList = nullptr;
0020 }
0021 
0022 void VersionListModel::setNode(const MergedNode *pNode) {
0023     beginResetModel();
0024     mNode = pNode;
0025     mVersionList = mNode->versionList();
0026     endResetModel();
0027 }
0028 
0029 int VersionListModel::rowCount(const QModelIndex &pParent) const {
0030     Q_UNUSED(pParent)
0031     if(mVersionList != nullptr) {
0032         return mVersionList->count();
0033     }
0034     return 0;
0035 }
0036 
0037 QVariant VersionListModel::data(const QModelIndex &pIndex, int pRole) const {
0038     if(!pIndex.isValid() || mVersionList == nullptr) {
0039         return QVariant();
0040     }
0041     QMimeDatabase db;
0042     KFormat lFormat;
0043     VersionData *lData = mVersionList->at(pIndex.row());
0044     switch (pRole) {
0045     case Qt::DisplayRole:
0046         return lFormat.formatRelativeDateTime(QDateTime::fromSecsSinceEpoch(static_cast<qint64>(lData->mModifiedDate)), QLocale::ShortFormat);
0047     case VersionBupUrlRole: {
0048         QUrl lUrl;
0049         mNode->getBupUrl(pIndex.row(), &lUrl);
0050         return lUrl;
0051     }
0052     case VersionMimeTypeRole:
0053         if(mNode->isDirectory()) {
0054             return QString(QStringLiteral("inode/directory"));
0055         }
0056         return db.mimeTypeForFile(mNode->objectName(), QMimeDatabase::MatchExtension).name();
0057     case VersionSizeRole:
0058         return lData->size();
0059     case VersionSourceInfoRole: {
0060         BupSourceInfo lSourceInfo;
0061         mNode->getBupUrl(pIndex.row(), &lSourceInfo.mBupKioPath, &lSourceInfo.mRepoPath, &lSourceInfo.mBranchName,
0062                          &lSourceInfo.mCommitTime, &lSourceInfo.mPathInRepo);
0063         lSourceInfo.mIsDirectory = mNode->isDirectory();
0064         lSourceInfo.mSize = lData->size();
0065         return QVariant::fromValue<BupSourceInfo>(lSourceInfo);
0066     }
0067     case VersionIsDirectoryRole:
0068         return mNode->isDirectory();
0069     default:
0070         return QVariant();
0071     }
0072 }