diff options
author | alex-z <blackslayer4@gmail.com> | 2021-09-09 14:33:57 +0300 |
---|---|---|
committer | alex-z <blackslayer4@gmail.com> | 2021-10-25 14:45:05 +0300 |
commit | c1dab7e4cb254bf492b458c8c0c1bab8f55b17b0 (patch) | |
tree | 66681ba48f072166942867ee9021a7934d633e6f /src/gui/tray/unifiedsearchresultslistmodel.h | |
parent | b8e2dc24f3c1d67424dd363af85f9311a702dd78 (diff) |
Unified Search via Tray window
Signed-off-by: alex-z <blackslayer4@gmail.com>
Diffstat (limited to 'src/gui/tray/unifiedsearchresultslistmodel.h')
-rw-r--r-- | src/gui/tray/unifiedsearchresultslistmodel.h | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/src/gui/tray/unifiedsearchresultslistmodel.h b/src/gui/tray/unifiedsearchresultslistmodel.h new file mode 100644 index 000000000..5ae811f20 --- /dev/null +++ b/src/gui/tray/unifiedsearchresultslistmodel.h @@ -0,0 +1,129 @@ +/* + * Copyright (C) by Oleksandr Zolotov <alex@nextcloud.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#pragma once + +#include "unifiedsearchresult.h" + +#include <limits> + +#include <QtCore> + +namespace OCC { +class AccountState; + +/** + * @brief The UnifiedSearchResultsListModel + * @ingroup gui + * Simple list model to provide the list view with data for the Unified Search results. + */ + +class UnifiedSearchResultsListModel : public QAbstractListModel +{ + Q_OBJECT + + Q_PROPERTY(bool isSearchInProgress READ isSearchInProgress NOTIFY isSearchInProgressChanged) + Q_PROPERTY(QString currentFetchMoreInProgressProviderId READ currentFetchMoreInProgressProviderId NOTIFY + currentFetchMoreInProgressProviderIdChanged) + Q_PROPERTY(QString errorString READ errorString NOTIFY errorStringChanged) + Q_PROPERTY(QString searchTerm READ searchTerm WRITE setSearchTerm NOTIFY searchTermChanged) + + struct UnifiedSearchProvider + { + QString _id; + QString _name; + qint32 _cursor = -1; // current pagination value + qint32 _pageSize = -1; // how many max items per step of pagination + bool _isPaginated = false; + qint32 _order = std::numeric_limits<qint32>::max(); // sorting order (smaller number has bigger priority) + }; + +public: + enum DataRole { + ProviderNameRole = Qt::UserRole + 1, + ProviderIdRole, + ImagePlaceholderRole, + IconsRole, + TitleRole, + SublineRole, + ResourceUrlRole, + RoundedRole, + TypeRole, + TypeAsStringRole, + }; + + explicit UnifiedSearchResultsListModel(AccountState *accountState, QObject *parent = nullptr); + + QVariant data(const QModelIndex &index, int role) const override; + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + + bool isSearchInProgress() const; + + QString currentFetchMoreInProgressProviderId() const; + QString searchTerm() const; + QString errorString() const; + + Q_INVOKABLE void resultClicked(const QString &providerId, const QUrl &resourceUrl) const; + Q_INVOKABLE void fetchMoreTriggerClicked(const QString &providerId); + + QHash<int, QByteArray> roleNames() const override; + +private: + void startSearch(); + void startSearchForProvider(const QString &providerId, qint32 cursor = -1); + + void parseResultsForProvider(const QJsonObject &data, const QString &providerId, bool fetchedMore = false); + + // append initial search results to the list + void appendResults(QVector<UnifiedSearchResult> results, const UnifiedSearchProvider &provider); + + // append pagination results to existing results from the initial search + void appendResultsToProvider(const QVector<UnifiedSearchResult> &results, const UnifiedSearchProvider &provider); + + void removeFetchMoreTrigger(const QString &providerId); + + void disconnectAndClearSearchJobs(); + + void clearCurrentFetchMoreInProgressProviderId(); + +signals: + void currentFetchMoreInProgressProviderIdChanged(); + void isSearchInProgressChanged(); + void errorStringChanged(); + void searchTermChanged(); + +public slots: + void setSearchTerm(const QString &term); + +private slots: + void slotSearchTermEditingFinished(); + void slotFetchProvidersFinished(const QJsonDocument &json, int statusCode); + void slotSearchForProviderFinished(const QJsonDocument &json, int statusCode); + +private: + QMap<QString, UnifiedSearchProvider> _providers; + QVector<UnifiedSearchResult> _results; + + QString _searchTerm; + QString _errorString; + + QString _currentFetchMoreInProgressProviderId; + + QMap<QString, QMetaObject::Connection> _searchJobConnections; + + QTimer _unifiedSearchTextEditingFinishedTimer; + + AccountState *_accountState = nullptr; +}; +} |