File indexing completed on 2024-12-22 05:15:22

0001 /*
0002     SPDX-FileCopyrightText: 2012 Aurélien Gâteau <agateau@kde.org>
0003     SPDX-FileCopyrightText: 2014 Eike Hein <hein@kde.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #pragma once
0009 
0010 #include "abstractmodel.h"
0011 
0012 #include <KConfigWatcher>
0013 #include <KRunner/QueryMatch>
0014 #include <KSharedConfig>
0015 #include <QAbstractListModel>
0016 #include <QTimer>
0017 
0018 namespace KRunner
0019 {
0020 class RunnerManager;
0021 }
0022 
0023 class AbstractModel;
0024 class RunnerMatchesModel;
0025 
0026 class RunnerModel : public QAbstractListModel
0027 {
0028     Q_OBJECT
0029 
0030     Q_PROPERTY(int count READ count NOTIFY countChanged)
0031     Q_PROPERTY(AbstractModel *favoritesModel READ favoritesModel WRITE setFavoritesModel NOTIFY favoritesModelChanged)
0032     Q_PROPERTY(QObject *appletInterface READ appletInterface WRITE setAppletInterface NOTIFY appletInterfaceChanged)
0033     Q_PROPERTY(QStringList runners READ runners WRITE setRunners NOTIFY runnersChanged)
0034     Q_PROPERTY(QString query READ query WRITE setQuery NOTIFY queryChanged)
0035     Q_PROPERTY(bool mergeResults READ mergeResults WRITE setMergeResults NOTIFY mergeResultsChanged)
0036 
0037 public:
0038     explicit RunnerModel(QObject *parent = nullptr);
0039     ~RunnerModel() override;
0040 
0041     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0042 
0043     QHash<int, QByteArray> roleNames() const override;
0044     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0045     int count() const;
0046 
0047     Q_INVOKABLE RunnerMatchesModel *modelForRow(int row);
0048 
0049     QStringList runners() const;
0050     void setRunners(const QStringList &runners);
0051 
0052     QString query() const;
0053     void setQuery(const QString &query);
0054 
0055     AbstractModel *favoritesModel() const;
0056     void setFavoritesModel(AbstractModel *model);
0057 
0058     QObject *appletInterface() const;
0059     void setAppletInterface(QObject *appletInterface);
0060 
0061     bool mergeResults() const;
0062     void setMergeResults(bool merge);
0063 
0064 Q_SIGNALS:
0065     void countChanged() const;
0066     void favoritesModelChanged() const;
0067     void appletInterfaceChanged() const;
0068     void runnersChanged() const;
0069     void queryChanged() const;
0070     void queryFinished();
0071     void mergeResultsChanged() const;
0072     void requestUpdateQuery(const QString &query);
0073 
0074 private Q_SLOTS:
0075     void startQuery();
0076 
0077 private:
0078     void clear();
0079     // Initializes the models when the first query is run or mergeResults changed
0080     void initializeModels();
0081 
0082     AbstractModel *m_favoritesModel = nullptr;
0083     QObject *m_appletInterface = nullptr;
0084     QStringList m_runners;
0085     QList<RunnerMatchesModel *> m_models;
0086     QString m_query;
0087     QTimer m_queryTimer;
0088     bool m_mergeResults;
0089     int m_queryingModels = 0;
0090     KSharedConfigPtr m_krunnerConfig;
0091     KConfigWatcher::Ptr m_configWatcher;
0092     QStringList m_favoritePluginIds;
0093 };