| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2014 Antonis Tsiapaliokas <antonis.tsiapaliokas@kde.org> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 | */ |
| 6 | |
| 7 | #include "queryresultsmodel.h" |
| 8 | #include "query.h" |
| 9 | |
| 10 | #include <QMimeDatabase> |
| 11 | #include <QUrl> |
| 12 | |
| 13 | Query::Query(QObject *parent) |
| 14 | : QObject(parent) |
| 15 | , m_limit(0) |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | Query::~Query() |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | void Query::setSearchString(const QString &searchString) |
| 24 | { |
| 25 | if (m_searchString == searchString) { |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | m_searchString = searchString; |
| 30 | Q_EMIT searchStringChanged(); |
| 31 | } |
| 32 | |
| 33 | QString Query::searchString() const |
| 34 | { |
| 35 | return m_searchString; |
| 36 | } |
| 37 | |
| 38 | void Query::setLimit(const int &limit) |
| 39 | { |
| 40 | if (m_limit == limit) { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | m_limit = limit; |
| 45 | Q_EMIT limitChanged(); |
| 46 | } |
| 47 | |
| 48 | int Query::limit() const |
| 49 | { |
| 50 | return m_limit; |
| 51 | } |
| 52 | |
| 53 | QueryResultsModel::QueryResultsModel(QObject *parent) |
| 54 | : QAbstractListModel(parent), |
| 55 | m_query(new Query(this)) |
| 56 | { |
| 57 | connect(sender: m_query, signal: &Query::searchStringChanged, context: this, slot: &QueryResultsModel::populateModel); |
| 58 | connect(sender: m_query, signal: &Query::limitChanged, context: this, slot: &QueryResultsModel::populateModel); |
| 59 | } |
| 60 | |
| 61 | QueryResultsModel::~QueryResultsModel() |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | QHash<int, QByteArray> QueryResultsModel::roleNames() const |
| 66 | { |
| 67 | QHash<int, QByteArray> roleNames = QAbstractListModel::roleNames(); |
| 68 | roleNames[UrlRole] = "url" ; |
| 69 | |
| 70 | return roleNames; |
| 71 | } |
| 72 | |
| 73 | QVariant QueryResultsModel::data(const QModelIndex &index, int role) const |
| 74 | { |
| 75 | if (!index.isValid()) { |
| 76 | return QVariant(); |
| 77 | } |
| 78 | |
| 79 | switch (role) { |
| 80 | case Qt::DisplayRole: { |
| 81 | const QUrl url = QUrl::fromLocalFile(localfile: m_balooEntryList.at(i: index.row())); |
| 82 | return url.fileName(); |
| 83 | } |
| 84 | case Qt::DecorationRole: { |
| 85 | QString localUrl = m_balooEntryList.at(i: index.row()); |
| 86 | return QMimeDatabase().mimeTypeForFile(fileName: localUrl).iconName(); |
| 87 | } |
| 88 | case UrlRole: |
| 89 | return m_balooEntryList.at(i: index.row()); |
| 90 | default: |
| 91 | return QVariant(); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | int QueryResultsModel::rowCount(const QModelIndex &parent) const |
| 96 | { |
| 97 | if (parent.isValid()) { |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | return m_balooEntryList.count(); |
| 102 | } |
| 103 | |
| 104 | void QueryResultsModel::setQuery(Query *query) |
| 105 | { |
| 106 | if (m_query == query) { |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | delete m_query; |
| 111 | m_query = query; |
| 112 | m_query->setParent(this); |
| 113 | Q_EMIT queryChanged(); |
| 114 | } |
| 115 | |
| 116 | Query* QueryResultsModel::query() const |
| 117 | { |
| 118 | return m_query; |
| 119 | } |
| 120 | |
| 121 | void QueryResultsModel::populateModel() |
| 122 | { |
| 123 | Baloo::Query query; |
| 124 | query.setSearchString(m_query->searchString()); |
| 125 | query.setLimit(m_query->limit()); |
| 126 | Baloo::ResultIterator it = query.exec(); |
| 127 | |
| 128 | beginResetModel(); |
| 129 | m_balooEntryList.clear(); |
| 130 | while (it.next()) { |
| 131 | m_balooEntryList << it.filePath(); |
| 132 | } |
| 133 | endResetModel(); |
| 134 | } |
| 135 | |
| 136 | #include "moc_queryresultsmodel.cpp" |
| 137 | |