Baloo

queryresultsmodel.cpp
1 /*
2  SPDX-FileCopyrightText: 2014 Antonis Tsiapaliokas <[email protected]>
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(m_query, &Query::searchStringChanged, this, &QueryResultsModel::populateModel);
58  connect(m_query, &Query::limitChanged, this, &QueryResultsModel::populateModel);
59 }
60 
61 QueryResultsModel::~QueryResultsModel()
62 {
63 }
64 
65 QHash<int, QByteArray> QueryResultsModel::roleNames() const
66 {
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(m_balooEntryList.at(index.row()));
82  return url.fileName();
83  }
84  case Qt::DecorationRole: {
85  QString localUrl = m_balooEntryList.at(index.row());
86  return QMimeDatabase().mimeTypeForFile(localUrl).iconName();
87  }
88  case UrlRole:
89  return m_balooEntryList.at(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 {
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"
std::optional< QSqlQuery > query(const QString &queryStatement)
DisplayRole
void setSearchString(const QString &str)
Set some text which should be used to search for Items.
virtual QHash< int, QByteArray > roleNames() const const
QUrl fromLocalFile(const QString &localFile)
QString fileName(QUrl::ComponentFormattingOptions options) const const
bool isValid() const const
int row() const const
const QChar at(int position) const const
QMimeType mimeTypeForFile(const QString &fileName, QMimeDatabase::MatchMode mode) const const
void setLimit(uint limit)
Only a maximum of limit results will be returned.
Definition: query.cpp:102
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Nov 29 2023 03:56:26 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.