File indexing completed on 2024-04-14 03:49:50

0001 /*
0002     SPDX-FileCopyrightText: 2014 Antonis Tsiapaliokas <antonis.tsiapaliokas@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "queryresultsmodel.h"
0008 #include "query.h"
0009 
0010 #include <QMimeDatabase>
0011 #include <QUrl>
0012 
0013 Query::Query(QObject *parent)
0014     : QObject(parent)
0015     , m_limit(0)
0016 {
0017 }
0018 
0019 Query::~Query()
0020 {
0021 }
0022 
0023 void Query::setSearchString(const QString &searchString)
0024 {
0025     if (m_searchString == searchString) {
0026         return;
0027     }
0028 
0029     m_searchString = searchString;
0030     Q_EMIT searchStringChanged();
0031 }
0032 
0033 QString Query::searchString() const
0034 {
0035     return m_searchString;
0036 }
0037 
0038 void Query::setLimit(const int &limit)
0039 {
0040     if (m_limit == limit) {
0041         return;
0042     }
0043 
0044     m_limit = limit;
0045     Q_EMIT limitChanged();
0046 }
0047 
0048 int Query::limit() const
0049 {
0050     return m_limit;
0051 }
0052 
0053 QueryResultsModel::QueryResultsModel(QObject *parent)
0054     : QAbstractListModel(parent),
0055       m_query(new Query(this))
0056 {
0057     connect(m_query, &Query::searchStringChanged, this, &QueryResultsModel::populateModel);
0058     connect(m_query, &Query::limitChanged, this, &QueryResultsModel::populateModel);
0059 }
0060 
0061 QueryResultsModel::~QueryResultsModel()
0062 {
0063 }
0064 
0065 QHash<int, QByteArray> QueryResultsModel::roleNames() const
0066 {
0067     QHash<int, QByteArray> roleNames = QAbstractListModel::roleNames();
0068     roleNames[UrlRole] = "url";
0069 
0070     return roleNames;
0071 }
0072 
0073 QVariant QueryResultsModel::data(const QModelIndex &index, int role) const
0074 {
0075     if (!index.isValid()) {
0076         return QVariant();
0077     }
0078 
0079     switch (role) {
0080     case Qt::DisplayRole: {
0081         const QUrl url = QUrl::fromLocalFile(m_balooEntryList.at(index.row()));
0082         return url.fileName();
0083     }
0084     case Qt::DecorationRole: {
0085         QString localUrl = m_balooEntryList.at(index.row());
0086         return QMimeDatabase().mimeTypeForFile(localUrl).iconName();
0087     }
0088     case UrlRole:
0089         return m_balooEntryList.at(index.row());
0090     default:
0091         return QVariant();
0092     }
0093 }
0094 
0095 int QueryResultsModel::rowCount(const QModelIndex &parent) const
0096 {
0097     if (parent.isValid()) {
0098         return 0;
0099     }
0100 
0101     return m_balooEntryList.count();
0102 }
0103 
0104 void QueryResultsModel::setQuery(Query *query)
0105 {
0106     if (m_query == query) {
0107         return;
0108     }
0109 
0110     delete m_query;
0111     m_query = query;
0112     m_query->setParent(this);
0113     Q_EMIT queryChanged();
0114 }
0115 
0116 Query* QueryResultsModel::query() const
0117 {
0118     return m_query;
0119 }
0120 
0121 void QueryResultsModel::populateModel()
0122 {
0123     Baloo::Query query;
0124     query.setSearchString(m_query->searchString());
0125     query.setLimit(m_query->limit());
0126     Baloo::ResultIterator it = query.exec();
0127 
0128     beginResetModel();
0129     m_balooEntryList.clear();
0130     while (it.next()) {
0131         m_balooEntryList << it.filePath();
0132     }
0133     endResetModel();
0134 }
0135 
0136 #include "moc_queryresultsmodel.cpp"