File indexing completed on 2025-01-26 05:06:22

0001 /*
0002     SPDX-FileCopyrightText: 2014 Eike Hein <hein@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "placesmodel.h"
0008 
0009 #include <QStandardPaths>
0010 
0011 #include <KFilePlacesModel>
0012 #include <KPluginMetaData>
0013 
0014 PlacesModel::PlacesModel(QObject *parent)
0015     : QSortFilterProxyModel(parent)
0016     , m_sourceModel(new KFilePlacesModel(this))
0017 {
0018 
0019     connect(m_sourceModel, &KFilePlacesModel::rowsInserted, this, &PlacesModel::placesChanged);
0020     connect(m_sourceModel, &KFilePlacesModel::rowsRemoved, this, &PlacesModel::placesChanged);
0021 
0022     setSourceModel(m_sourceModel);
0023 
0024     setDynamicSortFilter(true);
0025 }
0026 
0027 PlacesModel::~PlacesModel()
0028 {
0029 }
0030 
0031 QHash<int, QByteArray> PlacesModel::roleNames() const
0032 {
0033     QHash<int, QByteArray> roleNames = QSortFilterProxyModel::roleNames();
0034     roleNames[Qt::DisplayRole] = "display";
0035     roleNames[Qt::DecorationRole] = "decoration";
0036     return roleNames;
0037 }
0038 
0039 bool PlacesModel::activityLinkingEnabled() const
0040 {
0041     KPluginMetaData plugin = KPluginMetaData::findPluginById(QLatin1String("kf6/kfileitemaction"), QStringLiteral("kactivitymanagerd_fileitem_linking_plugin"));
0042     return plugin.isValid();
0043 }
0044 
0045 bool PlacesModel::showDesktopEntry() const
0046 {
0047     return m_showDesktopEntry;
0048 }
0049 
0050 void PlacesModel::setShowDesktopEntry(bool showDesktopEntry)
0051 {
0052     if (m_showDesktopEntry != showDesktopEntry) {
0053         m_showDesktopEntry = showDesktopEntry;
0054 
0055         invalidateFilter();
0056 
0057         Q_EMIT showDesktopEntryChanged();
0058     }
0059 }
0060 
0061 QString PlacesModel::urlForIndex(int idx) const
0062 {
0063     return m_sourceModel->url(mapToSource(index(idx, 0))).toString();
0064 }
0065 
0066 int PlacesModel::indexForUrl(const QString &url) const
0067 {
0068     QUrl _url(url);
0069     QModelIndex idx;
0070 
0071     for (int i = 0; i < rowCount(); i++) {
0072         if (_url == m_sourceModel->url(mapToSource(index(i, 0)))) {
0073             idx = index(i, 0);
0074             break;
0075         }
0076     }
0077 
0078     if (idx.isValid()) {
0079         return idx.row();
0080     }
0081 
0082     return -1;
0083 }
0084 
0085 bool PlacesModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
0086 {
0087     const QModelIndex index = m_sourceModel->index(sourceRow, 0, sourceParent);
0088 
0089     if (!m_showDesktopEntry) {
0090         const QUrl url = index.data(KFilePlacesModel::UrlRole).toUrl();
0091         const QUrl desktopUrl = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation));
0092         if (url == desktopUrl) {
0093             return false;
0094         }
0095     }
0096 
0097     return !m_sourceModel->isHidden(index);
0098 }