Debian Codegraph @

src/timeline/TimelineFilter.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 // SPDX-FileCopyrightText: Nheko Contributors
2 //
3 // SPDX-License-Identifier: GPL-3.0-or-later
4
5 #include "TimelineFilter.h"
6
7 #include <QCoreApplication>
8 #include <QEvent>
9
10 #include "Logging.h"
11 #include "TimelineModel.h"
12
13 /// Searching currently can be done incrementally. For that we define a specific role to filter on
14 /// and then process that role in chunk. This is the `FilterRole`. Of course we need to then also
15 /// send proper update signals. Filtering then works as follows:
16 ///
17 /// - At first no range is filtered (incrementalSearchIndex == 0).
18 /// - Then, when filtering is requested, we start posting events to the
19 /// event loop with lower than low priority (low prio - 1). The only thing those events do is
20 /// increment the incrementalSearchIndex and emit a dataChanged for that range of events.
21 /// - This then causes those events to be reevaluated if they should be visible.
22
23 static int FilterRole = Qt::UserRole * 3;
24
25 static QEvent::Type
26 getFilterEventType()
27 {
28 static QEvent::Type t = static_cast<QEvent::Type>(QEvent::registerEventType());
29 return t;
30 }
31
32 TimelineFilter::TimelineFilter(QObject *parent)
33 : QSortFilterProxyModel(parent)
34 {
35 setDynamicSortFilter(true);
36 setFilterRole(FilterRole);
37 }
38
39 void
40 TimelineFilter::startFiltering()
41 {
42 incrementalSearchIndex = 0;
43 emit isFilteringChanged();
44 invalidateFilter();
45 beginResetModel();
46 endResetModel();
47
48 continueFiltering();
49 }
50
51 void
52 TimelineFilter::continueFiltering()
53 {
54 if (auto s = source(); s) {
55 if (s->rowCount() > incrementalSearchIndex) {
56 auto ev = new QEvent(getFilterEventType());
57 // request filtering a new chunk with lower than low priority.
58 QCoreApplication::postEvent(this, ev, Qt::LowEventPriority - 1);
59 } else {
60 // We reached the end, so fetch more!
61 fetchAgain();
62 }
63 }
64 }
65
66 bool
67 TimelineFilter::event(QEvent *ev)
68 {
69 if (ev->type() == getFilterEventType()) {
70 if (incrementalSearchIndex < std::numeric_limits<int>::max()) {
71 int orgIndex = incrementalSearchIndex;
72 // process the next 100 events by claiming their "filterrole" data has changed.
73 incrementalSearchIndex += 100;
74
75 if (auto s = source(); s) {
76 auto count = s->rowCount();
77 if (incrementalSearchIndex >= count) {
78 incrementalSearchIndex = std::numeric_limits<int>::max();
79 }
80 nhlog::ui()->debug("Filter progress {}/{}", incrementalSearchIndex, count);
81 s->dataChanged(s->index(orgIndex),
82 s->index(std::min(incrementalSearchIndex, count - 1)),
83 {FilterRole});
84
85 continueFiltering();
86 }
87 emit isFilteringChanged();
88 }
89 return true;
90 }
91 return QSortFilterProxyModel::event(ev);
92 }
93
94 void
95 TimelineFilter::setThreadId(const QString &t)
96 {
97 nhlog::ui()->debug("Filtering by thread '{}'", t.toStdString());
98 if (this->threadId != t) {
99 this->threadId = t;
100
101 emit threadIdChanged();
102 startFiltering();
103 fetchMore({});
104 }
105 }
106
107 void
108 TimelineFilter::setContentFilter(const QString &c)
109 {
110 nhlog::ui()->debug("Filtering by content '{}'", c.toStdString());
111 if (this->contentFilter != c) {
112 this->contentFilter = c;
113
114 emit contentFilterChanged();
115 startFiltering();
116 fetchMore({});
117 }
118 }
119
120 void
121 TimelineFilter::fetchAgain()
122 {
123 if (threadId.isEmpty() && contentFilter.isEmpty())
124 return;
125
126 if (auto s = source(); s && incrementalSearchIndex == std::numeric_limits<int>::max()) {
127 if (this->rowCount() == cachedCount && s->canFetchMore(QModelIndex()) &&
128 // If we already have the event id of the thread in the timeline and we are filtering by
129 // thread, we can stop fetching more messages In theory an event could have been edited
130 // earlier in the timeline into the thread. So in theory this check is insufficient and
131 // we should instead verify that all events referring to this thread are in the timeline
132 // instead of just the thread root, but only Nheko supports that atm and the check would
133 // be expensive.
134 // TODO(Nico): check that all thread referrencing events are in the timeline by also
135 // checking all edits inside the thread.
136 (threadId.isEmpty() || s->idToIndex(threadId) == -1))
137 s->fetchMore(QModelIndex());
138 else
139 cachedCount = this->rowCount();
140 }
141 }
142
143 void
144 TimelineFilter::sourceDataChanged(const QModelIndex &topLeft,
145 const QModelIndex &bottomRight,
146 const QVector<int> &roles)
147 {
148 if (!roles.contains(TimelineModel::Roles::Body) && !roles.contains(TimelineModel::ThreadId))
149 return;
150
151 if (auto s = source()) {
152 s->dataChanged(topLeft, bottomRight, {FilterRole});
153 }
154 }
155
156 void
157 TimelineFilter::setSource(TimelineModel *s)
158 {
159 if (auto orig = this->source(); orig != s) {
160 cachedCount = 0;
161 incrementalSearchIndex = 0;
162
163 if (orig) {
164 disconnect(orig,
165 &TimelineModel::currentIndexChanged,
166 this,
167 &TimelineFilter::currentIndexChanged);
168 disconnect(orig, &TimelineModel::fetchedMore, this, &TimelineFilter::fetchAgain);
169 disconnect(orig, &TimelineModel::dataChanged, this, &TimelineFilter::sourceDataChanged);
170 }
171
172 this->setSourceModel(s);
173
174 if (s) {
175 connect(
176 s, &TimelineModel::currentIndexChanged, this, &TimelineFilter::currentIndexChanged);
177 connect(s,
178 &TimelineModel::fetchedMore,
179 this,
180 &TimelineFilter::fetchAgain,
181 Qt::QueuedConnection);
182 connect(s,
183 &TimelineModel::dataChanged,
184 this,
185 &TimelineFilter::sourceDataChanged,
186 Qt::QueuedConnection);
187 }
188
189 // reset the search index a second time just to be safe.
190 incrementalSearchIndex = 0;
191
192 emit sourceChanged();
193 emit isFilteringChanged();
194 invalidateFilter();
195 }
196 }
197
198 TimelineModel *
199 TimelineFilter::source() const
200 {
201 return qobject_cast<TimelineModel *>(sourceModel());
202 }
203
204 void
205 TimelineFilter::setCurrentIndex(int idx)
206 {
207 // TODO: maybe send read receipt in thread timeline? Or not at all?
208 if (auto s = source()) {
209 s->setCurrentIndex(this->mapToSource(index(idx, 0)).row());
210 }
211 }
212
213 int
214 TimelineFilter::currentIndex() const
215 {
216 if (auto s = source())
217 return this->mapFromSource(s->index(s->currentIndex())).row();
218 else
219 return -1;
220 }
221
222 bool
223 TimelineFilter::isFiltering() const
224 {
225 return incrementalSearchIndex != std::numeric_limits<int>::max() &&
226 !(threadId.isEmpty() && contentFilter.isEmpty());
227 }
228
229 bool
230 TimelineFilter::filterAcceptsRow(int source_row, const QModelIndex &) const
231 {
232 // this chunk is still unfiltered.
233 if (source_row > incrementalSearchIndex)
234 return false;
235
236 if (threadId.isEmpty() && contentFilter.isEmpty())
237 return true;
238
239 if (auto s = sourceModel()) {
240 auto idx = s->index(source_row, 0);
241 if (!contentFilter.isEmpty() && !s->data(idx, TimelineModel::Body)
242 .toString()
243 .contains(contentFilter, Qt::CaseInsensitive)) {
244 return false;
245 }
246
247 if (threadId.isEmpty())
248 return true;
249
250 return s->data(idx, TimelineModel::EventId) == threadId ||
251 s->data(idx, TimelineModel::ThreadId) == threadId;
252 } else {
253 return true;
254 }
255 }
256
257 #include "moc_TimelineFilter.cpp"