diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a5cb7d534..a93cea8c6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -324,6 +324,15 @@ ecm_qt_declare_logging_category(neochat EXPORT NEOCHAT ) +ecm_qt_declare_logging_category(neochat + HEADER "publicroomlist_logging.h" + IDENTIFIER "PublicRoomList" + CATEGORY_NAME "org.kde.neochat.publicroomlistmodel" + DESCRIPTION "Neochat: publicroomlistmodel" + DEFAULT_SEVERITY Info + EXPORT NEOCHAT +) + ecm_qt_declare_logging_category(neochat HEADER "eventhandler_logging.h" IDENTIFIER "EventHandling" diff --git a/src/models/publicroomlistmodel.cpp b/src/models/publicroomlistmodel.cpp index ad41d0315..d0dba9030 100644 --- a/src/models/publicroomlistmodel.cpp +++ b/src/models/publicroomlistmodel.cpp @@ -5,6 +5,8 @@ #include +#include "publicroomlist_logging.h" + using namespace Quotient; PublicRoomListModel::PublicRoomListModel(QObject *parent) @@ -70,7 +72,6 @@ void PublicRoomListModel::setServer(const QString &value) nextBatch = QString(); attempted = false; rooms.clear(); - Q_EMIT searchingChanged(); endResetModel(); @@ -99,26 +100,16 @@ void PublicRoomListModel::setSearchText(const QString &value) } m_searchText = value; - - beginResetModel(); + Q_EMIT searchTextChanged(); nextBatch = QString(); attempted = false; - rooms.clear(); - - endResetModel(); if (job) { job->abandon(); job = nullptr; Q_EMIT searchingChanged(); } - - if (m_connection) { - next(); - } - - Q_EMIT searchTextChanged(); } bool PublicRoomListModel::showOnlySpaces() const @@ -135,15 +126,28 @@ void PublicRoomListModel::setShowOnlySpaces(bool showOnlySpaces) Q_EMIT showOnlySpacesChanged(); } -void PublicRoomListModel::next(int count) +void PublicRoomListModel::search(int limit) { - if (count < 1) { + if (limit < 1 || attempted) { return; } if (job) { - qDebug() << "PublicRoomListModel: Other jobs running, ignore"; + qCDebug(PublicRoomList) << "Other job running, ignore"; + return; + } + next(limit); +} + +void PublicRoomListModel::next(int limit) +{ + if (m_connection == nullptr || limit < 1) { + return; + } + + if (job) { + qCDebug(PublicRoomList) << "Other job running, ignore"; return; } @@ -151,11 +155,17 @@ void PublicRoomListModel::next(int count) if (m_showOnlySpaces) { roomTypes += QLatin1String("m.space"); } - job = m_connection->callApi(m_server, count, nextBatch, QueryPublicRoomsJob::Filter{m_searchText, roomTypes}); + job = m_connection->callApi(m_server, limit, nextBatch, QueryPublicRoomsJob::Filter{m_searchText, roomTypes}); Q_EMIT searchingChanged(); connect(job, &BaseJob::finished, this, [this] { - attempted = true; + if (!attempted) { + beginResetModel(); + rooms.clear(); + endResetModel(); + + attempted = true; + } if (job->status() == BaseJob::Success) { nextBatch = job->nextBatch(); @@ -177,8 +187,7 @@ QVariant PublicRoomListModel::data(const QModelIndex &index, int role) const } if (index.row() >= rooms.count()) { - qDebug() << "PublicRoomListModel, something's wrong: index.row() >= " - "rooms.count()"; + qCDebug(PublicRoomList) << "something's wrong: index.row() >= rooms.count()"; return {}; } auto room = rooms.at(index.row()); @@ -267,7 +276,7 @@ int PublicRoomListModel::rowCount(const QModelIndex &parent) const bool PublicRoomListModel::canFetchMore(const QModelIndex &parent) const { Q_UNUSED(parent) - return !(attempted && nextBatch.isEmpty()); + return !nextBatch.isEmpty(); } void PublicRoomListModel::fetchMore(const QModelIndex &parent) diff --git a/src/models/publicroomlistmodel.h b/src/models/publicroomlistmodel.h index 3e50c20d3..e2c81ea10 100644 --- a/src/models/publicroomlistmodel.h +++ b/src/models/publicroomlistmodel.h @@ -108,6 +108,13 @@ public: [[nodiscard]] bool searching() const; + /** + * @brief Search the room directory. + * + * @param limit the maximum number of rooms to load. + */ + Q_INVOKABLE void search(int limit = 50); + private: QPointer m_connection = nullptr; QString m_server; @@ -117,9 +124,9 @@ private: /** * @brief Load the next set of rooms. * - * @param count the maximum number of rooms to load. + * @param limit the maximum number of rooms to load. */ - void next(int count = 50); + void next(int limit = 50); bool canFetchMore(const QModelIndex &parent) const override; void fetchMore(const QModelIndex &parent) override; diff --git a/src/models/userdirectorylistmodel.cpp b/src/models/userdirectorylistmodel.cpp index 6226a6492..6bbf78405 100644 --- a/src/models/userdirectorylistmodel.cpp +++ b/src/models/userdirectorylistmodel.cpp @@ -59,6 +59,12 @@ void UserDirectoryListModel::setSearchText(const QString &value) m_searchText = value; Q_EMIT searchTextChanged(); + if (m_job) { + m_job->abandon(); + m_job = nullptr; + Q_EMIT searchingChanged(); + } + attempted = false; } diff --git a/src/qml/SearchPage.qml b/src/qml/SearchPage.qml index 24595fba7..a908f3151 100644 --- a/src/qml/SearchPage.qml +++ b/src/qml/SearchPage.qml @@ -110,6 +110,7 @@ Kirigami.ScrollablePage { Keys.onEnterPressed: searchButton.clicked() Keys.onReturnPressed: searchButton.clicked() onTextChanged: { + searchTimer.restart() if (model) { model.searchText = text; } @@ -124,6 +125,14 @@ Kirigami.ScrollablePage { } } } + Timer { + id: searchTimer + interval: 500 + running: true + onTriggered: if (typeof model.search === 'function') { + model.search() + } + } } } @@ -149,7 +158,7 @@ Kirigami.ScrollablePage { Kirigami.LoadingPlaceholder { anchors.centerIn: parent - visible: root.model.searching + visible: searchField.text.length > 0 && listView.count === 0 && root.model.searching } } }