File indexing completed on 2024-05-12 15:44:45

0001 /*
0002     SPDX-FileCopyrightText: 2011 Aaron Seigo <aseigo@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef RUNNERMODEL_H
0008 #define RUNNERMODEL_H
0009 
0010 #include <QAbstractListModel>
0011 #include <QStringList>
0012 
0013 #include <KRunner/QueryMatch>
0014 
0015 namespace Plasma
0016 {
0017 class RunnerManager;
0018 class QueryMatch;
0019 } // namespace Plasma
0020 
0021 class QTimer;
0022 
0023 /**
0024  * This model provides bindings to use KRunner from QML
0025  *
0026  * @author Aaron Seigo <aseigo@kde.org>
0027  */
0028 class RunnerModel : public QAbstractListModel
0029 {
0030     Q_OBJECT
0031 
0032     /**
0033      * @property string set the KRunner query
0034      */
0035     Q_PROPERTY(QString query WRITE scheduleQuery READ currentQuery NOTIFY queryChanged)
0036 
0037     /**
0038      * @property Array The list of all allowed runner plugins that will be executed
0039      */
0040     Q_PROPERTY(QStringList runners WRITE setRunners READ runners NOTIFY runnersChanged)
0041 
0042     /**
0043      * @property int The number of rows of the model
0044      */
0045     Q_PROPERTY(int count READ count NOTIFY countChanged)
0046 
0047     /**
0048      * @property bool running: true when queries are in execution
0049      */
0050     Q_PROPERTY(bool running READ isRunning NOTIFY runningChanged)
0051 
0052 public:
0053     /**
0054      * @enum Roles of the model, they will be accessible from delegates
0055      */
0056     enum Roles {
0057         Type = Qt::UserRole + 1,
0058         Label,
0059         Icon,
0060         Relevance,
0061         Data,
0062         Id,
0063         SubText,
0064         Enabled,
0065         RunnerId,
0066         RunnerName,
0067         Actions,
0068     };
0069 
0070     explicit RunnerModel(QObject *parent = nullptr);
0071     QHash<int, QByteArray> roleNames() const override;
0072 
0073     QString currentQuery() const;
0074 
0075     QStringList runners() const;
0076     void setRunners(const QStringList &allowedRunners);
0077 
0078     Q_SCRIPTABLE void run(int row);
0079 
0080     bool isRunning() const;
0081 
0082     int rowCount(const QModelIndex &) const override;
0083     int count() const;
0084     QVariant data(const QModelIndex &, int) const override;
0085 
0086 public Q_SLOTS:
0087     void scheduleQuery(const QString &query);
0088 
0089 Q_SIGNALS:
0090     void queryChanged();
0091     void countChanged();
0092     void runnersChanged();
0093     void runningChanged(bool running);
0094 
0095 private Q_SLOTS:
0096     void startQuery();
0097 
0098 private:
0099     bool createManager();
0100 
0101 private Q_SLOTS:
0102     void matchesChanged(const QList<Plasma::QueryMatch> &matches);
0103     void queryHasFinished();
0104 
0105 private:
0106     Plasma::RunnerManager *m_manager = nullptr;
0107     QList<Plasma::QueryMatch> m_matches;
0108     QStringList m_pendingRunnersList;
0109     QString m_singleRunnerId;
0110     QString m_pendingQuery;
0111     QTimer *const m_startQueryTimer;
0112     QTimer *const m_runningChangedTimeout;
0113     bool m_running = false;
0114 };
0115 
0116 #endif