2
Copyright 2011 Aaron Seigo <aseigo@kde.org>
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Library General Public
6
License as published by the Free Software Foundation; either
7
version 2 of the License, or (at your option) any later version.
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Library General Public License for more details.
14
You should have received a copy of the GNU Library General Public License
15
along with this library; see the file COPYING.LIB. If not, write to
16
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
Boston, MA 02110-1301, USA.
20
#include "runnermodel.h"
28
#include <Plasma/RunnerManager>
30
RunnerModel::RunnerModel(QObject *parent)
31
: QAbstractListModel(parent),
33
m_startQueryTimer(new QTimer(this)),
34
m_runningChangedTimeout(new QTimer(this)),
37
QHash<int, QByteArray> roles;
38
roles.insert(Qt::DisplayRole, "label");
39
roles.insert(Qt::DecorationRole, "icon");
40
roles.insert(Type, "type");
41
roles.insert(Relevance, "relevance");
42
roles.insert(Data, "data");
43
roles.insert(Id, "id");
44
roles.insert(SubText, "description");
45
roles.insert(Enabled, "enabled");
46
roles.insert(RunnerId, "runnerid");
47
roles.insert(RunnerName, "runnerName");
48
roles.insert(Actions, "actions");
51
m_startQueryTimer->setSingleShot(true);
52
m_startQueryTimer->setInterval(10);
53
connect(m_startQueryTimer, SIGNAL(timeout()), this, SLOT(startQuery()));
55
//FIXME: HACK: some runners stay in a running but finished state, not possible to say if it's actually over
56
m_runningChangedTimeout->setSingleShot(true);
57
connect(m_runningChangedTimeout, SIGNAL(timeout()), this, SLOT(queryHasFinished()));
60
int RunnerModel::rowCount(const QModelIndex& index) const
62
return index.isValid() ? 0 : m_matches.count();
65
int RunnerModel::count() const
67
return m_matches.count();
70
QStringList RunnerModel::runners() const
72
return m_manager ? m_manager->allowedRunners() : m_pendingRunnersList;
75
void RunnerModel::setRunners(const QStringList &allowedRunners)
77
//use sets to ensure comparison is order-independent
78
if (allowedRunners.toSet() == runners().toSet()) {
82
m_manager->setAllowedRunners(allowedRunners);
84
//automagically enter single runner mode if there's only 1 allowed runner
85
m_manager->setSingleMode(allowedRunners.count() == 1);
87
m_pendingRunnersList = allowedRunners;
88
kDebug() << "runners set" << m_pendingRunnersList.count();
91
// to trigger single runner fun!
92
if (allowedRunners.count() == 1) {
93
m_singleRunnerId = allowedRunners.first();
94
scheduleQuery(QString());
96
m_singleRunnerId.clear();
98
emit runnersChanged();
101
void RunnerModel::run(int index)
103
if (index >= 0 && index < m_matches.count()) {
104
m_manager->run(m_matches.at(index));
108
bool RunnerModel::isRunning() const
113
QVariant RunnerModel::data(const QModelIndex &index, int role) const
115
if (!index.isValid() || index.parent().isValid() ||
116
index.column() > 0 || index.row() < 0 || index.row() >= m_matches.count()) {
117
// index requested must be valid, but we have no child items!
118
//kDebug() << "invalid index requested";
122
if (role == Qt::DisplayRole) {
123
return m_matches.at(index.row()).text();
124
} else if (role == Qt::DecorationRole) {
125
return m_matches.at(index.row()).icon();
126
} else if (role == Type) {
127
return m_matches.at(index.row()).type();
128
} else if (role == Relevance) {
129
return m_matches.at(index.row()).relevance();
130
} else if (role == Data) {
131
return m_matches.at(index.row()).data();
132
} else if (role == Id) {
133
return m_matches.at(index.row()).id();
134
} else if (role == SubText) {
135
return m_matches.at(index.row()).subtext();
136
} else if (role == Enabled) {
137
return m_matches.at(index.row()).isEnabled();
138
} else if (role == RunnerId) {
139
return m_matches.at(index.row()).runner()->id();
140
} else if (role == RunnerName) {
141
return m_matches.at(index.row()).runner()->name();
142
} else if (role == Actions) {
143
QVariantList actions;
144
Plasma::QueryMatch amatch = m_matches.at(index.row());
145
QList<QAction*> theactions = m_manager->actionsForMatch(amatch);
146
foreach(QAction* action, theactions) {
147
actions += qVariantFromValue<QObject*>(action);
155
QString RunnerModel::currentQuery() const
157
return m_manager ? m_manager->query() : QString();
160
void RunnerModel::scheduleQuery(const QString &query)
162
m_pendingQuery = query;
163
m_startQueryTimer->start();
166
void RunnerModel::startQuery()
168
// avoid creating a manager just so we can run nothing
169
// however, if we have one pending runner, then we'll be in single query mode
170
// and a null query is a valid query
171
if (!m_manager && m_pendingRunnersList.count() != 1 && m_pendingQuery.isEmpty()) {
175
//kDebug() << "!!!!!!!!!!!!!" << m_pendingQuery << m_manager;
177
if (createManager() || m_pendingQuery != m_manager->query()) {
178
//kDebug() << "running query" << m_pendingQuery << m_manager;
179
m_manager->launchQuery(m_pendingQuery, m_singleRunnerId);
182
emit runningChanged(true);
186
bool RunnerModel::createManager()
189
m_manager = new Plasma::RunnerManager(this);
190
connect(m_manager, SIGNAL(matchesChanged(QList<Plasma::QueryMatch>)),
191
this, SLOT(matchesChanged(QList<Plasma::QueryMatch>)));
192
connect(m_manager, SIGNAL(queryFinished()),
193
this, SLOT(queryHasFinished()));
195
if (!m_pendingRunnersList.isEmpty()) {
196
setRunners(m_pendingRunnersList);
197
m_pendingRunnersList.clear();
199
//connect(m_manager, SIGNAL(queryFinished()), this, SLOT(queryFinished()));
206
void RunnerModel::matchesChanged(const QList<Plasma::QueryMatch> &matches)
208
//kDebug() << "got matches:" << matches.count();
209
bool fullReset = false;
210
int oldCount = m_matches.count();
211
int newCount = matches.count();
212
if (newCount > oldCount) {
213
// We received more matches than we had. If all common matches are the
214
// same, we can just append new matches instead of resetting the whole
216
for (int row = 0; row < oldCount; ++row) {
217
if (!(m_matches.at(row) == matches.at(row))) {
223
// Not a full reset, inserting rows
224
beginInsertRows(QModelIndex(), oldCount, newCount);
239
m_runningChangedTimeout->start(3000);
242
void RunnerModel::queryHasFinished()
245
emit runningChanged(false);
248
#include "runnermodel.moc"